【发布时间】:2019-10-20 02:04:04
【问题描述】:
在 tensorflow + keras 上实现和训练 Tiny-DSOD 网络。开始第一个 epoch 时,训练因错误而终止:tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [7,128,2,2] vs. [7,128,3,3]
批量大小为 8,图像大小为 (300,300),用于训练的数据集为 PASCAL VOC 2007+2012。错误发生在预测层的输出之一(非常类似于 SSD)和损失之间: [[{{节点 add_fpn_0_/add}}]] [[{{node loss/add_50}}]]
目前tensorflow的版本是1.13,keras是2.2.4。 Python 版本是 3.6。我已经检查了模型本身的所有内容(形状符合预期),为批次生成的图像(每个图像都符合预期),改变损失计算(当前使用 Adam,但也尝试使用 SGD,它完全一样问题。)并检查张量板是否可以提供任何信息(在终止点之前一切顺利)。
history = model.fit_generator(generator=train_generator,
steps_per_epoch=math.ceil(n_train_samples/batch_size),
epochs=epochs,
callbacks=[tf.keras.callbacks.ModelCheckpoint('tinydsod300_weights_epoch--{epoch:02d}_loss--{loss:.4f}_val_loss--{val_loss:.4f}.h5',
monitor='val_loss',
verbose=1,
save_best_only=True,
save_weights_only=True,
mode='auto', period=1),
tf.keras.callbacks.LearningRateScheduler(lr_schedule),
tf.keras.callbacks.EarlyStopping(monitor='val_loss',
min_delta=0.001,
patience=2),
tf.keras.callbacks.TerminateOnNaN(),
tf.keras.callbacks.TensorBoard(log_dir='./logs'),
tf.keras.callbacks.BaseLogger()],
validation_data=val_generator,
validation_steps=math.ceil(n_val_samples/batch_size)
完全错误:
WARNING:tensorflow:From /home/alexandre.pires/.conda/envs/neural_network/lib/python3.6/site-packages/tensorflow/python/ops/math_grad.py:102: div (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Deprecated in favor of operator or tf.math.divide.
2019-06-04 15:45:59.614299: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu devices: 0
2019-06-04 15:45:59.614330: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-06-04 15:45:59.614337: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 0
2019-06-04 15:45:59.614341: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1003] 0: N
2019-06-04 15:45:59.614513: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 2998 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1050 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
Epoch 1/10
2019-06-04 15:46:28.296307: W tensorflow/core/common_runtime/bfc_allocator.cc:211] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.77GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.
Traceback (most recent call last):
File "/home/alexandre.pires/PycharmProjects/neural_networks/tiny-dsod.py", line 830, in <module>
validation_steps=math.ceil(n_val_samples/batch_size)
File "/home/alexandre.pires/.conda/envs/neural_network/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1426, in fit_generator
initial_epoch=initial_epoch)
File "/home/alexandre.pires/.conda/envs/neural_network/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_generator.py", line 191, in model_iteration
batch_outs = batch_function(*batch_data)
File "/home/alexandre.pires/.conda/envs/neural_network/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1191, in train_on_batch
outputs = self._fit_function(ins) # pylint: disable=not-callable
File "/home/alexandre.pires/.conda/envs/neural_network/lib/python3.6/site-packages/tensorflow/python/keras/backend.py", line 3076, in __call__
run_metadata=self.run_metadata)
File "/home/alexandre.pires/.conda/envs/neural_network/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1439, in __call__
run_metadata_ptr)
File "/home/alexandre.pires/.conda/envs/neural_network/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 528, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [7,128,2,2] vs. [7,128,3,3]
[[{{node add_fpn_0_/add}}]]
[[{{node loss/add_50}}]]
最后要补充的是,预测层的先前输出确实具有形状 [7,128,2,2],但这从未产生任何错误。关于下一步我应该在哪里调试的任何提示?或者这个错误究竟来自哪里?
EDIT1 - 更正
在模型中进行了一些更正并出现了一个新错误,但仍然具有相同的不兼容形状:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [8,128,2,2] vs. [8,128,3,3]
[[{{node add_fpn_0_/add}}]]
[[{{node loss/predictions_loss/broadcast_weights/assert_broadcastable/is_valid_shape/has_valid_nonscalar_shape/has_invalid_dims/concat}}]]
对深度卷积进行了修正,以按照原始模型(在 caffe 中制作)中的预期行为。
卷积
layer_name = "conv_" + name
output = tf.keras.layers.Conv2D(filters=filter, kernel_size=kernel, padding=pad,
strides=stride, kernel_initializer=self.kernel_initializer,
kernel_regularizer=self.regularize, name=layer_name)(input)
output = tf.keras.layers.BatchNormalization(name=layer_name + "batch_")(output)
output = tf.keras.layers.Activation('relu', name=layer_name + "relu_")(output)
return output
深度分析
if stride == 2:
output = tf.keras.layers.ZeroPadding2D(padding=self.correct_pad(input, kernel[0]),
name='zeropad_' + layer_name)(input)
output = tf.keras.layers.DepthwiseConv2D(kernel_size=kernel, padding='SAME' if stride == 1 else 'VALID',
strides=stride, kernel_initializer=self.kernel_initializer,
kernel_regularizer=self.regularize, name=layer_name)(output)
else:
output = tf.keras.layers.DepthwiseConv2D(kernel_size=kernel, padding='SAME' if stride == 1 else 'VALID',
strides=stride, kernel_initializer=self.kernel_initializer,
kernel_regularizer=self.regularize, name=layer_name)(input)
if use_batch_norm:
output = tf.keras.layers.BatchNormalization(center=True, scale=True, trainable=True,
name=layer_name + "batch_")(output)
output = tf.keras.layers.Activation('relu', name=layer_name + "relu_")(output)
上采样(简单双线性)
layer_name = "upsample_" + name
output = tf.keras.layers.UpSampling2D(size=(input_shape[0], input_shape[1]), interpolation='bilinear',
name=layer_name)(input)
output = self._depthwise_conv_2d(output, filter=128, kernel=(3, 3), pad='SAME', stride=1, name=layer_name)
return output
【问题讨论】:
-
您能分享一下您是如何定义模型的吗?
-
@AndrewXia 我添加了一些我目前已经实现的操作。由于工作原因,我不能透露更多。有关网络本身的更多信息,请查看bmvc2018.org/contents/papers/0145.pdf
标签: python tensorflow keras