前提
- Macを利用
- VSCでソースを記述してターミナルで実行
実現したいこと
2クラス分類を行うため、データセットを作成したい。
そのため、集めた画像をそれぞれ"gog_learn"、"bad_learn"という二つの学習用フォルダに保存し、同じ階層に用意した。
これらからnpy形式のデータセットを作成を作成したい。
発生している問題・エラーメッセージ
画像が読み込めていない。26行目のprint(files)でデータを確認したが、入っていない....。
素人質問で大変恐縮です。
プログラムの記述ミスなどありましたら、ご教授頂ければ幸いです。以下は階層とエラー文になります。
(base) fuku@fukushinoMacBook-Pro ~ % cd workspace/imori
(base) fuku@fukushinoMacBook-Pro imori % ls
bad_learn
cnn.h5
judge
god_learn
npy_test.py
imori.npy
(base) fuku@fukushinoMacBook-Pro imori % python npy_test.py
2022-12-22 00:15:13.427782: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
[]
[]
該当のソースコード
- ディレクトリ構造
python
1from PIL import Image 2import os, glob 3import numpy as np 4from keras.utils import np_utils 5from sklearn import model_selection 6from sklearn.model_selection import train_test_split 7 8 9# indexを教師ラベルとして割り当て(0にはgod、1にはbad) 10classes = ["god", "bad"] 11num_classes = len(classes) 12image_size = 64 13num_testdata = 50 14 15#datesetのディレクトリ, フォルダーの直前までのパスを記入 16datadir = './' 17 18X_train = [] 19X_test = [] 20y_train = [] 21y_test = [] 22 23for index, classlabel in enumerate(classes): 24 photos_dir = datadir + classlabel 25 files = glob.glob(photos_dir + "/*.jpeg") 26 print(files) 27 28 for i, file in enumerate(files): 29 image = Image.open(file) 30 image = image.convert("RGB") 31 image = image.resize((image_size, image_size)) 32 data = np.asarray(image) 33 34 #print(image) 35 #print(data) 36 if i < num_testdata: 37 X_test.append(data) 38 y_test.append(index) 39 else: 40 41 # angleに代入される値 42 # -20 43 # -15 44 # -10 45 # -5 46 # 0 47 # 5 48 # 10 49 # 15 50 # 画像を5度ずつ回転 51 for angle in range(-20, 20, 5): 52 53 img_r = image.rotate(angle) 54 data = np.asarray(img_r) 55 X_train.append(data) 56 y_train.append(index) 57 # FLIP_LEFT_RIGHT は 左右反転 58 img_trains = img_r.transpose(Image.FLIP_LEFT_RIGHT) 59 data = np.asarray(img_trains) 60 X_train.append(data) 61 y_train.append(index) # indexを教師ラベルとして割り当てる.0にはgodを指定し、1にはbadを指定 62 63X_train = np.asanyarray(X_train) 64X_test = np.asanyarray(X_test) 65y_train = np.asanyarray(y_train) 66y_test = np.asanyarray(y_test) 67 68xy = (X_train, X_test, y_train, y_test) 69np.save('./god_bad.npy', xy) 70classes = ["god", "bad"] 71num_classes = len(classes) 72image_size = 64
試したこと
- datadir = './'を datadir = './imori'に変更したが結果は同じであった。
- 先ほどの部分をコメントアウトし、23行目からを
for index, classlabel in enumerate(classes):
photos_dir = "./" + classlabel
files = glob.glob(photos_dir + "/*.jpg")
と記述したが同上。
アドバイス頂ければ幸いです。