Python 3.x 如何在新图像上测试复杂的CNN模型?

Python 3.x 如何在新图像上测试复杂的CNN模型?,python-3.x,deep-learning,conv-neural-network,satellite-image,Python 3.x,Deep Learning,Conv Neural Network,Satellite Image,我正在学习美国有线电视新闻网(CNN),我在网上找到了一个脚本,可以根据卫星图像对建筑物屋顶进行分类。该脚本工作正常,但我无法找到在新的单个图像上测试脚本的方法。我将简要地展示代码,然后展示我尝试过的内容: seq = iaa.Sequential([ iaa.imgcorruptlike.Fog(severity=1), iaa.imgcorruptlike.Spatter(severity =1), ]) batch_size = 16 size = 512 epochs

我正在学习美国有线电视新闻网(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], (3, 3), 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),ysize=extent[1],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, (inputsize-size)+1, stride):
        
            s = arr[i:(i+size),j:(j+size), ]
            result.append(s)
 
    result = np.array(result)
    return result

def batchslice (arr, size, inputsize, stride, num_img):
    result = []
    for i in range(0, num_img):
        s= slice(arr[i,], size, inputsize, 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, size, Y.shape[1], size, Y.shape[0]).squeeze()
X_cl =batchslice(X_cl, size, X_cl.shape[1], size, 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:,] 
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,2000,3))
testpre = model.predict(test_image1)
img = Image.fromarray(test_image, 'RGB')
img.show()
然后是大型unet模型架构。可以找到整个脚本

此模型仅适用于数据集。我正在尝试用我自己的数据集外图像进行测试,这是我尝试过的:

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], (3, 3), 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),ysize=extent[1],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, (inputsize-size)+1, stride):
        
            s = arr[i:(i+size),j:(j+size), ]
            result.append(s)
 
    result = np.array(result)
    return result

def batchslice (arr, size, inputsize, stride, num_img):
    result = []
    for i in range(0, num_img):
        s= slice(arr[i,], size, inputsize, 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, size, Y.shape[1], size, Y.shape[0]).squeeze()
X_cl =batchslice(X_cl, size, X_cl.shape[1], size, 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:,] 
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,2000,3))
testpre = model.predict(test_image1)
img = Image.fromarray(test_image, 'RGB')
img.show()
我的测试图像的原始形状是
(18523312,3)
。 我得到了一个奇怪的预测图像,这与预期毫无意义。我相信,我对我的测试图像做了错误的预处理。任何帮助都将不胜感激


整个脚本都可以找到。

你看到他们是如何预测的了吗?我没有做任何测试,但他们使用自己的函数plot\u imgs来绘制预测。如果按ctrl-f键选择plot_imgs,您将发现
plot_imgs(org_imgs=X_test[:,:,:],mask_imgs=Y_test[:,:,:],pred_imgs=testpre[,:,:],nm_img_to_plot=3)#可选-要打印的图像数
他们使用plot_imgs()绘制预测。预测是这样做的:
testpre=unet\u model.predict(X\u test[0:1,])
那么为什么不这样做呢?它们是为测试数据集做的。我要求一个新的外国形象。如何实现?他们使用load_rasters_simple函数加载数据。似乎他们使用gdal包来处理geodata(?)。对不起,我不是很确定。但是他们的图像是512x512x3,比你的小。但是你可以下载他们的数据集,看看你是否可以让它与他们的图像一起工作。这将提供一些信息,让它在您自己的图像工作。