forked from YangHM/Convolutional-Prototype-Learning
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmnist_gcpl.py
More file actions
173 lines (138 loc) · 6.3 KB
/
mnist_gcpl.py
File metadata and controls
173 lines (138 loc) · 6.3 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# An example on MNIST to introduce how to train and test under GCPL
from nets import mnist_net
import functions as func
import numpy as np
import tensorflow as tf
import argparse
import time
import os
import pickle
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
FLAGS = None
# compute accuracy on the test dataset
def do_eval(sess, eval_correct, images, labels, test_x, test_y):
true_count = 0.0
test_num = test_y.shape[0]
batch_size = FLAGS.batch_size
batch_num = test_num // batch_size if test_num % batch_size == 0 else test_num // batch_size + 1
for i in range(batch_num):
batch_x = test_x[i*batch_size:(i+1)*batch_size]
batch_y = test_y[i*batch_size:(i+1)*batch_size]
true_count += sess.run(eval_correct, feed_dict={images:batch_x, labels:batch_y})
return true_count / test_num
# initialize the prototype with the mean vector (on the train dataset) of the corresponding class
def compute_centers(sess, add_op, count_op, average_op, images_placeholder, labels_placeholder, train_x, train_y):
train_num = train_y.shape[0]
batch_size = FLAGS.batch_size
batch_num = train_num // batch_size if train_num % batch_size == 0 else train_num // batch_size + 1
for i in range(batch_num):
batch_x = train_x[i*batch_size:(i+1)*batch_size]
batch_y = train_y[i*batch_size:(i+1)*batch_size]
sess.run([add_op, count_op], feed_dict={images_placeholder:batch_x, labels_placeholder:batch_y})
sess.run(average_op)
def run_training():
# load the data
print (150*'*')
train_x = mnist.train.images
train_y = mnist.train.labels
train_y = np.argmax(train_y, axis=1)
test_x = mnist.test.images
test_y = mnist.test.labels
test_y = np.argmax(test_y, axis=1)
# train_x, train_y = dataset[0]
# test_x, test_y = dataset[1]
# train_x = [np.reshape(x, (784, 1)) for x in train_x]
# train_y = [vectorized_label(x) for x in train_y]
train_num = train_x.shape[0]
# test_num = test_x.shape[0]
print("train:" + str(train_x.shape) + "label:" + str(train_y.shape));
# construct the computation graph
images = tf.placeholder(tf.float32, shape=[None,784])
labels = tf.placeholder(tf.int32, shape=[None])
lr= tf.placeholder(tf.float32)
features, _ = mnist_net(images)
centers = func.construct_center(features, FLAGS.num_classes)
loss1 = func.dce_loss(features, labels, centers, FLAGS.temp)
loss2 = func.pl_loss(features, labels, centers)
loss = loss1 + FLAGS.weight_pl * loss2
eval_correct = func.evaluation(features, labels, centers)
train_op = func.training(loss, lr)
#counts = tf.get_variable('counts', [FLAGS.num_classes], dtype=tf.int32,
# initializer=tf.constant_initializer(0), trainable=False)
#add_op, count_op, average_op = net.init_centers(features, labels, centers, counts)
init = tf.global_variables_initializer()
# initialize the variables
sess = tf.Session()
sess.run(init)
#compute_centers(sess, add_op, count_op, average_op, images, labels, train_x, train_y)
# run the computation graph (train and test process)
epoch = 1
loss_before = np.inf
score_before = 0.0
stopping = 0
index = list(range(train_num))
np.random.shuffle(index)
batch_size = FLAGS.batch_size
# print("batch size",batch_size)
batch_num = train_num//batch_size if train_num % batch_size==0 else train_num//batch_size+1
#saver = tf.train.Saver(max_to_keep=1)
# train the framework with the training data
while stopping<FLAGS.stop:
time1 = time.time()
loss_now = 0.0
score_now = 0.0
print("centers:",sess.run(centers))
for i in range(batch_num):
batch_x = train_x[index[i*batch_size:(i+1)*batch_size]]
batch_y = train_y[index[i*batch_size:(i+1)*batch_size]]
# print("train images:",batch_x.shape,"label:",batch_y.shape)
result = sess.run([train_op, loss, eval_correct], feed_dict={images:batch_x,
labels:batch_y, lr:FLAGS.learning_rate})
loss_now += result[1]
score_now += result[2]
score_now /= train_num
print('epoch {}: training: loss --> {:.3f}, acc --> {:.3f}%'.format(epoch, loss_now, score_now*100))
#print sess.run(centers)
if loss_now > loss_before or score_now < score_before:
stopping += 1
FLAGS.learning_rate *= FLAGS.decay
print ("\033[1;31;40mdecay learning rate {}th time!\033[0m".format(stopping))
loss_before = loss_now
score_before = score_now
#checkpoint_file = os.path.join(FLAGS.log_dir, 'model.ckpt')
#saver.save(sess, checkpoint_file, global_step=epoch)
epoch += 1
np.random.shuffle(index)
time2 = time.time()
print('time for this epoch: {:.3f} minutes'.format((time2-time1)/60.0))
# test the framework with the test data
test_score = do_eval(sess, eval_correct, images, labels, test_x, test_y)
print('测试集准确率 {:.10f}%'.format(test_score * 100))
sess.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--learning_rate', type=float, default=0.001, help='initial learning rate')
parser.add_argument('--batch_size', type=int, default=50, help='batch size for training')
#parser.add_argument('--log_dir', type=str, default='data/', help='directory to save the data')
parser.add_argument('--stop', type=int, default=50, help='stopping number')
parser.add_argument('--decay', type=float, default=0.3, help='the value to decay the learning rate')
parser.add_argument('--temp', type=float, default=1.0, help='the temperature used for calculating the loss')
parser.add_argument('--weight_pl', type=float, default=0.001, help='the weight for the prototype loss (PL)')
parser.add_argument('--gpu', type=int, default=0, help='the gpu id for use')
parser.add_argument('--num_classes', type=int, default=10, help='the number of the classes')
FLAGS, unparsed = parser.parse_known_args()
print (150*'*')
print ('Configuration of the training:')
print ('learning rate:', FLAGS.learning_rate)
print ('batch size:', FLAGS.batch_size)
print ('stopping:', FLAGS.stop)
print ('learning rate decay:', FLAGS.decay)
print ('value of the temperature:', FLAGS.temp)
print ('prototype loss weight:', FLAGS.weight_pl)
print ('number of classes:', FLAGS.num_classes)
print ('GPU id:', FLAGS.gpu)
#print 'path to save the model:', FLAGS.log_dir
os.environ["CUDA_VISIBLE_DEVICES"] = str(FLAGS.gpu)
run_training()