def prediction(): #recalling data data = forclassification() #recalling trained model model = training() a = model.predict(data) print(a.shape) #reshaping the prediction or classification result to original image size clmap = np.reshape(a, (img2.shape[1], img2.shape[2])) #converting array into TIF map output = "landslide_detection_map.tif" raster.export(clmap, ds, filename=output, dtype="int") return (clmap, clmap.shape)
def exportTifChip(self, labelType, featureWindow, labelWindow=None, featureFile='x.tif', labelFile='y.tif', ds=None): if labelType == 'label': raster.export(featureWindow, ds, filename=featureFile, bands='all') if labelType == 'mask': raster.export(featureWindow, ds, filename=featureFile, bands='all') raster.export(labelWindow, ds, filename=labelFile)
#predicted=np.argmax(predicted, axis=1) #print("distribution de predicted") #unique, counts = np.unique(predicted, return_counts=True) #print(np.asarray((unique, counts/predicted.size)).T) #print("distribution de predicted filter") #unique, counts = np.unique(predicted_filter, return_counts=True) #print(np.asarray((unique, counts/predicted_filter.size)).T) #Export raster #prediction = np.reshape(predicted, (ds1.RasterYSize, ds1.RasterXSize)) outFile_1 = 'data_predicted_v24_randomforest_nofilter_Seredou_2017.tif' raster.export(prediction_1,ds1, filename=outFile_1, dtype='float') #â—„prediction_filter = np.reshape(predicted_filter, (ds1.RasterYSize, ds1.RasterXSize-1)) outFile_2 = 'data_predicted_v24_randomforest_nofilter_Seredou_2015.tif' raster.export(prediction_2, ds1, filename=outFile_2, dtype='float') #enregistrement modele from joblib import dump,load dump(model, "randomforest_v24_from_seredou2015&2017.sav") #----------------------- ## test sur DIECKE 2017 #from joblib import dump,load #model=load("random_forest_median_filter_v22_from_seredou2015&2017.sav")
ds2, tempArr2 = raster.read(RasterFile2) nBands1, rows1, cols1 = ds1.RasterCount, ds1.RasterXSize, ds1.RasterYSize features1 = np.empty((nBands1, cols1, rows1)) features2 = np.empty((nBands1, cols1, rows1)) features1[:, :, :] = tempArr1 features2[:, :, :] = tempArr2 features_result1 = np.empty((cols1, rows1)) features_result2 = np.empty((cols1, rows1)) for i in range(0, cols1): for j in range(0, rows1): list1 = [] list2 = [] for k in range(0, nBands1): list1.append(features1[k, i, j]) list2.append(features2[k, i, j]) r, p_value = pearsonr(list1, list2) features_result1[i, j] = r features_result2[i, j] = p_value outFile1 = 'd:/aa.tif' outFile2 = 'd:/bb.tif' raster.export(features_result1, ds1, filename=outFile1, dtype='float', bands='all') raster.export(features_result2, ds1, filename=outFile2, dtype='float', bands='all')
features = np.empty((rows * cols, kSize, kSize, nBands)) n = 0 for row in range(margin, rows + margin): for col in range(margin, cols + margin): feat = mxBands[:, row - margin:row + margin + 1, col - margin:col + margin + 1] b1, b2, b3, b4, b5, b6 = feat feat = np.dstack((b1, b2, b3, b4, b5, b6)) features[n, :, :, :] = feat n += 1 return (features) # Call the function to generate features tensor new_features = CNNdataGenerator(featuresHyderabad, kSize=11) print('Shape of the new features', new_features.shape) # Predict new data and export the probability raster newPredicted = model.predict(new_features) newPredicted = newPredicted[:, 1] prediction = np.reshape(newPredicted, (ds.RasterYSize, ds.RasterXSize)) raster.export(prediction, ds, filename=outFile, dtype='float') plt.imshow(prediction) plt.show()