问题描述
我正在学习CNN,并且在网上找到了一个脚本,该脚本根据卫星图像对建筑物的屋顶进行分类。该脚本可以正常工作,但是我无法找到一种在新的单个图像上测试脚本的方法。我将简要显示代码,然后将显示我尝试过的内容:
seq = iaa.Sequential([
iaa.imgcorruptlike.Fog(severity=1),iaa.imgcorruptlike.Spatter(severity =1),])
batch_size = 16
size = 512
epochs =50
version = 1 # version 2 for MobilV2unet
data_augmentation = True
model_type = 'UNet%d' % (version)
translearn = True
from tensorflow.keras.applications import MobileNetV2
def m_u_net(input_shape):
inputs = Input(shape=input_shape,name="input_image")
encoder = MobileNetV2(input_tensor=inputs,weights="imagenet",include_top=False,alpha=1.3)
#encoder.trainable=False
skip_connection_names = ["input_image","block_1_expand_relu","block_3_expand_relu","block_6_expand_relu"]
encoder_output = encoder.get_layer("block_13_expand_relu").output
f = [16,32,48,64]
x = encoder_output
for i in range(1,len(skip_connection_names)+1,1):
x_skip = encoder.get_layer(skip_connection_names[-i]).output
x = UpSampling2D((2,2))(x)
x = Concatenate()([x,x_skip])
x = Conv2D(f[-i],(3,3),padding="same")(x)
x = Batchnormalization()(x)
x = Activation("relu")(x)
x = Conv2D(f[-i],padding="same")(x)
x = Batchnormalization()(x)
x = Activation("relu")(x)
x = Conv2D(1,(1,1),padding="same")(x)
x = Activation("sigmoid")(x)
model = Model(inputs,x)
return model
def load_rasters_simple(path,pathX,pathY ): # Subset from original raster with extent and upperleft coord
"""Load training data pairs (two high resolution images and two low resolution images)"""
pathXabs = os.path.join(path,pathX)
pathYabs = os.path.join(path,pathY)
le = len(os.listdir(pathXabs) )
stackX = []
stackY = []
for i in range(0,le):
fileX = os.path.join(pathXabs,os.listdir(pathXabs)[i])
fileY = os.path.join(pathYabs,os.listdir(pathXabs)[i])
datax = gdal_array.LoadFile(fileX) #.astype(np.int),ysize=extent[1],xsize=extent[0]
stackX.append(datax)
dataY = gdal_array.LoadFile(fileY) #.astype(np.int),xsize=extent[0]
stackY.append(dataY)
stackX = np.array(stackX)
stackY = np.array(stackY)
return stackX,stackY
X,Y= load_rasters_simple('/Users/vaibhavsaxena/Desktop/segmentation/Classification/Satellite dataset ó± (global cities)','image','label')
def slice (arr,size,inputsize,stride):
result = []
if stride is None:
stride = size
for i in range(0,(inputsize-size)+1,stride):
for j in range(0,stride):
s = arr[i:(i+size),j:(j+size),]
result.append(s)
result = np.array(result)
return result
def batchslice (arr,stride,num_img):
result = []
for i in range(0,num_img):
s= slice(arr[i,],stride )
result.append(s )
result = np.array(result)
result = result.reshape(result.shape[0]*result.shape[1],result.shape[2],result.shape[3],-1)
return result
Y=batchslice(Y,Y.shape[1],Y.shape[0]).squeeze()
X_cl =batchslice(X_cl,X_cl.shape[1],X_cl.shape[0])
X_train = X_cl[:int(X_cl.shape[0]*0.8),]
Y_train = Y[:int(Y.shape[0]*0.8),]
X_test = X_cl[int(X_cl.shape[0]*0.8)+1:,]
Y_test = Y[int(Y.shape[0]*0.8)+1:,]
大型的unet模型架构。整个脚本可以在here中找到。
此模型仅适用于数据集。我正在尝试使用自己的数据集图像进行测试,这就是我尝试过的:
model = load_model('no_aug_unet_model.h5',custom_objects=dependencies)
model.compile(loss='binary_crossentropy',metrics=[IoU],optimizer=Adam(learning_rate=lr_schedule(0)))
from keras.preprocessing import image
test_image= image.load_img('bangkok_noi_2.jpg',target_size = (2000,2000))
test_image = image.img_to_array(test_image)
test_image1 = test_image.reshape((1,2000,3))
testpre = model.predict(test_image1)
img = Image.fromarray(test_image,'RGB')
img.show()
我的测试图像的原始形状为(1852,3312,3)
。
我得到一张奇怪的预测图像,与预期没有任何意义。我相信,我对测试图像进行了错误的预处理。任何帮助将不胜感激。
可以找到整个脚本here。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)