-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
126 lines (83 loc) · 3.01 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
https://www.tensorflow.org/tutorials/images/intro_to_cnns?hl=ja
に載ってるサンプルをちょっと改変
"""
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras import datasets, layers, models
from tensorflow.python.keras.backend import learning_phase
from model import get_model
# ================
# 手動設定パラメータ
# ================
model_name = 'cnn' # 'cnn'か'resnet50' か 'my_cnn' (model.pyを参照)
epoch = 25
learning_rate = 0.0001
batch_size = 16
# ===================
# データセットの読み込み
# ===================
(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data() # サイズは 28x28のモノクロ画像
# ===================
# データセットの前処理
# ===================
train_images = train_images.reshape((60000, 28, 28, 1)) # 28x28x1の配列にする
test_images = test_images.reshape((10000, 28, 28, 1)) # 28x28x1の配列にする
if model_name == 'resnet50':
# ResNet50というモデルは32x32以上の画像サイズしか受け取れないため、リサイズする
train_images = tf.image.resize(train_images, [32,32])
test_images = tf.image.resize(test_images, [32,32])
# ===================
# モデルの読み込み
# ===================
model = get_model(model_name=model_name, input_shape=train_images.shape[1:])
adam = tf.keras.optimizers.Adam(lr=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=0.00000001, decay=0.0, amsgrad=False)
model.compile(optimizer=adam, loss='sparse_categorical_crossentropy', metrics=['acc'])
print(model.summary())
# ===================
# モデルを学習
# ===================
history = model.fit(train_images, train_labels, epochs=epoch, validation_split=0.2, verbose=2)
# ============
# モデルをテスト
# ============
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2) #予測結果から正解率まで自動計算
print('test_loss', test_loss)
print('test_accuracy', test_acc)
# =================
# 学習結果のグラフを保存
# =================
# Loss
plt.figure(figsize=(8,8))
plt.title('Loss Value')
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.legend(['loss', 'val_loss'])
plt.xlim(0, epoch)
plt.show()
plt.savefig('loss_history.png')
plt.clf()
# Accuracy
plt.figure(figsize=(8,8))
plt.title('Accuracy')
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.legend(['acc', 'val_acc'])
plt.xlim(0, epoch)
plt.ylim(0, 1.0)
plt.show()
plt.savefig('accuracy_history.png')
plt.clf()
"""
追加
"""
tf.keras.models.save_model(model, 'model.h5')
predictions = model.predict(test_images) #予測結果のみ取得
print(predictions[0]) #テストデータセットの1枚目の予測結果。(各数字である確率)
print(predictions[0].argmax()) #テストデータセットの1枚目の予測結果。(何の数字か。)
print('#################################')
for i in range(predictions.shape[0]):
print(predictions[i].argmax())
"""
追加ここまで
"""