下面的问题是我在设计一个基本的自动编码器拱门时遇到的一个实际问题的简化。下面的例子足以准确地再现我的错误。我已经试了两天了,但我找不到任何出路
import tensorflow as tf
import random
import os
RES = [256, 256]
def generator_data(n):
for i in range(n):
for j in range(6):
yield tf.zeros((1, 256, 256, 3)), tf.zeros((1, 256, 256, 3))
def mymodel():
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same'))
# 256 x 256 x 8
model.add(tf.keras.layers.MaxPooling2D((2, 2), padding='same'))
# 128 x 128 x 8
model.add(tf.keras.layers.Conv2D(16, (3, 3), activation='relu', padding='same'))
# 128 x 128 x 16
model.add(tf.keras.layers.MaxPooling2D((2, 2), padding='same'))
# 64 x 64 x 16
model.add(tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same'))
# 64 x 64 x 32
model.add(tf.keras.layers.MaxPooling2D((2, 2), padding='same'))
# 32 x 32 x 32
# 32 x 32 x 32
model.add(tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same'))
# 32 x 32 x 32
model.add(tf.keras.layers.UpSampling2D((2, 2)))
# 64 x 64 x 32
model.add(tf.keras.layers.Conv2D(16, (3, 3), activation='relu', padding='same'))
# 64 x 64 x 16
model.add(tf.keras.layers.UpSampling2D((2, 2)))
# 128 x 128 x 16
model.add(tf.keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same'))
# 128 x 128 x 8
model.add(tf.keras.layers.UpSampling2D((2, 2)))
# 256 x 256 x 8
model.add(tf.keras.layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same'))
return model
if __name__ == "__main__":
# import some data to play with
x_val, y_val = zip(*generator_data(20))
model = mymodel()
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
model.compile(optimizer=optimizer, loss=tf.keras.losses.MeanSquaredError())
model(tf.zeros((1, 256, 256, 3)))
model.summary()
# generator_data(train_list)
model.fit(x=generator_data(1000),
validation_data=(list(x_val), list(y_val)),
verbose=1, epochs=1000)
首先,我有一个模型的奇怪行为。summary((包含:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) multiple 224
_________________________________________________________________
max_pooling2d (MaxPooling2D) multiple 0
_________________________________________________________________
conv2d_1 (Conv2D) multiple 1168
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 multiple 0
_________________________________________________________________
conv2d_2 (Conv2D) multiple 4640
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 multiple 0
_________________________________________________________________
conv2d_3 (Conv2D) multiple 9248
_________________________________________________________________
up_sampling2d (UpSampling2D) multiple 0
_________________________________________________________________
conv2d_4 (Conv2D) multiple 4624
_________________________________________________________________
up_sampling2d_1 (UpSampling2 multiple 0
_________________________________________________________________
conv2d_5 (Conv2D) multiple 1160
_________________________________________________________________
up_sampling2d_2 (UpSampling2 multiple 0
_________________________________________________________________
conv2d_6 (Conv2D) multiple 73
=================================================================
Total params: 21,137
Trainable params: 21,137
Non-trainable params: 0
输出形状上只有多个。我在这里查过了,但解决方法似乎不起作用。但第二,更重要的是,我得到了错误:
ValueError: 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), for inputs ['input_1'] but instead got the following list of 120 arrays: [<tf.Tensor: shape=(1, 256, 256, 3), dtype=float32, numpy=
array([[[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
...,
[0., 0., 0.],
[0., 0., 0.],
[0....
这对我来说毫无意义。我的生成器返回[batch, x-dim, y-dim, channel]
(我也尝试过使用[batch, channel, x-dim, y-dim]
,但也没有成功(。在这种情况下,批次等于1,而不是120。正如我所说,我无论如何都无法解决/调试这些问题,所以我真的很感谢你的帮助。我对DL很陌生,但不是python,我在python-3.7 中使用Tensorflow-2.1.0
非常感谢。
下面是工作代码。
import tensorflow as tf
import random
import os
import numpy as np
RES = [256, 256]
def generator_data(n):
for i in range(n):
for j in range(1):
yield tf.zeros((1, 256, 256, 3)), tf.zeros((1, 256, 256, 3))
def mymodel():
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same'))
# 256 x 256 x 8
model.add(tf.keras.layers.MaxPooling2D((2, 2), padding='same'))
# 128 x 128 x 8
model.add(tf.keras.layers.Conv2D(16, (3, 3), activation='relu', padding='same'))
# 128 x 128 x 16
model.add(tf.keras.layers.MaxPooling2D((2, 2), padding='same'))
# 64 x 64 x 16
model.add(tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same'))
# 64 x 64 x 32
model.add(tf.keras.layers.MaxPooling2D((2, 2), padding='same'))
# 32 x 32 x 32
# 32 x 32 x 32
model.add(tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same'))
# 32 x 32 x 32
model.add(tf.keras.layers.UpSampling2D((2, 2)))
# 64 x 64 x 32
model.add(tf.keras.layers.Conv2D(16, (3, 3), activation='relu', padding='same'))
# 64 x 64 x 16
model.add(tf.keras.layers.UpSampling2D((2, 2)))
# 128 x 128 x 16
model.add(tf.keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same'))
# 128 x 128 x 8
model.add(tf.keras.layers.UpSampling2D((2, 2)))
# 256 x 256 x 8
model.add(tf.keras.layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same'))
return model
if __name__ == "__main__":
# import some data to play with
z = list(zip(*generator_data(2)))
x_val = z[0][0]
y_val = z[0][1]
model = mymodel()
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
model.compile(optimizer=optimizer, loss=tf.keras.losses.MeanSquaredError())
model(tf.zeros((1, 256, 256, 3)))
model.summary()
print(x_val.numpy().shape)
print(y_val.numpy().shape)
model.fit(x=generator_data(10),
validation_data=(x_val, y_val),
verbose=1, epochs=1)
你在生成器中使用unzip的方式不对。我把输出键入一个列表,这样它就可以订阅了。其中一个有用的技巧是在每一步中打印X,y的形状和长度,以找出错误所在
更新:
是的,确切地说,但你需要传递一个形状张量[batch,256256,3]。但是,如果a是一个列表,并且a[0]具有形状[1256256,3],那么你需要将a[0]传递给模型,这就是我所做的。但是,您传递的是a。但是a是一个列表,而不是numpy数组/张量,即使我们将其类型转换为numpy数组,我们也会得到shape=(1,1,256,256,3(-这是无效的。
另外,在generator_data中,为什么要使用不必要的第二个循环?
def generator_data(n):
for i in range(n):
for j in range(1): # ??????? Why?
yield tf.zeros((1, 256, 256, 3)), tf.zeros((1, 256, 256, 3))
我通过每晚更新到tf-2.2并使用tf.data模块解决了这些问题。
如果你有同样的问题,看看这里:
import tensorflow as tf
import random
import os
from functools import partial
RES = [256, 256]
def generator_data(n):
for i in range(n):
for j in range(6):
yield tf.zeros((1, 256, 256, 3)), tf.zeros((1, 256, 256, 3))
def generator_data_val(n):
for i in range(n):
for j in range(6):
yield tf.zeros((256, 256, 3)), tf.zeros((256, 256, 3))
def model():
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same', input_shape=(256, 256, 3)))
# 256 x 256 x 8
model.add(tf.keras.layers.MaxPooling2D((2, 2), padding='same'))
# 128 x 128 x 8
model.add(tf.keras.layers.Conv2D(16, (3, 3), activation='relu', padding='same'))
# 128 x 128 x 16
model.add(tf.keras.layers.MaxPooling2D((2, 2), padding='same'))
# 64 x 64 x 16
model.add(tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same'))
# 64 x 64 x 32
model.add(tf.keras.layers.MaxPooling2D((2, 2), padding='same'))
# 32 x 32 x 32
# 32 x 32 x 32
model.add(tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same'))
# 32 x 32 x 32
model.add(tf.keras.layers.UpSampling2D((2, 2)))
# 64 x 64 x 32
model.add(tf.keras.layers.Conv2D(16, (3, 3), activation='relu', padding='same'))
# 64 x 64 x 16
model.add(tf.keras.layers.UpSampling2D((2, 2)))
# 128 x 128 x 16
model.add(tf.keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same'))
# 128 x 128 x 8
model.add(tf.keras.layers.UpSampling2D((2, 2)))
# 256 x 256 x 8
model.add(tf.keras.layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same'))
return model
if __name__ == "__main__":
# import some data to play with
x_val, y_val = zip(*generator_data_val(5))
x_val, y_val = list(x_val), list(y_val)
model = model()
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
model.compile(optimizer=optimizer, loss=tf.keras.losses.MeanSquaredError())
model(tf.zeros((1, 256, 256, 3)))
model.summary()
train_dataset = generator_data(5)
gen = partial(generator_data, n=5)
train_dataset = tf.data.Dataset.from_generator(
gen, output_types=(tf.float32, tf.float32),
output_shapes=(tf.TensorShape([1, 256, 256, 3]), tf.TensorShape([1, 256, 256, 3]))).repeat()
val_dataset = tf.data.Dataset.from_tensor_slices((x_val, y_val)).batch(2)
# generator_data(train_list)
model.fit(x=train_dataset,
steps_per_epoch=40,
validation_data=val_dataset,
verbose=1, epochs=1000)