In [2]:
import os
from pyrsgis import raster
from sklearn import cluster
from osgeo import gdal
import geopandas as gdp
import pandas as pd
import numpy as np
import rasterio as rio
import earthpy as et
import earthpy.spatial as es
import earthpy.plot as ep
import matplotlib.pyplot as plt
from glob import glob
from rasterio.plot import show
import pandas as pd
import cv2
Warning! matplotlib_scalebar library not found.

Group Members:

Luis Garcia
Caleb Roudy
Rowan Beswick

Introduction

**The 2019 to 2020 Australian bushfire season, also known as Black summer, was one of the worst natural disasters in modern history. Not only causing havic within some on the wider ecosystem in central- southern Australia, but also affecting human society. By the end of it all, more than 9,000 buildings were vanquished, along with 34 unfortunate lives lost.These damages can be assessed from above with the many commericial domestic and international satelittes and platforms. One of the more prominent platforms from above is Landsat 8, which has been around monitoring most portions of the globe. The images from the satelitte are utilized to monitor and detect surface processes such as wildfire, ice melt, deforestation, as well as others.

Methods

Data used entails (band 1 — band 7) of Landsat 8/OLI Sydney fire as features and try to predict the binary fire/nonfire class. These 2 images will be used for training and testing. Finally, another multispectral Landsat 8 data acquired in the the outside of Newcastle( rural town of Murrurundi) will be used for new predictions. To further prove the effiency o this method, a California fire image will also. Lastly, a Desicion Tree method will also be implemented on the California Fire.

Portions of the electromagnetic spectrum that Landsat 8/OLI covers image This is a graph showing the different ranges and disributions of the electromagnetic spectrum at which each sensors covers. Most areas at which they cover were designed to capture solar radiation signal from the so called "Atmosphereic Window"

Source : https://prd-wret.s3.us-west-2.amazonaws.com/assets/palladium/production/s3fs-public/styles/full_width/public/thumbnails/image/dmidS2LS7Comparison.png

In [2]:
# Path of the file we will be using to idenitfy fire

# Image of the fire in Newcastle( rural town of Murrurundi)
landsat_path = glob("LC08_L1TP_090082_20191231_20200111_01_T1/LC08_L1TP_090082_20191231_20200111_01_T1_B*.TIF")
landsat_path.sort()
array_stack, meta_data = es.stack(landsat_path)

# Image of the fire in the outskirts of Sydney, Australia
landsat_path_2 = glob("LC08_L1TP_090083_20191215_20191226_01_T1/LC08_L1TP_090083_20191215_20191226_01_T1_B*_Refl.TIF")
landsat_path_2.sort()
array_stack_2, meta_data_2 = es.stack(landsat_path_2)
In [3]:
title = ['Aerosol' , 'Blue' , 'Green' , 'Red' , 'NIR' , 'SWIR1' , 'SWIR2']

ep.plot_bands(array_stack,
              title=title)
Out[3]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x000002518571B148>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000025185775C48>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000251857B4048>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x00000251857E5688>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000025185817E48>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000002518584CBC8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000025185883AC8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000251858B9848>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000251858F3B48>]],
      dtype=object)
In [12]:
%matplotlib notebook
# Tr
ep.plot_rgb(array_stack,
            rgb=[5, 4, 2],
            title="Landsat 8 RGB Image\n of the rural town of Murrurundi (image for neaural network)")
plt.figure(figsize=(4,4))

# Adjust the amount of linear stretch to futher brighten the image
ep.plot_rgb(array_stack_2,
            rgb=[5, 4, 2],
            title="Landsat 8 RGB Image\n of outside of Sydney (trained image)")
plt.figure(figsize=(4,4))

Up above are images utilizing the portions of the electromagnetic spectrum that Landsat 8 covers. Notice how cloud/smoke cover dissapates as electromagnetic spectrum shifts to longer wavelengths. This process is explained by the scattering proporties of particles in our atmosphere.
The second set of images here are true color composites, using blue, green, red bands of the spectrum

K Means Cluster; finding ideal number of clusters that the data naturally binds to:

In [6]:
# Run the Kmeans algorithm and get the index of data points clusters
file = 'LC08_L1TP_044032_20181108_20181116_01_T1.tar/LC08_L1TP_044032_20181108_20181116_01_T1/LC08_L1TP_044032_20181108_20181116_01_T1_B7.tif'

dataset = gdal.Open(file)
Band= dataset.GetRasterBand(1)
img = Band.ReadAsArray()
X = img.reshape((-1,1))

sse = []
list_k = list(range(1, 5))

