-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocess.py
152 lines (131 loc) · 6.03 KB
/
preprocess.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import os
import torch
from torch.utils.data import DataLoader
from datasets.mini_imagenet import MiniImageNet
from datasets.tiered_imagenet import TieredImageNet
from datasets.cifarfs import CIFAR_FS
from datasets.fc100 import FC100
from datasets.omniglot import OmniglotDataset
from utils import seed_torch, set_gpu
from efnet import returnCusModel
def preprocess(args):
if args.dataset == "mini":
splits = ['train', 'val', 'test']
model = returnCusModel(900).cuda()
model.load_state_dict(torch.load('efficientnetb0-900-based-max.pth'))
model.eval()
with torch.no_grad():
for split in splits:
trainset = MiniImageNet(split=split, size=args.size)
train_loader = DataLoader(dataset=trainset, batch_size=1, shuffle=False, num_workers=0)
datafile = {
'data': [],
'label': []
}
for i, batch in enumerate(train_loader, 1):
data, label = batch
f = model(data.cuda(), is_feat=True)
datafile['data'].append(f.squeeze(0).cpu())
datafile['label'].append(label.item())
print(i, "sample")
torch.save(datafile, os.path.join(args.save_path, split+'_file.pt'))
elif args.dataset == "tiered":
splits = ['train', 'val', 'test']
model = returnCusModel(392).cuda()
model.load_state_dict(torch.load('efficientnetb0-392-based-max.pth'))
model.eval()
with torch.no_grad():
for split in splits:
data_set = TieredImageNet(split=split, size=args.size)
data_loader = DataLoader(dataset=data_set, batch_size=1, shuffle=False, num_workers=0)
datafile = {
'data': [],
'label': []
}
for i, batch in enumerate(data_loader, 1):
data, label = batch
f = model(data.cuda(), is_feat=True)
datafile['data'].append(f.squeeze(0).cpu())
datafile['label'].append(label.item())
print(i, "sample")
torch.save(datafile, os.path.join(args.save_path, split+'_file.pt'))
elif args.dataset == "cifarfs":
splits = ['train', 'val', 'test']
model = returnCusModel().cuda()
model.load_state_dict(torch.load('efficientnetb0-full-based-max.pth'))
model.eval()
with torch.no_grad():
for split in splits:
data_set = CIFAR_FS(split=split, size=args.size)
data_loader = DataLoader(dataset=data_set, batch_size=1, shuffle=False, num_workers=0)
datafile = {
'data': [],
'label': []
}
for i, batch in enumerate(data_loader, 1):
data, label = batch
f = model(data.cuda(), is_feat=True)
datafile['data'].append(f.squeeze(0).cpu())
datafile['label'].append(label.item())
print(i, "sample")
torch.save(datafile, os.path.join(args.save_path, split+'_file.pt'))
elif args.dataset == "fc100":
splits = ['train', 'val', 'test']
model = returnCusModel().cuda()
model.load_state_dict(torch.load('efficientnetb0-full-based-max.pth'))
model.eval()
with torch.no_grad():
for split in splits:
data_set = FC100(split=split, size=args.size)
data_loader = DataLoader(dataset=data_set, batch_size=1, shuffle=False, num_workers=0)
datafile = {
'data': [],
'label': []
}
for i, batch in enumerate(data_loader, 1):
data, label = batch
f = model(data.cuda(), is_feat=True)
datafile['data'].append(f.squeeze(0).cpu())
datafile['label'].append(label.item())
print(i, "sample")
torch.save(datafile, os.path.join(args.save_path, split+'_file.pt'))
elif args.dataset == "omniglot":
print("Omniglot...")
model = returnCusModel().cuda()
model.load_state_dict(torch.load('efficientnetb0-full-based-max.pth'))
model.eval()
with torch.no_grad():
splits = ['trainval', 'test']
for split in splits:
datafile = {
'data': [],
'label': []
}
data_set = OmniglotDataset(mode=split)
data_loader = DataLoader(dataset=data_set, batch_size=1, shuffle=False, num_workers=0)
for i, data in enumerate(data_loader, 1):
x, y = data
x = torch.cat((x,x,x), 1)
x = model(x.cuda(), is_feat=True)
datafile['data'].append(x.cpu())
datafile['label'].append(y[0])
print(split, "sample", i, "with label", y[0].item())
torch.save(datafile, os.path.join(args.save_path, split+"_file.pt"))
print("Completed", split, "set...")
else:
print("Invalid dataset")
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', type=str, default='1')
parser.add_argument('--seed', type=int, default=2512)
parser.add_argument('--save-path', default="./data/prefc100")
parser.add_argument('--dataset', type=str, default='fc100', choices=['omniglot','mini','tiered','cifarfs','fc100'])
parser.add_argument('--size', type=int, default=224)
args = parser.parse_args()
set_gpu(args.gpu)
seed_torch(args.seed)
if not os.path.exists(args.save_path):
os.makedirs(args.save_path)
preprocess(args)
print("Done...")