Kaggle competition: Human Protein Atlas - Single Cell Classification
batch_size = 1024
epochs = 50<train_model>
!pip install /kaggle/input/iterative-stratification/iterative-stratification-master/
history = model.fit_generator(data_gen.flow(x_train, y_train, batch_size=batch_size),
validation_data=data_gen.flow(x_validation, y_validation, batch_size=batch_size),
steps_per_epoch=len(x_train)//batch_size,
epochs=epochs,
callbacks=[learning_rate_reduction, es] )<load_from_csv>
raw_test = pd.read_csv('.. /input/Kannada-MNIST/test.csv' )<save_to_csv>
package_path = '.. /input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master'
sys.path.append(package_path)
sample_sub=pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 raw_test_id=raw_test.id
raw_test=raw_test.drop("id",axis="columns")
 raw_test=raw_test / 255
test=raw_test.values.reshape(-1,28,28,1)
 sub=model.predict(test)
 sub=np.argmax(sub,axis=1)
 sample_sub['label']=sub
sample_sub.to_csv('submission.csv',index=False )<import_modules>
import pandas as pd
import numpy as np
from fastai.vision.all import *
import pickle
import os
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from keras.utils.np_utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense,Conv2D,Flatten,MaxPool2D,Dropout,BatchNormalization
from keras.optimizers import RMSprop,Adam
from keras.callbacks import ReduceLROnPlateau 
<load_from_csv>
root = ".. /input/Kannada-MNIST"
train = pd.read_csv(os.path.join(root,'train.csv'))
test = pd.read_csv(os.path.join(root,'test.csv'))
final = pd.read_csv(os.path.join(root,'sample_submission.csv'))<drop_column>
path = Path('.. /input/hpa-cell-tiles-sample-balanced-dataset' )
test = test.drop('id',axis=1 )<prepare_x_and_y>
df = pd.read_csv(path/'cell_df.csv' )
X_train=train.drop('label',axis=1)
 Y_train=train.label<feature_engineering>
labels = [str(i)for i in range(19)]
for x in labels: df[x] = df['image_labels'].apply(lambda r: int(x in r.split('|')) )
X_train=X_train/255
test=test/255<categorify>
unique_counts = {}
for lbl in labels:
unique_counts[lbl] = len(dfs[dfs.image_labels == lbl])
 full_counts = {}
for lbl in labels:
count = 0
for row_label in dfs['image_labels']:
if lbl in row_label.split('|'): count += 1
full_counts[lbl] = count
counts = list(zip(full_counts.keys() , full_counts.values() , unique_counts.values()))
counts = np.array(sorted(counts, key=lambda x:-x[1]))
counts = pd.DataFrame(counts, columns=['label', 'full_count', 'unique_count'])
 counts.set_index('label' ).T
Y_train=to_categorical(Y_train )<split>
nfold = 5
seed = 42
y = dfs[labels].values
X = dfs[['image_id', 'cell_id']].values
dfs['fold'] = np.nan
mskf = MultilabelStratifiedKFold(n_splits=nfold, random_state=seed)
 for i,(_, test_index)in enumerate(mskf.split(X, y)) :
dfs.iloc[test_index, -1] = i
dfs['fold'] = dfs['fold'].astype('int' )
X_train,X_test,y_train,y_test=train_test_split(X_train,Y_train,random_state=42,test_size=0.2 )<define_variables>
dfs['is_valid'] = False
dfs['is_valid'][dfs['fold'] == 0] = True
data_tweaker = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 data_tweaker.fit(X_train )<choose_model_class>
dfs.is_valid.value_counts()
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(BatchNormalization(momentum=.15))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(BatchNormalization(momentum=0.15))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(BatchNormalization(momentum=.15))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.4))
model.add(Dense(10, activation = "softmax"))<choose_model_class>
def get_y(r): return r['image_labels'].split('|')
 get_y(dfs.loc[12] )
optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)
 model.compile(optimizer=optimizer,loss=['categorical_crossentropy'],metrics=['accuracy'] )<choose_model_class>