for k in list_k:
    km =cluster.KMeans(n_clusters=k)
    km.fit(X)
    sse.append(km.inertia_)


# Plot sse against k
plt.figure(figsize=(6, 6))
plt.plot(list_k, sse, '-o')
plt.xlabel(r'Number of clusters *k*')
plt.ylabel('Sum of squared distance')
Out[6]:
Text(0, 0.5, 'Sum of squared distance')

Neural Network Classification

This diagram illustrates the theory behind neural netoworks for detecton. In short, features, or bands of an image in this case, are put into the network, as well as a true label of those bands, and ultimately outputs a final map

In [3]:
# Landsat 8/OLI image of Outside of Sydney
southerAus = 'LC08_L1TP_090083_20191215_20191226_01_T1/LC08_20191215_1226_LayersStacked_ALL.tif' 
# 'LC08_L1TP_090083_20191215_20191226_01_T1/LC08_20191215_1226_LayersStacked_ALL.tif'

# Binary Classification of fire non fire ground truth of Sydney (produced from K-means)
fire_Binary = 'LC08_L1TP_090083_20191215_20191226_01_T1_NEW/min_distance_fire_nonfire.tif' 

# Outside of Newcastle in the rural town of Murrurundi 
eastAus = 'LC08_L1TP_090082_20191231_20200111_01_T1/LC08_20191231_20200111_01_T1_Stacked.tif' 

#LC08_L1TP_091086_20191222_20200110_01_T1 (Victoria, Australia- outside of the town of Bumberrah)

# Read the rasters as array
ds7, featuresSouthAus = raster.read(southerAus, bands='all')
ds6, labelsAus = raster.read(fire_Binary, bands=1)
ds5, featuresEastAus = raster.read(eastAus, bands='all')
In [13]:
print("Fire b7 image shape: ", featuresSouthAus.shape)
print("Fire b6 image shape: ", labelsAus.shape)
print("Fire b5 image shape: ", featuresEastAus.shape)
Fire b7 image shape:  (7, 7691, 7621)
Fire b6 image shape:  (7691, 7621)
Fire b5 image shape:  (7, 7701, 7641)
In [4]:
from pyrsgis.convert import changeDimension

featuresSouthAus = changeDimension(featuresSouthAus)
labelsAus  = changeDimension(labelsAus)
featuresEastAus = changeDimension(featuresEastAus)
nBands = featuresSouthAus.shape[1]
labelsAus= (labelsAus == 1).astype(int)

print("Souther Aus multispectral image shape: ", featuresSouthAus.shape)
print("Southern Aus Fire/nonFire binary shape: ", labelsAus.shape)
print("Victoria multispectral image shape: ", featuresEastAus.shape)
Souther Aus multispectral image shape:  (58613111, 7)
Southern Aus Fire/nonFire binary shape:  (58613111,)
Victoria multispectral image shape:  (58843341, 7)
In [6]:
from sklearn.model_selection import train_test_split

                              #X (Indepent Varibles/Data)  #y(Dependent/Labels (Fire/NonFire))
xTrain, xTest, yTrain, yTest = train_test_split(featuresSouthAus, labelsAus, test_size=0.4, random_state=42)

print(xTrain.shape)
print(yTrain.shape)

print(xTest.shape)
print(yTest.shape)
(35167866, 7)
(35167866,)
(23445245, 7)
(23445245,)
In [7]:
# Reshape the data
xTrain = xTrain.reshape((xTrain.shape[0], 1, xTrain.shape[1]))
xTest = xTest.reshape((xTest.shape[0], 1, xTest.shape[1]))
featuresEastAus = featuresEastAus.reshape((featuresEastAus.shape[0], 1, featuresEastAus.shape[1]))

# Print the shape of reshaped data
print(xTrain.shape, xTest.shape, featuresEastAus.shape)
(35167866, 1, 7) (23445245, 1, 7) (58843341, 1, 7)
In [8]:
from tensorflow import keras

# Define the parameters of the model
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(1, nBands)),
    keras.layers.Dense(14, activation='relu'),
    keras.layers.Dense(2, activation='softmax')])

# Define the accuracy metrics and parameters
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])

