如何将不同尺寸的图像输入CNN?

发布于 2025-01-11 20:31:14 字数 1535 浏览 1 评论 0原文

我有一个灰度图像数据集,我想在卷积神经网络中使用它来进行预测。 这些图像的尺寸范围很广,出于多种原因,我不想调整它们的大小、填充或裁剪它们以获得相同的形状。 从CNN的结构来看,解决方案是添加一个GlobalMaxPooling2D或GlobalAveragePooling层作为 CNN 提取部分的最后一步,为了每个特征图获得一个神经元,因此一维数组仅 取决于滤波器的数量(即与输入图像的大小无关)。 此外,CNN 的第一层应具有输入:“input_shape=(None,None,1)”,并且 model.fit 应使用“batch size=1”。

这很好,但我不知道如何向 CNN 提供不同大小的图像,因为我无法将它们堆叠到单个数组结构中。 我尝试将每个单独的图像放入数组列表中,或者放入具有 dtype= 对象格式的 numpy 数组中,但当我运行“model.fit”命令时总是出现错误。根据我使用的输入类型,我会收到如下错误:“确保所有数组包含相同数量的样本”或“无法将 NumPy 数组转换为张量(不支持的对象类型 numpy.ndarray)”或“ValueError:数据”基数不明确:x 大小:235 y 大小:50000 确保所有数组包含相同数量的样本。')。 我还使用 y_train 作为一维列表或作为 numpy 数组,或作为分类变量,但它没有帮助。

我已经多次看到此类问题,但我从未见过一些代码显示如何“真实地”向 CNN 提供非均匀图像。 有什么想法吗?

我使用的基本代码如下(keras with tensorflow)

from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, MaxPooling2D, GlobalMaxPooling2D

model = Sequential() 
model.add(Conv2D(10, kernel_size=(7,7), padding='same', strides=(1,1), activation='relu', input_shape=(None,None,1))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
model.add(Conv2D(10, kernel_size=(5,5), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(10, kernel_size=(3,3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(GlobalMaxPooling2D(None,10))
model.add(Flatten())
model.add(Dense(3,activation='softmax')) 

model.fit(x=x_train, y=y_train, validation_data=(x_val, y_val), batch_size=1, epochs=10) 

I have a dataset of grayscale images that I want to use in a Convolutionnal Neural Network, for prediction purposes.
The images have a very wide range of sizes, and for multiple reasons, I don't want to resize or pad or crop them to get the same shape.
In terms of structure of the CNN, a solution is to add a GlobalMaxPooling2D or a GlobalAveragePooling layer as the
last step of the extraction part of the CNN, in order to get a single neuron per feature map, hence a 1D array that only
depends on the number of filters (i.e. is independent of the size of the input images).
Also, the first layer of the CNN should have as input: 'input_shape=(None,None,1)', and model.fit should use 'batch size=1'.

That's very fine, but I don't how to feed my CNN with images of different sizes, as I can't stack them into a single array structure.
I'v tried to put each individual image in a list of arrays, or in a numpy array with dtype= object format, but I always get an error when I run the 'model.fit' command. Depending on the input type I use, I get errors like: 'Make sure all arrays contain the same number of samples' or 'Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)' or 'ValueError: Data cardinality is ambiguous: x sizes: 235 y sizes: 50000 Make sure all arrays contain the same number of samples.').
I also used y_train as a 1D list or as an numpy array, or as a categorical variable, but it doesn't help.

I've seen this type of question several times, but I've never seen some code showing how to feed the CNN with inhomogeneous images 'in real'.
Any idea?

The basic code I use is as follows (keras with tensorflow):

from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, MaxPooling2D, GlobalMaxPooling2D

model = Sequential() 
model.add(Conv2D(10, kernel_size=(7,7), padding='same', strides=(1,1), activation='relu', input_shape=(None,None,1))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
model.add(Conv2D(10, kernel_size=(5,5), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(10, kernel_size=(3,3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(GlobalMaxPooling2D(None,10))
model.add(Flatten())
model.add(Dense(3,activation='softmax')) 

with

model.fit(x=x_train, y=y_train, validation_data=(x_val, y_val), batch_size=1, epochs=10) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文