Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<choose_model_class>
%matplotlib inline
dtc = DecisionTreeClassifier()<categorify>
train_df = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv' )
xx = X_train.todense()<train_model>
IMG_SIZE = 224
dtc.fit(X_train, y_train )<choose_model_class>
def load_img(path):
image = cv2.imread(path)
 return image
<compute_test_metric>
plt.imshow(preprocess_img('.. /input/aptos2019-blindness-detection/train_images/'+train_df.loc[1]['id_code']+'.png'))
<categorify>
N = train_df.shape[0]
train = np.empty(( N, IMG_SIZE, IMG_SIZE, 3), dtype=np.float32)
 for i, image_id in enumerate(tqdm(train_df['id_code'])) :
train[i,:,:,:] = preprocess_img('.. /input/aptos2019-blindness-detection/train_images/'+image_id+'.png' )
xx_test = X_test.todense()<predict_on_test>
y = np.zeros(( train_df.shape[0],5), dtype=np.int8 )
y_pred = dtc.predict(X_test )<save_to_csv>
for i, diagnosis in enumerate(train_df['diagnosis']):
for k in range(diagnosis+1):
y[i,k] = 1
y_pred_ = lb.inverse_transform(y_pred)
 submission = pd.DataFrame({"id": df2["id"], "cuisine": y_pred_})
 submission.to_csv('submission.csv', index = False )<import_modules>
X_train, X_val, y_train, y_val = train_test_split(train, y, test_size=0.15, random_state=42 )
import pandas as pd
from keras.preprocessing.text import Tokenizer
from keras.models import Sequential
from keras.layers import Dense, Embedding, Activation,Dropout
from keras.layers import LSTM
from keras.preprocessing import sequence
import keras as keras
import numpy as np<load_from_disk>
BATCH_SIZE = 32
data=pd.read_json('.. /input/train.json' )<define_variables>
class TestSequence(tf.keras.utils.Sequence):
def __init__(self, x_set, batch_size, aug=True):
self.x = x_set
self.batch_size = batch_size
self.aug = aug
def __len__(self):
return int(np.ceil(len(self.x)/ self.batch_size))
def __getitem__(self, idx):
batch_x = self.x[idx * self.batch_size:(idx + 1)* self.batch_size]
images = np.empty(( batch_x.shape[0], IMG_SIZE, IMG_SIZE, 3), dtype=np.float32)
 for i, image_id in enumerate(batch_x):
preprocessed_img = preprocess_img('.. /input/aptos2019-blindness-detection/test_images/'+image_id+'.png')
 if self.aug:
h_flip = bool(random.getrandbits(1))
v_flip = bool(random.getrandbits(1))
rotate = random.randint(0,20)
 transform_parameters={'theta':rotate, 'flip_horizontal': h_flip, 'flip_vertical': v_flip}
preprocessed_img = tf.keras.preprocessing.image.ImageDataGenerator().apply_transform(preprocessed_img, transform_parameters)
 images[i,:,:,:] = preprocessed_img
return images
cat_dict=[]
data_dict=[]<drop_column>
train_data_generator = tf.keras.preprocessing.image.ImageDataGenerator(
 fill_mode='constant',
cval=0.,
horizontal_flip=True,
vertical_flip=True,
rotation_range=15,
)
data=data.drop(['id'],axis=1 )<define_variables>
train_gen = train_data_generator.flow(X_train, y_train, batch_size=BATCH_SIZE)
 val_gen = val_data_generator.flow(X_val, y_val, batch_size=BATCH_SIZE, shuffle=False )
top_len=0
for x in range(0,len(data)) :
l=len(data['ingredients'][x])
 if(l>top_len):
top_len=l<feature_engineering>
class Metrics(tf.keras.callbacks.Callback):
def __init__(self, val_data, batch_size = 20):
super().__init__()
self.validation_data = val_data
self.batch_size = batch_size
def on_train_begin(self, logs={}):
self.val_kappas = []
def on_epoch_end(self, epoch, logs={}):
X_val, y_val = self.validation_data[:2]
y_val = y_val.sum(axis=1)- 1
y_pred = self.model.predict(X_val)> 0.5
y_pred = y_pred.astype(int ).sum(axis=1)- 1
_val_kappa = metrics.cohen_kappa_score(
 y_val,
y_pred,
weights='quadratic' 
)
 self.val_kappas.append(_val_kappa)
 print(f" val_kappa: {_val_kappa:.4f}")
 if _val_kappa == max(self.val_kappas):
print("Validation Kappa has improved.Saving model.")
 self.model.save('model.h5')
 return
m=[]
for x in range(0,len(data)) :
k=''
for f in data['ingredients'][x]:
k+=f.lower() +' '
m.append(k)
 data['ingredients']=m<define_variables>
