Kaggle competition: Abstraction and Reasoning Challenge
<SOS> metric: MeanBestErrorAtK Kaggle data source: abstraction-and-reasoning-challenge<import_modules>
%matplotlib inline
fig = plt.figure(figsize=(8., 6.) )
plt.rcParams['figure.figsize'] =(12, 8)
 plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
K.set_image_data_format('channels_last')
 random_state = 42
image_shape =(28, 28, 1 )<load_from_csv>
def mask_match(task):
result = True
for i in range(len(task)) :
if not np.array_equal(np.clip(np.array(task[i]['input']), 0, 1), np.clip(np.array(task[i]['output']), 0, 1)) :
result = False
break
return result
def shape_match(task):
result = True
for i in range(len(task)) :
if not np.array_equal(np.array(task[i]['input'] ).shape, np.array(task[i]['output'] ).shape):
result = False
break
return result
def rotation_match(task):
r_ = {r:0 for r in range(1,4)}
result = 0
for i in range(len(task)) :
for r in range(3):
rot = np.rot90(np.array(task[i]['input']), r+1)
 rot = np.flip(rot, axis=1)
 if np.array_equal(np.array(task[i]['output']), rot):
r_[r + 1] += 1
for r in r_:
if r_[r]==len(task):
result = r
return result
def color_match(task):
result = True
for i in range(len(task)) :
if not np.array_equal(np.unique(np.array(task[i]['input'])) , np.unique(np.array(task[i]['output']))):
result = False
break
return result
source_df = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test_df = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 dig_df = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv' )<prepare_x_and_y>
path = '/kaggle/input/abstraction-and-reasoning-challenge/'
tasks = pd.DataFrame(glob.glob(path + '**/**'), columns=['path'])
 tasks['tte'] = tasks['path'].map(lambda x: x.split('/')[-2])
 tasks['output_id'] = tasks['path'].map(lambda x: x.split('/')[-1].split('.')[0])
 tasks['file'] = tasks['path'].map(lambda x: eval(open(x ).read()))
tasks['train'] = tasks['file'].map(lambda x: x['train'])
 tasks['test'] = tasks['file'].map(lambda x: x['test'])
 tasks.drop(columns=['file'], inplace=True)
 tasks['l'] = tasks.apply(lambda r:(len(r['train']), len(r['test'])) , axis=1)
 tasks.tte.value_counts()
def get_X_Y(df):
X = df.loc[:, 'pixel0':'pixel783'].copy()
Y = df.loc[:, 'label'].copy()
return(X, Y)
 source_X, source_Y = get_X_Y(source_df)
 dig_X, dig_Y = get_X_Y(dig_df)
 print('source_X shape {}, source_Y shape {}'.format(source_X.shape, source_Y.shape))<split>
tasks['mask_match'] = tasks['train'].map(lambda x: mask_match(x))
tasks['shape_match'] = tasks['train'].map(lambda x: shape_match(x))
tasks['rotation_match'] = tasks['train'].map(lambda x: rotation_match(x))
tasks['color_match'] = tasks['train'].map(lambda x: color_match(x))
tasks.head()
train_X, valid_X, train_Y, valid_Y = train_test_split(
 source_X, source_Y, test_size=0.2, stratify=source_Y, random_state=random_state)
 print('train_X shape {}, train_Y shape {}, valid_X shape {}, valid_Y shape {}'.format(
 train_X.shape, train_Y.shape, valid_X.shape, valid_Y.shape))<choose_model_class>
def flattener(pred):
str_pred = '|'+ '|'.join([''.join([str(v)for v in row])for row in pred])+'|'
str_pred = ' '.join([str_pred for i in range(2)])
 str_pred += ' |'+ '|'.join([''.join([str(0)for v in row])for row in pred])+'|'
