forked from ml-lab/MobilePose-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
training.py
144 lines (117 loc) · 5.3 KB
/
training.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
# coding: utf-8
'''
File: training.py
Project: MobilePose
File Created: Thursday, 8th March 2018 2:50:11 pm
Author: Yuliang Xiu ([email protected])
-----
Last Modified: Thursday, 8th March 2018 2:50:51 pm
Modified By: Yuliang Xiu ([email protected]>)
-----
Copyright 2018 - 2018 Shanghai Jiao Tong University, Machine Vision and Intelligence Group
'''
# remove warning
import warnings
warnings.filterwarnings('ignore')
import os
import numpy as np
from networks import *
from dataloader import *
import argparse
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='MobilePose Demo')
parser.add_argument('--model', type=str, default="resnet")
parser.add_argument('--gpu', type=str, default="0")
args = parser.parse_args()
modeltype = args.model
# user defined parameters
num_threads = 10
if modeltype =='resnet':
modelname = "final-aug.t7"
pretrain = True
batchsize = 256
minloss = 316.52189376 #changed expand ratio
# minloss = 272.49565467 #fixed expand ratio
learning_rate = 1e-05
net = Net().cuda()
inputsize = 227
elif modeltype == "mobilenet":
modelname = "final-aug.t7"
pretrain = True
batchsize = 128
minloss = 396.84708708 # change expand ratio
# minloss = 332.48316225 # fixed expand ratio
learning_rate = 1e-06
net = MobileNetV2(image_channel=5).cuda()
inputsize = 224
# gpu setting
os.environ["CUDA_VISIBLE_DEVICES"]=args.gpu
torch.backends.cudnn.enabled = True
gpus = [0,1]
print("GPU NUM: %d"%(torch.cuda.device_count()))
logname = modeltype+'-log.txt'
if pretrain:
net = torch.load('./models/%s/%s'%(modeltype,modelname)).cuda(device_id=gpus[0])
ROOT_DIR = "../deeppose_tf/datasets/mpii"
PATH_PREFIX = './models/{}/'.format(modeltype)
train_dataset = PoseDataset(csv_file=os.path.join(ROOT_DIR,'train_joints.csv'),
transform=transforms.Compose([
# Augmentation(),
Rescale((inputsize,inputsize)),
# Wrap((inputsize,inputsize)),
Expansion(),
ToTensor()
]))
train_dataloader = DataLoader(train_dataset, batch_size=batchsize,
shuffle=False, num_workers = num_threads)
test_dataset = PoseDataset(csv_file=os.path.join(ROOT_DIR,'test_joints.csv'),
transform=transforms.Compose([
Rescale((inputsize,inputsize)),
# Wrap((inputsize, inputsize)),
Expansion(),
ToTensor()
]))
test_dataloader = DataLoader(test_dataset, batch_size=batchsize,
shuffle=False, num_workers = num_threads)
criterion = nn.MSELoss().cuda()
# optimizer = optim.Adam(net.parameters(), lr=learning_rate, betas=(0.9, 0.999), eps=1e-08)
optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=0.9)
def mse_loss(input, target):
return torch.sum(torch.pow(input - target,2)) / input.nelement()
train_loss_all = []
valid_loss_all = []
for epoch in range(1000): # loop over the dataset multiple times
train_loss_epoch = []
for i, data in enumerate(train_dataloader):
images, poses = data['image'], data['pose']
images, poses = Variable(images.cuda()), Variable(poses.cuda())
optimizer.zero_grad()
outputs = net(images)
loss = criterion(outputs, poses)
loss.backward()
optimizer.step()
train_loss_epoch.append(loss.data[0])
if epoch%2==0:
valid_loss_epoch = []
for i_batch, sample_batched in enumerate(test_dataloader):
net_forward = net
images = sample_batched['image'].cuda()
poses = sample_batched['pose'].cuda()
outputs = net_forward(Variable(images, volatile=True))
valid_loss_epoch.append(mse_loss(outputs.data,poses))
if np.mean(np.array(valid_loss_epoch)) < minloss:
minloss = np.mean(np.array(valid_loss_epoch))
checkpoint_file = PATH_PREFIX + modelname
torch.save(net, checkpoint_file)
print('==> checkpoint model saving to %s'%checkpoint_file)
print('[epoch %d] train loss: %.8f, valid loss: %.8f' %
(epoch + 1, np.mean(np.array(train_loss_epoch)), np.mean(np.array(valid_loss_epoch))))
with open(PATH_PREFIX+logname, 'a+') as file_output:
file_output.write('[epoch %d] train loss: %.8f, valid loss: %.8f\n' %
(epoch + 1, np.mean(np.array(train_loss_epoch)), np.mean(np.array(valid_loss_epoch))))
file_output.flush()
print('Finished Training')