-
Notifications
You must be signed in to change notification settings - Fork 1
/
flower_dnn.py
158 lines (128 loc) · 5.81 KB
/
flower_dnn.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
153
154
155
156
import numpy as np
import matplotlib.pyplot as plt
import torch
from PIL import Image
from torchvision import datasets, transforms
# load flower dataset
class dataset(torch.utils.data.Dataset):
def __init__(self, train, data_index, transforms):
super().__init__()
self.train = train
self.data_index = data_index
self.transforms = transforms
def __len__(self):
return len(self.data_index)
def __getitem__(self, index):
img_id = self.data_index[index]
num = str(self.data_index[index]+1)
if len(num) < 4:
diff = 4 - len(num)
for i in range(diff):
num = '0' + num
path = "flower_data/jpg/image_" + num + ".jpg"
picture = Image.open(path)
label = img_id // 80
picture = self.transforms(picture)
return picture, label
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.507, 0.487, 0.441], std=[0.267, 0.256, 0.276]),
transforms.Resize((224, 224))
]
)
index = np.random.permutation(np.arange(1360))
train_index = index[:1020]
test_index = index[1020:]
trainset = dataset(train=True, data_index=train_index, transforms=transform)
testset = dataset(train=False, data_index=test_index, transforms=transform)
# define the network
# load resnet18
from torchvision import models
resnet18 = models.resnet18(weights=models.ResNet18_Weights.IMAGENET1K_V1)
resnet34 = models.resnet34(weights=models.ResNet34_Weights.IMAGENET1K_V1)
resnet50 = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V1)
vgg16 = models.vgg16(weights=models.VGG16_Weights.IMAGENET1K_V1)
mobileNet = models.mobilenet_v2(weights=models.MobileNet_V2_Weights.IMAGENET1K_V1)
# freeze the parameters
for param in resnet18.parameters():
param.requires_grad = False
for param in resnet34.parameters():
param.requires_grad = False
for param in resnet50.parameters():
param.requires_grad = False
for param in vgg16.parameters():
param.requires_grad = False
for param in mobileNet.parameters():
param.requires_grad = False
# remove the last average pooling layer
resnet18 = torch.nn.Sequential(*(list(resnet18.children())[:-1]))
resnet34 = torch.nn.Sequential(*(list(resnet34.children())[:-1]))
resnet50 = torch.nn.Sequential(*(list(resnet50.children())[:-1]))
vgg16 = torch.nn.Sequential(*(list(vgg16.children())[:-1]))
mobileNet = torch.nn.Sequential(*(list(mobileNet.children())[:-1]))
# Write all the layers in the network to file
with open('model_info.txt', 'w') as f:
f.write('ResNet18')
f.write(str(resnet18))
f.write('\n\n ResNet34')
f.write(str(resnet34))
f.write('\n\n ResNet50')
f.write(str(resnet50))
f.write('\n\n VGG16')
f.write(str(vgg16))
f.write('\n MobileNet')
f.write(str(mobileNet))
# test each of the models
from torch.utils.data import DataLoader
from tqdm import tqdm
from sklearn.metrics import accuracy_score
def test_model(model, trainset):
model.eval()
testloader = DataLoader(trainset, batch_size=128, shuffle=True)
output_total = []
labels_total = []
for images, labels in tqdm(testloader):
images, labels = images.cuda(), labels.cuda()
output = model(images)
output = output.cpu().detach().numpy()
output_total.append(output)
labels_total.append(labels.cpu().detach().numpy())
output_total = np.concatenate(output_total, axis=0)
labels = np.concatenate(labels_total, axis=0)
return output_total, labels
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--max_pool', type=int, default=0)
max_pool = parser.parse_args().max_pool
models = ['resnet18', 'resnet34', 'resnet50', 'vgg16', 'mobileNet']
for model in models:
net = globals()[model].cuda()
train_output, train_labels = test_model(net, trainset)
test_output, test_labels = test_model(net, testset)
# bilinear interpolation seems to be better used for dl model input, not output
# do max pooling to reduce the dimension
if max_pool == 1:
train_output = train_output.reshape(train_output.shape[0], -1)
test_output = test_output.reshape(test_output.shape[0], -1)
if model == 'resnet18' or model == 'resnet34': # (50000, 512, 1, 1)
train_output = torch.nn.functional.max_pool1d(torch.from_numpy(train_output), kernel_size=2)
test_output = torch.nn.functional.max_pool1d(torch.from_numpy(test_output), kernel_size=2)
elif model == 'resnet50': # (50000, 2048, 1, 1)
train_output = torch.nn.functional.max_pool1d(torch.from_numpy(train_output), kernel_size=8)
test_output = torch.nn.functional.max_pool1d(torch.from_numpy(test_output), kernel_size=8)
elif model == 'vgg16': # (50000, 512, 7, 7)
train_output = torch.nn.functional.max_pool1d(torch.from_numpy(train_output), kernel_size=98)
test_output = torch.nn.functional.max_pool1d(torch.from_numpy(test_output), kernel_size=98)
elif model == 'mobileNet': # (50000, 1280, 1, 1)
train_output = torch.nn.functional.max_pool1d(torch.from_numpy(train_output), kernel_size=5)
test_output = torch.nn.functional.max_pool1d(torch.from_numpy(test_output), kernel_size=5)
# convert it back to 50000 x 256 x 1 x 1
train_output = train_output.numpy()
test_output = test_output.numpy()
train_output = train_output.reshape(1020, 256, 1, 1)
test_output = test_output.reshape((1360-1020), 256, 1, 1)
np.save(f'train_{model}_data.npy', train_output)
np.save(f'train_{model}_labels.npy', train_labels)
np.save(f"test_{model}_data.npy", test_output)
np.save(f"test_{model}_labels.npy", test_labels)