def __init__(self, dataset, batch_size): # Get cifar data print( '-----------------------------data_layer--------------------------------' ) self.dataset = dataset self.batch_size = batch_size if dataset == 'cifar10': print('--------loading cifar10 dataset------------------') self.train_images, self.train_labels, self.valid_images, self.valid_labels = cifar_tools.get_dataset_cifar10( 'training') print(self.train_images.shape, self.train_labels.shape) print(self.valid_images.shape, self.valid_labels.shape) self.test_images, self.test_labels = cifar_tools.get_dataset_cifar10( 'test') print(self.test_images.shape, self.test_labels.shape) self.sup_by_label = backend.sample_by_label( self.train_images, self.train_labels, -1, 10, None) elif dataset == 'cifar100': print('--------loading cifar100 dataset------------------') self.train_images, self.train_labels, self.valid_images, self.valid_labels = cifar_tools.get_dataset_cifar100( 'training') print(self.train_images.shape, self.train_labels.shape) print(self.valid_images.shape, self.valid_labels.shape) self.test_images, self.test_labels = cifar_tools.get_dataset_cifar100( 'test') print(self.test_images.shape, self.test_labels.shape) self.sup_by_label = None elif dataset == 'mnist': print('--------loading mnist dataset------------------') self.train_images, self.train_labels = mnist_tools.get_data( 'train') print(self.train_images.shape, self.train_labels.shape) self.test_images, self.test_labels = mnist_tools.get_data('test') print(self.test_images.shape, self.test_labels.shape) self.sup_by_label = None elif dataset == 'stl10': print('--------loading stl10 dataset------------------') self.trainval_images, self.trainval_labels = stl10_tools.get_data( 'training') self.train_images, self.train_labels = self.trainval_images[: 4500], self.trainval_labels[: 4500] self.valid_images, self.valid_labels = self.trainval_images[ 4500:], self.trainval_labels[4500:] print(self.train_images.shape, self.train_labels.shape) print(self.valid_images.shape, self.valid_labels.shape) self.test_images, self.test_labels = stl10_tools.get_data('test') print(self.test_images.shape, self.test_labels.shape) self.sup_by_label = None self.it = [0, 0] self.perm = [[], []] print( '-----------------------------data_layer--------------------------------' )
def init_data(): train_images, train_labels = mnist_tools.get_data('train') test_images, test_labels = mnist_tools.get_data('test') # Sample labeled training subset. seed = FLAGS.sup_seed if FLAGS.sup_seed != -1 else np.random.randint( 0, 1000) print('Seed:', seed) sup_by_label = semisup.sample_by_label(train_images, train_labels, FLAGS.sup_per_class, NUM_LABELS, seed) return sup_by_label, train_images, train_labels, test_images, test_labels, seed
def main(_): train_images, train_labels = mnist_tools.get_data('train') test_images, test_labels = mnist_tools.get_data('test') # Sample labeled training subset. seed = FLAGS.sup_seed if FLAGS.sup_seed != -1 else np.random.randint( 0, 1000) print('Seed:', seed) sup_by_label = semisup.sample_by_label(train_images, train_labels, FLAGS.sup_per_class, NUM_LABELS, seed) # add twos, fours, and sixes if FLAGS.testadd: num_to_add = 10 for i in range(10): items = np.where(train_labels == i)[0] inds = np.random.choice(len(items), num_to_add, replace=False) sup_by_label[i] = np.vstack( [sup_by_label[i], train_images[items[inds]]]) add_random_samples = 0 if add_random_samples > 0: rng = np.random.RandomState() indices = rng.choice(len(train_images), add_random_samples, False) for i in indices: l = train_labels[i] sup_by_label[l] = np.vstack([sup_by_label[l], [train_images[i]]]) print(l) graph = tf.Graph() with graph.as_default(): model = semisup.SemisupModel(semisup.architectures.mnist_model, NUM_LABELS, IMAGE_SHAPE, dropout_keep_prob=0.8) # Set up inputs. if FLAGS.random_batches: sup_lbls = np.asarray( np.hstack([ np.ones(len(i)) * ind for ind, i in enumerate(sup_by_label) ]), np.int) sup_images = np.vstack(sup_by_label) t_sup_images, t_sup_labels = semisup.create_input( sup_images, sup_lbls, FLAGS.sup_per_batch * NUM_LABELS) else: t_sup_images, t_sup_labels = semisup.create_per_class_inputs( sup_by_label, FLAGS.sup_per_batch) # Compute embeddings and logits. t_sup_emb = model.image_to_embedding(t_sup_images) t_sup_logit = model.embedding_to_logit(t_sup_emb) # Add losses. if FLAGS.semisup: if FLAGS.equal_cls_unsup: allimgs_bylabel = semisup.sample_by_label( train_images, train_labels, 5000, NUM_LABELS, seed) t_unsup_images, _ = semisup.create_per_class_inputs( allimgs_bylabel, FLAGS.sup_per_batch) else: t_unsup_images, _ = semisup.create_input( train_images, train_labels, FLAGS.unsup_batch_size) t_unsup_emb = model.image_to_embedding(t_unsup_images) model.add_semisup_loss(t_sup_emb, t_unsup_emb, t_sup_labels, walker_weight=FLAGS.walker_weight, visit_weight=FLAGS.visit_weight, proximity_weight=FLAGS.proximity_weight) model.add_logit_loss(t_sup_logit, t_sup_labels) t_learning_rate = tf.train.exponential_decay(FLAGS.learning_rate, model.step, FLAGS.decay_steps, FLAGS.decay_factor, staircase=True) train_op = model.create_train_op(t_learning_rate) summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.logdir, graph) saver = tf.train.Saver() with tf.Session(graph=graph) as sess: tf.global_variables_initializer().run() coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) for step in range(FLAGS.max_steps): _, summaries = sess.run([train_op, summary_op]) if (step + 1) % FLAGS.eval_interval == 0 or step == 99: print('Step: %d' % step) test_pred = model.classify(test_images, sess).argmax(-1) conf_mtx = semisup.confusion_matrix(test_labels, test_pred, NUM_LABELS) test_err = (test_labels != test_pred).mean() * 100 print(conf_mtx) print('Test error: %.2f %%' % test_err) print() test_summary = tf.Summary(value=[ tf.Summary.Value(tag='Test Err', simple_value=test_err) ]) summary_writer.add_summary(summaries, step) summary_writer.add_summary(test_summary, step) saver.save(sess, FLAGS.logdir, model.step) coord.request_stop() coord.join(threads)
def main(_): FLAGS.emb_size = 128 optimizer = 'adam' train_images, train_labels = mnist_tools.get_data('train') test_images, test_labels = mnist_tools.get_data('test') # Sample labeled training subset. seed = FLAGS.sup_seed if FLAGS.sup_seed != -1 else np.random.randint(0, 1000) print('Seed:', seed) sup_by_label = semisup.sample_by_label(train_images, train_labels, FLAGS.sup_per_class, NUM_LABELS, seed) graph = tf.Graph() with graph.as_default(): model_func = semisup.architectures.mnist_model if FLAGS.dropout_keep_prob < 1: model_func = semisup.architectures.mnist_model_dropout model = semisup.SemisupModel(model_func, NUM_LABELS, IMAGE_SHAPE, optimizer=optimizer, emb_size=FLAGS.emb_size, dropout_keep_prob=FLAGS.dropout_keep_prob) # Set up inputs. t_sup_images, t_sup_labels = semisup.create_per_class_inputs( sup_by_label, FLAGS.sup_per_batch) # Compute embeddings and logits. t_sup_emb = model.image_to_embedding(t_sup_images) t_sup_logit = model.embedding_to_logit(t_sup_emb) t_unsup_images = tf.placeholder("float", shape=[None] + IMAGE_SHAPE) proximity_weight = tf.placeholder("float", shape=[]) visit_weight = tf.placeholder("float", shape=[]) walker_weight = tf.placeholder("float", shape=[]) t_logit_weight = tf.placeholder("float", shape=[]) t_l1_weight = tf.placeholder("float", shape=[]) t_learning_rate = tf.placeholder("float", shape=[]) t_unsup_emb = model.image_to_embedding(t_unsup_images) model.add_semisup_loss( t_sup_emb, t_unsup_emb, t_sup_labels, walker_weight=walker_weight, visit_weight=visit_weight, proximity_weight=proximity_weight) model.add_logit_loss(t_sup_logit, t_sup_labels, weight=t_logit_weight) model.add_emb_regularization(t_sup_emb, weight=t_l1_weight) model.add_emb_regularization(t_unsup_emb, weight=t_l1_weight) train_op = model.create_train_op(t_learning_rate) summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.logdir, graph) saver = tf.train.Saver() sess = tf.InteractiveSession(graph=graph) unsup_images_iterator = semisup.create_input(train_images, train_labels, FLAGS.unsup_batch_size) tf.global_variables_initializer().run() coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) use_new_visit_loss = False for step in range(FLAGS.max_steps): unsup_images, _ = sess.run(unsup_images_iterator) if use_new_visit_loss: _, summaries, train_loss = sess.run([train_op, summary_op, model.train_loss], { t_unsup_images: unsup_images, walker_weight: FLAGS.walker_weight, proximity_weight: 0.3 + apply_envelope("lin", step, 0.7, FLAGS.warmup_steps, 0) - apply_envelope("lin", step, FLAGS.visit_weight, 2000, FLAGS.warmup_steps), t_logit_weight: FLAGS.logit_weight, t_l1_weight: FLAGS.l1_weight, visit_weight: apply_envelope("lin", step, FLAGS.visit_weight, 2000, FLAGS.warmup_steps), t_learning_rate: 5e-5 + apply_envelope("log", step, FLAGS.learning_rate, FLAGS.warmup_steps, 0) }) else: _, summaries, train_loss = sess.run([train_op, summary_op, model.train_loss], { t_unsup_images: unsup_images, walker_weight: FLAGS.walker_weight, visit_weight: 0.3 + apply_envelope("lin", step, 0.7, FLAGS.warmup_steps, 0), proximity_weight: 0, t_logit_weight: FLAGS.logit_weight, t_l1_weight: FLAGS.l1_weight, t_learning_rate: 5e-5 + apply_envelope("log", step, FLAGS.learning_rate, FLAGS.warmup_steps, 0) }) if (step + 1) % FLAGS.eval_interval == 0 or step == 99: print('Step: %d' % step) test_pred = model.classify(test_images).argmax(-1) conf_mtx = semisup.confusion_matrix(test_labels, test_pred, NUM_LABELS) test_err = (test_labels != test_pred).mean() * 100 print(conf_mtx) print('Test error: %.2f %%' % test_err) print('Train loss: %.2f ' % train_loss) print() test_summary = tf.Summary( value=[tf.Summary.Value( tag='Test Err', simple_value=test_err)]) summary_writer.add_summary(summaries, step) summary_writer.add_summary(test_summary, step)
def main(_): train_images, train_labels = mnist_tools.get_data('train') test_images, test_labels = mnist_tools.get_data('test') # Sample labeled training subset. seed = FLAGS.sup_seed if FLAGS.sup_seed != -1 else None sup_by_label = semisup.sample_by_label(train_images, train_labels, FLAGS.sup_per_class, NUM_LABELS, seed) graph = tf.Graph() with graph.as_default(): model = semisup.SemisupModel(semisup.architectures.mnist_model, NUM_LABELS, IMAGE_SHAPE) # Set up inputs. t_unsup_images, _ = semisup.create_input(train_images, train_labels, FLAGS.unsup_batch_size) t_sup_images, t_sup_labels = semisup.create_per_class_inputs( sup_by_label, FLAGS.sup_per_batch) # Compute embeddings and logits. t_sup_emb = model.image_to_embedding(t_sup_images) t_unsup_emb = model.image_to_embedding(t_unsup_images) t_sup_logit = model.embedding_to_logit(t_sup_emb) # Add losses. model.add_semisup_loss( t_sup_emb, t_unsup_emb, t_sup_labels, visit_weight=FLAGS.visit_weight) model.add_logit_loss(t_sup_logit, t_sup_labels) t_learning_rate = tf.train.exponential_decay( FLAGS.learning_rate, model.step, FLAGS.decay_steps, FLAGS.decay_factor, staircase=True) train_op = model.create_train_op(t_learning_rate) summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.logdir, graph) saver = tf.train.Saver() with tf.Session(graph=graph) as sess: tf.global_variables_initializer().run() coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) for step in xrange(FLAGS.max_steps): _, summaries = sess.run([train_op, summary_op]) if (step + 1) % FLAGS.eval_interval == 0 or step == 99: print('Step: %d' % step) test_pred = model.classify(test_images).argmax(-1) conf_mtx = semisup.confusion_matrix(test_labels, test_pred, NUM_LABELS) test_err = (test_labels != test_pred).mean() * 100 print(conf_mtx) print('Test error: %.2f %%' % test_err) print() test_summary = tf.Summary( value=[tf.Summary.Value( tag='Test Err', simple_value=test_err)]) summary_writer.add_summary(summaries, step) summary_writer.add_summary(test_summary, step) saver.save(sess, FLAGS.logdir, model.step) coord.request_stop() coord.join(threads)
def main(_): if FLAGS.logdir is not None: FLAGS.logdir = FLAGS.logdir + '/t_mnist_eval' try: shutil.rmtree(FLAGS.logdir) except OSError as e: print ("Error: %s - %s." % (e.filename, e.strerror)) train_images, train_labels = mnist_tools.get_data('train') test_images, test_labels = mnist_tools.get_data('test') # Sample labeled training subset. if FLAGS.sup_seed >= 0: seed = FLAGS.sup_seed elif FLAGS.sup_seed == -2: seed = FLAGS.sup_per_class else: seed = np.random.randint(0, 1000) print('Seed:', seed) sup_by_label = semisup.sample_by_label(train_images, train_labels, FLAGS.sup_per_class, NUM_LABELS, seed) graph = tf.Graph() with graph.as_default(): model = semisup.SemisupModel(semisup.architectures.mnist_model, NUM_LABELS, IMAGE_SHAPE, dropout_keep_prob=FLAGS.dropout_keep_prob) # Set up inputs. t_sup_images, t_sup_labels = semisup.create_per_class_inputs( sup_by_label, FLAGS.sup_per_batch) # Compute embeddings and logits. t_sup_emb = model.image_to_embedding(t_sup_images) t_sup_logit = model.embedding_to_logit(t_sup_emb) # Add losses. if FLAGS.semisup: t_unsup_images, _ = semisup.create_input(train_images, train_labels, FLAGS.unsup_batch_size) t_unsup_emb = model.image_to_embedding(t_unsup_images) model.add_semisup_loss( t_sup_emb, t_unsup_emb, t_sup_labels, walker_weight=FLAGS.walker_weight, visit_weight=FLAGS.visit_weight) #model.add_emb_regularization(t_unsup_emb, weight=FLAGS.l1_weight) model.add_logit_loss(t_sup_logit, t_sup_labels, weight=FLAGS.logit_weight) #model.add_emb_regularization(t_sup_emb, weight=FLAGS.l1_weight) t_learning_rate = tf.placeholder("float", shape=[]) train_op = model.create_train_op(t_learning_rate) summary_op = tf.summary.merge_all() if FLAGS.logdir is not None: summary_writer = tf.summary.FileWriter(FLAGS.logdir, graph) saver = tf.train.Saver() with tf.Session(graph=graph) as sess: tf.global_variables_initializer().run() coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) learning_rate_ = FLAGS.learning_rate for step in range(FLAGS.max_steps): lr = learning_rate_ if step < FLAGS.warmup_steps: lr = 1e-6 + semisup.apply_envelope("log", step, FLAGS.learning_rate, FLAGS.warmup_steps, 0) _, summaries = sess.run([train_op, summary_op], { t_learning_rate: lr }) sys.stderr.write("\rstep: %d" % step) sys.stdout.flush() if (step + 1) % FLAGS.eval_interval == 0 or step == 99: print('\nStep: %d' % step) test_pred = model.classify(test_images, sess).argmax(-1) conf_mtx = semisup.confusion_matrix(test_labels, test_pred, NUM_LABELS) test_err = (test_labels != test_pred).mean() * 100 print(conf_mtx) print('Test error: %.2f %%' % test_err) if FLAGS.logdir is not None: sum_values = { 'Test error': test_err } summary_writer.add_summary(summaries, step) for key, value in sum_values.items(): summary = tf.Summary( value=[tf.Summary.Value(tag=key, simple_value=value)]) summary_writer.add_summary(summary, step) if step % FLAGS.decay_steps == 0 and step > 0: learning_rate_ = learning_rate_ * FLAGS.decay_factor coord.request_stop() coord.join(threads) print('FINAL RESULTS:') print('Test error: %.2f %%' % (test_err)) print('final_score', 1 - test_err/100) print('@@test_error:%.4f' % (test_err/100)) print('@@train_loss:%.4f' % 0) print('@@reg_loss:%.4f' % 0) print('@@estimated_error:%.4f' % 0) print('@@centroid_norm:%.4f' % 0) print('@@emb_norm:%.4f' % 0) print('@@k_score:%.4f' % 0) print('@@svm_score:%.4f' % 0)
def main(_): train_images, train_labels = mnist_tools.get_data('train') test_images, test_labels = mnist_tools.get_data('test') # Sample labeled training subset. seed = FLAGS.sup_seed if FLAGS.sup_seed != -1 else None sup_by_label = semisup.sample_by_label(train_images, train_labels, FLAGS.sup_per_class, NUM_LABELS, seed) import numpy as np if 0: indices = [374, 2507, 9755, 12953, 16507, 16873, 23474, 23909, 30280, 35070, 49603, 50106, 51171, 51726, 51805, 55205, 57251, 57296, 57779, 59154] + \ [16644, 45576, 52886, 42140, 29201, 7767, 24, 134, 8464, 15022, 15715, 15602, 11030, 3898, 10195, 1454, 3290, 5293, 5806, 274] indices = [374, 2507, 9755, 12953, 16507, 16873, 23474, 23909, 30280, 35070, 49603, 50106, 51171, 51726, 51805, 55205, 57251, 57296, 57779, 59154] + \ [9924, 34058, 53476, 15715, 6428, 33598, 33464, 41753, 21250, 26389, 12950, 12464, 3795, 6761, 5638, 3952, 8300, 5632, 1475, 1875] sup_by_label = [ [] for i in range(NUM_LABELS)] for ind in indices: i = train_labels[ind] sup_by_label[i] = sup_by_label[i] + [train_images[ind]] for i in range(10): sup_by_label[i] = np.asarray(sup_by_label[i]) sup_by_label = np.asarray(sup_by_label) add_random_samples = 20 if add_random_samples > 0: rng = np.random.RandomState() indices = rng.choice(len(train_images), add_random_samples, False) for i in indices: l = train_labels[i] sup_by_label[l] = np.vstack([sup_by_label[l], [train_images[i]]]) graph = tf.Graph() with graph.as_default(): model = semisup.SemisupModel(semisup.architectures.mnist_model, NUM_LABELS, IMAGE_SHAPE) # Set up inputs. t_unsup_images, _ = semisup.create_input(train_images, train_labels, FLAGS.unsup_batch_size) t_sup_images, t_sup_labels = semisup.create_per_class_inputs( sup_by_label, FLAGS.sup_per_batch) # Compute embeddings and logits. t_sup_emb = model.image_to_embedding(t_sup_images) t_unsup_emb = model.image_to_embedding(t_unsup_images) t_sup_logit = model.embedding_to_logit(t_sup_emb) # Add losses. model.add_semisup_loss( t_sup_emb, t_unsup_emb, t_sup_labels, visit_weight=FLAGS.visit_weight) model.add_logit_loss(t_sup_logit, t_sup_labels) t_learning_rate = tf.train.exponential_decay( FLAGS.learning_rate, model.step, FLAGS.decay_steps, FLAGS.decay_factor, staircase=True) train_op = model.create_train_op(t_learning_rate) summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.logdir, graph) saver = tf.train.Saver() with tf.Session(graph=graph) as sess: tf.global_variables_initializer().run() coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) for step in range(FLAGS.max_steps): _, summaries = sess.run([train_op, summary_op]) if (step + 1) % FLAGS.eval_interval == 0 or step == 99: print('Step: %d' % step) test_pred = model.classify(test_images).argmax(-1) conf_mtx = semisup.confusion_matrix(test_labels, test_pred, NUM_LABELS) test_err = (test_labels != test_pred).mean() * 100 print(conf_mtx) print('Test error: %.2f %%' % test_err) print() test_summary = tf.Summary( value=[tf.Summary.Value( tag='Test Err', simple_value=test_err)]) summary_writer.add_summary(summaries, step) summary_writer.add_summary(test_summary, step) saver.save(sess, FLAGS.logdir, model.step) coord.request_stop() coord.join(threads)