Kaggle competition: Humpback Whale Identification
def acc(input:Tensor, targs:Tensor)->Rank0Tensor:
"Compute accuracy with `targs` when `input` is bs * n_classes."
n = targs.shape[0]
input = input.argmax(dim=-1 ).view(n,-1)
 targs = targs.view(n,-1)
 return(input==targs ).float().mean()<init_hyperparams>
TRAIN_DF = '.. /input/humpback-whale-identification/train.csv'
SUB_Df = '.. /input/humpback-whale-identification/sample_submission.csv'
TRAIN = '.. /input/humpback-whale-identification/train/'
TEST = '.. /input/humpback-whale-identification/test/'
P2H = '.. /input/metadata/p2h.pickle'
P2SIZE = '.. /input/metadata/p2size.pickle'
BB_DF = ".. /input/metadata/bounding_boxes.csv"
i=torch.rand(3,2)
 print(i)
<normalization>
!pip install lap
tagged = dict([(p, w)for _, p, w in read_csv(TRAIN_DF ).to_records() ])
 submit = [p for _, p, _ in read_csv(SUB_Df ).to_records() ]
join = list(tagged.keys())+ submit
len(tagged),len(submit),len(join),list(tagged.items())[:5],submit[:5]
class ArcMarginProduct(nn.Module):
r
def __init__(self, in_features, out_features=5004):
super(ArcMarginProduct, self ).__init__()
self.weight = nn.Parameter(torch.FloatTensor(out_features, in_features))
self.reset_parameters()
def reset_parameters(self):
stdv = 1./ math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
 def forward(self, features):
cosine = F.linear(F.normalize(features), F.normalize(self.weight.cuda()))
return cosine
class Customhead(nn.Module):
r
def __init__(self, in_features, out_features=5004):
super(Customhead, self ).__init__()
body = create_body(m.resnet50, True, -2)
 nf = num_features_model(nn.Sequential(*body.children())) * 2
self.head=create_head(nf, 1024,[2048], ps=0.5, bn_final=False)
 self.arc_margin=ArcMarginProduct(in_features,out_features)
 def forward(self, features):
x=self.head(features)
 cosine = self.arc_margin(x)
 cosine = cosine.clamp(-1, 1)
 return cosine<normalization>
import gzip
import pickle
from lap import lapjv
from math import sqrt
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from imagehash import phash
from keras.utils import Sequence
import time
class CustomheadRes(nn.Module):
r
def __init__(self, in_features, out_features=5004):
super(CustomheadRes, self ).__init__()
body = create_body(m.densenet121, True, -1)
 nf = num_features_model(nn.Sequential(*body.children())) * 2
self.head=create_head(nf, 1024, ps=0.5, bn_final=False)
 self.arc_margin=ArcMarginProduct(in_features,out_features)
 def forward(self, features):
x=self.head(features)
 cosine = self.arc_margin(x)
 cosine = cosine.clamp(-1, 1)
 return cosine<feature_engineering>
import platform
import random
import keras
import sys
from scipy.ndimage import affine_transform
from keras import backend as K
from keras.preprocessing.image import img_to_array,array_to_img
class ArcFaceLoss(nn.modules.Module):
def __init__(self,s=30.0,m=0.5):
super(ArcFaceLoss, self ).__init__()
self.classify_loss = nn.CrossEntropyLoss()
self.s = s
self.easy_margin = False
self.cos_m = math.cos(m)
 self.sin_m = math.sin(m)
 self.th = math.cos(math.pi - m)
 self.mm = math.sin(math.pi - m)* m
def forward(self, inputs, labels, epoch=0,reduction=None):
cosine = inputs
sine = torch.sqrt(1.0 - torch.pow(cosine, 2))
phi = cosine * self.cos_m - sine * self.sin_m
if self.easy_margin:
phi = torch.where(cosine > 0, phi, cosine)
 else:
phi = torch.where(cosine > self.th, phi, cosine - self.mm)
 one_hot = torch.zeros(cosine.size() , device='cuda')
 one_hot.scatter_(1, labels.view(-1, 1 ).long() , 1)
 output =(one_hot * phi)+(( 1.0 - one_hot)* cosine)
 output *= self.s
