-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinetune.py
216 lines (187 loc) · 8.33 KB
/
finetune.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
214
215
216
import torch
import time
import sys
import os
import pandas as pd
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import cv2
import torchvision
from torch.optim.lr_scheduler import StepLR
from torch.utils.data import DataLoader
from torch.utils.data import Dataset
import glob
from torch.quantization.quantize_fx import prepare_fx, convert_fx
from torch.quantization import default_dynamic_qconfig, float_qparams_weight_only_qconfig
class RestNetBasicBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride):
super(RestNetBasicBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1)
self.bn1 = nn.BatchNorm2d(out_channels)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=stride, padding=1)
self.bn2 = nn.BatchNorm2d(out_channels)
def forward(self, x):
output = self.conv1(x)
output = F.relu(self.bn1(output))
output = self.conv2(output)
output = self.bn2(output)
return F.relu(x + output)
class RestNetDownBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride):
super(RestNetDownBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride[0], padding=1)
self.bn1 = nn.BatchNorm2d(out_channels)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=stride[1], padding=1)
self.bn2 = nn.BatchNorm2d(out_channels)
self.extra = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride[0], padding=0),
nn.BatchNorm2d(out_channels)
)
def forward(self, x):
extra_x = self.extra(x)
output = self.conv1(x)
out = F.relu(self.bn1(output))
out = self.conv2(out)
out = self.bn2(out)
return F.relu(extra_x + out)
class ResNet18(nn.Module):
def __init__(self):
super(ResNet18, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3)
self.bn1 = nn.BatchNorm2d(64)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = nn.Sequential(RestNetBasicBlock(64, 64, 1),
RestNetBasicBlock(64, 64, 1))
self.layer2 = nn.Sequential(RestNetDownBlock(64, 128, [2, 1]),
RestNetBasicBlock(128, 128, 1))
self.layer3 = nn.Sequential(RestNetDownBlock(128, 256, [2, 1]),
RestNetBasicBlock(256, 256, 1))
self.layer4 = nn.Sequential(RestNetDownBlock(256, 512, [2, 1]),
RestNetBasicBlock(512, 512, 1))
self.avgpool = nn.AdaptiveAvgPool2d(output_size=(1, 1))
self.fc = nn.Linear(512, 2)
def forward(self, x):
with torch.no_grad():
out = self.conv1(x)
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = self.avgpool(out)
feature = out.reshape(x.shape[0], -1)
out = self.fc(feature)
return out
class Discriminator(nn.Module):
def __init__(self):
super().__init__()
self.model = nn.Sequential(
nn.Linear(512, 512),
nn.LeakyReLU(0.2, inplace=True),
nn.Dropout(0.3),
nn.Linear(512, 256),
nn.LeakyReLU(0.2, inplace=True),
nn.Dropout(0.3),
nn.Linear(256, 2),
nn.Sigmoid()
)
def forward(self, x):
x = x.view(x.size(0), 512)
out = self.model(x)
return out
class CDataset(Dataset):
def __init__(self, csv_file, transform=None):
self.data = []
for i in range(1, 10):
csv_file2 = csv_file + "/000%s/face" % (str(i))
tmp = glob.glob(os.path.join(csv_file2, '*.jpg'))
self.data += tmp
for i in range(10, 57):
csv_file2 = csv_file + "/00%s/face" % (str(i))
tmp = glob.glob(os.path.join(csv_file2, '*.jpg'))
self.data += tmp
self.transform = transform
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
label = []
image_name = self.data[idx]
image_tmp = cv2.imread(image_name)
image = torch.from_numpy(image_tmp)
image = image.transpose(0, 2).contiguous()
label_tmp = image_name.split("\'")
label_tmp = label_tmp[-1].split("_")
label1 = int(label_tmp[3].strip("V"))
label2 = int(label_tmp[2].strip("P")) + int(label_tmp[4].strip("H.jpg"))
label.append(label1 * np.pi / 180)
label.append(label2 * np.pi / 180)
label = torch.tensor(label)
type_ = torch.tensor([0, 1]) # fake image
type_g = torch.tensor([1, 0])
return image, label, type_, type_g
class MPIIDataset(Dataset):
def __init__(self, mpii_file, transform=None):
for k in range(2):
mpii_file2 = mpii_file + "/p0%s.label" % (str(k))
tmp = pd.read_csv(mpii_file2, sep=' ')
tmp = tmp[['Face', '2DGaze']]
if k == 0:
self.data = tmp
else:
self.data = pd.concat([self.data, tmp])
self.transform = transform
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
image_name = self.data.iloc[idx, 0]
image_path = 'datasets\\MPIIFaceGaze\\Image\\' + image_name
image_tmp = cv2.imread(image_path)
image_tmp = torch.from_numpy(image_tmp)
image = image_tmp.transpose(0, 2).contiguous()
label = self.data.iloc[idx, 1].split(',')
label = [float(x) for x in label]
label = torch.tensor(label)
type_ = torch.tensor([1, 0]) # real image
type_g = torch.tensor([0, 1])
return image, label, type_, type_g
if __name__ == '__main__':
annotations_file1 = r"datasets\ColumbiaGazeCutSet"
train_datasetC = CDataset(csv_file=annotations_file1)
print(len(train_datasetC))
annotations_file = "datasets/MPIIFaceGaze/Label"
train_datasetMPII = MPIIDataset(mpii_file=annotations_file)
print(len(train_datasetMPII))
train_dataset = torch.utils.data.ConcatDataset([train_datasetC, train_datasetMPII])
print(len(train_dataset))
train_dataloader = DataLoader(train_dataset, batch_size=50, shuffle=True, pin_memory=True,
persistent_workers=True, prefetch_factor=8, num_workers=8)
train_dataloaderC = DataLoader(train_datasetC, batch_size=50, shuffle=True, pin_memory=True,
persistent_workers=True, prefetch_factor=8, num_workers=8)
train_dataloaderMPII = DataLoader(train_datasetMPII, batch_size=50, shuffle=True, pin_memory=True,
persistent_workers=True, prefetch_factor=8, num_workers=8)
device = torch.device("cuda:0")
lossfunc = "MSELoss"
loss_op = getattr(nn, lossfunc)().cuda()
loss2 = nn.BCELoss().cuda()
savepath = r"E:\yuusa\code\my_mpiifacegaze\saveGAN"
myResnet = ResNet18()
myResnet.load_state_dict(torch.load(os.path.join(savepath, f"Iter_2_Resnet.pt")))
myResnet.train()
optimizer = optim.Adam(myResnet.parameters(), lr=1e-5, betas=(0.9, 0.95))
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=240 * 2, gamma=0.1)
myResnet.to(device)
myepoch = 4
for epoch in range(1, myepoch + 1):
for i, (data, label, type_, type_g) in enumerate(train_dataloaderMPII):
data = data.to(device)
label = label.to(device)
gaze = myResnet(data.to(torch.float32))
loss = loss_op(gaze.float(), label.float())
optimizer.zero_grad()
loss.backward()
optimizer.step()
scheduler.step()
print("epoch %d, time %d, loss %f" % (epoch, i, loss))
torch.save(myResnet.state_dict(), os.path.join(savepath, f"Iter_{epoch}_finetune.pt"))
print("Save Model Iter_%d_finetune.pt" % (epoch))