Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<predict_on_test>
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv" )
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )<save_to_csv>
train.isnull().sum().sum()
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_submission.csv",index=False )<train_model>
labels = train["label"]
pureimg_train = train.drop(labels = ["label"], axis = 1)
 del train
( x_train1, y_train1),(x_test1, y_test1)= mnist.load_data()
Y_train1 = y_train1
X_train1 = x_train1.reshape(-1, 28*28 )<load_from_csv>
norm_train = pureimg_train/255
norm_test = test/255
train_data = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test_data = pd.read_csv('.. /input/digit-recognizer/test.csv' )<prepare_x_and_y>
feature_train, feature_validate, target_train, target_validate = train_test_split(norm_train, labels, test_size = 0.1, random_state = 0)
train_images = train_data.copy()
train_images = train_images.values
X_train = train_images[:,1:]
y_train = train_images[:,0]
X_test = test_data.values<define_variables>
Test = torch.from_numpy(norm_test.values.reshape(( -1,1,28,28)))
 featuresTrain = torch.from_numpy(feature_train.values.reshape(( -1,1,28,28)))
 targetsTrain = torch.from_numpy(target_train.values)
 featuresValidation = torch.from_numpy(feature_validate.values.reshape(( -1,1,28,28)))
 targetsValidation = torch.from_numpy(target_validate.values )
predictions = np.zeros(( X_train.shape[0]))<find_best_params>
batch_size = 88
x1=0
x2=0
print("Classifying Kaggle's 'test.csv' using KNN where K=1 and MNIST 70k images.. ")
 for i in range(0,28000):
for j in range(0,70000):
if np.absolute(X_test[i,:]-mnist_image[j,:] ).sum() ==0:
predictions[i]=mnist_label[j]
if i%1000==0:
print(" %d images classified perfectly"%(i),end="")
 if j<60000:
x1+=1
else:
x2+=1
break
if x1+x2==28000:
print(" 28000 images classified perfectly.")
 print("All 28000 images are contained in MNIST.npz Dataset.")
 print("%d images are in MNIST.npz train and %d images are in MNIST.npz test"%(x1,x2))<define_variables>
class MNISTDataset(Dataset):

 def __init__(self, feature, target=None, transform=None):
self.X = feature
self.y = target
self.transform = transform
def __len__(self):
return len(self.X)
 def __getitem__(self, idx):
if self.transform is not None:
return self.transform(self.X[idx]), self.y[idx]
elif self.y is None:
return [self.X[idx]]
return self.X[idx], self.y[idx]
final_pred = predictions[0:28000]<prepare_output>
data_transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=45, translate=(0.1, 0.1), scale=(0.8, 1.2)) ,
transforms.ToTensor() ])
 train_set = MNISTDataset(featuresTrain.float() , targetsTrain, transform=data_transform)
 validate_set = MNISTDataset(featuresValidation.float() , targetsValidation)
 test_set = MNISTDataset(Test.float() )
my_submission = pd.DataFrame({'ImageId':np.arange(28000),'Label':final_pred.squeeze().astype(np.int)})
 my_submission.head()<feature_engineering>
train_set = torch.utils.data.TensorDataset(featuresTrain.float() , targetsTrain)
 validate_set = torch.utils.data.TensorDataset(featuresValidation.float() , targetsValidation)
 test_set = torch.utils.data.TensorDataset(Test.float() )
my_submission["ImageId"]=my_submission["ImageId"]+1<save_to_csv>
train_loader = torch.utils.data.DataLoader(train_set, batch_size = batch_size, shuffle = True)
 validate_loader = torch.utils.data.DataLoader(validate_set, batch_size = batch_size, shuffle = False)
 test_loader = torch.utils.data.DataLoader(test_set, batch_size = batch_size, shuffle = False )
my_submission.to_csv('best_submission.csv', index=False )<install_modules>
class CNNModel(nn.Module):
def __init__(self):
super(CNNModel, self ).__init__()
self.cnn = nn.Sequential(nn.Conv2d(in_channels=1, out_channels=32, kernel_size=5),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=5),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),
nn.Dropout(0.25),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Dropout(0.25))
self.classifier = nn.Sequential(nn.Linear(576, 256),
nn.Dropout(0.5),
nn.Linear(256, 10))
def forward(self, x):
x = self.cnn(x)
 x = x.view(x.size(0), -1)
 x = self.classifier(x)
 return x
! pip install.. /input/mlcollection/ml_collections-0.1.0-py3-none-any.whl<import_modules>
model = CNNModel()
optimizer = optim.RMSprop(model.parameters() , lr=0.001, alpha=0.9)
 criterion = nn.CrossEntropyLoss()
