-
Notifications
You must be signed in to change notification settings - Fork 76
/
train.py
194 lines (158 loc) · 6.99 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
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
"""
Training Code for Learning To Count Everything, CVPR 2021
Authors: Viresh Ranjan,Udbhav, Thu Nguyen, Minh Hoai
Last modified by: Minh Hoai Nguyen ([email protected])
Date: 2021/04/19
"""
import torch.nn as nn
from model import Resnet50FPN,CountRegressor,weights_normal_init
from utils import MAPS, Scales, Transform,TransformTrain,extract_features, visualize_output_and_save
from PIL import Image
import os
import torch
import argparse
import json
import numpy as np
from tqdm import tqdm
from os.path import exists,join
import random
import torch.optim as optim
import torch.nn.functional as F
parser = argparse.ArgumentParser(description="Few Shot Counting Evaluation code")
parser.add_argument("-dp", "--data_path", type=str, default='/home/hoai/DataSets/AgnosticCounting/FSC147_384_V2/', help="Path to the FSC147 dataset")
parser.add_argument("-o", "--output_dir", type=str,default="./logsSave", help="/Path/to/output/logs/")
parser.add_argument("-ts", "--test-split", type=str, default='val', choices=["train", "test", "val"], help="what data split to evaluate on on")
parser.add_argument("-ep", "--epochs", type=int,default=1500, help="number of training epochs")
parser.add_argument("-g", "--gpu", type=int,default=0, help="GPU id")
parser.add_argument("-lr", "--learning-rate", type=float,default=1e-5, help="learning rate")
args = parser.parse_args()
data_path = args.data_path
anno_file = data_path + 'annotation_FSC147_384.json'
data_split_file = data_path + 'Train_Test_Val_FSC_147.json'
im_dir = data_path + 'images_384_VarV2'
gt_dir = data_path + 'gt_density_map_adaptive_384_VarV2'
if not exists(args.output_dir):
os.mkdir(args.output_dir)
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpu)
criterion = nn.MSELoss().cuda()
resnet50_conv = Resnet50FPN()
resnet50_conv.cuda()
resnet50_conv.eval()
regressor = CountRegressor(6, pool='mean')
weights_normal_init(regressor, dev=0.001)
regressor.train()
regressor.cuda()
optimizer = optim.Adam(regressor.parameters(), lr = args.learning_rate)
with open(anno_file) as f:
annotations = json.load(f)
with open(data_split_file) as f:
data_split = json.load(f)
def train():
print("Training on FSC147 train set data")
im_ids = data_split['train']
random.shuffle(im_ids)
train_mae = 0
train_rmse = 0
train_loss = 0
pbar = tqdm(im_ids)
cnt = 0
for im_id in pbar:
cnt += 1
anno = annotations[im_id]
bboxes = anno['box_examples_coordinates']
dots = np.array(anno['points'])
rects = list()
for bbox in bboxes:
x1 = bbox[0][0]
y1 = bbox[0][1]
x2 = bbox[2][0]
y2 = bbox[2][1]
rects.append([y1, x1, y2, x2])
image = Image.open('{}/{}'.format(im_dir, im_id))
image.load()
density_path = gt_dir + '/' + im_id.split(".jpg")[0] + ".npy"
density = np.load(density_path).astype('float32')
sample = {'image':image,'lines_boxes':rects,'gt_density':density}
sample = TransformTrain(sample)
image, boxes,gt_density = sample['image'].cuda(), sample['boxes'].cuda(),sample['gt_density'].cuda()
with torch.no_grad():
features = extract_features(resnet50_conv, image.unsqueeze(0), boxes.unsqueeze(0), MAPS, Scales)
features.requires_grad = True
optimizer.zero_grad()
output = regressor(features)
#if image size isn't divisible by 8, gt size is slightly different from output size
if output.shape[2] != gt_density.shape[2] or output.shape[3] != gt_density.shape[3]:
orig_count = gt_density.sum().detach().item()
gt_density = F.interpolate(gt_density, size=(output.shape[2],output.shape[3]),mode='bilinear')
new_count = gt_density.sum().detach().item()
if new_count > 0: gt_density = gt_density * (orig_count / new_count)
loss = criterion(output, gt_density)
loss.backward()
optimizer.step()
train_loss += loss.item()
pred_cnt = torch.sum(output).item()
gt_cnt = torch.sum(gt_density).item()
cnt_err = abs(pred_cnt - gt_cnt)
train_mae += cnt_err
train_rmse += cnt_err ** 2
pbar.set_description('actual-predicted: {:6.1f}, {:6.1f}, error: {:6.1f}. Current MAE: {:5.2f}, RMSE: {:5.2f} Best VAL MAE: {:5.2f}, RMSE: {:5.2f}'.format( gt_cnt, pred_cnt, abs(pred_cnt - gt_cnt), train_mae/cnt, (train_rmse/cnt)**0.5,best_mae,best_rmse))
print("")
train_loss = train_loss / len(im_ids)
train_mae = (train_mae / len(im_ids))
train_rmse = (train_rmse / len(im_ids))**0.5
return train_loss,train_mae,train_rmse
def eval():
cnt = 0
SAE = 0 # sum of absolute errors
SSE = 0 # sum of square errors
print("Evaluation on {} data".format(args.test_split))
im_ids = data_split[args.test_split]
pbar = tqdm(im_ids)
for im_id in pbar:
anno = annotations[im_id]
bboxes = anno['box_examples_coordinates']
dots = np.array(anno['points'])
rects = list()
for bbox in bboxes:
x1 = bbox[0][0]
y1 = bbox[0][1]
x2 = bbox[2][0]
y2 = bbox[2][1]
rects.append([y1, x1, y2, x2])
image = Image.open('{}/{}'.format(im_dir, im_id))
image.load()
sample = {'image':image,'lines_boxes':rects}
sample = Transform(sample)
image, boxes = sample['image'].cuda(), sample['boxes'].cuda()
with torch.no_grad():
output = regressor(extract_features(resnet50_conv, image.unsqueeze(0), boxes.unsqueeze(0), MAPS, Scales))
gt_cnt = dots.shape[0]
pred_cnt = output.sum().item()
cnt = cnt + 1
err = abs(gt_cnt - pred_cnt)
SAE += err
SSE += err**2
pbar.set_description('{:<8}: actual-predicted: {:6d}, {:6.1f}, error: {:6.1f}. Current MAE: {:5.2f}, RMSE: {:5.2f}'.format(im_id, gt_cnt, pred_cnt, abs(pred_cnt - gt_cnt), SAE/cnt, (SSE/cnt)**0.5))
print("")
print('On {} data, MAE: {:6.2f}, RMSE: {:6.2f}'.format(args.test_split, SAE/cnt, (SSE/cnt)**0.5))
return SAE/cnt, (SSE/cnt)**0.5
best_mae, best_rmse = 1e7, 1e7
stats = list()
for epoch in range(0,args.epochs):
regressor.train()
train_loss,train_mae,train_rmse = train()
regressor.eval()
val_mae,val_rmse = eval()
stats.append((train_loss, train_mae, train_rmse, val_mae, val_rmse))
stats_file = join(args.output_dir, "stats" + ".txt")
with open(stats_file, 'w') as f:
for s in stats:
f.write("%s\n" % ','.join([str(x) for x in s]))
if best_mae >= val_mae:
best_mae = val_mae
best_rmse = val_rmse
model_name = args.output_dir + '/' + "FamNet.pth"
torch.save(regressor.state_dict(), model_name)
print("Epoch {}, Avg. Epoch Loss: {} Train MAE: {} Train RMSE: {} Val MAE: {} Val RMSE: {} Best Val MAE: {} Best Val RMSE: {} ".format(
epoch+1, stats[-1][0], stats[-1][1], stats[-1][2], stats[-1][3], stats[-1][4], best_mae, best_rmse))