<jupyter_start><jupyter_text>If you plan on using this implementation, please cite our work:@INPROCEEDINGS{Grabowski2021IGARSS,author={Grabowski, <NAME> <NAME>},booktitle={IGARSS 2021 - 2021 IEEE International Geoscienceand Remote Sensing Symposium},title={Towards Robust Cloud Detection inSatellite Images Using U-Nets},year={2021},note={in press}}  Demo of the cloud detection using U-Net architectureThis document presents the cloud detection on example Landsat 8 multispectral images using trained U-Net model. The full script can be found in cloud_detection/exp_main.py. First, we import neccessary libraries.<jupyter_code>import numpy as np
from pathlib import Path
from tensorflow import keras

from cloud_detection.models import unet
from cloud_detection.evaluate_L8CCA import evaluate_model
from cloud_detection.losses import (
    JaccardIndexLoss,
    JaccardIndexMetric,
    DiceCoefMetric,
    recall,
    precision,
    specificity,
)<jupyter_output><empty_output><jupyter_text>Next, we set the parameters for the experiment.These parameters are the following:- dpath - path to the dataset.- rpath - path to directory where results should be stored.- mpath - path to trained model weights.- vids - tuple of IDs of images which should be used to create visualizations. If contains '*' visualizations will be created for all images in the datasets.- eval_imgs - IDs of images to evaluate.- batch_size - size of generated batches, only one batch is loaded to memory at a time.- thr - threshold for determining whether pixels contain the clouds.- learning_rate - learning rate for training (needed to load the trained model).- bn_momentum - momentum of the batch normalization layer.<jupyter_code>dpath = Path("datasets/clouds/Landsat-Cloud-Cover-Assessment-Validation-Data-Partial")
rpath = Path("artifacts/cloud_detection_demo/")
mpath = "examples/cloud_model_weights/best_weights"
vids = ["*"]
eval_imgs = ["LC81390292014135LGN00", "LC80460282014171LGN00"]
batch_size = 8
thr = 0.5
learning_rate = 0.01
bn_momentum = 0.9<jupyter_output><empty_output><jupyter_text>We create the instance of the untrained U-Net model. Next, we load the trained weights into the model.<jupyter_code>model = unet(input_size=4, bn_momentum=bn_momentum)
model.compile(
    optimizer=keras.optimizers.Adam(lr=learning_rate),
    loss=JaccardIndexLoss(),
    metrics=[
        keras.metrics.binary_crossentropy,
        keras.metrics.binary_accuracy,
        JaccardIndexLoss(),
        JaccardIndexMetric(),
        DiceCoefMetric(),
        recall,
        precision,
        specificity,
    ],
)
model.load_weights(mpath)<jupyter_output><empty_output><jupyter_text>We create the directory to store the results of the model evaluation. Next, we evaluate the model using example Landsat 8 images. The following files are created for each image:- gt.png - image of the ground-truth cloud mask.- pred.png - image of the model prediction.- masks.png - visualisation of the model prediction. Yellow color denotes True Positives, red color denotes False Positives and purple color stands for False Negatives.- unc.png - uncertainty map, where pixels with uncertain prediction scores are marked in yellow (Note: In the case of the tested model, in most cases almost all of the pixels' prediction scores are very low or very high, which means that the map will almost always not include any yellow pixels.).If the model's prediction Jaccard Index Metric does not exceed 0.6, the following files are also created:- roc.html - ROC curve.- prec_recall.html - precision-recall curve.- activation_hist.html - histogram of the model's activations scores (please note the logarithmic scale).<jupyter_code>rpath.mkdir(parents=True, exist_ok=True)
metrics_L8CCA, _ = evaluate_model(
    model=model,
    thr=thr,
    dpath=dpath,
    rpath=rpath / "eval_vis",
    vids=vids,
    batch_size=batch_size,
    img_ids=eval_imgs
)<jupyter_output>Processing Barren-LC81390292014135LGN00
Scene prediction took 80.25445532798767 seconds
Average inference time: 80.25445532798767 seconds
Creating visualisation for LC81390292014135LGN00<jupyter_text>Finally, we process the output metrics to obtain the mean metrics for the model evaluation.<jupyter_code>mean_metrics_L8CCA = {}
for key, value in metrics_L8CCA.items():
    mean_metrics_L8CCA[key] = np.mean(list(value.values()))
print(mean_metrics_L8CCA)<jupyter_output>{'L8CCA_binary_crossentropy': 2.502019, 'L8CCA_binary_accuracy': 0.8431798, 'L8CCA_jaccard_index_loss': 0.33403495, 'L8CCA_jaccard_index_metric': 0.665965, 'L8CCA_dice_coeff_metric': 0.78108644, 'L8CCA_recall': 0.95701694, 'L8CCA_precision': 0.6854487, 'L8CCA_specificity': 0.8053814, 'L8CCA_normalized_mutual_info_score': 0.4949737413470268, 'L8CCA_adjusted_rand_score': 0.5242536788935748}
{'fraction_non_alphanumeric': 0.07869401423189619, 'fraction_numerical': 0.05023022185014651, 'mean_word_length': 5.279894875164257, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '486963', 'license': 'MIT', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1421, 'n_words': 464, 'path': 'beetles/examples/cloud_detection.ipynb', 'repo_name': 'ESA-PhiLab/hypernet', 'size': '4827'}