-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
187 lines (140 loc) · 5.96 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import argparse
import json
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from datasets.shapes_dataset import ShapesClassificationDataset, ShapesCounterDataset, ShapesCounterDataset135
from datasets.transformers import RandomVerticalFlip, RandomHorizontalFlip, RandomRightRotation
from metrics import counter_loss, counter135_loss
from models.shapes_classifier import ShapesClassifier
from models.shapes_counter import ShapesCounter, ShapesCounter135
from training import train_and_evaluate_model, setup_neptune, upload_file
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
BATCH_SIZE = 100
WORKERS = 2
def train_classifier(transform_images, transform_all):
train_set = ShapesClassificationDataset(
"data/train.csv",
"data/images",
transform_all=transform_all,
transform_images=transform_images
)
validation_set = ShapesClassificationDataset(
"data/val.csv",
"data/images",
transform_all=None,
transform_images=transform_images
)
train_loader = torch.utils.data.DataLoader(train_set, batch_size=BATCH_SIZE,
shuffle=True, num_workers=WORKERS)
validation_loader = torch.utils.data.DataLoader(validation_set, batch_size=BATCH_SIZE,
shuffle=False, num_workers=WORKERS)
model = ShapesClassifier().to(device)
criterion = nn.BCEWithLogitsLoss(reduction='sum')
optimizer = optim.Adam(model.parameters(), lr=0.001)
hist = train_and_evaluate_model(model, criterion, optimizer,
train_loader, train_set,
validation_loader, validation_set,
device, num_epochs=150)
torch.save(model, 'classifier.pt')
return hist
def train_counter(transform_images, transform_all):
train_set = ShapesCounterDataset(
"data/train.csv",
"data/images",
transform_all=transform_all,
transform_images=transform_images
)
validation_set = ShapesCounterDataset(
"data/val.csv",
"data/images",
transform_all=None,
transform_images=transform_images
)
train_loader = torch.utils.data.DataLoader(train_set, batch_size=BATCH_SIZE,
shuffle=True, num_workers=WORKERS)
validation_loader = torch.utils.data.DataLoader(validation_set, batch_size=BATCH_SIZE,
shuffle=False, num_workers=WORKERS)
model = ShapesCounter().to(device)
criterion = counter_loss
optimizer = optim.Adam(model.parameters(), lr=0.01)
hist = train_and_evaluate_model(model, criterion, optimizer,
train_loader, train_set,
validation_loader, validation_set,
device, num_epochs=300)
return hist
def train_counter135(transform_images, transform_all):
train_set = ShapesCounterDataset135(
"data/train.csv",
"data/images",
transform_all=transform_all,
transform_images=transform_images
)
validation_set = ShapesCounterDataset135(
"data/val.csv",
"data/images",
transform_all=None,
transform_images=transform_images
)
train_loader = torch.utils.data.DataLoader(train_set, batch_size=BATCH_SIZE,
shuffle=True, num_workers=WORKERS)
validation_loader = torch.utils.data.DataLoader(validation_set, batch_size=BATCH_SIZE,
shuffle=False, num_workers=WORKERS)
model = ShapesCounter135().to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.01)
hist = train_and_evaluate_model(model, criterion, optimizer,
train_loader, train_set,
validation_loader, validation_set,
device, num_epochs=100)
return hist
def main(args):
global BATCH_SIZE, WORKERS
if args.neptune:
setup_neptune(args.model)
if args.entropy:
BATCH_SIZE = 1000
WORKERS = 4
print('-' * 10)
print(f'Settings: batch = {BATCH_SIZE}, workers = {WORKERS}')
print('-' * 10)
transform_images = transforms.Compose([
transforms.Grayscale(num_output_channels=1),
transforms.ToTensor(),
transforms.Normalize(0.5, 0.5)
])
transform_all = transforms.Compose([
RandomHorizontalFlip(0.5),
RandomVerticalFlip(0.5),
RandomRightRotation(0.5),
])
hist = None
if args.model == 'classifier':
hist = train_classifier(transform_images, transform_all)
elif args.model == 'counter':
hist = train_counter(transform_images, transform_all)
elif args.model == 'counter135':
hist = train_counter135(transform_images, transform_all)
else:
raise ValueError('Unknown model')
if args.file:
with open(args.file, 'w') as f:
json.dump(hist, f)
if args.neptune:
upload_file(args.file)
print('-' * 10)
print('Finished Training')
print('-' * 10)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Work with shapes networks.')
parser.add_argument('-n', '--neptune', action='store_true',
help='use neptune.ai for logging')
parser.add_argument('-m', '--model', action='store', type=str, required=True,
help='which model should be trained')
parser.add_argument('-e', '--entropy', action='store_true',
help='development or training environment')
parser.add_argument('-f', '--file', action='store', type=str,
help='file for storing output for plotting')
args = parser.parse_args()
main(args)