lr_reduction = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=3, threshold=0.0001, threshold_mode='rel', cooldown=0, min_lr=0.00001)
 if torch.cuda.is_available() :
model = model.cuda()
criterion = criterion.cuda()
from glob import glob
from sklearn.model_selection import GroupKFold, StratifiedKFold
import cv2
from skimage import io
import torch
from torch import nn
import os
from datetime import datetime
import time
import random
import cv2
import torchvision
from torchvision import transforms
import pandas as pd
import numpy as np
from tqdm import tqdm
from fastai.vision.all import *
import matplotlib.pyplot as plt
from torch.utils.data import Dataset,DataLoader
from torch.utils.data.sampler import SequentialSampler, RandomSampler
from torch.cuda.amp import autocast, GradScaler
import sklearn
import warnings
import joblib
from sklearn.metrics import roc_auc_score, log_loss
from sklearn import metrics
import warnings
import cv2
import pydicom
import timm
from scipy.ndimage.interpolation import zoom
from sklearn.metrics import log_loss 
<init_hyperparams>
count = 0
loss_list = []
iteration_list = []
average_training_accuracy = []
average_validation_accuracy = []
average_training_loss = []
average_validation_loss = []
CFG = {
'fold_num': 5,
'seed': 719,
'model_arch': 'resnext101_ibn_a',
'model_arch_eff':'tf_efficientnet_b4_ns',
'img_size': 512,
'epochs': 10,
'train_bs': 32,
'valid_bs': 32,
'lr': 1e-4,
'num_workers': 4,
'accum_iter': 1,
'verbose_step': 1,
'device': 'cuda' if torch.cuda.is_available() else 'cpu',
'tta': 4,
}
ckpt_paths = ['.. /input/tf-efficientnet-b4-ns-fold-0-8-089533/tf_efficientnet_b4_ns_fold_0_4_0.89299',
'.. /input/tf-efficientnet-b4-ns-fold-0-8-089533/tf_efficientnet_b4_ns_fold_0_6_0.89065',
'.. /input/tf-efficientnet-b4-ns-fold-0-8-089533/tf_efficientnet_b4_ns_fold_0_8_0.89533',
'.. /input/tf-efficientnet-b4-ns-fold-0-8-089533/tf_efficientnet_b4_ns_fold_0_9_0.88995'
]
ckpt_paths = [
'.. /input/resnext101-ibn-a-01-22-10-05-bit-ls015-896/resnext101_ibn_a_fold_0_6_0.88435',
'.. /input/resnext101-ibn-a-01-22-10-05-bit-ls015-896/resnext101_ibn_a_fold_0_7_0.89299',
'.. /input/resnext101-ibn-a-01-22-10-05-bit-ls015-896/resnext101_ibn_a_fold_0_8_0.88995',
'.. /input/resnext101-ibn-a-01-22-10-05-bit-ls015-896/resnext101_ibn_a_fold_0_9_0.89603']
ckpt_weights = [1,1,1,1]
ckpt_dir = '.. /input/resnext101-ibn-a-01-26-01-27-bit-ema-5folds'
ckpt_paths = [os.path.join(ckpt_dir, sub_path)for sub_path in os.listdir(ckpt_dir)if not ckpt_dir.endswith('.txt')]
ckpt_paths = ['.. /input/resnext101-ibn-a-01-26-01-27-bit-ema-5folds/resnext101_ibn_a_fold_0_9_0.89439',
'.. /input/resnext101-ibn-a-01-26-01-27-bit-ema-5folds/resnext101_ibn_a_fold_0_19_0.89393',
'.. /input/resnext101-ibn-a-01-26-01-27-bit-ema-5folds/resnext101_ibn_a_fold_1_9_0.89603',
'.. /input/resnext101-ibn-a-01-26-01-27-bit-ema-5folds/resnext101_ibn_a_fold_1_8_0.89486',
'.. /input/resnext101-ibn-a-01-26-01-27-bit-ema-5folds/resnext101_ibn_a_fold_2_9_0.88759',
'.. /input/resnext101-ibn-a-01-26-01-27-bit-ema-5folds/resnext101_ibn_a_fold_2_17_0.88619',
'.. /input/resnext101-ibn-a-01-26-01-27-bit-ema-5folds/resnext101_ibn_a_fold_3_16_0.89928',
'.. /input/resnext101-ibn-a-01-26-01-27-bit-ema-5folds/resnext101_ibn_a_fold_3_19_0.89904',
'.. /input/resnext101-ibn-a-01-26-01-27-bit-ema-5folds/resnext101_ibn_a_fold_4_18_0.89857',
'.. /input/resnext101-ibn-a-01-26-01-27-bit-ema-5folds/resnext101_ibn_a_fold_4_9_0.89787',
]
ckpt_paths = [
'.. /input/vit-convert-tta-seed719/vit__1920-fold0_checkpoint-17.pth',
'.. /input/vit-convert-tta-seed719/vit__1920-fold1_checkpoint-14.pth',
'.. /input/vit-convert-tta-seed719/vit__1920-fold2_checkpoint-16.pth',
'.. /input/vit-convert-tta-seed719/vit__1920-fold3_checkpoint-8.pth',
'.. /input/vit-convert-tta-seed719/vit__1920-fold4_checkpoint-9.pth',
'.. /input/alldata-ema-resnext101-ibn-a/resnext101_ibn_a_fold_0_11_0.89603',
'.. /input/alldata-ema-resnext101-ibn-a/resnext101_ibn_a_fold_1_10_0.89369',
'.. /input/alldata-ema-resnext101-ibn-a/resnext101_ibn_a_fold_2_13_0.88899',
'.. /input/alldata-ema-resnext101-ibn-a/resnext101_ibn_a_fold_3_12_0.89834',
'.. /input/alldata-ema-resnext101-ibn-a/resnext101_ibn_a_fold_4_11_0.89741',
'.. /input/alldata-ema-resnext101-32x4d-swsl-adam-02-05-17-28/resnext101_32x4d_swsl_fold_0_12_0.89369',
'.. /input/alldata-ema-resnext101-32x4d-swsl-adam-02-05-17-28/resnext101_32x4d_swsl_fold_1_12_0.89416',
'.. /input/alldata-ema-resnext101-32x4d-swsl-adam-02-05-17-28/resnext101_32x4d_swsl_fold_2_8_0.89203',
'.. /input/alldata-ema-resnext101-32x4d-swsl-adam-02-05-17-28/resnext101_32x4d_swsl_fold_3_12_0.90115',
'.. /input/alldata-ema-resnext101-32x4d-swsl-adam-02-05-17-28/resnext101_32x4d_swsl_fold_4_14_0.89530',
]
model_names = [
'vit',
'vit',
'vit',
'vit',
'vit',
'resnext101_ibn_a',
'resnext101_ibn_a',
'resnext101_ibn_a',
'resnext101_ibn_a',
'resnext101_ibn_a',
'resnext101',
'resnext101',
'resnext101',
'resnext101',
'resnext101',
]
assert len(model_names)== len(ckpt_paths)
 print(len(ckpt_paths))
