質問編集履歴

1

プログラムの修正

2024/01/20 12:44

投稿

dundee_dance
dundee_dance

スコア2

test CHANGED
File without changes
test CHANGED
@@ -40,11 +40,11 @@
40
40
  from icrawler.builtin import BingImageCrawler
41
41
  from sklearn.model_selection import train_test_split
42
42
  from keras.models import Sequential
43
- from keras.layers import Conv2D, UpSampling2D, InputLayer, MaxPool2D, Dropout
43
+ from keras.layers import Conv2D, UpSampling2D, InputLayer, MaxPool2D, Dropout, BatchNormalization, Activation
44
44
  from tensorflow.python.keras.optimizers import adam_v2
45
45
 
46
46
  # カラー画像と白黒画像のペアを用意する関数
47
- def prepare_data(img_paths, img_size=(256, 256)):
47
+ def prepare_data(img_paths, img_size=(96, 96)):
48
48
  color_imgs = []
49
49
  gray_imgs = []
50
50
 
@@ -55,12 +55,13 @@
55
55
  color_img = cv2.resize(color_img, img_size)
56
56
 
57
57
  # 白黒画像
58
- '''
58
+
59
59
  gray_img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
60
60
  gray_img = cv2.resize(gray_img, img_size)
61
61
  gray_img = np.expand_dims(gray_img, axis=-1) # チャンネル次元を追加
62
+
63
+
62
64
  '''
63
-
64
65
  img = cv2.imread(img_path)
65
66
  img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
66
67
 
@@ -74,6 +75,7 @@
74
75
 
75
76
  gray_img = cv2.resize(gray_img, img_size)
76
77
  gray_img = np.expand_dims(gray_img, axis=-1) # チャンネル次元を追加
78
+ '''
77
79
 
78
80
  color_imgs.append(color_img)
79
81
  gray_imgs.append(gray_img)
@@ -85,39 +87,64 @@
85
87
  return color_imgs, gray_imgs
86
88
 
87
89
  # データセットのパス
88
- dataset_paths = glob.glob('../image3/slime/Camera*.png')
90
+ dataset_paths = glob.glob('Python/ml/image3/slime2/*.png')
89
91
 
90
92
  # カラー画像と白黒画像のペアを用意
91
93
  color_images, gray_images = prepare_data(dataset_paths)
92
94
 
93
95
  # データを訓練データとテストデータに分割
94
- X_train, X_test, y_train, y_test = train_test_split(gray_images, color_images, test_size=0.1, random_state=42)
96
+ X_train, X_test, y_train, y_test = train_test_split(gray_images, color_images, test_size=0.2, random_state=42)
95
97
 
96
98
  # モデルの定義
97
99
  model = Sequential()
100
+
101
+ #モデルの初期化
98
- model.add(InputLayer(input_shape=(256, 256, 1)))
102
+ model.add(InputLayer(shape=(96, 96, 1)))
103
+
99
-
104
+ model.add(Conv2D(3, (3, 3), padding='same', strides=1))
105
+ model.add(BatchNormalization(momentum=0.9, epsilon=1e-5))
106
+ model.add(Activation('relu'))
107
+ model.add(Dropout(0.5))
108
+
100
- model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
109
+ model.add(Conv2D(64, (3, 3), padding='same', strides=1))
110
+ model.add(BatchNormalization(momentum=0.9, epsilon=1e-5))
101
- model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
111
+ model.add(Activation('relu'))
102
-
112
+ model.add(Dropout(0.5))
113
+
103
- model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
114
+ model.add(Conv2D(128, (3, 3), padding='same', strides=1))
115
+ model.add(BatchNormalization(momentum=0.9, epsilon=1e-5))
116
+ model.add(Activation('relu'))
117
+ model.add(Dropout(0.5))
118
+
119
+ model.add(Conv2D(256, (3, 3), padding='same', strides=1))
120
+ model.add(BatchNormalization(momentum=0.9, epsilon=1e-5))
121
+ model.add(Activation('relu'))
122
+ model.add(Dropout(0.5))
123
+
104
- model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
124
+ model.add(Conv2D(128, (3, 3), padding='same', strides=1))
105
-
125
+ model.add(BatchNormalization(momentum=0.9, epsilon=1e-5))
126
+ model.add(Activation('relu'))
127
+ model.add(Dropout(0.5))
128
+
106
- model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
129
+ model.add(Conv2D(64, (3, 3), padding='same', strides=1))
130
+ model.add(BatchNormalization(momentum=0.9, epsilon=1e-5))
107
- model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
131
+ model.add(Activation('relu'))
108
-
132
+ model.add(Dropout(0.5))
133
+
109
- model.add(Conv2D(3, (3, 3), activation='linear', padding='same')) # Sigmoid から Linear に変更
134
+ model.add(Conv2D(3, (3, 3), padding='same', strides=1)) # Sigmoid から Linear に変更
110
-
135
+ model.add(BatchNormalization(momentum=0.9, epsilon=1e-5))
136
+ model.add(Activation('sigmoid'))
111
137
 
112
138
  # モデルのコンパイル
139
+ #初期値:0.0002
113
- optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=0.0002)
140
+ optimizer = tf.keras.optimizers.Adam(learning_rate=0.002)
114
141
  model.compile(optimizer=optimizer, loss='mae', metrics=['accuracy'])
115
142
 
116
143
 
117
144
  print(model.summary())
118
145
 
119
146
  # モデルの学習
120
- history = model.fit(X_train, y_train, epochs=20, batch_size=30, validation_data=(X_test, y_test))
147
+ history = model.fit(X_train, y_train, epochs=20, batch_size=150, validation_data=(X_test, y_test))
121
148
 
122
149
  # 学習過程の損失と精度の情報を取得
123
150
  train_loss = history.history['loss']
@@ -150,13 +177,13 @@
150
177
  plt.show()
151
178
 
152
179
  # ある白黒データに対する予測
153
- input_gray_image = X_test[0].reshape(1, 256, 256, 1)
180
+ input_gray_image = X_test[0].reshape(1, 96, 96, 1)
154
181
  predicted_color_image = model.predict(input_gray_image)
155
182
 
156
183
  # 結果の表示
157
184
  plt.subplot(1, 3, 1)
158
185
  plt.title('Input Grayscale Image')
159
- plt.imshow(X_test[0].reshape(256, 256), cmap='gray') # グレースケール画像なので256x256に修正
186
+ plt.imshow(X_test[0].reshape(96, 96), cmap='gray') # グレースケール画像なので256x256に修正
160
187
 
161
188
  plt.subplot(1, 3, 2)
162
189
  plt.title('Input RGB Image')