# Run the model
model.fit(xTrain, yTrain, epochs=2)
Train on 35167866 samples
Epoch 1/2
35167866/35167866 [==============================] - 1111s 32us/sample - loss: 0.3852 - accuracy: 0.9999
Epoch 2/2
35167866/35167866 [==============================] - 921s 26us/sample - loss: 0.0028 - accuracy: 0.9999
Out[8]:
<tensorflow.python.keras.callbacks.History at 0x18a11511108>
In [15]:
np.unique(xTest)
Out[15]:
array([    0,   968,  4436, ..., 65531, 65533, 65535])
In [12]:
yTestPredicted = model.predict(xTest)
Out[12]:
array([[1., 0.],
       [1., 0.],
       [1., 0.],
       ...,
       [1., 0.],
       [1., 0.],
       [1., 0.]], dtype=float32)
In [13]:
np.unique(yTestPredicted)
Out[13]:
array([0.00000000e+00, 1.43727778e-38, 1.76250865e-38, 2.22284094e-38,
       2.30213187e-38, 3.11031394e-38, 3.34968403e-38, 3.91820931e-38,
       3.98203818e-38, 4.21756702e-38, 4.37972472e-38, 4.57770717e-38,
       9.83168267e-38, 1.06747957e-37, 1.27007404e-37, 1.47308676e-37,
       1.70485177e-37, 1.83511939e-37, 2.01736106e-37, 3.93437256e-37,
       4.01647296e-37, 4.06291894e-37, 5.71950170e-37, 5.86049833e-37,
       6.36379183e-37, 6.63671185e-37, 7.25194914e-37, 7.85146319e-37,
       8.51033624e-37, 1.12532257e-36, 1.40030478e-36, 1.85312193e-36,
       1.96809836e-36, 2.06000165e-36, 2.75138644e-36, 2.99415358e-36,
       3.32047702e-36, 3.54259337e-36, 4.12480961e-36, 5.50289091e-36,
       6.55922045e-36, 6.78858247e-36, 7.43767504e-36, 8.17592194e-36,
       9.59676343e-36, 1.14824934e-35, 1.34008580e-35, 1.42146195e-35,
       1.67439341e-35, 2.13171926e-35, 2.80185708e-35, 3.12674724e-35,
       7.20387134e-35, 8.80437348e-35, 9.89985501e-35, 1.00985009e-34,
       1.07653919e-34, 1.79408230e-34, 1.83517110e-34, 1.91030081e-34,
       1.91780687e-34, 2.33644563e-34, 2.37047068e-34, 2.57990360e-34,
       2.68169292e-34, 3.07689917e-34, 3.15419412e-34, 3.18157763e-34,
       3.26147749e-34, 3.97524731e-34, 4.33435899e-34, 5.39946708e-34,
       5.57920200e-34, 5.87736568e-34, 6.63092644e-34, 7.91272267e-34,
       8.19972972e-34, 9.06437010e-34, 9.41129797e-34, 9.78926349e-34,
       1.14552062e-33, 1.28314704e-33, 1.31419745e-33, 1.35921861e-33,
       1.59215806e-33, 1.71576386e-33, 1.81003832e-33, 1.82658818e-33,
       2.29684837e-33, 2.60491422e-33, 2.61798424e-33, 2.84153517e-33,
       2.90925319e-33, 2.94959396e-33, 3.76338851e-33, 5.78234304e-33,
       6.13974769e-33, 7.19937172e-33, 8.04049303e-33, 1.01635908e-32,
       1.31625040e-32, 1.33709044e-32, 1.45754540e-32, 1.60740933e-32,
       1.68705862e-32, 1.75840378e-32, 1.82112786e-32, 1.86306333e-32,
       2.16331897e-32, 2.99415926e-32, 3.66522019e-32, 4.00273224e-32,
       4.39372723e-32, 4.87976681e-32, 5.75003289e-32, 5.86035656e-32,
       5.88908623e-32, 6.01918230e-32, 9.60198329e-32, 1.19581594e-31,
       1.27015538e-31, 1.42925126e-31, 1.70263410e-31, 1.76076065e-31,
       1.79149300e-31, 2.01749037e-31, 2.07927859e-31, 2.28369283e-31,
       2.34370651e-31, 2.70321453e-31, 3.12639532e-31, 4.14964552e-31,
       4.37017720e-31, 4.90415914e-31, 5.52661349e-31, 5.63587475e-31,
       5.65258276e-31, 6.89037643e-31, 7.29503284e-31, 7.90311183e-31,
       8.93874210e-31, 9.39556460e-31, 1.29537042e-30, 1.31318217e-30,
       1.70084986e-30, 1.73505787e-30, 1.80515213e-30, 1.80628192e-30,
       1.90552995e-30, 1.92153059e-30, 2.06928590e-30, 2.37966268e-30,
       2.83096881e-30, 3.69709938e-30, 4.44833958e-30, 4.50537908e-30,
       5.51013297e-30, 6.63321795e-30, 9.02429910e-30, 9.09237169e-30,
       9.72771266e-30, 1.23826530e-29, 1.31658888e-29, 1.31673950e-29,
       1.34735847e-29, 1.38152176e-29, 1.65743094e-29, 1.66054462e-29,
       2.00047893e-29, 2.07300780e-29, 2.32603813e-29, 4.92290805e-29,
       8.14367019e-29, 9.39607655e-29, 9.45720797e-29, 9.54178639e-29,
       1.25382313e-28, 1.29457159e-28, 1.31191194e-28, 1.31729780e-28,
       1.65079978e-28, 1.79358810e-28, 1.83174944e-28, 2.48214622e-28,
       2.54029149e-28, 2.69808822e-28, 2.96703712e-28, 2.99623397e-28,
       2.99931017e-28, 4.33414067e-28, 4.74724687e-28, 5.32370707e-28,
       7.50797404e-28, 8.05869178e-28, 9.17604026e-28, 1.02329819e-27,
       1.08346858e-27, 1.24016214e-27, 2.60004780e-27, 3.67892833e-27,
       3.71807940e-27, 4.50296326e-27, 8.18972435e-27, 9.84885445e-27,
       1.01897487e-26, 1.71818034e-26, 1.96801059e-26, 2.17630407e-26,
       2.28597330e-26, 2.51270133e-26, 3.75540007e-26, 3.77555300e-26,
       4.03232291e-26, 5.39290213e-26, 5.55611622e-26, 6.34668735e-26,
       6.85968360e-26, 7.43357251e-26, 7.84644629e-26, 8.58082895e-26,
       9.04178626e-26, 9.48442659e-26, 1.31734496e-25, 1.58728268e-25,
       1.78955845e-25, 1.98188890e-25, 3.04061407e-25, 3.25399873e-25,
       3.67956355e-25, 4.07501976e-25, 5.02478414e-25, 5.72543018e-25,
       6.12788630e-25, 7.02739678e-25, 8.14741657e-25, 8.57648196e-25,
       1.12970788e-24, 1.88525490e-24, 2.02677713e-24, 2.51084486e-24,
       3.60525960e-24, 7.09993905e-24, 9.74430730e-24, 1.37987218e-23,
       1.98404687e-23, 2.16697109e-23, 2.33613773e-23, 2.60181946e-23,
       2.67569944e-23, 3.29557593e-23, 3.40304371e-23, 3.50764098e-23,
       3.51084029e-23, 5.12498917e-23, 5.18247441e-23, 1.32867211e-22,
       1.54931562e-22, 1.92920617e-22, 2.08751377e-22, 2.13824093e-22,
       2.62912799e-22, 3.01280419e-22, 3.04624912e-22, 3.26077612e-22,
       3.52935889e-22, 3.73832676e-22, 3.80270513e-22, 4.23879526e-22,
       8.64435547e-22, 1.09580025e-21, 1.12563156e-21, 1.20833134e-21,
       1.80449269e-21, 2.00025055e-21, 2.19290205e-21, 2.98813968e-21,
       3.12659004e-21, 3.16743027e-21, 3.46971652e-21, 4.25801802e-21,
       4.46139459e-21, 5.94115205e-21, 6.39945898e-21, 7.26197369e-21,
       9.47450654e-21, 1.01301732e-20, 1.01671443e-20, 1.04712923e-20,
       1.29914305e-20, 1.32769565e-20, 1.72149856e-20, 1.90684130e-20,
       2.58536997e-20, 2.61913057e-20, 2.67717377e-20, 2.77342189e-20,
       2.93514507e-20, 3.14829012e-20, 3.15259243e-20, 4.81090862e-20,
       5.04413639e-20, 5.08852917e-20, 5.42398936e-20, 5.69990450e-20,
       9.24420088e-20, 1.32479218e-19, 1.45585468e-19, 1.64550729e-19,
       1.69049028e-19, 1.74319422e-19, 2.22545861e-19, 2.68667927e-19,
       4.72935604e-19, 5.47780508e-19, 5.77942874e-19, 6.45174977e-19,
       6.55537430e-19, 6.62741139e-19, 9.45783216e-19, 1.18926848e-18,
       1.22805932e-18, 1.76214307e-18, 1.76977567e-18, 1.92943083e-18,
       2.47271747e-18, 2.57261277e-18, 2.98687909e-18, 3.64882603e-18,
       3.90200534e-18, 5.23985789e-18, 5.49203548e-18, 6.43545524e-18,
       6.65216126e-18, 7.49390480e-18, 9.40606265e-18, 1.32841195e-17,
       1.54417415e-17, 1.80184108e-17, 2.11761992e-17, 2.15187033e-17,
       2.18021946e-17, 2.25824824e-17, 2.31024051e-17, 2.72346901e-17,
       2.77478084e-17, 3.24922401e-17, 5.31841830e-17, 5.80418011e-17,
       9.46484277e-17, 1.24345654e-16, 1.42557021e-16, 1.43926899e-16,
       1.83072645e-16, 2.60813832e-16, 6.33339346e-16, 6.41025005e-16,
       7.73524569e-16, 8.08904606e-16, 8.83490675e-16, 8.93294816e-16,
       9.84019562e-16, 1.10980374e-15, 1.29726950e-15, 1.39869651e-15,
       1.85267514e-15, 3.81368665e-15, 4.37969143e-15, 4.44547243e-15,
       5.38635282e-15, 6.04521316e-15, 6.78694551e-15, 7.13788142e-15,
       9.30415633e-15, 1.11154143e-14, 1.12862363e-14, 1.14831384e-14,
       1.28438367e-14, 1.65739902e-14, 2.52103077e-14, 2.81366879e-14,
       3.10295612e-14, 3.33436180e-14, 3.57608840e-14, 3.85721592e-14,
       4.12087627e-14, 4.20423787e-14, 4.43019611e-14, 5.44288803e-14,
       7.42685671e-14, 8.12830773e-14, 9.28274655e-14, 9.97612027e-14,
       1.18209343e-13, 1.28098853e-13, 1.58081149e-13, 1.76571363e-13,
       2.08842709e-13, 2.54749157e-13, 2.83802143e-13, 3.26035558e-13,
       4.37845181e-13, 4.95669695e-13, 6.24840403e-13, 6.36178176e-13,
       6.47279593e-13, 6.82376410e-13, 7.56746445e-13, 9.04988703e-13,
       1.03682677e-12, 1.18018366e-12, 1.21825770e-12, 1.44009555e-12,
       1.57610036e-12, 1.60086842e-12, 2.12820126e-12, 2.17397953e-12,
       2.36202572e-12, 2.39586562e-12, 2.40406414e-12, 2.85252334e-12,
       2.90329132e-12, 3.41521559e-12, 3.61230230e-12, 4.68187598e-12,
       5.32135447e-12, 9.52299524e-12, 9.68902562e-12, 1.20183473e-11,
       1.24074926e-11, 1.74901951e-11, 1.86836033e-11, 2.17095213e-11,
       3.08019964e-11, 3.59538059e-11, 3.70335290e-11, 3.74617073e-11,
       3.75726950e-11, 4.62561170e-11, 4.77048748e-11, 6.40794293e-11,
       7.55672747e-11, 7.62150273e-11, 8.01923181e-11, 1.01620753e-10,
       1.02655398e-10, 1.18854315e-10, 1.24187702e-10, 1.78171838e-10,
       1.83334584e-10, 1.99351619e-10, 1.99987998e-10, 2.09135598e-10,
       2.12035139e-10, 2.78215645e-10, 3.51918356e-10, 3.53966828e-10,
       4.46413462e-10, 5.10398057e-10, 5.34412237e-10, 5.47611623e-10,
       6.80537016e-10, 7.02411407e-10, 8.19335544e-10, 8.51949844e-10,
       9.25169275e-10, 1.38365597e-09, 1.52061108e-09, 2.25852870e-09,
       2.70651035e-09, 3.60624952e-09, 4.19555635e-09, 4.33139657e-09,
       4.76968331e-09, 5.67104674e-09, 7.39215222e-09, 8.38652436e-09,
       8.82316531e-09, 1.61110894e-08, 2.16830696e-08, 2.19312160e-08,
       3.05203400e-08, 3.10776329e-08, 3.56169529e-08, 4.02473752e-08,
       4.39233148e-08, 4.83129625e-08, 5.05802014e-08, 6.09794952e-08,
       7.02693370e-08, 7.50212124e-08, 8.23777171e-08, 9.38213418e-08,
       1.04248073e-07, 1.26715420e-07, 1.65454310e-07, 1.69784187e-07,
       2.29990462e-07, 2.87658622e-07, 3.41785892e-07, 3.45107736e-07,
       3.53535427e-07, 3.62828501e-07, 3.91263427e-07, 4.87811349e-07,
       5.10878806e-07, 5.55385554e-07, 6.99562179e-07, 8.99195527e-07,
       1.06463006e-06, 1.09860105e-06, 1.14090312e-06, 1.36424558e-06,
       1.49427626e-06, 1.52902646e-06, 1.56404622e-06, 1.88218576e-06,
       2.10688609e-06, 2.11288852e-06, 2.53688268e-06, 3.25304791e-06,
       3.35531536e-06, 3.77474112e-06, 4.25142161e-06, 4.98004420e-06,
       5.02329294e-06, 5.16238561e-06, 5.81431095e-06, 6.26640758e-06,
       6.59792249e-06, 7.16212344e-06, 7.86980763e-06, 1.07908818e-05,
       1.08156401e-05, 1.14502109e-05, 1.41494702e-05, 1.94281638e-05,
       2.21773844e-05, 2.38719131e-05, 2.45356605e-05, 2.54818024e-05,
       3.43588035e-05, 3.62877727e-05, 3.70872585e-05, 5.10243844e-05,
       5.21752299e-05, 5.58810825e-05, 9.91160341e-05, 1.05301559e-04,
       1.30879460e-04, 1.70865431e-04, 2.71033903e-04, 3.34335607e-04,
       4.23685298e-04, 4.72680724e-04, 5.72275894e-04, 8.63071415e-04,
       8.91295262e-04, 9.14390665e-04, 1.31646439e-03, 1.66576344e-03,
       5.69134997e-03, 5.88185433e-03, 8.53935536e-03, 8.97946302e-03,
       8.99771508e-03, 9.97979939e-03, 1.25747118e-02, 1.26327788e-02,
       1.51957264e-02, 1.58530809e-02, 2.01276038e-02, 2.25290600e-02,
       2.41416916e-02, 2.66729705e-02, 3.06999553e-02, 3.37536670e-02,
       4.20273915e-02, 4.65364717e-02, 5.07689044e-02, 5.45894019e-02,
       7.09813014e-02, 9.07974839e-02, 9.36941877e-02, 9.43047851e-02,
       9.52420756e-02, 9.53597948e-02, 9.82854068e-02, 9.94848087e-02,
       1.43743038e-01, 1.47194624e-01, 1.55930459e-01, 1.58674493e-01,
       1.60690159e-01, 1.89814344e-01, 2.00519696e-01, 2.05323696e-01,
       2.21984431e-01, 2.29149818e-01, 2.30862960e-01, 2.53033549e-01,
       2.53124356e-01, 2.59846956e-01, 2.68764108e-01, 2.90675104e-01,
       3.08509409e-01, 3.14415395e-01, 3.15746963e-01, 3.16799730e-01,
       3.40296239e-01, 3.55521411e-01, 3.75075430e-01, 3.80473047e-01,
       4.21585381e-01, 4.58865225e-01, 5.41134715e-01, 5.78414559e-01,
       6.19526923e-01, 6.24924600e-01, 6.44478559e-01, 6.59703732e-01,
       6.83200240e-01, 6.84253037e-01, 6.85584605e-01, 6.91490531e-01,
       7.09324837e-01, 7.31235802e-01, 7.40153015e-01, 7.46875644e-01,
       7.46966362e-01, 7.69137025e-01, 7.70850182e-01, 7.78015614e-01,
       7.94676304e-01, 7.99480379e-01, 8.10185671e-01, 8.39309871e-01,
       8.41325521e-01, 8.44069481e-01, 8.52805376e-01, 8.56256962e-01,
       9.00515199e-01, 9.01714563e-01, 9.04640198e-01, 9.04757977e-01,
       9.05695260e-01, 9.06305850e-01, 9.09202576e-01, 9.29018736e-01,
       9.45410550e-01, 9.49231148e-01, 9.53463495e-01, 9.57972646e-01,
       9.66246367e-01, 9.69299972e-01, 9.73326981e-01, 9.75858331e-01,
       9.77470994e-01, 9.79872406e-01, 9.84146953e-01, 9.84804273e-01,
       9.87367272e-01, 9.87425268e-01, 9.90020156e-01, 9.91002262e-01,
       9.91020501e-01, 9.91460681e-01, 9.94118094e-01, 9.94308650e-01,
       9.98334229e-01, 9.98683512e-01, 9.99085665e-01, 9.99108732e-01,
       9.99136984e-01, 9.99427736e-01, 9.99527335e-01, 9.99576271e-01,
       9.99665618e-01, 9.99728978e-01, 9.99829054e-01, 9.99869108e-01,
       9.99894738e-01, 9.99900818e-01, 9.99944091e-01, 9.99947786e-01,
       9.99948978e-01, 9.99962926e-01, 9.99963760e-01, 9.99965668e-01,
       9.99974489e-01, 9.99975443e-01, 9.99976158e-01, 9.99977827e-01,
       9.99980569e-01, 9.99985814e-01, 9.99988556e-01, 9.99989152e-01,
       9.99992132e-01, 9.99992847e-01, 9.99993443e-01, 9.99993682e-01,
       9.99994159e-01, 9.99994874e-01, 9.99994993e-01, 9.99995708e-01,
       9.99996185e-01, 9.99996662e-01, 9.99996781e-01, 9.99997497e-01,
       9.99997854e-01, 9.99998093e-01, 9.99998450e-01, 9.99998689e-01,
       9.99998808e-01, 9.99998927e-01, 9.99999046e-01, 9.99999285e-01,
       9.99999404e-01, 9.99999523e-01, 9.99999642e-01, 9.99999762e-01,
       9.99999881e-01, 1.00000000e+00], dtype=float32)