loss1 = self.classify_loss(output, labels)
 loss2 = self.classify_loss(cosine, labels)
 gamma=1
loss=(loss1+gamma*loss2)/(1+gamma)
 return loss.mean()<set_options>
with open(P2H, 'rb')as f:
p2h = pickle.load(f)
 h2ps = {}
for p, h in p2h.items() :
if h not in h2ps: h2ps[h] = []
if p not in h2ps[h]: h2ps[h].append(p)
 with open(P2SIZE, 'rb')as f:
p2size = pickle.load(f)
 p2bb = pd.read_csv(BB_DF ).set_index("Image")
 old_stderr = sys.stderr
sys.stderr = open('/dev/null' if platform.system() != 'Windows' else 'nul', 'w')
 sys.stderr = old_stderr
img_shape =(384, 384, 1)
 anisotropy = 2.15
crop_margin = 0.1
f1_score = partial(fbeta, thresh=0.4, beta=1)
 acc_02 = partial(accuracy_thresh, thresh=0.2)
 custom_head=Customhead(1024,5004)
 custom_headres=CustomheadRes(1024,5004)
 ar=ArcFaceLoss().cuda()
gc.collect()
learn1 = create_cnn(
 data2,
resnet501,
cut=-2,
split_on=_resnet_split,
lin_ftrs=[1024],
custom_head=custom_head,
loss_func=ar,
path=path1,
metrics=[accuracy], callback_fns= partial(GradientClipping, clip=1))
learn1.callback_fns.append(partial(SaveModelCallback,monitor='val_loss',mode='min'))
learn1.callback_fns.append(partial(ReduceLROnPlateauCallback, min_delta=1e-5, patience=3))
learn2 = create_cnn(
 data2,
dense,
cut=-1,
split_on=_densenet_split,
lin_ftrs=[512],
custom_head=custom_headres,
loss_func=ar,
path=path1,
metrics=[accuracy], callback_fns= partial(GradientClipping, clip=1))
learn2.callback_fns.append(partial(ReduceLROnPlateauCallback, min_delta=1e-5, patience=3)) 
<train_model>
def prefer(ps):
if len(ps)== 1: return ps[0]
best_p = ps[0]
best_s = p2size[best_p]
for i in range(1, len(ps)) :
p = ps[i]
s = p2size[p]
if s[0] * s[1] > best_s[0] * best_s[1]:
best_p = p
best_s = s
return best_p
h2p = {}
for h, ps in h2ps.items() :
h2p[h] = prefer(ps)
 len(h2p), list(h2p.items())[:5]
print('Train_loss',learn1.recorder.losses[-1])
 print('Val loss',learn1.recorder.val_losses)
 print('Accuracy',learn1.recorder.metrics)
