Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<load_from_csv>
%matplotlib inline
df_train = pd.read_csv(train_dir)
 df_train.info()<prepare_x_and_y>
TEST_SIZE = 0.5
RANDOM_STATE = 2018
BATCH_SIZE = 64
NO_EPOCHS = 20
NUM_CLASSES = 2
SAMPLE_SIZE = 20000
PATH = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/'
TRAIN_FOLDER = './train/'
TEST_FOLDER = './test/'
IMG_SIZE = 224
RESNET_WEIGHTS_PATH = '/kaggle/input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
y_train = df_train['label']
X_train = df_train.drop(columns=['label'] )<train_model>
train_image_path = os.path.join(PATH, "train.zip")
 test_image_path = os.path.join(PATH, "test.zip" )
display_image(X_train, y_train, n=10, label=True )<split>
with zipfile.ZipFile(train_image_path,"r")as z:
z.extractall("." )
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, shuffle=False )<prepare_x_and_y>
with zipfile.ZipFile(test_image_path,"r")as z:
z.extractall("." )
X_train = X_train.values.reshape(-1, 28,28,1)
 X_val = X_val.values.reshape(-1, 28,28,1 )<feature_engineering>
train_image_list = os.listdir("./train/")[0:SAMPLE_SIZE]
test_image_list = os.listdir("./test/" )
X_train = X_train / 255.0
X_val = X_val / 255.0<categorify>
def label_pet_image_one_hot_encoder(img):
pet = img.split('.')[-3]
if pet == 'cat': return [1,0]
elif pet == 'dog': return [0,1]
Y_train = pd.get_dummies(y_train ).values
Y_val = pd.get_dummies(y_val ).values<categorify>
def process_data(data_image_list, DATA_FOLDER, isTrain=True):
data_df = []
for img in tqdm(data_image_list):
path = os.path.join(DATA_FOLDER,img)
 if(isTrain):
label = label_pet_image_one_hot_encoder(img)
 else:
label = img.split('.')[0]
img = cv2.imread(path,cv2.IMREAD_COLOR)
 img = cv2.resize(img,(IMG_SIZE,IMG_SIZE))
data_df.append([np.array(img),np.array(label)])
 shuffle(data_df)
 return data_df
print("La valeur {} est encodée vers le vecteur {}".format(y_train[0], Y_train[0]))
print("valeur {} transformée en vecteur: {}".format(y_train[20], Y_train[20]))<init_hyperparams>
train = process_data(train_image_list, TRAIN_FOLDER )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=20,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False )<choose_model_class>
test = process_data(test_image_list, TEST_FOLDER, False )
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5, 5), activation='relu', padding='Same', input_shape =(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(5, 5), activation='relu', padding='Same'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(strides=(2,2)))
 model.add(Dropout(0.25))<choose_model_class>
X = np.array([i[0] for i in train] ).reshape(-1,IMG_SIZE,IMG_SIZE,3)
 y = np.array([i[1] for i in train] )
model.add(Flatten())
 model.add(Dense(units=1024, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(units=1024, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(units=10, activation='softmax'))<choose_model_class>
model = Sequential()
model.add(ResNet50(include_top=False, pooling='max', weights=RESNET_WEIGHTS_PATH))
model.add(Dense(NUM_CLASSES, activation='softmax'))
model.layers[0].trainable = True
model.compile(loss='categorical_crossentropy', optimizer = Adam(lr=0.0001), metrics=["accuracy"] )<train_model>
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'] )
hist = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=32),
steps_per_epoch=1000,
epochs=25,
verbose=1,
validation_data=(X_val, Y_val))<compute_test_metric>
plot_model(model, to_file='model.png')
 SVG(model_to_dot(model ).create(prog='dot', format='svg'))
final_loss, final_acc = model.evaluate(X_val, Y_val, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss, final_acc))<predict_on_test>
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=TEST_SIZE, random_state=RANDOM_STATE )
Y_hat = model.predict(X_val)
 Y_hat[0]<prepare_output>
train_model = model.fit(X_train, y_train,
batch_size=BATCH_SIZE,
epochs=NO_EPOCHS,
verbose=1,
validation_data=(X_val, y_val))
Y_pred = np.argmax(Y_hat, axis=1)
 Y_true = np.argmax(Y_val, axis=1 )<compute_test_metric>
score = model.evaluate(X_val, y_val, verbose=0)
 print('Validation loss:', score[0])
 print('Validation accuracy:', score[1] )
cm = confusion_matrix(Y_true, Y_pred)
 print(cm )<load_from_csv>
predicted_classes = model.predict_classes(X_val)
 y_true = np.argmax(y_val,axis=1 )
X_test = pd.read_csv(test_dir)
 X_test = X_test.values.reshape(-1, 28,28,1)
 X_test = X_test / 255.0<predict_on_test>
correct = np.nonzero(predicted_classes==y_true)[0]
incorrect = np.nonzero(predicted_classes!=y_true)[0]
Y_hat = model.predict(X_test, verbose=1)
 Y_pred = np.argmax(Y_hat, axis=1 )<create_dataframe>
target_names = ["Class {}:".format(i)for i in range(NUM_CLASSES)]
print(classification_report(y_true, predicted_classes, target_names=target_names))
display_image(pd.DataFrame(X_test.reshape(-1, 784)) , Y_pred, n=10, label=True )<save_to_csv>
pred_list = []
img_list = []
for img in tqdm(test):
img_data = img[0]
img_idx = img[1]
data = img_data.reshape(-1,IMG_SIZE,IMG_SIZE,3)
 predicted = model.predict([data])[0]
img_list.append(img_idx)
 pred_list.append(predicted[1] )
<set_options><EOS> 
submission = pd.DataFrame({'id':img_list , 'label':pred_list})
 submission.head()
submission.to_csv("submission.csv", index=False )