Kaggle competition: Aerial Cactus Identification
y_pred = model.predict(test_x ).astype(int)
 print('Mean =', y_pred.mean() , ' Std =', y_pred.std())
<predict_on_test>
def load_data(dataframe=None, batch_size=1024, mode='categorical'):
if dataframe is None:
dataframe = pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 dataframe['has_cactus'] = dataframe['has_cactus'].apply(str)
 gen = ImageDataGenerator(rescale=1./255.,
validation_split=0.1, horizontal_flip=True, vertical_flip=True,rotation_range = 3,
width_shift_range = 0.1,height_shift_range = 0.1,shear_range = 0.1)
 trainGen = gen.flow_from_dataframe(dataframe, directory='.. /input/aerial-cactus-identification/train/train', x_col='id', y_col='has_cactus', has_ext=True, target_size=(32, 32),
class_mode=mode, batch_size=batch_size, shuffle=True, subset='training')
 testGen = gen.flow_from_dataframe(dataframe, directory='.. /input/aerial-cactus-identification/train/train', x_col='id', y_col='has_cactus', has_ext=True, target_size=(32, 32),
class_mode=mode, batch_size=batch_size, shuffle=True, subset='validation')
 return trainGen, testGen
train_y_pred = model.predict(train_x ).astype(int)
 diff = sum(abs(train_y-train_y_pred)) *100/len(train_y)
 diff 
<save_to_csv>
def baseline_model() :
model = Sequential()
model.add(Conv2D(32,(3, 3), input_shape=(32, 32, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D())
 model.add(Conv2D(64,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D())
 model.add(Conv2D(128,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(128,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(128,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D())
 model.add(GlobalAveragePooling2D())
 model.add(Dense(2, activation='softmax'))
return model
pd.DataFrame({'Survived': y_pred}, index=testdf.index ).reset_index().to_csv('survived_new.csv', index=False )<prepare_output>
def train_baseline() :
batch_size = 1024
trainGen, valGen = load_data(batch_size=batch_size)
 model = baseline_model()
model.load_weights('.. /input/h5models/baseline.h5')
 opt = Adam(1e-4)
 model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
 cbs = [ReduceLROnPlateau(monitor='loss', factor=0.1, patience=1, min_lr=1e-8, verbose=1)]
model.fit_generator(trainGen, steps_per_epoch=1000, epochs=10, validation_data=valGen,
validation_steps=100, shuffle=True, callbacks=cbs)
 return model
def predict_baseline(model):
testdf = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 pred = np.empty(( testdf.shape[0],))
for n in tqdm(range(testdf.shape[0])) :
data = np.array(Image.open('.. /input/aerial-cactus-identification/test/test/'+testdf.id[n]))
data = data.astype(np.float32)/ 255.
 pred[n] = model.predict(data.reshape(( 1, 32, 32, 3)))[0][1]
testdf['has_cactus'] = pred
testdf.to_csv('sample_submission.csv', index=False )
train_x_all['Pred'] = train_y_pred
train_x_all['Survived'] = train_y<set_options>
model = train_baseline()
model.save('baseline.h5')
 predict_baseline(model )