<train_model>
def build_transform(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = np.deg2rad(rotation)
 shear = np.deg2rad(shear)
 rotation_matrix = np.array(
 [[np.cos(rotation), np.sin(rotation), 0], [-np.sin(rotation), np.cos(rotation), 0], [0, 0, 1]])
 shift_matrix = np.array([[1, 0, height_shift], [0, 1, width_shift], [0, 0, 1]])
 shear_matrix = np.array([[1, np.sin(shear), 0], [0, np.cos(shear), 0], [0, 0, 1]])
 zoom_matrix = np.array([[1.0 / height_zoom, 0, 0], [0, 1.0 / width_zoom, 0], [0, 0, 1]])
 shift_matrix = np.array([[1, 0, -height_shift], [0, 1, -width_shift], [0, 0, 1]])
 return np.dot(np.dot(rotation_matrix, shear_matrix), np.dot(zoom_matrix, shift_matrix))
print('Train_loss',learn2.recorder.losses[-1])
 print('Val loss',learn2.recorder.val_losses)
 print('Accuracy',learn2.recorder.metrics )<predict_on_test>
def read_cropped_image(p, augment):
if p in h2p:
p = h2p[p]
size_x, size_y = p2size[p]
row = p2bb.loc[p]
x0, y0, x1, y1 = row['x0'], row['y0'], row['x1'], row['y1']
dx = x1 - x0
dy = y1 - y0
x0 -= dx * crop_margin
x1 += dx * crop_margin + 1
y0 -= dy * crop_margin
y1 += dy * crop_margin + 1
if x0 < 0:
x0 = 0
if x1 > size_x:
x1 = size_x
if y0 < 0:
y0 = 0
if y1 > size_y:
y1 = size_y
dx = x1 - x0
dy = y1 - y0
if dx > dy * anisotropy:
dy = 0.5 *(dx / anisotropy - dy)
 y0 -= dy
y1 += dy
else:
dx = 0.5 *(dy * anisotropy - dx)
 x0 -= dx
x1 += dx
trans = np.array([[1, 0, -0.5 * img_shape[0]], [0, 1, -0.5 * img_shape[1]], [0, 0, 1]])
 trans = np.dot(np.array([[(y1 - y0)/ img_shape[0], 0, 0], [0,(x1 - x0)/ img_shape[1], 0], [0, 0, 1]]), trans)
 if augment:
trans = np.dot(build_transform(
 random.uniform(-5, 5),
random.uniform(-5, 5),
random.uniform(0.8, 1.0),
random.uniform(0.8, 1.0),
random.uniform(-0.05 *(y1 - y0), 0.05 *(y1 - y0)) ,
random.uniform(-0.05 *(x1 - x0), 0.05 *(x1 - x0)) 
), trans)
 trans = np.dot(np.array([[1, 0, 0.5 *(y1 + y0)], [0, 1, 0.5 *(x1 + x0)], [0, 0, 1]]), trans)
 img = read_raw_image(p ).convert('L')
 img = img_to_array(img)
 matrix = trans[:2, :2]
offset = trans[:2, 2]
img = img.reshape(img.shape[:-1])
 img = affine_transform(img, matrix, offset, output_shape=img_shape[:-1], order=1, mode='constant',
cval=np.average(img))
img = img.reshape(img_shape)
 img -= np.mean(img, keepdims=True)
 img /= np.std(img, keepdims=True)+ K.epsilon()
return img
learn1.model.eval()
learn1.load('resnet_ar_c424_2')
 preds1,y1 = learn1.TTA(ds_type=DatasetType.Test,beta=0.30,with_loss=False,scale=1.1)
<compute_test_metric>
Lambda, MaxPooling2D, Reshape
def apk(actual, predicted, k=10):
if len(predicted)>k:
predicted = predicted[:k]
score = 0.0
num_hits = 0.0
for i,p in enumerate(predicted):
if p in actual and p not in predicted[:i]:
num_hits += 1.0
score += num_hits /(i+1.0)
 if not actual:
return 0.0
return score / min(len(actual), k)
 def mapk(actual, predicted, k=10):
return np.mean([apk(a,p,k)for a,p in zip(actual, predicted)])
 def sigmoid_np(x):
return 1.0/(1.0 + np.exp(-x))
preds_tv = sigmoid_np(predsv )<define_search_space>
def subblock(x, filter, **kwargs):
x = BatchNormalization()(x)
 y = x
y = Conv2D(filter,(1, 1), activation='relu', **kwargs )(y)
 y = BatchNormalization()(y)
 y = Conv2D(filter,(3, 3), activation='relu', **kwargs )(y)
 y = BatchNormalization()(y)
 y = Conv2D(K.int_shape(x)[-1],(1, 1), **kwargs )(y)
 y = Add()([x, y])
 y = Activation('relu' )(y)
 return y
def build_model(lr, l2, activation='sigmoid'):
regul = regularizers.l2(l2)
 optim = Adam(lr=lr)
 kwargs = {'padding': 'same', 'kernel_regularizer': regul}
inp = Input(shape=img_shape)
 x = Conv2D(64,(9, 9), strides=2, activation='relu', **kwargs )(inp)
 x = MaxPooling2D(( 2, 2), strides=(2, 2))(x)
 for _ in range(2):
x = BatchNormalization()(x)
 x = Conv2D(64,(3, 3), activation='relu', **kwargs )(x)
 x = MaxPooling2D(( 2, 2), strides=(2, 2))(x)
 x = BatchNormalization()(x)
 x = Conv2D(128,(1, 1), activation='relu', **kwargs )(x)
 for _ in range(4):
x = subblock(x, 64, **kwargs)
 x = MaxPooling2D(( 2, 2), strides=(2, 2))(x)
 x = BatchNormalization()(x)
 x = Conv2D(256,(1, 1), activation='relu', **kwargs )(x)
 for _ in range(4):
x = subblock(x, 64, **kwargs)
 x = MaxPooling2D(( 2, 2), strides=(2, 2))(x)
 x = BatchNormalization()(x)
 x = Conv2D(384,(1, 1), activation='relu', **kwargs )(x)
 for _ in range(4):
x = subblock(x, 96, **kwargs)
 x = MaxPooling2D(( 2, 2), strides=(2, 2))(x)
 x = BatchNormalization()(x)
 x = Conv2D(512,(1, 1), activation='relu', **kwargs )(x)
 for _ in range(4):
x = subblock(x, 128, **kwargs)
 x = GlobalMaxPooling2D()(x)
 branch_model = Model(inp, x)
 mid = 32
xa_inp = Input(shape=branch_model.output_shape[1:])
 xb_inp = Input(shape=branch_model.output_shape[1:])
 x1 = Lambda(lambda x: x[0] * x[1] )([xa_inp, xb_inp])
 x2 = Lambda(lambda x: x[0] + x[1] )([xa_inp, xb_inp])
 x3 = Lambda(lambda x: K.abs(x[0] - x[1]))([xa_inp, xb_inp])
 x4 = Lambda(lambda x: K.square(x))(x3)
 x = Concatenate()([x1, x2, x3, x4])
 x = Reshape(( 4, branch_model.output_shape[1], 1), name='reshape1' )(x)
 x = Conv2D(mid,(4, 1), activation='relu', padding='valid' )(x)
 x = Reshape(( branch_model.output_shape[1], mid, 1))(x)
 x = Conv2D(1,(1, mid), activation='linear', padding='valid' )(x)
 x = Flatten(name='flatten' )(x)
 x = Dense(1, use_bias=True, activation=activation, name='weighted-average' )(x)
 head_model = Model([xa_inp, xb_inp], x, name='head')
 img_a = Input(shape=img_shape)
 img_b = Input(shape=img_shape)
 xa = branch_model(img_a)
 xb = branch_model(img_b)
 x = head_model([xa, xb])
 model = Model([img_a, img_b], x)
 model.compile(optim, loss='binary_crossentropy', metrics=['binary_crossentropy', 'acc'])
 return model, branch_model, head_model
model, branch_model, head_model = build_model(64e-5, 0)
 head_model.summary()
i=preds_tv[99,:].argsort(descending=True)
 probs = preds_tv[0,i]
probs[:5]
np.stack(top_5s_v ).shape
labels_list[2] 
<define_search_space>
h2ws = {}
new_whale = 'new_whale'
for p, w in tagged.items() :
if w != new_whale:
h = p2h[p]
if h not in h2ws: h2ws[h] = []
if w not in h2ws[h]: h2ws[h].append(w)
 for h, ws in h2ws.items() :
if len(ws)> 1:
h2ws[h] = sorted(ws)
 w2hs = {}
for h, ws in h2ws.items() :
if len(ws)== 1:
w = ws[0]
if w not in w2hs: w2hs[w] = []
if h not in w2hs[w]: w2hs[w].append(h)
 for w, hs in w2hs.items() :
if len(hs)> 1:
w2hs[w] = sorted(hs )
<compute_test_metric>
train = []
for hs in w2hs.values() :
if len(hs)> 1:
train += hs
random.shuffle(train)
 train_set = set(train)
 w2ts = {}
for w, hs in w2hs.items() :
for h in hs:
if h in train_set:
if w not in w2ts:
w2ts[w] = []
if h not in w2ts[w]:
w2ts[w].append(h)
 for w, ts in w2ts.items() :
w2ts[w] = np.array(ts)
 t2i = {}
for i, t in enumerate(train):
t2i[t] = i
def sigmoid_np(x):
return 1.0/(1.0 + np.exp(-x))
preds_t = sigmoid_np(preds1)
<find_best_params>
try:
segment = False
except ImportError:
print('Module lap not found, emulating with much slower scipy.optimize.linear_sum_assignment')
 segment = True
class TrainingData(Sequence):
def __init__(self, score, steps=1000, batch_size=32):

 super(TrainingData, self ).__init__()
self.score = -score
self.steps = steps
self.batch_size = batch_size
for ts in w2ts.values() :
idxs = [t2i[t] for t in ts]
for i in idxs:
for j in idxs:
self.score[
i, j] = 10000.0
self.on_epoch_end()
def __getitem__(self, index):
start = self.batch_size * index
end = min(start + self.batch_size, len(self.match)+ len(self.unmatch))
size = end - start
assert size > 0
a = np.zeros(( size,)+ img_shape, dtype=K.floatx())
 b = np.zeros(( size,)+ img_shape, dtype=K.floatx())
 c = np.zeros(( size, 1), dtype=K.floatx())
 j = start // 2
for i in range(0, size, 2):
a[i, :, :, :] = read_for_training(self.match[j][0])
 b[i, :, :, :] = read_for_training(self.match[j][1])
 c[i, 0] = 1
a[i + 1, :, :, :] = read_for_training(self.unmatch[j][0])
 b[i + 1, :, :, :] = read_for_training(self.unmatch[j][1])
 c[i + 1, 0] = 0
j += 1
return [a, b], c
def on_epoch_end(self):
if self.steps <= 0: return
self.steps -= 1
self.match = []
self.unmatch = []
_, _, x = lapjv(self.score)
 y = np.arange(len(x), dtype=np.int32)
 for ts in w2ts.values() :
d = ts.copy()
while True:
random.shuffle(d)
 if not np.any(ts == d): break
for ab in zip(ts, d): self.match.append(ab)
 for i, j in zip(x, y):
if i == j:
print(self.score)
 print(x)
 print(y)
 print(i, j)
 assert i != j
self.unmatch.append(( train[i], train[j]))
self.score[x, y] = 10000.0
self.score[y, x] = 10000.0
random.shuffle(self.match)
 random.shuffle(self.unmatch)
 assert len(self.match)== len(train)and len(self.unmatch)== len(train)
 def __len__(self):
return(len(self.match)+ len(self.unmatch)+ self.batch_size - 1)// self.batch_size
i=preds_t[99,:].argsort(descending=True)
 probs = preds_t[99,i]
probs[:5]<save_to_csv>
def read_for_training(p):

 return read_cropped_image(p, True)
 def read_for_validation(p):

 return read_cropped_image(p, False )
top_5_classes = []
for top_5 in top_5s:
top_5_classes.append(' '.join([t for t in top_5]))
sub = pd.DataFrame({'Image': [path.name for path in data1.test_ds.x.items]})
 sub['Id'] = top_5_classes
sub.to_csv('pred_res484.csv',index=False)
 FileLink('pred_res484.csv')
<prepare_x_and_y>
score = np.random.random_sample(size=(len(train), len(train)))
 data = TrainingData(score)
(a, b), c = data[0]
a.shape, b.shape, c.shape
<import_modules>
class FeatureGen(Sequence):
def __init__(self, data, batch_size=64, verbose=1):
super(FeatureGen, self ).__init__()
self.data = data
self.batch_size = batch_size
self.verbose = verbose
if self.verbose > 0: self.progress = tqdm(total=len(self), desc='Features')
 def __getitem__(self, index):
start = self.batch_size * index
size = min(len(self.data)- start, self.batch_size)
 a = np.zeros(( size,)+ img_shape, dtype=K.floatx())
 for i in range(size): a[i, :, :, :] = read_for_validation(self.data[start + i])
 if self.verbose > 0:
self.progress.update()
if self.progress.n >= len(self): self.progress.close()
return a
def __len__(self):
return(len(self.data)+ self.batch_size - 1)// self.batch_size
class ScoreGen(Sequence):
def __init__(self, x, y=None, batch_size=2048, verbose=1):
super(ScoreGen, self ).__init__()
self.x = x
self.y = y
self.batch_size = batch_size
self.verbose = verbose
if y is None:
self.y = self.x
self.ix, self.iy = np.triu_indices(x.shape[0], 1)
 else:
self.iy, self.ix = np.indices(( y.shape[0], x.shape[0]))
self.ix = self.ix.reshape(( self.ix.size,))
self.iy = self.iy.reshape(( self.iy.size,))
self.subbatch =(len(self.x)+ self.batch_size - 1)// self.batch_size
if self.verbose > 0:
self.progress = tqdm(total=len(self), desc='Scores')
 def __getitem__(self, index):
start = index * self.batch_size
end = min(start + self.batch_size, len(self.ix))
a = self.y[self.iy[start:end], :]
b = self.x[self.ix[start:end], :]
if self.verbose > 0:
self.progress.update()
if self.progress.n >= len(self): self.progress.close()
return [a, b]
def __len__(self):
return(len(self.ix)+ self.batch_size - 1)// self.batch_size
import pandas as pd
import csv<define_variables>
def set_lr(model, lr):
K.set_value(model.optimizer.lr, float(lr))
def get_lr(model):
return K.get_value(model.optimizer.lr )
sub_files = [
".. /input/ensemble-0842/sub_simi_800.csv",
".. /input/ensemble-0842/sub_simi_805.csv",
".. /input/ensemble-0842/sub_ens_833.csv",
".. /input/ensemble-0842/sub_ens_824.csv",
".. /input/ensemble-0842/sub_tri_760.csv",
".. /input/ensemble-0842/sub_siam_822.csv",
]
sub_weight = [
0.800**2,
0.805**2,
0.833**2,
0.824**2,
0.76**2,
0.822**2,
]<save_to_csv>
def score_reshape(score, x, y=None):
if y is None:
m = np.zeros(( x.shape[0], x.shape[0]), dtype=K.floatx())
 m[np.triu_indices(x.shape[0], 1)] = score.squeeze()
m += m.transpose()
else:
m = np.zeros(( y.shape[0], x.shape[0]), dtype=K.floatx())
 iy, ix = np.indices(( y.shape[0], x.shape[0]))
ix = ix.reshape(( ix.size,))
iy = iy.reshape(( iy.size,))
m[iy, ix] = score.squeeze()
return m
Hlabel = 'Image'
Htarget = 'Id'
npt = 6
place_weights = {}
for i in range(npt):
place_weights[i] =(1 /(i + 1))
print(place_weights)
 lg = len(sub_files)
 sub = [None]*lg
for i, file in enumerate(sub_files):
print("Reading {}: w={} - {}".format(i, sub_weight[i], file))
reader = csv.DictReader(open(file,"r"))
sub[i] = sorted(reader, key=lambda d: str(d[Hlabel]))
out = open("submission_1.csv", "w", newline='')
 writer = csv.writer(out)
 writer.writerow([Hlabel,Htarget])
 for p, row in enumerate(sub[0]):
target_weight = {}
for s in range(lg):
row1 = sub[s][p]
for ind, trgt in enumerate(row1[Htarget].split(' ')) :
target_weight[trgt] = target_weight.get(trgt,0)+(place_weights[ind]*sub_weight[s])
 tops_trgt = sorted(target_weight, key=target_weight.get, reverse=True)[:npt]
writer.writerow([row1[Hlabel], " ".join(tops_trgt)])
 out.close()<load_from_csv>
def compute_score(verbose=1):
features = branch_model.predict_generator(FeatureGen(train, verbose=verbose), max_queue_size=12, workers=6,
verbose=0)
 score = head_model.predict_generator(ScoreGen(features, verbose=verbose), max_queue_size=12, workers=6, verbose=0)
 score = score_reshape(score, features)
 return features, score
train_on_gpu = True
print("PyTorch Version: ",torch.__version__)
 print("Torchvision Version: ",torchvision.__version__)
 df_train = pd.read_csv(".. /input/train.csv")
 data_dir = ".. /input/train"
model_name = "densenet"
num_classes = 5005
batch_size = 32
num_epochs = 15
feature_extract = False
num_workers = 0
def prepare_labels(y):
values = np.array(y)
 label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(values)
 onehot_encoder = OneHotEncoder(sparse=False)
 integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
 onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
 y = onehot_encoded
return y, label_encoder
y, le = prepare_labels(df_train['Id'])
 print(y)
 class WhaleDataset(Dataset):
def __init__(self, datafolder, datatype='train', df=None,
transform = transforms.Compose([transforms.ToTensor() ]), y=None):
self.datafolder = datafolder
self.datatype = datatype
self.y = y
if self.datatype == 'train':
self.df = df.values
self.image_files_list = [s for s in os.listdir(datafolder)]
self.transform = transform
def __len__(self):
return len(self.image_files_list)
 def __getitem__(self, idx):
if self.datatype == 'train':
img_name = os.path.join(self.datafolder, self.df[idx][0])
 label = self.y[idx]
elif self.datatype == 'test':
img_name = os.path.join(self.datafolder, self.image_files_list[idx])
 label = np.zeros(( 5005,))
image = Image.open(img_name ).convert('RGB')
 image = self.transform(image)
 if self.datatype == 'train':
return image, label
elif self.datatype == 'test':
return image, label, self.image_files_list[idx]
def set_parameter_requires_grad(model, feature_extracting):
if feature_extracting:
for param in model.parameters() :
param.requires_grad = False
def initialize_model(model_name, num_classes, feature_extract, use_pretrained=True):
model_ft = None
input_size = 0
if model_name == "resnet":

 model_ft = models.resnet34(pretrained=use_pretrained)
 set_parameter_requires_grad(model_ft, feature_extract)
 num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, num_classes)
 input_size = 224
