-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.py
213 lines (166 loc) · 5.91 KB
/
main2.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from facial_dataset import FacialDataset
from transform import Rescale, ToTensor, Normalize, RandomCrop
from torchvision import transforms, models
import matplotlib.pyplot as plt
from cnn import Net
import torch
import numpy as np
import cv2
from torch.utils.data import DataLoader
import torch.optim as optim
from transform import *
from collections import OrderedDict
def getImage(image):
return np.transpose(image,(1,2,0)).numpy().squeeze(2)
def reprocessing(image, key_pts, key_pts_2):
return np.transpose(image,(0,2,3,1)).numpy().squeeze(3), key_pts.data * 50.0 + 100, key_pts_2.data * 50.0 + 100
def loadModel(model, optimizer):
model_dir = 'SavedModels/'
model_name = 'keypoints_model_6_Loss_transfer.pt'
check_point = torch.load(model_dir+model_name)
model.load_state_dict(check_point['model_state'])
optimizer.load_state_dict(check_point['optimizer_state'])
return model, optimizer
def saveModel(model, optimizer):
model_dir = 'SavedModels/'
model_name = 'keypoints_model_6_Loss_transfer.pt'
torch.save({'model_state':model.state_dict(), 'optimizer_state':optimizer.state_dict()}, model_dir+model_name)
def display(image, key_points):
plt.imshow(image, cmap='gray')
plt.scatter(key_points[:,0], key_points[:,1])
plt.show()
def model_output(model, testLoader, batch_size):
for idx, sample in enumerate(testLoader):
images = sample['image']
key_pts = sample['keypoints']
images = images.type(torch.FloatTensor)
out = model(images)
out = out.view(batch_size, -1, 2)
if idx == 0:
return images, out, key_pts
def train(model, optimizer, epoch, critirion, train_loader, testLoader, batch_size):
if torch.cuda.is_available():
model = model.cuda()
least_cost = 0.003532963704087378
for epoch in range(epoch):
train_loss = 0
test_loss = 0
model.train()
for sample in trainLoader:
images = sample['image']
target = sample['keypoints']
#flatting
target = target.view(target.size(0), -1)
target = target.type(torch.FloatTensor)
images = images.type(torch.FloatTensor)
if torch.cuda.is_available():
images = images.cuda()
target = target.cuda()
out = model(images)
cost = critirion(out, target)
optimizer.zero_grad()
cost.backward()
optimizer.step()
train_loss += cost.item()
else:
model.eval()
for sample in testLoader:
images = sample['image']
target = sample['keypoints']
#flatting
target = target.view(target.size(0), -1)
target = target.type(torch.FloatTensor)
images = images.type(torch.FloatTensor)
if torch.cuda.is_available():
images = images.cuda()
target = target.cuda()
out = model(images)
cost = critirion(out, target)
test_loss += cost.item()
train_loss = train_loss/len(trainLoader)
test_loss = test_loss/len(testLoader)
if test_loss < least_cost:
least_cost = test_loss
saveModel(model, optimizer)
print('epoch: {} Tloss: {} test LOss: {}'.format(epoch+1, train_loss, test_loss))
def test(model, testLoader, batch_size, im_num, savedModel=True):
image, out, key_pts = model_output(model, testLoader, batch_size)
image, out, key_pts = reprocessing(image, out, key_pts )
for idx in range(im_num):
display(image[idx], out.data[idx])
display(image[idx], key_pts.data[idx])
def feature_visualization(weights, image, depth):
fig = plt.figure(figsize=(20,8))
el = depth
depth = np.sqrt(depth)
row = np.round(depth).astype(int) if depth - np.round(depth) == 0 else (np.round(depth) + 1).astype(int)
column = row * 2
for i in range(0,el):
fig.add_subplot(row,column,(i*2)+1)
plt.imshow(weights[i][0], cmap='gray')
fig.add_subplot(row,column,(i*2)+2)
plt.imshow(cv2.filter2D(image, -1, weights[i][0]), cmap='gray')
plt.show()
#=================================================================
csv_dir_train = 'data/training_frames_keypoints.csv'
root_dir_train = 'data/training/'
csv_dir_test = 'data/test_frames_keypoints.csv'
root_dir_test = 'data/test/'
batch_size = 8
transform = transforms.Compose([
RandomRotation(15),
Rescale(224),
RandomCrop(223),
Normalize(),
ToTensor()
])
trainDataset = FacialDataset(csv_dir_train, root_dir_train, transform)
testDataset = FacialDataset(csv_dir_test, root_dir_test, transform)
'''
sample = trainDataset[0]
angle = 22.5
sample_x = transform(sample)
display(sample_x['image'], sample_x['keypoints'])
print(sample['keypoints'][0][1])
'''
trainLoader = DataLoader(trainDataset, batch_size=batch_size, shuffle=True, num_workers=0)
testLoader = DataLoader(testDataset, batch_size=1, shuffle=True, num_workers=0)
#Model
model = models.densenet161(pretrained=False)
model.features.conv0 = torch.nn.Conv2d(1, 96, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
classifier = torch.nn.Sequential(OrderedDict([
('fc1', torch.nn.Linear(2208, 136)),
]))
model.classifier = classifier
critirion = torch.nn.SmoothL1Loss()
optimizer = optim.Adam(model.parameters(), lr=0.0001)
epoch = 1000
#model.cuda()
model, optimizer = loadModel(model, optimizer)
#train(epoch=epoch, train_loader=trainLoader, optimizer= optimizer, critirion=critirion, model=model, testLoader= testLoader, batch_size= batch_size)
test(model=model, testLoader=testLoader, batch_size=batch_size, im_num=12)
#model = loadModel(model)
#weights = model.conv2.weight.data.numpy()
#feature_visualization(weights=weights, image=getImage(iter(testLoader).next()['image'][0]), depth =32)
### Test
def read_image(dir):
transform = transforms.Compose([
Rescale(224),
RandomCrop(223),
Normalize(),
ToTensor()
])
image = cv2.imread(dir)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
sample = {'image': image, 'keypoints': None}
transformed_image = transform(sample)
images = transformed_image['image']
print(images.shape)
images = images.type(torch.FloatTensor)
images = images.unsqueeze(0)
out = model(images)
out = out.view(-1, 2)
out = out.data * 50.0 + 100
images = np.transpose(images,(0,2,3,1)).numpy().squeeze(3)
display(images.squeeze(0), out)
read_image('mohamed.jpg')