-
Notifications
You must be signed in to change notification settings - Fork 3
/
embed_all.py
40 lines (27 loc) · 1.08 KB
/
embed_all.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import copy
train_dataloader_IMG = copy.deepcopy(train_dataloader)
valid_dataloader_IMG = copy.deepcopy(valid_dataloader)
RN50_model.fc = nn.Identity()
train_embedding = torch.Tensor()
train_Ys = torch.Tensor()
with torch.no_grad():
for X,y in train_dataloader_IMG:
embedding = RN50_model(X.to(device)).to("cpu")
train_embedding = torch.concat((train_embedding,embedding))
train_Ys = torch.concat((train_Ys,y))
print( train_embedding.shape )
print( train_Ys.shape )
valid_embedding = torch.Tensor()
valid_Ys = torch.Tensor()
with torch.no_grad():
for X,y in valid_dataloader_IMG:
embedding = RN50_model(X.to(device)).to("cpu")
valid_embedding = torch.concat((valid_embedding,embedding))
valid_Ys = torch.concat((valid_Ys,y))
print( valid_embedding.shape )
print( valid_Ys.shape )
## saving to a file
torch.save(train_embedding, 'data/chest_Xray_train_embed.pt')
torch.save(train_Ys, 'data/chest_Xray_train_y.pt')
torch.save(valid_embedding, 'data/chest_Xray_valid_embed.pt')
torch.save(valid_Ys, 'data/chest_Xray_valid_y.pt')