-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkernel.py
283 lines (213 loc) · 9.3 KB
/
kernel.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
#!/usr/bin/env python
import multiprocessing
import os
import time
import numpy as np
import pandas as pd
from typing import Any, Optional, Tuple
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
import torch.backends.cudnn as cudnn
from torch.utils.data import TensorDataset, DataLoader, Dataset
from sklearn.preprocessing import LabelEncoder
from PIL import Image
from tqdm import tqdm
IN_KERNEL = os.environ.get('KAGGLE_WORKING_DIR') is not None
MIN_SAMPLES_PER_CLASS = 50
BATCH_SIZE = 512
LEARNING_RATE = 1e-3
LR_STEP = 3
LR_FACTOR = 0.5
NUM_WORKERS = multiprocessing.cpu_count()
MAX_STEPS_PER_EPOCH = 15000
NUM_EPOCHS = 2 ** 32
LOG_FREQ = 500
NUM_TOP_PREDICTS = 20
TIME_LIMIT = 9 * 60 * 60
class ImageDataset(torch.utils.data.Dataset):
def __init__(self, dataframe: pd.DataFrame, mode: str) -> None:
print(f'creating data loader - {mode}')
assert mode in ['train', 'val', 'test']
self.df = dataframe
self.mode = mode
transforms_list = [transforms.RandomHorizontalFlip()] if self.mode == 'train' else []
transforms_list.extend([
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
self.transforms = transforms.Compose(transforms_list)
def __getitem__(self, index: int) -> Any:
''' Returns: tuple (sample, target) '''
filename = self.df.id.values[index]
part = 1 if self.mode == 'test' or filename[0] in '01234567' else 2
directory = 'test' if self.mode == 'test' else 'train_' + filename[0]
sample = Image.open(f'../input/google-landmarks-2019-64x64-part{part}/{directory}/{self.mode}_64/{filename}.jpg')
assert sample.mode == 'RGB'
image = self.transforms(sample)
if self.mode == 'test':
return image
else:
return image, self.df.landmark_id.values[index]
def __len__(self) -> int:
return self.df.shape[0]
def GAP(predicts: torch.Tensor, confs: torch.Tensor, targets: torch.Tensor) -> float:
''' Simplified GAP@1 metric: only one prediction per sample is supported '''
assert len(predicts.shape) == 1
assert len(confs.shape) == 1
assert len(targets.shape) == 1
assert predicts.shape == confs.shape and confs.shape == targets.shape
_, indices = torch.sort(confs, descending=True)
confs = confs.cpu().numpy()
predicts = predicts[indices].cpu().numpy()
targets = targets[indices].cpu().numpy()
res, true_pos = 0.0, 0
for i, (c, p, t) in enumerate(zip(confs, predicts, targets)):
rel = int(p == t)
true_pos += rel
res += true_pos / (i + 1) * rel
res /= targets.shape[0] # FIXME: incorrect, not all test images depict landmarks
return res
class AverageMeter:
''' Computes and stores the average and current value '''
def __init__(self) -> None:
self.reset()
def reset(self) -> None:
self.val = 0.0
self.avg = 0.0
self.sum = 0.0
self.count = 0
def update(self, val: float, n: int = 1) -> None:
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def load_data() -> 'Tuple[DataLoader[np.ndarray], DataLoader[np.ndarray], LabelEncoder, int]':
torch.multiprocessing.set_sharing_strategy('file_system')
cudnn.benchmark = True
# only use classes which have at least MIN_SAMPLES_PER_CLASS samples
print('loading data...')
df = pd.read_csv('../input/google-landmarks-2019-64x64-part1/train.csv')
df.drop(columns='url', inplace=True)
counts = df.landmark_id.value_counts()
selected_classes = counts[counts >= MIN_SAMPLES_PER_CLASS].index
num_classes = selected_classes.shape[0]
print('classes with at least N samples:', num_classes)
train_df = df.loc[df.landmark_id.isin(selected_classes)].copy()
print('train_df', train_df.shape)
test_df = pd.read_csv('../input/google-landmarks-2019-64x64-part1/test.csv', dtype=str)
test_df.drop(columns='url', inplace=True)
print('test_df', test_df.shape)
# filter non-existing test images
exists = lambda img: os.path.exists(f'../input/google-landmarks-2019-64x64-part1/test/test_64/{img}.jpg')
test_df = test_df.loc[test_df.id.apply(exists)].copy()
print('test_df after filtering', test_df.shape)
assert test_df.shape[0] > 112000
label_encoder = LabelEncoder()
label_encoder.fit(train_df.landmark_id.values)
print('found classes', len(label_encoder.classes_))
assert len(label_encoder.classes_) == num_classes
train_df.landmark_id = label_encoder.transform(train_df.landmark_id)
train_dataset = ImageDataset(train_df, mode='train')
test_dataset = ImageDataset(test_df, mode='test')
train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE,
shuffle=False, num_workers=NUM_WORKERS, drop_last=True)
test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE,
shuffle=False, num_workers=NUM_WORKERS)
return train_loader, test_loader, label_encoder, num_classes
def train(train_loader: Any, model: Any, criterion: Any, optimizer: Any,
epoch: int, lr_scheduler: Any) -> None:
print(f'epoch {epoch}')
batch_time = AverageMeter()
losses = AverageMeter()
avg_score = AverageMeter()
model.train()
num_steps = min(len(train_loader), MAX_STEPS_PER_EPOCH)
print(f'total batches: {num_steps}')
end = time.time()
lr_str = ''
for i, (input_, target) in enumerate(train_loader):
if i >= num_steps:
break
output = model(input_.cuda())
loss = criterion(output, target.cuda())
confs, predicts = torch.max(output.detach(), dim=1)
avg_score.update(GAP(predicts, confs, target))
losses.update(loss.data.item(), input_.size(0))
optimizer.zero_grad()
loss.backward()
optimizer.step()
batch_time.update(time.time() - end)
end = time.time()
if i % LOG_FREQ == 0:
print(f'{epoch} [{i}/{num_steps}]\t'
f'time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
f'loss {losses.val:.4f} ({losses.avg:.4f})\t'
f'GAP {avg_score.val:.4f} ({avg_score.avg:.4f})'
+ lr_str)
if has_time_run_out():
break
print(f' * average GAP on train {avg_score.avg:.4f}')
def inference(data_loader: Any, model: Any) -> Tuple[torch.Tensor, torch.Tensor,
Optional[torch.Tensor]]:
''' Returns predictions and targets, if any. '''
model.eval()
activation = nn.Softmax(dim=1)
all_predicts, all_confs, all_targets = [], [], []
with torch.no_grad():
for i, data in enumerate(tqdm(data_loader, disable=IN_KERNEL)):
if data_loader.dataset.mode != 'test':
input_, target = data
else:
input_, target = data, None
output = model(input_.cuda())
output = activation(output)
confs, predicts = torch.topk(output, NUM_TOP_PREDICTS)
all_confs.append(confs)
all_predicts.append(predicts)
if target is not None:
all_targets.append(target)
predicts = torch.cat(all_predicts)
confs = torch.cat(all_confs)
targets = torch.cat(all_targets) if len(all_targets) else None
return predicts, confs, targets
def generate_submission(test_loader: Any, model: Any, label_encoder: Any) -> np.ndarray:
sample_sub = pd.read_csv('../input/landmark-recognition-2019/recognition_sample_submission.csv')
predicts_gpu, confs_gpu, _ = inference(test_loader, model)
predicts, confs = predicts_gpu.cpu().numpy(), confs_gpu.cpu().numpy()
labels = [label_encoder.inverse_transform(pred) for pred in predicts]
print('labels')
print(np.array(labels))
print('confs')
print(np.array(confs))
sub = test_loader.dataset.df
def concat(label: np.ndarray, conf: np.ndarray) -> str:
return ' '.join([f'{L} {c}' for L, c in zip(label, conf)])
sub['landmarks'] = [concat(label, conf) for label, conf in zip(labels, confs)]
sample_sub = sample_sub.set_index('id')
sub = sub.set_index('id')
sample_sub.update(sub)
sample_sub.to_csv('submission.csv')
def has_time_run_out() -> bool:
return time.time() - global_start_time > TIME_LIMIT - 500
if __name__ == '__main__':
global_start_time = time.time()
train_loader, test_loader, label_encoder, num_classes = load_data()
model = torchvision.models.resnet50(pretrained=True)
model.avg_pool = nn.AdaptiveAvgPool2d(1)
model.fc = nn.Linear(model.fc.in_features, num_classes)
model.cuda()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=LR_STEP,
gamma=LR_FACTOR)
for epoch in range(1, NUM_EPOCHS + 1):
print('-' * 50)
train(train_loader, model, criterion, optimizer, epoch, lr_scheduler)
lr_scheduler.step()
if has_time_run_out():
break
print('inference mode')
generate_submission(test_loader, model, label_encoder)