Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<train_model>
!pip install pytorchcv
model5 = second_cnn_model_keras(Adam(learning_rate = 0.001, amsgrad = True))
h5 = model5.fit(X_train, y_train, batch_size = batch_size, epochs = 20, verbose = 1,
validation_data =(X_valid, y_valid), callbacks = [callback_es])
 final_loss_second_adam, final_acc_second_adam = model5.evaluate(X_valid, y_valid, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss_second_adam, final_acc_second_adam))<train_model>
!pip install fastai==1.0.47
model6 = second_cnn_model_keras(RMSprop(lr=0.001))
h6 = model6.fit(X_train, y_train, batch_size = batch_size, epochs = 20, verbose = 1,
validation_data =(X_valid, y_valid), callbacks = [callback_es])
 final_loss_second_rmsprop, final_acc_second_rmsprop = model6.evaluate(X_valid, y_valid, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss_second_rmsprop, final_acc_second_rmsprop))<train_model>
start = time.time()
model7 = second_cnn_model_keras(Adam(learning_rate = 0.001, amsgrad = True))
h7 = model7.fit_generator(train_batches, epochs = 20, verbose = 1,
validation_data =(X_valid, y_valid),callbacks = [callback_es])
 final_loss_second_adam_aug, final_acc_second_adam_aug = model7.evaluate(X_valid, y_valid, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss_second_adam_aug, final_acc_second_adam_aug))<train_model>
import numpy as np
import pandas as pd
from pytorchcv.model_provider import get_model as ptcv_get_model
model8 = second_cnn_model_keras(RMSprop(lr=0.001))
h8 = model8.fit_generator(train_batches, epochs = 20, verbose = 1,
validation_data =(X_valid, y_valid), callbacks = [callback_es])
 final_loss_second_rmsprop_aug, final_acc_second_rmsprop_aug = model8.evaluate(X_valid, y_valid, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss_second_rmsprop_aug, final_acc_second_rmsprop_aug))<define_variables>
from pathlib import Path
from fastai import *
from fastai.vision import *
import torch
models = ['first_cnn_adam', 'first_cnn_rmsprop', 'first_cnn_adam_aug', 'first_cnn_rmsprop_aug',
'second_cnn_adam', 'second_cnn_rmsprop', 'second_cnn_adam_aug', 'second_cnn_rmsprop_aug']
dict_values = {'loss': [final_loss_first_adam, final_loss_first_rmsprop, final_loss_first_adam_aug,
final_loss_first_rmsprop_aug, final_loss_second_adam, final_loss_second_rmsprop,
final_loss_second_adam_aug, final_loss_second_rmsprop_aug],
'accuracy': [final_acc_first_adam, final_acc_first_rmsprop, final_acc_first_adam_aug,
final_acc_first_rmsprop_aug, final_acc_second_adam, final_acc_second_rmsprop,
final_acc_second_adam_aug, final_acc_second_rmsprop_aug]}
df = pd.DataFrame(dict_values, index = models, columns = ['loss', 'accuracy'])
 df<choose_model_class>
data_folder = Path(".. /input")
model = [0]*10
for i in range(10):
model[i] = Sequential()
model[i].add(Convolution2D(64, kernel_size =(3, 3), input_shape = input_shape, kernel_initializer = 'he_uniform'))
model[i].add(Activation('relu'))
model[i].add(BatchNormalization())
 model[i].add(Convolution2D(64, kernel_size =(3, 3), kernel_initializer = 'he_uniform'))
model[i].add(Activation('relu'))
model[i].add(BatchNormalization())
 model[i].add(Convolution2D(64, kernel_size =(5, 5), kernel_initializer = 'he_uniform'))
model[i].add(Activation('relu'))
model[i].add(BatchNormalization())
 model[i].add(MaxPooling2D(pool_size =(2, 2), strides =(2, 2)))
 model[i].add(Dropout(0.45))
model[i].add(Convolution2D(128, kernel_size =(3, 3), kernel_initializer = 'he_uniform'))
model[i].add(Activation('relu'))
model[i].add(BatchNormalization())
 model[i].add(Convolution2D(128, kernel_size =(3, 3), kernel_initializer = 'he_uniform'))
model[i].add(Activation('relu'))
model[i].add(BatchNormalization())
 model[i].add(Convolution2D(128, kernel_size =(5, 5), kernel_initializer = 'he_uniform'))
model[i].add(Activation('relu'))
model[i].add(BatchNormalization())
 model[i].add(MaxPooling2D(pool_size =(2, 2), strides =(2, 2)))
 model[i].add(Dropout(0.45))
model[i].add(Flatten())
 model[i].add(Dense(512))
model[i].add(Activation('relu'))
model[i].add(Dropout(0.45))
model[i].add(Dense(1024))
model[i].add(Activation('relu'))
model[i].add(Dropout(0.45))
model[i].add(Dense(10))
model[i].add(Activation('softmax'))
model[i].compile(loss = 'categorical_crossentropy', optimizer = Adam(lr = 0.0005, amsgrad = True), metrics=['accuracy'] )<train_model>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/sample_submission.csv" )
callback_lrs = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 epochs = 40
history = [0]*10
for j in range(10):
X_train1, X_valid1, y_train1, y_valid1 = train_test_split(X_train, y_train, test_size = 0.1)
 history[j] = model[j].fit_generator(datagen.flow(X_train1, y_train1, batch_size = batch_size), epochs = epochs, verbose = 0,
validation_data =(X_valid1, y_valid1), callbacks = [callback_lrs])
 print('CNN:', j+1, 'Epochs =', epochs, 'Train accuracy:', max(history[j].history['accuracy']), 'Validation accuracy:', max(history[j].history['val_accuracy']))<save_to_csv>
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
results = np.zeros(( X_test.shape[0],10))
for j in range(10):
results = results + model[j].predict(X_test)
 y_test_class = np.argmax(results, axis = 1)
 submission = pd.DataFrame({'ImageId': list(range(1, len(y_test_class)+1)) , 'Label': np.array(y_test_class)})
 submission.to_csv('submission.csv', index=False)
 print(submission )<load_from_csv>
learn = cnn_learner(train_img, md, metrics=[error_rate, accuracy] )
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )<set_options>
lr = 3.5e-02
learn.fit_one_cycle(5, slice(lr))
params = {
'axes.labelsize': "large",
'xtick.labelsize': 'medium',
'legend.fontsize': 'medium',
'legend.loc': "best",
}
plot.rcParams.update(params)
 train_data['Died'] = 1 - train_data['Survived']<categorify>
preds,_ = learn.get_preds(ds_type=DatasetType.Test )
title_mapping = {"Mrs": 4, "Miss": 3, "Mr": 0, "Noble": 2,"Crew": 1}
train_data['Title Map'] = train_data['Titles'].map(title_mapping)
 test_data['Title Map'] = test_data['Titles'].map(title_mapping )<data_type_conversions>
test_df.has_cactus = preds.numpy() [:, 0]
train_data["Fare"] = train_data["Fare"].fillna(train_data["Fare"].median())
 test_data["Fare"] = test_data["Fare"].fillna(test_data["Fare"].median() )<sort_values>
test_df.to_csv('submission.csv', index=False )