In [20]:
from sklearn.metrics import confusion_matrix, precision_score, recall_score

# Predict for test data 
yTestPredicted = model.predict(xTest)
yTestPredicted = yTestPredicted[:,1]

# Calculate and display the error metrics
yTestPredicted = (yTestPredicted>0.5).astype(int)
cMatrix = confusion_matrix(yTest, yTestPredicted)
pScore = precision_score(yTest, yTestPredicted)
rScore = recall_score(yTest, yTestPredicted)

print("Confusion matrix: for 14 nodes\n", cMatrix)
print("\nP-Score: %.3f, R-Score: %.3f" % (pScore, rScore))
Confusion matrix: for 14 nodes
 [[23438087     2235]
 [      24     4899]]

P-Score: 0.687, R-Score: 0.995
In [26]:
landsat_path = glob("LC08_L1TP_090082_20191231_20200111_01_T1/LC08_L1TP_090082_20191231_20200111_01_T1_B*.TIF")
landsat_path.sort()
array_stack, meta_data = es.stack(landsat_path)

print(len(landsat_path))
7
In [28]:
# Adjust the amount of linear stretch to futher brighten the image

ep.plot_rgb(array_stack,
            rgb=[5, 4, 2],
            title="Landsat RGB Image\n Linear Stretch Applied")
