Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<create_dataframe>
print(os.listdir(".. /input"))
seed = 42
np.random.seed(seed )
traindf = titanic_train.copy()
testdf = titanic_test.copy()<define_variables>
base_dir = os.path.join(".. ", "input")
 train_df = pd.read_csv(os.path.join(base_dir, "train.csv"))
train_dir = os.path.join(base_dir, "train/train")
 test_dir = os.path.join(base_dir, "test/test")
all_data = [traindf, testdf] 
<drop_column>
train_df['has_cactus'] = train_df['has_cactus'].astype(str)
 batch_size = 64
train_size = 15750
validation_size = 1750
datagen = ImageDataGenerator(
 rescale=1./255,
horizontal_flip=True,
vertical_flip=True,
validation_split=0.1,
zca_whitening = False 
)
 data_args = {
"dataframe": train_df,
"directory": train_dir,
"x_col": 'id',
"y_col": 'has_cactus',
"featurewise_center" : True,
"featurewise_std_normalization" : True,
"samplewise_std_normalization" : False,
"samplewise_center" : False,
"shuffle": True,
"target_size":(32, 32),
"batch_size": batch_size,
"class_mode": 'binary'
}
train_generator = datagen.flow_from_dataframe(**data_args, subset='training')
 validation_generator = datagen.flow_from_dataframe(**data_args, subset='validation')
