カラー画像をkerasを使っての2クラス分類を試みています。
しかし、train_lossが減らず、train_accuracy,val_accuracy共に0.5前後をさまよってしまいます。
以下、使用したコードを載せます。
python
1import os 2import re 3 4import tensorflow.keras 5from tensorflow.keras import utils, datasets, optimizers 6from tensorflow.keras.layers import Input, Embedding, LSTM, Conv2D, MaxPooling2D, concatenate 7from tensorflow.keras.models import Sequential, Model 8from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten 9from tensorflow.keras.preprocessing.image import array_to_img, img_to_array, load_img 10from tensorflow.keras.applications.resnet50 import ResNet50 11import numpy as np 12 13 14np.set_printoptions(threshold=np.inf) 15 16def list_pictures(directory, ext='jpg|jpeg|bmp|png|ppm'): 17 return [os.path.join(root, f) 18 for root, _, files in os.walk(directory) for f in files 19 if re.match(r'([\w]+.(?:' + ext + '))', f.lower())] 20 21 22# フォルダの中にある画像を順次読み込む 23# カテゴリーは0から始める 24X = [] 25Y = [] 26 27i=0 28# 対象Aの画像 29for picture in list_pictures('./images/png/train/A/'): 30 img = img_to_array(load_img(picture, target_size=(64,64))) 31 X.append(img) 32 Y.append(0) 33 i=i+1 34 if i >= 20865: 35 break 36 37j=0 38# 対象Bの画像 39for picture in list_pictures('./images/png/train/B/'): 40 img = img_to_array(load_img(picture, target_size=(64,64))) 41 X.append(img) 42 Y.append(1) 43 j=j+1 44 if j >= 20865: 45 break 46 47# arrayに変換 48images = np.asarray(X) 49labels = np.asarray(Y) 50 51images = images.astype('float32') 52images /= 255.0 53 54#データのシャッフル 55p = np.random.permutation(len(images)) 56images = images[p] 57labels = labels[p] 58 59 60###モデルの定義### 61image_input = Input(shape=(64, 64, 3,), name='image_input') 62 63ResNet50 = ResNet50(include_top=False, weights=None, input_shape=(64, 64, 3))(image_input) 64x = Flatten()(ResNet50) 65image_out = Dense(1, activation='relu')(x) 66main_output = Dense(1, activation='sigmoid', name='main_output')(image_out) 67 68model = Model(inputs=[image_input], outputs=[main_output]) 69 70model.compile(optimizer=optimizers.SGD(lr=1e-2, momentum=0.9), 71 loss='binary_crossentropy', 72 metrics=['accuracy']) 73model.summary() 74 75model.fit( images, labels, epochs=100, batch_size=16, validation_split=0.1, shuffle=True)
Epoch 1/100 2021-02-16 21:54:43.420110: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10 2021-02-16 21:54:43.598753: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7 2348/2348 [==============================] - 57s 24ms/step - loss: 0.6935 - accuracy: 0.5003 - val_loss: 0.6931 - val_accuracy: 0.5006 Epoch 2/100 2348/2348 [==============================] - 56s 24ms/step - loss: 0.6936 - accuracy: 0.4968 - val_loss: 0.6939 - val_accuracy: 0.5006 Epoch 3/100 2348/2348 [==============================] - 56s 24ms/step - loss: 0.6937 - accuracy: 0.4971 - val_loss: 0.6934 - val_accuracy: 0.5006 Epoch 4/100 2348/2348 [==============================] - 56s 24ms/step - loss: 0.6936 - accuracy: 0.4979 - val_loss: 0.6937 - val_accuracy: 0.5006 Epoch 5/100 2348/2348 [==============================] - 56s 24ms/step - loss: 0.6935 - accuracy: 0.5046 - val_loss: 0.6934 - val_accuracy: 0.4994 Epoch 6/100 2348/2348 [==============================] - 56s 24ms/step - loss: 0.6935 - accuracy: 0.4979 - val_loss: 0.6942 - val_accuracy: 0.5006 Epoch 7/100 2348/2348 [==============================] - 56s 24ms/step - loss: 0.6937 - accuracy: 0.4988 - val_loss: 0.6932 - val_accuracy: 0.5006 Epoch 8/100 2348/2348 [==============================] - 56s 24ms/step - loss: 0.6934 - accuracy: 0.5037 - val_loss: 0.6933 - val_accuracy: 0.5006 Epoch 9/100 2348/2348 [==============================] - 56s 24ms/step - loss: 0.6935 - accuracy: 0.4993 - val_loss: 0.6942 - val_accuracy: 0.4994 Epoch 10/100 2348/2348 [==============================] - 56s 24ms/step - loss: 0.6936 - accuracy: 0.4982 - val_loss: 0.6933 - val_accuracy: 0.5006 Epoch 11/100 2348/2348 [==============================] - 55s 23ms/step - loss: 0.6936 - accuracy: 0.4990 - val_loss: 0.6936 - val_accuracy: 0.5006 Epoch 12/100 2348/2348 [==============================] - 55s 23ms/step - loss: 0.6937 - accuracy: 0.4970 - val_loss: 0.6932 - val_accuracy: 0.5006 Epoch 13/100 2348/2348 [==============================] - 56s 24ms/step - loss: 0.6936 - accuracy: 0.4988 - val_loss: 0.6932 - val_accuracy: 0.4994 Epoch 14/100 2348/2348 [==============================] - 55s 23ms/step - loss: 0.6935 - accuracy: 0.4989 - val_loss: 0.6932 - val_accuracy: 0.5006
試したこと
学習用データをシャッフルせずに学習したところ、正答率は0.9をこえました。
ということは学習用データをシャッフルする過程に問題があるのでしょうか?
Epoch 1/100 2021-02-16 21:45:31.089529: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10 2021-02-16 21:45:31.269549: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7 2348/2348 [==============================] - 61s 26ms/step - loss: 0.1165 - accuracy: 0.9726 - val_loss: 0.0100 - val_accuracy: 1.0000 Epoch 2/100 2348/2348 [==============================] - 60s 25ms/step - loss: 0.1806 - accuracy: 0.9472 - val_loss: 0.0100 - val_accuracy: 1.0000 Epoch 3/100 2348/2348 [==============================] - 60s 25ms/step - loss: 0.1806 - accuracy: 0.9472 - val_loss: 0.0100 - val_accuracy: 1.0000 Epoch 4/100 2348/2348 [==============================] - 60s 25ms/step - loss: 0.1806 - accuracy: 0.9472 - val_loss: 0.0100 - val_accuracy: 1.0000 Epoch 5/100 2348/2348 [==============================] - 59s 25ms/step - loss: 0.1806 - accuracy: 0.9472 - val_loss: 0.0100 - val_accuracy: 1.0000 Epoch 6/100 2348/2348 [==============================] - 60s 26ms/step - loss: 0.1806 - accuracy: 0.9472 - val_loss: 0.0100 - val_accuracy: 1.0000 Epoch 7/100 2348/2348 [==============================] - 60s 26ms/step - loss: 0.1806 - accuracy: 0.9472 - val_loss: 0.0100 - val_accuracy: 1.0000
回答2件
あなたの回答
tips
プレビュー