-
Notifications
You must be signed in to change notification settings - Fork 2
/
experiment11.py
292 lines (185 loc) · 7.31 KB
/
experiment11.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python
# coding: utf-8
# ## Load libraries
# In[1]:
get_ipython().system('pip install -q -r requirements.txt')
# In[1]:
import sys
import os
import numpy as np
import pandas as pd
from PIL import Image
import torch
import torch.nn as nn
import torch.utils.data as D
from torch.optim.lr_scheduler import ExponentialLR
import torch.nn.functional as F
from torch.autograd import Variable
from torchvision import transforms
from ignite.engine import Events
from scripts.ignite import create_supervised_evaluator, create_supervised_trainer
from ignite.metrics import Loss, Accuracy
from ignite.contrib.handlers.tqdm_logger import ProgressBar
from ignite.handlers import EarlyStopping, ModelCheckpoint
from ignite.contrib.handlers import LinearCyclicalScheduler, CosineAnnealingScheduler
from tqdm import tqdm_notebook
from sklearn.model_selection import train_test_split
from efficientnet_pytorch import EfficientNet
from scripts.evaluate import eval_model
from scripts.focalloss import FocalLoss
import warnings
warnings.filterwarnings('ignore')
# ## Define dataset and model
# In[2]:
img_dir = '../input/rxrxairgb'
path_data = '../input/rxrxaicsv'
device = 'cuda'
batch_size = 44
torch.manual_seed(0)
model_name = 'efficientnet-b0'
# In[3]:
jitter = (0.6, 1.4)
class ImagesDS(D.Dataset):
# taken textbook from https://arxiv.org/pdf/1812.01187.pdf
transform_train = transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.ColorJitter(brightness=jitter, contrast=jitter, saturation=jitter, hue=.1),
transforms.RandomHorizontalFlip(p=0.5),
# PCA Noise should go here,
transforms.ToTensor(),
transforms.Normalize(mean=(123.68, 116.779, 103.939), std=(58.393, 57.12, 57.375))
])
transform_validation = transforms.Compose([
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=(123.68, 116.779, 103.939), std=(58.393, 57.12, 57.375))
])
def __init__(self, df, img_dir=img_dir, mode='train', validation=False, site=1):
self.records = df.to_records(index=False)
self.site = site
self.mode = mode
self.img_dir = img_dir
self.len = df.shape[0]
self.validation = validation
@staticmethod
def _load_img_as_tensor(file_name, validation):
with Image.open(file_name) as img:
if not validation:
return ImagesDS.transform_train(img)
else:
return ImagesDS.transform_validation(img)
def _get_img_path(self, index, site=1):
experiment, well, plate = self.records[index].experiment, self.records[index].well, self.records[index].plate
return f'{self.img_dir}/{self.mode}/{experiment}_{plate}_{well}_s{site}.jpeg'
def __getitem__(self, index):
img1, img2 = [self._load_img_as_tensor(self._get_img_path(index, site), self.validation) for site in [1,2]]
if self.mode == 'train':
return img1, img2, int(self.records[index].sirna)
else:
return img1, img2, self.records[index].id_code
def __len__(self):
return self.len
# In[4]:
# dataframes for training, cross-validation, and testing
df = pd.read_csv(path_data+'/train.csv')
df_train, df_val = train_test_split(df, test_size = 0.1, random_state=42)
df_test = pd.read_csv(path_data+'/test.csv')
# pytorch training dataset & loader
ds = ImagesDS(df_train, mode='train', validation=False)
loader = D.DataLoader(ds, batch_size=batch_size, shuffle=True, num_workers=4)
# pytorch cross-validation dataset & loader
ds_val = ImagesDS(df_val, mode='train', validation=True)
val_loader = D.DataLoader(ds_val, batch_size=batch_size, shuffle=True, num_workers=4)
# pytorch test dataset & loader
ds_test = ImagesDS(df_test, mode='test', validation=True)
tloader = D.DataLoader(ds_test, batch_size=batch_size, shuffle=False, num_workers=4)
# In[5]:
class EfficientNetTwoInputs(nn.Module):
def __init__(self):
super(EfficientNetTwoInputs, self).__init__()
self.classes = 1108
model = model = EfficientNet.from_pretrained(model_name, num_classes=1108)
model._fc = nn.Identity()
self.resnet = model
self.fc = nn.Linear(2560, self.classes)
def forward(self, x1, x2):
x1_out = self.resnet(x1)
x2_out = self.resnet(x2)
N, _, _, _ = x1.size()
x1_out = x1_out.view(N, -1)
x2_out = x2_out.view(N, -1)
out = torch.cat((x1_out, x2_out), 1)
out = self.fc(out)
return out
model = EfficientNetTwoInputs()
# In[6]:
criterion = FocalLoss(gamma=2)
optimizer = torch.optim.Adam(model.parameters(), lr=3e-4)
# In[7]:
metrics = {
'loss': Loss(criterion),
'accuracy': Accuracy(),
}
trainer = create_supervised_trainer(model, optimizer, criterion, device=device)
val_evaluator = create_supervised_evaluator(model, metrics=metrics, device=device)
# #### EarlyStopping
# In[8]:
handler = EarlyStopping(patience=10, score_function=lambda engine: engine.state.metrics['accuracy'], trainer=trainer)
val_evaluator.add_event_handler(Events.COMPLETED, handler)
# #### LR Scheduler
# In[9]:
scheduler = CosineAnnealingScheduler(optimizer, 'lr', 3e-4, 1e-7, len(loader))
trainer.add_event_handler(Events.ITERATION_STARTED, scheduler)
@trainer.on(Events.ITERATION_COMPLETED)
def print_lr(engine):
epoch = engine.state.epoch
iteration = engine.state.iteration
if epoch < 3 and iteration % 10 == 0:
print(f'Iteration {iteration} | LR {optimizer.param_groups[0]["lr"]}')
# #### Compute and display metrics
# In[10]:
@trainer.on(Events.EPOCH_COMPLETED)
def compute_and_display_val_metrics(engine):
epoch = engine.state.epoch
metrics = val_evaluator.run(val_loader).metrics
print("Validation Results - Epoch: {} | Average Loss: {:.4f} | Accuracy: {:.4f} "
.format(engine.state.epoch, metrics['loss'], metrics['accuracy']))
# #### Save best epoch only
# In[11]:
get_ipython().system('mkdir -p models')
# In[12]:
def get_saved_model_path(epoch):
return f'models/Model_{model_name}_{epoch}.pth'
best_acc = 0.
best_epoch = 1
best_epoch_file = ''
@trainer.on(Events.EPOCH_COMPLETED)
def save_best_epoch_only(engine):
epoch = engine.state.epoch
global best_acc
global best_epoch
global best_epoch_file
best_acc = 0. if epoch == 1 else best_acc
best_epoch = 1 if epoch == 1 else best_epoch
best_epoch_file = '' if epoch == 1 else best_epoch_file
metrics = val_evaluator.run(val_loader).metrics
if metrics['accuracy'] > best_acc:
prev_best_epoch_file = get_saved_model_path(best_epoch)
if os.path.exists(prev_best_epoch_file):
os.remove(prev_best_epoch_file)
best_acc = metrics['accuracy']
best_epoch = epoch
best_epoch_file = get_saved_model_path(best_epoch)
print(f'\nEpoch: {best_epoch} - New best accuracy! Accuracy: {best_acc}\n\n\n')
torch.save(model.state_dict(), best_epoch_file)
# #### Progress bar - uncomment when testing in notebook
# In[13]:
# pbar = ProgressBar(bar_format='')
# pbar.attach(trainer, output_transform=lambda x: {'loss': x})
# #### Train
# In[14]:
print('Training started\n')
trainer.run(loader, max_epochs=50)
# #### Evaluate
# In[ ]:
eval_model(model, tloader, best_epoch_file, path_data)