-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
130 lines (108 loc) · 4.31 KB
/
train.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
from __future__ import print_function
import os
import argparse
import itertools
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms
from ssd import SSD300
from utils import progress_bar
from datagen import ListDataset
from multibox_loss import MultiBoxLoss
from torch.autograd import Variable
parser = argparse.ArgumentParser(description='PyTorch SSD Training')
parser.add_argument('--lr', default=1e-3, type=float, help='learning rate')
parser.add_argument('--resume', '-r', action='store_true', help='resume from checkpoint')
args = parser.parse_args()
use_cuda = torch.cuda.is_available()
best_loss = float('inf') # best test loss
start_epoch = 0 # start from epoch 0 or last epoch
# Data
print('==> Preparing data..')
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))])
'''transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=(0.485,), std=(0.229,))])'''
'''trainset = ListDataset(root='/home/biometric/Ranjeet/Data_For_Faster_RCNN/Iris_jpg', list_file='./voc_data/training.txt', train=True, transform=transform)'''
trainset = ListDataset(root='images/training/images', list_file='labels/training.txt', train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=20, shuffle=True, num_workers=3)
testset = ListDataset(root='images/training/images', list_file='labels/testing.txt', train=False, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=20, shuffle=False, num_workers=3)
# Modl
net = SSD300()
if args.resume:
print('==> Resuming from checkpoint..')
checkpoint = torch.load('./checkpoint/ckpt.pth')
net.load_state_dict(checkpoint['net'])
best_loss = checkpoint['loss']
start_epoch = checkpoint['epoch']
else:
# Convert from pretrained VGG model.
print("Om")
net.load_state_dict(torch.load('./model/ssd.pth'))
print("Namah")
criterion = MultiBoxLoss()
if use_cuda:
net = torch.nn.DataParallel(net, device_ids=[0])
net.cuda()
cudnn.benchmark = True
optimizer = optim.SGD(net.parameters(), lr=args.lr, momentum=0.9, weight_decay=1e-4)
# Training
def train(epoch):
print('\nEpoch: %d' % epoch)
net.train()
train_loss = 0
for batch_idx, (images, loc_targets, conf_targets) in enumerate(trainloader):
if use_cuda:
images = images.cuda()
loc_targets = loc_targets.cuda()
conf_targets = conf_targets.cuda()
images = Variable(images)
loc_targets = Variable(loc_targets)
conf_targets = Variable(conf_targets)
optimizer.zero_grad()
loc_preds, conf_preds = net(images)
#print ("Ranjeet")
loss = criterion(loc_preds, loc_targets, conf_preds, conf_targets)
loss.backward()
optimizer.step()
train_loss += loss.data[0]
print('%.3f %.3f' % (loss.data[0], train_loss/(batch_idx+1)))
def test(epoch):
print('\nTest')
net.eval()
test_loss = 0
for batch_idx, (images, loc_targets, conf_targets) in enumerate(testloader):
if use_cuda:
images = images.cuda()
loc_targets = loc_targets.cuda()
conf_targets = conf_targets.cuda()
images = Variable(images, volatile=True)
loc_targets = Variable(loc_targets)
conf_targets = Variable(conf_targets)
loc_preds, conf_preds = net(images)
loss = criterion(loc_preds, loc_targets, conf_preds, conf_targets)
test_loss += loss.data[0]
print('%.3f %.3f' % (loss.data[0], test_loss/(batch_idx+1)))
# Save checkpoint.
global best_loss
test_loss /= len(testloader)
if test_loss < best_loss:
print('Saving..')
state = {
'net': net.module.state_dict(),
'loss': test_loss,
'epoch': epoch,
}
states = {'net': net.module.state_dict()}
if not os.path.isdir('checkpoint'):
os.mkdir('checkpoint')
torch.save(state, './checkpoint/ckpt.pth')
best_loss = test_loss
for epoch in range(start_epoch, start_epoch+10000):
train(epoch)
test(epoch)