kappa_cb = Metrics(((X_val, y_val)) )
cuisine_dic_t=[]<define_variables>
densenet = tf.keras.applications.DenseNet121(include_top=False, weights='.. /input/densenet121/densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5', input_shape=(IMG_SIZE, IMG_SIZE, 3))
for x in range(0,len(data)) :
if data['cuisine'][x] not in cuisine_dic_t:
cuisine_dic_t.append(data['cuisine'][x] )<create_dataframe>
model = tf.keras.Sequential([
densenet,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(5, activation='sigmoid')
 ] )
cuisine_dic=pd.DataFrame(data=cuisine_dic_t )<define_variables>
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_acc', factor=0.6, patience=6, verbose=1)
 class_weights = class_weight.compute_class_weight('balanced', np.unique(np.sum(y_train, axis=1)) , np.sum(y_train, axis=1))
cuisine=[]<string_transform>
optimizer = tf.keras.optimizers.Adam(lr=5e-5 )
tokenizer = Tokenizer(filters='!"
tokenizer.fit_on_texts(data['ingredients'])
 ingridients = tokenizer.texts_to_sequences(data['ingredients'] )<prepare_x_and_y>
steps_per_epoch = int(np.ceil(X_train.shape[0]/BATCH_SIZE))
val_steps_per_epoch = int(np.ceil(X_val.shape[0]/BATCH_SIZE))
X_train=ingridients[:30000]
y_train=cuisine[:30000]
X_test=ingridients[30000:]
y_test=cuisine[30000:]<categorify>
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'] )
X_train = tokenizer.sequences_to_matrix(X_train,mode='binary')
 X_test = tokenizer.sequences_to_matrix(X_test,mode='binary')
 y_train = keras.utils.to_categorical(y_train,len(cuisine_dic))
y_test = keras.utils.to_categorical(y_test,len(cuisine_dic))<choose_model_class>
history = model.fit_generator(train_gen, validation_data=(X_val, y_val), steps_per_epoch=steps_per_epoch,
callbacks=[kappa_cb], epochs=25, class_weight=class_weights )
model = Sequential()
model.add(Dense(2048, input_shape=(len(tokenizer.word_index)+1,)))
 model.add(Activation('relu'))
model.add(Dropout(0.7))
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(len(cuisine_dic)))
 model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'] )<train_model>
model.load_weights('model.h5' )
model.fit(X_train, y_train,
batch_size=128,
epochs=10,
validation_data=(X_test, y_test))<compute_test_metric>
val_preds = np.squeeze(model.predict_generator(val_gen, steps=val_steps_per_epoch))
list(zip(val_preds, y_val)) [:10]
model.evaluate(X_test, y_test,
batch_size=32, verbose=1 )<load_from_disk>
pred = val_preds > 0.5
pred = np.sum(pred.astype(np.int8), axis=1)-1
kappa, acc = metrics.accuracy_score(pred, y_val.sum(axis=1)-1), metrics.cohen_kappa_score(y_val.sum(axis=1)-1, pred, weights='quadratic')
 print('kappa score: {:.4f}, accuracy: {:.4f}'.format(kappa, acc))
test_data=pd.read_json('.. /input/test.json' )<feature_engineering>
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = metrics.cohen_kappa_score(y, X_p, weights='quadratic')
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']
m=[]
for x in range(0,len(test_data)) :
k=''
for f in test_data['ingredients'][x]:
k+=f.lower() +' '
m.append(k)
 test_data['ingredients']=m<string_transform>
optR = OptimizedRounder()
optR.fit(np.sum(val_preds,axis=1), np.sum(y_val, axis=1))
coefficients = optR.coefficients()
ingridients_test = tokenizer.texts_to_sequences(test_data['ingredients'] )<concatenate>
sample = pd.read_csv(".. /input/aptos2019-blindness-detection/sample_submission.csv" )
ingridients_test=tokenizer.sequences_to_matrix(ingridients_test,mode='binary' )<predict_on_test>
test_gen = TestSequence(sample['id_code'], BATCH_SIZE, True )
s=model.predict_classes(ingridients_test )<concatenate>
prediction = np.empty(( 5,len(sample)))
 for i in tqdm(range(5)) :
pred = model.predict_generator(test_gen)> 0.5
prediction[i,:] = np.sum(pred.astype(np.int8), axis=1)-1
prediction = prediction.astype(np.int8 )
res_t=[]
for x in s:
res_t.append(cuisine_dic[0][x] )<feature_engineering>
def vote(x):
return np.argmax(np.bincount(x))
test_data['ingredients']=res_t<rename_columns>
preds = np.apply_along_axis(vote, 0, prediction )
<save_to_csv><EOS> 
sample.diagnosis = preds
sample.to_csv("submission.csv", index=False )