-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcell10.py
More file actions
58 lines (45 loc) · 2.16 KB
/
cell10.py
File metadata and controls
58 lines (45 loc) · 2.16 KB
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
num_epochs = 200
# our dataset has two classes only - background and crater
num_classes = 2
# use our dataset and defined transformations
dataset = CraterDataset('/kaggle/working/train', get_transform(train=True))
dataset_test = CraterDataset('../input/martianlunar-crater-detection-dataset/craters/test', get_transform(train=False))
# define training and validation data loaders
data_loader = torch.utils.data.DataLoader(
dataset, batch_size=8, shuffle=True, num_workers=2,
collate_fn=utils.collate_fn)
data_loader_test = torch.utils.data.DataLoader(
dataset_test, batch_size=1, shuffle=False, num_workers=2,
collate_fn=utils.collate_fn)
# get the model using our helper function
model = get_model_bbox(num_classes)
model.to(device)
# construct an optimizer
params = [p for p in model.parameters() if p.requires_grad]
optimizer = torch.optim.SGD(params, lr=0.005, # Feel free to play with values
momentum=0.9, weight_decay=0)
# Defining learning rate scheduler
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
step_size=20,
gamma=0.2)
result_mAP = []
best_epoch = None
# Let's train!
for epoch in range(num_epochs):
# train for one epoch, printing every 10 iterations
train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=50)
# update the learning rate
lr_scheduler.step()
# evaluate on the test dataset
results = evaluate(model, data_loader_test, device=device)
# saves results of mAP @ IoU = 0.5
result_mAP.append(results.coco_eval['bbox'].stats[1])
#save the best model so far
if result_mAP[-1] == max(result_mAP):
best_save_path = os.path.join(f'Crater_bestmodel_noaug_sgd(wd=0)_8batch-epoch{epoch}.pth')
torch.save(model.state_dict(), best_save_path)
best_epoch = int(epoch)
print(f'\n\nmodel from epoch number {epoch} saved!\n result is {max(result_mAP)}\n\n')
save_path = os.path.join(f'Crater_noaug_sgd_2batch-lastepoch{num_epochs-1}.pth')
torch.save(model.state_dict(), save_path)
print(f'model from last epoch(no.{num_epochs-1}) saved')