-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcell9.py
More file actions
59 lines (45 loc) · 2.17 KB
/
cell9.py
File metadata and controls
59 lines (45 loc) · 2.17 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
59
# train on the GPU or on the CPU, if a GPU is not available
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
k_folds = 5
num_epochs = 50
# 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_val = CraterDataset('/kaggle/working/train', get_transform(train=False))
# Define the K-fold Cross Validator
kfold = KFold(n_splits=k_folds, shuffle=True)
# Start print
print('--------------------------------')
# K-fold Cross Validation model evaluation
for fold, (train_ids, val_ids) in enumerate(kfold.split(dataset)):
print(f'FOLD {fold}')
print('--------------------------------')
dataset_subset = torch.utils.data.Subset(dataset, list(train_ids))
dataset_val_subset = torch.utils.data.Subset(dataset_val, list(val_ids))
# define training and validation data loaders
data_loader = torch.utils.data.DataLoader(
dataset_subset, batch_size=8, shuffle=True, num_workers=2,
collate_fn=utils.collate_fn)
data_loader_val = torch.utils.data.DataLoader(
dataset_val_subset, 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, # Check if beneficial
momentum=0.9, weight_decay=0)
# and a learning rate scheduler
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
step_size=10,
gamma=0.1)
# let's train!
for epoch in range(num_epochs):
# train for one epoch, printing every 50 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
evaluate(model, data_loader_val, device=device)