回答編集履歴
1
あ
test
CHANGED
@@ -1 +1,83 @@
|
|
1
1
|
設定ファイル `~/.keras/keras.json` で `"image_data_format": "channels_last"` とちゃんとなっていますか?
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
## 追記
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
自分の環境で試したところ、確かに1チャンネルでは質問のエラーが出ました。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
```python
|
14
|
+
|
15
|
+
from tensorflow.keras.applications.vgg16 import VGG16
|
16
|
+
|
17
|
+
from tensorflow.keras.layers import Input
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
# 学習済みモデル VGG16 を構築する。
|
22
|
+
|
23
|
+
h, w, c = 256, 256, 1
|
24
|
+
|
25
|
+
input_tensor = Input(shape = (w, h, c))
|
26
|
+
|
27
|
+
vgg16 = VGG16(include_top=False, input_tensor=input_tensor)
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
```
|
34
|
+
|
35
|
+
ValueError: Shapes (3, 3, 1, 64) and (64, 3, 3, 3) are incompatible
|
36
|
+
|
37
|
+
```
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
Keras のコードを調べたところ、コンストラクタの引数で `weights='imagenet'` となっており、何も指定しない場合は ImageNet で学習済みのモデルを使用するようになっています。これは3チャンネル画像の入力のモデルであるため、重みを読み込む際に不整合となりエラーとなっておりました。
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
質問者さんが取れる対策は次のいずれかです。
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
1 `weights=None` と指定し、ImageNet の学習済みモデルを使用しない。
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
# 学習済みモデル VGG16 を構築する。
|
56
|
+
|
57
|
+
h, w, c = 256, 256, 3
|
58
|
+
|
59
|
+
input_tensor = Input(shape = (w, h, c))
|
60
|
+
|
61
|
+
vgg16 = VGG16(include_top=False, weights=None, input_tensor=input_tensor)
|
62
|
+
|
63
|
+
```
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
2 入力をRGB画像に変換する。
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
OpenCV の場合
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
```python
|
76
|
+
|
77
|
+
cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
|
78
|
+
|
79
|
+
```
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
フルスクラッチで学習する場合は1、ImageNet の学習済みモデルを使用したい場合は2を選んでください。
|