sample_stats =([0.07237246, 0.04476176, 0.07661699], [0.17179589, 0.10284516, 0.14199627] )
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<define_variables>
dls.show_batch(nrows=3, ncols=3 )
epochs=30
batch_size=64<train_model>
def get_learner(lr=1e-3):
opt_func = partial(Adam, lr=lr, wd=0.01, eps=1e-8)
 model = EfficientNet.from_pretrained("efficientnet-b5", advprop=True)
 model._fc = nn.Linear(2048, dls.c)
 learn = Learner(
 dls, model, opt_func=opt_func,
metrics=[accuracy_multi, PrecisionMulti() ]
).to_fp16()
return learn
train_info = model.fit_generator(data_tweaker.flow(X_train,y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_test,y_test),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )<load_from_csv>
learn=get_learner()
test_data = pd.read_csv(os.path.join(root,'test.csv'))
test_data = test_data.drop('id',axis=1)
 test_data = test_data/255
test_data = test_data.values.reshape(-1,28,28,1 )<predict_on_test>
learn.lr_find()
pred = model.predict(test_data)
<save_to_csv>
lr=3e-2
final['label'] = np.argmax(pred, axis=1)
 final['id'] = np.arange(len(final['label']))
final.to_csv('.. /working/submission.csv',index=False )<set_options>
learn.fine_tune(6,base_lr=lr )
%matplotlib inline 
<choose_model_class>
from sklearn.metrics import multilabel_confusion_matrix as cm
lr_schedule = LearningRateScheduler(lambda epoch: 1e-6 * 2**(epoch/2))<load_from_csv>
val_targ = dfs[labels][dfs.is_valid == True].values
train_path = Path.cwd() /'.. '/'input'/'Kannada-MNIST'/'train.csv'
all_data = pd.read_csv(train_path )<prepare_x_and_y>
val_preds_all = learn.get_preds(dl=learn.dls.valid )
y = all_data.label
x = all_data.iloc[:,1:].values
x = x.reshape(x.shape[0], 28, 28, 1 )<choose_model_class>
val_preds = val_preds_all[0].numpy()
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)
 train_generator = train_datagen.flow(x, y, batch_size=1024 )<train_model>
val_preds = val_preds > 0.5
class LR_Updater(Callback):

 def __init__(self, epoch_iterations):

 self.epoch_iterations = epoch_iterations
self.trn_iterations = 0.
 self.history = {}
def on_train_begin(self, logs={}):
self.trn_iterations = 0.
 logs = logs or {}
def on_batch_end(self, batch, logs=None):
logs = logs or {}
self.trn_iterations += 1
K.set_value(self.model.optimizer.lr, self.setRate())
 self.history.setdefault('lr', [] ).append(K.get_value(self.model.optimizer.lr))
self.history.setdefault('iterations', [] ).append(self.trn_iterations)
 for k, v in logs.items() :
self.history.setdefault(k, [] ).append(v)
 class LR_Cycle(LR_Updater):

 def __init__(self, iterations, cycle_mult = 1):

 self.min_lr = 0
self.cycle_mult = cycle_mult
self.cycle_iterations = 0.
 super().__init__(iterations)
 def setRate(self):
self.cycle_iterations += 1
if self.cycle_iterations == self.epoch_iterations:
self.cycle_iterations = 0.
 self.epoch_iterations *= self.cycle_mult
decay_phase = np.pi*self.cycle_iterations/self.epoch_iterations
decay =(np.cos(decay_phase)+ 1.) / 2.
 return self.max_lr * decay
def on_train_begin(self, logs={}):
super().on_train_begin(logs={})
 self.cycle_iterations = 0.
 self.max_lr = K.get_value(self.model.optimizer.lr )<choose_model_class>
