Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<rename_columns>
warnings.simplefilter("ignore")
df.rename(columns={'count':'rentals'},inplace=True )<concatenate>
print('Train images: %d' %len(os.listdir(
 os.path.join(WORK_DIR, "train_images"))))
df = df.append(test, sort=False )<feature_engineering>
with open(os.path.join(WORK_DIR, "label_num_to_disease_map.json")) as file:
print(json.dumps(json.loads(file.read()), indent=4))
for col in ['rentals','registered','casual']:
df[col] = np.log(df[col]+1 )<feature_engineering>
train_labels = pd.read_csv(os.path.join(WORK_DIR, "train.csv"))
train_labels.head()
df['year'] = df.datetime.dt.year
df['month'] = df.datetime.dt.month
df['day'] = df.datetime.dt.day
df['dayofweek'] = df.datetime.dt.dayofweek
df['hour'] = df['datetime'].dt.hour<rename_columns>
BATCH_SIZE = 8
STEPS_PER_EPOCH = len(train_labels)*0.7 / BATCH_SIZE
VALIDATION_STEPS = len(train_labels)*0.3 / BATCH_SIZE
EPOCHS = 20
TARGET_SIZE = 512
df.set_index('datetime', inplace=True )<sort_values>
train_labels.label = train_labels.label.astype('str')
 train_datagen = ImageDataGenerator(featurewise_center=True,
featurewise_std_normalization=True,
validation_split = 0.3,
preprocessing_function = None,
rotation_range = 45,
zoom_range = 0.3,
horizontal_flip = True,
vertical_flip = True,
fill_mode = 'nearest',
shear_range = 0.1,
height_shift_range = 0.1,
width_shift_range = 0.1 
)
 train_generator = train_datagen.flow_from_dataframe(train_labels,
directory = os.path.join(WORK_DIR, "train_images"),
subset = "training",
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse")
 validation_datagen = ImageDataGenerator(validation_split = 0.3)
 validation_generator = validation_datagen.flow_from_dataframe(train_labels,
directory = os.path.join(WORK_DIR, "train_images"),
subset = "validation",
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse" )
df.sort_index(inplace=True )<feature_engineering>
generator = train_datagen.flow_from_dataframe(train_labels.iloc[20:21],
directory = os.path.join(WORK_DIR, "train_images"),
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse")
 aug_images = [generator[0][0][0]/255 for i in range(10)]
fig, axes = plt.subplots(2, 5, figsize =(20, 10))
axes = axes.flatten()
for img, ax in zip(aug_images, axes):
ax.imshow(img)
 ax.axis('off')
 plt.tight_layout()
plt.show()
df['rolling_temp'] = df['temp'].rolling(4, min_periods=1 ).mean()<split>
test = df[df['rentals'].isnull() ]
df = df[~df['rentals'].isnull() ]<define_variables>
def create_model() :
conv_base = EfficientNetB0(include_top = False, weights = None,
input_shape =(TARGET_SIZE, TARGET_SIZE, 3))
model = conv_base.output
model = layers.GlobalAveragePooling2D()(model)
 model = layers.Dense(5, activation = "softmax" )(model)
 model = models.Model(conv_base.input, model)
 model.compile(optimizer = Adam(lr = 0.001),
loss = "sparse_categorical_crossentropy",
metrics = ["acc"])
 return model
removed_cols = ['rentals', 'casual','registered','datetime']
feats = [c for c in df.columns if c not in removed_cols]<split>
print('Our EfficientNet CNN has %d layers' %len(model.layers))
train, valid = train_test_split(df, random_state=42 )<choose_model_class>
model.load_weights('.. /input/cassava-leaf-disease-models/basic_EfNetB0_imagenet_512.h5' )
rf = RandomForestRegressor(random_state=42, n_estimators=100,n_jobs=-1 )<train_model>
model_save = ModelCheckpoint('./EffNetB0_512_8_best_weights.h5',
save_best_only = True,
save_weights_only = True,
monitor = 'val_loss',
mode = 'min', verbose = 1)
 early_stop = EarlyStopping(monitor = 'val_loss', min_delta = 0.001,
patience = 5, mode = 'min', verbose = 1,
restore_best_weights = True)
 reduce_lr = ReduceLROnPlateau(monitor = 'val_loss', factor = 0.3,
patience = 2, min_delta = 0.001,
mode = 'min', verbose = 1)
 history = model.fit(
 train_generator,
steps_per_epoch = STEPS_PER_EPOCH,
epochs = EPOCHS,
validation_data = validation_generator,
validation_steps = VALIDATION_STEPS,
callbacks = [model_save, early_stop, reduce_lr]
)
rf.fit(train[feats],train['rentals'] )<predict_on_test>
model.save('./EffNetB0_512_8.h5' )
preds = rf.predict(valid[feats] )<compute_test_metric>
def all_activations_vis(img, layers = 10):
layer_outputs = [layer.output for layer in model.layers[:layers]]
activation_model = models.Model(inputs = model.input, outputs = layer_outputs)
 activations = activation_model.predict(img)
 layer_names = []
for layer in model.layers[:layers]:
layer_names.append(layer.name)
 images_per_row = 3
for layer_name, layer_activation in zip(layer_names, activations):
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 * 5 * display_grid.shape[1],
scale * 5 * display_grid.shape[0]))
plt.title(layer_name)
 plt.grid(False)
 plt.axis('off')
 plt.imshow(display_grid, aspect = 'auto', cmap = 'viridis' )
mean_squared_error(valid['rentals'],preds)**(1/2 )<predict_on_test>
ss = pd.read_csv(os.path.join(WORK_DIR, "sample_submission.csv"))
ss
preds_test = rf.predict(test[feats] )<prepare_output>
preds = []
for image_id in ss.image_id:
image = Image.open(os.path.join(WORK_DIR, "test_images", image_id))
image = image.resize(( TARGET_SIZE, TARGET_SIZE))
image = np.expand_dims(image, axis = 0)
 preds.append(np.argmax(model.predict(image)))
 ss['label'] = preds
ss
<save_to_csv><EOS> 
ss.to_csv('submission.csv', index = False )