Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<data_type_conversions>
!pip install pytorchcv
y_train = train['label'].astype('int32')
 X_train =(train.drop(['label'], axis = 1)).values.astype('float32')
 X_test = test.values.astype('float32')
 batch_size, img_rows, img_cols = 64, 28, 28
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_train.shape, X_test.shape<categorify>
!pip install fastai==1.0.47
X_train /= 255
X_test /= 255<categorify>
import numpy as np
import pandas as pd
from pytorchcv.model_provider import get_model as ptcv_get_model
y_train = np_utils.to_categorical(y_train, 10 )<split>
from pathlib import Path
from fastai import *
from fastai.vision import *
import torch
X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train,
test_size = 0.1, random_state = 12345 )<choose_model_class>
data_folder = Path(".. /input")
input_shape =(img_rows, img_cols, 1)
 callback_es = EarlyStopping(monitor = 'val_accuracy', patience = 3)
 def first_cnn_model_keras(optimizer):
model = Sequential()
model.add(Convolution2D(64, 5, 5, padding = 'same', kernel_initializer = 'he_uniform',
input_shape = input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size =(2, 2), strides =(2, 2), padding = 'same'))
model.add(Convolution2D(128, 5, 5, padding = 'same', kernel_initializer = 'he_uniform'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size =(2, 2), strides =(2, 2), padding = 'same'))
model.add(Flatten())
 model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(10))
model.add(Activation('softmax'))
model.compile(optimizer, loss='categorical_crossentropy', metrics = ['accuracy'])
 return model<train_model>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/sample_submission.csv" )
model1 = first_cnn_model_keras(Adam(learning_rate = 0.001, amsgrad = True))
h1 = model1.fit(X_train, y_train, batch_size = batch_size, epochs = 20, verbose = 1,
validation_data =(X_valid, y_valid), callbacks = [callback_es])
 final_loss_first_adam, final_acc_first_adam = model1.evaluate(X_valid, y_valid, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss_first_adam, final_acc_first_adam))<train_model>
def md(f=None):
mdl = ptcv_get_model('condensenet74_c4_g4', pretrained=True)
 mdl.features.final_pool = nn.AvgPool2d(kernel_size=7, stride=1, padding=3)
 return mdl
model2 = first_cnn_model_keras(RMSprop(lr=0.001))
h2 = model2.fit(X_train, y_train, batch_size = batch_size, epochs = 20, verbose = 1,
validation_data =(X_valid, y_valid),callbacks = [callback_es])
 final_loss_first_rmsprop, final_acc_first_rmsprop = model2.evaluate(X_valid, y_valid, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss_first_rmsprop, final_acc_first_rmsprop))<train_model>
lr = 3.5e-02
learn.fit_one_cycle(5, slice(lr))
datagen = ImageDataGenerator(rotation_range = 10,
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1)
 datagen.fit(X_train)
 train_batches = datagen.flow(X_train, y_train, batch_size = batch_size )<train_model>
preds,_ = learn.get_preds(ds_type=DatasetType.Test )
model3 = first_cnn_model_keras(Adam(learning_rate = 0.001, amsgrad = True))
h3 = model3.fit_generator(train_batches, epochs = 40, verbose = 1,
validation_data =(X_valid, y_valid), callbacks = [callback_es])
 final_loss_first_adam_aug, final_acc_first_adam_aug = model3.evaluate(X_valid, y_valid, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss_first_adam_aug, final_acc_first_adam_aug))<train_model>
test_df.has_cactus = preds.numpy() [:, 0]
<choose_model_class><EOS> 
test_df.to_csv('simple_fastai_submission.csv', index=False )