Kaggle competition: Fashion MNIST challenge2019
<SOS> metric: categorizationaccuracy Kaggle data source: fashion-mnist-challenge2019<train_on_grid>
import numpy as np
import pandas as pd
import os
from subprocess import check_output
import keras
from keras.models import Sequential
from keras.utils import to_categorical
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from sklearn.model_selection import train_test_split
kappa_metrics = Metrics()
es = EarlyStopping(monitor='val_loss', mode='auto', verbose=1, patience=12)
 rlr = ReduceLROnPlateau(monitor='val_loss',
factor=0.5,
patience=3,
verbose=1,
mode='auto',
epsilon=0.0001)
 model.fit_generator(train_generator,
steps_per_epoch=train_generator.samples // BATCH_SIZE,
epochs=20,
validation_data=val_generator,
validation_steps = val_generator.samples // BATCH_SIZE,
callbacks=[kappa_metrics, es, rlr] )<load_pretrained>
data_train = pd.read_csv('.. /input/train42000.csv')
 data_test = pd.read_csv('.. /input/test28000.csv' )
model.load_weights(SAVED_MODEL_NAME )<compute_train_metric>
X = np.array(data_train.iloc[:, 1:])
 y = to_categorical(np.array(data_train.iloc[:, 0]))
y_train_preds, train_labels = get_preds_and_labels(model, train_generator)
 y_train_preds = np.rint(y_train_preds ).astype(np.uint8 ).clip(0, 4)
 train_score = cohen_kappa_score(train_labels.astype(np.uint8), y_train_preds, weights="quadratic")
<compute_test_metric>
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=13)
 X_test = np.array(data_test.iloc[:, 0:] )
print(f"The Training Cohen Kappa Score is: {round(train_score, 5)}")
<compute_test_metric>
img_rows, img_cols = 28, 28
input_shape =(img_rows, img_cols, 1)
 X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)
 X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)
 X_val = X_val.reshape(X_val.shape[0], img_rows, img_cols, 1 )
y_val_preds, val_labels = get_preds_and_labels(model, val_generator)
 y_val_preds = np.rint(y_val_preds ).astype(np.uint8 ).clip(0, 4)
 val_score = cohen_kappa_score(val_labels.astype(np.uint8), y_val_preds, weights="quadratic")
 print(f"The Training Cohen Kappa Score is: {round(train_score, 5)}")
 print(f"The Validation Cohen Kappa Score is: {round(val_score, 5)}" )<compute_train_metric>
X_train = X_train.astype('float32')
 X_test = X_test.astype('float32')
 X_val = X_val.astype('float32')
 X_train /= 255
X_test /= 255
X_val /= 255
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']<compute_train_metric>
batch_size = 256
num_classes = 10
epochs = 5
y_val_preds, val_labels = get_preds_and_labels(model, val_generator)
 optR = OptimizedRounder()
optR.fit(y_val_preds, val_labels.astype(np.uint8))
coefficients = optR.coefficients()
opt_val_predictions = optR.predict(y_val_preds, coefficients)
 new_val_score = cohen_kappa_score(val_labels.astype(np.uint8), opt_val_predictions, weights="quadratic")
 print(f"Optimized Thresholds:
{coefficients}
")
 print(f"The Validation Quadratic Weighted Kappa(QWK)
 \
with optimized rounding thresholds is: {round(new_val_score, 5)}
")
 print(f"This is an improvement of {round(new_val_score - val_score, 5)}
\
over the unoptimized rounding" )<data_type_conversions>
model = Sequential()
model.add(Conv2D(20, kernel_size=(5, 5),
activation='relu',
kernel_initializer='he_normal',
input_shape=input_shape))
model.add(MaxPooling2D(( 2, 2)))
 model.add(Flatten())
 model.add(Dense(20, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
print(classification_report(val_labels.astype(np.uint8), opt_val_predictions,digits=4))<feature_engineering>
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adam() ,
metrics=['accuracy'] )
test_df['DR_level'] = np.zeros(test_df.shape[0])
 test_generator = ImageDataGenerator(preprocessing_function=preprocess_image,
rescale=1 / 128.).flow_from_dataframe(test_df,
x_col='image_id',
y_col='DR_level',
directory=test_data,
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=BATCH_SIZE,
class_mode='other',
shuffle=False )<save_to_csv>
history = model.fit(X_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(X_val, y_val))
y_test, _ = get_preds_and_labels(model, test_generator)
 y_test = optR.predict(y_test, coefficients ).astype(np.uint8)
 test_df['DR_levle'] = y_test
test_df['image_id'] = test_df['image_id'].str.replace(r'.jpg$', '')
 test_df.to_csv('Challengel1_upload.csv', index=False )<count_values>
predicted_classes = model.predict_classes(X_test )
<define_variables><EOS> 
df = pd.DataFrame(predicted_classes,columns=['label'])
 df.to_csv(path_or_buf="Submission.csv",index_label="image_id" )