2017-11-21 11 views
-2

このスレッドで提案されている回帰ソリューションを実装しようとすると問題が発生します。 収入ValueError:Pythonでアンパックする値が多すぎる(期待値2)

Using Keras ImageDataGenerator in a regression model

別のスタックの質問

は、同様の問題があった: Tensorflow ValueError: Too many vaues to unpack (expected 2)が、私は私の場合は仕事と解決策を見つけるcouldntの。私は thisの結果を得ることなく歩留まりの説明を行った。私にとって奇妙なのは、最初の2つのループは完了していますが、出力が同じであれば3番目のループでクラッシュすることです。

ディレクトリの場合、フォルダはそれぞれlist_of_valuesの0.1,0.3、および0.5に対応する0,1,2というラベルが付けられています。

import numpy as np 
from keras.preprocessing.image import ImageDataGenerator  
train_datagen = ImageDataGenerator(
      rescale=1./255, 
      height_shift_range=0.15, 
      shear_range=0.2) 
def regression_flow_from_directory(flow_from_directory_gen, list_of_values): 
    for x, y in flow_from_directory_gen: 
     print (list_of_values[y], list_of_values,y) 
     yield (x, list_of_values[y]) 
batch_size=3 
list_of_values=[0.1,0.3,0.5] 
(x_train,y_train) = regression_flow_from_directory(train_datagen.flow_from_directory(
       'figs/train', # this is the target directory 
       batch_size=batch_size, 
       class_mode='sparse'), 
       np.asarray(list_of_values)) 

出力

Found 9 images belonging to 3 classes. 
[ 0.5 0.3 0.1] [ 0.1 0.3 0.5] [2 1 0] 
[ 0.3 0.1 0.3] [ 0.1 0.3 0.5] [1 0 1] 
[ 0.5 0.5 0.1] [ 0.1 0.3 0.5] [2 2 0] 
--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-179-3cf97453bd05> in <module>() 
     5   batch_size=batch_size, 
     6   class_mode='sparse'), 
----> 7   np.asarray(list_of_values)) 

ValueError: too many values to unpack (expected 2) 

EDIT:エラーは、2つの変数(x_train、y_train)に機能regression_flow_from_directoryを返していました。 x_trainだけに戻るとジェネレータが正しく渡されます。

x_train = regression_flow_from_directory(train_datagen.flow_from_directory(
     'figs/train', # this is the target directory 
     batch_size=batch_size, 
     class_mode='sparse'), 
     np.asarray(list_of_values)) 
+1

ようこそStackOverflow。ヘルプドキュメントの投稿ガイドラインを読み、それに従ってください。 [最小、完全で検証可能な例](http://stackoverflow.com/help/mcve)がここに適用されます。投稿されたコードをテキストファイルに貼り付け、説明した問題を再現できるはずです。 – Prune

答えて

1

エラーがnp.asarrayとは何の関係もありません。関数regression_flow_from_directoryにはyield文が含まれています。したがって、あなたがそれを呼び出すときには、生成された値のタプルではなく、ジェネレータオブジェクトが得られます。これは2要素タプルに解凍しようとしているオブジェクトの1つです。これがエラーメッセージの理由です。

+0

うん、間違いは(x_train、y_train)に解き放つことであった。それをx_trainだけに変更しました。 'x_train = regression_flow_from_directory(train_datagen.flow_from_directory( '図/列車'、#これはターゲットディレクトリである BATCH_SIZE = BATCH_SIZE、 class_mode = '疎')、 np.asarray(list_of_values))' – Peaksandpeaks

0
(x_train,y_train) = regression_flow_from_directory(
    train_datagen.flow_from_directory(
     'figs/train', # this is the target directory 
     batch_size=batch_size, 
     class_mode='sparse'), 
     np.asarray(list_of_values)) 

問題は、あなたの日常regression_flow_from_directoryつ以上の値を返すことであるように思われます。その割り当ての左にペアがあるので、右にちょうど2つの値が必要です。コンポーネントではなく実際の戻り値を印刷してみてください。例えば:

result = regression_flow_from_directory(...) 
print (result) 
(x,y) = result 

あなたは問題が表示されます:あなたはそれらの引数を指定してregression_flow_from_directory反復に持っています。元本の

簡単な例:

>>> (x, y) = 1, 2 
>>> x 
1 
>>> y 
2 
>>> (x, y) = 1, 2, 3 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ValueError: too many values to unpack 
+0

なぜ 'regression_flow_from_directory'は未定義の関数だと思いますか?その定義は私にとっては良さそうだ。 –

+0

...私は検索でそれをミスタイプしたので。一숨...引っ込め... – Prune

関連する問題