elif model_name == "alexnet":

 model_ft = models.alexnet(pretrained=use_pretrained)
 set_parameter_requires_grad(model_ft, feature_extract)
 num_ftrs = model_ft.classifier[6].in_features
model_ft.classifier[6] = nn.Linear(num_ftrs,num_classes)
 input_size = 224
elif model_name == "vgg":

 model_ft = models.vgg11_bn(pretrained=use_pretrained)
 set_parameter_requires_grad(model_ft, feature_extract)
 num_ftrs = model_ft.classifier[6].in_features
model_ft.classifier[6] = nn.Linear(num_ftrs,num_classes)
 input_size = 224
elif model_name == "squeezenet":

 model_ft = models.squeezenet1_0(pretrained=use_pretrained)
 set_parameter_requires_grad(model_ft, feature_extract)
 model_ft.classifier[1] = nn.Conv2d(512, num_classes, kernel_size=(1,1), stride=(1,1))
model_ft.num_classes = num_classes
input_size = 224
elif model_name == "densenet":

 model_ft = models.densenet121(pretrained=use_pretrained)
 set_parameter_requires_grad(model_ft, feature_extract)
 num_ftrs = model_ft.classifier.in_features
model_ft.classifier = nn.Linear(num_ftrs, num_classes)
 input_size = 224
