-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevel.py
More file actions
196 lines (174 loc) · 7.14 KB
/
devel.py
File metadata and controls
196 lines (174 loc) · 7.14 KB
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
import torch
import os
import cv2
import argparse
import torch.nn as nn
import torch.nn.functional as F
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as utils
import numpy as np
from torch.autograd import Variable
from torch import optim
from torch.utils.data import Dataset, DataLoader
from PIL import Image
# Parsing arguments
parser = argparse.ArgumentParser(description='Face recognition using BCELoss.')
parser.add_argument('--load', type = str, help = 'Using trained parameter to test on both train and test sets.')
parser.add_argument('--save', type = str, help = 'Train the model using splitting file provided.')
args = parser.parse_args()
# Define dataset class
class FaceDateSet(Dataset):
"""lfw face data set."""
def __init__(self, root_dir, split_file, transform = None):
self.root_dir = root_dir
self.split_file = split_file
self.transform = transform
self.img_paths = self.parse_files()
def __len__(self):
return len(self.img_paths)
def __getitem__(self, idx):
# Get items from path here
img1_path = os.path.join(self.root_dir, self.img_paths[idx][0])
img2_path = os.path.join(self.root_dir, self.img_paths[idx][1])
img_label = map(float,self.img_paths[idx][2])
img_label = torch.from_numpy(np.array(img_label)).float()
img1 = Image.open(img1_path)
img2 = Image.open(img2_path)
img1 = img1.convert('RGB')
img2 = img2.convert('RGB')
if self.transform is not None:
img1 = self.transform(img1)
img2 = self.transform(img2)
sample = {'img1': img1, 'img2': img2, 'label': img_label}
return sample
def parse_files(self):
img_paths = []
with open(self.split_file) as f:
img_paths = f.readlines()
img_paths = [x.split() for x in img_paths]
return img_paths
# Define deep neural network
class SiameseNet(nn.Module):
def __init__(self):
super(SiameseNet, self).__init__()
self.nn1 = nn.Sequential(
nn.Conv2d(3,64,5,padding=2),
nn.ReLU(inplace=True),
nn.BatchNorm2d(64),
nn.MaxPool2d(2,stride=2),
nn.Conv2d(64,128,5,padding=2),
nn.ReLU(inplace=True),
nn.BatchNorm2d(128),
nn.MaxPool2d(2,stride=2),
nn.Conv2d(128,256,3,padding=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(256),
nn.MaxPool2d(2,stride=2),
nn.Conv2d(256,512,3,padding=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(512),
)
self.nn2 = nn.Sequential(
nn.Linear(131072,1024),
nn.ReLU(inplace=True),
nn.BatchNorm2d(1024),
)
self.nn3 = nn.Sequential(
nn.Linear(2048, 1),
nn.Sigmoid()
)
def net_forward(self,x):
temp = self.nn1(x)
temp = temp.view(temp.size()[0], -1)
output = self.nn2(temp)
return output
def forward(self,x1,x2):
output1 = self.net_forward(x1)
output2 = self.net_forward(x2)
output12 = torch.cat((output1,output2),1)
output = self.nn3(output12)
return output
# Switch to training
if args.save != None:
weights_dir = args.save
# Training process setup
data_trans = transforms.Compose([transforms.Scale((128,128)),transforms.ToTensor()])
face_train = FaceDateSet(root_dir='lfw', split_file='train.txt', transform = data_trans)
train_loader = DataLoader(face_train, batch_size=16, shuffle=True, num_workers=4)
# Training the net
net = SiameseNet().cuda()
optimizer = optim.Adam(net.parameters(), lr = 1e-6)
loss_fn = nn.BCELoss()
total_epoch = 40
counter = []
loss_history = []
iteration = 0
for epoch in range(total_epoch):
for batch_idx, batch_sample in enumerate(train_loader):
img1 = batch_sample['img1']
img2 = batch_sample['img2']
label = batch_sample['label']
# label = label.view(label.numel(),-1)
img1, img2, y = Variable(img1).cuda(), Variable(img2).cuda(), Variable(label).cuda()
optimizer.zero_grad()
y_pred = net(img1, img2)
bce_loss = loss_fn(y_pred, y)
bce_loss.backward()
optimizer.step()
if batch_idx % 20 == 0:
print "Epoch %d, Batch %d Loss %f" % (epoch, batch_idx, bce_loss.data[0])
iteration += 20
counter.append(iteration)
loss_history.append(bce_loss.data[0])
# Save the trained network
torch.save(net.state_dict(), weights_dir)
plt.plot(counter,loss_history)
plt.show()
# Switching to testing
elif args.load != None:
if os.path.isfile(args.load):
weights_dir = args.load
net = SiameseNet().cuda()
net.load_state_dict(torch.load(weights_dir))
loss_fn = nn.BCELoss()
# Testing on the training data
data_trans1 = transforms.Compose([transforms.ToPILImage(),transforms.Scale((128,128)),transforms.ToTensor()])
face_test1 = FaceDateSet(root_dir='lfw', split_file='train.txt', transform = data_trans1)
test1_loader = DataLoader(face_test1, batch_size=1, shuffle=False)
total_loss = 0.0
for batch_idx, batch_sample in enumerate(test1_loader):
img1 = batch_sample['img1']
img2 = batch_sample['img2']
label = batch_sample['label'].float()
label = label.view(label.numel(),-1)
img1, img2, y = Variable(img1).cuda(), Variable(img2).cuda(), Variable(label).cuda()
y_pred = net(img1, img2)
bce_loss = loss_fn(y_pred, y)
if batch_idx % int(len(face_test1)/10) == 0:
print "Batch %d Loss %f" % (batch_idx, bce_loss.data[0])
total_loss += bce_loss.data[0]
mean_loss = total_loss / float(len(face_test1))
print "Average BCE loss on training data is: ", mean_loss
# Testing on the testing data
data_trans2 = transforms.Compose([transforms.ToPILImage(),transforms.Scale((128,128)),transforms.ToTensor()])
face_test2 = FaceDateSet(root_dir='lfw', split_file='test.txt', transform = data_trans2)
test2_loader = DataLoader(face_test2, batch_size=1, shuffle=False)
total_loss = 0.0
for batch_idx, batch_sample in enumerate(test2_loader):
img1 = batch_sample['img1']
img2 = batch_sample['img2']
label = batch_sample['label'].float()
label = label.view(label.numel(),-1)
img1, img2, y = Variable(img1).cuda(), Variable(img2).cuda(), Variable(label).cuda()
y_pred = net(img1, img2)
bce_loss = loss_fn(y_pred, y)
if batch_idx % int(len(face_test1)/10) == 0:
print "Batch %d Loss %f" % (batch_idx, bce_loss.data[0])
total_loss += bce_loss.data[0]
mean_loss = total_loss / float(len(face_test1))
print "Average BCE loss on testing data is: ", mean_loss
else:
print "Parameter file does not exist!"
else:
print "Please use [-h] for help on the usage."