-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_assignment_p1.py
139 lines (109 loc) · 4.26 KB
/
run_assignment_p1.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
import math
import os
import torch
from torch import nn
from torch.optim import Adadelta
from torch.optim.lr_scheduler import StepLR
from src.NNModule import NNModule
from src.cuda import is_using_cuda
from src.load_cifar10 import load_cifar10
from src.loss_functions import nll_loss_fn
from src.parse_dir_structure import parse_dir_structure
from src.path_functions import path_join, get_relative_path
from src.summary import summary
DEVICE_NAME = None
DATA_FOLDER = get_relative_path(__file__, "data")
DRY_RUN = False
SAVE_ALL_MODEL = False
VERBOSE_LOG_FILE = False
UPLOAD_WITH_RCLONE = False
class NNModel(NNModule):
def __init__(self, out_features, device: torch.device, log_dir: str):
super().__init__(log_dir, device)
self.conv_nn = nn.Sequential(
nn.Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1)),
nn.ReLU(inplace=True),
nn.Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1)),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),
nn.Dropout(0.25)
)
self.flatten = nn.Sequential(
nn.Flatten(start_dim=1)
)
self.fc_nn = nn.Sequential(
nn.Linear(64 * 14 * 14, 128),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(128, out_features),
nn.LogSoftmax(dim=1)
)
def forward(self, x):
x = self.conv_nn(x)
x = self.flatten(x)
x = self.fc_nn(x)
return x
def assignment_p1(
name="assignment_p1",
batch_size=64,
epochs=14,
gamma=0.7,
learning_rate=1,
loss_fn=nll_loss_fn,
device_name=DEVICE_NAME,
data_folder=DATA_FOLDER,
):
name, models_path, tensorboard_path = parse_dir_structure(get_relative_path(__file__, data_folder), name)
device, is_cuda = is_using_cuda(device_name)
log_file = path_join(data_folder, f"{name}_logs.txt")
rclone_parameters = "--log-file=rclone.log --log-level INFO > /dev/null 2>&1"
train_loader, test_loader, classes = load_cifar10(
path=path_join(data_folder, "cifar10"),
batch_size=batch_size,
is_cuda=is_cuda
)
model = NNModel(
out_features=len(classes),
device=device,
log_dir=tensorboard_path,
)
optimizer = Adadelta(model.parameters(), lr=learning_rate)
scheduler = StepLR(optimizer, step_size=1, gamma=gamma)
model.compile(
optimizer=optimizer,
scheduler=scheduler,
loss_fn=loss_fn
)
if VERBOSE_LOG_FILE:
with open(log_file, "a+") as file:
file.write(str(model) + "\n\n")
file.write(summary(model, (3, 32, 32), device) + "\n\n")
for epoch in range(epochs):
train_loss, train_accuracy, test_loss, test_accuracy = model.fit(train_loader, test_loader, epoch)
str_epoch = str(epoch).zfill(math.ceil(math.log10(epochs)))
print_str = f'({epoch})' \
f' Training Loss: {train_loss:.4f},' \
f' Training Accuracy: {100. * train_accuracy:.0f}%' \
f' Testing Loss: {test_loss:.4f},' \
f' Testing Accuracy: {100. * test_accuracy:.0f}%\n'
print(print_str)
if VERBOSE_LOG_FILE:
with open(log_file, "a+") as file:
file.write(print_str)
if SAVE_ALL_MODEL:
torch.save(model.state_dict(), path_join(models_path, f"{name}_{str_epoch}_cifar10.pt"))
if epoch % 4 == 3 and UPLOAD_WITH_RCLONE:
os.system(f"./rclone move \"{path_join(data_folder, 'models')}\" drive:models {rclone_parameters}")
if DRY_RUN:
break
if not SAVE_ALL_MODEL:
torch.save(model.state_dict(), path_join(models_path, f"{name}_cifar10.pt"))
model.close()
if UPLOAD_WITH_RCLONE:
rclone_parameters = "--delete-empty-src-dirs " + rclone_parameters
os.system(f"./rclone move \"{path_join(data_folder, 'models')}\" drive:models {rclone_parameters}")
os.system(f"./rclone move \"{path_join(data_folder, 'tensorboard')}\" drive:tensorboard {rclone_parameters}")
os.system(f"./rclone move \"{path_join(data_folder, f'{name}_logs.txt')}\" drive: {rclone_parameters}")
return model
if __name__ == '__main__':
assignment_p1()