Kaggle competition: Aerial Cactus Identification
x_train = x_train/255.0
x_test = x_test/255.0<define_search_space>
!pip install -U efficientnet
batch_size = 64
num_classes = 10
epochs = 20
input_shape =(28, 28, 1 )<split>
train_dir = '.. /input/train/train'
test_dir = '.. /input/test'
train_df = pd.read_csv('.. /input/train.csv')
 train_df.head()
y_train = keras.utils.to_categorical(y_train, num_classes)
 X_train, X_val, Y_train, Y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state=42 )<choose_model_class>
cactus = []
cactus.append(cv2.imread(train_dir + '/' + train_df['id'][0]))
cactus.append(cv2.imread(train_dir + '/' + train_df['id'][1]))
cactus.append(cv2.imread(train_dir + '/' + train_df['id'][2]))
cactus.append(cv2.imread(train_dir + '/' + train_df['id'][6]))
cactus.append(cv2.imread(train_dir + '/' + train_df['id'][7]))
cactus.append(cv2.imread(train_dir + '/' + train_df['id'][11]))
labels = ['cactus','cactus','cactus','no cactus','no cactus',' no cactus']
plt.figure(figsize=[10,10])
 for x in range(0,6):
plt.subplot(3, 3,x+1)
 plt.imshow(cactus[x])
 plt.title(labels[x])
 x += 1
plt.show()
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal',input_shape=input_shape))
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal'))
model.add(MaxPool2D(( 2, 2)))
 model.add(Dropout(0.20))
