Keras 2d填充和输入

2024-04-25 07:25:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试着用不同尺寸的图像给2dcnn。为了这个目的(因为我不想重塑我的图像),我尝试执行2D填充。在

问题是:要传送到网络的图像被读取并转换为三维烦恼(形状为:(80,35,3)。由于图像的形状不同,我无法创建np.数组把它们放在一个列表里。因此,著名的错误是:

"Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 18418 arrays: " etc.

所以问题是:如何将这些图像输入填充层?在

我尝试过很多方法,比如在开始时调用输入:

    inputs = Input(shape = (None, None, 3,))

或者配置填充层如下:

^{pr2}$

但我做不好。在

有人会有解决办法吗?在

提前谢谢你


Tags: oftheto图像目的网络nonemodel
1条回答
网友
1楼 · 发布于 2024-04-25 07:25:22

填充

如果您想填充(这会使您的模型由于执行太多不必要的操作而变慢),则必须在模型之外使用numpy进行填充。在

如果您有一个图像列表,如numpy数组列表,则每个numpy的形状为(side1,side2,3)

desiredX = someValue
desiredY = someValue
padded_images = []

for img in list_of_images:
    shape = img.shape
    xDiff = desiredX - shape[0]
    xLeft = xDiff//2
    xRight = xDiff-xLeft

    yDiff = desiredY - shape[1]
    yLeft = yDiff//2
    yRight = yDiff - yLeft

    padded_images.append(np.pad(img,((xLeft,xRight),(yLeft,yRight),(0,0)), mode='constant')
         #or choose another mode

padded_images = np.asarray(padded_images) #this can go into the model

单独培训

或者,您可以训练一个图像的批处理,或者对具有相同大小的图像进行小批量分组。(我不知道哪个更有效,但如果尺寸差异太大,这可能更好)

^{pr2}$

相关问题 更多 >