<jupyter_start><jupyter_text>Code used to georefence and evaluate the results<jupyter_code># import libraries
import numpy as np
import tensorflow as  tf
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from datetime import datetime
from packaging import version

%tensorflow_version 2.x
from tensorflow import keras
from tensorflow.keras.models import *
from tensorflow.keras.layers import *
from tensorflow.keras.optimizers import *
from sklearn.model_selection import train_test_split

import numpy as np
import pandas as pd

print("TensorFlow version: ", tf.__version__)
assert version.parse(tf.__version__).release[0] >= 2, \
    "This notebook requires TensorFlow 2.0 or above."
# Install segmentation_models
pip install segmentation_models
import segmentation_models as sm
# Load data
X_test = np.load("/content/drive/My Drive/Mestrado/artigo/artigo_final/data/test/test_with_terrain/area_1/arrays/X_test_area_1.npy")
Y_test = np.load("/content/drive/My Drive/Mestrado/artigo/artigo_final/data/test/test_with_terrain/area_1/arrays/Y_test_area_1.npy")
original_image_path = "/content/drive/My Drive/Mestrado/artigo/artigo_final/data/test/test_with_terrain/area_1/images/test_area_1.tif"
# Define TP metric
def true_positive(y_true, y_pred):
    y_pred_pos = tf.keras.backend.round(tf.keras.backend.clip(y_pred, 0, 1))
    y_pred_neg = 1 - y_pred_pos

    y_pos = tf.keras.backend.round(tf.keras.backend.clip(y_true, 0, 1))
    y_neg = 1 - y_pos

    tp = tf.keras.backend.sum(y_pos * y_pred_pos)


    return tp * (0.005**2)
# Define TN metrics
def true_negative(y_true, y_pred):
    y_pred_pos = tf.keras.backend.round(tf.keras.backend.clip(y_pred, 0, 1))
    y_pred_neg = 1 - y_pred_pos

    y_pos = tf.keras.backend.round(tf.keras.backend.clip(y_true, 0, 1))
    y_neg = 1 - y_pos

    
    tn = tf.keras.backend.sum(y_neg * y_pred_neg)


    return tn * (0.005**2)
# Define FP metrics
def false_positve(y_true, y_pred):
    y_pred_pos = tf.keras.backend.round(tf.keras.backend.clip(y_pred, 0, 1))
    y_pred_neg = 1 - y_pred_pos

    y_pos = tf.keras.backend.round(tf.keras.backend.clip(y_true, 0, 1))
    y_neg = 1 - y_pos

   
    fp = tf.keras.backend.sum(y_neg * y_pred_pos)
   

    return fp * (0.005**2)
# Define FN metrics
def false_negative(y_true, y_pred):
    y_pred_pos = tf.keras.backend.round(tf.keras.backend.clip(y_pred, 0, 1))
    y_pred_neg = 1 - y_pred_pos

    y_pos = tf.keras.backend.round(tf.keras.backend.clip(y_true, 0, 1))
    y_neg = 1 - y_pos

    fn = tf.keras.backend.sum(y_pos * y_pred_neg)



    return fn * (0.005**2)
