Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<predict_on_test>
warnings.simplefilter("ignore")
predictions = model.predict(X_test)
 print(predictions.shape)
 predictions<load_from_csv>
print('Train images: %d' %len(os.listdir(
 os.path.join(WORK_DIR, "train_images"))))
submission = pd.read_csv("/kaggle/input/bike-sharing-demand/sampleSubmission.csv")
 print(submission.shape)
 submission.head()<prepare_output>
with open(os.path.join(WORK_DIR, "label_num_to_disease_map.json")) as file:
print(json.dumps(json.loads(file.read()), indent=4))
submission["count"] = predictions
print(submission.shape)
 submission.head()<save_to_csv>
train_labels = pd.read_csv(os.path.join(WORK_DIR, "train.csv"))
train_labels.head()
submission.to_csv("submission.csv", index=False )<save_to_csv>
BATCH_SIZE = 8
STEPS_PER_EPOCH = len(train_labels)*0.8 / BATCH_SIZE
VALIDATION_STEPS = len(train_labels)*0.2 / BATCH_SIZE
EPOCHS = 5
TARGET_SIZE = 512
submission.to_csv("submission.csv", index=False )<load_from_csv>
train_labels.label = train_labels.label.astype('str')
 train_datagen = ImageDataGenerator(validation_split = 0.2,
preprocessing_function = None,
rotation_range = 45,
zoom_range = 0.2,
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.2)
 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" )
train=data=pd.read_csv('/kaggle/input/bike-sharing-demand/train.csv')
 test=pd.read_csv('/kaggle/input/bike-sharing-demand/test.csv')
 train.info()
Y1train=train['casual']
Y2train=train['registered']
Ytrain=train['count'] 
<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()
feature_names=list(test)
 train=train[feature_names]
all_data=pd.concat(( train, test))
print(train.shape, test.shape, all_data.shape)
 print(Ytrain)
 all_data['datetime']=pd.to_datetime(all_data['datetime'])
 all_data['year']=all_data['datetime'].dt.year
all_data['month']=all_data['datetime'].dt.month
all_data['day']=all_data['datetime'].dt.day
all_data['hour']=all_data['datetime'].dt.hour
all_data['dayofweek']=all_data['datetime'].dt.dayofweek
all_data=all_data.drop(columns='datetime')
 all_data.loc[all_data['windspeed']==0, 'windspeed']=all_data['windspeed'].mean()
print(train.shape, test.shape, all_data.shape )<categorify>
classes_to_predict = sorted(train_labels.label.unique())
 dropout_rate = 0.3
def create_model() :
model = models.Sequential()
model.add(EfficientNetB4(include_top = False, weights = None,
input_shape =(TARGET_SIZE, TARGET_SIZE, 3)))
 model.add(layers.GlobalAveragePooling2D())
 model.add(Dropout(dropout_rate))
model.add(Dense(len(classes_to_predict), activation="softmax"))
model.compile(optimizer = Adam(lr = 0.001),
loss = "sparse_categorical_crossentropy",
metrics = ["acc"])
 return model
Xtrain=all_data[:len(train)]
Xtest=all_data[len(train):]
Xtrain.info()
tmpXtrain = copy.deepcopy(Xtrain)
 tmpXtest = copy.deepcopy(Xtest)
 for cmb in itertools.combinations_with_replacement(list(Xtrain.keys()), 2):
tmpXtrain["-".join(cmb)] = Xtrain[cmb[0]] * Xtrain[cmb[1]]
tmpXtest["-".join(cmb)] = Xtest[cmb[0]] * Xtest[cmb[1]] 
<install_modules>
print('Our EfficientNet CNN has %d layers' %len(model.layers))
!pip install optuna<train_model>
model.load_weights('.. /input/cassava-leaf-keras-efficientnetb-baseline/best_baseline_model.h5' )
X_train, X_test, y_train, y_test = train_test_split(tmpXtrain, np.log1p(Y1train), test_size=0.1)
 lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)
 lgbm_params = {
'objective': 'regression',
'metric': 'rmse',
}
best_params, tuning_history = dict() , list()
booster_casual = lgb.train(lgbm_params, lgb_train, valid_sets=lgb_eval,
verbose_eval=0,
best_params=best_params,
tuning_history=tuning_history)
 print("Best Params:", best_params)
 print("Tuning history:", tuning_history)
<train_model>
model_save = ModelCheckpoint('./EffNetB4_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]
)
X_train, X_test, y_train, y_test = train_test_split(tmpXtrain, np.log1p(Y2train), test_size=0.1)
 lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)
 lgbm_params = {
'objective': 'regression',
'metric': 'rmse',
}
best_params, tuning_history = dict() , list()
booster_registered = lgb.train(lgbm_params, lgb_train, valid_sets=lgb_eval,
verbose_eval=0,
best_params=best_params,
tuning_history=tuning_history)
 print("Best Params:", best_params)
 print("Tuning history:", tuning_history )<save_to_csv>
avg_acc = sum(history.history['acc'])/len(history.history['acc'])
 avg_val_acc = sum(history.history['val_acc'])/len(history.history['val_acc'])
 avg_loss = sum(history.history['loss'])/len(history.history['loss'])
 avg_val_loss = sum(history.history['val_loss'])/len(history.history['val_loss'])
 print('Training produced an average accuracy of %.3f and average loss of %.3f' %(avg_acc,avg_loss))
print('Validation produced an average accuracy of %.3f and average loss of %.3f' %(avg_val_acc,avg_val_loss))
pred_casual = booster_casual.predict(tmpXtest, num_iteration=booster_casual.best_iteration)
 pred_casual = np.expm1(pred_casual)
 pred_registered = booster_registered.predict(tmpXtest, num_iteration=booster_registered.best_iteration)
 pred_registered = np.expm1(pred_registered)
 pred = pred_casual + pred_registered
pred[pred<0] = 0
submission = pd.DataFrame({'datetime': test.datetime, 'count': pred},
columns=['datetime', 'count'])
 submission.to_csv("submission.csv", index=False )<set_options>
ss = pd.read_csv(os.path.join(WORK_DIR, "sample_submission.csv"))
ss
%matplotlib inline<load_from_csv>
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
<load_from_csv><EOS> 
ss.to_csv('submission.csv', index = False )