-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn.py
60 lines (50 loc) · 1.64 KB
/
cnn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import cnn_model
import keras
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
# 入力と出力を指定
im_rows = 32 # 画像の縦ピクセルサイズ
im_cols = 32 # 画像の横未来セルサイズ
im_color = 3 # 画像の色空間
in_shape = (im_rows, im_cols, im_color)
nb_classes = 8
# 写真データを読み込み
photos = np.load("image/photos.npz")
x = photos["x"]
y = photos["y"]
# 読み込んだデータを三次元配列に変換
x = x.reshape(-1, im_rows, im_cols, im_color)
x = x.astype("float32") / 255
# ラベルデータをone-hotベクトルに直す
y = keras.utils.np_utils.to_categorical(y.astype("int32"), nb_classes)
# 学習用とテスト用に分ける
x_train, x_test, y_train, y_test = train_test_split(
x, y, train_size=0.8)
# CNNモデルを取得
model = cnn_model.get_model(in_shape, nb_classes)
# 学習を実行
hist = model.fit(x_train, y_train,
batch_size=32,
epochs=20,
verbose=1,
validation_data=(x_test, y_test))
# モデルを評価
score = model.evaluate(x_test, y_test, verbose=1)
print("正解率=", score[1], "loss=", score[0])
# 学習の様子をグラフへ描画
# 正解率の推移をプロット
"""
plt.plot(hist.history['accuracy'])
plt.plot(hist.history["val_accuracy"])
plt.title("Accuracy")
plt.legend(["train", "test"], loc="upper left")
plt.show()
# ロスの推移をプロット
plt.plot(hist.history["loss"])
plt.plot(hist.history["val_loss"])
plt.title("Loss")
plt.legend(["train", "test"], loc="upper left")
plt.show()
"""
model.save_weights("./image/photos-model-light.hdf5")