Kaggle competition: APTOS 2019 Blindness Detection
parameters = {
'tfidf__tokenizer': [tok_clean],
'tfidf__binary': [False],
'tfidf__use_idf': [True],
'tfidf__sublinear_tf': [True],
'classifier__estimator__multi_class' : ['multinomial'],
'classifier__estimator__C' : [2.6],
'classifier__estimator__fit_intercept' : [True],
'classifier__estimator__class_weight' : [None],
'classifier__estimator__warm_start': [True]
}
pipe = Pipeline(
 memory=None,
steps=[ 
('tfidf', TfidfVectorizer(preprocessor=None, lowercase=False)) , 
('classifier', OneVsRestClassifier(LogisticRegression(solver='saga', max_iter=500, random_state=42))),
])
 grid = GridSearchCV(pipe, parameters, verbose=2, cv=2, iid=False, scoring='f1_micro', n_jobs=3)
 grid.fit(train['ingredients'], y_train)
 print(grid.best_params_)
 print(grid.best_score_)
<save_to_csv>
import os
import cv2
import random
import numpy as np
import pandas as pd
import scipy as sp
import torch
from fastai.vision import *
import glob
predicted_classes = encoder.inverse_transform(grid.predict(test['ingredients']))
submissions = pd.DataFrame({"id": test['id'], "cuisine": predicted_classes})
 submissions.to_csv("submission.csv", index=False, header=True )<load_from_disk>
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
SEED = 2019
seed_everything(SEED)
 print('Make sure cudnn is enabled:', torch.backends.cudnn.enabled )
train_df = pd.read_json(".. /input/train.json")
 train_df.head()<load_from_disk>
PATH = ".. /input/aptos2019-blindness-detection"
!mkdir -p /tmp/.cache/torch/checkpoints/
!cp.. /input/fastai-pretrained/densenet161-8d451a50.pth /tmp/.cache/torch/checkpoints/densenet161-8d451a50.pth
!cp.. /input/fastai-pretrained/densenet169-b2777c0a.pth /tmp/.cache/torch/checkpoints/densenet169-b2777c0a.pth
test_df = pd.read_json(".. /input/test.json")
 test_df.head()<concatenate>
SIZE=300
train_df=pd.read_csv(PATH+'/train.csv')
 test_df=pd.read_csv(PATH+'/sample_submission.csv' )
test_df["cuisine"] = "unknown"
df = pd.concat([train_df, test_df], ignore_index=True)
 df.head()<categorify>
train = ImageList.from_df(train_df, path=PATH, cols='id_code', folder="train_images", suffix='.png')
 test = ImageList.from_df(test_df, path=PATH, cols='id_code', folder="test_images", suffix='.png' )
df.ingredients = df.ingredients.apply(lambda x:(" ".join(x)).lower())
 df.ingredients = df.ingredients.apply(lambda x: re.sub(r'[^\w\d ,]', '', x))
df.head()<define_variables>
def quadratic_kappa(y_hat, y):
return torch.tensor(cohen_kappa_score(torch.round(y_hat), y, weights='quadratic'),device='cuda:0' )
train_set = df[df.cuisine != 'unknown']
test_set = df[df.cuisine == 'unknown']<categorify>
data.show_batch(rows=4,figsize=(15,15))
tfidf = TfidfVectorizer(binary=True)
 x = tfidf.fit_transform(train_set.ingredients ).todense()
x_test = tfidf.transform(test_set.ingredients ).todense()<categorify>
learn_1 = cnn_learner(data, models.densenet169, metrics=[quadratic_kappa], pretrained=True ).mixup()
lb = LabelEncoder()
y = lb.fit_transform(train_set.cuisine)
 y = np_utils.to_categorical(y)
 print(y.shape )<split>
learn_1.fit_one_cycle(10,1e-02)
 learn_1.recorder.plot_losses()
