Kaggle competition: Kannada MNIST
df_suball[1].loc[df_suball[1]['ID'] == 'ID_000012eaf_any']<filter>
%matplotlib inline
df_suball[2].loc[df_suball[2]['ID'] == 'ID_000012eaf_any']<filter>
train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv")
 Dig_MNIST = pd.read_csv("/kaggle/input/Kannada-MNIST/Dig-MNIST.csv" )
df_suball[3].loc[df_suball[3]['ID'] == 'ID_000012eaf_any']<filter>
sample_sub = pd.read_csv("/kaggle/input/Kannada-MNIST/sample_submission.csv" )
df_suball[4].loc[df_suball[4]['ID'] == 'ID_000012eaf_any']<filter>
X=train.iloc[:,1:].values
Y=train.iloc[:,0].values
Y[:10]
df_suball[5].loc[df_suball[5]['ID'] == 'ID_000012eaf_any']<feature_engineering>
X_train, X_valid, Y_train, Y_valid = train_test_split(X, Y, test_size = 0.10, random_state=42 )
<feature_engineering>
train_datagen = ImageDataGenerator(rescale = 1./255.,
rotation_range = 10,
width_shift_range = 0.25,
height_shift_range = 0.25,
shear_range = 0.1,
zoom_range = 0.25,
horizontal_flip = False )
for i in range(1,ll):
df_suball[0]['Label'] += 0.15*df_suball[0]['Label'] + 0.15*df_suball[1]['Label'] + 0.20*df_suball[2]['Label'] + 0.15*df_suball[3]['Label']+ 0.20*df_suball[4]['Label'] + 0.15*df_suball[5]['Label']
df_suball[0]<save_to_csv>
valid_datagen = ImageDataGenerator(rescale=1./255 )
df_suball[0].to_csv('submission_average.csv', index=False )<import_modules>
def lr_decay(epoch):
return initial_learningrate * 0.99 ** epoch
import os
import time
import copy
import glob
import numpy as np
import pandas as pd
from tqdm import tqdm
from ipywidgets import IntProgress
import cv2
from albumentations import Compose, ShiftScaleRotate, Resize
from albumentations.pytorch import ToTensor
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
import torchvision
from torchvision import datasets, models, transforms
from torch.utils.data import Dataset, random_split
from matplotlib import pyplot as plt<feature_engineering>
es = EarlyStopping(monitor='val_loss', verbose=1, patience=10 )
data_path = '.. /input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/'
train_data_images = '.. /input/rsna-train-stage-1-images-png-224x/stage_1_train_png_224x/'
train = pd.read_csv(os.path.join(data_path,'stage_2_train.csv'))
train[['ID','Image','Diagnosis']]=train['ID'].str.split('_',expand= True)
 train = train[['Image','Diagnosis','Label']]
train.drop_duplicates(inplace= True)
 train = train.pivot(index = 'Image' , columns = 'Diagnosis', values = 'Label' ).reset_index()
train['Image'] = 'ID_' + train['Image']
png = glob.glob(os.path.join(train_data_images, '*.png'))
png = [os.path.basename(png)[:-4] for png in png]
png = np.array(png)
 train = train[train['Image'].isin(png)]
train = train[:300000]
train.to_csv('train.csv', index = False)
 print(train.shape )<save_to_csv>
from sklearn import metrics
data_path = '.. /input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/'
test_data_images = '.. /input/stage-2-png/'
test = pd.read_csv(os.path.join(data_path,'stage_2_sample_submission.csv'))
test[['ID','Image','Diagnosis']]=test['ID'].str.split('_',expand= True)
 test['Image'] = 'ID_' + test['Image']
test = test [['Image','Label']]
test.drop_duplicates(inplace= True)
 test.to_csv('test.csv', index = False)
 print(test.shape )<load_from_csv>
model = Sequential([
Conv2D(64,(3,3), padding='same', input_shape=(28, 28, 1)) ,
BatchNormalization(momentum=0.5, epsilon=1e-5, gamma_initializer="uniform"),
LeakyReLU(alpha=0.1),
Conv2D(64,(3,3), padding='same'),
BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform"),
LeakyReLU(alpha=0.1),
layers.MaxPooling2D(2, 2),
Dropout(0.2),
Conv2D(128,(3,3), padding='same'),
BatchNormalization(momentum=0.2, epsilon=1e-5, gamma_initializer="uniform"),
LeakyReLU(alpha=0.1),
Conv2D(128,(3,3), padding='same'),
BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform"),
LeakyReLU(alpha=0.1),
layers.MaxPooling2D(2,2),
Dropout(0.2),
Conv2D(256,(3,3), padding='same'),
BatchNormalization(momentum=0.2, epsilon=1e-5, gamma_initializer="uniform"),
LeakyReLU(alpha=0.1),
Conv2D(256,(3,3), padding='same'),
BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform"),
LeakyReLU(alpha=0.1),
layers.MaxPooling2D(2,2),
Dropout(0.2),
Flatten() ,
Dense(256),
LeakyReLU(alpha=0.1),
BatchNormalization() ,
Dense(10, activation='softmax')
 ])
 model.summary()
