Kaggle competition: Traveling Santa 2018 - Prime Paths
<SOS> metric: TravelingSanta2 Kaggle data source: traveling-santa-2018-prime-paths<train_model>
import time
import pandas as pd
import numpy as np
import sympy as sp
import matplotlib.pyplot as plt
import matplotlib.collections as mcoll
from itertools import chain
from itertools import combinations
from itertools import permutations
from itertools import product
from sklearn.neighbors import NearestNeighbors
for layer in model.layers:
layer.trainable = True
callbacks_list = [checkpoint, csv_logger, reduceLROnPlat, early, qwk]
model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=1e-4))
model.fit_generator(
 train_mixup,
steps_per_epoch=np.ceil(float(len(train_x)) / float(batch_size)) ,
validation_data=valid_generator,
validation_steps=np.ceil(float(len(valid_x)) / float(batch_size)) ,
epochs=epochs,
verbose=1,
workers=1, use_multiprocessing=False,
callbacks=callbacks_list )<load_from_csv>
def initial() :
df = pd.read_csv('.. /input/traveling-santa-2018-prime-paths/cities.csv')
 df['Z'] = 1 +.1 * ~df['CityId'].apply(sp.isprime)
 data = df[['X', 'Y', 'Z']].values
tour = np.loadtxt('.. /input/traveling-santa-lkh-solution/pure1502650.csv',
skiprows=1, dtype=int)
 return tour, data
tour, data = initial()
submit = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 model.load_weights('.. /working/densenet_bestqwk.h5')
 predicted = []<predict_on_test>
def distance(tour, data, pen=9):
xy, z = np.hsplit(data[tour], [2])
 dist = np.hypot(*(xy[:-1] - xy[1:] ).T)
 dist[pen::10] *= z[:-1][pen::10].flat
return dist
dist = distance(tour, data)
 dist
for i, name in tqdm(enumerate(submit['id_code'])) :
path = os.path.join('.. /input/aptos2019-blindness-detection/test_images/', name+'.png')
 image = cv2.imread(path)
 image = cv2.resize(image,(SIZE, SIZE))
X = np.array(( image[np.newaxis])/255)
 score_predict=(( model.predict(X ).ravel() *model.predict(X[:, ::-1, :, :] ).ravel() *model.predict(X[:, ::-1, ::-1, :] ).ravel() *model.predict(X[:, :, ::-1, :] ).ravel())**0.25 ).tolist()
label_predict = np.argmax(score_predict)
 predicted.append(str(label_predict))<save_to_csv>
distance(tour[3:9], data, pen = 9 - 3 % 10)== dist[3:8]
submit['diagnosis'] = predicted
submit.to_csv('submission.csv', index=False)
 submit.head()<set_options>
f'Initial Score: {np.sum(dist):.2f}'
warnings.filterwarnings('always')
 warnings.filterwarnings('ignore')
 %matplotlib inline
style.use('fivethirtyeight')
 sns.set(style='whitegrid', color_codes=True)
 !ls.. /input/*<import_modules>
def candidates(data, opt, ext):
nns = NearestNeighbors(n_neighbors=opt + ext ).fit(data[:, :2])
 kne = nns.kneighbors(data[:, :2], return_distance=False)
 np.random.shuffle(kne)
 cand = set()
for i in kne:
for j in combinations(i[1:], opt - 1):
cand.add(tuple(sorted(( i[0],)+ j)))
 return cand
fastai.__version__<set_options>
list(candidates(data, opt=3, ext=0)) [:5]
def is_interactive() :
return 'runtime' in get_ipython().config.IPKernelApp.connection_file
print('Interactive?', is_interactive() )<set_options>
len(candidates(data, opt=2, ext=0))
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(42 )<define_variables>
len(candidates(data, opt=2, ext=1))
Path('/tmp/.cache/torch/checkpoints/' ).mkdir(exist_ok=True, parents=True)
 model_path = '/tmp/.cache/torch/checkpoints/efficientNet.pth'
!cp.. /input/efficientnet*/efficientNet_*.pth {model_path}<load_from_csv>
def alternatives(tour, cuts, fil):
edges = [tuple(x)for x in np.split(tour, cuts)[1:-1]]
a, b = tour[cuts[0] - 1], tour[cuts[-1]]
alter = set()
for i in set(product(*zip(edges, [x[::-1] for x in edges]))):
for j in permutations(i):
if not fil or all(x != y for x, y in zip(edges, j)) :
alter.add(tuple(chain(( a,), *j,(b,))))
alter.discard(tuple(chain(( a,), *edges,(b,))))
return alter
PATH = Path('.. /input/aptos2019-blindness-detection')
 df_train = pd.read_csv(PATH/'train.csv')
 df_test = pd.read_csv(PATH/'test.csv')
 _ = df_train.hist()<define_variables>
