質問編集履歴
1
keras_cnn.h5の保存コードを追加いたしました。よろしくお願い致します。
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
kerasで学習させたh5モデルからmlmodelへの変換
|
1
|
+
kerasで学習させたh5モデルからmlmodelへの変換しようとするとエラーがでる
|
body
CHANGED
@@ -43,6 +43,79 @@
|
|
43
43
|
coreml_model.save('predict.mlmodel')
|
44
44
|
```
|
45
45
|
|
46
|
+
|
47
|
+
```
|
48
|
+
from keras.models import Sequential
|
49
|
+
|
50
|
+
from keras.layers import Conv2D, MaxPooling2D
|
51
|
+
from keras.layers import Activation, Dropout, Flatten, Dense
|
52
|
+
from keras.utils import np_utils
|
53
|
+
import keras
|
54
|
+
import numpy as np
|
55
|
+
|
56
|
+
|
57
|
+
classes = ["perfect", "normal", "bad"]
|
58
|
+
num_classes = len(classes)
|
59
|
+
image_size = 75
|
60
|
+
|
61
|
+
|
62
|
+
def main():
|
63
|
+
X_train, X_test, y_train, y_test = np.load("./Judgment.npy")
|
64
|
+
X_train = X_train.astype("float") / 256
|
65
|
+
X_test = X_test.astype("float") / 256
|
66
|
+
y_train = np_utils.to_categorical(y_train, num_classes)
|
67
|
+
y_test = np_utils.to_categorical(y_test, num_classes)
|
68
|
+
|
69
|
+
model = model_train(X_train, y_train)
|
70
|
+
model_eval(model, X_test, y_test)
|
71
|
+
|
72
|
+
|
73
|
+
def model_train(X, y):
|
74
|
+
model = Sequential()
|
75
|
+
model.add(Conv2D(32, (3, 3), padding='same', input_shape=X.shape[1:]))
|
76
|
+
model.add(Activation('relu'))
|
77
|
+
model.add(Conv2D(32, (3, 3)))
|
78
|
+
model.add(Activation('relu'))
|
79
|
+
model.add(MaxPooling2D(pool_size=(2, 2)))
|
80
|
+
model.add(Dropout(0.25))
|
81
|
+
|
82
|
+
model.add(Conv2D(64, (3, 3), padding='same'))
|
83
|
+
model.add(Activation('relu'))
|
84
|
+
model.add(Conv2D(64, (3, 3)))
|
85
|
+
model.add(Activation('relu'))
|
86
|
+
model.add(MaxPooling2D(pool_size=(2, 2)))
|
87
|
+
model.add(Dropout(0.25))
|
88
|
+
|
89
|
+
model.add(Flatten())
|
90
|
+
model.add(Dense(512))
|
91
|
+
model.add(Activation('relu'))
|
92
|
+
model.add(Dropout(0.5))
|
93
|
+
model.add(Dense(3))
|
94
|
+
model.add(Activation('softmax'))
|
95
|
+
|
96
|
+
opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
|
97
|
+
model.compile(
|
98
|
+
loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
|
99
|
+
model.fit(X, y, batch_size=32, epochs=100)
|
100
|
+
|
101
|
+
|
102
|
+
model.save('./keras_cnn.h5')
|
103
|
+
|
104
|
+
return model
|
105
|
+
|
106
|
+
|
107
|
+
def model_eval(model, X, y):
|
108
|
+
scores = model.evaluate(X, y, verbose=1)
|
109
|
+
print('Test Loss: ', scores[0])
|
110
|
+
print('Test Accuracy: ', scores[1])
|
111
|
+
|
112
|
+
|
113
|
+
if __name__ == "__main__":
|
114
|
+
main()
|
115
|
+
```
|
116
|
+
|
117
|
+
|
118
|
+
|
46
119
|
### 試したこと
|
47
120
|
|
48
121
|
python2.7で同様の条件での実行
|