Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<count_values>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Conv2D,MaxPool2D,Dropout,Dense,Flatten,Activation,BatchNormalization
from keras.models import Sequential
from keras.optimizers import Adam
from keras.losses import categorical_crossentropy
from sklearn.metrics import classification_report
from IPython.display import FileLink
new_train = train.loc[selected_patients].copy()
new_train["any"].value_counts()<save_to_csv>
tensorflow_backend._get_available_gpus()
png = glob.glob(os.path.join(dir_train_img, '*.png'))
png = [os.path.basename(png)[:-4] for png in png]
png = np.array(png)
 train = train[train['Image'].isin(png)]
train.to_csv('train.csv', index=False )<save_to_csv>
train=pd.read_csv(r'.. /input/Kannada-MNIST/train.csv')
 test=pd.read_csv(r'.. /input/Kannada-MNIST/test.csv')
 val=pd.read_csv(r'.. /input/Kannada-MNIST/Dig-MNIST.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 )<load_from_csv>
data=train.iloc[:,1:].values
data=data.reshape(-1,28,28,1)/255
print('Total training data shape:',data.shape )
transform_train = Compose([CenterCrop(200, 200),
HorizontalFlip() ,
RandomBrightnessContrast() ,
ShiftScaleRotate() ,
ToTensor()
])
 transform_test= Compose([CenterCrop(200, 200),
ToTensor()
])
 train_dataset = IntracranialDataset(
 csv_file='train.csv', path=dir_train_img, transform=transform_train, labels=True)
 test_dataset = IntracranialDataset(
 csv_file='test.csv', path=dir_test_img, transform=transform_test, labels=False)
 data_loader_train = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=False, num_workers=4)
 data_loader_test = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=4 )<choose_model_class>
labels=pd.get_dummies(train.iloc[:,0] ).values
device = torch.device("cuda:0")
 model = torch.hub.load('facebookresearch/WSL-Images', 'resnext101_32x8d_wsl')
 model.fc = torch.nn.Linear(2048, n_classes)
 model.to(device)
 criterion = torch.nn.BCEWithLogitsLoss()
plist = [{'params': model.parameters() , 'lr': 2e-5}]
optimizer = optim.Adam(plist, lr=2e-5)
 model, optimizer = amp.initialize(model, optimizer, opt_level="O1")
<train_model>
x_val=val.iloc[:,1:].values
x_val=x_val.reshape(-1,28,28,1)/255
for epoch in range(n_epochs):
print('Epoch {}/{}'.format(epoch, n_epochs - 1))
print('-' * 10)
 model.train()
tr_loss = 0
tk0 = tqdm(data_loader_train, 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)
 with amp.scale_loss(loss, optimizer)as scaled_loss:
scaled_loss.backward()
tr_loss += loss.item()
optimizer.step()
optimizer.zero_grad()
if epoch == 1 and step > 6000:
epoch_loss = tr_loss / 6000
print('Training Loss: {:.4f}'.format(epoch_loss))
break
epoch_loss = tr_loss / len(data_loader_train)
 print('Training Loss: {:.4f}'.format(epoch_loss))<predict_on_test>
y_val=pd.get_dummies(val.iloc[:,0] ).values
for param in model.parameters() :
param.requires_grad = False
model.eval()
test_pred = np.zeros(( len(test_dataset)* n_classes, 1))
for i, x_batch in enumerate(tqdm(data_loader_test)) :
x_batch = x_batch["image"]
x_batch = x_batch.to(device, dtype=torch.float)
 with torch.no_grad() :
pred = model(x_batch)
 test_pred[(i * batch_size * n_classes):(( i + 1)* batch_size * n_classes)] = torch.sigmoid(
 pred ).detach().cpu().reshape(( len(x_batch)* n_classes, 1))<save_to_csv>
aug_data=ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1 )
submission = pd.read_csv(os.path.join(dir_csv, 'stage_1_sample_submission.csv'))
submission = pd.concat([submission.drop(columns=['Label']), pd.DataFrame(test_pred)], axis=1)
 submission.columns = ['ID', 'Label']
submission.to_csv('submission.csv', index=False)
 submission.head()<load_from_zip>