# Network Metrics
metrics = [sm.metrics.Precision(threshold=0.5),sm.metrics.Recall(threshold=0.5),sm.metrics.FScore(threshold=0.5,beta=1),sm.metrics.IOUScore(threshold=0.5),matthews_correlation,true_positive,true_negative,false_positve,false_negative]
# Define model Architecture
def Unet(lr,filtersFirstLayer, pretrained_weights = None,input_size = (32,32,6)):
    inputs = Input(input_size)
    
    conv1 = Conv2D(filtersFirstLayer, 3, activation = 'relu', padding = 'same')(inputs)
    conv1 = Conv2D(filtersFirstLayer, 3, activation = 'relu', padding = 'same')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
   
    conv2 = Conv2D(filtersFirstLayer*2, 3, activation = 'relu', padding = 'same')(pool1)
    conv2 = Conv2D(filtersFirstLayer*2, 3, activation = 'relu', padding = 'same')(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)


    conv3 = Conv2D(filtersFirstLayer*4, 3, activation = 'relu', padding = 'same')(pool2)
    conv3 = Conv2D(filtersFirstLayer*4, 3, activation = 'relu', padding = 'same')(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

 
    conv4 = Conv2D(filtersFirstLayer*8, 3, activation = 'relu', padding = 'same')(pool3)
    conv4 = Conv2D(filtersFirstLayer*8, 3, activation = 'relu', padding = 'same')(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

  
    conv5 = Conv2D(filtersFirstLayer*16, 3, activation = 'relu', padding = 'same')(pool4)
    conv5 = Conv2D(filtersFirstLayer*16, 3, activation = 'relu', padding = 'same')(conv5)

    # Up 1 
    up6 = Conv2D(filtersFirstLayer*8, 2, activation = 'relu', padding = 'same')(UpSampling2D(size = (2,2))(conv5))
    merge6 = concatenate([conv4,up6], axis = 3)
    conv6 = Conv2D(filtersFirstLayer*8, 3, activation = 'relu', padding = 'same')(merge6)
    conv6 = Conv2D(filtersFirstLayer*8, 3, activation = 'relu', padding = 'same')(conv6)

    # Up 2 
    up7 = Conv2D(filtersFirstLayer*4, 2, activation = 'relu', padding = 'same')(UpSampling2D(size = (2,2))(conv6))
    merge7 = concatenate([conv3,up7], axis = 3)
    conv7 = Conv2D(filtersFirstLayer*4, 3, activation = 'relu', padding = 'same')(merge7)
    conv7 = Conv2D(filtersFirstLayer*4, 3, activation = 'relu', padding = 'same')(conv7)

    # Up 3 
    up8 = Conv2D(filtersFirstLayer*2, 2, activation = 'relu', padding = 'same')(UpSampling2D(size = (2,2))(conv7))
    merge8 = concatenate([conv2,up8], axis = 3)
    conv8 = Conv2D(filtersFirstLayer*2, 3, activation = 'relu', padding = 'same')(merge8)
    conv8 = Conv2D(filtersFirstLayer*2, 3, activation = 'relu', padding = 'same')(conv8)

    # Up 4 
    up9 = Conv2D(filtersFirstLayer, 2, activation = 'relu', padding = 'same')(UpSampling2D(size = (2,2))(conv8))
    merge9 = concatenate([conv1,up9], axis = 3)
    conv9 = Conv2D(filtersFirstLayer, 3, activation = 'relu', padding = 'same')(merge9)
    conv9 = Conv2D(filtersFirstLayer, 3, activation = 'relu', padding = 'same')(conv9)
    conv9 = Conv2D(2, 3, activation = 'relu', padding = 'same')(conv9)

    # output
    conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv9)

    model = Model(inputs, conv10)

    model.compile(optimizer = Adam(lr = lr), loss = 'binary_crossentropy', metrics = metrics)
    
    model.summary()

    if(pretrained_weights):
    	model.load_weights(pretrained_weights)

    return model

model = Unet(0.0001,32,input_size=(1024,1024,6))
# Load model weights
model.load_weights("/content/drive/My Drive/Mestrado/artigo/artigo_final/results/augmented_terrain/64_64/random/model/unet/unet_random_size_64_filters_32_batch_size_16_lr_0.001.hdf5")
# Evaluate the model 
model.evaluate(X_test,Y_test)
# Make the prediction over the test areas and save as .tif

# import rasterio
import rasterio


# Predict
preds_train = model.predict(X_test, verbose=1)
# Assign the value one for predictions with results > 0.5
preds_train_t = (preds_train > 0.5).astype(np.uint8)
# Open the original image
dataset = rasterio.open(original_image_path)
# Get the metadata from the image
meta = dataset.meta
# Change the metadata to 1 (Result image will have just one channel)
meta["count"] = 1
# Nodata values = 0
meta["nodata"] = 0
# data type = uint8 (8bits)
meta["dtype"] = "uint8"
# Ajust array dimensions
save = np.squeeze(preds_train_t, axis=(0,3))
save = np.expand_dims(save,axis = 0)
# Save .tif image
with rasterio.open("area_2_32_predict.tif", 'w', **meta) as dst:
    dst.write(save)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13554987212276215, 'fraction_numerical': 0.04120488775220233, 'mean_word_length': 4.62669864108713, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '524468', 'license': 'MIT', 'n_tokens_mistral': 2790, 'n_tokens_neox': 2598, 'n_words': 538, 'path': 'Landslide_Segmentation_with_U-Net_Landslide_Segmentation_with_U-Net:_Evaluating_Different_Sampling_Methods_and_Patch_Sizes/notebooks/Georeferencing_the_results.ipynb', 'repo_name': 'SPAMLab/data_sharing', 'size': '7038'}