我的目标是训练我的卷积神经网络来识别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的定义有什么问题吗?
发布于 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"
,这意味着没有填充。你不能有负维空间。
tf.keras.layers.Conv2D(71, (3, 3), strides=(2,2), padding="same", dilation_rate=(1,1), activation='relu'),
https://stackoverflow.com/questions/72872021
复制相似问题