Beispiel #1
0
def train(logdir1='logdir/default/train1', logdir2='logdir/default/train2', queue=True):
    model = Model(mode="train2", batch_size=hp.Train2.batch_size, queue=queue)

    # Loss
    loss_op = model.loss_net2()

    # Training Scheme
    global_step = tf.Variable(0, name='global_step', trainable=False)

    optimizer = tf.train.AdamOptimizer(learning_rate=hp.Train2.lr)
    with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
        var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'net/net2')
        train_op = optimizer.minimize(loss_op, global_step=global_step, var_list=var_list)

    # Summary
    summ_op = summaries(loss_op)

    session_conf = tf.ConfigProto(
        gpu_options=tf.GPUOptions(
            allow_growth=True,
            per_process_gpu_memory_fraction=0.6,
        ),
    )
    # Training
    with tf.Session(config=session_conf) as sess:
        # Load trained model
        sess.run(tf.global_variables_initializer())
        model.load(sess, mode='train2', logdir=logdir1, logdir2=logdir2)

        writer = tf.summary.FileWriter(logdir2, sess.graph)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        for epoch in range(1, hp.Train2.num_epochs + 1):
            for step in tqdm(range(model.num_batch), total=model.num_batch, ncols=70, leave=False, unit='b'):
                if queue:
                    sess.run(train_op)
                else:
                    mfcc, spec, mel = get_batch(model.mode, model.batch_size)
                    sess.run(train_op, feed_dict={model.x_mfcc: mfcc, model.y_spec: spec, model.y_mel: mel})

            # Write checkpoint files at every epoch
            summ, gs = sess.run([summ_op, global_step])

            if epoch % hp.Train2.save_per_epoch == 0:
                tf.train.Saver().save(sess, '{}/epoch_{}_step_{}'.format(logdir2, epoch, gs))

                # Eval at every n epochs
                with tf.Graph().as_default():
                    eval2.eval(logdir2, queue=False)

                # Convert at every n epochs
                with tf.Graph().as_default():
                    convert.convert(logdir2, queue=False)

            writer.add_summary(summ, global_step=gs)

        writer.close()
        coord.request_stop()
        coord.join(threads)
Beispiel #2
0
def train(logdir1='logdir/default/train1',
          logdir2='logdir/default/train2',
          queue=True):
    model = Model(mode="train2", batch_size=hp.Train2.batch_size, queue=queue)

    # Loss
    loss_op = model.loss_net2()

    # Training Scheme
    global_step = tf.Variable(0, name='global_step', trainable=False)

    optimizer = tf.train.AdamOptimizer(learning_rate=hp.Train2.lr)
    with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
        var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                     'net/net2')
        train_op = optimizer.minimize(loss_op,
                                      global_step=global_step,
                                      var_list=var_list)

    # Summary
    summ_op = summaries(loss_op)

    session_conf = tf.ConfigProto(gpu_options=tf.GPUOptions(
        allow_growth=True,
        per_process_gpu_memory_fraction=0.6,
    ), )
    # Training
    with tf.Session(config=session_conf) as sess:
        # Load trained model
        sess.run(tf.global_variables_initializer())
        model.load(sess, mode='train2', logdir=logdir1, logdir2=logdir2)

        writer = tf.summary.FileWriter(logdir2, sess.graph)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        for epoch in range(1, hp.Train2.num_epochs + 1):
            for step in tqdm(range(model.num_batch),
                             total=model.num_batch,
                             ncols=70,
                             leave=False,
                             unit='b'):
                if queue:
                    sess.run(train_op)
                else:
                    mfcc, spec, mel = get_batch(model.mode, model.batch_size)
                    sess.run(train_op,
                             feed_dict={
                                 model.x_mfcc: mfcc,
                                 model.y_spec: spec,
                                 model.y_mel: mel
                             })

            # Write checkpoint files at every epoch
            summ, gs = sess.run([summ_op, global_step])

            if epoch % hp.Train2.save_per_epoch == 0:
                tf.train.Saver().save(
                    sess, '{}/epoch_{}_step_{}'.format(logdir2, epoch, gs))

                # Eval at every n epochs
                with tf.Graph().as_default():
                    eval2.eval(logdir2, queue=False)

                # Convert at every n epochs
                with tf.Graph().as_default():
                    convert.convert(logdir2, queue=False)

            writer.add_summary(summ, global_step=gs)

        writer.close()
        coord.request_stop()
        coord.join(threads)
