forked from clcarwin/SFD_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fine_tuning.py
222 lines (142 loc) · 5.17 KB
/
fine_tuning.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
from __future__ import print_function
import torch
import torch.optim as optim
from torch.utils.data.dataset import Dataset
import pandas as pd
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
from torch.autograd import Variable
torch.backends.cudnn.bencmark = True
import torchvision
import torchvision.transforms as transforms
import os,sys,cv2,random,datetime,time,math
import argparse
import numpy as np
from net_s3fd import *
# from s3fd import *
from bbox import *
from sklearn.preprocessing import MultiLabelBinarizer
from PIL import Image
class Joint_Loss(torch.nn.Module):
def __init__(self, w1=1, w2=2,w3=3):
super(Joint_Loss, self).__init__()
self.w1 = w1
self.w2 = w2
self.w3 = w3
def forward(self,input,target):
mse_loss = torch.nn.MSELoss()
cross_entropy_loss = torch.nn.BCELoss()
# calculate cross-entropy loss on face and gender classificaition and MSE on regression
cls_loss_face = [cross_entropy_loss(a,b) for a,b in zip(input[0], target[0])]
reg_loss = [mse_loss(a,b) for a,b in zip(input[1], target[1])]
cls_loss_gen = [cross_entropy_loss(a,b) for a,b in zip(input[2], target[2])]
total = w1*sum(cls_loss_face) + w2*sum(reg_loss) + w3*sum(cls_loss_face)
return total
class AFLW_Dataset(Dataset):
"""Dataset wrapping images and target labels
Arguments:
path to ground truth labels
path to images
image extension
PIL transforms
"""
def __init__(self, annot_csv, transform=None):
tmp_df = pd.read_csv(annot_csv)
self.mlb = MultiLabelBinarizer()
self.img_path = img_path
self.img_ext = img_ext
self.transform = transform
self.x_train = tmp_df["filepath"]
self.y_trainRect = tmp_df[["x", "y", "w", "w"]]
self.y_trainGen = self.mlb.fit_transform(tmp_df["Gender"].str.split()).astype(np.float32)
def __len__(self):
return len(self.x_train.index)
def __getitem__(self, index):
img = cv2.imread(self.x_train[index])
rect = self.y_trainRect.iloc[index]
gen = self.y_trainGen.iloc[index]
labels = [rect, gen]
sample = {'image ': image, 'labels': labels}
if self.transform:
sample = self.transform(sample)
return sample
class Rescale(object):
"""Rescale an image to a given size.
args:
output_size
"""
def __init__(self, output_size):
assert isinstance(output_size, (int, tuple))
self.output_size = output_size
def __call__(self, sample):
image, labels = sample['image'], sample['labels']
h,w = image.shape[:2]
if isinstance(self.output_size, int):
if h > w:
new_h, new_w = self.output_size * h / w, self.output_size
else:
new_h, new_w = self.output_size, self.output_size * w / h
else:
new_h, new_w = int(new_h), int(new_w)
img = cv2.resize(img, (new_h, new_w))
# relocated bounding box
labels[0][0] = labels[0][0] * (new_w/w)
labels[0][1] = labels[0][1] * (new_h/h)
labels[0][2] = labels[0][2] * (new_w/w)
labels[0][3] = labels[0][3] * (new_h/h)
return {'image': img, 'labels': labels}
class ToTensor(object):
"""converts ndarrays to Tensors"""
def __call__(self, sample):
image, labels = sample['image'], sample['labels']
image = image.transpose((2,0,1))
return {"image": torch.from_numpy(image), 'labels': torch.from_numpy(labels)}
def save(model, optimizer, loss, filename):
save_dict = {
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': loss.data[0]
}
torch.save(save_dict, filename)
train_data = "index.csv"
img_path = "/Volumes/Seagate Expansion Drive/AFLW_Images/aflw/data/flickr/0"
img_ext = ".jpg"
def train_model(model, criterion, optimizer, num_classes, num_epochs = 100):
for epoch in range(num_epochs):
model.train()
running_loss = 0.0
def load_base_layers(model, source):
pretrained_weights = torch.load(source)
full_parameters = model.state_dict()
base_parameters = {k:v for k,v in pretrained_weights.items() if k in full_parameters}
full_parameters.update(base_parameters)
model.load_state_dict(full_parameters)
def freeze_layers(model):
for param in model.parameters():
param.requires_grad = False
def main():
transformations = transforms.Compose(
[
Rescale(256),
ToTensor()
])
dataset = AFLW_Dataset('/Volumes/Seagate Expansion Drive/AFLW_Images/aflw/data/AFLW_rect.csv', transform=transformations)
for i in range(10):
sample = dataset[i]
# dataloader = DataLoader()
# num_classes = 2
# model = s3fd(num_classes)
# load_base_layers(model, 's3fd_convert.pth')
# freeze_layers(model)
# model.conv3_3_norm_mbox_gen = nn.Conv2d(256, 4, kernel_size=3, stride=1, padding=1)
# model.conv4_3_norm_mbox_gen = nn.Conv2d(512, 2, kernel_size=3, stride=1, padding=1)
# mode.conv5_3_norm_mbox_gen = nn.Conv2d(512, 2, kernel_size=3, stride=1, padding=1)
# model.fc7_mbox_gen = nn.Conv2d(1024, 2, kernel_size=3, stride=1, padding=1)
# model.conv6_2_mbox_gen = nn.Conv2d(512, 2, kernel_size=3, stride=1, padding=1)
# model.conv7_2_mbox_gen = nn.Conv2d(256, 2, kernel_size=3, stride=1, padding=1)
# optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()))
if __name__ == "__main__":
main()