2017-05-10 2 views
0

私は長年に渡ってコード化しておらず、許してください。私は不可能かもしれない何かをしようとしています。私は、同じ基本的な動きをする人の38ビデオを持っています。モデルを訓練して、正しいことをしているものと正しいものがないものを識別したい。 グレースケールがうまく機能せず、私が使用した例のようにテストしたかったので、今は色を使用しています。私はan example, linkで定義されたモデルを使用しました。Keras model ValueError:モデルターゲットをチェックするときのエラー:

Keras、アナコンダ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) 

答えて

2

"ターゲット"は問題はのフォーマット対モデルの出力であることを意味しy_learny_learnが「正解」である一方、モデルは、「推測」を出力するので、

配列y_learnは、モデルの出力のまったく同じ形状でなければなりません。システムは、同じ次元を持つ場合にのみ、推測を正解と比較することができます。

は、違いを参照してください:(サマリーで見られる)

  • モデル出力:(None,130,240,320,1)
  • y_learn: "なし" バッチサイズではありません(None,1)

。あなたはy_learn [:3]を与えた後、このトレーニングセッションのバッチサイズは3です。

正しく修正するには、y_learnの内容を理解する必要があります。
私がよく理解していれば、各ビデオには0または1の数字しかありません。そうであれば、y_learnはまったく問題なく、(None,1)のようなものを出力するモデルが必要です。

ただ1つのニューロンで、最終的な緻密層を追加することです(おそらく最高ではない、と私はここにもっと助けになることができませんでした...)ことを行うには非常に単純な方法:

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

今度はmodel.summary()を実行すると、最終出力は(None,1)

+0

となり、非常にありがとうございます。 – DSP209

+0

あなたの質問に答えた場合は、それが有効な回答であることを示すチェックをマークすることを検討してください:) –

関連する問題