plt.show()
In [24]:
predicted = model.predict(featuresEastAus)
predicted = predicted[:,1]
#Export raster
prediction = np.reshape(predicted, (ds5.RasterYSize, ds5.RasterXSize))
outFile = 'Eastern Australia Fire Prediction.tif'
raster.export(prediction, ds5, filename=outFile, dtype='float')

Final output

In [41]:
# Read File Raster Data as Array using Gdal
img_file = "Eastern Australia Fire Prediction.tif"
gtif = gdal.Open(img_file)
georaster = gtif.ReadAsArray()

 # Plot image using matplotlib
plt.figure(figsize=(10,10))
plt.imshow(georaster)
Out[41]:
<matplotlib.image.AxesImage at 0x2c9001c1d88>

Demonstration 2

In [41]:
# Landsat 8/OLI image of Outside of Sydney
southerAus = 'LC08_L1TP_090083_20191215_20191226_01_T1/LC08_20191215_1226_LayersStacked_ALL.tif' 
# 'LC08_L1TP_090083_20191215_20191226_01_T1/LC08_20191215_1226_LayersStacked_ALL.tif'

# Binary Classification of fire non fire ground truth of Sydney (produced from K-means)
fire_Binary = 'LC08_L1TP_090083_20191215_20191226_01_T1_NEW/min_distance_fire_nonfire.tif' 

