├── ANN_CA.py ├── Accuracy_Kappa.py ├── Features3D_CNN.py ├── FoM.py ├── LR_CA.py ├── PST_CA.py ├── README.md ├── RF_CA.py ├── SOM_cluster.py ├── SVM_CA.py └── k-means_cluster.py /ANN_CA.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import random 3 | from osgeo import gdal 4 | from sklearn import preprocessing 5 | from keras.models import Sequential 6 | from keras.utils import np_utils 7 | from keras.optimizers import RMSprop, Adam 8 | from keras.layers import Dense, Dropout, Activation, Conv2D, MaxPooling2D, Flatten, Reshape 9 | 10 | # Read land use classification layer data 11 | file_land2005 = '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Landuse_final/lan2005_final.tif' 12 | data_land2005 = gdal.Open(file_land2005) 13 | 14 | file_land2010 = '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Landuse_final/lan2010_final.tif' 15 | data_land2010 = gdal.Open(file_land2010) 16 | 17 | file_land2015 = '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Landuse_final/lan2015_final.tif' 18 | data_land2015 = gdal.Open(file_land2015) 19 | 20 | im_height = data_land2005.RasterYSize # Number of rows in the raster matrix 21 | im_width = data_land2005.RasterXSize # Number of columns in the raster matrix 22 | 23 | # Land use data 24 | im_data_land2005 = data_land2005.ReadAsArray(0, 0, im_width, im_height) 25 | im_data_land2010 = data_land2010.ReadAsArray(0, 0, im_width, im_height) 26 | im_data_land2015 = data_land2015.ReadAsArray(0, 0, im_width, im_height) 27 | 28 | number = 0 29 | # Number of pixels in Shanghai 30 | for row in range(12,im_height-12): 31 | for col in range(12,im_width-12): 32 | if im_data_land2005[row][col] != 0 : 33 | number = number + 1 34 | 35 | print("number of ShangHai:\n",number) 36 | 37 | # train samples are composed of AllFactors、Neighborhood features in 2005 38 | All_Factors_2005 = np.loadtxt( 39 | '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/AllFactors/AllFactors_20102017TXT.txt') 40 | 41 | Neighbor_11_11_2005 = np.loadtxt( 42 | '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Neighbor_field/Neighbor_field11_11/im_field_2005.txt') 43 | 44 | # samples and labels in 2005 45 | Sample_2005 = np.concatenate((All_Factors_2005, Neighbor_11_11_2005), axis=1) 46 | 47 | # Normalization function 48 | min_max_scaler = preprocessing.MinMaxScaler() 49 | Sample_2005_Norm = min_max_scaler.fit_transform(Sample_2005) 50 | 51 | Label_2005 = np.empty((number, 1)) 52 | index = 0 53 | for row in range(12, im_height - 12): 54 | for col in range(12, im_width - 12): 55 | if im_data_land2010[row][col] != 0: 56 | Label_2005[index, 0] = im_data_land2010[row][col] 57 | index += 1 58 | 59 | print("the shape of Sample_2005_Norm:", Sample_2005_Norm.shape) 60 | print("the shape of Label_2005:", Label_2005.shape) 61 | 62 | # train samples are composed of AllFactors、Neighborhood features in 2010 63 | All_Factors_2010 = np.loadtxt( 64 | '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/AllFactors/AllFactors_20102017TXT.txt') 65 | 66 | Neighbor_11_11_2010 = np.loadtxt( 67 | '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Neighbor_field/Neighbor_field11_11/im_field_2010.txt') 68 | 69 | # samples and labels in 2010 70 | Sample_2010 = np.concatenate((All_Factors_2010, Neighbor_11_11_2010), axis=1) 71 | 72 | # Normalization function 73 | min_max_scaler = preprocessing.MinMaxScaler() 74 | Sample_2010_Norm = min_max_scaler.fit_transform(Sample_2010) 75 | 76 | Label_2010 = np.empty((number, 1)) 77 | index = 0 78 | for row in range(12, im_height - 12): 79 | for col in range(12, im_width - 12): 80 | if im_data_land2015[row][col] != 0: 81 | Label_2010[index, 0] = im_data_land2015[row][col] 82 | index += 1 83 | 84 | print("the shape of Sample_2010_Norm:", Sample_2010_Norm.shape) 85 | print("the shape of Label_2010:", Label_2010.shape) 86 | 87 | # acquire training samples 88 | def random_int_list(start, stop, length): 89 | start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start)) 90 | length = int(abs(length)) if length else 0 91 | random_list = [] 92 | 93 | for i in range(length): 94 | random_list.append(random.randint(start, stop)) 95 | return np.array(random_list) 96 | 97 | 98 | random_list = random_int_list(0,Sample_2005_Norm.shape[0]-1,int(Sample_2005_Norm.shape[0]* 0.2)) 99 | 100 | ann_data = np.zeros((int(Sample_2005_Norm.shape[0]* 0.2),23)) 101 | ann_data_label = np.zeros((int(Sample_2005_Norm.shape[0]* 0.2),1)) 102 | for i in range(int(Sample_2005_Norm.shape[0]* 0.2)): 103 | temp = random_list[i] 104 | ann_data[i]= Sample_2005_Norm[temp] #ann_data: 20% of origin samples 105 | ann_data_label[i] = Label_2005[temp] #ann_data_label 106 | 107 | train_num = int(ann_data.shape[0] * 0.7) # set number of training samples 108 | test_num = ann_data.shape[0] - train_num # set number of test samples 109 | 110 | # acquire training samples from ann_data 111 | ann_data_train = np.zeros((train_num,23)) 112 | ann_data_test = np.zeros((test_num,23)) 113 | ann_data_train_label = np.zeros((train_num,1)) 114 | ann_data_test_label = np.zeros((test_num,1)) 115 | for i in range(train_num): 116 | ann_data_train[i] = ann_data[i] 117 | ann_data_train_label[i] = ann_data_label[i] 118 | for j in range(train_num,ann_data.shape[0]): 119 | ann_data_test[j - train_num] = ann_data[j] 120 | ann_data_test_label[j - train_num] = ann_data_label[j] 121 | 122 | # transform label into one-hot 123 | ann_data_train_label = np_utils.to_categorical(ann_data_train_label, num_classes=7) 124 | ann_data_test_label = np_utils.to_categorical(ann_data_test_label, num_classes=7) 125 | 126 | # set neural networks 127 | model = Sequential([ 128 | Dense(32, input_dim=23), 129 | Activation('relu'), 130 | Dense(64), 131 | Activation('relu'), 132 | Dropout(0.02), 133 | Dense(7), 134 | Activation('softmax'), 135 | ]) 136 | 137 | # select optimizer 138 | rmsprop = RMSprop(lr=0.02, rho=0.8, epsilon=1e-08, decay=0.0) 139 | 140 | # The loss function and the precision evaluation index are selected 141 | model.compile(optimizer=rmsprop, 142 | loss='categorical_crossentropy', 143 | metrics=['accuracy']) 144 | 145 | # Start training using training data 146 | print('Training ------------') 147 | 148 | model.fit(ann_data_train, ann_data_train_label, epochs=10, batch_size=16) 149 | 150 | # Optimize with test data 151 | print('\nTesting ------------') 152 | 153 | loss, accuracy = model.evaluate(ann_data_test, ann_data_test_label) 154 | # Output training results 155 | print('test loss: ', loss) 156 | print('test accuracy: ', accuracy) 157 | 158 | # The simulation was performed using 2010 image data 159 | predict = model.predict(Sample_2010_Norm, batch_size=32, verbose=0) 160 | 161 | predict_out = np.zeros((predict.shape[0], predict.shape[1])) 162 | # Find the maximum value for each row and determine if it is greater than the threshold 163 | theshold = 0.8 164 | Change_sum = 0 165 | for i in range(predict.shape[0]): 166 | for j in range(predict.shape[1]): 167 | if predict[i][j] >= theshold: 168 | predict_out[i][j] = 1 169 | Change_sum = Change_sum + 1 170 | else: 171 | predict_out[i][j] = 0 172 | 173 | 174 | print("The number of predict > theshold:", Change_sum) 175 | print("The rate of predict > theshold:", Change_sum / number) 176 | 177 | # get data_new after simulation 178 | Label_predict = np.zeros((number, 1)) 179 | 180 | index = 0 181 | for row in range(12, im_height - 12): 182 | for col in range(12, im_width - 12): 183 | if im_data_land2010[row][col] != 0: 184 | Label_predict[index][0] = im_data_land2010[row][col] 185 | 186 | for j in range(predict.shape[1]): 187 | if predict_out[index][j] == 1: 188 | Label_predict[index][0] = j 189 | 190 | index += 1 191 | 192 | # Gets the simulated array data 193 | data_new = np.zeros((im_height, im_width)) 194 | index = 0 195 | for row in range(12, im_height - 12): 196 | for col in range(12, im_width - 12): 197 | if im_data_land2010[row][col] != 0: 198 | data_new[row][col] = Label_predict[index][0] 199 | index = index + 1 200 | 201 | same_label_origin = 0 202 | for row in range(12, im_height - 12): 203 | for col in range(12, im_width - 12): 204 | if im_data_land2010[row][col] != 0: 205 | if im_data_land2010[row][col] == im_data_land2015[row][col]: 206 | same_label_origin = same_label_origin + 1 207 | 208 | print("The same label between im_data_land2010 and im_data_land2015 = ", same_label_origin) 209 | 210 | same_label = 0 211 | for row in range(12, im_height - 12): 212 | for col in range(12, im_width - 12): 213 | if im_data_land2010[row][col] != 0: 214 | if im_data_land2010[row][col] == data_new[row][col]: 215 | same_label = same_label + 1 216 | 217 | print("The same label between im_data_land2010 and data_new = ", same_label) 218 | 219 | same = 0 220 | for row in range(12, im_height - 12): 221 | for col in range(12, im_width - 12): 222 | if im_data_land2010[row][col] != 0: 223 | if im_data_land2015[row][col] == data_new[row][col]: 224 | same = same + 1 225 | 226 | print("The same label between im_data_land2015 and data_new = ", same) 227 | print("the accuracy of predict is:", same / number) 228 | 229 | data_new_outtxt = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/CNNs_Merely/ANN_CA.txt' 230 | np.savetxt(data_new_outtxt, data_new, fmt='%s', newline='\n') -------------------------------------------------------------------------------- /Accuracy_Kappa.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import random 3 | from osgeo import gdal 4 | import csv 5 | 6 | 7 | def Confusion_Cal(im_data_refer, data_new): 8 | # Calculate the width and length of the image 9 | im_height = im_data_refer.shape[0] 10 | im_width = im_data_refer.shape[1] 11 | 12 | # Calculate the confusion matrix and Kappa coefficient 13 | Confusion_matrix = np.zeros((6, 6)) 14 | for row in range(12, im_height - 12): 15 | for col in range(12, im_width - 12): 16 | if im_data_refer[row][col] != 0: 17 | if im_data_refer[row][col] == 1: 18 | if data_new[row][col] == 1: 19 | Confusion_matrix[0][0] = Confusion_matrix[0][0] + 1 20 | elif data_new[row][col] == 2: 21 | Confusion_matrix[0][1] = Confusion_matrix[0][1] + 1 22 | elif data_new[row][col] == 3: 23 | Confusion_matrix[0][2] = Confusion_matrix[0][2] + 1 24 | elif data_new[row][col] == 4: 25 | Confusion_matrix[0][3] = Confusion_matrix[0][3] + 1 26 | elif data_new[row][col] == 5: 27 | Confusion_matrix[0][4] = Confusion_matrix[0][4] + 1 28 | elif data_new[row][col] == 6: 29 | Confusion_matrix[0][5] = Confusion_matrix[0][5] + 1 30 | 31 | if im_data_refer[row][col] == 2: 32 | if data_new[row][col] == 1: 33 | Confusion_matrix[1][0] = Confusion_matrix[1][0] + 1 34 | elif data_new[row][col] == 2: 35 | Confusion_matrix[1][1] = Confusion_matrix[1][1] + 1 36 | elif data_new[row][col] == 3: 37 | Confusion_matrix[1][2] = Confusion_matrix[1][2] + 1 38 | elif data_new[row][col] == 4: 39 | Confusion_matrix[1][3] = Confusion_matrix[1][3] + 1 40 | elif data_new[row][col] == 5: 41 | Confusion_matrix[1][4] = Confusion_matrix[1][4] + 1 42 | elif data_new[row][col] == 6: 43 | Confusion_matrix[1][5] = Confusion_matrix[1][5] + 1 44 | 45 | if im_data_refer[row][col] == 3: 46 | if data_new[row][col] == 1: 47 | Confusion_matrix[2][0] = Confusion_matrix[2][0] + 1 48 | elif data_new[row][col] == 2: 49 | Confusion_matrix[2][1] = Confusion_matrix[2][1] + 1 50 | elif data_new[row][col] == 3: 51 | Confusion_matrix[2][2] = Confusion_matrix[2][2] + 1 52 | elif data_new[row][col] == 4: 53 | Confusion_matrix[2][3] = Confusion_matrix[2][3] + 1 54 | elif data_new[row][col] == 5: 55 | Confusion_matrix[2][4] = Confusion_matrix[2][4] + 1 56 | elif data_new[row][col] == 6: 57 | Confusion_matrix[2][5] = Confusion_matrix[2][5] + 1 58 | 59 | if im_data_refer[row][col] == 4: 60 | if data_new[row][col] == 1: 61 | Confusion_matrix[3][0] = Confusion_matrix[3][0] + 1 62 | elif data_new[row][col] == 2: 63 | Confusion_matrix[3][1] = Confusion_matrix[3][1] + 1 64 | elif data_new[row][col] == 3: 65 | Confusion_matrix[3][2] = Confusion_matrix[3][2] + 1 66 | elif data_new[row][col] == 4: 67 | Confusion_matrix[3][3] = Confusion_matrix[3][3] + 1 68 | elif data_new[row][col] == 5: 69 | Confusion_matrix[3][4] = Confusion_matrix[3][4] + 1 70 | elif data_new[row][col] == 6: 71 | Confusion_matrix[3][5] = Confusion_matrix[3][5] + 1 72 | 73 | if im_data_refer[row][col] == 5: 74 | if data_new[row][col] == 1: 75 | Confusion_matrix[4][0] = Confusion_matrix[4][0] + 1 76 | elif data_new[row][col] == 2: 77 | Confusion_matrix[4][1] = Confusion_matrix[4][1] + 1 78 | elif data_new[row][col] == 3: 79 | Confusion_matrix[4][2] = Confusion_matrix[4][2] + 1 80 | elif data_new[row][col] == 4: 81 | Confusion_matrix[4][3] = Confusion_matrix[4][3] + 1 82 | elif data_new[row][col] == 5: 83 | Confusion_matrix[4][4] = Confusion_matrix[4][4] + 1 84 | elif data_new[row][col] == 6: 85 | Confusion_matrix[4][5] = Confusion_matrix[4][5] + 1 86 | 87 | if im_data_refer[row][col] == 6: 88 | if data_new[row][col] == 1: 89 | Confusion_matrix[5][0] = Confusion_matrix[5][0] + 1 90 | elif data_new[row][col] == 2: 91 | Confusion_matrix[5][1] = Confusion_matrix[5][1] + 1 92 | elif data_new[row][col] == 3: 93 | Confusion_matrix[5][2] = Confusion_matrix[5][2] + 1 94 | elif data_new[row][col] == 4: 95 | Confusion_matrix[5][3] = Confusion_matrix[5][3] + 1 96 | elif data_new[row][col] == 5: 97 | Confusion_matrix[5][4] = Confusion_matrix[5][4] + 1 98 | elif data_new[row][col] == 6: 99 | Confusion_matrix[5][5] = Confusion_matrix[5][5] + 1 100 | 101 | return Confusion_matrix 102 | 103 | 104 | def Multilabel_Precision_Recall(confusion_matrix): 105 | Precision_1 = confusion_matrix[0][0] / ( 106 | confusion_matrix[0][0] + confusion_matrix[1][0] + confusion_matrix[2][0] + confusion_matrix[3][0] + 107 | confusion_matrix[4][0] + confusion_matrix[5][0]) 108 | Recall_1 = confusion_matrix[0][0] / ( 109 | confusion_matrix[0][0] + confusion_matrix[0][1] + confusion_matrix[0][2] + confusion_matrix[0][3] + 110 | confusion_matrix[0][4] + confusion_matrix[0][5]) 111 | F1score_1 = 2 * Precision_1 * Recall_1 / (Precision_1 + Recall_1) 112 | 113 | Precision_2 = confusion_matrix[1][1] / ( 114 | confusion_matrix[0][1] + confusion_matrix[1][1] + confusion_matrix[2][1] + confusion_matrix[3][1] + 115 | confusion_matrix[4][1] + confusion_matrix[5][1]) 116 | Recall_2 = confusion_matrix[1][1] / ( 117 | confusion_matrix[1][0] + confusion_matrix[1][1] + confusion_matrix[1][2] + confusion_matrix[1][3] + 118 | confusion_matrix[1][4] + confusion_matrix[1][5]) 119 | F1score_2 = 2 * Precision_2 * Recall_2 / (Precision_2 + Recall_2) 120 | 121 | Precision_3 = confusion_matrix[2][2] / ( 122 | confusion_matrix[0][2] + confusion_matrix[1][2] + confusion_matrix[2][2] + confusion_matrix[3][2] + 123 | confusion_matrix[4][2] + confusion_matrix[5][2]) 124 | Recall_3 = confusion_matrix[2][2] / ( 125 | confusion_matrix[2][0] + confusion_matrix[2][1] + confusion_matrix[2][2] + confusion_matrix[2][3] + 126 | confusion_matrix[2][4] + confusion_matrix[2][5]) 127 | F1score_3 = 2 * Precision_3 * Recall_3 / (Precision_3 + Recall_3) 128 | 129 | Precision_4 = confusion_matrix[3][3] / ( 130 | confusion_matrix[0][3] + confusion_matrix[1][3] + confusion_matrix[2][3] + confusion_matrix[3][3] + 131 | confusion_matrix[4][3] + confusion_matrix[5][3]) 132 | Recall_4 = confusion_matrix[3][3] / ( 133 | confusion_matrix[3][0] + confusion_matrix[3][1] + confusion_matrix[3][2] + confusion_matrix[3][3] + 134 | confusion_matrix[3][4] + confusion_matrix[3][5]) 135 | F1score_4 = 2 * Precision_4 * Recall_4 / (Precision_4 + Recall_4) 136 | 137 | Precision_5 = confusion_matrix[4][4] / ( 138 | confusion_matrix[0][4] + confusion_matrix[1][4] + confusion_matrix[2][4] + confusion_matrix[3][4] + 139 | confusion_matrix[4][4] + confusion_matrix[5][4]) 140 | Recall_5 = confusion_matrix[4][4] / ( 141 | confusion_matrix[4][0] + confusion_matrix[4][1] + confusion_matrix[4][2] + confusion_matrix[4][3] + 142 | confusion_matrix[4][4] + confusion_matrix[4][5]) 143 | F1score_5 = 2 * Precision_5 * Recall_5 / (Precision_5 + Recall_5) 144 | 145 | Precision_6 = confusion_matrix[5][5] / ( 146 | confusion_matrix[0][5] + confusion_matrix[1][5] + confusion_matrix[2][5] + confusion_matrix[3][5] + 147 | confusion_matrix[4][5] + confusion_matrix[5][5]) 148 | Recall_6 = confusion_matrix[5][5] / ( 149 | confusion_matrix[5][0] + confusion_matrix[5][1] + confusion_matrix[5][2] + confusion_matrix[5][3] + 150 | confusion_matrix[5][4] + confusion_matrix[5][5]) 151 | F1score_6 = 2 * Precision_6 * Recall_6 / (Precision_6 + Recall_6) 152 | 153 | PRF = np.concatenate(([Precision_1, Recall_1, F1score_1], 154 | [Precision_2, Recall_2, F1score_2], 155 | [Precision_3, Recall_3, F1score_3], 156 | [Precision_4, Recall_4, F1score_4], 157 | [Precision_5, Recall_5, F1score_5], 158 | [Precision_6, Recall_6, F1score_6]), axis=0).reshape(6, 3) 159 | 160 | Accuracy = (confusion_matrix[0][0] + confusion_matrix[1][1] + confusion_matrix[2][2] + confusion_matrix[3][3] + 161 | confusion_matrix[4][4] + confusion_matrix[5][5]) / np.sum(confusion_matrix) 162 | Pe = (np.sum(confusion_matrix[0, :]) * np.sum(confusion_matrix[:, 0]) + np.sum(confusion_matrix[1, :]) * np.sum( 163 | confusion_matrix[:, 1]) + np.sum(confusion_matrix[2, :]) * np.sum(confusion_matrix[:, 2]) 164 | + np.sum(confusion_matrix[3, :]) * np.sum(confusion_matrix[:, 3]) + np.sum(confusion_matrix[4, :]) * np.sum( 165 | confusion_matrix[:, 4]) + np.sum(confusion_matrix[5, :]) * np.sum(confusion_matrix[:, 5])) / ( 166 | np.sum(confusion_matrix) * np.sum(confusion_matrix)) 167 | 168 | Kappa = (Accuracy - Pe) / (1 - Pe) 169 | 170 | macro_score = (F1score_1 + F1score_2 + F1score_3 + F1score_4 + F1score_5 + F1score_6) / 6 171 | 172 | return PRF, Accuracy, Kappa, macro_score 173 | 174 | # Actual LU map 175 | file_land_2015 = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalData/Landuse_final/lan2015_final.tif' 176 | data_land_2015 = gdal.Open(file_land_2015) 177 | 178 | im_height = data_land_2015.RasterYSize # Number of rows in the raster matrix 179 | im_width = data_land_2015.RasterXSize # Number of columns in the raster matrix 180 | 181 | im_data_land_2015 = data_land_2015.ReadAsArray(0, 0, im_width, im_height) 182 | 183 | # simulation results of ANN_CA 184 | data_ANN_whole = np.loadtxt('/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/Conventional_Models/ANN_whole.txt') 185 | 186 | # Calculate the confusion matrix for ANN model 2015 WholeArea 187 | confusion_matrix_ANN_whole = Confusion_Cal(im_data_land_2015,data_ANN_whole) 188 | 189 | PRF_ANN_whole,Accuracy_ANN_whole,Kappa_ANN_whole,macro_score_ANN_whole = Multilabel_Precision_Recall(confusion_matrix_ANN_whole) 190 | print("PRF_ANN_whole:",PRF_ANN_whole) 191 | print("Accuracy_ANN_whole:",Accuracy_ANN_whole) 192 | print("Kappa_ANN_whole:",Kappa_ANN_whole) 193 | print("macro_score_ANN_whole:",macro_score_ANN_whole) 194 | 195 | path = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/accuracy_data_new.csv' 196 | f = open(path,'a+',encoding='utf-8') 197 | csv_write = csv.writer(f) 198 | csv_write.writerow(['ANN_whole',Accuracy_ANN_whole,Kappa_ANN_whole,macro_score_ANN_whole]) 199 | f.close() -------------------------------------------------------------------------------- /Features3D_CNN.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import random 3 | from osgeo import gdal 4 | from keras.models import Sequential 5 | from keras.utils import np_utils 6 | from keras.optimizers import RMSprop 7 | from keras.layers import Bidirectional 8 | from keras.utils import plot_model 9 | from keras.layers import Dense,Dropout,Activation,Convolution3D,MaxPooling3D,Flatten 10 | from keras.optimizers import Adam 11 | from keras.layers import Conv3D, MaxPooling3D 12 | from keras import backend as K 13 | from time import time 14 | 15 | # Read land use classification layer data 16 | file_land2000 = '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Landuse_final/lan2000_final.tif' 17 | data_land2000 = gdal.Open(file_land2000) 18 | 19 | im_height = data_land2000.RasterYSize # Number of rows in the raster matrix 20 | im_width = data_land2000.RasterXSize # Number of columns in the raster matrix 21 | 22 | # Land use data 23 | im_data_land2000 = data_land2000.ReadAsArray(0, 0, im_width, im_height) 24 | 25 | number = 0 26 | # Number of pixels in Shanghai 27 | for row in range(12,im_height-12): 28 | for col in range(12,im_width-12): 29 | if im_data_land2000[row][col] != 0 : 30 | number = number + 1 31 | 32 | print("number of ShangHai:\n",number) 33 | 34 | # load data 35 | data_sample_2003 = np.load("/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/CNN_features/CNN_data_sample/CNN_35_35/data_sample_2003_npy.npy").reshape(-1,35,35,1) 36 | data_sample_2004 = np.load("/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/CNN_features/CNN_data_sample/CNN_35_35/data_sample_2004_npy.npy").reshape(-1,35,35,1) 37 | data_sample_2005 = np.load("/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/CNN_features/CNN_data_sample/CNN_35_35/data_sample_2005_npy.npy").reshape(-1,35,35,1) 38 | 39 | data_label_2005 = np.loadtxt('/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Labels/data_label_2009.txt') 40 | 41 | # concatenate 42 | data_sample_03to05 = np.concatenate((data_sample_2003,data_sample_2004,data_sample_2005), axis=3) 43 | data_sample_label_03to05 = data_label_2005 44 | 45 | # get training data 46 | def random_int_list(start, stop, length): 47 | start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start)) 48 | length = int(abs(length)) if length else 0 49 | random_list = [] 50 | 51 | for i in range(length): 52 | random_list.append(random.randint(start, stop)) 53 | return np.array(random_list) 54 | 55 | num_of_year = int(number * 0.2) 56 | random_list = random_int_list(0, number - 1, num_of_year) 57 | 58 | cnn_data_03to05 = np.zeros((num_of_year, 35, 35, 3)) 59 | cnn_data_label_03to05 = np.zeros((num_of_year, 1)) 60 | 61 | for i in range(0, num_of_year): 62 | temp = random_list[i] 63 | cnn_data_03to05[i] = data_sample_03to05[temp] 64 | cnn_data_label_03to05[i] = data_sample_label_03to05[temp] 65 | 66 | cnn_data = cnn_data_03to05 67 | cnn_data_label = cnn_data_label_03to05 68 | 69 | # cnn_data was used to train 70 | train_num = int(cnn_data.shape[0] * 0.7) # set number of training samples 71 | test_num = cnn_data.shape[0] - train_num # set number of test samples 72 | 73 | # get final training data from cnn_data 74 | cnn_data_train = np.zeros((train_num, 35, 35, 3)) 75 | cnn_data_test = np.zeros((test_num, 35, 35, 3)) 76 | cnn_data_train_label = np.zeros((train_num, 1)) 77 | cnn_data_test_label = np.zeros((test_num, 1)) 78 | for i in range(train_num): 79 | cnn_data_train[i] = cnn_data[i] 80 | cnn_data_train_label[i] = cnn_data_label[i] 81 | for j in range(train_num, cnn_data.shape[0]): 82 | cnn_data_test[j - train_num] = cnn_data[j] 83 | cnn_data_test_label[j - train_num] = cnn_data_label[j] 84 | 85 | # transform label into one-hot 86 | cnn_data_train_label = np_utils.to_categorical(cnn_data_train_label, num_classes=7) 87 | cnn_data_test_label = np_utils.to_categorical(cnn_data_test_label, num_classes=7) 88 | 89 | cnn_data_train = np.reshape(cnn_data_train,(cnn_data_train.shape[0],cnn_data_train.shape[1],cnn_data_train.shape[2],cnn_data_train.shape[3],1)) 90 | cnn_data_test = np.reshape(cnn_data_test,(cnn_data_test.shape[0],cnn_data_test.shape[1],cnn_data_test.shape[2],cnn_data_test.shape[3],1)) 91 | 92 | t1 = time() 93 | # set up a 3D CNN model 94 | model=Sequential() 95 | model.add(Conv3D(32, (3, 3, 3), input_shape=(35, 35, 3, 1), padding="same") ) 96 | model.add(Activation('relu')) 97 | model.add(MaxPooling3D(pool_size=(2, 2, 1), padding="same")) 98 | 99 | # This is adding a second layer of neural network, convolution layer, excitation function, pooling layer 100 | model.add(Conv3D(64, (3, 3, 3), input_shape=(35, 35, 3, 1), padding="same") ) 101 | model.add(Activation('relu')) 102 | # model.add(MaxPooling2D(pool_size=(2, 2), padding="same")) 103 | 104 | # add a third convolution layer 105 | model.add(Conv3D(32, (3, 3, 3), input_shape=(35, 35, 3, 1), padding="same") ) 106 | model.add(Activation('relu')) 107 | 108 | # sort into one dimension 109 | model.add(Flatten()) 110 | 111 | model.add(Activation('relu')) 112 | model.add(Dense(7)) 113 | model.add(Activation('softmax')) 114 | 115 | # define your optimize 116 | rms = RMSprop(lr=0.0001, rho=0.9, epsilon=1e-06) 117 | model.compile(loss='categorical_crossentropy', optimizer=rms, metrics=['accuracy']) 118 | 119 | # adm = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.6, amsgrad=False) 120 | # model.compile(loss='categorical_crossentropy', optimizer=adm, metrics=['accuracy']) 121 | 122 | print('\nTraining-----------') 123 | model.fit(cnn_data_train,cnn_data_train_label,epochs=10,batch_size=32) 124 | model.summary() 125 | print('\nTesting------------') 126 | loss,accuracy=model.evaluate(cnn_data_test,cnn_data_test_label) 127 | 128 | 129 | print('test loss: ', loss) 130 | print('test accuracy: ', accuracy) 131 | t2 = time() 132 | print("time used:",t2 - t1) 133 | 134 | predict_03to05 = model.predict(data_sample_03to05) 135 | data_new_outtxt = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalData/CNN_features/CNN_3years/CNN_3years_35_35/CNN_features_3years_35_35_03to05.txt' 136 | np.savetxt(data_new_outtxt, predict_03to05, fmt='%s', newline='\n') -------------------------------------------------------------------------------- /FoM.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import random 3 | from osgeo import gdal 4 | 5 | # 真实结果 6 | file_land_2015 = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalData/Landuse_final/lan2015_final.tif' 7 | data_land_2015 = gdal.Open(file_land_2015) 8 | 9 | im_height = data_land_2015.RasterYSize # Number of rows in the raster matrix 10 | im_width = data_land_2015.RasterXSize # Number of columns in the raster matrix 11 | 12 | im_data_land_2015 = data_land_2015.ReadAsArray(0, 0, im_width, im_height) 13 | 14 | # simulation results of conventional models in 2015 15 | file_LR = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/Conventional_Models/Logistic_Regression_CA.txt' 16 | im_data_LR = np.loadtxt(file_LR) 17 | 18 | file_SVM = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/Conventional_Models/SVM_CA.txt' 19 | im_data_SVM = np.loadtxt(file_SVM) 20 | 21 | file_RF = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/Conventional_Models/RF_CA.txt' 22 | im_data_RF = np.loadtxt(file_RF) 23 | 24 | file_ANN = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/Conventional_Models/ANN_whole.txt' 25 | im_data_ANN = np.loadtxt(file_ANN) 26 | 27 | 28 | # simulation results of partition in 2015 29 | file_K_Means = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/Partition_Merely/K_means/im_K_means.txt' 30 | im_data_K_Means = np.loadtxt(file_K_Means) 31 | 32 | file_SOM = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/Partition_Merely/im_SOM.txt' 33 | im_data_SOM = np.loadtxt(file_SOM) 34 | 35 | 36 | # simulation results of CNN in 2015 37 | file_CNN1year = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/CNNs_Merely/ANN_CNN1year.txt' 38 | im_data_CNN1year = np.loadtxt(file_CNN1year) 39 | 40 | file_CNN2years = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/CNNs_Merely/ANN_CNN2years.txt' 41 | im_data_CNN2years = np.loadtxt(file_CNN2years) 42 | 43 | file_CNN3years = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/CNNs_Merely/ANN_CNN3years.txt' 44 | im_data_CNN3years = np.loadtxt(file_CNN3years) 45 | 46 | file_CNN4years = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/CNNs_Merely/ANN_CNN4years.txt' 47 | im_data_CNN4years = np.loadtxt(file_CNN4years) 48 | 49 | file_CNN5years = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/CNNs_Merely/ANN_CNN5years.txt' 50 | im_data_CNN5years = np.loadtxt(file_CNN5years) 51 | 52 | 53 | # simulation results of PST_CA in 2015 54 | file_PST = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/Partition_CNNs/im_PST.txt' 55 | im_data_PST = np.loadtxt(file_PST) 56 | 57 | # Read land use classification layer data 58 | file_land2005 = '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Landuse_final/lan2005_final.tif' 59 | data_land2005 = gdal.Open(file_land2005) 60 | 61 | file_land2010 = '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Landuse_final/lan2010_final.tif' 62 | data_land2010 = gdal.Open(file_land2010) 63 | 64 | file_land2015 = '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Landuse_final/lan2015_final.tif' 65 | data_land2015 = gdal.Open(file_land2015) 66 | 67 | im_height = data_land2005.RasterYSize # Number of rows in the raster matrix 68 | im_width = data_land2005.RasterXSize # Number of columns in the raster matrix 69 | 70 | # Land use data 71 | im_data_land2005 = data_land2005.ReadAsArray(0, 0, im_width, im_height) 72 | im_data_land2010 = data_land2010.ReadAsArray(0, 0, im_width, im_height) 73 | im_data_land2015 = data_land2015.ReadAsArray(0, 0, im_width, im_height) 74 | 75 | number = 0 76 | # Number of pixels in Shanghai 77 | for row in range(12,im_height-12): 78 | for col in range(12,im_width-12): 79 | if im_data_land2015[row][col] != 0 : 80 | number = number + 1 81 | 82 | print("number of Shanghai:\n",number) 83 | 84 | error_1_PST_2015 = 0 85 | error_2_PST_2015 = 0 86 | error_3_PST_2015 = 0 87 | correct_4_PST_2015 = 0 88 | error_PST_2015 = 0 89 | 90 | data_error_PST_2015 = np.zeros((im_height, im_width)) 91 | for row in range(12, im_height - 12): 92 | for col in range(12, im_width - 12): 93 | if im_data_land2010[row][col] != 0: 94 | data_error_PST_2015[row][col] = 1 95 | 96 | # stay unchanged but simulate changed 97 | if im_data_land2015[row][col] == im_data_land2010[row][col] and im_data_PST[row][col] != \ 98 | im_data_land2010[row][col]: 99 | data_error_PST_2015[row][col] = 50 100 | error_1_PST_2015 = error_1_PST_2015 + 1 101 | 102 | # actual changed but simulate unchanged 103 | if im_data_land2015[row][col] != im_data_land2010[row][col] and im_data_PST[row][col] == \ 104 | im_data_land2010[row][col]: 105 | data_error_PST_2015[row][col] = 100 106 | error_2_PST_2015 = error_2_PST_2015 + 1 107 | 108 | # actual changed and simulate changed, but simulate wrongly 109 | if im_data_land2015[row][col] != im_data_land2010[row][col] and im_data_PST[row][col] != \ 110 | im_data_land2010[row][col] and im_data_PST[row][col] != im_data_land2015[row][col]: 111 | data_error_PST_2015[row][col] = 150 112 | error_3_PST_2015 = error_3_PST_2015 + 1 113 | 114 | # actual changed and simulate changed, and simulate correctly 115 | if im_data_land2015[row][col] != im_data_land2010[row][col] and im_data_PST[row][col] != \ 116 | im_data_land2010[row][col] and im_data_PST[row][col] == im_data_land2015[row][col]: 117 | data_error_PST_2015[row][col] = 200 118 | correct_4_PST_2015 = correct_4_PST_2015 + 1 119 | 120 | # The predicted results are inconsistent with the actual results, total errors 121 | if im_data_land2015[row][col] != im_data_PST[row][col]: 122 | error_PST_2015 = error_PST_2015 + 1 123 | 124 | FOM = correct_4_PST_2015 / (error_1_PST_2015 + error_2_PST_2015 + error_3_PST_2015 + correct_4_PST_2015) 125 | # FOM_2 = (error_3_PST_2015+correct_4_PST_2015)/(error_1_PST_2015+error_2_PST_2015+error_3_PST_2015+correct_4_PST_2015) 126 | 127 | print("number of error_1_PST_2015:", error_1_PST_2015) 128 | print("number of error_2_PST_2015:", error_2_PST_2015) 129 | print("number of error_3_PST_2015:", error_3_PST_2015) 130 | print("number of correct_4_PST_2015:", correct_4_PST_2015) 131 | print("number of error_PST_2015:", error_PST_2015) 132 | 133 | print("------------------------") 134 | print("correct FOM:", FOM) 135 | # print("change FOM_2:", FOM_2) 136 | 137 | data_error_PST_2015_outtxt = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/Errors analysis/data_error_PST_2015.txt' 138 | np.savetxt(data_error_PST_2015_outtxt, data_error_PST_2015, fmt='%s', newline='\n') -------------------------------------------------------------------------------- /LR_CA.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import time 3 | import csv 4 | import random 5 | from osgeo import gdal 6 | from sklearn import preprocessing 7 | from sklearn.linear_model import LogisticRegression 8 | from keras.utils import np_utils 9 | 10 | # Read land use classification layer data 11 | file_land2005 = '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Landuse_final/lan2005_final.tif' 12 | data_land2005 = gdal.Open(file_land2005) 13 | 14 | file_land2010 = '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Landuse_final/lan2010_final.tif' 15 | data_land2010 = gdal.Open(file_land2010) 16 | 17 | file_land2015 = '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Landuse_final/lan2015_final.tif' 18 | data_land2015 = gdal.Open(file_land2015) 19 | 20 | im_height = data_land2005.RasterYSize # Number of rows in the raster matrix 21 | im_width = data_land2005.RasterXSize # Number of columns in the raster matrix 22 | 23 | # Land use data 24 | im_data_land2005 = data_land2005.ReadAsArray(0, 0, im_width, im_height) 25 | im_data_land2010 = data_land2010.ReadAsArray(0, 0, im_width, im_height) 26 | im_data_land2015 = data_land2015.ReadAsArray(0, 0, im_width, im_height) 27 | 28 | number = 0 29 | # Number of pixels in Shanghai 30 | for row in range(12,im_height-12): 31 | for col in range(12,im_width-12): 32 | if im_data_land2005[row][col] != 0 : 33 | number = number + 1 34 | 35 | print("number of ShangHai:\n",number) 36 | 37 | # train samples are composed of AllFactors、Neighborhood features in 2005 38 | All_Factors_2005 = np.loadtxt( 39 | '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/AllFactors/AllFactors_20102017TXT.txt') 40 | 41 | Neighbor_11_11_2005 = np.loadtxt( 42 | '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Neighbor_field/Neighbor_field11_11/im_field_2005.txt') 43 | 44 | # samples and labels in 2005 45 | Sample_2005 = np.concatenate((All_Factors_2005, Neighbor_11_11_2005), axis=1) 46 | 47 | # Normalization function 48 | min_max_scaler = preprocessing.MinMaxScaler() 49 | Sample_2005_Norm = min_max_scaler.fit_transform(Sample_2005) 50 | 51 | Label_2005 = np.empty((number, 1)) 52 | index = 0 53 | for row in range(12, im_height - 12): 54 | for col in range(12, im_width - 12): 55 | if im_data_land2010[row][col] != 0: 56 | Label_2005[index, 0] = im_data_land2010[row][col] 57 | index += 1 58 | 59 | print("the shape of Sample_2005_Norm:", Sample_2005_Norm.shape) 60 | print("the shape of Label_2005:", Label_2005.shape) 61 | 62 | # train samples are composed of AllFactors、Neighborhood features in 2010 63 | All_Factors_2010 = np.loadtxt( 64 | '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/AllFactors/AllFactors_20102017TXT.txt') 65 | 66 | Neighbor_11_11_2010 = np.loadtxt( 67 | '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Neighbor_field/Neighbor_field11_11/im_field_2010.txt') 68 | 69 | # samples and labels in 2010 70 | Sample_2010 = np.concatenate((All_Factors_2010, Neighbor_11_11_2010), axis=1) 71 | 72 | # Normalization function 73 | min_max_scaler = preprocessing.MinMaxScaler() 74 | Sample_2010_Norm = min_max_scaler.fit_transform(Sample_2010) 75 | 76 | Label_2010 = np.empty((number, 1)) 77 | index = 0 78 | for row in range(12, im_height - 12): 79 | for col in range(12, im_width - 12): 80 | if im_data_land2015[row][col] != 0: 81 | Label_2010[index, 0] = im_data_land2015[row][col] 82 | index += 1 83 | 84 | print("the shape of Sample_2010_Norm:", Sample_2010_Norm.shape) 85 | print("the shape of Label_2010:", Label_2010.shape) 86 | 87 | # acquire training samples 88 | def random_int_list(start, stop, length): 89 | start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start)) 90 | length = int(abs(length)) if length else 0 91 | random_list = [] 92 | 93 | for i in range(length): 94 | random_list.append(random.randint(start, stop)) 95 | return np.array(random_list) 96 | 97 | 98 | random_list = random_int_list(0,Sample_2005_Norm.shape[0]-1,int(Sample_2005_Norm.shape[0]* 0.2)) 99 | 100 | ann_data = np.zeros((int(Sample_2005_Norm.shape[0]* 0.2),23)) 101 | ann_data_label = np.zeros((int(Sample_2005_Norm.shape[0]* 0.2),1)) 102 | for i in range(int(Sample_2005_Norm.shape[0]* 0.2)): 103 | temp = random_list[i] 104 | ann_data[i]= Sample_2005_Norm[temp] #ann_data: 20% of origin samples 105 | ann_data_label[i] = Label_2005[temp] #ann_data_label 106 | 107 | train_num = int(ann_data.shape[0] * 0.7) # set number of training samples 108 | test_num = ann_data.shape[0] - train_num # set number of test samples 109 | 110 | # acquire training samples from ann_data 111 | ann_data_train = np.zeros((train_num,23)) 112 | ann_data_test = np.zeros((test_num,23)) 113 | ann_data_train_label = np.zeros((train_num,1)) 114 | ann_data_test_label = np.zeros((test_num,1)) 115 | for i in range(train_num): 116 | ann_data_train[i] = ann_data[i] 117 | ann_data_train_label[i] = ann_data_label[i] 118 | for j in range(train_num,ann_data.shape[0]): 119 | ann_data_test[j - train_num] = ann_data[j] 120 | ann_data_test_label[j - train_num] = ann_data_label[j] 121 | 122 | # transform label into one-hot 123 | ann_data_train_label = np_utils.to_categorical(ann_data_train_label, num_classes=7) 124 | ann_data_test_label = np_utils.to_categorical(ann_data_test_label, num_classes=7) 125 | 126 | lr = LogisticRegression() 127 | lr.fit(ann_data_train, ann_data_train_label) 128 | 129 | Label_predict = lr.predict(Sample_2010_Norm) 130 | 131 | # Gets the simulated array data 132 | data_new = np.zeros((im_height, im_width)) 133 | index = 0 134 | for row in range(12, im_height - 12): 135 | for col in range(12, im_width - 12): 136 | if im_data_land2010[row][col] != 0: 137 | data_new[row][col] = Label_predict[index][0] 138 | index = index + 1 139 | 140 | same_label_origin = 0 141 | for row in range(12, im_height - 12): 142 | for col in range(12, im_width - 12): 143 | if im_data_land2010[row][col] != 0: 144 | if im_data_land2010[row][col] == im_data_land2015[row][col]: 145 | same_label_origin = same_label_origin + 1 146 | 147 | print("The same label between im_data_land2010 and im_data_land2015 = ", same_label_origin) 148 | 149 | same_label = 0 150 | for row in range(12, im_height - 12): 151 | for col in range(12, im_width - 12): 152 | if im_data_land2010[row][col] != 0: 153 | if im_data_land2010[row][col] == data_new[row][col]: 154 | same_label = same_label + 1 155 | 156 | print("The same label between im_data_land2010 and data_new = ", same_label) 157 | 158 | same = 0 159 | for row in range(12, im_height - 12): 160 | for col in range(12, im_width - 12): 161 | if im_data_land2010[row][col] != 0: 162 | if im_data_land2015[row][col] == data_new[row][col]: 163 | same = same + 1 164 | 165 | print("The same label between im_data_land2015 and data_new = ", same) 166 | print("the accuracy of predict is:", same / number) 167 | 168 | np.savetxt('/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/Conventional_Models/Logistic_Regression_CA.txt', data_new, fmt='%s', newline='\n') -------------------------------------------------------------------------------- /PST_CA.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import time 3 | import csv 4 | import random 5 | from osgeo import gdal 6 | from sklearn import preprocessing 7 | from keras.models import Sequential 8 | from keras.layers.core import Dense, Activation, Dropout 9 | from keras.utils import np_utils 10 | from keras.optimizers import RMSprop, Adam 11 | from keras.layers import Dense, Dropout, Activation, Conv2D, MaxPooling2D, Flatten, Reshape 12 | from keras.optimizers import Adam 13 | from keras.models import Model 14 | from keras.layers import Input, PReLU, LSTM, multiply, concatenate 15 | 16 | # Read land use classification layer data 17 | file_land2005 = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalData/Landuse_final/lan2005_final.tif' 18 | data_land2005 = gdal.Open(file_land2005) 19 | 20 | file_land2010 = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalData/Landuse_final/lan2010_final.tif' 21 | data_land2010 = gdal.Open(file_land2010) 22 | 23 | file_land2015 = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalData/Landuse_final/lan2015_final.tif' 24 | data_land2015 = gdal.Open(file_land2015) 25 | 26 | im_height = data_land2005.RasterYSize # Number of rows in the raster matrix 27 | im_width = data_land2005.RasterXSize # Number of columns in the raster matrix 28 | 29 | # Land use data 30 | im_data_land2005 = data_land2005.ReadAsArray(0, 0, im_width, im_height) 31 | im_data_land2010 = data_land2010.ReadAsArray(0, 0, im_width, im_height) 32 | im_data_land2015 = data_land2015.ReadAsArray(0, 0, im_width, im_height) 33 | 34 | file_partition = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalData/Landuse_final/im_SOM_final.tif' 35 | data_partition = gdal.Open(file_partition) 36 | 37 | partition_result = data_partition.ReadAsArray(0, 0, im_width, im_height) 38 | 39 | Category_0 = 0 40 | for row in range(12, im_height - 12): 41 | for col in range(12, im_width - 12): 42 | # partition_result “100” denotes the pixel is outside the study area 43 | if partition_result[row][col] != 100: 44 | if partition_result[row][col] == 0: 45 | Category_0 += 1 46 | 47 | print("Category_0:", Category_0) 48 | 49 | part_zero_samples_2005 = np.loadtxt('/home/anaconda_workspace/qyh/SH_100m/ExperimentalData/Samples/Partition_Merely/2005/part_zero_samples_2005.txt') 50 | part_zero_labels_2005 = np.loadtxt('/home/anaconda_workspace/qyh/SH_100m/ExperimentalData/Samples/Partition_Merely/2005/part_zero_labels_2005.txt') 51 | 52 | part_zero_CNN1year_2005 = np.loadtxt('/home/anaconda_workspace/qyh/SH_100m/ExperimentalData/CNN_features/CNN_1year/CNN_1year_11_11/part_zero_CNN_features_1year_11_11_2005.txt') 53 | 54 | part_zero_Sample_2005_UnNorm = np.concatenate((part_zero_samples_2005,part_zero_CNN1year_2005),axis=1) 55 | # 归一化函数 56 | min_max_scaler = preprocessing.MinMaxScaler() 57 | part_zero_Sample_2005_final = min_max_scaler.fit_transform(part_zero_Sample_2005_UnNorm) 58 | 59 | print("the shape of part_zero_Sample_2005_final:",part_zero_Sample_2005_final.shape) 60 | print("the shape of part_zero_labels_2005:",part_zero_labels_2005.shape) 61 | 62 | part_zero_samples_2010 = np.loadtxt('/home/anaconda_workspace/qyh/SH_100m/ExperimentalData/Samples/Partition_Merely/2010/part_zero_samples_2010.txt') 63 | part_zero_labels_2010 = np.loadtxt('/home/anaconda_workspace/qyh/SH_100m/ExperimentalData/Samples/Partition_Merely/2010/part_zero_labels_2010.txt') 64 | 65 | part_zero_CNN1year_2010 = np.loadtxt('/home/anaconda_workspace/qyh/SH_100m/ExperimentalData/CNN_features/CNN_1year/CNN_1year_11_11/part_zero_CNN_features_1year_11_11_2010.txt') 66 | 67 | part_zero_Sample_2010_UnNorm = np.concatenate((part_zero_samples_2010,part_zero_CNN1year_2010),axis=1) 68 | # Normalization function 69 | min_max_scaler = preprocessing.MinMaxScaler() 70 | part_zero_Sample_2010_final = min_max_scaler.fit_transform(part_zero_Sample_2010_UnNorm) 71 | 72 | print("the shape of part_zero_Sample_2010_final:",part_zero_Sample_2010_final.shape) 73 | print("the shape of part_zero_labels_2010:",part_zero_labels_2010.shape) 74 | 75 | # acquire training samples 76 | def random_int_list(start, stop, length): 77 | start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start)) 78 | length = int(abs(length)) if length else 0 79 | random_list = [] 80 | 81 | for i in range(length): 82 | random_list.append(random.randint(start, stop)) 83 | return np.array(random_list) 84 | 85 | 86 | random_list = random_int_list(0,part_zero_Sample_2005_final.shape[0]-1,int(part_zero_Sample_2005_final.shape[0]* 0.2)) 87 | ann_data = np.zeros((int(part_zero_Sample_2005_final.shape[0]* 0.2),30)) 88 | ann_data_label = np.zeros((int(part_zero_Sample_2005_final.shape[0]* 0.2),1)) 89 | for i in range(int(part_zero_Sample_2005_final.shape[0]* 0.2)): 90 | temp = random_list[i] 91 | ann_data[i]= part_zero_Sample_2005_final[temp] #ann_data: 20% of origin samples 92 | ann_data_label[i] = part_zero_labels_2005[temp] 93 | 94 | train_num = int(ann_data.shape[0] * 0.7) # set number of training samples 95 | test_num = ann_data.shape[0] - train_num # set number of test samples 96 | 97 | # acquire training samples from ann_data 98 | ann_data_train = np.zeros((train_num,30)) 99 | ann_data_test = np.zeros((test_num,30)) 100 | ann_data_train_label = np.zeros((train_num,1)) 101 | ann_data_test_label = np.zeros((test_num,1)) 102 | for i in range(train_num): 103 | ann_data_train[i] = ann_data[i] 104 | ann_data_train_label[i] = ann_data_label[i] 105 | for j in range(train_num,ann_data.shape[0]): 106 | ann_data_test[j - train_num] = ann_data[j] 107 | ann_data_test_label[j - train_num] = ann_data_label[j] 108 | 109 | # transform label into one-hot 110 | ann_data_train_label = np_utils.to_categorical(ann_data_train_label, num_classes=7) 111 | ann_data_test_label = np_utils.to_categorical(ann_data_test_label, num_classes=7) 112 | 113 | # set neural networks 114 | model = Sequential([ 115 | Dense(32, input_dim=30), 116 | Activation('relu'), 117 | Dense(64), 118 | Activation('relu'), 119 | Dropout(0.02), 120 | Dense(7), 121 | Activation('softmax'), 122 | ]) 123 | 124 | # select optimizer 125 | rmsprop = RMSprop(lr=0.0001, rho=0.8, epsilon=1e-08, decay=0.0) 126 | 127 | # The loss function and the precision evaluation index are selected 128 | model.compile(optimizer=rmsprop, 129 | loss='categorical_crossentropy', 130 | metrics=['accuracy']) 131 | 132 | # Start training using training data 133 | print('Training ------------') 134 | 135 | model.fit(ann_data_train, ann_data_train_label, epochs=10, batch_size=64) 136 | 137 | # Optimize with test data 138 | print('\nTesting ------------') 139 | 140 | loss, accuracy = model.evaluate(ann_data_test, ann_data_test_label) 141 | # Output training results 142 | print('test loss: ', loss) 143 | print('test accuracy: ', accuracy) 144 | 145 | # The simulation was performed using 2010 image data 146 | predict = model.predict(part_zero_Sample_2010_final, batch_size=32, verbose=0) 147 | 148 | predict_out = np.zeros((predict.shape[0], predict.shape[1])) 149 | # Find the maximum value for each row and determine if it is greater than the threshold 150 | theshold = 0.8 151 | Change_sum = 0 152 | for i in range(predict.shape[0]): 153 | for j in range(predict.shape[1]): 154 | if predict[i][j] >= theshold: 155 | predict_out[i][j] = 1 156 | Change_sum = Change_sum + 1 157 | else: 158 | predict_out[i][j] = 0 159 | 160 | # for i in range(predict.shape[0]): 161 | # for j in range(predict.shape[1]): 162 | # if j == 4: 163 | # predict_out[i][j] = 1 164 | # Change_sum = Change_sum + 1 165 | # else: 166 | # predict_out[i][j] = 0 167 | 168 | print("The number of predict > theshold:", Change_sum) 169 | print("The rate of predict > theshold:", Change_sum / Category_0) 170 | 171 | Cos = np.zeros((6, 7)) 172 | Cos = [[0, 1, 1, 1, 1, 1, 1], 173 | [0, 1, 1, 1, 1, 1, 1], 174 | [0, 1, 1, 1, 1, 1, 1], 175 | [0, 0, 0, 0, 1, 0, 1], 176 | [0, 0, 0, 0, 0, 0, 1], 177 | [0, 1, 1, 1, 1, 1, 1]] 178 | 179 | Change_sum_new = 0 180 | for i in range(predict_out.shape[0]): 181 | # Using the initial year of the simulation as a starting point, determine whether transformation can occur 182 | temp_label = int(part_zero_labels_2005[i]) 183 | predict_out[i] = np.multiply(predict_out[i], Cos[temp_label - 1]) 184 | Change_sum_new = Change_sum_new + np.sum(predict_out[i]) 185 | 186 | print("The number of predict > theshold and changeable:", Change_sum_new) 187 | print("The rate of predict > theshold and changeable:", Change_sum_new / Category_0) 188 | 189 | # acquire new labels 190 | Label_predict = np.zeros((Category_0, 1)) 191 | 192 | index = 0 193 | for row in range(12, im_height - 12): 194 | for col in range(12, im_width - 12): 195 | if partition_result[row][col] != 100: 196 | if partition_result[row][col] == 0: 197 | Label_predict[index][0] = im_data_land2010[row][col] 198 | 199 | for j in range(predict.shape[1]): 200 | if predict_out[index][j] == 1: 201 | Label_predict[index][0] = j 202 | 203 | index += 1 204 | 205 | assert (index == Category_0) 206 | 207 | same_label_origin = 0 208 | for i in range(Category_0): 209 | if part_zero_labels_2005[i] == part_zero_labels_2010[i]: 210 | same_label_origin += 1 211 | 212 | print("The same label of partition one between im_data_land2010 and im_data_land2015 = ", same_label_origin) 213 | 214 | same_label = 0 215 | for i in range(Category_0): 216 | if part_zero_labels_2005[i] == Label_predict[i,0]: 217 | same_label += 1 218 | 219 | print("The same label of partition one between im_data_land2010and Label_predict = ", same_label) 220 | 221 | same = 0 222 | for i in range(Category_0): 223 | if part_zero_labels_2010[i] == Label_predict[i,0]: 224 | same += 1 225 | 226 | print("The same label of partition one between im_data_land2015 and Label_predict = ", same) 227 | print("the accuracy of prediction is:",same/Category_0) 228 | 229 | np.savetxt('/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/Partition_CNNs/part_zero/Label_predict_part_zero_CNN1year.txt', Label_predict, fmt='%s', newline='\n') -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Land-use-change-simulation-with-PST-CA 2 | A novel hybrid cellular automata model coupling area partitioning and spatiotemporal neighborhood features for LUC simulation Requirements (1) Python 3 (2) Tensorflow > 1.2 (3) Keras > 2.1.1 (4) Numpy 3 | 4 | Data (1) Time-series land use maps (2) Factors that driving land use changing such as accessibility to transport lines, accessibility to public infrastructures, and terrain conditions, which may include distance to motorway, trunk, primary road, minor road, railway, river; distance to city center, county center, town center, airport, train station, railway station, coach station, bus station, ferry station; Elevation and slope. 5 | 6 | Highlights (1) Self-organizing map was two different models to divide entire study area into sseveral relatively homogeneous sub-regions, which may address spatial heterogeneity existing in LUC. (2) 3D CNN was employed to capture spatiotemporal features contained among time-series land use maps. 7 | 8 | Models (1) K-means_cluster.py and SOM_cluster were two different methods used to partition entire study area. (2) LR_CA, SVM_CA, RF_CA and ANN_CA were four traditional models used for LUC simulation. (3) PST_CA was our proposed model, which coupled area partitioning and spatiotemporal neighborhood features for LUC simulation. (4) Accuracy_Kappa.py and FoM.py were used to assess the performance of these models. 9 | -------------------------------------------------------------------------------- /RF_CA.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import random 3 | from osgeo import gdal 4 | from sklearn import preprocessing 5 | from sklearn.ensemble import RandomForestClassifier 6 | from keras.utils import np_utils 7 | 8 | # Read land use classification layer data 9 | file_land2005 = '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Landuse_final/lan2005_final.tif' 10 | data_land2005 = gdal.Open(file_land2005) 11 | 12 | file_land2010 = '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Landuse_final/lan2010_final.tif' 13 | data_land2010 = gdal.Open(file_land2010) 14 | 15 | file_land2015 = '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Landuse_final/lan2015_final.tif' 16 | data_land2015 = gdal.Open(file_land2015) 17 | 18 | im_height = data_land2005.RasterYSize # Number of rows in the raster matrix 19 | im_width = data_land2005.RasterXSize # Number of columns in the raster matrix 20 | 21 | # Land use data 22 | im_data_land2005 = data_land2005.ReadAsArray(0, 0, im_width, im_height) 23 | im_data_land2010 = data_land2010.ReadAsArray(0, 0, im_width, im_height) 24 | im_data_land2015 = data_land2015.ReadAsArray(0, 0, im_width, im_height) 25 | 26 | number = 0 27 | # Number of pixels in Shanghai 28 | for row in range(12,im_height-12): 29 | for col in range(12,im_width-12): 30 | if im_data_land2005[row][col] != 0 : 31 | number = number + 1 32 | 33 | print("number of ShangHai:\n",number) 34 | 35 | # train samples are composed of AllFactors、Neighborhood features in 2005 36 | All_Factors_2005 = np.loadtxt( 37 | '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/AllFactors/AllFactors_20102017TXT.txt') 38 | 39 | Neighbor_11_11_2005 = np.loadtxt( 40 | '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Neighbor_field/Neighbor_field11_11/im_field_2005.txt') 41 | 42 | # samples and labels in 2005 43 | Sample_2005 = np.concatenate((All_Factors_2005, Neighbor_11_11_2005), axis=1) 44 | 45 | # Normalization function 46 | min_max_scaler = preprocessing.MinMaxScaler() 47 | Sample_2005_Norm = min_max_scaler.fit_transform(Sample_2005) 48 | 49 | Label_2005 = np.empty((number, 1)) 50 | index = 0 51 | for row in range(12, im_height - 12): 52 | for col in range(12, im_width - 12): 53 | if im_data_land2010[row][col] != 0: 54 | Label_2005[index, 0] = im_data_land2010[row][col] 55 | index += 1 56 | 57 | print("the shape of Sample_2005_Norm:", Sample_2005_Norm.shape) 58 | print("the shape of Label_2005:", Label_2005.shape) 59 | 60 | # train samples are composed of AllFactors、Neighborhood features in 2010 61 | All_Factors_2010 = np.loadtxt( 62 | '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/AllFactors/AllFactors_20102017TXT.txt') 63 | 64 | Neighbor_11_11_2010 = np.loadtxt( 65 | '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Neighbor_field/Neighbor_field11_11/im_field_2010.txt') 66 | 67 | # samples and labels in 2010 68 | Sample_2010 = np.concatenate((All_Factors_2010, Neighbor_11_11_2010), axis=1) 69 | 70 | # Normalization function 71 | min_max_scaler = preprocessing.MinMaxScaler() 72 | Sample_2010_Norm = min_max_scaler.fit_transform(Sample_2010) 73 | 74 | Label_2010 = np.empty((number, 1)) 75 | index = 0 76 | for row in range(12, im_height - 12): 77 | for col in range(12, im_width - 12): 78 | if im_data_land2015[row][col] != 0: 79 | Label_2010[index, 0] = im_data_land2015[row][col] 80 | index += 1 81 | 82 | print("the shape of Sample_2010_Norm:", Sample_2010_Norm.shape) 83 | print("the shape of Label_2010:", Label_2010.shape) 84 | 85 | # acquire training samples 86 | def random_int_list(start, stop, length): 87 | start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start)) 88 | length = int(abs(length)) if length else 0 89 | random_list = [] 90 | 91 | for i in range(length): 92 | random_list.append(random.randint(start, stop)) 93 | return np.array(random_list) 94 | 95 | 96 | random_list = random_int_list(0,Sample_2005_Norm.shape[0]-1,int(Sample_2005_Norm.shape[0]* 0.2)) 97 | 98 | ann_data = np.zeros((int(Sample_2005_Norm.shape[0]* 0.2),23)) 99 | ann_data_label = np.zeros((int(Sample_2005_Norm.shape[0]* 0.2),1)) 100 | for i in range(int(Sample_2005_Norm.shape[0]* 0.2)): 101 | temp = random_list[i] 102 | ann_data[i]= Sample_2005_Norm[temp] #ann_data: 20% of origin samples 103 | ann_data_label[i] = Label_2005[temp] #ann_data_label 104 | 105 | train_num = int(ann_data.shape[0] * 0.7) # set number of training samples 106 | test_num = ann_data.shape[0] - train_num # set number of test samples 107 | 108 | # acquire training samples from ann_data 109 | ann_data_train = np.zeros((train_num,23)) 110 | ann_data_test = np.zeros((test_num,23)) 111 | ann_data_train_label = np.zeros((train_num,1)) 112 | ann_data_test_label = np.zeros((test_num,1)) 113 | for i in range(train_num): 114 | ann_data_train[i] = ann_data[i] 115 | ann_data_train_label[i] = ann_data_label[i] 116 | for j in range(train_num,ann_data.shape[0]): 117 | ann_data_test[j - train_num] = ann_data[j] 118 | ann_data_test_label[j - train_num] = ann_data_label[j] 119 | 120 | # transform label into one-hot 121 | ann_data_train_label = np_utils.to_categorical(ann_data_train_label, num_classes=7) 122 | ann_data_test_label = np_utils.to_categorical(ann_data_test_label, num_classes=7) 123 | 124 | # Classification training using random forest,2006 data as input 125 | rfc = RandomForestClassifier(n_estimators=5) 126 | rfc.fit(ann_data_train, ann_data_train_label) 127 | 128 | Label_predict = rfc.predict(Sample_2010_Norm) 129 | 130 | # Gets the simulated array data 131 | data_new = np.zeros((im_height, im_width)) 132 | index = 0 133 | for row in range(12, im_height - 12): 134 | for col in range(12, im_width - 12): 135 | if im_data_land2010[row][col] != 0: 136 | data_new[row][col] = Label_predict[index][0] 137 | index = index + 1 138 | 139 | same_label_origin = 0 140 | for row in range(12, im_height - 12): 141 | for col in range(12, im_width - 12): 142 | if im_data_land2010[row][col] != 0: 143 | if im_data_land2010[row][col] == im_data_land2015[row][col]: 144 | same_label_origin = same_label_origin + 1 145 | 146 | print("The same label between im_data_land2010 and im_data_land2015 = ", same_label_origin) 147 | 148 | same_label = 0 149 | for row in range(12, im_height - 12): 150 | for col in range(12, im_width - 12): 151 | if im_data_land2010[row][col] != 0: 152 | if im_data_land2010[row][col] == data_new[row][col]: 153 | same_label = same_label + 1 154 | 155 | print("The same label between im_data_land2010 and data_new = ", same_label) 156 | 157 | same = 0 158 | for row in range(12, im_height - 12): 159 | for col in range(12, im_width - 12): 160 | if im_data_land2010[row][col] != 0: 161 | if im_data_land2015[row][col] == data_new[row][col]: 162 | same = same + 1 163 | 164 | print("The same label between im_data_land2015 and data_new = ", same) 165 | print("the accuracy of predict is:", same / number) 166 | 167 | np.savetxt('/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/Conventional_Models/RF_CA.txt', data_new, fmt='%s', newline='\n') -------------------------------------------------------------------------------- /SOM_cluster.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pylab as pl 3 | from osgeo import gdal 4 | from sklearn import preprocessing 5 | 6 | class SOM(object): 7 | def __init__(self, X, output, iteration, batch_size): 8 | """ 9 | :param X: The shape is N * D, there are N input samples, each D dimension 10 | :param output: (n, m) a tuple whose shape of the output layer is a two-dimensional matrix of n * m 11 | :param iteration: Number of iterations 12 | :param batch_size:Number of samples per iteration 13 | Initialize a weight matrix with the shape D * (n * m), that is, there are n * m weight vectors, each D dimension 14 | """ 15 | self.X = X 16 | self.output = output 17 | self.iteration = iteration 18 | self.batch_size = batch_size 19 | self.W = np.random.rand(X.shape[1], output[0] * output[1]) 20 | print(self.W.shape) 21 | 22 | def GetN(self, t): 23 | """ 24 | :param t: Time t, here the number of iterations is used to represent time 25 | :return: Returns an integer representing the topological distance. The larger the time, the smaller the topological neighborhood. 26 | """ 27 | a = min(self.output) 28 | return int(a - float(a) * t / self.iteration) 29 | 30 | def Geteta(self, t, n): 31 | """ 32 | :param t: Time t, here the number of iterations is used to represent time 33 | :param n: topological distance 34 | :return: return learning rate 35 | """ 36 | return np.power(np.e, -n) / (t + 2) 37 | 38 | def updata_W(self, X, t, winner): 39 | N = self.GetN(t) 40 | for x, i in enumerate(winner): 41 | to_update = self.getneighbor(i[0], N) 42 | for j in range(N + 1): 43 | e = self.Geteta(t, j) 44 | for w in to_update[j]: 45 | self.W[:, w] = np.add(self.W[:, w], e * (X[x, :] - self.W[:, w])) 46 | 47 | def getneighbor(self, index, N): 48 | """ 49 | :param index:Index of the winning neuron 50 | :param N: Neighborhood radius 51 | :return ans: Returns a list of sets of neuron coordinates that need to be updated in different neighborhood radii 52 | """ 53 | a, b = self.output 54 | length = a * b 55 | 56 | def distence(index1, index2): 57 | i1_a, i1_b = index1 // a, index1 % b 58 | i2_a, i2_b = index2 // a, index2 % b 59 | return np.abs(i1_a - i2_a), np.abs(i1_b - i2_b) 60 | 61 | ans = [set() for i in range(N + 1)] 62 | for i in range(length): 63 | dist_a, dist_b = distence(i, index) 64 | if dist_a <= N and dist_b <= N: ans[max(dist_a, dist_b)].add(i) 65 | return ans 66 | 67 | def train(self): 68 | """ 69 | train_Y: The training samples and shape are batch_size * (n * m) 70 | winner: A one-dimensional vector, the index of the batch_size winning neurons 71 | :return: The return value is adjusted W 72 | """ 73 | count = 0 74 | while self.iteration > count: 75 | train_X = self.X[np.random.choice(self.X.shape[0], self.batch_size)] 76 | normal_W(self.W) 77 | normal_X(train_X) 78 | train_Y = train_X.dot(self.W) 79 | winner = np.argmax(train_Y, axis=1).tolist() 80 | self.updata_W(train_X, count, winner) 81 | count += 1 82 | return self.W 83 | 84 | def train_result(self): 85 | normal_X(self.X) 86 | train_Y = self.X.dot(self.W) 87 | winner = np.argmax(train_Y, axis=1).tolist() 88 | # print(winner) 89 | return winner 90 | 91 | def normal_X(X): 92 | """ 93 | :param X: 2D matrix, N * D, N D-dimensional data 94 | :return: Normalize X results 95 | """ 96 | N, D = X.shape 97 | for i in range(N): 98 | temp = np.sum(np.multiply(X[i], X[i])) 99 | X[i] /= np.sqrt(temp) 100 | return X 101 | 102 | 103 | def normal_W(W): 104 | """ 105 | :param W: 2D matrix, D*(n*m) 106 | :return: Normalize 107 | """ 108 | for i in range(W.shape[1]): 109 | temp = np.sum(np.multiply(W[:, i], W[:, i])) 110 | W[:, i] /= np.sqrt(temp) 111 | return W 112 | 113 | 114 | # Read land use classification layer data 115 | file_land2000 = '/home/anaconda_workspace/qyh/SH_100m/Landuse_final/lan2000_final.tif' 116 | data_land2000 = gdal.Open(file_land2000) 117 | 118 | im_height = data_land2000.RasterYSize # Number of rows in the raster matrix 119 | im_width = data_land2000.RasterXSize # Number of columns in the raster matrix 120 | 121 | # Land use data by year 122 | im_data_land2000 = data_land2000.ReadAsArray(0, 0, im_width, im_height) 123 | 124 | # Prepare initial cluster data 125 | 126 | # Input impact factor data: an array of Cluster_SH * 17, these 17 columns are spatial impact factors 127 | Cluster_SH = np.loadtxt('/home/anaconda_workspace/qyh/SH_100m/AllFactors/AllFactors_20102017TXT.txt') 128 | 129 | # Normalized function 130 | min_max_scaler = preprocessing.MinMaxScaler() 131 | Cluster_SH_Norm = min_max_scaler.fit_transform(Cluster_SH) 132 | print("shape of Cluster_SH_Norm:",Cluster_SH_Norm.shape) 133 | 134 | Cluster_SH_Norm_NoBinary = np.mat(Cluster_SH_Norm) 135 | som_NoBinary = SOM(Cluster_SH_Norm_NoBinary, (2, 3), 16, 64) 136 | som_NoBinary.train() 137 | res_NoBinary = som_NoBinary.train_result() 138 | 139 | classify_result_NoBinary = {} 140 | # classify_result is a dictionary that stores keywords and corresponding categories under the keyword, 141 | # win [0] represents the category label, and i represents the number 142 | for i, win in enumerate(res_NoBinary): 143 | if not classify_result_NoBinary.get(win[0]): 144 | classify_result_NoBinary.setdefault(win[0], [i]) 145 | else: 146 | classify_result_NoBinary[win[0]].append(i) 147 | 148 | number = len(res_NoBinary) 149 | 150 | Label_NoBinary = np.empty((number,1)) 151 | for i in range(number): 152 | # temp is the label after clustering 153 | temp = res_NoBinary[i][0] 154 | Label_NoBinary[i,0] = int(temp) 155 | 156 | # Get clustered array data 157 | data_new_NoBinary = np.empty((im_height, im_width)) 158 | 159 | # To avoid confusion with clustering "0", initialize to 100 160 | for row in range(im_height): 161 | for col in range(im_width): 162 | data_new_NoBinary[row, col] = 6 163 | 164 | index = 0 165 | for row in range(12, im_height - 12): 166 | for col in range(12, im_width - 12): 167 | if im_data_land2000[row][col] != 0: 168 | data_new_NoBinary[row][col] = Label_NoBinary[index][0] 169 | index = index + 1 170 | 171 | np.savetxt('/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/Partition_Merely/SOM_cluster.txt', data_new_NoBinary, fmt='%s', newline='\n') -------------------------------------------------------------------------------- /SVM_CA.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import time 3 | import csv 4 | import random 5 | from osgeo import gdal 6 | from sklearn import preprocessing 7 | from sklearn.multiclass import OneVsRestClassifier 8 | from sklearn import svm 9 | from keras.utils import np_utils 10 | 11 | # Read land use classification layer data 12 | file_land2005 = '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Landuse_final/lan2005_final.tif' 13 | data_land2005 = gdal.Open(file_land2005) 14 | 15 | file_land2010 = '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Landuse_final/lan2010_final.tif' 16 | data_land2010 = gdal.Open(file_land2010) 17 | 18 | file_land2015 = '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Landuse_final/lan2015_final.tif' 19 | data_land2015 = gdal.Open(file_land2015) 20 | 21 | im_height = data_land2005.RasterYSize # Number of rows in the raster matrix 22 | im_width = data_land2005.RasterXSize # Number of columns in the raster matrix 23 | 24 | # Land use data 25 | im_data_land2005 = data_land2005.ReadAsArray(0, 0, im_width, im_height) 26 | im_data_land2010 = data_land2010.ReadAsArray(0, 0, im_width, im_height) 27 | im_data_land2015 = data_land2015.ReadAsArray(0, 0, im_width, im_height) 28 | 29 | number = 0 30 | # Number of pixels in Shanghai 31 | for row in range(12,im_height-12): 32 | for col in range(12,im_width-12): 33 | if im_data_land2005[row][col] != 0 : 34 | number = number + 1 35 | 36 | print("number of ShangHai:\n",number) 37 | 38 | # train samples are composed of AllFactors、Neighborhood features in 2005 39 | All_Factors_2005 = np.loadtxt( 40 | '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/AllFactors/AllFactors_20102017TXT.txt') 41 | 42 | Neighbor_11_11_2005 = np.loadtxt( 43 | '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Neighbor_field/Neighbor_field11_11/im_field_2005.txt') 44 | 45 | # samples and labels in 2005 46 | Sample_2005 = np.concatenate((All_Factors_2005, Neighbor_11_11_2005), axis=1) 47 | 48 | # Normalization function 49 | min_max_scaler = preprocessing.MinMaxScaler() 50 | Sample_2005_Norm = min_max_scaler.fit_transform(Sample_2005) 51 | 52 | Label_2005 = np.empty((number, 1)) 53 | index = 0 54 | for row in range(12, im_height - 12): 55 | for col in range(12, im_width - 12): 56 | if im_data_land2010[row][col] != 0: 57 | Label_2005[index, 0] = im_data_land2010[row][col] 58 | index += 1 59 | 60 | print("the shape of Sample_2005_Norm:", Sample_2005_Norm.shape) 61 | print("the shape of Label_2005:", Label_2005.shape) 62 | 63 | # train samples are composed of AllFactors、Neighborhood features in 2010 64 | All_Factors_2010 = np.loadtxt( 65 | '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/AllFactors/AllFactors_20102017TXT.txt') 66 | 67 | Neighbor_11_11_2010 = np.loadtxt( 68 | '/home/kwan/Workspace/qyh/data/SH_100m/ExperimentalData/Neighbor_field/Neighbor_field11_11/im_field_2010.txt') 69 | 70 | # samples and labels in 2010 71 | Sample_2010 = np.concatenate((All_Factors_2010, Neighbor_11_11_2010), axis=1) 72 | 73 | # Normalization function 74 | min_max_scaler = preprocessing.MinMaxScaler() 75 | Sample_2010_Norm = min_max_scaler.fit_transform(Sample_2010) 76 | 77 | Label_2010 = np.empty((number, 1)) 78 | index = 0 79 | for row in range(12, im_height - 12): 80 | for col in range(12, im_width - 12): 81 | if im_data_land2015[row][col] != 0: 82 | Label_2010[index, 0] = im_data_land2015[row][col] 83 | index += 1 84 | 85 | print("the shape of Sample_2010_Norm:", Sample_2010_Norm.shape) 86 | print("the shape of Label_2010:", Label_2010.shape) 87 | 88 | # acquire training samples 89 | def random_int_list(start, stop, length): 90 | start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start)) 91 | length = int(abs(length)) if length else 0 92 | random_list = [] 93 | 94 | for i in range(length): 95 | random_list.append(random.randint(start, stop)) 96 | return np.array(random_list) 97 | 98 | 99 | random_list = random_int_list(0,Sample_2005_Norm.shape[0]-1,int(Sample_2005_Norm.shape[0]* 0.2)) 100 | 101 | ann_data = np.zeros((int(Sample_2005_Norm.shape[0]* 0.2),23)) 102 | ann_data_label = np.zeros((int(Sample_2005_Norm.shape[0]* 0.2),1)) 103 | for i in range(int(Sample_2005_Norm.shape[0]* 0.2)): 104 | temp = random_list[i] 105 | ann_data[i]= Sample_2005_Norm[temp] #ann_data: 20% of origin samples 106 | ann_data_label[i] = Label_2005[temp] #ann_data_label 107 | 108 | train_num = int(ann_data.shape[0] * 0.7) # set number of training samples 109 | test_num = ann_data.shape[0] - train_num # set number of test samples 110 | 111 | # acquire training samples from ann_data 112 | ann_data_train = np.zeros((train_num,23)) 113 | ann_data_test = np.zeros((test_num,23)) 114 | ann_data_train_label = np.zeros((train_num,1)) 115 | ann_data_test_label = np.zeros((test_num,1)) 116 | for i in range(train_num): 117 | ann_data_train[i] = ann_data[i] 118 | ann_data_train_label[i] = ann_data_label[i] 119 | for j in range(train_num,ann_data.shape[0]): 120 | ann_data_test[j - train_num] = ann_data[j] 121 | ann_data_test_label[j - train_num] = ann_data_label[j] 122 | 123 | # transform label into one-hot 124 | ann_data_train_label = np_utils.to_categorical(ann_data_train_label, num_classes=7) 125 | ann_data_test_label = np_utils.to_categorical(ann_data_test_label, num_classes=7) 126 | 127 | model = OneVsRestClassifier(svm.SVC(kernel='rbf')) 128 | clf = model.fit(ann_data_train,ann_data_train_label) 129 | Label_predict = clf.predict(Sample_2010_Norm) 130 | 131 | # Gets the simulated array data 132 | data_new = np.zeros((im_height, im_width)) 133 | index = 0 134 | for row in range(12, im_height - 12): 135 | for col in range(12, im_width - 12): 136 | if im_data_land2010[row][col] != 0: 137 | data_new[row][col] = Label_predict[index][0] 138 | index = index + 1 139 | 140 | same_label_origin = 0 141 | for row in range(12, im_height - 12): 142 | for col in range(12, im_width - 12): 143 | if im_data_land2010[row][col] != 0: 144 | if im_data_land2010[row][col] == im_data_land2015[row][col]: 145 | same_label_origin = same_label_origin + 1 146 | 147 | print("The same label between im_data_land2010 and im_data_land2015 = ", same_label_origin) 148 | 149 | same_label = 0 150 | for row in range(12, im_height - 12): 151 | for col in range(12, im_width - 12): 152 | if im_data_land2010[row][col] != 0: 153 | if im_data_land2010[row][col] == data_new[row][col]: 154 | same_label = same_label + 1 155 | 156 | print("The same label between im_data_land2010 and data_new = ", same_label) 157 | 158 | same = 0 159 | for row in range(12, im_height - 12): 160 | for col in range(12, im_width - 12): 161 | if im_data_land2010[row][col] != 0: 162 | if im_data_land2015[row][col] == data_new[row][col]: 163 | same = same + 1 164 | 165 | print("The same label between im_data_land2015 and data_new = ", same) 166 | print("the accuracy of predict is:", same / number) 167 | 168 | np.savetxt('/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/Conventional_Models/SVM_CA.txt', data_new, fmt='%s', newline='\n') -------------------------------------------------------------------------------- /k-means_cluster.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pylab as pl 3 | from sklearn.cluster import KMeans 4 | from osgeo import gdal 5 | from sklearn import preprocessing 6 | 7 | # Read land use classification layer data 8 | file_land2000 = '/home/anaconda_workspace/qyh/SH_100m/ExperimentalData/Landuse_final/lan2000_final.tif' 9 | data_land2000 = gdal.Open(file_land2000) 10 | 11 | im_height = data_land2000.RasterYSize # Number of rows in the raster matrix 12 | im_width = data_land2000.RasterXSize # Number of columns in the raster matrix 13 | 14 | # Land use data by year 15 | im_data_land2000 = data_land2000.ReadAsArray(0, 0, im_width, im_height) 16 | 17 | # Prepare initial cluster data 18 | # 19 | # # Input impact factor data: an array of Cluster_SH * 17, these 17 columns are spatial impact factors 20 | Cluster_SH = np.loadtxt('/home/anaconda_workspace/qyh/SH_100m/ExperimentalData/AllFactors/AllFactors_20102017TXT.txt') 21 | 22 | # Normalized function 23 | min_max_scaler = preprocessing.MinMaxScaler() 24 | Cluster_SH_Norm = min_max_scaler.fit_transform(Cluster_SH) 25 | Cluster_SH_Norm_mat = np.mat(Cluster_SH_Norm) 26 | 27 | print("shape of Cluster_SH_Norm_mat:",Cluster_SH_Norm_mat.shape) 28 | 29 | # Construct NoBinary clusterer 30 | estimator_NoBinary = KMeans(n_clusters=16)# Construct NoBinary clusterer 31 | estimator_NoBinary.fit(Cluster_SH_Norm_mat)# clusterer 32 | label_pred_NoBinary = estimator_NoBinary.labels_ # Get cluster labels 33 | centroids_NoBinary = estimator_NoBinary.cluster_centers_ # Get Cluster Center 34 | inertia_NoBinary = estimator_NoBinary.inertia_ # Get the sum of the clustering criteria 35 | 36 | Cluster_result_NoBinary = np.zeros((im_height, im_width)) 37 | for row in range(0, im_height): 38 | for col in range(0, im_width): 39 | Cluster_result_NoBinary[row, col] = 100 40 | 41 | index = 0 42 | for row in range(12, im_height - 12): 43 | for col in range(12, im_width - 12): 44 | if im_data_land2000[row][col] != 0: 45 | Cluster_result_NoBinary[row][col] = label_pred_NoBinary[index] 46 | index = index + 1 47 | 48 | np.savetxt('/home/anaconda_workspace/qyh/SH_100m/ExperimentalResult/Clustering/K-Means_cluster.txt', Cluster_result_NoBinary, fmt='%s', newline='\n') --------------------------------------------------------------------------------