Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<predict_on_test>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import os
import seaborn as sns
import matplotlib.image as mpimg
y_test = model.predict(X_test)
 y_pred = np.argmax(y_test, axis=1 )<save_to_csv>
train_input = ".. /input/digit-recognizer/train.csv"
test_input = ".. /input/digit-recognizer/test.csv"
train_dataset = pd.read_csv(train_input)
 test_dataset = pd.read_csv(test_input )
output_csv = {"ImageId":[*range(1,1+len(y_pred)) ], "Label":y_pred}
Y_pre = pd.DataFrame(output_csv)
 Y_pre.set_index("ImageId", drop=True, append=False, inplace=True)
 Y_pre.to_csv("/kaggle/working/submission.csv" )<import_modules>
train_labels = tf.keras.utils.to_categorical(train_dataset.pop("label"))
import numpy as np
import pandas as pd
import os
import torch
import torch.nn as nn
import torch.optim as optim
from PIL import Image
from matplotlib import pyplot as plt
from torch.utils.data import Dataset,DataLoader
from torchvision import transforms as T
from torchvision import models
import tqdm
from sklearn.metrics import f1_score,roc_auc_score,accuracy_score,confusion_matrix<load_from_csv>
train_dataset = np.array(train_dataset.values.reshape(-1, 28, 28, 1))
test_dataset = np.array(test_dataset.values.reshape(-1, 28, 28, 1))
train_df=pd.read_csv(".. /input/digit-recognizer/train.csv")
 test_df=pd.read_csv(".. /input/digit-recognizer/test.csv" )<categorify>
train_dataset = train_dataset/255.0
test_dataset = test_dataset/255.0
def get_image(data_df,idx):

 return Image.fromarray(np.uint8(np.reshape(data_df.iloc[idx][data_df.columns[-784:]].to_numpy() ,(28,28)))).convert('RGB')
<categorify>
checkpoint_path = "logs/checkpoints/"
class TrainDataSet(Dataset):
def __init__(self,data_df,transforms=T.ToTensor()):

 self.data_df=data_df
self.transform=transforms
def __len__(self):
return self.data_df.shape[0]
def __getitem__(self,idx):
image=self.transform(get_image(self.data_df,idx))
label=torch.tensor(self.data_df.label.iloc[idx],dtype=torch.long)
 return image,label<categorify>
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64,(3, 3), input_shape=(28, 28, 1), activation=tf.nn.relu, padding="SAME"),
tf.keras.layers.MaxPooling2D() ,
tf.keras.layers.Conv2D(64,(3, 3), activation=tf.nn.relu, padding="SAME"),
tf.keras.layers.MaxPooling2D() ,
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Conv2D(128,(3, 3), activation=tf.nn.relu, padding="SAME"),
tf.keras.layers.MaxPooling2D() ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(128,(3, 3), activation=tf.nn.relu, padding="SAME"),
tf.keras.layers.MaxPooling2D() ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(10, activation="softmax")
 ])
 epochs=50
callbacks = [
tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
monitor="accuracy",
save_best_only=True,
save_weights_only=True),
tf.keras.callbacks.EarlyStopping(monitor="accuracy", patience=15)
 ]
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
 history = model.fit(train_dataset, train_labels, epochs=epochs, callbacks=callbacks, batch_size=64 )
class TestDataSet(TrainDataSet):
def __getitem__(self,idx):
image=self.transform(get_image(self.data_df,idx))
return image<choose_model_class>
model.load_weights(checkpoint_path )
def create_model() :
model = models.resnet18(pretrained=True)
 num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 10)
 return model<categorify>
labels = [np.argmax(predict)for predict in model.predict(test_dataset)]
df = pd.DataFrame({
"ImageId": list(range(1, len(test_dataset)+1)) ,
"Label": labels,
} )
transform=T.Compose([
T.Resize(( 256,256)) ,
T.ToTensor() ,
T.Normalize(( 0.485, 0.456, 0.406),(0.229, 0.224, 0.225))
] )<choose_model_class>
df.to_csv("submission.csv", index=False )
def train_once(model,dataloader,criterion,optimizer,device):
total_loss=0
n_total=0
criterion.reduction="sum"
model.train()
for i,(images,labels)in enumerate(tqdm.tqdm(dataloader)) :
optimizer.zero_grad()
images=images.to(device)
 labels=labels.to(device)
 outputs=model(images)
 loss=criterion(outputs,labels)
 total_loss+=loss.item()
n_total+=labels.shape[0]
loss.backward()
optimizer.step()
return total_loss/n_total<choose_model_class>
model.save("model.h5" )