python-3.x - 检查目标时出错:预期 activation_21 的形状为 (708, 1268, 3) 但得到的数组的形状为 (720, 1280, 3)
问题描述
def datagen_train(batch,X_train,y_train):
lx = len(X_train)
for i in range(lx):
xx = []
yy = []
for j in range(batch):
xx.append(resize(imread(X_train[j]),(720,1280,3)))
yy.append(resize(imread(y_train[j]),(720,1280,3)))
xx = np.array(xx)
yy = np.array(yy)
print(i,xx.shape,yy.shape)
#yield np.array(xx),np.array(yy)
yield xx,yy
x = np.array(glob.glob(r'val_blur/*/*'))
y = np.array(glob.glob(r'val_sharp/*/*'))
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2,random_state=42)
print (X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)
(2400,) (2400,) (600,) (600,)
mo = sr.build(1280,720,3)
mo.compile(loss = "mse", optimizer = opt)
mo.summary()
Model: "sequential_7"
层(类型)输出形状参数#
conv2d_19 (Conv2D) (无, 712, 1272, 64) 15616
activation_19(激活)(无、712、1272、64)0
conv2d_20 (Conv2D) (无, 712, 1272, 32) 2080
activation_20(激活)(无,712、1272、32)0
conv2d_21 (Conv2D) (无, 708, 1268, 3) 2403
activation_21(激活)(无、708、1268、3)0
总参数:20,099 可训练参数:20,099 不可训练参数:0
h = mo.fit_generator(datagen_train(batch,X_train,y_train),
epochs=epo,steps_per_epoch=len(X_train)//batch)
Epoch 1/20
0 (1, 720, 1280, 3) (1, 720, 1280, 3)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-31-3abccc87c768> in <module>
1 h = mo.fit_generator(datagen_train(batch,X_train,y_train),
----> 2 epochs=epo,steps_per_epoch=len(X_train)//batch)
C:\ProgramData\Anaconda3\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1730 use_multiprocessing=use_multiprocessing,
1731 shuffle=shuffle,
-> 1732 initial_epoch=initial_epoch)
1733
1734 @interfaces.legacy_generator_methods_support
C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
218 sample_weight=sample_weight,
219 class_weight=class_weight,
--> 220 reset_metrics=False)
221
222 outs = to_list(outs)
C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py in train_on_batch(self, x, y, sample_weight, class_weight, reset_metrics)
1506 x, y,
1507 sample_weight=sample_weight,
-> 1508 class_weight=class_weight)
1509 if self._uses_dynamic_learning_phase():
1510 ins = x + y + sample_weights + [1]
C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
619 feed_output_shapes,
620 check_batch_axis=False, # Don't enforce the batch size.
--> 621 exception_prefix='target')
622
623 # Generate sample-wise weight values given the `sample_weight` and
C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
143 ': expected ' + names[i] + ' to have shape ' +
144 str(shape) + ' but got array with shape ' +
--> 145 str(data_shape))
146 return data
147
ValueError: Error when checking target: expected activation_21 to have shape (708, 1268, 3) but got array with shape (720, 1280, 3)
1 (1, 720, 1280, 3) (1, 720, 1280, 3)
print("[INFO] serializing model...")
mo.save('mode/',overwrite = False)
1 (10, 720, 1280, 3) (10, 720, 1280, 3)
在上面我得到了值错误所以需要什么更正
解决方案
您可以调整模型中图层中的strides
、kernel_size
和/或padding
参数Conv2D
以调整最后想要的输出大小(例如,(720, 1280, 3)
),或者您可以添加一个Lambda
图层来调整最终输出的大小(或最后activation
一层之前的那个) 以获得您想要的形状。您可以按照这个问题的答案创建一个Lambda
用于调整大小的图层。它的用法简单如下:
try:
out = keras.layers.Lambda(lambda image: keras.backend.tf.image.resize_images(image, (720,1280)))(previous_layer_output)
except :
# if you have older version of tensorflow
out = keras.layers.Lambda(lambda image: keras.backend.tf.image.resize_images(image, 720, 1280))(previous_layer_output)
推荐阅读
- tensorflow - ValueError,检查目标时出错:预期的 dense_1 有 4 个维度
- javascript - 无法读取 XMLHttpRequest.xhr.onload 处未定义的属性(Unsplash API)
- c# - 使用 C 访问 Dice.Com 上的工作详细信息链接(href?)
- java - 如何使用assertThat包含Junit(java)中列表比较的任何顺序方法
- jquery - jQuery 从 json 文件更新本地存储
- java - 无法读取 JSON:无法构造 `java.util.LinkedHashMap` 的实例
- python - 如何使用子进程获取输出和返回代码?
- javascript - 在数组中查找缺失的字母
- python - tkinter 在启动一些定制的 ttk.Frame 后变慢
- php - 正确格式化带有前导零的数字