-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiModelTrain.py
153 lines (115 loc) · 5.73 KB
/
multiModelTrain.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import os.path
import tensorflow as tf
import matplotlib.pyplot as plt
from configs import settings
from dataLoader import omniglotDataLoader
from models import RelationModel, CNNEncoder
encoderList = []
relationList = []
for _ in range(settings.MODEL_COUNT):
encoder = CNNEncoder()
relationNetwork = RelationModel(settings.RELATION_DIM)
encoderList.append(encoder)
relationList.append(relationNetwork)
dataLoader = omniglotDataLoader()
lossFn = tf.keras.losses.MeanSquaredError()
# optimizerList = [tf.keras.optimizers.Adam()] * settings.MODEL_COUNT
optimizer = tf.keras.optimizers.Adam()
train_loss = tf.keras.metrics.Mean(name="train_loss")
train_accuracy = tf.keras.metrics.CategoricalAccuracy(name="train_accuracy")
test_loss = tf.keras.metrics.Mean(name="test_loss")
test_accuracy = tf.keras.metrics.CategoricalAccuracy(name="test_accuracy")
def sampleDataset():
return dataLoader.getDataset()
def showTest(images, labels, predictions):
plt.figure(figsize=(20, 20))
for i in range(50):
plt.subplot(5, 10, i + 1)
plt.xticks([])
plt.xlabel("T: {}, P: {}".format(labels[i], predictions[i]))
plt.yticks([])
plt.grid(False)
plt.imshow(images[i])
plt.show()
def forward(train_images, test_images, modelIndex):
train_futures = encoderList[modelIndex](train_images, training=True)
test_futures = encoderList[modelIndex](test_images, training=True)
train_futures = tf.repeat(tf.expand_dims(train_futures, axis=0), settings.TEST_SHOT * settings.TRAIN_TEST_WAY,
axis=0)
test_futures = tf.repeat(tf.expand_dims(test_futures, axis=1), settings.TRAIN_SHOT * settings.TRAIN_TEST_WAY,
axis=1)
concat_futures = tf.concat([train_futures, test_futures], 4)
concat_futures = tf.reshape(concat_futures, [-1, 5, 5, 128])
relations = relationList[modelIndex](concat_futures, training=True)
relations = tf.reshape(relations, [settings.TEST_SHOT * settings.TRAIN_TEST_WAY, settings.TRAIN_TEST_WAY])
return relations
def labelEncode(labels):
return tf.one_hot(labels, depth=settings.TRAIN_TEST_WAY, axis=1)
def test():
test_accuracy.reset_state()
for _ in range(128):
# 采样一批数据集
train_images, train_labels, test_images, test_labels = dataLoader.sampleDatasetFromTest()
# predictions = tf.argmax(forward(train_images, test_images), axis=-1)
# 遍历模型进行预测
predictionsList = []
for modelIndex in range(settings.MODEL_COUNT):
predictionsList.append(tf.argmax(forward(train_images, test_images, modelIndex), axis=-1))
predictionsConcat = [[] for _ in range(len(predictionsList[0]))]
for predictions in predictionsList:
for i, prediction in enumerate(predictions):
predictionsConcat[i].append(prediction)
predictions = []
for multiPrediction in predictionsConcat:
predictions.append(max(multiPrediction, key=multiPrediction.count))
oneHotPredictions = labelEncode(predictions)
oneHotLabels = labelEncode(test_labels)
test_accuracy(oneHotLabels, oneHotPredictions)
print("test_accuracy={:.4f}".format(test_accuracy.result()))
# showTest(test_images, test_labels, predictions)
test()
def train_step(train_images, train_labels, test_images, test_labels, modelIndex):
with tf.GradientTape() as tape:
relations = forward(train_images, test_images, modelIndex)
oneHotLabels = labelEncode(test_labels)
loss = lossFn(oneHotLabels, relations)
gradiens = tape.gradient(loss, encoderList[modelIndex].trainable_variables + relationList[modelIndex].trainable_variables)
optimizer.apply_gradients(zip(gradiens, encoderList[modelIndex].trainable_variables + relationList[modelIndex].trainable_variables))
train_loss(loss)
train_accuracy(oneHotLabels, relations)
def test_step(train_images, train_labels, test_images, test_labels, modelIndex):
relations = forward(train_images, test_images, modelIndex)
oneHotLabels = labelEncode(test_labels)
loss = lossFn(oneHotLabels, relations)
test_loss(loss)
test_accuracy(oneHotLabels, relations)
COUNT = 128
EPOCHS = 40
WEIGHTS_SAVE_PATH = "./weights"
ENCODER_WEIGHTS_SAVE_NAME = "omniglot_encoder_{:03d}.ckpt"
RELATION_WEIGHTS_SAVE_NAME = "omniglot_relation_{:03d}.ckpt"
for modelIndex in range(settings.MODEL_COUNT):
optimizer = tf.keras.optimizers.Adam()
# optimizer.build(encoderList[modelIndex].trainable_variables + relationList[modelIndex].trainable_variables)
for epoch in range(EPOCHS):
train_loss.reset_state()
train_accuracy.reset_state()
test_loss.reset_state()
test_accuracy.reset_state()
for _ in range(COUNT):
train_images, train_labels, test_images, test_labels = sampleDataset()
train_step(train_images, train_labels, test_images, test_labels, modelIndex)
for _ in range(COUNT // 2):
train_images, train_labels, test_images, test_labels = sampleDataset()
test_step(train_images, train_labels, test_images, test_labels, modelIndex)
print(
"Epoch: {:.2f} ".format(epoch + 1),
"ModelIndex: {} ".format(modelIndex),
"train_loss: {:.2f} ".format(train_loss.result()),
"train_accuracy: {:.4f} ".format(train_accuracy.result()),
"test_loss: {:.2f} ".format(test_loss.result()),
"test_accuracy: {:.4f} ".format(test_accuracy.result())
)
encoderList[modelIndex].save_weights(os.path.join(WEIGHTS_SAVE_PATH, ENCODER_WEIGHTS_SAVE_NAME.format(modelIndex)))
relationList[modelIndex].save_weights(os.path.join(WEIGHTS_SAVE_PATH, RELATION_WEIGHTS_SAVE_NAME.format(modelIndex)))
test()