Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<load_pretrained>
activations, regularizers, Sequential
, utils, callbacks, optimizers 
)
 Flatten, Dense, BatchNormalization
, Activation, Dropout, Conv2D, MaxPool2D
)
PATH = ".. /input/hubmap-models2/"
model_names = [
"1_unet-timm-effb7_0.9509_epoch_28.pth",
"2_unet-timm-effb7_0.9488_epoch_28.pth",
"3_unet-timm-effb7_0.9503_epoch_29.pth",
"4_unet-timm-effb7_0.9500_epoch_28.pth",
"5_unet-timm-effb7_0.9518_epoch_27.pth",
]
models = []
for model_name in model_names:
models.append(torch.load(PATH + model_name, map_location= 'cpu'))<define_variables>
BASEPATH = '.. /input/digit-recognizer/'
def reload(df='train.csv', msg=True, path=BASEPATH):
o = pd.read_csv(BASEPATH + df)
 print(f'{df} loaded!')
 return o
train = reload()
y_train = train.label.copy()
y_train = utils.to_categorical(y_train, 10)
 X_train = train.drop(columns='label')
 def preprocess(df):
df = df / 256.0
df = df.values.reshape(-1, 28, 28, 1)
 return df
X_train = preprocess(X_train)
 test = reload('test.csv')
 X_test = preprocess(test )
sz = 512
test_path = '.. /input/hubmap-kidney-segmentation/test/'
for step, person_idx in enumerate(test_files):
print(f'load {step+1}/{len(test_files)} data...')
 img = tiff.imread(test_path + person_idx + '.tiff' ).squeeze()
if img.shape[0] == 3:
img = img.transpose(1,2,0)
 predict_mask_l1 = np.zeros(( img.shape[0], img.shape[1]), dtype = bool)
 landscape =img.shape[0]// 512
portrait = img.shape[1]// 512
sz = 512
print('predict mask...')
 for x in tqdm(range(landscape)) :
for y in range(portrait):
start_x =(512)* x
end_x =(1024)+ start_x
start_y =(512)* y
end_y =(1024)+ start_y
if x == landscape-1:
start_x = img.shape[0] - 1024
end_x = img.shape[0]
if y == portrait-1:
start_y = img.shape[1] - 1024
end_y = img.shape[1]
sample_img = img[start_x : end_x, start_y : end_y,:]
sample_img = cv2.resize(sample_img,(sz,sz),interpolation = cv2.INTER_AREA)/256
sample_img = torch.cuda.FloatTensor(sample_img.transpose([2,0,1])[np.newaxis,...])
 sample_pred = np.zeros([512,512])
 for model in models:
sample_pred += model.predict(sample_img ).cpu().numpy() [0,0,:,:]
sample_pred = sample_pred/len(models)
 sample_pred = cv2.resize(sample_pred,(1024,1024),interpolation = cv2.INTER_NEAREST)
 sample_pred = np.where(sample_pred > 0.3, True, False ).astype(bool)
 predict_mask_l1[start_x + 256 : end_x - 256, start_y + 256 : end_y - 256] = sample_pred[256:256 + 512,256:256 + 512]