return str_pred
def build_cnn(input_shape, classes):
input_layer = Input(shape=input_shape)
 conv_layer1 = Conv2D(32, kernel_size=(3,3), strides=1, padding='same', input_shape=(28, 28, 1))(input_layer)
 conv_layer1 = Conv2D(16, kernel_size=(3,3), strides=1, padding='same' )(conv_layer1)
 batch_norm_layer1 = BatchNormalization(momentum=0.5, gamma_initializer='uniform' )(conv_layer1)
 relu_layer1 = LeakyReLU()(batch_norm_layer1)
 max_pool_layer1 = MaxPooling2D(pool_size=(2,2), strides=(2,2))(relu_layer1)
 con_drop_layer1 = Dropout(0.5 )(max_pool_layer1)
 conv_layer2 = Conv2D(64, kernel_size=(3,3), strides=1, padding='same' )(con_drop_layer1)
 conv_layer2 = Conv2D(32, kernel_size=(3,3), strides=1, padding='same' )(conv_layer2)
 batch_norm_layer2 = BatchNormalization(momentum=0.5, gamma_initializer='uniform' )(conv_layer2)
 relu_layer2 = LeakyReLU()(batch_norm_layer2)
 max_pool_layer2 = MaxPooling2D(pool_size=(2,2), strides=(2,2))(relu_layer2)
 con_drop_layer2 = Dropout(0.5 )(max_pool_layer2)
 conv_layer3 = Conv2D(128, kernel_size=(3,3), strides=1, padding='same' )(con_drop_layer2)
 conv_layer3 = Conv2D(64, kernel_size=(3,3), strides=1, padding='same' )(conv_layer3)
 batch_norm_layer3 = BatchNormalization(momentum=0.5, gamma_initializer='uniform' )(conv_layer3)
 relu_layer3 = LeakyReLU()(batch_norm_layer3)
 max_pool_layer3 = MaxPooling2D(pool_size=(2,2), strides=(2,2))(relu_layer3)
 flatten = Flatten()(max_pool_layer3)
 dropout_layer1 = Dropout(0.5 )(flatten)
 dense_layer1 = Dense(256 )(dropout_layer1)
 relu_layer4 = LeakyReLU()(dense_layer1)
 dropout_layer2 = Dropout(0.5 )(relu_layer4)
 dense_layer2 = Dense(256 )(dropout_layer2)
 relu_layer5 = LeakyReLU()(dense_layer2)
 dropout_layer3 = Dropout(0.5 )(relu_layer5)
 output_layer = Dense(10, activation='softmax' )(dropout_layer3)
 model = Model(inputs=[input_layer], outputs=[output_layer])
 model.compile('adam', loss='categorical_crossentropy', metrics=['accuracy'])
 return model
cnn_model = build_cnn(image_shape, 10)
 cnn_model.summary()<split>
evaluation = tasks[tasks['tte']=='evaluation'].reset_index(drop=True)
 score = 0.
 denom = 0.
 for i in range(len(evaluation)) :
for j in range(len(evaluation['test'][i])) :
denom += 1
if evaluation['rotation_match'][i] > 0:
rot = np.array(evaluation['test'][i][j]['input'])
 rot = np.flip(np.rot90(rot, evaluation['rotation_match'][i]), axis=1)
 if not np.array_equal(np.array(evaluation['test'][i][j]['output']), rot):
score += 1
elif evaluation['shape_match'][i] == True:
if not np.array_equal(np.array(evaluation['test'][i][j]['output']), np.array(evaluation['test'][i][j]['input'])) : score += 1
else:
if not np.array_equal(np.array(evaluation['test'][i][j]['output']), np.array(evaluation['train'][i][0]['output'])) : score += 1
print(score/denom )
<set_options><EOS> 
test = tasks[tasks['tte']=='test'].reset_index(drop=True)
 sub = open('submission.csv','w')
 sub.write('output_id,output
')
 for i in range(len(test)) :
for j in range(len(test['test'][i])) :
if test['rotation_match'][i] > 0:
rot = np.array(test['test'][i][j]['input'])
 pred = np.flip(np.rot90(rot, test['rotation_match'][i]), axis=1)
 elif test['shape_match'][i] == True:
pred = np.array(test['test'][i][j]['input'])
 else:
pred = np.array(test['train'][i][0]['output'])
 sub.write(test['output_id'][i]+ '_' + str(j)+ ',' + flattener(pred)+'
')
 sub.close()