Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<load_from_csv>
sns.set()
test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 print(test_data.shape )<load_from_csv>
cactus_dir = ".. /input/aerial-cactus-identification/"
train_dir = cactus_dir + "/train/train/"
test_dir = cactus_dir + "/test/test/"
df_train_data = pd.read_csv(cactus_dir + "/train.csv")
 df_train_data['has_cactus'] = df_train_data['has_cactus'].astype(str)
 df_test = pd.read_csv(cactus_dir + "/sample_submission.csv" )
sample_submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 print(sample_submission.shape )<categorify>
print("The number of training images is: {}".format(len(df_train_data)))
 df_train_data['has_cactus'].value_counts()
encoder = OneHotEncoder(sparse=False,categories='auto')
 yy = [[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]]
encoder.fit(yy)
 train_label = train_label.reshape(-1,1)
 val_label = val_label.reshape(-1,1)
 train_label = encoder.transform(train_label)
 val_label = encoder.transform(val_label)
 print('train_label shape: %s'%str(train_label.shape))
print('val_label shape: %s'%str(val_label.shape))<import_modules>
has_cactus = df_train_data[df_train_data['has_cactus'] == '1']
not_cactus = df_train_data[df_train_data['has_cactus'] == '0']
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD
from keras.layers.normalization import BatchNormalization
from keras.layers import LeakyReLU 
<choose_model_class>
df_train = not_cactus[:3491]
df_train = df_train.append(has_cactus[:3491])
 df_train = df_train.sample(frac=1 ).reset_index(drop=True)
 df_train['has_cactus'].value_counts()
model = Sequential()
model.add(Conv2D(32,(3, 3), activation='relu', input_shape=(28, 28, 1),padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(32,(3, 3), activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64,(3, 3), activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(64,(3, 3), activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(128, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(128, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(256, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(256, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation='relu', name='my_dense'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.summary()<set_options>
df_valid = not_cactus[3491:]
df_valid = df_valid.append(has_cactus[3491:3491 + 873*3])
 df_valid = df_valid.sample(frac=1 ).reset_index(drop=True)
 df_valid['has_cactus'].value_counts()
datagen = ImageDataGenerator(
 rotation_range=15,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range = 15,
horizontal_flip = False,
zoom_range = 0.20 )<train_model>
train_datagen = ImageDataGenerator(rotation_range=50,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
rescale=1./255)
 valid_datagen = ImageDataGenerator(rescale=1./255 )
model.compile(loss='categorical_crossentropy',optimizer=Adam() ,metrics=['accuracy'])
 datagen.fit(train_image)
 history = model.fit_generator(datagen.flow(train_image,train_label, batch_size=32),
epochs = 75,
shuffle=True,
validation_data =(val_image,val_label),
verbose = 1,
steps_per_epoch=train_image.shape[0] // 32 )<predict_on_test>
new_image_size = 64
batch_size = 128
train_generator = train_datagen.flow_from_dataframe(df_train,
directory=train_dir,
x_col='id',
y_col='has_cactus',
target_size=(new_image_size, new_image_size),
class_mode='binary',
batch_size=batch_size)
 valid_generator = valid_datagen.flow_from_dataframe(df_valid,
directory=train_dir,
x_col='id',
y_col='has_cactus',
target_size=(new_image_size, new_image_size),
class_mode='binary',
batch_size=batch_size )
intermediate_output = intermediate_layer_model.predict(train_image)
 intermediate_output = pd.DataFrame(data=intermediate_output )<split>
model = Sequential()
model.add(Conv2D(32,(3, 3), input_shape=(new_image_size, new_image_size, 3), activation='relu'))
model.add(Dropout(0.2))
model.add(MaxPooling2D(( 2,2)))
 model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(Dropout(0.2))
model.add(MaxPooling2D(( 2,2)))
 model.add(Conv2D(128,(3,3), activation='relu'))
model.add(Dropout(0.2))
model.add(MaxPooling2D(( 2,2)))
 model.add(Conv2D(512,(3,3), activation='relu'))
model.add(Dropout(0.2))
model.add(MaxPooling2D(( 2,2)))
 model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))
model.summary()
val_data = intermediate_output[40000:]<predict_on_test>
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'] )
submission_cnn = model.predict(test_image )<predict_on_test>
checkpoint = ModelCheckpoint("model.hdf5",
monitor='val_acc',
verbose=1,
save_best_only=True,
mode='max')
 reduce_lr = ReduceLROnPlateau(monitor='val_acc',
factor=.2,
patience=10,
min_lr=1e-9,
verbose=1)
 early_stop = EarlyStopping(monitor='val_acc',
patience=100,
verbose=1,
mode='auto',
baseline=None,
restore_best_weights=True )
intermediate_test_output = intermediate_layer_model.predict(test_image)
 intermediate_test_output = pd.DataFrame(data=intermediate_test_output )<train_model>
model.evaluate_generator(valid_generator, steps=len(df_valid)//batch_size )
xgbmodel = XGBClassifier(objective='multi:softprob',
num_class= 10)
 xgbmodel.fit(intermediate_output, train_label1)
 xgbmodel.score(val_data, val_label1 )<predict_on_test>
test_fnames = []
test_imgs = []
for fname in os.listdir(test_dir):
test_fnames.append(fname)
 img = load_img(test_dir + fname, target_size=(new_image_size, new_image_size, 3))
img = img_to_array(img)/ 255
test_imgs.append(img)
 test_imgs = np.array(test_imgs)
 test_imgs.shape
submission_xgb = xgbmodel.predict(intermediate_test_output )<data_type_conversions>
pred = model.predict(test_imgs)
 pred[:5]
<define_variables><EOS> 
submission = pd.DataFrame()
submission['id'] = test_fnames
submission['has_cactus'] = pred
submission.to_csv('submission.csv', index=False)
 submission.head()