-
Notifications
You must be signed in to change notification settings - Fork 3
/
train_pose_estimator.py
222 lines (168 loc) · 7.75 KB
/
train_pose_estimator.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
217
218
219
220
221
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from torch.utils.data import DataLoader, Dataset
from torchvision import models
import numpy as np
import os
from PIL import Image
import torch.nn.functional as F
import random
name = "log_dinov2_mlp_1e-5_tmp"
gpuid = 1
from transformers import AutoImageProcessor, Dinov2Model
import logging
import sys
logging.basicConfig(encoding='utf-8', level=logging.INFO,
handlers=[logging.FileHandler("{}.log".format(name)),
logging.StreamHandler(sys.stdout) ] )
device ="cuda:{}".format(gpuid)
model_save_path = 'best_model_{}.pth'.format(name)
processor = AutoImageProcessor.from_pretrained("facebook/dinov2-base")
vit_model = Dinov2Model.from_pretrained("facebook/dinov2-base")
bs = 128 #100
lr = 1e-5
data_folder = "./imgs/pose_estimation_train_dataset"
class CustomDataset(Dataset):
def __init__(self, root_dir, transform=None, train=True, test_split=0.2):
self.root_dir = root_dir
self.transform = transform
self.train = train
self.test_split = test_split
self.data, self.labels = self.load_data()
def load_data(self):
data = []
labels = []
fur_dir_list = [d for d in os.listdir(self.root_dir) if os.path.isdir(os.path.join(self.root_dir, d))]
for fur_dir in fur_dir_list:
if "dreambooth" in fur_dir and "old" not in fur_dir:
fur_path = os.path.join(self.root_dir, fur_dir)
deg_dir_list = [d for d in os.listdir(fur_path) if os.path.isdir(fur_path )]
for deg_dir in deg_dir_list :
deg_path = os.path.join(fur_path, deg_dir)
files = [f for f in os.listdir(deg_path) if f.endswith('.png')]
for file in files:
file_path = os.path.join(deg_path, file)
r, t = file.split('_')
label = [float(r), float(t[:-4])] # 移除文件名中的".png"后缀
data.append(file_path)
labels.append(label)
zipped = list(zip(data, labels))
random.shuffle(zipped)
data, labels = zip(*zipped)
split_index = int(len(data) * (1 - self.test_split))
if self.train:
data = data[:split_index]
labels = labels[:split_index]
else:
data = data[split_index:]
labels = labels[split_index:]
return data, labels
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
img_path = self.data[idx]
label = self.labels[idx]
img = Image.open(img_path).convert('RGB')
inputs = processor(images=img, return_tensors="pt")
inputs['pixel_values'] = inputs['pixel_values'][0]
inputs['label'] = label
return inputs
class RegressionModel(nn.Module):
def __init__(self):
super(RegressionModel, self).__init__()
self.vit = vit_model
# self.fc1 = nn.Linear(768, 768)
# self.fc2 = nn.Linear(768, 768)
self.fc3 = nn.Linear(768, 128)
self.fc4 = nn.Linear(128, 2)
for param in self.vit.parameters():
param.requires_grad = True
def forward(self, x):
outputs = self.vit(x)
sequence_output = outputs[0]
x = sequence_output[:, 0, :] #[B,768]
# x = F.relu(self.fc1(x))
# x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = self.fc4(x)
return x
transform = transforms.Compose([transforms.Resize((224, 224)), transforms.ToTensor()])
train_dataset = CustomDataset(data_folder, transform=transform, train=True)
train_loader = DataLoader(train_dataset, batch_size=bs, shuffle=True, pin_memory=True, num_workers=32)
test_dataset = CustomDataset(data_folder, transform=transform, train=False)
test_loader = DataLoader(test_dataset, batch_size=bs, shuffle=False, pin_memory=True, num_workers=32)
model = RegressionModel().float().to(device)
model = model.to(device)
criterion = nn.MSELoss()
criterion_mae = nn.L1Loss()
optimizer = optim.AdamW(model.parameters(), lr=lr)
round_list =np.array (list(range(-160,170,10)))
def discretize(outputs):
outputs = outputs.numpy()
round_cand = round_list
for i in range(len(outputs.shape)):
round_cand = np.expand_dims(round_cand, 0)
outputs = np.expand_dims(outputs, -1)
diff = np.abs(outputs-round_cand)
pos = np.expand_dims(np.argmin(diff, axis = -1), axis = -1)
res = round_list[pos].squeeze()
return torch.tensor(res)
lowest_loss = float('inf')
num_epochs = 500
discrete_val = []
for epoch in range(num_epochs):
model.train()
for batch in train_loader:
inputs, labels = batch['pixel_values'], batch['label']
labels = torch.stack(labels, dim=-1)
inputs = inputs.float().to(device)
labels = labels.float().to(device)
optimizer.zero_grad()
outputs = model(inputs)
# from thop import profile
# flops, params = profile(model, (inputs,))
# print('flops: ', flops, 'params: ', params)
loss = criterion(outputs, labels)
loss_mae = criterion_mae(outputs.detach().cpu(), labels.detach().cpu())
loss.backward()
optimizer.step()
logging.info(f'Epoch [{epoch+1}/{num_epochs}], train MSE: {loss.item():.2f}, mae: {loss_mae.item():.2f}, RMSE: {np.sqrt(loss.item()):.2f}')
outputs_round =discretize(outputs.detach().cpu())
loss = criterion(outputs_round.detach().cpu(), labels.detach().cpu())
loss_mae = criterion_mae(outputs_round.detach().cpu(), labels.detach().cpu())
logging.info(f'Epoch [{epoch+1}/{num_epochs}], r MSE: {loss.item():.2f}, mae: {loss_mae.item():.2f}, RMSE: {np.sqrt(loss.item()):.2f}')
# After each epoch, the model will be evaluated on the test set
model.eval()
with torch.no_grad():
total_loss = 0
total_loss_mae = 0
r_total_loss = 0
r_total_loss_mae = 0
for batch in test_loader:
inputs, labels = batch['pixel_values'], batch['label']
labels = torch.stack(labels, dim=-1)
inputs = inputs.float().to(device)
labels = labels.float().to(device)
outputs = model(inputs)
loss = criterion(outputs, labels)
loss_mae = criterion_mae(outputs.detach().cpu(), labels.detach().cpu())
total_loss += loss.item()
total_loss_mae += loss_mae.item()
outputs_round =discretize(outputs.detach().cpu())
r_loss = criterion(outputs_round.detach().cpu(), labels.detach().cpu())
r_loss_mae = criterion_mae(outputs_round.detach().cpu(), labels.detach().cpu())
r_total_loss += r_loss.item()
r_total_loss_mae += r_loss_mae.item()
average_loss = total_loss / len(test_loader)
average_loss_mae = total_loss_mae / len(test_loader)
logging.info(f'Epoch [{epoch+1}/{num_epochs}], Test MSE: {average_loss:.2f}, mae: {average_loss_mae:.2f}, RMSE: {np.sqrt(average_loss):.2f}')
average_loss = r_total_loss / len(test_loader)
average_loss_mae = r_total_loss_mae / len(test_loader)
logging.info(f'Epoch [{epoch+1}/{num_epochs}], r MSE: {average_loss:.2f}, mae: {average_loss_mae:.2f}, RMSE: {np.sqrt(average_loss):.2f}')
# If the test loss of the current model is lower, save the current model
if average_loss < lowest_loss:
lowest_loss = average_loss
torch.save(model.state_dict(), model_save_path)
logging.info(f'Saved model with lowest test loss {lowest_loss}: {model_save_path}')