class RSNA(Dataset):
def __init__(self, csv_file, path, labels, transform=None):
self.path = path
self.data = pd.read_csv(csv_file)
 self.transform = transform
self.labels = labels
def __len__(self):
return len(self.data)
 def __getitem__(self, idx):
img_name = os.path.join(self.path, self.data.loc[idx, 'Image'] + '.png')
 img = cv2.imread(img_name)
 if self.transform:
augmented = self.transform(image=img)
 img = augmented['image']
if self.labels:
labels = torch.tensor(
 self.data.loc[idx, ['epidural', 'intraparenchymal', 'intraventricular', 'subarachnoid', 'subdural', 'any']])
 return {'image': img, 'labels': labels}
else:
return {'image': img}<load_from_csv>
initial_learningrate=2e-3
batch_size = 1024
epochs = 50
input_shape =(28, 28, 1 )
transform_train = Compose([ShiftScaleRotate() ,ToTensor() ])
 transform_test = Compose([ToTensor() ])
 train_dataset = RSNA(csv_file='train.csv', path=train_data_images, transform=transform_train, labels=True)
 test_dataset = RSNA(csv_file='test.csv', path=test_data_images, transform=transform_test, labels=False)
 batch_size = 64
data_train_generator = torch.utils.data.DataLoader(dataset=train_dataset,batch_size=batch_size,shuffle=True)
 data_test_generator = torch.utils.data.DataLoader(dataset=test_dataset,batch_size=batch_size,shuffle=False )<set_options>
model.compile(loss="categorical_crossentropy",
optimizer=RMSprop(lr=initial_learningrate),
metrics=['accuracy'] )
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 model0 = models.googlenet(pretrained = True)
 model = torch.nn.Sequential(model0, torch.nn.Linear(1000, 6)).to(device)
<choose_model_class>
history = model.fit_generator(
 train_datagen.flow(X,Y, batch_size=batch_size),
steps_per_epoch=100,
epochs=epochs,
callbacks=[LearningRateScheduler(lr_decay)
 ],
validation_data=valid_datagen.flow(X_valid,Y_valid),
validation_steps=50,
verbose=2 )
num_epochs = 3
optimizer = optim.Adam(model.parameters() , lr=4e-5)
 criterion = torch.nn.BCEWithLogitsLoss()<train_model>
preds_dig=model.predict_classes(x_dig/255)
 metrics.accuracy_score(preds_dig, y_dig )
def train_model(model,criterion,optimizer,num_epochs=1):
for epoch in range(1, num_epochs+1):
print('Epoch {}/{}'.format(epoch, num_epochs))
print('-' * 10)
 model.train()
tr_loss = 0
tk0 = tqdm(data_train_generator, desc="Iteration")
 for step, batch in enumerate(tk0):
inputs = batch["image"]
labels = batch["labels"]
inputs = inputs.to(device, dtype=torch.float)
 labels = labels.to(device, dtype=torch.float)
 outputs = model(inputs)
 loss = criterion(outputs, labels)
 loss.backward()
tr_loss += loss.item()
optimizer.step()
optimizer.zero_grad()
torch.save(model.state_dict() , f'resnext50_{epoch}.pth')
 epoch_loss = tr_loss / len(data_train_generator)
 print('Training Loss: {:.4f}'.format(epoch_loss))
return model 
<train_model>
predictions = model.predict_classes(x_test/255.)
model_trained = train_model(model,criterion,optimizer,num_epochs )<predict_on_test>
submission = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv' )
for param in model.parameters() :
param.requires_grad = False
model.eval()
test_pred = np.zeros(( len(test_dataset)* 6, 1))
for i, batch_ in enumerate(tqdm(data_test_generator)) :
batch_ = batch_["image"]
batch_ = batch_.to(device, dtype=torch.float)
 with torch.no_grad() :
pred = model(batch_)
 test_pred[(i * batch_size * 6):(( i + 1)* batch_size * 6)] = torch.sigmoid(
 pred ).detach().cpu().reshape(( len(batch_)* 6, 1))<save_to_csv>
submission['label'] = predictions
submission = pd.read_csv(os.path.join(data_path, 'stage_2_sample_submission.csv'))
submission = pd.concat([submission.drop(columns=['Label']), pd.DataFrame(test_pred)], axis=1)
 submission.columns = ['ID', 'Label']
submission.to_csv('submission-1.csv', index=False )<install_modules>
submission.to_csv("submission.csv",index=False )