full_preds = val_preds_all[0].numpy()
def build_model(optimizer=Adam()):
model = Sequential()
model.add(Conv2D(64, kernel_size=3, activation='relu', input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size=3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size=5, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(128, kernel_size=3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(128, kernel_size=3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(128, kernel_size=5, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(256, kernel_size=3, activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(256))
model.add(BatchNormalization())
 model.add(Dense(128))
model.add(BatchNormalization())
 model.add(Dense(10, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
 return model<choose_model_class>
vis_arr = cm(val_targ, val_preds )
model = build_model(Adam(learning_rate=1e-3))<choose_model_class>
val = dfs[dfs.is_valid==True]
len(val[val['16'] == 1] )
cyclic_lr = LR_Cycle(59, cycle_mult=2 )<train_model>
average_precision = average_precision_score(val_targ, val_preds)
 average_precision
history = model.fit_generator(train_generator,
epochs=31,
callbacks=[cyclic_lr] )<load_from_csv>
precision = dict()
recall = dict()
average_precision = dict()
for i in range(19):
precision[i], recall[i], _ = precision_recall_curve(val_targ[:, i], val_preds[:, i])
 average_precision[i] = average_precision_score(val_targ[:, i], val_preds[:, i])
 precision["micro"], recall["micro"], _ = precision_recall_curve(val_targ.ravel() , val_preds.ravel())
 average_precision["micro"] = average_precision_score(val_targ, val_preds, average="micro")
 print('Average precision score, micro-averaged over all classes: {0:0.2f}'.format(average_precision["micro"]))
test_path = Path.cwd() /'.. '/'input'/'Kannada-MNIST'/'test.csv'
all_data_test = pd.read_csv(test_path )<prepare_x_and_y>
path = Path('.. /input/hpa-cell-tiles-test-with-enc-dataset' )
x_test = all_data_test.iloc[:,1:].values
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1 )<predict_on_test>
df = pd.read_csv(path/'cell_df.csv' )
predictions = model.predict_classes(x_test/255.)<save_to_csv>
df.to_csv('cell_df.csv', index=False )
output = pd.DataFrame({'id': all_data_test.id,
'label': predictions})
 output.to_csv("submission.csv",index=False )<define_variables>
test_dl = learn.dls.test_dl(df )
print(os.listdir(".. /input"))
directory_train = ".. /input/Kannada-MNIST/train.csv"
directory_test = ".. /input/Kannada-MNIST/test.csv"<load_from_csv>
test_dl.show_batch()
test_data = pd.read_csv(directory_test)
 test_data.head(10 )<load_from_csv>
preds, _ = learn.get_preds(dl=test_dl )
train_data = pd.read_csv(directory_train)
 train_data.head(10 )<prepare_x_and_y>
with open('preds.pickle', 'wb')as handle:
pickle.dump(preds, handle )
y_train = train_data["label"]
x_train = train_data.drop(['label'],axis=1)
 y_train = to_categorical(y_train,num_classes=10)
 x_test = test_data.drop(['id'], axis=1 )<categorify>
tta, _ = learn.tta(dl=test_dl )
x_train /= 255.0
x_test /= 255.0
x_train = x_train.values.reshape(x_train.shape[0],28, 28,1)
 x_test = x_test.values.reshape(x_test.shape[0],28, 28,1)
<split>
with open('tta.pickle', 'wb')as handle:
pickle.dump(tta, handle )
def separating_data() :
return train_test_split(x_train,
y_train,
test_size = 0.05,
random_state = 42 
)
 x_train, x_validation, y_train, y_validation = train_test_split(x_train,
y_train,
test_size = 0.05,
random_state = 42 )<choose_model_class>
cls_prds = torch.argmax(preds, dim=-1)
 len(cls_prds), cls_prds
max_epochs = 2
model = Sequential()
activations = ['relu', 'softmax']
model.add(Conv2D(filters = 32,
kernel_size =(5,5),
padding = 'Same',
activation = activations[0],
input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32,
kernel_size =(5,5),
padding = 'Same',
activation = activations[0]))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64,
kernel_size =(3,3),
padding = 'Same',
activation = activations[0]))
model.add(Conv2D(filters = 64,
kernel_size =(3,3),
padding = 'Same',
activation = activations[0]))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = activations[0]))
model.add(Dropout(0.5))
model.add(Dense(10, activation = activations[1]))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=["accuracy"] )<choose_model_class>
sample_submission = pd.read_csv('.. /input/hpa-single-cell-image-classification/sample_submission.csv')
 sample_submission.head()
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )<train_model>
df['cls'] = cls_prds
df['pred'] = df[['cls', 'enc']].apply(lambda r: str(r[0])+ ' 1 ' + r[1], axis=1)
 df.head()
generator = ImageDataGenerator()
batches = generator.flow(x_train,
y_train,
batch_size=32)
 batches_value = generator.flow(x_validation,
y_validation,
batch_size=32)
 history = model.fit_generator(generator=batches,
steps_per_epoch=batches.n,
epochs=max_epochs,
validation_data=batches_value,
validation_steps=batches_value.n )<save_to_csv>
subm = df.groupby(['image_id'])['pred'].apply(lambda x: ' '.join(x)).reset_index()
subm.head()
my_predictions = model.predict_classes(x_test)
 submission = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 submission['label'] = my_predictions