learn_1.recorder.plot_metrics()
seed = 29
x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.2, random_state=seed)
 print(x_train.shape, y_train.shape, x_val.shape, y_val.shape )<choose_model_class>
learn_1.save("densenet161phase1")
 learn_1.export()
MLP = Sequential()
MLP.add(Dense(512, input_shape=(3073,), activation='relu'))
MLP.add(Dropout(0.5))
MLP.add(Dense(y_train.shape[1], activation='softmax'))
MLP.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 MLP.summary()<train_model>
learn_1.unfreeze()
file_path = "mlp.hdf5"
check_point = ModelCheckpoint(file_path, monitor="val_acc", verbose=1, save_best_only=True, mode="max")
 early_stop = EarlyStopping(monitor="val_acc", mode="max", patience=5)
 mlp_history = MLP.fit(x_train,
y_train,
batch_size=128,
epochs=50,
validation_data=(x_val, y_val),
callbacks=[check_point, early_stop] )<load_pretrained>
learn_1.lr_find()
mlp_best = load_model('mlp.hdf5' )<choose_model_class>
learn_1.fit_one_cycle(5, max_lr=slice(7e-05,1e-04))
learn_1.recorder.plot_losses()
learn_1.recorder.plot_metrics()
inp = Input(shape=(3073,), dtype='float32')
 reshape = Reshape(target_shape=(7,439))(inp)
 stacks = []
for kernel_size in [2, 3, 4]:
conv = Conv1D(128, kernel_size, padding='same', activation='relu', strides=1 )(reshape)
 pool = MaxPooling1D(pool_size=3 )(conv)
 drop = Dropout(0.5 )(pool)
 stacks.append(drop)
 merged = Concatenate()(stacks)
 flatten = Flatten()(merged)
 drop = Dropout(0.5 )(flatten)
 outp = Dense(y_train.shape[1], activation='softmax' )(drop)
 TextCNN = Model(inputs=inp, outputs=outp)
 TextCNN.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 TextCNN.summary()<train_model>
valid_preds, valid_y = learn_1.get_preds(ds_type=DatasetType.Valid)
 test_preds, _ = learn_1.get_preds(ds_type=DatasetType.Test )
file_path = "textcnn.hdf5"
check_point = ModelCheckpoint(file_path, monitor="val_acc", verbose=1, save_best_only=True, mode="max")
 early_stop = EarlyStopping(monitor="val_acc", mode="max", patience=5)
 textcnn_history = TextCNN.fit(x_train,
y_train,
batch_size=128,
epochs=50,
validation_data=(x_val, y_val),
callbacks=[check_point, early_stop] )<load_pretrained>
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 = 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']
textcnn_best = load_model('textcnn.hdf5' )<predict_on_test>
optR = OptimizedRounder()
optR.fit(valid_preds, valid_y)
 coefficients = optR.coefficients()
valid_predictions = optR.predict(valid_preds, coefficients)[:,0].astype(int)
 test_predictions = optR.predict(test_preds, coefficients)[:,0].astype(int)
 valid_score = cohen_kappa_score(valid_y.numpy().astype(int), valid_predictions, weights="quadratic" )
mlp_pred = mlp_best.predict(x_test)
 textcnn_pred = textcnn_best.predict(x_test)
 ensemble_pred =(mlp_pred + textcnn_pred ).argmax(axis=1)
 final = lb.inverse_transform(ensemble_pred )<save_to_csv>
valid_score = cohen_kappa_score(valid_y.numpy().astype(int), valid_predictions, weights="quadratic" )
sub = pd.DataFrame({'id': test_df.id, 'cuisine': final})
 sub.to_csv('tfidf-textcnn.csv', index = False )<import_modules>
print("coefficients:", coefficients)
 print("validation score:", valid_score )
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import SVC
import pandas as pd
import json<load_from_csv>
test_df.diagnosis = test_predictions
test_df.to_csv("submission.csv", index=None)
 test_df.head()