【问题标题】:Conv3d prediction throws error Input depth must be evenly divisible by filter depth: 1 vs 3Conv3d 预测抛出错误输入深度必须能被过滤器深度整除:1 vs 3
【发布时间】:2020-10-19 17:25:52
【问题描述】:

我是 CNN 的新手。我正在尝试手势识别。 conv3d 中使用了从开始中间和结束图像开始的 3 张图像。 模型经过训练,我正在加载保存的模型并尝试预测。 以下是代码。

img_idx=[0,14,29]  # sequence of images to use in a video 
n_timesteps = len(img_idx)
# load weights
model1 = Sequential()
#conv3d 1
model1.add(Conv3D(24, (3, 3, 3), padding='same', 
                 input_shape=(120, 160, 3, 3),
                 kernel_regularizer=regularizers.l1_l2(l1=1e-5, l2=1e-4),
                 bias_regularizer=regularizers.l2(1e-4),
                 activity_regularizer=regularizers.l2(1e-5)))
model1.add(LeakyReLU())
model1.add(Dropout(0.25))
#conv3d 2
model1.add(Conv3D(24, (3, 3, 3), padding='same',
                 kernel_regularizer=regularizers.l1_l2(l1=1e-5, l2=1e-4),
                 activity_regularizer=regularizers.l2(1e-5)
                 ))
model1.add(Dropout(0.25))
model1.add(LeakyReLU())

#MaxPooling3D 1
model1.add(MaxPooling3D(pool_size=(2, 2, 2), padding='same'))

model1.add(Flatten())
model1.add(Dense(30))  #, kernel_initializer='he_uniform')
model1.add(LeakyReLU(alpha=0.1))
model1.add(Dropout(0.25))
model1.add(Dense(out_class, activation='softmax'))

model1.load_weights("/model-00004-0.81386-0.81297-0.91821-0.83000.h5")
print(model1.inputs)
x=next(train_generator)

print(x[0].shape)
tgt=x[0][0,:,:,:,:]
print(tgt.shape)
tgt = tgt[np.newaxis,:,:,:,:]
print(tgt.shape)
model1.predict(tf.transpose(tgt),steps=1)

输出为

[<tf.Tensor 'conv3d_11_input:0' shape=(None, 120, 160, 3, 3) dtype=float32>]
(10, 120, 160, 3, 3)
(120, 160, 3, 3)
(1, 120, 160, 3, 3)
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-20-57e6204980ae> in <module>()
     40 tgt = tgt[np.newaxis,:,:,:,:]
     41 print(tgt.shape)
---> 42 model1.predict(tf.transpose(tgt),steps=1)

7 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError:  Input depth must be evenly divisible by filter depth: 1 vs 3
     [[node conv3d_11/convolution (defined at /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:3009) ]] [Op:__inference_keras_scratch_graph_1700]

Function call stack:
keras_scratch_graph

你能帮我解决这个异常吗?

执行平台是google colab

  • Keras==2.3.1
  • Keras-Applications==1.0.8
  • Keras 预处理==1.1.2
  • matplotlib==3.2.2
  • numpy==1.18.5
  • 熊猫==1.0.5
  • scikit-image==0.16.2
  • scikit-learn==0.22.2.post1
  • scipy==1.4.1
  • 张量板==2.2.2
  • tensorboard-plugin-wit==1.6.0.post3
  • tensorboardcolab==0.0.22
  • 张量流==2.2.0
  • tensorflow-addons==0.8.3
  • tensorflow-datasets==2.1.0
  • tensorflow-estimator==2.2.0
  • tensorflow-gcs-config==2.2.0
  • tensorflow-hub==0.8.0
  • tensorflow-metadata==0.22.2
  • tensorflow-privacy==0.2.2
  • 张量流概率==0.10.0

【问题讨论】:

    标签: tensorflow machine-learning keras


    【解决方案1】:

    问题出现在您的mode.predict() 中的tf.tranpose(tgt)。我不太确定您为什么首先要转置,因为 tgt 已经为您的模型提供了正确的形状。如果您尝试切换最后两个维度,则必须在tf.transpose() 中指定维度的顺序。例如,

    tgt = tf.transpose(tgt, (0, 1, 2, 4, 3))
    # tgt.shape == (1, 120, 160, 3, 3)
    

    现在,交换最后两个维度。

    【讨论】:

    • 感谢您的回复。这似乎给了同样的问题..原来也失败了
    • 如果你能解释一下可能是什么问题,这将有所帮助
    • 这适用于验证 ahist = model.fit_generator(train_generator, steps_per_epoch=steps_per_epoch, epochs=num_epochs, verbose=1, callbacks=callbacks_list, validation_data=val_generator, validation_steps=validation_steps, class_weight=None, workers =1,initial_epoch=0)
    • 这仍然是开放的任何帮助将不胜感激
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    相关资源
    最近更新 更多