ckpt_weights = [1 for i in range(len(ckpt_paths)) ]
print(ckpt_paths)
 model_wts={}
model_wts['vit']=0.6582084173812914
model_wts['resnext101']=0.16946139705016672
model_wts['resnext101_ibn_a']=0.6465801475470692 
<load_from_csv>
def train(epoch):
global count
model.train()
for batch_idx,(data, target)in enumerate(train_loader):
data, target = Variable(data), Variable(target)
 if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target)
 loss.backward()
optimizer.step()
if(batch_idx + 1)% 100 == 0:
loss_list.append(loss.item())
 iteration_list.append(count)
 count += 1
train = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 train.head()<count_values>
def evaluate(data_loader, validate=False):
model.eval()
loss = 0
correct = 0
for data, target in data_loader:
data, target = Variable(data), Variable(target)
 if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
output = model(data)
 loss += F.cross_entropy(output, target, size_average=False ).item()
pred = output.data.max(1, keepdim=True)[1]
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
loss /= len(data_loader.dataset)
 accuracy = 100.* correct / len(data_loader.dataset)
 if not validate:
lr_reduction.step(loss)
 average_training_accuracy.append(accuracy)
 average_training_loss.append(loss)
 else:
average_validation_accuracy.append(accuracy)
 average_validation_loss.append(loss)
 '.format(loss, correct, len(data_loader.dataset), accuracy))
train.label.value_counts()<load_from_csv>
def prediciton(data_loader):
model.eval()
test_pred = torch.LongTensor()
for i, data in enumerate(data_loader):
data = Variable(data[0])
 if torch.cuda.is_available() :
data = data.cuda()
output = model(data)
 pred = output.cpu().data.max(1, keepdim=True)[1]
test_pred = torch.cat(( test_pred, pred), dim=0)
 return test_pred
test_pred = prediciton(test_loader )
submission = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 submission.head()<categorify>
out_df = pd.DataFrame(np.c_[np.arange(1, len(test_set)+1)[:,None], test_pred.numpy() ],
columns=['ImageId', 'Label'])
 out_df.head()
<import_modules><EOS> 
out_df.to_csv('submission.csv', index=False )