-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
152 lines (111 loc) · 5.33 KB
/
main.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
import argparse
import os
from cv2 import cuda_BufferPool
import numpy as np
import time
import cv2
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
from torch.autograd import Variable
import torch.nn.functional as F
from dataset import *
from model import *
from utils import *
parser = argparse.ArgumentParser()
parser.add_argument('--test', action='store_true')
args = parser.parse_args()
class_num = 4 #cat dog person background
num_epochs = 100
batch_size = 2
boxs_default = default_box_generator([10,5,3,1], [0.2,0.4,0.6,0.8], [0.1,0.3,0.5,0.7])
#Create network
network = SSD(class_num)
device = ('cuda' if torch.cuda.is_available() else 'cpu')
print(device)
network.to(device)
cudnn.benchmark = True
if not args.test:
imgs_dir = "CMPT733-Lab3-Workspace/data/train/images/"
annot_dir = "CMPT733-Lab3-Workspace/data/train/annotations/"
dataset = COCO(imgs_dir, annot_dir, class_num, boxs_default, train = True, image_size=320)
dataset_val = COCO(imgs_dir, annot_dir, class_num, boxs_default, train = False, image_size=320)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True, num_workers=0)
dataloader_val = torch.utils.data.DataLoader(dataset_val, batch_size=batch_size, shuffle=False, num_workers=0)
optimizer = optim.Adam(network.parameters(), lr = 1e-4)
#feel free to try other optimizers and parameters.
start_time = time.time()
for epoch in range(num_epochs):
#TRAINING
network.train()
avg_loss = 0
avg_count = 0
for i, data in enumerate(dataloader, 0):
images_, ann_box_, ann_confidence_ = data
images = images_.to(device)
ann_box = ann_box_.to(device)
ann_confidence = ann_confidence_.to(device)
optimizer.zero_grad()
pred_confidence, pred_box = network(images)
loss_net = SSD_loss(pred_confidence, pred_box, ann_confidence, ann_box)
loss_net.backward()
optimizer.step()
avg_loss += loss_net.data
avg_count += 1
break
print('[%d] time: %f train loss: %f' % (epoch, time.time()-start_time, avg_loss/avg_count))
#visualize
pred_confidence_ = pred_confidence[0].detach().cpu().numpy()
pred_box_ = pred_box[0].detach().cpu().numpy()
visualize_pred("train", pred_confidence_, pred_box_, ann_confidence_[0].numpy(), ann_box_[0].numpy(), images_[0].numpy(), boxs_default)
break
#VALIDATION
network.eval()
# TODO: split the dataset into 90% training and 10% validation
# use the training set to train and the validation set to evaluate
for i, data in enumerate(dataloader_val, 0):
images_, ann_box_, ann_confidence_ = data
images = images_.to(device)
ann_box = ann_box_.to(device)
ann_confidence = ann_confidence_.to(device)
pred_confidence, pred_box = network(images)
pred_confidence_ = pred_confidence.detach().cpu().numpy()
pred_box_ = pred_box.detach().cpu().numpy()
#optional: implement a function to accumulate precision and recall to compute mAP or F1.
#update_precision_recall(pred_confidence_, pred_box_, ann_confidence_.numpy(), ann_box_.numpy(), boxs_default,precision_,recall_,thres)
#visualize
pred_confidence_ = pred_confidence[0].detach().cpu().numpy()
pred_box_ = pred_box[0].detach().cpu().numpy()
visualize_pred("val", pred_confidence_, pred_box_, ann_confidence_[0].numpy(), ann_box_[0].numpy(), images_[0].numpy(), boxs_default)
#optional: compute F1
#F1score = 2*precision*recall/np.maximum(precision+recall,1e-8)
#print(F1score)
#save weights
if epoch%10==9:
#save last network
print('saving net...')
torch.save(network.state_dict(), 'network.pth')
else:
#TEST
dataset_test = COCO("data/test/images/", "data/test/annotations/", class_num, boxs_default, train = False, image_size=320)
dataloader_test = torch.utils.data.DataLoader(dataset_test, batch_size=1, shuffle=False, num_workers=0)
network.load_state_dict(torch.load('network.pth'))
network.eval()
for i, data in enumerate(dataloader_test, 0):
images_, ann_box_, ann_confidence_ = data
images = images_.to(device)
ann_box = ann_box_.to(device)
ann_confidence = ann_confidence_.to(device)
pred_confidence, pred_box = network(images)
pred_confidence_ = pred_confidence[0].detach().cpu().numpy()
pred_box_ = pred_box[0].detach().cpu().numpy()
#pred_confidence_,pred_box_ = non_maximum_suppression(pred_confidence_,pred_box_,boxs_default)
#TODO: save predicted bounding boxes and classes to a txt file.
#you will need to submit those files for grading this assignment
visualize_pred("test", pred_confidence_, pred_box_, ann_confidence_[0].numpy(), ann_box_[0].numpy(), images_[0].numpy(), boxs_default)
cv2.waitKey(1000)