Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<import_modules>
!pip install.. /input/keras-efficientnet-whl/Keras_Applications-1.0.8-py3-none-any.whl
!pip install.. /input/keras-efficientnet-whl/efficientnet-1.1.1-py3-none-any.whl
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
import scipy
from tqdm import tqdm_notebook as tqdm<load_from_csv>
%matplotlib inline
print("Tensorflow version " + tf.__version__)
dd0=pd.read_csv("/kaggle/input/cat-in-the-dat/train.csv")
 ddtest0=pd.read_csv("/kaggle/input/cat-in-the-dat/test.csv")
 ddall=dd0.append(ddtest0, sort=False)
 num_train=len(dd0)
 ddall.head()<feature_engineering>
input_dir = ".. /input/cassava-leaf-disease-classification"
train_images_path = os.path.join(input_dir,"train_images")
 test_images_path = os.path.join(input_dir,'test_images' )
drop_cols=["bin_0"]
ddall["ord_5a"]=ddall["ord_5"].str[0]
ddall["ord_5b"]=ddall["ord_5"].str[1]
drop_cols.append("ord_5" )<drop_column>
train = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv' )
X=ddall[ddall.columns.difference(["id", "target"] + drop_cols)]<categorify>
image_list = train['image_id'].to_list()
label_list = train['label'].to_list()
X_oh=X[X.columns.difference(["ord_1", "ord_4", "ord_5a", "ord_5b", "day", "month"])]
oh1=pd.get_dummies(X_oh, columns=X_oh.columns, drop_first=True, sparse=True)
 ohc1=oh1.sparse.to_coo()<categorify>
TARGET_SIZE = 512
class ThermometerEncoder(TransformerMixin):

 def __init__(self, sort_key=None):
self.sort_key = sort_key
self.value_map_ = None
def fit(self, X, y=None):
self.value_map_ = {val: i for i, val in enumerate(sorted(X.unique() , key=self.sort_key)) }
return self
def transform(self, X, y=None):
values = X.map(self.value_map_)
 possible_values = sorted(self.value_map_.values())
 idx1 = []
idx2 = []
all_indices = np.arange(len(X))
for idx, val in enumerate(possible_values[:-1]):
new_idxs = all_indices[values > val]
idx1.extend(new_idxs)
 idx2.extend(repeat(idx, len(new_idxs)))
 result = scipy.sparse.coo_matrix(( [1] * len(idx1),(idx1, idx2)) , shape=(len(X), len(possible_values)) , dtype="int8")
 return result<categorify>
model2 = keras.models.load_model('.. /input/efficb3002/results-4/efficb1.h5')
 model1 = keras.models.load_model('.. /input/effb116/results-2/efficb1.h5' )
thermos=[]
for col in ["ord_1", "ord_2", "ord_3", "ord_4", "ord_5a", "day", "month"]:
if col=="ord_1":
sort_key=['Novice', 'Contributor', 'Expert', 'Master', 'Grandmaster'].index
elif col=="ord_2":
sort_key=['Freezing', 'Cold', 'Warm', 'Hot', 'Boiling Hot', 'Lava Hot'].index
elif col in ["ord_3", "ord_4", "ord_5a"]:
sort_key=str
elif col in ["day", "month"]:
sort_key=int
else:
raise ValueError(col)
 enc=ThermometerEncoder(sort_key=sort_key)
 thermos.append(enc.fit_transform(X[col]))<prepare_x_and_y>
submission_file = pd.read_csv(os.path.join('.. /input/cassava-leaf-disease-classification/sample_submission.csv'))
submission_file
ohc=scipy.sparse.hstack([ohc1] + thermos ).tocsr()
display(ohc)
 X_train = ohc[:num_train]
X_test = ohc[num_train:]
y_train = dd0["target"].values<save_to_csv>
preds = []
for image_id in submission_file.image_id:
image = Image.open(os.path.join(f'.. /input/cassava-leaf-disease-classification/test_images/{image_id}'))
image = image.resize(( TARGET_SIZE, TARGET_SIZE))
image = np.expand_dims(image, axis = 0)
 best_alpha = 0.51
probabilities1 = model1.predict(image)
 probabilities2 = model2.predict(image)
 probabilities = best_alpha * probabilities1 +(1 - best_alpha)* probabilities2
predictions = np.argmax(probabilities, axis=-1)
 preds.append(predictions[0])
 submission_file['label'] = preds
submission_file
<compute_train_metric><EOS> 
submission_file.to_csv('submission.csv', index = False )