Beispiel #3
0
def train(self):
    nrof_samples_per_class = 100
    nrof_train_per_class = int(round(FLAGS.train_test_split * 100))
    all_samples = tf.placeholder(tf.float32, shape=[None, NUM_FEATURES])

    sigmoid_activations = main_network(all_samples)
    embeddings = tf.nn.l2_normalize(sigmoid_activations, 1, 1e-10, name='embeddings')

    # HPSH methods
    anchors, positives, negatives= mySamples(embeddings, FLAGS.HASH_BITS)
    loss = lossFun(sigmoid_activations, anchors, positives, negatives) #HASH LIKE

    regularization_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
    total_loss = tf.add_n([loss] + regularization_loss, name='total_loss')
    ssx=lib.params_with_name('Classifier')
    train_op1 = tf.train.AdamOptimizer(learning_rate=1e-4, beta1=0.5, beta2=0.9)
    train_op=train_op1.minimize(loss, var_list=ssx)
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as session:
        session.run(tf.global_variables_initializer())
        max_map=0.0
        max_epoch=0
        count=0
        plt.ion()
        for epoch in range(FLAGS.ITERS):
            print('epoch:', epoch)
            path_list = []
            for i in range(2):
                for j in range(50):
                    triplets = get_triplets(train_data, train_labels, CLASSES)
                    count=count+1
                    iters=count
                    _, cost, flat_representation = session.run([train_op, total_loss,embeddings], feed_dict={all_samples: triplets})
            if epoch<30:
               continue
            with open('features_UCMD.pkl', 'rb') as f:
                features = pickle.load(f)

            test_images = np.empty((0, IMG_SIZE, IMG_SIZE, NUM_CHANNELS))
            test_embeddings = np.empty((0, FLAGS.HASH_BITS)).astype(np.int8)
            test_single_labels = np.empty((0,))

            start_idx = 0
            for idx_class in range(CLASSES):
                end_idx = (idx_class + 1) * nrof_samples_per_class
                class_features = features[start_idx:end_idx]

                test_class_features = class_features[nrof_train_per_class:]

                for idx in range(len(test_class_features)):
                    test_input = test_class_features[idx][0]
                    test_single_label = test_class_features[idx][1]
                    test_img_path = test_class_features[idx][2]

                    # Store the image
                    img = Image.open(test_img_path)
                    img = img.resize([IMG_SIZE, IMG_SIZE], Image.ANTIALIAS)
                    img.save(os.path.join('dump_dir', 'temp.jpg'))

                    read_image_path = os.path.join('dump_dir', 'temp.jpg')
                    path_list.append(test_img_path)
                    img = np.array(Image.open(read_image_path), dtype=int) / 256.
                    img = np.reshape(img, [-1, IMG_SIZE, IMG_SIZE, NUM_CHANNELS])
                    test_images = np.append(test_images, img, axis=0)

                    # Store the embedding
                    test_embedding0 = session.run(sigmoid_activations, feed_dict={all_samples: np.reshape(test_input, newshape=(-1, NUM_FEATURES))})
                    test_embedding = ((np.sign(test_embedding0 - 0.5) + 1) / 2)
                    #bellow one is using to store hash-codes
                    test_embeddings = np.append(test_embeddings, test_embedding, axis=0)

                    # Store the label
                    test_single_labels = np.append(test_single_labels, [test_single_label, ], axis=0)
                start_idx = end_idx

            np.save('data/test_labels.npy', test_single_labels)
            np.save('data/test_embeddings.npy', test_embeddings)
            np.save('data/test_images.npy', test_images)

            map = eval2.eval(FLAGS.HASH_BITS)
            if map > max_map:
                max_map = map
                max_epoch = epoch

                # max_epoch = epoch
        print("max_map:", max_map)
        print("max_epoch", max_epoch)
        print('Optimization finished!')
