<jupyter_start><jupyter_text>Dependencies<jupyter_code>from utillity_script_cloud_segmentation import *
seed = 0
seed_everything(seed)
warnings.filterwarnings("ignore")
base_path = '/content/drive/My Drive/Colab Notebooks/[Kaggle] Understanding Clouds from Satellite Images/'
data_path = base_path + 'Data/'
model_base_path = base_path + 'Models/files/classification/'
model_path = model_base_path + '19-EfficientNetB0_224x224_Cyclical_triangular.h5'
hold_out_set_path = data_path + 'hold-out.csv'
train_images_dest_path = 'train_images/'<jupyter_output><empty_output><jupyter_text>Load data<jupyter_code>hold_out_set = pd.read_csv(hold_out_set_path)
X_val = hold_out_set[hold_out_set['set'] == 'validation']
print('Validation samples: ', len(X_val)) 

# Preprocecss data
label_columns=['Fish', 'Flower', 'Gravel', 'Sugar']
for label in label_columns:
  X_val[label].replace({0: 1, 1: 0}, inplace=True)

display(X_val.head())<jupyter_output>Validation samples:  1105<jupyter_text>Model parameters<jupyter_code>HEIGHT = 224
WIDTH = 224
BETA1 = 0.25
BETA2 = 0.5
BETA3 = 1
TTA_STEPS = 8<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>model = load_model(model_path)<jupyter_output><empty_output><jupyter_text>Classification data generator<jupyter_code>datagen=ImageDataGenerator(rescale=1./255.,
                           vertical_flip=True,
                           horizontal_flip=True,
                           zoom_range=[1, 1.2],
                           fill_mode='constant',
                           cval=0.)

valid_generator=datagen.flow_from_dataframe(
                            dataframe=X_val,
                            directory=train_images_dest_path,
                            x_col="image",
                            y_col=label_columns,
                            target_size=(HEIGHT, WIDTH),
                            batch_size=1,
                            class_mode="other",
                            shuffle=False,
                            seed=seed)<jupyter_output>Found 1105 validated image filenames.<jupyter_text>Classification threshold and mask size tunning<jupyter_code>valid_preds = apply_tta(model, valid_generator, steps=TTA_STEPS)
print('BETA1')
best_tresholds1 = classification_tunning(valid_generator.labels, valid_preds, label_columns, beta=BETA1)
print('BETA2')
best_tresholds2 = classification_tunning(valid_generator.labels, valid_preds, label_columns, beta=BETA2)
print('BETA3')
best_tresholds3 = classification_tunning(valid_generator.labels, valid_preds, label_columns, beta=BETA3)<jupyter_output>BETA1
Fish treshold=0.75 Score=0.828
Flower treshold=0.80 Score=0.943
Gravel treshold=0.76 Score=0.781
Sugar treshold=0.73 Score=0.828
BETA2
Fish treshold=0.64 Score=0.777
Flower treshold=0.52 Score=0.907
Gravel treshold=0.61 Score=0.744
Sugar treshold=0.59 Score=0.733
BETA3
Fish treshold=0.46 Score=0.784
Flower treshold=0.29 Score=0.891
Gravel treshold=0.49 Score=0.777
Sugar treshold=0.40 Score=0.697<jupyter_text>Metrics (beta 0.25)<jupyter_code>get_metrics_classification(X_val, valid_preds, label_columns, best_tresholds1)<jupyter_output>Metrics for: Fish
              precision    recall  f1-score   support

           0       0.65      0.92      0.76       555
           1       0.87      0.49      0.63       550

    accuracy                           0.71      1105
   macro avg       0.76      0.71      0.69      1105
weighted avg       0.76      0.71      0.69      1105

Metrics for: Flower
              precision    recall  f1-score   support

           0       0.65      0.98      0.78       473
           1       0.98      0.61      0.75       632

    accuracy                           0.77      1105
   macro avg       0.81      0.79      0.77      1105
weighted avg       0.84      0.77      0.76      1105

Metrics for: Gravel
              precision    recall  f1-score   support

           0       0.63      0.93      0.75       587
           1       0.83      0.39      0.53       518

    accuracy                           0.68      1105
   macro avg       0.73      0.66      0.64      1105
weighted avg    [...]<jupyter_text>Metrics (beta 0.5)<jupyter_code>get_metrics_classification(X_val, valid_preds, label_columns, best_tresholds2)<jupyter_output>Metrics for: Fish
              precision    recall  f1-score   support

           0       0.73      0.83      0.78       555
           1       0.80      0.70      0.74       550

    accuracy                           0.76      1105
   macro avg       0.77      0.76      0.76      1105
weighted avg       0.77      0.76      0.76      1105

Metrics for: Flower
              precision    recall  f1-score   support

           0       0.81      0.91      0.86       473
           1       0.93      0.84      0.88       632

    accuracy                           0.87      1105
   macro avg       0.87      0.87      0.87      1105
weighted avg       0.88      0.87      0.87      1105

Metrics for: Gravel
              precision    recall  f1-score   support

           0       0.77      0.78      0.77       587
           1       0.75      0.73      0.74       518

    accuracy                           0.76      1105
   macro avg       0.76      0.76      0.76      1105
weighted avg    [...]<jupyter_text>Metrics (beta 1)<jupyter_code>get_metrics_classification(X_val, valid_preds, label_columns, best_tresholds3)<jupyter_output>Metrics for: Fish
              precision    recall  f1-score   support

           0       0.84      0.64      0.73       555
           1       0.71      0.88      0.78       550

    accuracy                           0.76      1105
   macro avg       0.78      0.76      0.75      1105
weighted avg       0.78      0.76      0.75      1105

Metrics for: Flower
              precision    recall  f1-score   support

           0       0.89      0.79      0.84       473
           1       0.85      0.93      0.89       632

    accuracy                           0.87      1105
   macro avg       0.87      0.86      0.86      1105
weighted avg       0.87      0.87      0.87      1105

Metrics for: Gravel
              precision    recall  f1-score   support

           0       0.86      0.66      0.75       587
           1       0.70      0.88      0.78       518

    accuracy                           0.76      1105
   macro avg       0.78      0.77      0.76      1105
weighted avg    [...]
StarcoderdataJupyterStructuredCleanDedup