Exemplo n.º 1
0
class FileReaderTest(TestCase):

    MORPH = file_reader.FileReader('/home/bingzhang/Documents/Dataset/MORPH/MORPH/','MORPH_Info.mat')
    # file_reader.__str__()
    print MORPH.__str__()
    data,_ =  MORPH.next_batch(20)
    sio.savemat('test.mat',{'data':data})
Exemplo n.º 2
0
class FileReaderTest(TestCase):

    CACD = file_reader.FileReader(
        '/home/bingzhang/Documents/Dataset/CACD/CACD2000/', 'cele.mat')
    # file_reader.__str__()
    cele1 = celebrity.Celebrity(CACD.age[0], CACD.identity[0],
                                str(CACD.path[0]))
    print cele1.__str__()
    print CACD.__str__()
    data = CACD.select_identity(2, 2)
    sio.savemat('test.mat', {'data': data})
Exemplo n.º 3
0
class FileReaderTest(TestCase):
    CACD = file_reader.FileReader(
        'C:/Users/BingZhang/Documents/MATLAB/CACD/CACD2000/',
        'cele.mat',
        contain_val=False,
        val_data_dir='/home/bingzhang/Documents/Dataset/lfw/',
        val_list='/home/bingzhang/Documents/Dataset/ZID/LFW/lfw_trip_val.txt')
    # file_reader.__str__()
    # cele1 = celebrity.Celebrity(CACD.age[0], CACD.identity[0], str(CACD.path[0]))
    data = CACD.get_next_batch(2)
    sio.savemat('./test.mat', {'data': data})
    print(CACD.__str__())
    _, label_data, image_path, ages = CACD.select_age(3, 20)
    print(ages)
    print(image_path)
    print(label_data)
