Kaggle competition: Abstraction and Reasoning Challenge
<SOS> metric: MeanBestErrorAtK Kaggle data source: abstraction-and-reasoning-challenge<train_model>
import numpy as np
import pandas as pd
import sys
import os
import re
import json
import cv2
import itertools
import traceback
from itertools import chain, product, combinations
from glob import glob
from pathlib import Path
from copy import copy, deepcopy
import matplotlib.pyplot as plt
from matplotlib import colors
from typing import List, Union, Callable
import toolz
import pydash as py
from pydash import py_ as _
lr = 1e-4
learn.fit_one_cycle(4,lr )<load_pretrained>
task_files = glob('.. /input/abstraction-and-reasoning-challenge/**/*.json')
 def load_tasks(task_files):
if isinstance(task_files, str): task_files = glob(task_files)
 tasks = { re.sub(r'^.*/ (.*?/.*$)','\\1',file): json.load(open(file, 'r')) for file in task_files }
for file, task in tasks.items() :
for test_train, specs in task.items() :
for index, spec in enumerate(specs):
for input_output, grid in spec.items() :
tasks[file][test_train][index][input_output] = np.array(grid ).astype('uint8')
 for file, task in tasks.items() :
tasks[file]['file'] = file
return tasks
tasks = load_tasks(task_files)
 task = list(tasks.values())[6]; task
!cp /kaggle/input/pretrained-pytorch/resnet101-5d3b4d8f.pth /tmp/.cache/torch/checkpoints/resnet101-5d3b4d8f.pth
learn101 = cnn_learner(data, models.resnet101, metrics=accuracy, model_dir = Path('.. /kaggle/working'),path = Path("."))<train_model>
solvers = {}
lr = 1e-2
learn101.fit_one_cycle(4 )<find_best_params>
class DoNothingSolver(Solver):
def action(self, grid, task):
return grid
solvers['DoNothingSolver'] = DoNothingSolver()
solvers['DoNothingSolver'].plot(tasks )
learn101.unfreeze()<train_model>
class ZoomSolver(Solver):
verbose = True
def detect(self, task):
ratios = self.task_shape_ratios(task)
 ratio = list(ratios)[0]
detect =(
 ratios != {(1,1)}
and len(ratios)== 1
and ratio[0] == ratio[1] 
)
 return detect
def get_scale(self, task):
return list(self.task_shape_ratios(task)) [0][0]
def action(self, grid, task):
scale = self.get_scale(task)
 if scale > 1:
resize = tuple(int(d*scale)for d in grid.shape)
 output = cv2.resize(grid, resize, interpolation=cv2.INTER_NEAREST)
 else:
resize = tuple(int(1/scale)for d in grid.shape)
 output = skimage.measure.block_reduce(grid, resize)
 if self.debug:
print('scale', scale, 'grid.shape', grid.shape, 'output.shape', output.shape)
 print('grid', grid)
 print('output', output)
 return output
solvers['ZoomSolver'] = ZoomSolver()
ZoomSolver().plot(tasks )
lr = 1e-4
learn101.fit_one_cycle(4,lr )<load_pretrained>
class GeometrySolver(Solver):
optimise = True
verbose = True
actions = {
"flip":(np.flip, [0,1]),
"rot90":(np.rot90, [1,2,3]),
"roll":(np.roll, product(range(-5,5),[0,1])) ,
"swapaxes":(np.swapaxes, [(0, 1),(1, 0)]),
"transpose":(np.transpose, []),
}
def __init__(self):
super().__init__()
for key,(function, arglist)in self.actions.items() :
self.actions[key] =(function, [(args,)if not isinstance(args, tuple)else args for args in arglist ])
 def detect(self, task):
return self.is_task_shape_ratio_unchanged(task)
 def test(self, task):
if task['file'] in self.cache: return True
max_roll =(self.task_grid_max_dim(task)+ 1)// 2
for key,(function, arglist)in self.actions.items() :
if function == np.roll: arglist = product(range(-max_roll,max_roll),[0,1])
 for args in arglist:
