File models/my_ccnn.py changed (mode: 100644) (index 1bd1c0a..9ee3cc0) |
... |
... |
class CustomCNNv3(nn.Module): |
169 |
169 |
|
|
170 |
170 |
# ideal from crowd counting using DMCNN |
# ideal from crowd counting using DMCNN |
171 |
171 |
self.front_cnn_1 = nn.Conv2d(3, 20, 3, padding=1) |
self.front_cnn_1 = nn.Conv2d(3, 20, 3, padding=1) |
|
172 |
|
self.front_bn1 = nn.BatchNorm2d(20) |
172 |
173 |
self.front_cnn_2 = nn.Conv2d(20, 16, 3, padding=1) |
self.front_cnn_2 = nn.Conv2d(20, 16, 3, padding=1) |
|
174 |
|
self.front_bn2 = nn.BatchNorm2d(16) |
173 |
175 |
self.front_cnn_3 = nn.Conv2d(16, 14, 3, padding=1) |
self.front_cnn_3 = nn.Conv2d(16, 14, 3, padding=1) |
|
176 |
|
self.front_bn3 = nn.BatchNorm2d(14) |
174 |
177 |
self.front_cnn_4 = nn.Conv2d(14, 10, 3, padding=1) |
self.front_cnn_4 = nn.Conv2d(14, 10, 3, padding=1) |
|
178 |
|
self.front_bn4 = nn.BatchNorm2d(10) |
175 |
179 |
|
|
176 |
180 |
self.c0 = nn.Conv2d(40, 40, 3, padding=1) |
self.c0 = nn.Conv2d(40, 40, 3, padding=1) |
|
181 |
|
self.bn0 = nn.BatchNorm2d(40) |
177 |
182 |
self.max_pooling = nn.MaxPool2d(kernel_size=2, stride=2) |
self.max_pooling = nn.MaxPool2d(kernel_size=2, stride=2) |
178 |
183 |
|
|
179 |
184 |
self.c1 = nn.Conv2d(40, 60, 3, padding=1) |
self.c1 = nn.Conv2d(40, 60, 3, padding=1) |
180 |
|
|
|
|
185 |
|
self.bn1 = nn.BatchNorm2d(60) |
181 |
186 |
# ideal from CSRNet |
# ideal from CSRNet |
182 |
187 |
self.c2 = nn.Conv2d(60, 40, 3, padding=2, dilation=2, bias=False) |
self.c2 = nn.Conv2d(60, 40, 3, padding=2, dilation=2, bias=False) |
183 |
188 |
self.bn2 = nn.BatchNorm2d(40) |
self.bn2 = nn.BatchNorm2d(40) |
|
... |
... |
class CustomCNNv3(nn.Module): |
192 |
197 |
#x_green = self.max_pooling(F.relu(self.green_cnn(x), inplace=True)) |
#x_green = self.max_pooling(F.relu(self.green_cnn(x), inplace=True)) |
193 |
198 |
#x_blue = self.max_pooling(F.relu(self.blue_cnn(x), inplace=True)) |
#x_blue = self.max_pooling(F.relu(self.blue_cnn(x), inplace=True)) |
194 |
199 |
|
|
195 |
|
x_red = F.relu(self.front_cnn_1(x), inplace=True) |
|
196 |
|
x_red = F.relu(self.front_cnn_2(x_red), inplace=True) |
|
197 |
|
x_red = F.relu(self.front_cnn_3(x_red), inplace=True) |
|
198 |
|
x_red = F.relu(self.front_cnn_4(x_red), inplace=True) |
|
|
200 |
|
x_red = F.relu(self.front_bn1(self.front_cnn_1(x)), inplace=True) |
|
201 |
|
x_red = F.relu(self.front_bn2(self.front_cnn_2(x_red)), inplace=True) |
|
202 |
|
x_red = F.relu(self.front_bn3(self.front_cnn_3(x_red)), inplace=True) |
|
203 |
|
x_red = F.relu(self.front_bn4(self.front_cnn_4(x_red)), inplace=True) |
199 |
204 |
x_red = self.max_pooling(x_red) |
x_red = self.max_pooling(x_red) |
200 |
205 |
|
|
201 |
|
x_green = F.relu(self.front_cnn_1(x), inplace=True) |
|
202 |
|
x_green = F.relu(self.front_cnn_2(x_green), inplace=True) |
|
203 |
|
x_green = F.relu(self.front_cnn_3(x_green), inplace=True) |
|
|
206 |
|
x_green = F.relu(self.front_bn1(self.front_cnn_1(x)), inplace=True) |
|
207 |
|
x_green = F.relu(self.front_bn2(self.front_cnn_2(x_green)), inplace=True) |
|
208 |
|
x_green = F.relu(self.front_bn3(self.front_cnn_3(x_green)), inplace=True) |
204 |
209 |
x_green = self.max_pooling(x_green) |
x_green = self.max_pooling(x_green) |
205 |
210 |
|
|
206 |
|
x_blue = F.relu(self.front_cnn_1(x), inplace=True) |
|
207 |
|
x_blue = F.relu(self.front_cnn_2(x_blue), inplace=True) |
|
|
211 |
|
x_blue = F.relu(self.front_bn1(self.front_cnn_1(x)), inplace=True) |
|
212 |
|
x_blue = F.relu(self.front_bn2(self.front_cnn_2(x_blue)), inplace=True) |
208 |
213 |
x_blue = self.max_pooling(x_blue) |
x_blue = self.max_pooling(x_blue) |
209 |
214 |
|
|
210 |
215 |
x = torch.cat((x_red, x_green, x_blue), 1) |
x = torch.cat((x_red, x_green, x_blue), 1) |
File train_custom_compact_cnn_lrscheduler.py changed (mode: 100644) (index 943fa68..07d6c9c) |
|
1 |
|
from comet_ml import Experiment |
|
2 |
|
|
1 |
3 |
from args_util import my_args_parse |
from args_util import my_args_parse |
2 |
4 |
from data_flow import get_train_val_list, get_dataloader, create_training_image_list, create_image_list |
from data_flow import get_train_val_list, get_dataloader, create_training_image_list, create_image_list |
3 |
5 |
from ignite.engine import Events, create_supervised_trainer, create_supervised_evaluator |
from ignite.engine import Events, create_supervised_trainer, create_supervised_evaluator |
4 |
6 |
from ignite.metrics import Loss, MeanAbsoluteError, MeanSquaredError |
from ignite.metrics import Loss, MeanAbsoluteError, MeanSquaredError |
|
7 |
|
from ignite.engine import Engine |
5 |
8 |
from ignite.handlers import Checkpoint, DiskSaver |
from ignite.handlers import Checkpoint, DiskSaver |
6 |
9 |
from crowd_counting_error_metrics import CrowdCountingMeanAbsoluteError, CrowdCountingMeanSquaredError |
from crowd_counting_error_metrics import CrowdCountingMeanAbsoluteError, CrowdCountingMeanSquaredError |
7 |
10 |
from visualize_util import get_readable_time |
from visualize_util import get_readable_time |
8 |
11 |
|
|
9 |
12 |
import torch |
import torch |
10 |
13 |
from torch import nn |
from torch import nn |
11 |
|
from models import CompactDilatedCNN |
|
|
14 |
|
from models import CustomCNNv3 |
12 |
15 |
import os |
import os |
13 |
16 |
from ignite.contrib.handlers import PiecewiseLinear |
from ignite.contrib.handlers import PiecewiseLinear |
|
17 |
|
from model_util import get_lr |
14 |
18 |
|
|
|
19 |
|
COMET_ML_API = "S3mM1eMq6NumMxk2QJAXASkUM" |
|
20 |
|
PROJECT_NAME = "crowd-counting-framework" |
15 |
21 |
|
|
16 |
22 |
if __name__ == "__main__": |
if __name__ == "__main__": |
|
23 |
|
experiment = Experiment(project_name=PROJECT_NAME, api_key=COMET_ML_API) |
|
24 |
|
|
17 |
25 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
18 |
26 |
print(device) |
print(device) |
19 |
27 |
args = my_args_parse() |
args = my_args_parse() |
|
28 |
|
experiment.set_name(args.task_id) |
20 |
29 |
print(args) |
print(args) |
|
30 |
|
experiment.set_cmd_args() |
|
31 |
|
|
21 |
32 |
DATA_PATH = args.input |
DATA_PATH = args.input |
22 |
33 |
TRAIN_PATH = os.path.join(DATA_PATH, "train_data") |
TRAIN_PATH = os.path.join(DATA_PATH, "train_data") |
23 |
34 |
TEST_PATH = os.path.join(DATA_PATH, "test_data") |
TEST_PATH = os.path.join(DATA_PATH, "test_data") |
|
... |
... |
if __name__ == "__main__": |
35 |
46 |
test_list = create_image_list(TEST_PATH) |
test_list = create_image_list(TEST_PATH) |
36 |
47 |
|
|
37 |
48 |
# create data loader |
# create data loader |
38 |
|
train_loader, val_loader, test_loader = get_dataloader(train_list, None, test_list, dataset_name=dataset_name) |
|
|
49 |
|
train_loader, val_loader, test_loader = get_dataloader(train_list, None, test_list, dataset_name=dataset_name, batch_size=args.batch_size) |
39 |
50 |
|
|
40 |
51 |
print("len train_loader ", len(train_loader)) |
print("len train_loader ", len(train_loader)) |
41 |
52 |
|
|
42 |
53 |
# model |
# model |
43 |
|
model = CompactDilatedCNN() |
|
|
54 |
|
model = CustomCNNv3() |
44 |
55 |
model = model.to(device) |
model = model.to(device) |
45 |
56 |
|
|
46 |
57 |
# loss function |
# loss function |
|
... |
... |
if __name__ == "__main__": |
49 |
60 |
optimizer = torch.optim.Adam(model.parameters(), args.lr, |
optimizer = torch.optim.Adam(model.parameters(), args.lr, |
50 |
61 |
weight_decay=args.decay) |
weight_decay=args.decay) |
51 |
62 |
|
|
52 |
|
milestones_values = [(50, 1e-4), (50, 5e-5), (50, 1e-5), (50, 5e-6), (50, 1e-6), (100, 1e-7)] |
|
|
63 |
|
milestones_values = [(70, 1e-4), (100, 1e-5), (200, 1e-5)] |
|
64 |
|
experiment.log_parameter("milestones_values", str(milestones_values)) |
53 |
65 |
lr_scheduler = PiecewiseLinear(optimizer, param_name="lr", milestones_values=milestones_values) |
lr_scheduler = PiecewiseLinear(optimizer, param_name="lr", milestones_values=milestones_values) |
|
66 |
|
|
54 |
67 |
trainer = create_supervised_trainer(model, optimizer, loss_fn, device=device) |
trainer = create_supervised_trainer(model, optimizer, loss_fn, device=device) |
55 |
68 |
evaluator = create_supervised_evaluator(model, |
evaluator = create_supervised_evaluator(model, |
56 |
69 |
metrics={ |
metrics={ |
57 |
70 |
'mae': CrowdCountingMeanAbsoluteError(), |
'mae': CrowdCountingMeanAbsoluteError(), |
58 |
71 |
'mse': CrowdCountingMeanSquaredError(), |
'mse': CrowdCountingMeanSquaredError(), |
59 |
|
'nll': Loss(loss_fn) |
|
|
72 |
|
'loss': Loss(loss_fn) |
60 |
73 |
}, device=device) |
}, device=device) |
61 |
74 |
print(model) |
print(model) |
62 |
75 |
|
|
|
... |
... |
if __name__ == "__main__": |
74 |
87 |
print("change lr to ", args.lr) |
print("change lr to ", args.lr) |
75 |
88 |
else: |
else: |
76 |
89 |
print("do not load, keep training") |
print("do not load, keep training") |
77 |
|
|
|
78 |
90 |
trainer.add_event_handler(Events.EPOCH_STARTED, lr_scheduler) |
trainer.add_event_handler(Events.EPOCH_STARTED, lr_scheduler) |
79 |
91 |
|
|
80 |
92 |
|
|
|
... |
... |
if __name__ == "__main__": |
90 |
102 |
metrics = evaluator.state.metrics |
metrics = evaluator.state.metrics |
91 |
103 |
timestamp = get_readable_time() |
timestamp = get_readable_time() |
92 |
104 |
print(timestamp + " Training set Results - Epoch: {} Avg mae: {:.2f} Avg mse: {:.2f} Avg loss: {:.2f}" |
print(timestamp + " Training set Results - Epoch: {} Avg mae: {:.2f} Avg mse: {:.2f} Avg loss: {:.2f}" |
93 |
|
.format(trainer.state.epoch, metrics['mae'], metrics['mse'], metrics['nll'])) |
|
94 |
|
|
|
|
105 |
|
.format(trainer.state.epoch, metrics['mae'], metrics['mse'], metrics['loss'])) |
|
106 |
|
experiment.log_metric("epoch", trainer.state.epoch) |
|
107 |
|
experiment.log_metric("train_mae", metrics['mae']) |
|
108 |
|
experiment.log_metric("train_mse", metrics['mse']) |
|
109 |
|
experiment.log_metric("train_loss", metrics['loss']) |
|
110 |
|
experiment.log_metric("lr", get_lr(optimizer)) |
95 |
111 |
|
|
96 |
112 |
@trainer.on(Events.EPOCH_COMPLETED) |
@trainer.on(Events.EPOCH_COMPLETED) |
97 |
113 |
def log_validation_results(trainer): |
def log_validation_results(trainer): |
|
... |
... |
if __name__ == "__main__": |
99 |
115 |
metrics = evaluator.state.metrics |
metrics = evaluator.state.metrics |
100 |
116 |
timestamp = get_readable_time() |
timestamp = get_readable_time() |
101 |
117 |
print(timestamp + " Validation set Results - Epoch: {} Avg mae: {:.2f} Avg mse: {:.2f} Avg loss: {:.2f}" |
print(timestamp + " Validation set Results - Epoch: {} Avg mae: {:.2f} Avg mse: {:.2f} Avg loss: {:.2f}" |
102 |
|
.format(trainer.state.epoch, metrics['mae'], metrics['mse'], metrics['nll'])) |
|
103 |
|
|
|
|
118 |
|
.format(trainer.state.epoch, metrics['mae'], metrics['mse'], metrics['loss'])) |
|
119 |
|
experiment.log_metric("valid_mae", metrics['mae']) |
|
120 |
|
experiment.log_metric("valid_mse", metrics['mse']) |
|
121 |
|
experiment.log_metric("valid_loss", metrics['loss']) |
104 |
122 |
|
|
105 |
123 |
|
|
106 |
124 |
# docs on save and load |
# docs on save and load |
|
... |
... |
if __name__ == "__main__": |
109 |
127 |
filename_prefix=args.task_id, |
filename_prefix=args.task_id, |
110 |
128 |
n_saved=5) |
n_saved=5) |
111 |
129 |
|
|
112 |
|
trainer.add_event_handler(Events.EPOCH_COMPLETED(every=3), save_handler) |
|
|
130 |
|
trainer.add_event_handler(Events.EPOCH_COMPLETED(every=5), save_handler) |
113 |
131 |
|
|
114 |
132 |
trainer.run(train_loader, max_epochs=args.epochs) |
trainer.run(train_loader, max_epochs=args.epochs) |