Kaggle competition: Digit Recognizer
class my_params:
seed = 1
batch_size = 256
test_size = 0.1<load_from_csv>
train = pd.read_csv(".. /input/train.csv")
 train.shape
train_pd = pd.read_csv(".. /input/digit-recognizer/train.csv", dtype=np.float32)
 final_test = pd.read_csv(".. /input/digit-recognizer/test.csv", dtype=np.float32)
 sample_sub = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 train_pd.info()<set_options>
test = pd.read_csv(".. /input/test.csv")
 test.shape
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 print(device )<split>
img_width = 28
img_height = 28
nclasses = 10
classes = np.arange(10)
 labels = [ str(c)for c in classes ]
train_X = train.iloc[:,1:].values.reshape(train.shape[0], img_width, img_height, 1)
 train_y = train.iloc[:,0].values
labels_np = train_pd.label.values
features_np = train_pd.loc[:, train_pd.columns != 'label'].values/255
features_train, features_val, labels_train, labels_val = train_test_split(features_np, labels_np,
test_size = my_params.test_size,
random_state = my_params.seed
)<data_type_conversions>
test_X = test.iloc[:,0:].values.reshape(test.shape[0], img_width, img_height, 1)
featuresTrain = torch.from_numpy(features_train)
 labelsTrain = torch.from_numpy(labels_train ).type(torch.LongTensor)
 featuresVal = torch.from_numpy(features_val)
 labelsVal = torch.from_numpy(labels_val ).type(torch.LongTensor )<categorify>
train_X = train_X / train_X.max()
test_X = test_X / test_X.max()
train_transforms = transforms.Compose([transforms.ToPILImage() ,
transforms.RandomRotation(degrees=(-10, 10)) ,
transforms.RandomAffine(0, translate=(0.1,0.1)) ,
transforms.ToTensor() ] )<categorify>
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, Dropout, BatchNormalization, MaxPool2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras.utils import to_categorical
from tensorflow import set_random_seed
class custom_mnist(Dataset):
def __init__(self, feat_tens, label_tens, transform=None):
self.data = feat_tens.reshape(len(feat_tens), 1, 28, 28)
 self.label_data = label_tens
self.transform = transform
def __len__(self):
return len(self.data)
 def __getitem__(self, index):
image = self.data[index]
label = self.label_data[index]
if self.transform is not None:
image = self.transform(image)
 return image, label<load_pretrained>
class MNIST_CNN:

 def build(self, img_rows, img_cols, nclasses=10):
input_shape=(img_rows, img_cols, 1)
 model = Sequential()
model.add(Conv2D(32,(3,3), activation="relu", input_shape=(img_rows, img_cols, 1),
kernel_initializer="glorot_normal", padding="same"))
model.add(Conv2D(32,(3,3), activation="relu",
kernel_initializer="glorot_normal", padding="same"))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64,(3,3), activation="relu",
kernel_initializer="glorot_normal", padding="same"))
model.add(Conv2D(64,(3,3), activation="relu",
kernel_initializer="glorot_normal", padding="same"))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(128,(3,3), activation="relu",
kernel_initializer="glorot_normal", padding="same"))
model.add(Conv2D(128,(3,3), activation="relu",
kernel_initializer="glorot_normal", padding="same"))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(300, activation="relu"))
model.add(Dropout(0.25))
model.add(Dense(nclasses, activation="softmax"))
self.model = model
def compile(self, **kw):
kw1 = dict(optimizer="adam", loss="categorical_crossentropy", metrics=["categorical_accuracy"])
 kw1.update(kw)
 self.model.compile(**kw1)
 def fit(self, *arg, **kw):
return self.model.fit(*arg, **kw)
 mdl = MNIST_CNN()
mdl.build(img_width, img_height)
 mdl.compile()
mdl.model.summary()
train_dataset = custom_mnist(featuresTrain, labelsTrain)
 val_dataset = custom_mnist(featuresVal, labelsVal)
 train_loader = torch.utils.data.DataLoader(train_dataset, batch_size = my_params.batch_size, shuffle = True)
 val_loader = torch.utils.data.DataLoader(val_dataset)

