简体   繁体   English

Keras模型ValueError:检查模型目标时出错:

[英]Keras model ValueError: Error when checking model target:

I have not coded in years, forgive me. 多年来我没有编码,原谅我。 I am trying to do something that may be impossible. 我正在尝试做一些可能不可能完成的事情。 I have 38 videos of people performing the same basic movement. 我有38个人的视频执行相同的基本动作。 I want to train the model to identify those doing it correct v not correct. 我想训练模型,以确定那些正确的做法v不正确。 I am using color now, because the grayscale did not work either and I wanted to test like the example I used. 我现在正在使用颜色,因为灰度也不起作用,我想像我使用的例子一样进行测试。 I used the model as defined in an example, link . 我使用了示例链接中定义的模型。

Keras, Python3.5 in Anaconda 64, Tensorflow backend, on Windows 10 (64bit) Keras,Anaconda 64中的Python3.5,Tensorflow后端,Windows 10(64位)

I was hoping to try different models on the problem and use grayscale to reduce memory, but cant get past first step! 我希望在这个问题上尝试不同的模型并使用灰度来减少内存,但是无法通过第一步!

Thanks!!! 谢谢!!!

Here is my code: 这是我的代码:

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)

Results: 结果:


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)

"Target" means that the problem is in the output of your model versus the format of y_learn . “目标”表示问题在模型的输出中 ,而不是y_learn的格式。

The array y_learn should be exactly the same shape of the model's output , because the model outputs a "guess", while y_learn is the "correct answer". 数组y_learn应该与模型输出的形状完全相同 ,因为模型输出“guess”,而y_learn是“正确答案”。 The system can only compare the guess with the correct answer if they have the same dimensions. 如果猜测具有相同的尺寸,则系统只能将猜测与正确答案进行比较。

See the difference: 看到不同:

  • Model Output (seen in the summary): (None,130,240,320,1) 模型输出(见摘要):( (None,130,240,320,1)
  • y_learn: (None,1) y_learn :( (None,1)

Where "None" is the batch size. 其中“无”是批量大小。 You gave y_learn[:3], then your batch size is 3 for this training session. 您给了y_learn [:3],然后您的批量大小为3,用于此培训课程。

In order to correct it properly, we need to understand what y_learn is. 为了正确纠正它,我们需要了解y_learn是什么。
If I understood well, you've got only a number, 0 or 1, for each video. 如果我理解得很清楚,每个视频你只有一个0或1的数字。 If that's so, your y_learn is totally ok, and what you need is for your model to output things like (None,1) . 如果是这样,你的y_learn就完全可以了,你需要的是你的模型输出像(None,1)这样的东西。

A very simple way to do that (perhaps not the best, and I couldn't be of more help here...) is to add a final Dense layer with just one neuron: 一个非常简单的方法(也许不是最好的,我在这里得到更多的帮助......)是添加一个只有一个神经元的最终Dense层:

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

Now, when you do model.summary() , you will see the final output as (None,1) 现在,当你执行model.summary() ,你会看到最终输出为(None,1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras ValueError:检查 model 目标时出错 (CNN) - Keras ValueError: Error when checking model target (CNN) Keras 多输出模型错误地计算目标维度:ValueError:检查目标时出错 - Keras multi-output model wrongly calculate target dimensions: ValueError: Error when checking target Keras ValueError:检查模型目标时出错:预期density_18 - Keras ValueError: Error when checking model target: expected dense_18 ValueError:检查目标时出错:预期(keras 序列模型层)具有 n 维,但得到了具有形状的数组 - ValueError: Error when checking target: expected (keras Sequence model layer) to have n dimensions, but got array with shape keras.model.predict提高ValueError:检查输入时出错 - keras.model.predict raise ValueError: Error when checking input ValueError:检查模型目标时出错:预期density_4 - ValueError: Error when checking model target: expected dense_4 Keras模型构建-ValueError:检查目标时出错:预期density_24具有形状(None,1),但形状为数组(576,2) - Keras model building - ValueError: Error when checking target: expected dense_24 to have shape (None, 1) but got array with shape (576, 2) Keras:ValueError:检查模型目标时出错:期望dense_1具有形状(无,10)但是具有形状的数组(10,1) - Keras: ValueError: Error when checking model target: expected dense_1 to have shape (None, 10) but got array with shape (10, 1) keras:ValueError:检查模型目标时出错:预期activation_1具有形状(无,60),但数组的形状为(10,100) - keras: ValueError: Error when checking model target: expected activation_1 to have shape (None, 60) but got array with shape (10, 100) ValueError:检查 model 目标时出错:您传递给 model 的 Numpy arrays 的列表不是预期的 model 的大小 - ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM