def run_training(log_file): # load the data print(150 * '*') with open("./mnist.pkl", "rb") as fid: dataset = pickle.load(fid, encoding='latin1') train_x, train_y = dataset[0] test_x, test_y = dataset[1] train_num = train_x.shape[0] test_num = test_x.shape[0] # label_index: list of list # ex) label_index[3] conatins every indicies of 3 data num_classes = FLAGS.num_classes label_index = [[] for i in range(num_classes)] for i in range(len(train_y)): label_index[train_y[i]].append(i) # construct the computation graph images = tf.placeholder(tf.float32, shape=[None, 1, 28, 28]) labels = tf.placeholder(tf.int32, shape=[None]) lr = tf.placeholder(tf.float32) features, _ = mnist_net(images) centers = func.construct_center(features, FLAGS.num_classes) loss = func.dce_loss(features, labels, centers, FLAGS.temp) eval_correct = func.evaluation(features, labels, centers) find_wrong = func.find_wrong(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() # init 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 (training and test) 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 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) ratio = FLAGS.ratio iter = 0 step = 0 log_loss = 0 log_acc = 0 # pdb.set_trace() # train the framework with the training data while stopping < FLAGS.stop: time1 = time.time() loss_now = 0.0 score_now = 0.0 iter += 1 if iter % 100 == 0: print("iter : {}".format(iter)) wrong_list = np.zeros(num_classes) for i in range(batch_num): # selecting probability class_prob = ratio * wrong_list / batch_size + ( 1 - ratio * sum(wrong_list) / batch_size) / 10 input_num_list = [ math.ceil(batch_size * class_prob[j]) for j in range(num_classes) ] batch_indicies = [] for k in range(num_classes): rand_smpl = [ label_index[k][j] for j in sorted( random.sample(range(len(label_index[k])), input_num_list[k])) ] batch_indicies += rand_smpl np.random.shuffle(batch_indicies) # pdb.set_trace() batch_x = train_x[batch_indicies[:batch_size]] batch_y = train_y[batch_indicies[:batch_size]] # JG batch_x_reshp = np.reshape(batch_x, (batch_size, 1, 28, 28)) # batch_x.shape : (50, 784) # images : <tf.Tensor 'Placeholder:0' shape=(?, 1, 28, 28) dtype=float32> ##result = sess.run([train_op, loss, eval_correct], feed_dict={images:batch_x, ## labels:batch_y, lr:FLAGS.learning_rate}) result = sess.run([train_op, loss, eval_correct, find_wrong], feed_dict={ images: batch_x_reshp, labels: batch_y, lr: FLAGS.learning_rate }) loss_now += result[1] score_now += result[2] wrong_list = result[3] log_loss += result[1] log_acc += result[2] if (step % FLAGS.log_period == 0) and (step != 0): log_file.write('{}, loss, {:.3f}, acc, {:.3f}\n'.format( step, log_loss / FLAGS.log_period, log_acc / batch_size / FLAGS.log_period * 100)) log_loss = 0 log_acc = 0 step += 1 score_now /= train_num print('epoch {}: training: loss --> {:.3f}, acc --> {:.3f}%'.format( epoch, loss_now, score_now * 100)) 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)) # pdb.set_trace() # test the framework with the test data test_score = do_eval(sess, eval_correct, images, labels, test_x, test_y) print('accuracy on the test dataset: {:.3f}%'.format(test_score * 100)) # test the framework with the test data test_score = do_eval(sess, eval_correct, images, labels, test_x, test_y) sess.close()
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()
def run_training(): # load the data print (150*'*') #with open("mnist.data", "rb") as fid: with open("/home/ubuntu/codes/Convolutional-Prototype-Learning/mnist.pkl", "rb") as fid: dataset = pickle.load(fid, encoding='latin1') train_x, train_y = dataset[0] test_x, test_y = dataset[1] train_num = train_x.shape[0] test_num = test_x.shape[0] # construct the computation graph images = tf.placeholder(tf.float32, shape=[None,1,28,28]) 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 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 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]] batch_x_reshp = np.reshape(batch_x, (batch_size, 1, 28, 28)) result = sess.run([train_op, loss, eval_correct], feed_dict={images:batch_x_reshp, 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)) pdb.set_trace() # test the framework with the test data test_score = do_eval(sess, eval_correct, images, labels, test_x, test_y) print ('accuracy on the test dataset: {:.3f}%'.format(test_score*100)) sess.close()
def run_training(): # load the data print (150*'*') #with open("mnist.data", "rb") as fid: with open("/home/ubuntu/datasets/mnist.pkl", "rb") as fid: dataset = pickle.load(fid, encoding='latin1') train_x, train_y = dataset[0] test_x, test_y = dataset[1] train_num = train_x.shape[0] test_num = test_x.shape[0] # construct the computation graph images = tf.placeholder(tf.float32, shape=[None,1,28,28]) 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) sess = tf.Session() load_saver = tf.train.Saver() os.makedirs(FLAGS.log_dir, exist_ok=True) file_list = os.listdir(FLAGS.log_dir) keep_last_int = 0 last_load_file_name = '' for name in file_list: if len(name.split('.')) < 2: continue if keep_last_int < int(name.split('.')[1].split('-')[1]): keep_last_int = int(name.split('.')[1].split('-')[1]) last_load_file_name = '.'.join(name.split('.')[:2]) load_file = os.path.join(FLAGS.log_dir, last_load_file_name) if os.path.isfile(load_file+".meta"): load_saver.restore(sess, load_file) else: 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 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 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]] batch_x_reshp = np.reshape(batch_x, (batch_size, 1, 28, 28)) result = sess.run([train_op, loss, eval_correct], feed_dict={images:batch_x_reshp, labels:batch_y, lr:FLAGS.learning_rate}) # features_eval = sess.run([features], feed_dict={images:batch_x_reshp, # labels:batch_y, lr:FLAGS.learning_rate}) # features_eval.shape (1, 50, 2) 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)) break # For testing only the first episode #pdb.set_trace() # test the framework with the test data test_score, eval_dots_perclass = do_eval(sess, eval_correct, images, labels, test_x, test_y, features) compute_overlap(eval_dots_perclass) # eval_dots_perclass # len(eval_dots_perclass) : 10 [num_categories=10] # eval_dots_perclass[0] : dots of category 0 # eval_dots_perclass[0][0] : (x, y) of index 0 of category 0 print ('accuracy on the test dataset: {:.3f}%'.format(test_score*100)) # pdb.set_trace() sess.close()