Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<count_values>
%matplotlib inline
test['Exterior1st'].fillna(test['Exterior1st'].value_counts(dropna=False ).index[0],inplace=True )<count_unique_values>
train_df = pd.read_csv('.. /input/train.csv')
 train_df.head()
x_train,x_test,y_train,y_test = train_test_split(
 train_df['id'],
train_df['has_cactus'],
test_size = 0.2,
random_state = 3 )
train['Exterior1st'].nunique() ,test['Exterior1st'].nunique()<count_values>
X_train = []
for images in tqdm(x_train):
img = plt.imread('.. /input/train/train/' + images)
 X_train.append(img)
 X_test = []
for images in tqdm(x_test):
img = plt.imread('.. /input/train/train/' + images)
 X_test.append(img)
 X_train = np.array(X_train)
 X_test = np.array(X_test)
 X_train = X_train.astype('float32')
 X_test = X_test.astype('float32')
 X_train = X_train/255
X_test = X_test/255
train['Exterior2nd'].value_counts(dropna=False )<count_values>
augmentations = ImageDataGenerator(
 vertical_flip=True,
horizontal_flip=True,
zoom_range=0.1)
 augmentations.fit(X_train )
test['Exterior2nd'].value_counts(dropna=False )<count_values>
best_model_weights = './base.model'
checkpoint = ModelCheckpoint(
 best_model_weights,
monitor='val_loss',
verbose=1,
save_best_only=True,
mode='min',
save_weights_only=False,
period=1 
)
 earlystop = EarlyStopping(
 monitor='val_loss',
min_delta=0.001,
patience=10,
verbose=1,
mode='auto' 
)
 tensorboard = TensorBoard(
 log_dir = './logs',
histogram_freq=0,
batch_size=16,
write_graph=True,
write_grads=True,
write_images=False, 
)
 csvlogger = CSVLogger(
 filename= "training_csv.log",
separator = ",",
append = False 
)
 reduce = ReduceLROnPlateau(
 monitor='val_loss',
factor=0.5,
patience=10,
verbose=1,
mode='auto',
cooldown=1 
)
 callbacks = [checkpoint,tensorboard,csvlogger,reduce]
test['Exterior2nd'].fillna(test['Exterior2nd'].value_counts(dropna=False ).index[0],inplace=True )<count_unique_values>
opt = SGD(lr=1e-4,momentum=0.99)
 opt1 = Adam(lr=1e-3)
 model.compile(
 loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'] 
)
 history = model.fit_generator(
 augmentations.flow(X_train,y_train,batch_size = 16),
steps_per_epoch=150,
validation_steps=150,
validation_data=(X_test,y_test),
epochs = 50,
verbose = 1,
callbacks=callbacks,
)
train['Exterior2nd'].nunique() ,test['Exterior2nd'].nunique()<rename_columns>
show_final_history(history)
 model.load_weights(best_model_weights)
 model_json = model.to_json()
with open("model.json","w")as json_file:
json_file.write(model_json)
 model.save("model.h5")
 print("Weights Saved")
 print("JSON Saved" )
train['MasVnrType'].fillna("None",inplace=True)
 test['MasVnrType'].fillna("None",inplace=True )<data_type_conversions>
train_pred = model.predict(X_train, verbose= 1)
 valid_pred = model.predict(X_test, verbose= 1)
 train_acc = roc_auc_score(np.round(train_pred), y_train)
 valid_acc = roc_auc_score(np.round(valid_pred), y_test)
 confusion_matrix(np.round(valid_pred),y_test )
train['MasVnrArea'].fillna(0,inplace=True)
 test['MasVnrArea'].fillna(0,inplace=True )<groupby>
sample = pd.read_csv('.. /input/sample_submission.csv')
 test = []
for images in tqdm(sample['id']):
img = plt.imread('.. /input/test/test/' + images)
 test.append(img)
 test = np.array(test )
train.groupby('ExterQual')['SalePrice'].agg(['count','min','max','mean'] ).sort_values(by='count' )<groupby>
test = test/255
test_pred = model.predict(test, verbose= 1)
 sample['has_cactus'] = test_pred
sample.head()
sample.to_csv('sub.csv', index= False )
<groupby><EOS> 
!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
!unzip ngrok-stable-linux-amd64.zip
LOG_DIR = './logs'
get_ipython().system_raw(
 'tensorboard --logdir {} --host 0.0.0.0 --port 8080 &' 
.format(LOG_DIR)
)
 get_ipython().system_raw('./ngrok http 8080 &')
 ! curl -s http://localhost:4040/api/tunnels | python3 -c \
"