print("train_loader: {}".format(len(train_loader)) , "
val_loader: {}".format(len(val_loader)) )<train_model>
DEBUG = 0
K = 3 if DEBUG else 10
set_random_seed(777)
 skf = StratifiedKFold(n_splits=K, shuffle=True)
 train_y_cat = to_categorical(train_y)
 histories = []
test_pred = np.zeros(( len(test_X), nclasses), dtype=np.float64)
 train_pred = np.zeros(( len(train_X), nclasses), dtype=np.float64)
 val_pred = np.zeros(( len(train_X), nclasses), dtype=np.float64)
 train_accs = []
val_accs = []
for i,(train_idx, val_idx)in enumerate(skf.split(train_X, train_y)) :
t0 = time.time()
batch_size=256 if DEBUG else 64
mdl = MNIST_CNN()
mdl.build(img_width, img_height)
 mdl.compile()
histories.append(
 mdl.model.fit_generator(
 datagen.flow(train_X[train_idx], train_y_cat[train_idx], batch_size=batch_size),
epochs=5 if DEBUG else 200,
verbose=1 if DEBUG else 0,
steps_per_epoch=len(train_idx)/ batch_size,
validation_data=(train_X[val_idx], train_y_cat[val_idx]), 
), 
)
 test_pred += mdl.model.predict(test_X)
 train_pred += mdl.model.predict(train_X)
 val_pred[val_idx] += mdl.model.predict(train_X[val_idx])
 train_accs.append(accuracy_score(train_y, mdl.model.predict(train_X ).argmax(axis=1)))
 val_accs.append(accuracy_score(train_y[val_idx], val_pred[val_idx].argmax(axis=1)))
 print("******* [dt={0}] accuracy_scores at fold {1}: train={2} val={3}".format(
 time.time() - t0, i + 1, train_accs[-1], val_accs[-1]))
test_y = np.argmax(test_pred, axis=1)
 val_y = np.argmax(val_pred, axis=1 )
img, lab = train_dataset.__getitem__(3)
 print("image_shape: ", img.shape)
 print("image_label: ", lab)
 plt.imshow(np.squeeze(img))<choose_model_class>
print("global fit accuracy_scores: train={0} val={1}".format(
 accuracy_score(train_y, train_pred.argmax(axis=1)) ,
accuracy_score(train_y, val_pred.argmax(axis=1))))
print(f"{K}-Fold train accuracy = {np.mean(train_accs):0.4f} +- {np.std(train_accs):0.4f}")
 print(f"{K}-Fold cross validation accuracy = {np.mean(val_accs):0.4f} +- {np.std(val_accs):0.4f}" )
class NeuralNetwork(nn.Module):
def __init__(self):

 super(NeuralNetwork, self ).__init__()
self.features = torch.nn.Sequential(
 nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, padding=1),
nn.ReLU() ,
nn.BatchNorm2d(num_features=32),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, padding=1),
nn.ReLU() ,
nn.BatchNorm2d(num_features=32),
nn.MaxPool2d(kernel_size =(2,2), stride=2),
nn.Dropout(0.25),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1),
nn.ReLU() ,
nn.BatchNorm2d(num_features=64),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1),
nn.ReLU() ,
nn.BatchNorm2d(num_features=64),
nn.MaxPool2d(kernel_size =(2,2), stride=2),
nn.Dropout(0.25),
nn.Flatten() ,
nn.Linear(in_features=3136, out_features=512),
nn.BatchNorm1d(num_features=512),
nn.Dropout(0.25),
nn.Linear(in_features=512, out_features=1024),
nn.BatchNorm1d(num_features=1024),
nn.Dropout(0.50),
nn.Linear(in_features=1024, out_features=10),
nn.ReLU() ,
nn.Softmax(dim=1), 
)
 def forward(self, x):
logits = self.features(x)
 return logits
NeuralNetwork()<choose_model_class>
df_test = pd.DataFrame({'ImageId': 1 + np.arange(len(test_X)) ,
'Label': test_y })
 df_test.to_csv('submit.csv', index=False)