elif model_name == "inception":

 model_ft = models.inception_v3(pretrained=use_pretrained)
 set_parameter_requires_grad(model_ft, feature_extract)
 num_ftrs = model_ft.AuxLogits.fc.in_features
model_ft.AuxLogits.fc = nn.Linear(num_ftrs, num_classes)
 num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs,num_classes)
 input_size = 299
else:
print("Invalid model name, exiting...")
 exit()
return model_ft, input_size
model_ft, input_size = initialize_model(model_name, num_classes, feature_extract, use_pretrained=True)
 print(model_ft)
 data_transforms = transforms.Compose([
transforms.Resize(( 224,224)) ,
transforms.RandomHorizontalFlip(0.5),
transforms.RandomAffine(30),
transforms.ToTensor() ,
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
 ])
 test_transforms = transforms.Compose([
transforms.Resize(( 224,224)) ,
transforms.ToTensor() ,
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
 ])
 def accuracy(outputs,labels):
preds = outputs.max(dim=1)[1]
return(preds==labels ).float().sum()
def train(batch_size = 32, epochs = 10, transform = None):
tfmd_dataset = WhaleDataset(datafolder = ".. /input/train", datatype='train', df = df_train,
transform = transform, y=y)
 dataloader = torch.utils.data.DataLoader(tfmd_dataset, batch_size=batch_size, shuffle=True,
num_workers=num_workers)
 criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model_ft.parameters() , lr=0.0001)
 if torch.cuda.is_available() :