del sample_img
del sample_pred
gc.collect()
predict_mask_l1 = predict_mask_l1.astype(np.uint8)
 contours, hierarchy = cv2.findContours(predict_mask_l1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
 predict_mask_l2 = np.zeros(( img.shape[0], img.shape[1]), dtype = bool)
 for cont in tqdm(contours):
center_y, center_x = cont.mean(axis = 0 ).round(0 ).astype(int)[0]
left_x = int(center_x - 512)
 top_y = int(center_y - 512)
 if left_x < 0:
left_x = 0
elif left_x + 1024 > img.shape[0]:
left_x = img.shape[0] - 1024
if top_y < 0:
top_y = 0
elif top_y + 1024 > img.shape[1]:
top_y = img.shape[1] - 1024
sample_img_l2 = img[left_x : left_x + 1024, top_y : top_y+ 1024,:]
sample_img_l2 = cv2.resize(sample_img_l2,(sz,sz),interpolation = cv2.INTER_AREA)/256
sample_img_l2 = torch.cuda.FloatTensor(sample_img_l2.transpose([2,0,1])[np.newaxis,...])
 sample_pred_l2 = np.zeros([512,512])
 for model in models:
sample_pred_l2 += model.predict(sample_img_l2 ).cpu().numpy() [0,0,:,:]
sample_pred_l2 = sample_pred_l2/len(models)
 sample_pred_l2 = cv2.resize(sample_pred_l2,(1024,1024),interpolation = cv2.INTER_NEAREST)
 sample_pred_l2 = np.where(sample_pred_l2 > 0.5, True, False ).astype(np.uint8)
 contours_l2, hierarchy = cv2.findContours(sample_pred_l2,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
 if len(contours_l2)< 1:
print('no contour')
 continue
for cont_l2 in contours_l2:
min_y, min_x = cont_l2.min(axis = 0 ).round(0 ).astype(int)[0]
max_y, max_x = cont_l2.max(axis = 0 ).round(0 ).astype(int)[0]
if(min_x < 512)and(max_x > 512):
if(min_y < 512)and(max_y > 512):
sample_mask_l2 = np.zeros(sample_pred_l2.shape,
dtype = np.uint8)
 sample_center = cv2.drawContours(sample_mask_l2,
[cont_l2],
0, 
(255, 255, 255),
-1)
 predict_mask_l2[left_x : left_x + 1024,
top_y : top_y+ 1024] =\
np.logical_or(predict_mask_l2[left_x : left_x + 1024,
top_y : top_y+ 1024],
sample_center)
 del predict_mask_l1
del img
gc.collect()
print('convert mask to rle
')
 predict_rle = rle_encode_less_memory(predict_mask_l2)
 sample_submission.loc[person_idx,'predicted'] = predict_rle
del predict_rle
del predict_mask_l2
gc.collect()
sample_submission = sample_submission.reset_index()
sample_submission.to_csv('/kaggle/working/submission.csv',index=False )<install_modules>
l1 = 0
l2 = 0.01
ini_lr = 0.001
val_size = 0.3
batch_size = 30
activation = 'relu'
if activation == 'selu':
initializer = 'lecun_normal'
elif activation in ['relu', 'elu']:
initializer = 'he_normal'
else:
initializer = 'glorot_normal'
sched_lr_val_acc = True
decay_rate = 0.97
X_tr, X_val, y_tr, y_val = train_test_split(
 X_train, y_train, test_size=val_size, random_state=713)
 n_train_val_samples = X_tr.shape[0]
n_train_samples = X_tr.shape[0] + X_val.shape[0]
def generate_tfdata(X, y=None, batch_size=batch_size
, shuffle_repeat=True):

 if y is None:
data = Dataset.from_tensor_slices(( X,))
else:
data = Dataset.from_tensor_slices(( X, y))
if shuffle_repeat:
data = data.shuffle(10000, 713, reshuffle_each_iteration=True ).repeat()
if batch_size:
data = data.batch(batch_size)
 return data.prefetch(1)
 tr_set = generate_tfdata(X_tr, y_tr)
 val_set = generate_tfdata(X_val, y_val, shuffle_repeat=False)
 train_set = generate_tfdata(X_train, y_train)
 test_set = generate_tfdata(X_test, shuffle_repeat=False )
!pip install.. /input/keras-applications/Keras_Applications-1.0.8/ -f./ --no-index
!pip install.. /input/image-classifiers/image_classifiers-1.0.0/ -f./ --no-index
!pip install.. /input/efficientnet-1-0-0/efficientnet-1.0.0/ -f./ --no-index
!pip install.. /input/segmentation-models/segmentation_models-1.0.1/ -f./ --no-index<define_variables>
def get_regularizer(l1=l1, l2=l2):
return regularizers.l1_l2(l1=l1, l2=l2)
 def get_optimizer(lr=ini_lr, beta_1=0.9, beta_2=0.999
, decay_rate=decay_rate
, n_samples=n_train_samples, batch_size=batch_size):
if sched_lr_val_acc == False:
lr = optimizers.schedules.ExponentialDecay(
 lr
, decay_steps = n_samples // batch_size // 2
, decay_rate = decay_rate 
)
 return optimizers.Adam(lr, beta_1=beta_1, beta_2=beta_2)
 def get_callbacks(es_params=['val_loss', 1e-3, 15]
, lr_params=['val_accuracy', 0.001, 5]):
cb = []
cb.append(callbacks.EarlyStopping(
 monitor=es_params[0], min_delta=es_params[1], patience=es_params[2]
, verbose=1, restore_best_weights=True 
))
if sched_lr_val_acc:
cb.append(callbacks.ReduceLROnPlateau(
 monitor=lr_params[0], factor=0.5, patience=lr_params[2]
, min_delta=lr_params[1], min_lr=ini_lr*0.1**5 
))
cb.append(callbacks.TerminateOnNaN())
 return cb
DefaultConv2D = partial(Conv2D
, kernel_size=3
, strides=1
, activation=activation
, kernel_initializer=initializer
, padding='SAME')
 DefaultMaxPool2D = partial(MaxPool2D
, pool_size=2
, strides=2)
 DefaultDense = partial(Dense
, activation=activation
, kernel_initializer=initializer
, kernel_regularizer=get_regularizer())
 def get_model(compiling=True, n_samples=n_train_samples, batch_size=batch_size
, activation=activation, initializer=initializer, p=0.5):
model = Sequential(
 [DefaultConv2D(filters=32, kernel_size=5, input_shape=(28,28,1))
, BatchNormalization()
, DefaultMaxPool2D()
, DefaultConv2D(filters=64)
 , BatchNormalization()
, DefaultConv2D(filters=64)
 , BatchNormalization()
, DefaultMaxPool2D()
, DefaultConv2D(filters=128)
 , BatchNormalization()
, DefaultConv2D(filters=128)
 , BatchNormalization()
, DefaultMaxPool2D()
, Flatten()
, DefaultDense(units=64)
 , Dropout(p)
 , DefaultDense(units=32)
 , Dropout(p)
 , Dense(units=10, activation='softmax')
 ] 
)
 optimizer = get_optimizer(n_samples=n_samples, batch_size=batch_size)
 if compiling:
opt = get_optimizer()
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
 return model
%env SM_FRAMEWORK=tf.keras<set_options>
verbose = 1
model = get_model(n_samples=n_train_val_samples)
 print(model.summary())
 epochs = 100
cb = get_callbacks()
history = model.fit(tr_set, epochs = epochs
, steps_per_epoch = n_train_val_samples // batch_size
, validation_data = val_set
, callbacks = cb
, verbose = verbose )
warnings.filterwarnings('ignore')
 print('tensorflow version:', tf.__version__)
 os.environ['CUDA_VISIBLE_DEVICES'] = '0'
gpu_devices = tf.config.experimental.list_physical_devices('GPU')
 if gpu_devices:
for gpu_device in gpu_devices:
print('device available:', gpu_device)
 pd.set_option('display.max_columns', None )<define_variables>
y_pred = model.predict(test_set)
 y_pred = np.argmax(y_pred, axis=1)
 submission = reload('sample_submission.csv')
 submission['Label'] = y_pred
submission.to_csv('submission.csv', index=False )