submission.to_csv("submission.csv",index=False )<set_options>
sub = pd.merge(
 sample_submission,
subm,
how="left",
left_on='ID',
right_on='image_id',
)
warnings.filterwarnings('ignore')
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))<load_from_csv>
def isNaN(num):
return num != num
raw_train = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 raw_test = pd.read_csv('.. /input/Kannada-MNIST/test.csv' )<define_search_space>
for i, row in sub.iterrows() :
if isNaN(row['pred']): continue
sub.PredictionString.loc[i] = row['pred']
raw_train.iloc[[0,-1],[1,-1]]<prepare_x_and_y>
sub.to_csv('submission_1.csv', index=False )
x = raw_train.iloc[:, 1:].values.astype('float32')/ 255
y = raw_train.iloc[:, 0]<split>
cell_df = pd.read_csv('cell_df.csv')
 cell_df.head()
cell_df['cls'] = ''
x_train, x_val, y_train, y_val = train_test_split(x, y, test_size = 0.2, random_state=42 )<categorify>
threshold = 0.0
for i in range(preds.shape[0]):
p = torch.nonzero(preds[i] > threshold ).squeeze().numpy().tolist()
if type(p)!= list: p = [p]
if len(p)== 0: cls = [(preds[i].argmax().item() , preds[i].max().item())]
else: cls = [(x, preds[i][x].item())for x in p]
cell_df['cls'].loc[i] = cls
x_train = x_train.reshape(-1, 28, 28,1)
 x_val = x_val.reshape(-1, 28, 28,1)
 y_train = to_categorical(y_train)
 y_val = to_categorical(y_val )<choose_model_class>
def combine(r):
cls = r[0]
enc = r[1]
classes = [str(c[0])+ ' ' + str(c[1])+ ' ' + enc for c in cls]
return ' '.join(classes)
 combine(cell_df[['cls', 'enc']].loc[24] )
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64,(3,3), padding='same', input_shape=(28, 28, 1)) ,
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(64,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(64,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(128,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(128,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(128,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(256,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(256,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(256),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(10, activation='softmax')
 ] )<choose_model_class>
cell_df['pred'] = cell_df[['cls', 'enc']].apply(combine, axis=1)
 cell_df.head()
optimizer = RMSprop(learning_rate=0.002,
rho=0.9,
momentum=0.1,
epsilon=1e-07,
centered=True,
name='RMSprop')
 model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'] )<define_variables>
subm = cell_df.groupby(['image_id'])['pred'].apply(lambda x: ' '.join(x)).reset_index()
subm.head()
batch_size = 1024
num_classes = 10
epochs = 40 
<train_model>
sample_submission = pd.read_csv('.. /input/hpa-single-cell-image-classification/sample_submission.csv')
 sample_submission.head()
datagen_try = ImageDataGenerator(rotation_range=15,
width_shift_range = 0.15,
height_shift_range = 0.15,
shear_range = 0.15,
zoom_range = 0.4,)
 datagen_try.fit(x_train)
 for x_batch, y_batch in datagen_try.flow(x_train, y_train, batch_size=9):
	
	for i in range(0, 9):
		plt.subplot(330 + 1 + i)
 		plt.imshow(x_batch[i].reshape(28, 28), cmap=plt.get_cmap('gray'))
	
	plt.show()
	break<define_search_model>
sub = pd.merge(
 sample_submission,
subm,
how="left",
left_on='ID',
right_on='image_id', 
)
 sub.head()
datagen_train = ImageDataGenerator(rotation_range = 10,
width_shift_range = 0.25,
height_shift_range = 0.25,
shear_range = 0.1,
zoom_range = 0.4,
horizontal_flip = False)
 datagen_val = ImageDataGenerator()
step_train = x_train.shape[0] // batch_size
step_val = x_val.shape[0] // batch_size
learning_rate_reduction = tf.keras.callbacks.ReduceLROnPlateau(
 monitor='loss',
factor=0.25,
patience=2,
verbose=1,
mode="auto",
min_delta=0.0001,
cooldown=0,
min_lr=0.00001 
)
 es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=300, restore_best_weights=True )<train_model>
def isNaN(num):
return num != num
for i, row in sub.iterrows() :
if isNaN(row['pred']): continue
sub.PredictionString.loc[i] = row['pred']
history = model.fit_generator(datagen_train.flow(x_train, y_train, batch_size=batch_size),
steps_per_epoch=len(x_train)//batch_size,
epochs=epochs,
validation_data=(x_val, y_val),
validation_steps=50,
callbacks=[learning_rate_reduction, es],
verbose=2 )<train_model>
sub.to_csv('submission.csv', index=False )
<compute_test_metric><EOS> 
sub.to_csv('submission.csv', index=False )