Kaggle competition: iWildCam 2019 - FGVC6
<SOS> metric: MacroFScore Kaggle data source: iwildcam-2019-fgvc6<choose_model_class>
import os
import json
import numpy as np
import pandas as pd
import keras
from keras import layers
from keras.applications import DenseNet121
from keras.callbacks import Callback, ModelCheckpoint
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.utils.vis_utils import plot_model
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, f1_score, precision_score, recall_score
model = Sequential()
model.add(Convolution2D(32,(3,3), padding='same', use_bias=False, input_shape=(96,96,1)))
 model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(32,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(64,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(64,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(96,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(96,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(128,(3,3),padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(128,(3,3),padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(256,(3,3),padding='same',use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(256,(3,3),padding='same',use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(512,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(512,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dense(512,activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(30))
model.summary()<train_model>
x_train = np.load('.. /input/reducing-image-sizes-to-32x32/X_train.npy')
 x_test = np.load('.. /input/reducing-image-sizes-to-32x32/X_test.npy')
 y_train = np.load('.. /input/reducing-image-sizes-to-32x32/y_train.npy')
 print('x_train shape:', x_train.shape)
 print(x_train.shape[0], 'train samples')
 print(x_test.shape[0], 'test samples')
 x_train = x_train.astype('float32')
 x_test = x_test.astype('float32')
 x_train /= 255.
 x_test /= 255 .
%%time
if os.path.exists('.. /input/data-augmentation-for-facial-keypoint-detection/best_model.hdf5'):
model = load_model('.. /input/data-augmentation-for-facial-keypoint-detection/best_model.hdf5')
 checkpointer = ModelCheckpoint(filepath = 'best_model.hdf5', monitor='val_mae', verbose=1, save_best_only=True, mode='min')
 model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mae', 'acc'])
 history = model.fit(train_images, train_keypoints, epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, validation_split=0.05, callbacks=[checkpointer] )<train_model>
class Metrics(Callback):
def on_train_begin(self, logs={}):
self.val_f1s = []
self.val_recalls = []
self.val_precisions = []
def on_epoch_end(self, epoch, logs={}):
X_val, y_val = self.validation_data[:2]
y_pred = self.model.predict(X_val)
 y_pred_cat = keras.utils.to_categorical(
 y_pred.argmax(axis=1),
num_classes=14 
)
 _val_f1 = f1_score(y_val, y_pred_cat, average='macro')
 _val_recall = recall_score(y_val, y_pred_cat, average='macro')
 _val_precision = precision_score(y_val, y_pred_cat, average='macro')
 self.val_f1s.append(_val_f1)
 self.val_recalls.append(_val_recall)
 self.val_precisions.append(_val_precision)
 print(( f"val_f1: {_val_f1:.4f}"
f" — val_precision: {_val_precision:.4f}"
f" — val_recall: {_val_recall:.4f}"))
return
f1_metrics = Metrics()
%%time
checkpointer = ModelCheckpoint(filepath = 'best_model.hdf5', monitor='mae', verbose=1, save_best_only=True, mode='min')
 model.fit(train_images, train_keypoints, epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, callbacks=[checkpointer] )<predict_on_test>
densenet = DenseNet121(
 weights='.. /input/densenet-keras/DenseNet-BC-121-32-no-top.h5',
include_top=False,
input_shape=(32,32,3)
)
 model = Sequential()
model.add(densenet)
 model.add(layers.GlobalAveragePooling2D())
 model.add(Dropout(0.5))
model.add(layers.Dense(14, activation='softmax'))
plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)
 model.summary()
%%time
model = load_model('best_model.hdf5')
 test_preds = model.predict(test_images )<save_to_csv>
checkpoint = ModelCheckpoint(
 'model.h5',
monitor='val_acc',
verbose=1,
save_best_only=True,
save_weights_only=False,
mode='auto'
)
feature_names = list(idlookup_data['FeatureName'])
 image_ids = list(idlookup_data['ImageId']-1)
 row_ids = list(idlookup_data['RowId'])
 feature_list = []
for feature in feature_names:
feature_list.append(feature_names.index(feature))
predictions = []
for x,y in zip(image_ids, feature_list):
predictions.append(test_preds[x][y])
 row_ids = pd.Series(row_ids, name = 'RowId')
 locations = pd.Series(predictions, name = 'Location')
 locations = locations.clip(0.0,96.0)
 submission_result = pd.concat([row_ids,locations],axis = 1)
 submission_result.to_csv('submission.csv',index = False )<import_modules>
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 history = model.fit(
 x=x_train,
y=y_train,
batch_size=256,
epochs=30,
callbacks=[checkpoint, f1_metrics],
validation_split=0.2
)
<load_from_csv><EOS> 
model.load_weights('model.h5')
 y_test = model.predict(x_test)
 submission_df = pd.read_csv('.. /input/iwildcam-2019-fgvc6/sample_submission.csv')
 submission_df['Predicted'] = y_test.argmax(axis=1)
 print(submission_df.shape)
 submission_df.head()
submission_df.to_csv('submission.csv',index=False )