Skip to content

Instantly share code, notes, and snippets.

View PratyushTripathy's full-sized avatar

Pratyush Tripathy PratyushTripathy

  • University of California, Santa Barbara
View GitHub Profile
# 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')
# 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())
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())
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)
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)
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'))
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))
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'
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')
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)