alternatives(tour, cuts = [2,5,7], fil=False )
aptos19_stats =([0.42, 0.22, 0.075], [0.27, 0.15, 0.081])
 data = ImageDataBunch.from_df(df=df_train,
path=PATH, folder='train_images', suffix='.png',
valid_pct=0.1,
ds_tfms=get_transforms(flip_vert=True, max_warp=0.1, max_zoom=1.15, max_rotate=45.) ,
size=224,
bs=32,
num_workers=os.cpu_count()
).normalize(aptos19_stats )<import_modules>
alternatives(tour, cuts = [2,5,7], fil=True )
print(f'Classes: {data.classes}' )<define_variables>
def submit(tour):
np.savetxt('submission.csv', tour, fmt='%d', header='Path', comments='' )
data.show_batch(rows=3, figsize=(7,6))<define_variables>
def kopt(opt, ext, fil):
tour, data = initial()
sequ = 1 + np.argsort(tour[1:])
 dist = distance(tour, data)
 print(f'opt:{opt} & ext:{ext} & fil:{fil}...')
 cand = candidates(data, opt, ext)
 print(f' Initial Score:\t{np.sum(dist):0.2f}')
 for c in cand:
cuts = sorted(sequ[j] for j in c)
 alter = alternatives(tour, cuts, fil)
 if not alter:
continue
atour, pen = np.array(list(alter)) , 9 -(cuts[0] - 1)% 10
adist = np.array([distance(x, data, pen)for x in atour])
 if np.any(np.sum(adist, 1)< np.sum(dist[cuts[0] - 1:cuts[-1]])) :
arg = np.argmin(np.sum(adist, 1))
dist[cuts[0] - 1:cuts[-1]] = adist[arg]
tour[cuts[0]:cuts[-1]] = atour[arg][1:-1]
sequ[atour[arg][1:-1]] = range(cuts[0], cuts[-1])
 print(f' Final Score:\t{np.sum(dist):0.2f}')
 submit(tour )
package_path = '.. /input/efficientnet-pytorch/efficientnet-pytorch/EfficientNet-PyTorch-master'
sys.path.append(package_path)
<load_pretrained>
t0 = time.time()
kopt(opt=2, ext=0, fil=False)
 print(f'Time:\t{time.time() -t0:.2f}s' )
def EfficientNetB4(pretrained=True):

 model = EfficientNet.from_name('efficientnet-b4', override_params={'num_classes':5})
 if pretrained:
model_state = torch.load(model_path)
 if '_fc.weight' in model_state.keys() :
model_state.pop('_fc.weight')
 model_state.pop('_fc.bias')
 res = model.load_state_dict(model_state, strict=False)
 assert str(res.missing_keys)== str(['_fc.weight', '_fc.bias']), 'issue loading pretrained weights'
else:
mapping = { i:o for i,o in zip(model_state.keys() , model.state_dict().keys())}
mapped_model_state = OrderedDict([ 
(mapping[k], v)for k,v in model_state.items() if not mapping[k].startswith('_fc')
 ])
 res = model.load_state_dict(mapped_model_state, strict=False)
 print(res)
 return model<choose_model_class>
t0 = time.time()
kopt(opt=2, ext=1, fil=False)
 print(f'Time:\t{time.time() -t0:.2f}s' )
model = EfficientNetB4(pretrained=True)
 model<compute_train_metric>
t0 = time.time()
kopt(opt=3, ext=0, fil=False)
 print(f'Time:\t{time.time() -t0:.2f}s' )
<choose_model_class><EOS> 
t0 = time.time()
kopt(opt=4, ext=0, fil=True)
 print(f'Time:\t{time.time() -t0:.2f}s' )