Kaggle competition: Kannada MNIST
pred_list = []
for line in pred_t:
s = ' '.join(list([str(i)for i in np.nonzero(line>th_t)[0]]))
pred_list.append(s )<save_to_csv>
def get_images_and_labels(csv,label):
fileraw = pd.read_csv(csv)
 labels = fileraw[label].to_numpy()
data = fileraw.drop([label],axis=1 ).to_numpy(dtype=np.float32)
 data = np.true_divide(data,255.).reshape(( fileraw.shape[0],28,28))
data = np.expand_dims(data, axis=1)
 return data, labels
sample_df = pd.read_csv(SAMPLE)
 sample_list = list(sample_df.Id)
 pred_dic = dict(( key, value)for(key, value)
 in zip(learner.data.test_ds.fnames,pred_list))
pred_list_cor = [pred_dic[id] for id in sample_list]
df = pd.DataFrame({'Id':sample_list,'Predicted':pred_list_cor})
 df.to_csv('protein_classification.csv', header=True, index=False )<install_modules>
plt.title(f'Training Label: {train_labels[4]}')
 plt.imshow(train_data[4,0],cmap='gray');
!pip install fastai==0.7.0 --no-deps
!pip install torch==0.4.1 torchvision==0.2.1<import_modules>
np.random.seed(42)
 ran_10_pct_idx =(np.random.random_sample(train_labels.shape)) <.1
train_90_labels = train_labels[np.invert(ran_10_pct_idx)]
train_90_data = train_data[np.invert(ran_10_pct_idx)]
valid_10_labels = train_labels[ran_10_pct_idx]
valid_10_data = train_data[ran_10_pct_idx]
from fastai.conv_learner import *
from fastai.dataset import *
import pandas as pd
import numpy as np
import os
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
import scipy.optimize as opt
import matplotlib.pyplot as plt<define_variables>
train_ds = ArrayDataset(train_90_data,train_90_labels)
 valid_ds = ArrayDataset(valid_10_data,valid_10_labels)
 other_ds = ArrayDataset(other_data, other_labels)
 test_ds = ArrayDataset(test_data, test_labels )
PATH = './'
TRAIN = '.. /input/human-protein-atlas-image-classification/train/'
TEST = '.. /input/human-protein-atlas-image-classification/test/'
LABELS = '.. /input/human-protein-atlas-image-classification/train.csv'
SPLIT = '.. /input/protein-trainval-split/'
nw = 2<define_variables>
bs = 128
databunch = DataBunch.create(train_ds, valid_ds, test_ds=test_ds, bs=bs )
name_label_dict = {
0: 'Nucleoplasm',
1: 'Nuclear membrane',
2: 'Nucleoli',
3: 'Nucleoli fibrillar center',
4: 'Nuclear speckles',
5: 'Nuclear bodies',
6: 'Endoplasmic reticulum',
7: 'Golgi apparatus',
8: 'Peroxisomes',
9: 'Endosomes',
10: 'Lysosomes',
11: 'Intermediate filaments',
12: 'Actin filaments',
13: 'Focal adhesion sites',
14: 'Microtubules',
15: 'Microtubule ends',
16: 'Cytokinetic bridge',
17: 'Mitotic spindle',
18: 'Microtubule organizing center',
19: 'Centrosome',
20: 'Lipid droplets',
21: 'Plasma membrane',
22: 'Cell junctions',
23: 'Mitochondria',
24: 'Aggresome',
25: 'Cytosol',
26: 'Cytoplasmic bodies',
27: 'Rods & rings' }<load_from_csv>
leak = 0.15
best_architecture = nn.Sequential(
 conv_layer(1,32,stride=1,ks=3,leaky=leak),
conv_layer(32,32,stride=1,ks=3,leaky=leak),
conv_layer(32,32,stride=2,ks=5,leaky=leak),
nn.Dropout(0.4),
conv_layer(32,64,stride=1,ks=3,leaky=leak),
conv_layer(64,64,stride=1,ks=3,leaky=leak),
conv_layer(64,64,stride=2,ks=5,leaky=leak),
nn.Dropout(0.4),
Flatten() ,
nn.Linear(3136, 128),
relu(inplace=True),
nn.BatchNorm1d(128),
nn.Dropout(0.4),
nn.Linear(128,10)
 )
with open(os.path.join(SPLIT,'tr_names.txt'), 'r')as text_file:
tr_n = text_file.read().split(',')
 with open(os.path.join(SPLIT,'val_names.txt'), 'r')as text_file:
val_n = text_file.read().split(',')
 test_names = sorted({f[:36] for f in os.listdir(TEST)})
 print(len(tr_n),len(val_n))<load_from_csv>
learn = Learner(databunch, best_architecture, loss_func = nn.CrossEntropyLoss() , metrics=[accuracy] )
class Oversampling:
def __init__(self,path):
self.train_labels = pd.read_csv(path ).set_index('Id')
 self.train_labels['Target'] = [[int(i)for i in s.split() ]
for s in self.train_labels['Target']]
self.multi = [1,1,1,1,1,1,1,1,
4,4,4,1,1,1,1,4,
1,1,1,1,2,1,1,1,
1,1,1,4]
def get(self,image_id):
labels = self.train_labels.loc[image_id,'Target'] if image_id \
in self.train_labels.index else []
m = 1
for l in labels:
if m < self.multi[l]: m = self.multi[l]
return m
s = Oversampling(os.path.join(PATH,LABELS))
tr_n = [idx for idx in tr_n for _ in range(s.get(idx)) ]
print(len(tr_n),flush=True )<data_type_conversions>
learn.fit_one_cycle(20 )
def open_rgby(path,id):
colors = ['red','green','blue','yellow']
flags = cv2.IMREAD_GRAYSCALE
img = [cv2.imread(os.path.join(path, id+'_'+color+'.png'), flags ).astype(np.float32)/255
for color in colors]
return np.stack(img, axis=-1 )<prepare_x_and_y>
preds, ids = learn.get_preds(DatasetType.Test)
 y = torch.argmax(preds, dim=1 )
class pdFilesDataset(FilesDataset):
def __init__(self, fnames, path, transform):
self.labels = pd.read_csv(LABELS ).set_index('Id')
 self.labels['Target'] = [[int(i)for i in s.split() ] for s in self.labels['Target']]
super().__init__(fnames, transform, path)
 def get_x(self, i):
return open_rgby(self.path,self.fnames[i])
 def get_y(self, i):
if(self.path == TEST): return np.zeros(len(name_label_dict),dtype=np.int)
 else:
labels = self.labels.loc[self.fnames[i]]['Target']
return np.eye(len(name_label_dict),dtype=np.float)[labels].sum(axis=0)
 @property
def is_multi(self): return True
@property
def is_reg(self):return True
def get_c(self): return len(name_label_dict )<train_model>
submission = pd.DataFrame({ 'id': ids,'label': y })
 submission.to_csv(path_or_buf ="submission.csv", index=False )