继上篇。
提取特征。
import gdal
from keras import Model
from keras.models import load_model
import numpy as np
import cv2
import os
import pandas as pd
model=load_model("output/classify3_vgg16_pro.h5")
# model.trainable=False
dense1_layer_model = Model(inputs=model.input,outputs=model.layers[-2].output)
dense1_layer_model.trainable=False
dense1_layer_model.summary()
dataset = gdal.Open(r"D:/test.tif")
size=128
def get_cell( pos_x, pos_y):
try:
output = []
for i in [3,2,1]:
band = dataset.GetRasterBand(i)
t = band.ReadAsArray(int(pos_x - size / 2), int(pos_y - size / 2), size, size)
output.append(t)
img2 = np.moveaxis(np.array(output, dtype=np.uint8), 0, 2)
# print(img2.shape)
# self.showImg(img2)
except:
return None
return img2
def get_cells():
res=[]
fea = pd.read_csv("tr_1.txt", header=None) # 读取坐标-特征文件
fea_len = len(fea)
print("fea length: %d" % fea_len)
for i in range(fea_len):
temp = fea.iloc[i, :].values
img = get_cell(temp[1], temp[0])
if img is None:
continue
input=[]
input.append(img)
input=np.array(input)
pred_y=dense1_layer_model.predict(input).reshape(-1)
# 并上其他特征
res.append(np.append(pred_y,temp[2:]))
if(i%1000==0):
print("%d fininsh." % i)
res=np.array(res)
df=pd.DataFrame(res)
print(df.shape)
df.to_csv("output/1024&108feather.txt",header=False,index=None)
get_cells()
使用其他分类器分类(RF)
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder,StandardScaler
from sklearn.ensemble import RandomForestClassifier
data=pd.read_csv("output/1024&108feather.txt",header=None)
print(data.iloc[:5])
X=data.iloc[:,:-2]
Y=data.iloc[:,-2]
scaler = StandardScaler().fit(X)
X_scaler=scaler.transform(X)
label_t=LabelEncoder().fit(Y)
Y_t=label_t.transform(Y)
rf = RandomForestClassifier(n_estimators=100, max_depth=20)
rf.fit(X_scaler,Y_t)
data_test=pd.read_csv('output/test_1024&108feather',header=None)
X_test=data_test.iloc[:,:-2]
Y_test=data_test.iloc[:,-2]
X_test_scaler=scaler.transform(X_test)
Y_test_t=label_t.transform(Y_test)
score=rf.score(X_test_scaler,Y_test_t)
print(score)
作为对比,直接使用其他特征使用其他分类器分类。
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder,StandardScaler
from sklearn.ensemble import RandomForestClassifier
data=pd.read_csv(r"D:/train1.txt",header=None, sep='\t')
print(data.iloc[:5])
X=data.iloc[:,:-2]
Y=data.iloc[:,-2]
scaler = StandardScaler().fit(X)
X_scaler=scaler.transform(X)
label_t=LabelEncoder().fit(Y)
Y_t=label_t.transform(Y)
rf = RandomForestClassifier(n_estimators=100, max_depth=20)
rf.fit(X_scaler,Y_t)
data_test=pd.read_csv(r"D:/test1.txt",header=None, sep='\t')
X_test=data_test.iloc[:,:-2]
Y_test=data_test.iloc[:,-2]
X_test_scaler=scaler.transform(X_test)
Y_test_t=label_t.transform(Y_test)
score=rf.score(X_test_scaler,Y_test_t)
print(score)