model_ft.cuda()
nn.DataParallel(model_ft)
 print("GPU")
 epoch_loss_data = []
epoch_accuracy_data = []
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 print(device)
 for epoch in range(1,epochs+1):
running_loss, running_loss_total,epoch_accuracy = 0.0, 0.0,0.0
for i, data in enumerate(dataloader, 0):
inputs, labels = data
labels = labels.double()
inputs, labels = inputs.to(device), labels.to(device)
 optimizer.zero_grad()
outputs = model_ft(inputs ).double()
loss = criterion(outputs, labels)
 loss.backward()
optimizer.step()
running_loss += loss.item()
running_loss_total += loss.item()
if i%30 == 29:
print('[%d, %5d] loss: %.3f, epoch_accuracy:' %(epoch, i + 1,
running_loss))
running_loss = 0.0
if i%100 == 99:
torch.save(model_ft.state_dict() , 'checkpoint')
 torch.save(optimizer.state_dict() , 'optimizer_checkpoint')
 with open('Running_loss.p','wb')as f:
pickle.dump(running_loss_total,f)
 epoch_loss_data.append(running_loss_total)
<train_model>
def make_steps(step, ampl):
global w2ts, t2i, steps, features, score, histories
random.shuffle(train)
 w2ts = {}
for w, hs in w2hs.items() :
for h in hs:
if h in train_set:
if w not in w2ts: w2ts[w] = []
if h not in w2ts[w]: w2ts[w].append(h)
 for w, ts in w2ts.items() : w2ts[w] = np.array(ts)
 t2i = {}
