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
# define the file names | |
mx_outfile = mx_file.replace('.tif', '_byte.tif') | |
sb_outfile = sb_file.replace('.tif', '_byte.tif') | |
# export the files using pyrsgis | |
raster.export(arr_mx, ds_mx, filename=mx_outfile, dtype='uint8') | |
raster.export(arr_sb, ds_sb, filename=sb_outfile, dtype='uint8') |
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
# binarise the array | |
arr_sb = (arr_sb == 1).astype(int) | |
# display the range of values | |
print('Min and max value of single band raster:', arr_sb.min(), arr_sb.max()) |
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('Min and max value of multispectral raster:', arr_mx.min(), arr_mx.max()) | |
print('Min and max value of single band raster:', arr_sb.min(), arr_sb.max()) |
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 import raster | |
# define the file names | |
mx_file = r"D:\210904_ReduceGeotiffSize\l5_Bangalore2011_raw.tif" | |
sb_file = r"D:\210904_ReduceGeotiffSize\l5_Bangalore2011_builtup.tif" | |
# read both the rasters | |
ds_mx, arr_mx = raster.read(mx_file) | |
ds_sb, arr_sb = raster.read(sb_file) |
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 tensorflow as tf | |
# Normalise the features | |
features = features / 255.0 | |
print('New values in input features, min: %d & max: %d' % (features.min(), features.max())) | |
# Transpose the features to channel last format | |
features = tf.transpose(features, [0, 2, 3, 1]) | |
print('Reshaped features:', features.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
import os, glob | |
import numpy as np | |
from pyrsgis import raster | |
# Change the working directory | |
imageDirectory = r"E:\CNN_Builtup\ImageChips" | |
os.chdir(imageDirectory) | |
# Get the number of files in the directory | |
nFiles = len(glob.glob('*.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)) |
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
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
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) |