for dat in all_data:
dat.drop(['Name', 'Ticket'], axis=1, inplace=True )<feature_engineering>
def Le_Conv() :
model1 = Sequential()
model1.add(Conv2D(6, kernel_size=(5, 5), strides=(1, 1), activation='relu', input_shape=(32, 32, 3), padding="same"))
model1.add(AveragePooling2D(pool_size=(2, 2), strides=(1, 1), padding='valid'))
model1.add(Conv2D(16, kernel_size=(5, 5), strides=(1, 1), activation='relu', padding='valid'))
model1.add(AveragePooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid'))
model1.add(Conv2D(120, kernel_size=(5, 5), strides=(1, 1), activation='relu', padding='valid'))
model1.add(Flatten())
 model1.add(Dense(84, activation='relu'))
model1.add(Dense(1, activation='sigmoid'))
model1.compile(optimizer = Adamax(lr =0.001), loss = "binary_crossentropy", metrics=["acc"])
 return model1
def VggNet_Model() :
model = Sequential()
model.add(Conv2D(16,(2,2), activation="relu", input_shape=(32, 32, 3)))
 model.add(Conv2D(16,(2,2), activation="relu"))
model.add(BatchNormalization())
 model.add(MaxPooling2D(2,2))
model.add(Dropout(0.3))
model.add(Conv2D(32,(3,3), activation="relu"))
model.add(Conv2D(32,(3,3), activation="relu"))
model.add(BatchNormalization())
 model.add(Dropout(0.3))
model.add(Conv2D(64,(3,3), activation="relu"))
model.add(Conv2D(64,(3,3), activation="relu"))
model.add(BatchNormalization())
 model.add(MaxPooling2D(2,2))
model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(units=1024, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(units=64, activation='relu'))
model.add(Dropout(0.7))
model.add(Dense(units=1, activation='sigmoid'))
model.summary()
adam = Adam(lr=0.001)
 model.compile(optimizer=adam,
loss='binary_crossentropy',
metrics=['acc'])
 return model
def Custom_Conv() :
model = Sequential()
model.add(Conv2D(32,(5,5), activation="relu", input_shape=(32, 32, 3)))
 model.add(BatchNormalization())
 model.add(Conv2D(32,(5,5), activation="relu"))
model.add(BatchNormalization())
 model.add(MaxPooling2D(2,2))
model.add(Dropout(0.3))
model.add(Conv2D(64,(3,3), activation="relu"))
model.add(BatchNormalization())
 model.add(Conv2D(128,(3,3), activation="relu"))
model.add(BatchNormalization())
 model.add(MaxPooling2D(2,2))
model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(units=128, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(units=64, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(units=1, activation='sigmoid'))
adam = Adam(lr=0.001)
 model.compile(optimizer=adam,
loss='binary_crossentropy',
metrics=['acc'])
 return model
def Complex_model() :
model = Sequential()
model.add(Conv2D(3, kernel_size = 3, activation = 'relu', input_shape =(32, 32, 3)))
 model.add(Conv2D(filters = 16, kernel_size = 3, activation = 'relu'))
model.add(Conv2D(filters = 16, kernel_size = 3, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(DepthwiseConv2D(kernel_size = 3, strides = 1, padding = 'Same', use_bias = True))
model.add(Conv2D(filters = 32, kernel_size = 1, activation = 'relu'))
model.add(Conv2D(filters = 64, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(DepthwiseConv2D(kernel_size = 3, strides = 2, padding = 'Same', use_bias = True))
model.add(Conv2D(filters = 128, kernel_size = 1, activation = 'relu'))
model.add(Conv2D(filters = 256, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(DepthwiseConv2D(kernel_size = 3, strides = 1, padding = 'Same', use_bias = True))
model.add(Conv2D(filters = 256, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 512, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(DepthwiseConv2D(kernel_size = 3, strides = 2, padding = 'Same', use_bias = True))
model.add(Conv2D(filters = 512, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 1024, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(DepthwiseConv2D(kernel_size = 3, strides = 1, padding = 'Same', use_bias = True))
model.add(Conv2D(filters = 1024, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 2048, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(470, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(256, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(128, activation = 'tanh'))
model.add(Dense(1, activation = 'sigmoid'))
opt=Adam(0.001)
 model.compile(optimizer = opt, loss = 'mean_squared_error', metrics = ['accuracy'])
 return model
for dat in all_data:
dat['Fam_Size'] = dat['SibSp'] + dat['Parch']<categorify>
models = []
histories =[]
for i in range(len(model)) :
ckpt_path = 'aerial_cactus_detection_'+str(i)+'.hdf5'
earlystop = EarlyStopping(monitor='val_acc', patience=25, verbose=1, restore_best_weights=False)
 reducelr = ReduceLROnPlateau(monitor='val_acc', factor=0.5, patience=3, verbose=1, min_lr=1e-6)
 modelckpt_cb = ModelCheckpoint(ckpt_path, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
 tb = TensorBoard()
callbacks = [earlystop, reducelr, modelckpt_cb, tb]
history = model[i].fit_generator(train_generator,
validation_data=validation_generator,
steps_per_epoch=train_size//batch_size,
validation_steps=validation_size//batch_size,
epochs=80, verbose=1,
shuffle=True,
callbacks=callbacks)
 models.append(model[i])
 histories.append(history )
traindf = pd.get_dummies(traindf)
 traindf.head()<categorify>
test_df = pd.read_csv(os.path.join(base_dir, "sample_submission.csv"))
print(test_df.head())
 test_images = []
images = test_df['id'].values
for image_id in images:
test_images.append(cv2.imread(os.path.join(test_dir, image_id)))
 test_images = np.asarray(test_images)
 test_images = test_images / 255.0
print("Number of Test set images: " + str(len(test_images)) )
testdf = pd.get_dummies(testdf)
 testdf.head()<import_modules>
final_pred =[]
predictcustom = models[0].predict(test_images)
 predictle = models[1].predict(test_images)
 predictcomplex = models[2].predict(test_images)
 stacked_arrays = np.hstack(( predictcustom, predictle, predictcomplex))
pred =np.mean(stacked_arrays, axis=1)
 predweighted = 0.3*predictcustom +0.2*predictle + 0.5*predictcomplex
from sklearn.metrics import confusion_matrix
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score<split>
test_df['has_cactus'] = predweighted
test_df.to_csv('aerial-cactus-submission.csv', index = False )