首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ValueError:调用层"conv2d_1“时遇到的异常(键入Conv2D)

ValueError:调用层"conv2d_1“时遇到的异常(键入Conv2D)
EN

Stack Overflow用户
提问于 2022-07-05 15:31:40
回答 1查看 574关注 0票数 0

我的目标是训练我的卷积神经网络来识别png图像。我首先把图像转换成张量。

代码语言:javascript
运行
复制
file_path = f"./STFT_spectra/STFT_spectra0.png"
image = io.read_file(file_path)
image = io.decode_png(image)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.image.resize(image, [128,128])
print("----Here-----")
print(type(image))
print(image.shape)

输出:

代码语言:javascript
运行
复制
----Here-----
<class 'tensorflow.python.framework.ops.EagerTensor'>
(128, 128, 4)

然后,转换所有生成的图像,并将numpy数组保存为硬盘上的"image_list.p“文件。

代码语言:javascript
运行
复制
# total = 100
# image_list = np.empty(shape=(total, 128, 128, 4))
# for i in tqdm(range(total)):
#     file_path = f"./STFT_spectra/STFT_spectra{i}.png"
#     image = io.read_file(file_path)
#     image = io.decode_png(image)
#     image = tf.image.convert_image_dtype(image, tf.float32)
#     image = tf.image.resize(image, [128, 128])
#     image_list[i] = image

# pickle.dump(image_list, open("image_list.p", "wb"))

至于实际情况,每个标签是10个浮动组合,如0.2、0.3、0.5、0.6、0.9、0.5、0.4、0.6、0.7、0.1。

代码语言:javascript
运行
复制
Then, I assemble the dataset:
labels = pickle.load(open(".././labels.p", "rb"))
fetched_image_list = pickle.load(open("../image_list.p", "rb"))
fetched_image_list = fetched_image_list.reshape(fetched_image_list.shape[0],
                                                fetched_image_list.shape[1],
                                                fetched_image_list.shape[2],
                                                fetched_image_list.shape[3],
                                                1)

dataset = tf.data.Dataset.from_tensor_slices((fetched_image_list, labels))

CNN的模式是这样的:

代码语言:javascript
运行
复制
model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(32, (3, 3), strides=(2,2), dilation_rate=(1,1), input_shape=(128,128,4,1), activation='relu'),
    tf.keras.layers.Conv2D(71, (3, 3), strides=(2,2), dilation_rate=(1,1), activation='relu'),
    tf.keras.layers.Conv2D(128, (3, 4), strides=(2,3), dilation_rate=(1,1),activation='relu'),
    tf.keras.layers.Conv2D(128, (3, 3), strides=(2,2), dilation_rate=(1,1),activation='relu'),
    tf.keras.layers.Conv2D(128, (3, 4), strides=(2, 3), dilation_rate=(1, 1), activation='relu'),
    tf.keras.layers.Conv2D(128, (3, 3), strides=(2, 2), dilation_rate=(1, 1), activation='relu'),
    tf.keras.layers.Dropout(0.20),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(10)
])

一切看起来都很好,但问题来了,

代码语言:javascript
运行
复制
ValueError: Exception encountered when calling layer "conv2d_1" (type Conv2D).

Negative dimension size caused by subtracting 3 from 1 for '{{node conv2d_1/Conv2D/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true](conv2d_1/Conv2D/Reshape, conv2d_1/Conv2D/Conv2D/ReadVariableOp)' with input shapes: [?,63,1,32], [3,3,32,71].

Call arguments received by layer "conv2d_1" (type Conv2D):
  • inputs=tf.Tensor(shape=(None, 128, 63, 1, 32), dtype=float32)

我怎样才能解决这个问题?CNN的定义有什么问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-05 20:32:34

你需要使用填充与你的卷积层。你已经没有空间了。

当输入形状为:?,63,1,32,3,32,71的{nodecon2d_1/conv2D/conv2D}}= Conv2DT=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true‘时,从1中减去3的Conv2D负维数。

所以添加,至少在第二个卷积层,padding="same",在默认情况下,它是"valid",这意味着没有填充。你不能有负维空间。

代码语言:javascript
运行
复制
tf.keras.layers.Conv2D(71, (3, 3), strides=(2,2), padding="same", dilation_rate=(1,1), activation='relu'),
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72872021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档