Exemplo n.º 4
0
 def extract_feature(self):
     self.sess.run(tf.global_variables_initializer())
     step = 0
     saver = tf.train.Saver()
     saver.restore(self.sess, './DRModel-76000')
     feature = []
     idx = []
     CACD = fr.FileReader(self.paths.data_dir,
                          'celenew.mat',
                          contain_val=True,
                          val_data_dir=self.paths.val_dir,
                          val_list=self.paths.val_list)
     for i in xrange(0, CACD.total_images // self.batch_size):
         data, label = CACD.get_next_batch(self.batch_size)
         idx.append(label)
         feature_tmp = self.sess.run(self.id_embeddings,
                                     feed_dict={self.image_in: data})
         feature.append(feature_tmp)
         print('processing %d/%d batch', i,
               CACD.total_images // self.batch_size)
     sio.savemat('f.mat', {'feature': feature, 'label': idx})
Exemplo n.º 5
0
    def train(self):
        init_op = tf.global_variables_initializer()
        self.sess.run(init_op)
        step = 0
        saver = tf.train.Saver()
        # saver.restore(self.sess, self.model)
        # step = 120000
        # saver = tf.train.Saver()
        CACD = fr.FileReader(self.data_dir,
                             'cele.mat',
                             contain_val=True,
                             val_data_dir=self.val_dir,
                             val_list=self.val_list)
        triplet_select_times = 1
        writer_train = tf.summary.FileWriter(self.log_dir + '/train',
                                             self.sess.graph)
        writer_train_1 = tf.summary.FileWriter(
            self.log_dir + '/train/margin=0.5', self.sess.graph)
        writer_train_2 = tf.summary.FileWriter(
            self.log_dir + '/train/margin=1', self.sess.graph)
        writer_train_3 = tf.summary.FileWriter(
            self.log_dir + '/train/margin=1.3', self.sess.graph)
        writer_train_4 = tf.summary.FileWriter(
            self.log_dir + '/train/margin=1.6', self.sess.graph)
        sampled_freq = np.zeros([2000, 1])
        acc = 0
        with tf.name_scope('ToCheck'):
            tf.summary.image('affinity', self.affinity, 1)
            tf.summary.image('result', self.result)
        tf.summary.scalar('possible triplets', self.possible_triplets)
        tf.summary.image('sampled_freq', self.sampled_freq)

        summary_op = tf.summary.merge_all()
        val_summary_op = tf.summary.scalar('val_acc', self.val_acc)
        while triplet_select_times < 19999:
            # ID step
            print '!!!!!!!!!ID!!!!!!!!!!start forward propagation on a SAMPLE_BATCH (nof_sampled_id,nof_image_per_id)=(%d,%d)' % (
                self.nof_sampled_id, self.nof_images_per_id)
            time_start = time.time()
            image, label, image_path, sampled_id = CACD.select_identity(
                self.nof_sampled_id, self.nof_images_per_id)
            sampled_freq[sampled_id] += 1
            id_emb = self.sess.run(self.id_embeddings,
                                   feed_dict={
                                       self.image_in: image,
                                       self.label_in: label
                                   })
            aff = []
            for idx in range(len(label)):
                aff.append(np.sum(np.square(id_emb[idx][:] - id_emb), 1))
            result = get_rank_k(aff, self.nof_images_per_id)

            print 'Time Elapsed %lf' % (time.time() - time_start)

            time_start = time.time()
            print '[%d]selecting id triplets' % triplet_select_times
            triplet = triplet_sample(id_emb, self.nof_sampled_id,
                                     self.nof_images_per_id, self.delta)
            nof_triplet = len(triplet)

            print 'num of selected id triplets:%d' % nof_triplet
            print 'Time Elapsed:%lf' % (time.time() - time_start)
            inner_step = 0
            for i in xrange(0, nof_triplet, self.batch_size // 3):
                if i + self.batch_size // 3 < nof_triplet:
                    triplet_image, triplet_label = CACD.read_triplet(
                        image_path, label, triplet, i, self.batch_size // 3)
                    triplet_image = np.reshape(triplet_image,
                                               [-1, 250, 250, 3])
                    triplet_label = np.reshape(triplet_label, [-1])
                    start_time = time.time()
                    err, summary, _ = self.sess.run(
                        [self.id_loss, summary_op, self.id_opt],
                        feed_dict={
                            self.image_in:
                            triplet_image,
                            self.label_in:
                            triplet_label,
                            self.affinity:
                            np.reshape(np.array(aff), [
                                -1,
                                self.nof_images_per_id * self.nof_sampled_id,
                                self.nof_images_per_id * self.nof_sampled_id, 1
                            ]),
                            self.possible_triplets:
                            nof_triplet,
                            self.val_acc:
                            acc,
                            self.result:
                            np.reshape(result, [
                                -1,
                                self.nof_images_per_id * self.nof_sampled_id,
                                self.nof_images_per_id * self.nof_sampled_id, 1
                            ]),
                            self.sampled_freq:
                            np.reshape(sampled_freq, [1, 50, 40, 1])
                        })
                    print '[%d/%d@%dth select_triplet & global_step %d] \033[1;31;40m loss:[%lf] \033[1;m time elapsed:%lf' % (
                        inner_step, (nof_triplet * 3) // self.batch_size,
                        triplet_select_times, step, err,
                        time.time() - start_time)
                    writer_train.add_summary(summary, step)
                    step += 1
                    inner_step += 1
                    if inner_step % 5 == 0:
                        emb = self.sess.run(self.id_embeddings,
                                            feed_dict={
                                                self.image_in: image,
                                                self.label_in: label
                                            })
                        aff = []
                        for idx in range(len(label)):
                            aff.append(np.sum(np.square(emb[idx][:] - emb), 1))
                        result = get_rank_k(aff, self.nof_images_per_id)
                    if step % 200 == 0:
                        # perform validate
                        val_iters = CACD.val_size // 20
                        true_label = []
                        emb = []
                        for _ in range(val_iters):
                            validate_data, validate_label = CACD.get_test(20)
                            validate_data = np.reshape(validate_data,
                                                       [-1, 250, 250, 3])
                            true_label.append(validate_label)
                            emb_bacth = self.sess.run(
                                self.id_embeddings,
                                feed_dict={self.image_in: validate_data})
                            emb.append(emb_bacth)
                        true_label = np.reshape(true_label, (-1, ))
                        emb = np.reshape(emb, (-1, 128))
                        pre_label = []
                        for j in range(CACD.val_size):
                            if np.sum(np.square(emb[j * 2] -
                                                emb[j * 2 + 1])) < 0.5:
                                pre_label.append(1)
                            else:
                                pre_label.append(0)
                        correct = np.sum(
                            abs(np.array(pre_label) - np.array(true_label)))
                        acc = 1 - float(correct) / CACD.val_size
                        sum = self.sess.run(val_summary_op,
                                            feed_dict={self.val_acc: acc})
                        writer_train_1.add_summary(sum, step)

                        pre_label = []
                        for j in range(CACD.val_size):
                            if np.sum(np.square(emb[j * 2] -
                                                emb[j * 2 + 1])) < 1:
                                pre_label.append(1)
                            else:
                                pre_label.append(0)
                        correct = np.sum(
                            abs(np.array(pre_label) - np.array(true_label)))
                        acc = 1 - float(correct) / CACD.val_size
                        sum = self.sess.run(val_summary_op,
                                            feed_dict={self.val_acc: acc})
                        writer_train_2.add_summary(sum, step)

                        pre_label = []
                        for j in range(CACD.val_size):
                            if np.sum(np.square(emb[j * 2] -
                                                emb[j * 2 + 1])) < 1.3:
                                pre_label.append(1)
                            else:
                                pre_label.append(0)
                        correct = np.sum(
                            abs(np.array(pre_label) - np.array(true_label)))
                        acc = 1 - float(correct) / CACD.val_size
                        sum = self.sess.run(val_summary_op,
                                            feed_dict={self.val_acc: acc})
                        writer_train_3.add_summary(sum, step)

                        pre_label = []
                        for j in range(CACD.val_size):
                            if np.sum(np.square(emb[j * 2] -
                                                emb[j * 2 + 1])) < 1.6:
                                pre_label.append(1)
                            else:
                                pre_label.append(0)
                        correct = np.sum(
                            abs(np.array(pre_label) - np.array(true_label)))
                        acc = 1 - float(correct) / CACD.val_size
                        sum = self.sess.run(val_summary_op,
                                            feed_dict={self.val_acc: acc})
                        writer_train_4.add_summary(sum, step)
                    if step % 10000 == 0:
                        saver.save(self.sess, 'QModel', step)

            # AGE step
            print '!!!!!!!!!AGE!!!!!!!!!!start forward propagation on a SAMPLE_BATCH (nof_sampled_age,nof_image_per_age)=(%d,%d)' % (
                self.nof_sampled_age, self.nof_images_per_age)
            time_start = time.time()
            image, label, image_path, sampled_id = CACD.select_age(
                self.nof_sampled_age, self.nof_images_per_age)

            age_emb = self.sess.run(self.age_embeddings,
                                    feed_dict={
                                        self.image_in: image,
                                        self.label_in: label
                                    })

            print 'Time Elapsed %lf' % (time.time() - time_start)

            time_start = time.time()
            print '[%d]selecting age triplets' % triplet_select_times
            triplet = triplet_sample(age_emb, self.nof_sampled_age,
                                     self.nof_images_per_age, self.delta)
            nof_triplet = len(triplet)

            print 'num of selected age triplets:%d' % nof_triplet
            print 'Time Elapsed:%lf' % (time.time() - time_start)
            inner_step = 0
            for i in xrange(0, nof_triplet, self.batch_size // 3):
                if i + self.batch_size // 3 < nof_triplet:
                    triplet_image, triplet_label = CACD.read_triplet(
                        image_path, label, triplet, i, self.batch_size // 3)
                    triplet_image = np.reshape(triplet_image,
                                               [-1, 250, 250, 3])
                    triplet_label = np.reshape(triplet_label, [-1])
                    start_time = time.time()
                    err, summary, _ = self.sess.run(
                        [self.age_loss, summary_op, self.age_opt],
                        feed_dict={
                            self.image_in:
                            triplet_image,
                            self.label_in:
                            triplet_label,
                            self.affinity:
                            np.reshape(np.array(aff), [
                                -1,
                                self.nof_images_per_id * self.nof_sampled_id,
                                self.nof_images_per_id * self.nof_sampled_id, 1
                            ]),
                            self.possible_triplets:
                            nof_triplet,
                            self.val_acc:
                            acc,
                            self.result:
                            np.reshape(result, [
                                -1,
                                self.nof_images_per_id * self.nof_sampled_id,
                                self.nof_images_per_id * self.nof_sampled_id, 1
                            ]),
                            self.sampled_freq:
                            np.reshape(sampled_freq, [1, 50, 40, 1])
                        })
                    print '[%d/%d@%dth select_triplet & global_step %d] \033[1;31;40m loss:[%lf] \033[1;m time elapsed:%lf' % (
                        inner_step, (nof_triplet * 3) // self.batch_size,
                        triplet_select_times, step, err,
                        time.time() - start_time)
                    inner_step += 1
                    writer_train.add_summary(summary, step)
                    step += 1
                    if step % 10000 == 0:
                        saver.save(self.sess, 'QModel', step)
            triplet_select_times += 1
Exemplo n.º 6
0
    def train(self):
        self.sess.run(tf.global_variables_initializer())
        step = 0
        saver = tf.train.Saver()
        writer_train = tf.summary.FileWriter(self.paths.log_dir,
                                             self.sess.graph)
        CACD = fr.FileReader(self.paths.data_dir,
                             'cele.mat',
                             contain_val=True,
                             val_data_dir=self.paths.val_dir,
                             val_list=self.paths.val_list)
        fp = open('dis.txt', 'a')
        with tf.name_scope('ToCheck'):
            tf.summary.image('affinity', self.affinity, 1)
            tf.summary.image('result', self.result)
            tf.summary.image('dis', self.dis_check, 1)
        tf.summary.scalar('id_loss', self.id_loss)
        tf.summary.scalar('possile_triples', self.possible_triplets)
        dis = np.zeros(200)
        summary_op = tf.summary.merge_all()
        triplet_select_times = 1
        while triplet_select_times < 19999:
            # ID step
            print(
                '!!!!!!!!!ID!!!!!!!!!!start forward propagation on a SAMPLE_BATCH (nof_sampled_id,nof_image_per_id)=(%d,%d)'
                % (self.nof_sampled_id, self.nof_images_per_id))
            time_start = time.time()
            image, label, image_path, sampled_id = CACD.select_identity(
                self.nof_sampled_id, self.nof_images_per_id)
            id_emb = self.sess.run(self.id_embeddings,
                                   feed_dict={
                                       self.image_in: image,
                                       self.label_in: label
                                   })
            aff = []
            for idx in range(len(label)):
                aff.append(np.sum(np.square(id_emb[idx][:] - id_emb), 1))
            result = get_rank_k(aff, self.nof_images_per_id)
            print('Time Elapsed %lf' % (time.time() - time_start))
            time_start = time.time()
            print('[%d]selecting id triplets' % triplet_select_times)
            triplet = triplet_sample(id_emb, self.nof_sampled_id,
                                     self.nof_images_per_id, self.delta)
            nof_triplet = len(triplet)

            print('num of selected id triplets:%d' % nof_triplet)
            print('Time Elapsed:%lf' % (time.time() - time_start))
            inner_step = 0
            for i in xrange(0, nof_triplet, self.batch_size // 3):
                if i + self.batch_size // 3 < nof_triplet:
                    triplet_image, triplet_label = CACD.read_triplet(
                        image_path, label, triplet, i, self.batch_size // 3)
                    triplet_image = np.reshape(triplet_image,
                                               [-1, 250, 250, 3])
                    triplet_label = np.reshape(triplet_label, [-1])
                    start_time = time.time()
                    err, summary, _ = self.sess.run(
                        [self.id_loss, summary_op, self.id_opt],
                        feed_dict={
                            self.image_in:
                            triplet_image,
                            self.label_in:
                            triplet_label,
                            self.dis_check:
                            np.reshape(np.array(dis), [-1, 10, 20, 1]),
                            self.possible_triplets:
                            nof_triplet,
                            self.affinity:
                            np.reshape(np.array(aff), [
                                -1,
                                self.nof_images_per_id * self.nof_sampled_id,
                                self.nof_images_per_id * self.nof_sampled_id, 1
                            ]),
                            self.result:
                            np.reshape(result, [
                                -1,
                                self.nof_images_per_id * self.nof_sampled_id,
                                self.nof_images_per_id * self.nof_sampled_id, 1
                            ]),
                        })
                    print(
                        '[%d/%d@%dth select_triplet & global_step %d] \033[1;31;40m loss:[%lf] \033[1;m time elapsed:%lf'
                        % (inner_step, (nof_triplet * 3) // self.batch_size,
                           triplet_select_times, step, err,
                           time.time() - start_time))
                    writer_train.add_summary(summary, step)
                    step += 1
                    inner_step += 1

                    if inner_step % 5 == 0:
                        emb = self.sess.run(self.id_embeddings,
                                            feed_dict={
                                                self.image_in: image,
                                                self.label_in: label
                                            })
                        aff = []
                        for idx in range(len(label)):
                            aff.append(np.sum(np.square(emb[idx][:] - emb), 1))
                        result = get_rank_k(aff, self.nof_images_per_id)

                    if step % 200 == 0:
                        val_iters = CACD.val_size // 20
                        ground_truth = []
                        emb = []
                        # extract embeddings by batch as GPU memory is not enough
                        for _ in range(val_iters):
                            val_data, val_label = CACD.get_test(20)
                            val_data = np.reshape(val_data, [-1, 250, 250, 3])
                            ground_truth.append(val_label)
                            emb_batch = self.sess.run(
                                self.id_embeddings,
                                feed_dict={self.image_in: val_data})
                            emb.append(emb_batch)
                        ground_truth = np.reshape(ground_truth, (-1, ))
                        emb = np.reshape(emb, (-1, 128))
                        for j in range(CACD.val_size):
                            dis[j] = np.sum(
                                np.square(emb[j * 2] - emb[j * 2 + 1]))
                        for item in dis:
                            fp.write(str(item) + ' ')
                            print(item)
                        fp.write('\n')
                    if step % 2000 == 0:
                        saver.save(self.sess, 'DRModel', step)
            triplet_select_times += 1
Exemplo n.º 7
0
    print("Please provide a valid input.")
    sys.exit()

output_path = '{}_graphs'.format(person_name)

############################################################
#                                                          #
#                     INTERNAL FUNCTIONS                   #
#                                                          #
############################################################

# setting figure up
figure = plt.gcf()  # get current figure
figure.set_size_inches(16, 9)

read_filesVar = file_reader.FileReader(person_name)

cvmList = read_filesVar.get_cvms()
repList = read_filesVar.get_rms()


print("Creating output directory")
print("Processing data")
os.makedirs(output_path, exist_ok=True)

# ############################################################
# #                                                          #
# #                       CALCULATING RMS                    #
# #                                                          #
# ############################################################