model.add(Conv2D(64,(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(Conv2D(64,(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(128,(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.RMSprop() ,
metrics=['accuracy'])
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.0001)
 datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=15,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False )<train_model>
from keras import applications
from efficientnet import EfficientNetB3
from keras import callbacks
from keras.models import Sequential
datagen.fit(X_train)
 h = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 1, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction], )<compute_test_metric>
train_df['has_cactus'] = train_df['has_cactus'].astype('str' )
final_loss, final_acc = model.evaluate(X_val, Y_val, verbose=0)
 print("Final loss: {0:.6f}, final accuracy: {1:.6f}".format(final_loss, final_acc))<init_hyperparams>
train_datagen = ImageDataGenerator(
 rescale=1/255,
validation_split=0.10,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest' 
)
 train_generator = train_datagen.flow_from_dataframe(
 dataframe = train_df,
directory = train_dir,
x_col="id",
y_col="has_cactus",
target_size=(32,32),
subset="training",
batch_size=1024,
shuffle=True,
class_mode="binary" 
)
 val_generator = train_datagen.flow_from_dataframe(
 dataframe = train_df,
directory = train_dir,
x_col="id",
y_col="has_cactus",
target_size=(32,32),
subset="validation",
batch_size=256,
shuffle=True,
class_mode="binary"
)
layer_outputs = [layer.output for layer in model.layers[:8]]
activation_model = models.Model(input=model.input, output=layer_outputs)
 activations = activation_model.predict(test_im.reshape(1,28,28,1))
first_layer_activation = activations[0]
plt.matshow(first_layer_activation[0, :, :, 4], cmap='viridis' )<choose_model_class>
efficient_net = EfficientNetB3(
 weights='imagenet',
input_shape=(32,32,3),
include_top=False,
pooling='max' 
)
 model = Sequential()
model.add(efficient_net)
 model.add(Dense(units = 120, activation='relu'))
model.add(Dense(units = 120, activation = 'relu'))
model.add(Dense(units = 1, activation='sigmoid'))
model.summary()
model.layers[:-1]<define_variables>
model.compile(optimizer=Adam(lr=0.0001), loss='binary_crossentropy', metrics=['accuracy'] )
layer_names = []
for layer in model.layers[:-1]:
layer_names.append(layer.name)
 images_per_row = 16
for layer_name, layer_activation in zip(layer_names, activations):
if layer_name.startswith('conv'):
n_features = layer_activation.shape[-1]
size = layer_activation.shape[1]
n_cols = n_features // images_per_row
display_grid = np.zeros(( size * n_cols, images_per_row * size))
for col in range(n_cols):
for row in range(images_per_row):
channel_image = layer_activation[0,:, :, col * images_per_row + row]
channel_image -= channel_image.mean()
channel_image /= channel_image.std()
channel_image *= 64
channel_image += 128
channel_image = np.clip(channel_image, 0, 255 ).astype('uint8')
 display_grid[col * size :(col + 1)* size,
row * size :(row + 1)* size] = channel_image
scale = 1./ size
plt.figure(figsize=(scale * display_grid.shape[1],
scale * display_grid.shape[0]))
plt.title(layer_name)
 plt.grid(False)
 plt.imshow(display_grid, aspect='auto', cmap='viridis' )<define_variables>
history = model.fit_generator(
 train_generator,
epochs = 50,
steps_per_epoch = 15,
validation_data = val_generator,
validation_steps = 7
)
layer_names = []
for layer in model.layers[:-1]:
layer_names.append(layer.name)
 images_per_row = 16
for layer_name, layer_activation in zip(layer_names, activations):
if layer_name.startswith('max'):
n_features = layer_activation.shape[-1]
size = layer_activation.shape[1]
n_cols = n_features // images_per_row
display_grid = np.zeros(( size * n_cols, images_per_row * size))
for col in range(n_cols):
for row in range(images_per_row):
channel_image = layer_activation[0,:, :, col * images_per_row + row]
channel_image -= channel_image.mean()
channel_image /= channel_image.std()
channel_image *= 64
channel_image += 128
channel_image = np.clip(channel_image, 0, 255 ).astype('uint8')
 display_grid[col * size :(col + 1)* size,
row * size :(row + 1)* size] = channel_image
scale = 1./ size
plt.figure(figsize=(scale * display_grid.shape[1],
scale * display_grid.shape[0]))
plt.title(layer_name)
 plt.grid(False)
 plt.imshow(display_grid, aspect='auto', cmap='viridis' )<define_variables>
preds = model.predict_generator(
 test_generator,
steps=len(test_generator.filenames)
 )
layer_names = []
for layer in model.layers[:-1]:
layer_names.append(layer.name)
 images_per_row = 16
for layer_name, layer_activation in zip(layer_names, activations):
if layer_name.startswith('drop'):
n_features = layer_activation.shape[-1]
size = layer_activation.shape[1]
n_cols = n_features // images_per_row
display_grid = np.zeros(( size * n_cols, images_per_row * size))
for col in range(n_cols):
for row in range(images_per_row):
channel_image = layer_activation[0,:, :, col * images_per_row + row]
channel_image -= channel_image.mean()
channel_image /= channel_image.std()
channel_image *= 64
channel_image += 128
channel_image = np.clip(channel_image, 0, 255 ).astype('uint8')
 display_grid[col * size :(col + 1)* size,
row * size :(row + 1)* size] = channel_image
scale = 1./ size
plt.figure(figsize=(scale * display_grid.shape[1],
scale * display_grid.shape[0]))
plt.title(layer_name)
 plt.grid(False)
 plt.imshow(display_grid, aspect='auto', cmap='viridis' )<predict_on_test>
image_ids = [name.split('/')[-1] for name in test_generator.filenames]
predictions = preds.flatten()
data = {'id': image_ids, 'has_cactus':predictions}
submission = pd.DataFrame(data)
 print(submission.head() )
Y_pred = model.predict(X_val)
 Y_pred_classes = np.argmax(Y_pred, axis = 1)
 Y_true_classes = np.argmax(Y_val, axis = 1 )<compute_test_metric>
submission.to_csv("submission.csv", index=False )
target_names = ["Class {}".format(i)for i in range(num_classes)]
print(classification_report(Y_true_classes, Y_pred_classes, target_names=target_names))<save_to_csv>
submission.to_csv("submission.csv", index=False )