Beispiel #4
0
def train(self):
    all_samples = tf.placeholder(tf.float32, shape=[None, NUM_FEATURES])
    sigmoid_activations = main_network(all_samples)
    embeddings = tf.nn.l2_normalize(sigmoid_activations, 1, 1e-10, name='embeddings')

    # Hard negative samples
    anchors, positives, negatives= mySamples3(embeddings, FLAGS.HASH_BITS)

    #LOSS FUNCTION
    loss = triplet_loss4(sigmoid_activations, anchors, positives, negatives) #MARGIN LOSS
    regularization_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
    total_loss = tf.add_n([loss] + regularization_loss, name='total_loss')
    ssx=lib.params_with_name('Classifier')
    train_op1 = tf.train.AdamOptimizer(learning_rate=1e-4, beta1=0.5, beta2=0.9)
    train_op=train_op1.minimize(loss, var_list=ssx)
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as session:
        session.run(tf.global_variables_initializer())
        max_map=0.0
        max_epoch=0.
        count=0
        for epoch in range(FLAGS.ITERS):
            print('epoch:', epoch)
            path_list = []
            for i in range(30):
                for j in range(30):
                    triplets = get_triplets2(train_data, train_labels, CLASSES)
                    count=count+1
                    iters=count
                    _, cost = session.run([train_op, total_loss], feed_dict={all_samples: triplets})

                    if (iters < 10) or (iters % 100 == 99):
                        lib.plot.flush()

                    lib.plot.plot('Loss', cost)

                    lib.plot.tick()

            if epoch<35:
                continue
            with open('features_AID.pkl', 'rb') as f:
                features = pickle.load(f)

            test_images = np.empty((0, IMG_SIZE, IMG_SIZE, NUM_CHANNELS))
            test_embeddings = np.empty((0, FLAGS.HASH_BITS)).astype(np.int8)
            test_single_labels = np.empty((0,))

            features_ar = np.empty((0, NUM_FEATURES))
            labels_ar = np.empty((0,))
            paths = []

            if not os.path.exists('dump_dir/'):
                os.mkdir('dump_dir/')

            for idx in range(len(features)):
                feature = features[idx][0]
                label = features[idx][1]
                path = features[idx][2]

                features_ar = np.append(features_ar, np.reshape(feature, newshape=(-1, NUM_FEATURES)), axis=0)
                labels_ar = np.append(labels_ar, [label, ], axis=0)
                paths.append(path)

            start_idx = 0
            for idx_class in range(CLASSES):
                class_label_idxs = np.where(labels_ar == idx_class)[0]
                nrof_samples_in_class = len(class_label_idxs)
                nrof_train_samples = int(np.ceil(FLAGS.train_test_split * nrof_samples_in_class))
                nrof_test_samples = nrof_samples_in_class - nrof_train_samples
                end_idx = start_idx + nrof_samples_in_class

                # test data
                class_features_test = features_ar[class_label_idxs[nrof_train_samples:], :]
                class_labels_test = labels_ar[class_label_idxs[nrof_train_samples:]]
                class_paths_test = []
                for _, idx in enumerate(class_label_idxs[nrof_train_samples:]):
                    class_paths_test.append(paths[idx])

                for i in range(nrof_test_samples):
                    test_input = class_features_test[i,:]
                    test_single_label = class_labels_test[i]
                    test_img_path = class_paths_test[i]

                    # Store the embedding
                    test_embedding0 = session.run(sigmoid_activations, feed_dict={all_samples: np.reshape(test_input, newshape=(-1, NUM_FEATURES))})
                    test_embedding = ((np.sign(test_embedding0 - 0.5) + 1) / 2)
                    test_embeddings = np.append(test_embeddings, test_embedding.astype(np.int8), axis=0)

                    # Store the label
                    test_single_labels = np.append(test_single_labels, [test_single_label, ], axis=0)

                    # Store the image
                    img = Image.open(test_img_path)
                    path_list.append(test_img_path)
                    img = img.resize([IMG_SIZE, IMG_SIZE], Image.ANTIALIAS)
                    img.save(os.path.join('dump_dir', 'temp.jpg'))

                    read_image_path = os.path.join('dump_dir', 'temp.jpg')
                    img = np.array(Image.open(read_image_path), dtype=int) / 256.
                    img = np.reshape(img, [-1, IMG_SIZE, IMG_SIZE, NUM_CHANNELS])
                    test_images = np.append(test_images, img, axis=0)

                start_idx = end_idx

            out_dir = 'data/'
            if not os.path.exists(out_dir):
                os.mkdir(out_dir)

            np.save('data/test_labels.npy', test_single_labels)
            np.save('data/test_embeddings.npy', test_embeddings)
            np.save('data/test_images.npy', test_images)
            map = eval2.eval(FLAGS.HASH_BITS)
            if map > max_map:
                max_map = map
                max_epoch = epoch
        print("max_map:", max_map)
        print("max_epoch", max_epoch)
        print('Optimization finished!')