[英]Python - Keras: Exception encountered when calling layer "conv2d_19" (type Conv2D)
[英]ValueError: Exception encountered when calling layer "conv2d_1" (type Conv2D)
我的目标是训练我的卷积神经网络来识别 png 图像。 我首先将图像转换为张量。
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)
输出:
----Here-----
<class 'tensorflow.python.framework.ops.EagerTensor'>
(128, 128, 4)
然后,我转换所有生成的图像,并将 numpy 数组保存为硬盘上的“image_list.p”文件。
# 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]。
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 模型是这样的:
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)
])
一切看起来都很好,但问题来了,
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的定义有问题吗?
您需要对卷积层使用填充。 您的维度空间不足。
'{{node conv2d_1/Conv2D/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[] 由 1 减去 3 导致的负维度大小, padding="VALID", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true](conv2d_1/Conv2D/Reshape, conv2d_1/Conv2D/Conv2D/ReadVariableOp)' 输入形状:[?,63,1, 32],[3,3,32,71]。
因此,至少在第二个卷积层中添加padding="same"
,默认情况下它是"valid"
,这意味着没有填充。 你不能有负维度空间。
tf.keras.layers.Conv2D(71, (3, 3), strides=(2,2), padding="same", dilation_rate=(1,1), activation='relu'),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.