This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from pyrsgis import raster | |
os.chdir("E:\\yourDirectoryName") | |
mxBangalore = 'l5_Bangalore2011_raw.tif' | |
builtupBangalore = 'l5_Bangalore2011_builtup.tif' | |
mxHyderabad = 'l5_Hyderabad2011_raw.tif' | |
# Read the rasters as array | |
ds1, featuresBangalore = raster.read(mxBangalore, bands='all') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("Bangalore multispectral image shape: ", featuresBangalore.shape) | |
print("Bangalore binary built-up image shape: ", labelBangalore.shape) | |
print("Hyderabad multispectral image shape: ", featuresHyderabad.shape) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyrsgis.convert import changeDimension | |
featuresBangalore = changeDimension(featuresBangalore) | |
labelBangalore = changeDimension (labelBangalore) | |
featuresHyderabad = changeDimension(featuresHyderabad) | |
nBands = featuresBangalore.shape[1] | |
labelBangalore = (labelBangalore == 1).astype(int) | |
print("Bangalore multispectral image shape: ", featuresBangalore.shape) | |
print("Bangalore binary built-up image shape: ", labelBangalore.shape) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.model_selection import train_test_split | |
xTrain, xTest, yTrain, yTest = train_test_split(featuresBangalore, labelBangalore, test_size=0.4, random_state=42) | |
print(xTrain.shape) | |
print(yTrain.shape) | |
print(xTest.shape) | |
print(yTest.shape) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Normalise the data | |
xTrain = xTrain / 255.0 | |
xTest = xTest / 255.0 | |
featuresHyderabad = featuresHyderabad / 255.0 | |
# Reshape the data | |
xTrain = xTrain.reshape((xTrain.shape[0], 1, xTrain.shape[1])) | |
xTest = xTest.reshape((xTest.shape[0], 1, xTest.shape[1])) | |
featuresHyderabad = featuresHyderabad.reshape((featuresHyderabad.shape[0], 1, featuresHyderabad.shape[1])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
predicted = model.predict(featuresHyderabad) | |
predicted = predicted[:,1] | |
#Export raster | |
prediction = np.reshape(predicted, (ds.RasterYSize, ds.RasterXSize)) | |
outFile = 'Hyderabad_2011_BuiltupNN_predicted.tif' | |
raster.export(prediction, ds3, filename=outFile, dtype='float') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from pyrsgis import raster | |
# Change the directory | |
os.chdir("E:\\BuiltUpPrediction") | |
# Assign file names | |
mxBangalore = 'l5_Bangalore2011_raw.tif' | |
builtupBangalore = 'l5_Bangalore2011_builtup.tif' | |
mxHyderabad = 'l5_Hyderabad2011_raw.tif' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, cv2 | |
from pyrsgis import raster | |
import numpy as np | |
inFile = r"E:\CNN_Builtup\l5_Bangalore2011_raw.tif" | |
normFile = inFile.replace('.tif', '_normalised.tif') | |
histEqlFile = inFile.replace('.tif', '_histEqualised.tif') | |
ds, arr = raster.read(inFile) | |
normBands = np.random.randint(1, size = (ds.RasterCount, ds.RasterYSize, ds.RasterXSize)) |
OlderNewer