generator=aug_data.flow(data,labels,batch_size=64 )
!rm -rf /kaggle/working/apex
!rm test.csv
!rm train.csv<install_modules>
def create_model() :
model=Sequential()
model.add(Conv2D(64,(3,3),input_shape=(28,28,1),activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,(3,3),activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,(5,5),strides=(2,2),padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128,(3,3),activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(128,(3,3),activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(128,(5,5),strides=(2,2),padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(256,(3,3),activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dense(1024,activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(10,activation='softmax'))
model.compile(optimizer='adam',loss=categorical_crossentropy,metrics=['accuracy'])
 return model
pip install git+https://github.com/fastai/fastai_dev<define_variables>
model=create_model()
dir_csv = '.. /input/rsna-intracranial-hemorrhage-detection'
dir_train_img = '.. /input/rsna-train-stage-1-images-png-224x/stage_1_train_png_224x'
dir_test_img = '.. /input/rsna-test-stage-1-images-png-224x/stage_1_test_png_224x'<import_modules>
history=model.fit_generator(generator,epochs=50,validation_data=(x_val,y_val),steps_per_epoch=data.shape[0]//64,shuffle=True )
from fastai2.torch_basics import *
from fastai2.test import *
from fastai2.layers import *
from fastai2.data.all import *
from fastai2.optimizer import *
from fastai2.learner import *
from fastai2.metrics import *
from fastai2.vision.all import *
from fastai2.vision.learner import *
from fastai2.vision.models import *
from fastai2.callback.all import *<define_variables>
data=np.concatenate(( data,x_val))
labels=np.concatenate(( labels,y_val))
items = get_image_files(dir_train_img)
 items = [i for i in items if '(copy)' not in i.name]<save_to_csv>
generator=aug_data.flow(data,labels,batch_size=64 )
!mkdir -p data
df_train = pd.read_csv(f'{dir_csv}/stage_1_train.csv')
 df_train['fn'] = df_train.ID.apply(lambda x: '_'.join(x.split('_')[:2])+ '.png')
 df_train.columns = ['ID', 'probability', 'fn']
df_train['label'] = df_train.ID.apply(lambda x: x.split('_')[-1])
 df_train.drop_duplicates('ID', inplace=True)
 pivot = df_train.pivot(index='fn', columns='label', values='probability')
 pivot.reset_index(inplace=True)
 pivot.to_csv('data/train_pivot.csv', index=False)
 d = defaultdict(list)
 for fn in df_train.fn.unique() : d[fn]
for tup in df_train.itertuples() :
if tup.probability: d[tup.fn].append(tup.label)
 ks, vs = [], []
for k, v in d.items() :
ks.append(k), vs.append(' '.join(v))
pd.DataFrame(data={'fn': ks, 'labels': vs} ).to_csv('data/train_labels_as_strings.csv', index=False )<load_from_csv>
model=create_model()
class Labeller() :

 def __init__(self):
self.df = pd.read_csv('data/train_labels_as_strings.csv')
 self.df.set_index('fn', inplace=True)
 def __call__(self, path):
fn = path.name
labels_txt = self.df.loc[fn].labels
if isinstance(labels_txt, float)or labels_txt == ' ': return []
return labels_txt.split(' ' )<choose_model_class>
history=model.fit_generator(generator,epochs=50,validation_data=(x_val,y_val),steps_per_epoch=data.shape[0]//64,shuffle=True )
labeler = Labeller()<load_from_csv>
print(classification_report(np.argmax(y_val,1),np.argmax(model.predict(x_val),1)) )
classes = L(pd.read_csv('data/train_pivot.csv' ).columns.tolist() [1:])
 classes<compute_train_metric>
test_id=test['id'].values
mcat = MultiCategorize(vocab=classes)
 mcat.o2i = classes.val2idx()<split>
test.drop('id',axis=1,inplace=True )
tfms = [PILImage.create, [Labeller() , mcat, OneHotEncode(mcat.vocab)]]
ds_img_tfms = [ToTensor() ]
dsrc = DataSource(items, tfms, splits=RandomSplitter()(items))<define_variables>
x_test=test.values.reshape(-1,28,28,1)/255
test_paths = get_image_files(dir_test_img)
 test_tfms = [PILImage.create, [lambda x: np.array([0,0,0,0,0,0])]]
dsrc_test = DataSource([test_paths[0]] + test_paths, test_tfms, splits=[[0], L(range(len(test_paths)) ).map(lambda x: x + 1)] )<load_pretrained>
pred=np.argmax(model.predict(x=x_test),1 )
ds_img_tfms = [ToTensor() ]
dl_tfms = [Cuda() , ByteToFloatTensor() , Normalize(*dataset_stats)]
dbch = dsrc.databunch(after_item=ds_img_tfms, after_batch=dl_tfms, bs=128, num_workers=4)
 dbch_test = dsrc_test.databunch(after_item=ds_img_tfms, after_batch=dl_tfms, bs=128, num_workers=4 )<define_variables>
def submit(test_id,pred):
df=np.concatenate(( test_id.reshape(-1,1),pred.reshape(-1,1)) ,axis=1)
 df=pd.DataFrame(df,columns=['id','label'])
 df['id']=df['id'].astype('int32')
 df.to_csv('Submission.csv',index=False )
<choose_model_class><EOS> 
submit(test_id,pred )