【问题标题】:Keras model ValueError: Error when checking model target:Keras 模型 ValueError:检查模型目标时出错:
【发布时间】:2017-05-10 17:33:15
【问题描述】:

我好几年没写代码了,见谅。我正在尝试做一些可能不可能的事情。我有 38 个视频,显示人们执行相同的基本动作。我想训练模型来识别那些做对和不正确的人。 我现在使用颜色,因为灰度也不起作用,我想像我使用的示例一样进行测试。我使用了an example, link中定义的模型。

凯拉斯, Anaconda 64 中的 Python3.5, 张量流后端, 在 Windows 10(64 位)上

我希望尝试不同的模型来解决这个问题并使用灰度来减少内存,但无法通过第一步!

谢谢!!!

这是我的代码:

import time
import numpy as np
import sys
import os
import cv2
import keras
import tensorflow as tf

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, BatchNormalization
from keras.layers import Conv3D, Conv2D, MaxPooling2D, GRU, ConvLSTM2D, TimeDistributed


y_cat = np.zeros(40,np.float)
good = "Good"
bad = "Bad"


batch_size = 32
num_classes = 1
epochs = 1
nvideos = 38
nframes = 130
nrows = 240
ncols = 320
nchan = 3

x_learn = np.zeros((nvideos,nframes,nrows,ncols,nchan),np.int32)
x_learn = np.load(".\\train\\datasetcolor.npy")

with open(".\\train\\tags.txt") as ft:
    y_learn = ft.readlines()
y_learn = [x.strip() for x in y_learn] 
ft.close()

# transform string tags to numeric.
for i in range (0,len(y_learn)):
    if (y_learn[i] == good): y_cat[i] = 1
    elif (y_learn[i]  == bad): y_cat[i] = 0


#build model 
# duplicating from https://github.com/fchollet/keras/blob/master/examples/conv_lstm.py
model = Sequential()
model.image_dim_ordering = 'tf'
model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),
                   input_shape=(nframes,nrows,ncols,nchan),
                   padding='same', return_sequences=True))
model.add(BatchNormalization())
model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),
                   padding='same', return_sequences=True))
model.add(BatchNormalization())

model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),
                   padding='same', return_sequences=True))
model.add(BatchNormalization())

model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),
                   padding='same', return_sequences=True))
model.add(BatchNormalization())

model.add(Conv3D(filters=1, kernel_size=(3, 3, 3),
               activation='sigmoid',
               padding='same', data_format='channels_last'))
model.compile(loss='binary_crossentropy', optimizer='adadelta')


print(model.summary())

# fit with first 3 videos because I don't have the horsepower yet
history = model.fit(x_learn[:3], y_learn[:3],
              batch_size=batch_size,
              epochs=epochs)

print (history)

结果:


Layer (type)                 Output Shape              Param #   
=================================================================
conv_lst_m2d_5 (ConvLSTM2D)  (None, 130, 240, 320, 40) 62080     
_________________________________________________________________
batch_normalization_5 (Batch (None, 130, 240, 320, 40) 160       
_________________________________________________________________
conv_lst_m2d_6 (ConvLSTM2D)  (None, 130, 240, 320, 40) 115360    
_________________________________________________________________
batch_normalization_6 (Batch (None, 130, 240, 320, 40) 160       
_________________________________________________________________
conv_lst_m2d_7 (ConvLSTM2D)  (None, 130, 240, 320, 40) 115360    
_________________________________________________________________
batch_normalization_7 (Batch (None, 130, 240, 320, 40) 160       
_________________________________________________________________
conv_lst_m2d_8 (ConvLSTM2D)  (None, 130, 240, 320, 40) 115360    
_________________________________________________________________
batch_normalization_8 (Batch (None, 130, 240, 320, 40) 160       
_________________________________________________________________
conv3d_1 (Conv3D)            (None, 130, 240, 320, 1)  1081      
=================================================================
Total params: 409,881.0
Trainable params: 409,561
Non-trainable params: 320.0
_________________________________________________________________
None
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-d909d285f474> in <module>()
     82 history = model.fit(x_learn[:3], y_learn[:3],
     83               batch_size=batch_size,
---> 84               epochs=epochs)
     85 
     86 print (history)

ValueError: Error when checking model target: expected conv3d_1 to have 5 dimensions, but got array with shape (3, 1)

【问题讨论】:

    标签: keras


    【解决方案1】:

    “目标”表示问题出在模型的输出y_learn的格式之间。

    数组y_learn 应该与模型输出的形状完全相同,因为模型输出的是“猜测”,而y_learn 是“正确答案”。只有当它们具有相同的维度时,系统才能将猜测与正确答案进行比较。

    看看区别:

    • 模型输出(见摘要):(None,130,240,320,1)
    • y_learn:(None,1)

    其中“无”是批量大小。你给了 y_learn[:3],那么这次训练的批大小是 3。

    为了正确纠正它,我们需要了解 y_learn 是什么。
    如果我理解得很好,每个视频只有一个数字,0 或 1。如果是这样,你的 y_learn 就完全没问题了,你需要让你的模型输出像(None,1) 这样的东西。

    一种非常简单的方法(也许不是最好的,我在这里帮不上忙...)是添加一个只有一个神经元的最终 Dense 层:

    model.add(Flatten())
    model.add(Dense(1, activation='sigmoid'))
    

    现在,当您执行model.summary() 时,您将看到最终输出为(None,1)

    【讨论】:

    • 如果它回答了您的问题,请考虑将支票标记为有效答案 :)
    猜你喜欢
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2020-04-07
    相关资源
    最近更新 更多