Skip to content

Commit

Permalink
Formulate PT OD configs in terms of epochs only (openvinotoolkit#1019)
Browse files Browse the repository at this point in the history
* Formulate PT OD configs in terms of epochs only

* Recalculate epochs correctly
  • Loading branch information
vshampor authored Dec 20, 2021
1 parent ddb7532 commit 8f9fcfb
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"rgb": true
},
"batch_size": 128,
"max_iter": 75000,
"iter_size": 1,
"epochs": 580,
"save_freq": 50,
"optimizer": {
"type": "Adam",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
"normalize_coef": 255,
"rgb": true
},
"max_iter": 100000,
"epochs": 386,
"batch_size": 256,
"iter_size": 1,
"save_freq": 50,
"optimizer": {
"type": "Adam",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
"normalize_coef": 255,
"rgb": true
},
"max_iter": 100000,
"epochs": 386,
"batch_size": 256,
"iter_size": 1,
"save_freq": 50,
"optimizer": {
"type": "Adam",
Expand Down
7 changes: 3 additions & 4 deletions examples/torch/object_detection/configs/ssd300_vgg_voc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
"rgb": true
},
"batch_size": 256,
"max_iter": 75000,
"iter_size": 1,
"save_freq": 50, // in epochs
"epochs": 290,
"save_freq": 50,
"optimizer": {
"type": "Adam",
"base_lr": 1e-4,
"weight_decay": 5e-4,
"schedule_type": "multistep",
"steps": [2, 3, 4] // in epochs
"steps": [2, 3, 4]
},
"ssd_params": {
"clip": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
"normalize_coef": 255,
"rgb": true
},
"max_iter": 3000,
"epochs": 24,
"batch_size": 128,
"test_interval": 500, // in iterations
"iter_size": 1,
"save_freq": 3, // in epochs
"epochs": 3,
"test_interval": 1,
"save_freq": 3,
"optimizer": {
"type": "Adam",
"base_lr": 1e-5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
"normalize_coef": 255,
"rgb": true
},
"max_iter": 3000,
"epochs": 24,
"batch_size": 128,
"test_interval": 500, // in iterations
"iter_size": 1,
"save_freq": 3, // in epochs
"epochs": 3,
"test_interval": 1,
"save_freq": 3,
"optimizer": {
"type": "Adam",
"base_lr": 1e-5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
"normalize_coef": 255,
"rgb": true
},
"max_iter": 30000,
"epochs": 232,
"batch_size": 128,
"iter_size": 1,
"save_freq": 10,
"epochs": 130,
"optimizer": {
"type": "Adam",
"base_lr": 1e-4,
Expand Down Expand Up @@ -60,4 +58,4 @@
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@
"rgb": true
},
"batch_size": 256,
"test_interval": 5000, // in iterations
"max_iter": 100000,
"iter_size": 1,
"save_freq": 50, // in epochs
"test_interval": 50,
"epochs": 780,
"save_freq": 50,
"optimizer": {
"type": "Adam",
"base_lr": 1e-3,
"weight_decay": 5e-4,
"schedule_type": "multistep",
"steps": [100, 200, 300, 400] // in epochs
"steps": [100, 200, 300, 400]
},
"ssd_params": {
"clip": false,
Expand Down
7 changes: 3 additions & 4 deletions examples/torch/object_detection/configs/ssd512_vgg_voc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
"rgb": true
},
"batch_size": 160,
"max_iter": 120000,
"iter_size": 1,
"save_freq": 50, // in epochs
"epochs": 740,
"save_freq": 50,
"optimizer": {
"type": "Adam",
"base_lr": 1e-4,
Expand All @@ -34,4 +33,4 @@
"clip": false,
"flip": true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
"normalize_coef": 255,
"rgb": true
},
"max_iter": 5000,
"epochs": 78,
"batch_size": 64,
"iter_size": 1,
"save_freq": 3, // in epochs
"save_freq": 3,
"optimizer": {
"type": "Adam",
"base_lr": 1e-4,
Expand All @@ -41,4 +40,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
"normalize_coef": 255,
"rgb": true
},
"max_iter": 30000,
"epochs": 310,
"batch_size": 96,
"iter_size": 1,
"save_freq": 3, // in epochs
"epochs": 111,
"save_freq": 30,
"optimizer": {
"type": "Adam",
"base_lr": 1e-4,
Expand Down
54 changes: 26 additions & 28 deletions examples/torch/object_detection/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,28 +350,27 @@ def train_step(batch_iterator, compression_ctrl, config, criterion, net, train_d
batch_loss_l = torch.tensor(0.).to(config.device)
batch_loss_c = torch.tensor(0.).to(config.device)
batch_loss = torch.tensor(0.).to(config.device)
for _ in range(0, config.iter_size):
# load train data
try:
images, targets = next(batch_iterator)
except StopIteration:
logger.debug("StopIteration: can not load batch")
batch_iterator = iter(train_data_loader)
break

images = images.to(config.device)
targets = [anno.requires_grad_(False).to(config.device) for anno in targets]

# forward
out = net(images)
# backprop
loss_l, loss_c = criterion(out, targets)
loss_comp = compression_ctrl.loss()
loss = loss_l + loss_c + loss_comp
batch_loss += loss
loss.backward()
batch_loss_l += loss_l
batch_loss_c += loss_c
# load train data
try:
images, targets = next(batch_iterator)
except StopIteration:
logger.debug("StopIteration: can not load batch")
batch_iterator = iter(train_data_loader)

images = images.to(config.device)
targets = [anno.requires_grad_(False).to(config.device) for anno in targets]

# forward
out = net(images)
# backprop
loss_l, loss_c = criterion(out, targets)
loss_comp = compression_ctrl.loss()
loss = loss_l + loss_c + loss_comp
batch_loss += loss
loss.backward()
batch_loss_l += loss_l
batch_loss_c += loss_c

return batch_iterator, batch_loss, batch_loss_c, batch_loss_l, loss_comp


Expand All @@ -386,9 +385,11 @@ def train(net, compression_ctrl, train_data_loader, test_data_loader, criterion,

best_mAp = 0
best_compression_stage = CompressionStage.UNCOMPRESSED
test_freq_in_epochs = max(config.test_interval // epoch_size, 1)
test_freq_in_epochs = config.test_interval
if config.test_interval is None:
test_freq_in_epochs = 1

max_epochs = config['max_iter'] // epoch_size
max_epochs = config['epochs']

for epoch in range(config.start_epoch, max_epochs):
compression_ctrl.scheduler.epoch_step(epoch)
Expand Down Expand Up @@ -489,10 +490,7 @@ def train_epoch(compression_ctrl, net, config, train_data_loader, criterion, opt
)
optimizer.step()

batch_loss_l = batch_loss_l / config.iter_size
batch_loss_c = batch_loss_c / config.iter_size
model_loss = (batch_loss_l + batch_loss_c) / config.iter_size
batch_loss = batch_loss / config.iter_size
model_loss = batch_loss_l + batch_loss_c

loc_loss += batch_loss_l.item()
conf_loss += batch_loss_c.item()
Expand Down

0 comments on commit 8f9fcfb

Please sign in to comment.