Kaggle competition: Kannada MNIST
bs = 16
sz = 256
md = get_data(sz,bs,is_test=True)
 x,y = next(iter(md.aug_dl))
x.shape, y.shape<categorify>
train = pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 test= pd.read_csv(".. /input/Kannada-MNIST/test.csv")
 print("Train size:{}
Test size:{}".format(train.shape, test.shape))
x_train = train.drop(['label'], axis=1 ).values.astype('float32')
 y_train = train['label'].values.astype('int32')
 x_test = test.drop(['id'], axis=1 ).values.astype('float32')
 x_train = x_train.reshape(x_train.shape[0], 28, 28)/ 255.0
x_test = x_test.reshape(x_test.shape[0], 28, 28)/ 255.0
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.15, random_state=42)
 print(x_train.shape)
 print(x_val.shape)
 print(y_train.shape)
 print(x_test.shape )
class Resnet34_4(nn.Module):
def __init__(self, pre=True):
super().__init__()
encoder = resnet34(pretrained=pre)
 self.conv1 = nn.Conv2d(4, 64, kernel_size=7, stride=2, padding=3, bias=False)
 if(pre):
w = encoder.conv1.weight
self.conv1.weight = nn.Parameter(torch.cat(( w,
0.5*(w[:,:1,:,:]+w[:,2:,:,:])) ,dim=1))
self.bn1 = encoder.bn1
self.relu = nn.ReLU(inplace=True)
 self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
 self.layer0 = nn.Sequential(self.conv1,self.relu,self.bn1,self.maxpool)
 self.layer1 = encoder.layer1
self.layer2 = encoder.layer2
self.layer3 = encoder.layer3
self.layer4 = encoder.layer4
def forward(self, x):
x = self.layer0(x)
 x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 return x<train_model>
x_train = x_train.reshape(x_train.shape[0], 28, 28,1)
 x_val = x_val.reshape(x_val.shape[0], 28, 28,1)
 x_test = x_test.reshape(x_test.shape[0], 28, 28,1)
 print("Train size:{}
validation size:{}
Test size:{}".format(x_train.shape,x_val.shape, x_test.shape))
mean_px = x_train.mean().astype(np.float32)
 std_px = x_train.std().astype(np.float32)
class FocalLoss(nn.Module):
def __init__(self, gamma=2):
super().__init__()
self.gamma = gamma
def forward(self, input, target):
if not(target.size() == input.size()):
raise ValueError("Target size({})must be the same as input size({})" 
.format(target.size() , input.size()))
max_val =(-input ).clamp(min=0)
 loss = input - input * target + max_val + \ 
(( -max_val ).exp() +(-input - max_val ).exp() ).log()
invprobs = F.logsigmoid(-input *(target * 2.0 - 1.0))
loss =(invprobs * self.gamma ).exp() * loss
return loss.sum(dim=1 ).mean()<compute_test_metric>
input = Input(shape=[28, 28, 1])
 x = Conv2D(32,(5, 5), strides=1, padding='same', name='conv1' )(input)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch1' )(x)
 x = Activation('relu', name='relu1' )(x)
 x = Conv2D(32,(5, 5), strides=1, padding='same', name='conv2' )(x)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch2' )(x)
 x = Activation('relu', name='relu2' )(x)
 x = Conv2D(32,(5, 5), strides=1, padding='same', name='conv2add' )(x)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch2add' )(x)
 x = Activation('relu', name='relu2add' )(x)
 x = Dropout(0.15 )(x)
 x = MaxPool2D(pool_size=2, strides=2, padding='same' )(x)
 x = Conv2D(64,(3, 3), strides=1, padding='same', name='conv3' )(x)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch3' )(x)
 x = Activation('relu', name='relu3' )(x)
 x = Conv2D(64,(3, 3), strides=1, padding='same', name='conv4' )(x)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch4' )(x)
 x = Activation('relu', name='relu4' )(x)
 x = Conv2D(32,(3, 3), strides=1, padding='same', name='conv5' )(x)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch5' )(x)
 x = Activation('relu', name='relu5' )(x)
 x = Dropout(0.15 )(x)
 x = MaxPool2D(pool_size=2, strides=2 )(x)
 x = Flatten()(x)
 x = Dense(100, name='Dense30' )(x)
 x = Activation('relu', name='relu6' )(x)
 x = Dropout(0.05 )(x)
 x = Dense(10, name='Dense10' )(x)
 x = Activation('softmax' )(x)
 model = Model(inputs = input, outputs =x)
 print(model.summary() )
def acc(preds,targs,th=0.0):
preds =(preds > th ).int()
targs = targs.int()
return(preds==targs ).float().mean()<feature_engineering>
checkpoint = ModelCheckpoint("best_weights.hdf5", monitor='val_acc', verbose=1, save_best_only=True, mode='max')
 datagen = ImageDataGenerator(
 rotation_range= 8,
zoom_range = 0.13,
width_shift_range=0.13,
height_shift_range=0.13)
 epochs = 70
lr_initial = 0.0011
optimizer = Adam(lr=lr_initial, decay= lr_initial /(epochs*1.3))
model.compile(optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
 datagen.fit(x_train)
 batch_size = 64
history = model.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size),
epochs = epochs, validation_data =(x_val,y_val),
verbose = 2, steps_per_epoch=x_train.shape[0] // batch_size, callbacks=[checkpoint])
 model.load_weights("best_weights.hdf5")
class F1:
__name__ = 'F1 macro'
def __init__(self,n=28):
self.n = n
self.TP = np.zeros(self.n)
 self.FP = np.zeros(self.n)
 self.FN = np.zeros(self.n)
 def __call__(self,preds,targs,th=0.0):
preds =(preds > th ).int()
targs = targs.int()
self.TP +=(preds*targs ).float().sum(dim=0)
 self.FP +=(preds > targs ).float().sum(dim=0)
 self.FN +=(preds < targs ).float().sum(dim=0)
 score =(2.0*self.TP/(2.0*self.TP + self.FP + self.FN + 1e-6)).mean()
return score
def reset(self):
score =(2.0*self.TP/(2.0*self.TP + self.FP + self.FN + 1e-6))
print('F1 macro:',score.mean() ,flush=True)
 self.TP = np.zeros(self.n)
 self.FP = np.zeros(self.n)
 self.FN = np.zeros(self.n)
 class F1_callback(Callback):
def __init__(self, n=28):
self.f1 = F1(n)
 def on_epoch_end(self, metrics):
self.f1.reset()<choose_model_class>
test1 = pd.read_csv('.. /input/Kannada-MNIST/test.csv' ).values
print(test1)
 x_samples = test1[0:,1:]
id_samples = test1[0:,0]
<train_model><EOS> 
results = model.predict(x_test)
 results = np.argmax(results,axis = 1)
 dg = pd.DataFrame()
dg['id'] = list(id_samples)
 dg['label'] = results
dg.to_csv('submission.csv',index = False )