Kaggle competition: Kannada MNIST
!pip install torch torchvision feather-format kornia pyarrow --upgrade > /dev/null
!pip install git+https://github.com/fastai/fastai_dev > /dev/null<set_options>
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import RMSprop,Adam
from keras.preprocessing.image import ImageDataGenerator
np.set_printoptions(linewidth=120)
 matplotlib.rcParams['image.cmap'] = 'bone'<define_variables>
from sklearn.model_selection import train_test_split
path = Path('.. /input/rsna-intracranial-hemorrhage-detection/')
 path_trn = path/'stage_1_train_images'
path_tst = path/'stage_1_test_images'
path_inp = Path('.. /input')
 path_xtra = path_inp/'rsna-hemorrhage-jpg'
path_meta = path_xtra/'meta'/'meta'
path_jpg = path_xtra/'train_jpg'/'train_jpg'<load_from_disk>
from sklearn.model_selection import train_test_split
df_comb = pd.read_feather(path_meta/'comb.fth' ).set_index('SOPInstanceUID')
 df_tst = pd.read_feather(path_meta/'df_tst.fth' ).set_index('SOPInstanceUID')
 df_samp = pd.read_feather(path_meta/'wgt_sample.fth' ).set_index('SOPInstanceUID')
 bins =(path_meta/'bins.pkl' ).load()<count_unique_values>
path_in = '.. /input/Kannada-MNIST/'
set_seed(42)
 patients = df_comb.PatientID.unique()
pat_mask = np.random.random(len(patients)) <0.8
pat_trn = patients[pat_mask]<split>
train_data = pd.read_csv(path_in+'train.csv')
 val_data = pd.read_csv(path_in+'Dig-MNIST.csv')
 test_data = pd.read_csv(path_in+'test.csv')
 samp_subm = pd.read_csv(path_in+'sample_submission.csv' )
def split_data(df):
idx = L.range(df)
 mask = df.PatientID.isin(pat_trn)
 return idx[mask],idx[~mask]
splits = split_data(df_samp )<filter>
X_train = train_data.copy()
y_train = train_data['label']
del X_train['label']
X_val = val_data.copy()
y_val = val_data['label']
del X_val['label']
X_test = test_data.copy()
del X_test['id']
y_train = to_categorical(y_train, num_classes = 10)
 y_val = to_categorical(y_val, num_classes = 10 )
df_trn = df_samp.iloc[splits[0]]
p1 = L.range(df_samp)[df_samp.PatientID==df_trn.PatientID[0]]
assert len(p1)== len(set(p1)& set(splits[0]))<categorify>
X_train = X_train.astype('float32')/255
X_val = X_val.astype('float32')/255
X_test = X_test.astype('float32')/255
def filename(o): return os.path.splitext(os.path.basename(o)) [0]
fns = L(list(df_samp.fname)).map(filename)
 fn = fns[0]
fn<data_type_conversions>
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state=2020 )
htypes = ['any','epidural','intraparenchymal','intraventricular','subarachnoid','subdural']
def fn2label(fn): return df_comb.loc[fn][htypes].values.astype(np.float32)
 fn2label(fn )<find_best_params>
model = Sequential()
model.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu', input_shape=(28,28,1)))
 model.add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=2))
model.add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
model.add(Conv2D(filters=256, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=2, padding='same'))
model.add(Conv2D(filters=256, kernel_size=3, padding='same', activation='relu'))
model.add(Conv2D(filters=512, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=2, padding='same'))
model.add(Dropout(0.5))
model.add(Flatten())
 model.add(Dense(512, activation='relu'))
model.add(Dense(10, activation='softmax'))
bs,nw = 128,4<categorify>
optimizer = RMSprop(lr=0.001 )
tfms = [[fn2image], [fn2label,EncodedMultiCategorize(htypes)]]
dsrc = DataSource(fns, tfms, splits=splits)
 nrm = Normalize(tensor([0.6]),tensor([0.25]))
aug = aug_transforms(p_lighting=0.)
batch_tfms = [IntToFloatTensor() , nrm, Cuda() , *aug]<prepare_x_and_y>
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['acc'] )
def get_data(bs, sz):
return dsrc.databunch(bs=bs, num_workers=nw, after_item=[ToTensor],
after_batch=batch_tfms+[AffineCoordTfm(size=sz)] )<categorify>
epochs = 50
batch_size = 512
dbch = get_data(128, 96)
 xb,yb = to_cpu(dbch.one_batch())
 dbch.show_batch(max_n=4, figsize=(9,6))
xb.mean() ,xb.std() ,xb.shape,len(dbch.train_dl )<compute_test_metric>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.15,
height_shift_range=0.15,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )
def accuracy_any(inp, targ, thresh=0.5, sigmoid=True):
inp,targ = flatten_check(inp[:,0],targ[:,0])
 if sigmoid: inp = inp.sigmoid()
return(( inp>thresh)==targ.bool() ).float().mean()<choose_model_class>
history = model.fit(datagen.flow(X_train, y_train, batch_size=batch_size),
epochs=epochs,
validation_data=(X_val,y_val),
steps_per_epoch=X_train.shape[0] // batch_size )
def get_loss(scale=1.0):
loss_weights = tensor(2.0, 1, 1, 1, 1, 1 ).cuda() *scale
return BaseLoss(nn.BCEWithLogitsLoss, pos_weight=loss_weights, floatify=True, flatten=False,
is_2d=False, activation=torch.sigmoid )<find_best_params>
y_test = model.predict(X_test )
loss_func = get_loss(0.14*2)
 opt_func = partial(Adam, wd=0.01, eps=1e-3)
 metrics=[accuracy_multi,accuracy_any]<choose_model_class>
y_test_classes = np.argmax(y_test, axis = 1 )
def get_learner() :
dbch = get_data(128,128)
 learn = cnn_learner(dbch, xresnet50, loss_func=loss_func, opt_func=opt_func, metrics=metrics)
 return learn.to_fp16()<choose_model_class>
output = pd.DataFrame({'id': samp_subm['id'],
'label': y_test_classes})
 output.to_csv('submission.csv', index=False )
learn = get_learner()<train_model>
del model