# Outside of Newcastle in the rural town of Murrurundi 
cal_fire = 'LC08_L1TP_044032_20181108_20181116_01_T1.tar/LC08_L1TP_044032_20181108_20181116_01_T1/LC08_L1TP_044032_20181108_20181116_01_T1_LayerStacked.tif' 

#LC08_L1TP_091086_20191222_20200110_01_T1 (Victoria, Australia- outside of the town of Bumberrah)

# Read the rasters as array
ds1, ftSouthAus = raster.read(southerAus, bands='all')
ds2, labelsAus = raster.read(fire_Binary, bands=1)
ds3, ftCal = raster.read(cal_fire, bands='all')
In [33]:
landsat_path_3 = glob("LC08_L1TP_044032_20181108_20181116_01_T1.tar/LC08_L1TP_044032_20181108_20181116_01_T1/LC08_L1TP_044032_20181108_20181116_01_T1_B*.TIF")
landsat_path_3.sort()
array_stack_3, meta_data_3 = es.stack(landsat_path_3)

ep.plot_rgb(array_stack_3,
            rgb=[5, 4, 2],
            title="Landsat 8 RGB Image\California Fire(image for neaural network)")
plt.figure(figsize=(4,4))
Out[33]:
<Figure size 288x288 with 0 Axes>
<Figure size 288x288 with 0 Axes>
In [43]:
print("Fire b7 image shape: ", ftSouthAus.shape)
print("Fire b6 image shape: ", labelsAus.shape)
print("Fire b5 image shape: ", ftCal.shape)
Fire b7 image shape:  (7, 7691, 7621)
Fire b6 image shape:  (7691, 7621)
Fire b5 image shape:  (7, 7781, 7651)
In [44]:
from pyrsgis.convert import changeDimension

