簡體   English   中英

Keras模型ValueError:檢查模型目標時出錯:

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

多年來我沒有編碼,原諒我。 我正在嘗試做一些可能不可能完成的事情。 我有38個人的視頻執行相同的基本動作。 我想訓練模型,以確定那些正確的做法v不正確。 我現在正在使用顏色,因為灰度也不起作用,我想像我使用的例子一樣進行測試。 我使用了示例鏈接中定義的模型。

Keras,Anaconda 64中的Python3.5,Tensorflow后端,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)

“目標”表示問題在模型的輸出中 ,而不是y_learn的格式。

數組y_learn應該與模型輸出的形狀完全相同 ,因為模型輸出“guess”,而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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM