Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<install_modules>
mnist_test = pd.read_csv(".. /input/mnist-in-csv/mnist_test.csv")
 mnist_train = pd.read_csv(".. /input/mnist-in-csv/mnist_train.csv" )
!pip install.. /input/validators
!cp -R.. /input/vit-keras/vit_keras./<import_modules>
sample_submission = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 train = pd.read_csv(".. /input/digit-recognizer/train.csv" )
Flatten,GlobalAveragePooling2D,BatchNormalization, Activation
print(tf.__version__ )<find_best_params>
test['dataset'] = 'test'
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print(f'Running on TPU {tpu.master() }')
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
AUTO = tf.data.experimental.AUTOTUNE
REPLICAS = strategy.num_replicas_in_sync
print(f'REPLICAS: {REPLICAS}' )<define_variables>
train['dataset'] = 'train'
SEED = 100
DEBUG = False
WANDB = False
VALIDATION_SIZE = 0.2
BATCH_SIZE = 8 *REPLICAS
LEARNING_RATE = 3e-5 * REPLICAS
EPOCHS=40
MODEL_NAME = "VitL16"
N_FOLDS = 5
TTA = False
N_TTA = 3
T_1 = 0.2
T_2 = 1.2
SMOOTH_FRACTION = 0.01
N_ITER = 5
HEIGHT = 512
WIDTH = 512
HEIGHT_RS = 512
WIDTH_RS = 512
CHANNELS = 3
N_CLASSES = 5
MODEL_SAVE_PATH = ""
if DEBUG:
EPOCHS = 2 
<normalization>
dataset = pd.concat([train.drop('label', axis=1), test] ).reset_index()
def transform_rotation(image, height, rotation):
DIM = height
XDIM = DIM%2
rotation = rotation * tf.random.uniform([1],dtype='float32')
 rotation = math.pi * rotation / 180.
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = tf.reshape(tf.concat([c1,s1,zero, -s1,c1,zero, zero,zero,one],axis=0),[3,3])
 x = tf.repeat(tf.range(DIM//2,-DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2,DIM//2),[DIM])
 z = tf.ones([DIM*DIM],dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(rotation_matrix,tf.cast(idx,dtype='float32'))
idx2 = K.cast(idx2,dtype='int32')
 idx2 = K.clip(idx2,-DIM//2+XDIM+1,DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image, tf.transpose(idx3))
return tf.reshape(d,[DIM,DIM,3])
 def transform_shear(image, height, shear):
DIM = height
XDIM = DIM%2
shear = shear * tf.random.uniform([1],dtype='float32')
 shear = math.pi * shear / 180.
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = tf.reshape(tf.concat([one,s2,zero, zero,c2,zero, zero,zero,one],axis=0),[3,3])
 x = tf.repeat(tf.range(DIM//2,-DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2,DIM//2),[DIM])
 z = tf.ones([DIM*DIM],dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(shear_matrix,tf.cast(idx,dtype='float32'))
idx2 = K.cast(idx2,dtype='int32')
 idx2 = K.clip(idx2,-DIM//2+XDIM+1,DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image, tf.transpose(idx3))
return tf.reshape(d,[DIM,DIM,3])
 def data_augment_cutout(image, min_mask_size=(int(HEIGHT *.1), int(HEIGHT *.1)) ,
max_mask_size=(int(HEIGHT *.125), int(HEIGHT *.125))):
p_cutout = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 if p_cutout >.85:
n_cutout = tf.random.uniform([], 10, 15, dtype=tf.int32)
 image = random_cutout(image, HEIGHT, WIDTH,
min_mask_size=min_mask_size, max_mask_size=max_mask_size, k=n_cutout)
 elif p_cutout >.6:
n_cutout = tf.random.uniform([], 5, 10, dtype=tf.int32)
 image = random_cutout(image, HEIGHT, WIDTH,
min_mask_size=min_mask_size, max_mask_size=max_mask_size, k=n_cutout)
 elif p_cutout >.25:
n_cutout = tf.random.uniform([], 2, 5, dtype=tf.int32)
 image = random_cutout(image, HEIGHT, WIDTH,
min_mask_size=min_mask_size, max_mask_size=max_mask_size, k=n_cutout)
 else:
image = random_cutout(image, HEIGHT, WIDTH,
min_mask_size=min_mask_size, max_mask_size=max_mask_size, k=1)
 return image
def random_cutout(image, height, width, channels=3, min_mask_size=(10, 10), max_mask_size=(80, 80), k=1):
assert height > min_mask_size[0]
assert width > min_mask_size[1]
assert height > max_mask_size[0]
assert width > max_mask_size[1]
for i in range(k):
mask_height = tf.random.uniform(shape=[], minval=min_mask_size[0], maxval=max_mask_size[0], dtype=tf.int32)
 mask_width = tf.random.uniform(shape=[], minval=min_mask_size[1], maxval=max_mask_size[1], dtype=tf.int32)
 pad_h = height - mask_height
pad_top = tf.random.uniform(shape=[], minval=0, maxval=pad_h, dtype=tf.int32)
 pad_bottom = pad_h - pad_top
pad_w = width - mask_width
pad_left = tf.random.uniform(shape=[], minval=0, maxval=pad_w, dtype=tf.int32)
 pad_right = pad_w - pad_left
cutout_area = tf.zeros(shape=[mask_height, mask_width, channels], dtype=tf.uint8)
 cutout_mask = tf.pad([cutout_area], [[0,0],[pad_top, pad_bottom], [pad_left, pad_right], [0,0]], constant_values=1)
 cutout_mask = tf.squeeze(cutout_mask, axis=0)
 image = tf.multiply(tf.cast(image, tf.float32), tf.cast(cutout_mask, tf.float32))
return image<define_search_space>
mnist = pd.concat([mnist_train, mnist_test] ).reset_index(drop=True)
 labels = mnist['label'].values
mnist.drop('label', axis=1, inplace=True)
 mnist.columns = cols
def data_augment(image, label):
p_rotation = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_spatial = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_rotate = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_pixel_1 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_pixel_2 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_pixel_3 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_shear = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_crop = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 p_cutout = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 flip_left = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 flip_right = tf.random.uniform([], 0, 1.0, dtype=tf.float32)
 if p_rotation >.2:
if p_rotation >.6:
image = transform_rotation(image, HEIGHT, rotation=10.)
else:
image = transform_rotation(image, HEIGHT, rotation=-10.)
if flip_left>0.5:
image = tf.image.random_flip_left_right(image)
 if flip_right>0.5 :
image = tf.image.random_flip_up_down(image)
 image = tf.image.resize(image, size=[HEIGHT, WIDTH])
 return image, label<define_variables>
idx_mnist = mnist.sort_values(by=list(mnist.columns)).index
dataset_from = dataset.sort_values(by=list(mnist.columns)) ['dataset'].values
original_idx = dataset.sort_values(by=list(mnist.columns)) ['index'].values
copyfile(src = ".. /input/bitempered-logistic-loss-tensorflow-v2/bi_tempered_loss.py", dst = ".. /working/loss.py")
<compute_train_metric>
for i in range(len(idx_mnist)) :
if dataset_from[i] == 'test':
sample_submission.loc[original_idx[i], 'Label'] = labels[idx_mnist[i]]
<choose_model_class><EOS> 
sample_submission.to_csv('submission.csv', index=False )