前提・実現したいこと
kerasでいくつかの画像を入力して、特徴ベクトルを出力しようとしている最中、エラーが出た。
エラー内容的にも入力画像が入っていないんだろうな、と思いつつ41行目周辺のtrain_dirを見ているがパスは絶対に合っている。
でもこのエラーが治らない。。。
発生している問題・エラーメッセージ
could not broadcast input array from shape (0) into shape (20,4,4,512)
python
1from keras.applications import VGG16 2import os 3import numpy as np 4from keras.preprocessing.image import ImageDataGenerator 5from keras import models 6from keras import layers 7from keras import optimizers 8 9def extract_features(directory,sample_count): 10 11 # 画像の特徴量とラベルを入れるnumpyの配列を用意する 12 # 配列のサイズはVGG16モデルの出力と合わせる 13 features = np.zeros(shape=(sample_count,4,4,512)) 14 labels = np.zeros(shape=(sample_count)) 15 16 generator = datagen.flow_from_directory(directory, 17 target_size=(224,224), 18 batch_size=batch_size, 19 class_mode="binary") 20 21 loop = int(sample_count/batch_size) 22 print(loop) 23 for i in range(loop): 24 #ジェネレータから20個の画像データとラベルを取得 25 data_batch,label_batch = generator.next() 26 #VGG16のモデルを使って、(4,4,512)の特徴を抽出 27 features_batch = conv_base.predict(data_batch) 28 29 # 20個ずつ特徴量とラベルを詰めていく 30 start = i * batch_size 31 end = (i+1) * batch_size 32 print(start,end) 33 features[start : end] = features_batch 34 labels[start : end] = label_batch 35 36 return features,labels 37 38 39base_dir = "/home/atsushi/デスクトップ/reseaching/image/" 40 41train_dir = os.path.join(base_dir,'DKK_小山の神B') 42# validation_dir = os.path.join(base_dir,'validation') 43# weightsは重みのチェックポイント。include_topは全結合層を含めるかどうか。imagenetの1000クラス分類に対応。 44conv_base = VGG16(weights = "imagenet", 45 include_top=False, 46 input_shape=(224,224,3)) 47conv_base.summary() 48# 正規化しているらしいdatagenには正規化された画像データがはいってるのか??? 49# ではなく、インスタンス生成して跡で入力してる 50datagen = ImageDataGenerator(rescale=1./255) 51batch_size = 20 52# バッチサイズのデフォは32らしい 53 54datagen = ImageDataGenerator(rescale=1./255) 55batch_size = 20 56#(フォルダのパス、画像の数) 57train_features, train_label = extract_features(train_dir, 48) 58# validation_features,validation_label = extract_features(validation_dir,1000) 59# train_featuresの中に特徴量が入っているということ、2000 x 8176 60train_features = np.reshape(train_features ,(2000, 4 * 4 * 512)) 61# validation_features = np.reshape(validation_features ,(1000, 4 * 4 * 512))
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。