if self.is_lambda_valid(task, function, *args):
self.cache[task['file']] =(function, args)
 if self.verbose: print(function, args)
 return True
if self.optimise: return False
for(( function1, arglist1),(function2, arglist2)) in combinations(self.actions.values() , 2):
if function1 == np.roll: arglist1 = product(range(-max_roll,max_roll),[0,1])
 if function2 == np.roll: arglist2 = product(range(-max_roll,max_roll),[0,1])
 for args1, args2 in product(arglist1, arglist2):
function = lambda grid, args1, args2: function1(function2(grid, *args2), *args1)
 if self.is_lambda_valid(task, function, *(args1,args2)) :
self.cache[task['file']] =(function,(args1,args2))
if self.verbose: print(function,(args1,args2))
return True
return False
def action(self, grid, function=None, args=None, task=None):
try:
return function(grid, *args)
 except Exception as exception:
if self.debug: print(function, args, exception)
 return grid
solvers['GeometrySolver'] = GeometrySolver()
GeometrySolver().plot(tasks )
!cp /kaggle/input/pretrained-pytorch/resnet152-b121ed2d.pth /tmp/.cache/torch/checkpoints/resnet152-b121ed2d.pth
learn152 = cnn_learner(data, models.resnet152, metrics=accuracy, model_dir = Path('.. /kaggle/working'),path = Path("."))<train_model>
def score_tasks(tasks: dict, label: str):
total = 0
solved = 0
for task_file, task in tasks.items() :
for index, spec in enumerate(task['test']): total += 1
if 'solution' in task:
for index, spec in enumerate(task['solution']):
solved += 1
print(f'{label.ljust(11)}| solutions found: {str(solved ).rjust(3)}/{total} | error: {round(1-solved/total,4)}')
 return solved
for label in ['training', 'evaluation', 'test']:
score_tasks({ file: task for(file,task)in tasks.items() if label in file }, label )
lr = 1e-2
learn152.fit_one_cycle(4 )<train_model>
test_files = glob('.. /input/abstraction-and-reasoning-challenge/test/*.json');
tests = load_tasks(test_files)
 len(tests )
lr = 1e-4
learn152.fit_one_cycle(4,lr )<predict_on_test>
for name, solver in solvers.items() :
solver.cache = {}
solver.plot(tests )
preds50, _ = learn.get_preds(DatasetType.Test)
 preds101, _ = learn101.get_preds(DatasetType.Test)
 preds152, _ = learn152.get_preds(DatasetType.Test)
 preds = 0.34*preds50 + 0.33*preds101 + 0.33*preds152
y = torch.argmax(preds, dim=1 )<define_variables>
".join([ ",".join(row)for row in output ])
<save_to_csv><EOS> 
def output_id(filename, index=0):
return re.sub('^.*/|\.json$', '', filename)+ '_' + str(index);
def flattener(grid):
grid = grid.astype('uint8' ).tolist()
str_pred = str([row for row in grid])
 str_pred = str_pred.replace(', ', '')
 str_pred = str_pred.replace('[[', '|')
 str_pred = str_pred.replace('][', '|')
 str_pred = str_pred.replace(']]', '|')
 return str_pred
def write_submission_csv(tests):
submission_csv = pd.read_csv('.. /input/abstraction-and-reasoning-challenge/sample_submission.csv')
 submission_csv = submission_csv.set_index('output_id')
 total = 0
solved = 0
for task_file, test in tests.items() :
for index, spec in enumerate(test['test']): total += 1
if 'solution' in test:
for index, spec in enumerate(test['solution']):
submission_csv.loc[ output_id(task_file, index), 'output' ] = flattener(spec['output'])
 solved += 1
submission_csv.to_csv('submission.csv')
 print(f'wrote: submission.csv')
 print(f'solutions found: {solved}/{total} | error: {round(1-solved/total,4)}')
 return submission_csv
write_submission_csv(tests )