Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<load_from_csv>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Model, load_model
from keras.layers import Conv2D, Input, MaxPooling2D, Dense, Dropout, Flatten
from keras.layers import LeakyReLU
from keras.layers.normalization import BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.utils.np_utils import to_categorical
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from sklearn.model_selection import train_test_split
sample_submission = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 sample_submission.head()<categorify>
train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 submission = pd.read_csv("/kaggle/input/digit-recognizer/sample_submission.csv")
 X = train.drop(['label'],1 ).values
Y = train['label'].values
x_test = test.values
X = X/255.
 x_test = x_test/255.
 X = X.reshape(-1,28,28,1)
 x_test = x_test.reshape(-1,28,28,1)
 Y = to_categorical(Y)
class CassavaDataset(Dataset):
def __init__(self, df, data_root, transforms = None, output_label = True):

 super().__init__()
self.df = df.reset_index(drop = True ).copy()
self.transforms = transforms
self.data_root = data_root
self.output_label = output_label
def __len__(self):

 return self.df.shape[0]
def __getitem__(self, index: int):

 path = "{}/{}".format(self.data_root, self.df.iloc[index]['image_id'])
 img = get_img(path)
 if self.transforms:
img = self.transforms(image = img)['image']
if self.output_label:
target = self.df.iloc[index]['label']
if self.output_label:
return img, target
else:
return img<import_modules>
x_train, x_valid, y_train, y_valid = train_test_split(X,Y, test_size=0.1 )
HorizontalFlip, VerticalFlip, Transpose, ShiftScaleRotate,
HueSaturationValue,RandomResizedCrop, RandomBrightnessContrast,
Compose, Normalize, Cutout, CoarseDropout, CenterCrop, Resize 
)
<categorify>
def get_model() :
In = Input(shape=(28,28,1))
x = Conv2D(32,(3,3), padding="same" )(In)
 x = LeakyReLU(alpha=0.01 )(x)
 x = Conv2D(32,(3,3), padding="same" )(x)
 x = LeakyReLU(alpha=0.01 )(x)
 x = BatchNormalization()(x)
 x = MaxPooling2D(( 2,2))(x)
 x = Conv2D(64,(3,3), padding="same" )(x)
 x = LeakyReLU(alpha=0.01 )(x)
 x = Conv2D(64,(3,3), padding="same" )(x)
 x = LeakyReLU(alpha=0.01 )(x)
 x = BatchNormalization()(x)
 x = MaxPooling2D(( 2,2))(x)
 x = Dropout(0.2 )(x)
 x = Conv2D(128,(3,3), padding="same" )(x)
 x = LeakyReLU(alpha=0.01 )(x)
 x = Conv2D(128,(3,3), padding="same" )(x)
 x = LeakyReLU(alpha=0.01 )(x)
 x = BatchNormalization()(x)
 x = MaxPooling2D(( 2,2))(x)
 x = Dropout(0.2 )(x)
 x = Flatten()(x)
 x = Dense(512 )(x)
 x = LeakyReLU(alpha=0.01 )(x)
 x = BatchNormalization()(x)
 x = Dropout(0.2 )(x)
 x = Dense(256 )(x)
 x = LeakyReLU(alpha=0.01 )(x)
 x = BatchNormalization()(x)
 x = Dropout(0.2 )(x)
 x = Dense(128 )(x)
 x = LeakyReLU(alpha=0.01 )(x)
 x = BatchNormalization()(x)
 x = Dropout(0.2 )(x)
 Out = Dense(10, activation="softmax" )(x)
 model = Model(In, Out)
 model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
 return model
model = get_model()
model.summary()
def get_train_transforms() :

 return Compose([
RandomResizedCrop(CFG['img_size'], CFG['img_size']),
Transpose(p=0.5),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
ShiftScaleRotate(p=0.5),
HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
CoarseDropout(p=0.5),
Cutout(p=0.5),
ToTensorV2(p=1.0),
], p=1.)<categorify>
best_checkpoint = ModelCheckpoint('best.hdf5',monitor = 'val_loss', mode = "min", verbose = 1, save_best_only = True)
 lr_reduction = ReduceLROnPlateau(monitor = 'val_loss', patience = 3, verbose = 1, factor = 0.5, min_lr = 1e-6 )
def get_inference_transforms() :

 return Compose([
RandomResizedCrop(CFG['img_size'], CFG['img_size']),
Transpose(p=0.5),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.)<define_variables>
data_generator = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 epochs = 100
batch_size = 128
train_generator = data_generator.flow(x_train, y_train, batch_size=batch_size)
 valid_generator = data_generator.flow(x_valid, y_valid, batch_size=batch_size )
package_path = '.. /input/pytorch-image-models/pytorch-image-models-master'
sys.path.append(package_path)
<import_modules>
hist = model.fit_generator(train_generator, epochs=epochs, steps_per_epoch = x_train.shape[0]//batch_size,
validation_data = valid_generator, validation_steps = x_valid.shape[0]//batch_size, callbacks=[best_checkpoint, lr_reduction], verbose=1 )
class CassavaImgClassifier(nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):

 super().__init__()
self.model = timm.create_model(model_arch, pretrained=pretrained)
 n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(n_features, n_class)
 def forward(self, x):
x = self.model(x)
 return x<define_variables>
best = load_model("best.hdf5")
 preds = best.predict(x_test, verbose=1)
 preds = np.array([np.argmax(i)for i in preds])
 preds
<import_modules><EOS> 
submission['Label'] = preds
submission.to_csv("submission.csv", index=False)
 submission.head()