質問編集履歴
3
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
### 前提
|
2
2
|
|
3
|
-
Google colabでUnet
|
3
|
+
Google colabでUnetを行い、ロスを下げるためにデータセットを見直しました。
|
4
4
|
しかし、新しくエラーが出るようになりました。
|
5
5
|
train_step(image,alpha) ←この部分でエラーが出ています。
|
6
6
|
|
@@ -25,181 +25,6 @@
|
|
25
25
|
ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 128, 128, 3), found shape=(50, 128, 128, 4)
|
26
26
|
|
27
27
|
|
28
|
-
### 該当のソースコード
|
29
|
-
|
30
|
-
```python
|
31
|
-
import tensorflow as tf
|
32
|
-
from tensorflow.keras.layers import Conv2D, Activation, BatchNormalization, Dropout, Flatten, Dense
|
33
|
-
from tensorflow.keras import Model, layers
|
34
|
-
from keras.layers import Input
|
35
|
-
from keras.layers import Conv2D, MaxPooling2D, Input, Conv2DTranspose, Concatenate, BatchNormalization, UpSampling2D
|
36
|
-
#from keras.layers.merge import concatenate
|
37
|
-
from keras.layers import concatenate
|
38
|
-
|
39
|
-
|
40
|
-
#Dataset
|
41
|
-
Trimap = Trimap[..., tf.newaxis]
|
42
|
-
GT = GT[..., tf.newaxis]
|
43
|
-
DS = tf.concat([Combine,Trimap],3)
|
44
|
-
|
45
|
-
#free memory
|
46
|
-
del Combine
|
47
|
-
gc.collect()
|
48
|
-
del Trimap
|
49
|
-
gc.collect()
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
train_DS0 = DS[0:12000]#訓練データ(0:12000)
|
54
|
-
train_GT0 = GT[0:12000]
|
55
|
-
|
56
|
-
test_DS = DS[12000:13500]#訓練データ(12000):合計(13500)
|
57
|
-
test_GT = GT[12000:13500]
|
58
|
-
|
59
|
-
|
60
|
-
train_ds0 = np.asarray(train_DS0)#配列を生成
|
61
|
-
train_gt0 = np.asarray(train_GT0)
|
62
|
-
test_ds = np.asarray(test_DS)
|
63
|
-
test_gt = np.asarray(test_GT)
|
64
|
-
|
65
|
-
train_ds0 = train_ds0.astype('float32')/255.0#計算の都合上、入力を 0〜1の範囲の数値にした方が良い→データ型をfloatに変換したのち、255で割る
|
66
|
-
train_gt0 = train_gt0.astype('float32')/255.0
|
67
|
-
test_ds = test_ds.astype('float32')/255.0
|
68
|
-
test_gt = test_gt.astype('float32')/255.0
|
69
|
-
|
70
|
-
|
71
|
-
#パラメータ(エポックとバッチ)
|
72
|
-
epochs = 75#epochsはデータを何周するか
|
73
|
-
epoch_num=list(range(1,epochs+1))
|
74
|
-
train_loss_num=[]
|
75
|
-
test_loss_num=[]
|
76
|
-
|
77
|
-
train_ds = tf.data.Dataset.from_tensor_slices((train_ds0,train_gt0)).batch(50)#tf.data.Dataset.from_tensor_slices().batch()を用いてバッチ化(一定量のデータを集め、一括処理する)
|
78
|
-
test_ds = tf.data.Dataset.from_tensor_slices((test_ds,test_gt)).batch(20)
|
79
|
-
print(train_ds)
|
80
|
-
print(test_ds)
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
#U-Netの実装
|
85
|
-
def unet(size=(128,128,3)):#画像サイズ
|
86
|
-
x=Input(size)
|
87
|
-
inputs=x
|
88
|
-
|
89
|
-
#エンコーダ
|
90
|
-
conv1=Conv2D(64, (3, 3) , activation = 'relu', padding = 'same')(x)#3*3convolution(畳み込み)、フィルタ(カーネル)64、padding(サイズ保持)
|
91
|
-
conv1=Conv2D(64, (3, 3) , activation = 'relu', padding = 'same')(conv1)#二回反復
|
92
|
-
down1=MaxPooling2D(pool_size=(2, 2), strides=None)(conv1)#サイズ2*2の最大プーリング層(入力画像内の2*2の領域で最大の数値を出力)
|
93
|
-
|
94
|
-
conv2=Conv2D(128, (3, 3) , activation = 'relu', padding = 'same')(down1)#(活性化関数)ReLU関数は入力値が負であれば0、正であれば1
|
95
|
-
conv2=Conv2D(128, (3, 3) , activation = 'relu', padding = 'same')(conv2)
|
96
|
-
down2=MaxPooling2D((2, 2), strides=2)(conv2)#2*2 max pooling と stride(各次元方向に1つ隣の要素に移動するために必要なバイト数)2
|
97
|
-
|
98
|
-
conv3=Conv2D(256, (3, 3) , activation = 'relu', padding = 'same')(down2)
|
99
|
-
conv3=Conv2D(256, (3, 3) , activation = 'relu', padding = 'same')(conv3)
|
100
|
-
down3=MaxPooling2D((2, 2), strides=2)(conv3)
|
101
|
-
|
102
|
-
conv4=Conv2D(512, (3, 3) , activation = 'relu', padding = 'same')(down3)
|
103
|
-
conv4=Conv2D(512, (3, 3) , activation = 'relu', padding = 'same')(conv4)
|
104
|
-
drop4=Dropout(0.5)(conv4)#入力にドロップアウトを適用(過学習を防ぐ)
|
105
|
-
down4=MaxPooling2D((2, 2), strides=2)(drop4)
|
106
|
-
|
107
|
-
conv5=Conv2D(1024, (3, 3) , activation = 'relu', padding = 'same')(down4)
|
108
|
-
conv5=Conv2D(1024, (3, 3) , activation = 'relu', padding = 'same')(conv5)
|
109
|
-
drop5=Dropout(0.5)(conv5)
|
110
|
-
|
111
|
-
#デコーダ
|
112
|
-
up1=UpSampling2D((2, 2))(drop5)
|
113
|
-
conv6=Conv2D(512, (2, 2), activation='relu', padding='same')(up1)
|
114
|
-
concat1=concatenate([drop4, up1], axis=3)#対応する(同じサイズ同士)マップ同士を連結(concatenate()....配列同士を結合)
|
115
|
-
conv6=Conv2D(512, (3, 3), activation='relu', padding='same')(concat1)
|
116
|
-
conv6=Conv2D(512, (3, 3), activation='relu', padding='same')(conv6)
|
117
|
-
|
118
|
-
up2=UpSampling2D((2, 2))(drop5)
|
119
|
-
conv7=Conv2D(256, (2, 2), activation='relu', padding='same')(up2)#up1はミス?
|
120
|
-
concat2=concatenate([down3, up2], axis=3)
|
121
|
-
conv7=Conv2D(256, (3, 3), activation='relu', padding='same')(concat2)
|
122
|
-
conv7=Conv2D(256, (3, 3), activation='relu', padding='same')(conv7)
|
123
|
-
|
124
|
-
up3=UpSampling2D((2, 2))(conv6)
|
125
|
-
conv8=Conv2D(128, (2, 2), activation='relu', padding='same')(up3)
|
126
|
-
concat3=concatenate([down2, up3], axis=3)
|
127
|
-
conv8=Conv2D(128, (3, 3), activation='relu', padding='same')(concat3)
|
128
|
-
conv8=Conv2D(128, (3, 3), activation='relu', padding='same')(conv8)
|
129
|
-
|
130
|
-
up4=UpSampling2D((2, 2))(conv8)
|
131
|
-
conv9=Conv2D(64, (2, 2), activation='relu', padding='same')(up4)
|
132
|
-
concat4=concatenate([down1, up4], axis=3)
|
133
|
-
conv9=Conv2D(64, (3, 3), activation='relu', padding='same')(concat4)
|
134
|
-
conv9=Conv2D(64, (3, 3), activation='relu', padding='same')(conv9)
|
135
|
-
|
136
|
-
outputs = Conv2D(1, 1, activation='sigmoid')(x)#最後は1*1convolutionを使って2クラス(前、背)で分類?,sigmoid関数(あらゆる入力値を0~1.0の範囲の数値に変換して出力)
|
137
|
-
model = Model(inputs=[inputs], outputs=[outputs])#F...sigmoidで3チャンネル(RGB)、背景の差分
|
138
|
-
|
139
|
-
return model
|
140
|
-
|
141
|
-
model=unet()
|
142
|
-
|
143
|
-
#optimize(損失(予測値と正解値との差)を最小にする )
|
144
|
-
optimizer = tf.keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
|
145
|
-
train_loss = tf.keras.metrics.Mean(name='train_loss')#与えられた値の(加重された)平均を計算
|
146
|
-
test_loss = tf.keras.metrics.Mean(name='test_loss')
|
147
|
-
|
148
|
-
@tf.function#計算の高速化
|
149
|
-
def train_step(image,alpha):
|
150
|
-
with tf.GradientTape() as tape:#GradientTapeは勾配(実際の結果の差を少なくする)を求める
|
151
|
-
predictions = model(image)
|
152
|
-
loss = tf.reduce_mean(tf.square(tf.subtract(predictions, alpha)))#平均二乗誤差
|
153
|
-
gradients = tape.gradient(loss, model.trainable_variables)
|
154
|
-
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
|
155
|
-
train_loss(loss)
|
156
|
-
|
157
|
-
@tf.function
|
158
|
-
def test_step(image,alpha):
|
159
|
-
predictions = model(image)
|
160
|
-
t_loss = tf.reduce_mean(tf.square(tf.subtract(predictions, alpha)))
|
161
|
-
test_loss(t_loss)
|
162
|
-
|
163
|
-
##Do train & test
|
164
|
-
for epoch in range(epochs):
|
165
|
-
for image,alpha in train_ds:
|
166
|
-
train_step(image,alpha)
|
167
|
-
|
168
|
-
for test_image,test_alpha in test_ds:
|
169
|
-
test_step(test_image,test_alpha)
|
170
|
-
|
171
|
-
template = 'Epoch {}, Loss: {}, Test Loss: {}'
|
172
|
-
print (template.format(epoch+1,
|
173
|
-
train_loss.result(),
|
174
|
-
test_loss.result()
|
175
|
-
)
|
176
|
-
)
|
177
|
-
train_loss_num.append(train_loss.result())
|
178
|
-
test_loss_num.append(test_loss.result())
|
179
|
-
|
180
|
-
if (epoch+1)%5==0 or (epoch+1)==1: #5エポックごとにテストデータから一枚出力
|
181
|
-
for pre_image,alpha in test_ds:
|
182
|
-
decorded_images = model(pre_image)
|
183
|
-
break
|
184
|
-
deco_img = decorded_images[0]
|
185
|
-
deco_img = tf.squeeze(deco_img,[2])
|
186
|
-
plt.imshow(deco_img,cmap = "gray")
|
187
|
-
plt.show()
|
188
|
-
|
189
|
-
|
190
|
-
#グラフの表示
|
191
|
-
plt.xlabel("epoch")
|
192
|
-
plt.plot(epoch_num, train_loss_num, color = 'red', label="train_loss")#スキップ接続あり(赤)
|
193
|
-
plt.plot(epoch_num, test_loss_num, color = 'blue', label="test_loss")#スキップ接続なし(青)
|
194
|
-
plt.show()
|
195
|
-
|
196
|
-
|
197
|
-
```
|
198
|
-
|
199
|
-
### 試したこと
|
200
|
-
|
201
|
-
DSとGTは何度か割合を変更しました。
|
202
|
-
|
203
28
|
### 補足情報(FW/ツールのバージョンなど)
|
204
29
|
|
205
30
|
ここにより詳細な情報を記載してください。
|
2
情報の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
Google colabでUnetの実装を画像13500枚に対して行い、ロスを下げるためにデータセットを見直しました。
|
4
4
|
しかし、新しくエラーが出るようになりました。
|
5
|
+
train_step(image,alpha) ←この部分でエラーが出ています。
|
5
6
|
|
6
7
|
Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 128, 128, 3), found shape=(50, 128, 128, 4)
|
7
8
|
これは何を指しているのでしょうか?またどのように変更すれば良いのでしょうか?
|
1
問題の変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Unetの
|
1
|
+
Unetのエラーを解決したい
|
test
CHANGED
@@ -1,14 +1,27 @@
|
|
1
1
|
### 前提
|
2
2
|
|
3
|
-
Google colabでUnetの実装を画像1
|
3
|
+
Google colabでUnetの実装を画像13500枚に対して行い、ロスを下げるためにデータセットを見直しました。
|
4
|
-
|
4
|
+
しかし、新しくエラーが出るようになりました。
|
5
|
-
|
5
|
+
|
6
|
-
|
6
|
+
Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 128, 128, 3), found shape=(50, 128, 128, 4)
|
7
|
+
これは何を指しているのでしょうか?またどのように変更すれば良いのでしょうか?
|
7
8
|
|
8
9
|
### 実現したいこと
|
9
10
|
|
10
|
-
Unetの
|
11
|
+
Unetのエラーを解決したい
|
12
|
+
|
13
|
+
|
11
|
-
|
14
|
+
### エラー文
|
15
|
+
ValueError: in user code:
|
16
|
+
|
17
|
+
File "<ipython-input-4-78d0837b4be5>", line 112, in train_step *
|
18
|
+
predictions = model(image)
|
19
|
+
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler **
|
20
|
+
raise e.with_traceback(filtered_tb) from None
|
21
|
+
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
|
22
|
+
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
|
23
|
+
|
24
|
+
ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 128, 128, 3), found shape=(50, 128, 128, 4)
|
12
25
|
|
13
26
|
|
14
27
|
### 該当のソースコード
|
@@ -22,16 +35,31 @@
|
|
22
35
|
#from keras.layers.merge import concatenate
|
23
36
|
from keras.layers import concatenate
|
24
37
|
|
38
|
+
|
39
|
+
#Dataset
|
40
|
+
Trimap = Trimap[..., tf.newaxis]
|
41
|
+
GT = GT[..., tf.newaxis]
|
42
|
+
DS = tf.concat([Combine,Trimap],3)
|
43
|
+
|
44
|
+
#free memory
|
45
|
+
del Combine
|
46
|
+
gc.collect()
|
47
|
+
del Trimap
|
48
|
+
gc.collect()
|
49
|
+
|
50
|
+
|
51
|
+
|
25
|
-
train_
|
52
|
+
train_DS0 = DS[0:12000]#訓練データ(0:12000)
|
26
|
-
train_
|
53
|
+
train_GT0 = GT[0:12000]
|
27
|
-
|
54
|
+
|
28
|
-
test_
|
55
|
+
test_DS = DS[12000:13500]#訓練データ(12000):合計(13500)
|
29
|
-
test_
|
56
|
+
test_GT = GT[12000:13500]
|
30
|
-
|
57
|
+
|
58
|
+
|
31
|
-
train_ds0 = np.asarray(train_
|
59
|
+
train_ds0 = np.asarray(train_DS0)#配列を生成
|
32
|
-
train_gt0 = np.asarray(train_
|
60
|
+
train_gt0 = np.asarray(train_GT0)
|
33
|
-
test_ds = np.asarray(test_
|
61
|
+
test_ds = np.asarray(test_DS)
|
34
|
-
test_gt = np.asarray(test_
|
62
|
+
test_gt = np.asarray(test_GT)
|
35
63
|
|
36
64
|
train_ds0 = train_ds0.astype('float32')/255.0#計算の都合上、入力を 0〜1の範囲の数値にした方が良い→データ型をfloatに変換したのち、255で割る
|
37
65
|
train_gt0 = train_gt0.astype('float32')/255.0
|
@@ -40,15 +68,17 @@
|
|
40
68
|
|
41
69
|
|
42
70
|
#パラメータ(エポックとバッチ)
|
43
|
-
epochs =
|
71
|
+
epochs = 75#epochsはデータを何周するか
|
44
72
|
epoch_num=list(range(1,epochs+1))
|
45
73
|
train_loss_num=[]
|
46
74
|
test_loss_num=[]
|
47
75
|
|
48
|
-
train_ds = tf.data.Dataset.from_tensor_slices((train_ds0,train_gt0)).batch(
|
76
|
+
train_ds = tf.data.Dataset.from_tensor_slices((train_ds0,train_gt0)).batch(50)#tf.data.Dataset.from_tensor_slices().batch()を用いてバッチ化(一定量のデータを集め、一括処理する)
|
49
|
-
test_ds = tf.data.Dataset.from_tensor_slices((test_ds,test_gt)).batch(
|
77
|
+
test_ds = tf.data.Dataset.from_tensor_slices((test_ds,test_gt)).batch(20)
|
50
78
|
print(train_ds)
|
51
79
|
print(test_ds)
|
80
|
+
|
81
|
+
|
52
82
|
|
53
83
|
#U-Netの実装
|
54
84
|
def unet(size=(128,128,3)):#画像サイズ
|
@@ -85,7 +115,7 @@
|
|
85
115
|
conv6=Conv2D(512, (3, 3), activation='relu', padding='same')(conv6)
|
86
116
|
|
87
117
|
up2=UpSampling2D((2, 2))(drop5)
|
88
|
-
conv7=Conv2D(256, (2, 2), activation='relu', padding='same')(up1
|
118
|
+
conv7=Conv2D(256, (2, 2), activation='relu', padding='same')(up2)#up1はミス?
|
89
119
|
concat2=concatenate([down3, up2], axis=3)
|
90
120
|
conv7=Conv2D(256, (3, 3), activation='relu', padding='same')(concat2)
|
91
121
|
conv7=Conv2D(256, (3, 3), activation='relu', padding='same')(conv7)
|
@@ -146,11 +176,28 @@
|
|
146
176
|
train_loss_num.append(train_loss.result())
|
147
177
|
test_loss_num.append(test_loss.result())
|
148
178
|
|
179
|
+
if (epoch+1)%5==0 or (epoch+1)==1: #5エポックごとにテストデータから一枚出力
|
180
|
+
for pre_image,alpha in test_ds:
|
181
|
+
decorded_images = model(pre_image)
|
182
|
+
break
|
183
|
+
deco_img = decorded_images[0]
|
184
|
+
deco_img = tf.squeeze(deco_img,[2])
|
185
|
+
plt.imshow(deco_img,cmap = "gray")
|
186
|
+
plt.show()
|
187
|
+
|
188
|
+
|
189
|
+
#グラフの表示
|
190
|
+
plt.xlabel("epoch")
|
191
|
+
plt.plot(epoch_num, train_loss_num, color = 'red', label="train_loss")#スキップ接続あり(赤)
|
192
|
+
plt.plot(epoch_num, test_loss_num, color = 'blue', label="test_loss")#スキップ接続なし(青)
|
193
|
+
plt.show()
|
194
|
+
|
195
|
+
|
149
196
|
```
|
150
197
|
|
151
198
|
### 試したこと
|
152
199
|
|
153
|
-
|
200
|
+
DSとGTは何度か割合を変更しました。
|
154
201
|
|
155
202
|
### 補足情報(FW/ツールのバージョンなど)
|
156
203
|
|