質問編集履歴

1

文章の補足

2021/05/08 04:06

投稿

pasunon
pasunon

スコア1

test CHANGED
File without changes
test CHANGED
@@ -1,5 +1,189 @@
1
+ ### 前提・実現したいこと
2
+
1
3
  エディターにVScode 言語にpython3.6.13を使って人工知能のモデルを作成しました。
2
4
 
3
5
  モデルを学習をするところまでできたのですが、model.save()を使って保存したところうまく保存ができません。
4
6
 
5
7
  解決法を教えてください。
8
+
9
+
10
+
11
+
12
+
13
+ ### 発生している問題・エラーメッセージ
14
+
15
+
16
+
17
+ ```
18
+
19
+ call()を呼び出したときのエラー
20
+
21
+ ---------------------------------------------------------------------------
22
+
23
+ AttributeError Traceback (most recent call last)
24
+
25
+ <ipython-input-9-0b9011992b43> in <module>
26
+
27
+ ----> 1 call()
28
+
29
+
30
+
31
+ <ipython-input-7-b496908ddcf6> in call()
32
+
33
+ 3 image = Image.open(name)
34
+
35
+ 4 image = image.resize((64,64))
36
+
37
+ ----> 5 model = load_model("model.h5")
38
+
39
+ 6 np_image = np.array(image)
40
+
41
+ 7 np_image = np_image / 255
42
+
43
+
44
+
45
+ ~\anaconda3\envs\karas-env\lib\site-packages\keras\engine\saving.py in load_wrapper(*args, **kwargs)
46
+
47
+ 490 os.remove(tmp_filepath)
48
+
49
+ 491 return res
50
+
51
+ --> 492 return load_function(*args, **kwargs)
52
+
53
+ 493
54
+
55
+ 494 return load_wrapper
56
+
57
+
58
+
59
+ ~\anaconda3\envs\karas-env\lib\site-packages\keras\engine\saving.py in load_model(filepath, custom_objects, compile)
60
+
61
+ 582 if H5Dict.is_supported_type(filepath):
62
+
63
+ 583 with H5Dict(filepath, mode='r') as h5dict:
64
+
65
+ --> 584 model = _deserialize_model(h5dict, custom_objects, compile)
66
+
67
+ 585 elif hasattr(filepath, 'write') and callable(filepath.write):
68
+
69
+ 586 def load_function(h5file):
70
+
71
+
72
+
73
+ ~\anaconda3\envs\karas-env\lib\site-packages\keras\engine\saving.py in _deserialize_model(h5dict, custom_objects, compile)
74
+
75
+ 271 if model_config is None:
76
+
77
+ 272 raise ValueError('No model found in config.')
78
+
79
+ --> 273 model_config = json.loads(model_config.decode('utf-8'))
80
+
81
+ 274 model = model_from_config(model_config, custom_objects=custom_objects)
82
+
83
+ 275 model_weights_group = h5dict['model_weights']
84
+
85
+
86
+
87
+ AttributeError: 'str' object has no attribute 'decode'
88
+
89
+ ```
90
+
91
+
92
+
93
+ ### 該当のソースコード
94
+
95
+
96
+
97
+ ```python
98
+
99
+ def main():
100
+
101
+ model = Sequential()
102
+
103
+ model.add(Conv2D(64,(3,3),input_shape=(64,64,3)))
104
+
105
+ model.add(Activation("relu"))
106
+
107
+ model.add(MaxPooling2D(pool_size=(2,2)))
108
+
109
+ model.add(Conv2D(64,(3,3)))
110
+
111
+ model.add(Activation("relu"))
112
+
113
+ model.add(MaxPooling2D(pool_size=(2,2)))
114
+
115
+ model.add(Flatten())
116
+
117
+ model.add(Dense(256))
118
+
119
+ model.add(Activation("relu"))
120
+
121
+ model.add(Dense(2))
122
+
123
+ model.add(Activation("softmax"))
124
+
125
+ model.compile(optimizer="adam",loss="categorical_crossentropy",metrics=["accuracy"])
126
+
127
+ train_datagen = ImageDataGenerator(rescale=1./255)
128
+
129
+ test_datagen = ImageDataGenerator(rescale=1./255)
130
+
131
+ train_generator = train_datagen.flow_from_directory("data/train",target_size=(64,64),batch_size=10)
132
+
133
+ validation_generator = test_datagen.flow_from_directory("data/validation",target_size=(64,64),batch_size=10)
134
+
135
+ model.fit_generator(train_generator,epochs=20,steps_per_epoch=10,validation_data=validation_generator,validation_steps=10)
136
+
137
+ model.save("model.h5")
138
+
139
+ ```
140
+
141
+ ```python
142
+
143
+ def call():
144
+
145
+ name = "data/validation/dog/images3.jpg"
146
+
147
+ image = Image.open(name)
148
+
149
+ image = image.resize((64,64))
150
+
151
+ model = load_model("model.h5")
152
+
153
+ np_image = np.array(image)
154
+
155
+ np_image = np_image / 255
156
+
157
+ np_image = np_image[np.newaxis, :, :, :]
158
+
159
+ result = model.predict(np_image)
160
+
161
+ if result[0][0] > result[0][1]:
162
+
163
+ print("犬")
164
+
165
+ else:
166
+
167
+ print("猫")
168
+
169
+ ```
170
+
171
+
172
+
173
+ ### 試したこと
174
+
175
+ model.h5が文字化けしていたのでJISやUTF-16に変更してみましたが改善されませんでした。
176
+
177
+
178
+
179
+ ### 補足情報(FW/ツールのバージョンなど)
180
+
181
+
182
+
183
+ Windows10
184
+
185
+ Python3.6.13
186
+
187
+ Keras2.3.1
188
+
189
+ VSCode Jupyternotebook