for i, t in enumerate(train): t2i[t] = i
features, score = compute_score()
history = model.fit_generator(
 TrainingData(score + ampl * np.random.random_sample(size=score.shape), steps=step, batch_size=32),
initial_epoch=steps, epochs=steps + step, max_queue_size=12, workers=6, verbose=1 ).history
steps += step
history['epochs'] = steps
history['ms'] = np.mean(score)
 history['lr'] = get_lr(model)
 print(history['epochs'], history['lr'], history['ms'])
 histories.append(history )
train(transform=data_transforms,epochs=28,batch_size=32 )<load_from_csv>
histories = []
steps = 0
tmp = keras.models.load_model('.. /input/piotte/mpiotte-standard.model')
 model.set_weights(tmp.get_weights())
 model.summary()
<train_model><EOS> 
def prepare_submission(threshold, filename):
vtop = 0
vhigh = 0
pos = [0, 0, 0, 0, 0, 0]
with open(filename, 'wt', newline='
')as f:
f.write('Image,Id
')
 for i, p in enumerate(tqdm(submit)) :
t = []
s = set()
a = score[i, :]
for j in list(reversed(np.argsort(a))):
h = known[j]
if a[j] < threshold and new_whale not in s:
pos[len(t)] += 1
s.add(new_whale)
 t.append(new_whale)
 if len(t)== 5: break;
for w in h2ws[h]:
assert w != new_whale
if w not in s:
if a[j] > 1.0:
vtop += 1
elif a[j] >= threshold:
vhigh += 1
s.add(w)
 t.append(w)
 if len(t)== 5: break;
if len(t)== 5: break;
if new_whale not in s: pos[5] += 1
assert len(t)== 5 and len(s)== 5
f.write(p + ',' + ' '.join(t[:5])+ '
')
 return vtop, vhigh, pos
tic = time.time()
h2ws = {}
for p, w in tagged.items() :
if w != new_whale:
h = p2h[p]
if h not in h2ws: h2ws[h] = []
if w not in h2ws[h]: h2ws[h].append(w)
 known = sorted(list(h2ws.keys()))
h2i = {}
for i, h in enumerate(known): h2i[h] = i
fknown = branch_model.predict_generator(FeatureGen(known), max_queue_size=20, workers=10, verbose=0)
 fsubmit = branch_model.predict_generator(FeatureGen(submit), max_queue_size=20, workers=10, verbose=0)
 score = head_model.predict_generator(ScoreGen(fknown, fsubmit), max_queue_size=20, workers=10, verbose=0)
 score = score_reshape(score, fknown, fsubmit)
 prepare_submission(0.99, 'submission.csv')
 toc = time.time()