ftSouthAus = changeDimension(ftSouthAus)
labelsAus = changeDimension(labelsAus)
ftCal = changeDimension(ftCal)
nBands = ftSouthAus.shape[1]
labelsAus= (labelsAus == 1).astype(int)

print("Souther Aus multispectral image shape: ", ftSouthAus.shape)
print("Southern Aus Fire/nonFire binary shape: ", labelsAus.shape)
print("California FIre multispectral image shape: ", ftCal.shape)
Souther Aus multispectral image shape:  (58613111, 7)
Southern Aus Fire/nonFire binary shape:  (58613111,)
California FIre multispectral image shape:  (59532431, 7)
In [45]:
from sklearn.model_selection import train_test_split

                              #X (Indepent Varibles/Data)  #y(Dependent/Labels (Fire/NonFire))
xTrain, xTest, yTrain, yTest = train_test_split(ftSouthAus, labelsAus, test_size=0.4, random_state=42)

print(xTrain.shape)
print(yTrain.shape)

print(xTest.shape)
print(yTest.shape)
(35167866, 7)
(35167866,)
(23445245, 7)
(23445245,)
In [46]:
# Reshape the data
xTrain = xTrain.reshape((xTrain.shape[0], 1, xTrain.shape[1]))
xTest = xTest.reshape((xTest.shape[0], 1, xTest.shape[1]))
ftCal= ftCal.reshape((ftCal.shape[0], 1, ftCal.shape[1]))

