【问题标题】:Dimensional error while predicting预测时的尺寸误差
【发布时间】:2017-10-21 18:22:34
【问题描述】:

我已经使用 code 训练了一个卷积 3d 模型

我试图得到如下预测,

import cv2
from keras.models import Sequential, load_model
import numpy as np

#create an empty frame
frames = []

#defince row, col
img_rows,img_cols,img_depth=16,16,15

cap = cv2.VideoCapture('run.avi')
fps = cap.get(5)

#Use only first 15 frames for prediction
for k in range(15):
    ret, frame = cap.read()
    frame=cv2.resize(frame,(img_rows,img_cols),interpolation=cv2.INTER_AREA)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    frames.append(gray)


#preprocess
input = np.array(frames)
ipt=np.rollaxis(np.rollaxis(input,2,0),2,0)
reshape_frames = np.expand_dims(ipt, axis=0)

#run prediction
model = load_model('current.h5')
preds = model.predict(reshape_frames)
print(preds)

但它会引发以下错误,

ValueError:检查时出错:预期 conv3d_1_input 有 5 尺寸,但得到形状为 (1, 16, 16, 15) 的数组

我怎样才能解决这个问题?

【问题讨论】:

    标签: python opencv neural-network keras


    【解决方案1】:

    docs for convolutional 3D layers

    输入形状

    具有形状的 5D 张量: (samples, channels, conv_dim1, conv_dim2, conv_dim3) 如果 data_format='channels_first' 或具有形状的 5D 张量: (samples, conv_dim1, conv_dim2, conv_dim3, channels) 如果 data_format='channels_last'。

    所以基本上发生的情况是您提供给第一个 conv 3D 层的输入形状不适合预期的输入。

    要解决这个问题,你可以这样做:

    • 更改提供的输入,使其与预期的输入匹配(还要考虑上面提到的data_format)。正如它在您的代码中所看到的那样,您根本不使用img_depth 信息。您基本上是向 3D 转换网络提供 2D 图像。
    • 使用 2D 卷积网络并创建新模型

    【讨论】:

      猜你喜欢
      • 2019-05-19
      • 2012-05-01
      • 2018-03-26
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      • 2016-10-28
      相关资源
      最近更新 更多