# Print the shape of reshaped data
print(xTrain.shape, xTest.shape, ftCal.shape)
(35167866, 1, 7) (23445245, 1, 7) (59532431, 1, 7)
In [47]:
from tensorflow import keras

# Define the parameters of the model
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(1, nBands)),
    keras.layers.Dense(14, activation='relu'),
    keras.layers.Dense(2, activation='softmax')])

# Define the accuracy metrics and parameters
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])

# Run the model
model.fit(xTrain, yTrain, epochs=2)
Train on 35167866 samples
Epoch 1/2
35167866/35167866 [==============================] - 925s 26us/sample - loss: 0.0542 - accuracy: 1.0000
Epoch 2/2
35167866/35167866 [==============================] - 924s 26us/sample - loss: 0.0013 - accuracy: 1.0000
Out[47]:
<tensorflow.python.keras.callbacks.History at 0x1d47f89cf88>
In [48]:
from sklearn.metrics import confusion_matrix, precision_score, recall_score

# Predict for test data 
yTestPredicted = model.predict(xTest)
yTestPredicted = yTestPredicted[:,1]

# Calculate and display the error metrics
yTestPredicted = (yTestPredicted>0.5).astype(int)
cMatrix = confusion_matrix(yTest, yTestPredicted)
pScore = precision_score(yTest, yTestPredicted)
rScore = recall_score(yTest, yTestPredicted)

print("Confusion matrix: for 14 nodes\n", cMatrix)
print("\nP-Score: %.3f, R-Score: %.3f" % (pScore, rScore))
Confusion matrix: for 14 nodes
 [[23438509     1813]
 [      31     4892]]

P-Score: 0.730, R-Score: 0.994
In [49]:
predicted = model.predict(ftCal)
predicted = predicted[:,1]
#Export raster
prediction = np.reshape(predicted, (ds3.RasterYSize, ds3.RasterXSize))
outFile = 'California Fire Prediction.tif'
raster.export(prediction, ds3, filename=outFile, dtype='float')

Output #2

In [59]:
img_file = "California Fire Prediction.tif"
gtif = gdal.Open(img_file)
georaster = gtif.ReadAsArray()    
plt.figure(figsize=(7,7))
plt.imshow(georaster)
Out[59]:
<matplotlib.image.AxesImage at 0x1d488351708>