Пример #1
0
def predictor2(image_tru, num_face):

    # image = align_face(image_tru)
    image = image_tru

    ######**********************************************************#####
    # Encode
    data_file = 'tmp_aug.tfrecords'
    writer = tf.python_io.TFRecordWriter(data_file)
    # image = load_image('./')
    gender = np.array([0])
    race = np.array([0])
    age = np.array([0])
    addrs = ['0']
    feature = {
        'val/gender': _int64_feature(gender.astype(np.int8)),
        'val/race': _int64_feature(race.astype(np.int8)),
        'val/age': _int64_feature(age.astype(np.int8)),
        'val/image': _bytes_feature(tf.compat.as_bytes(image.tostring())),
        'val/address': _bytes_feature(os.path.basename(addrs[0].encode()))
    }
    example = tf.train.Example(features=tf.train.Features(feature=feature))
    writer.write(example.SerializeToString())
    writer.close()
    sys.stdout.flush()
    # decode
    tf.reset_default_graph()
    filename_queue = tf.train.string_input_producer([data_file])
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)

    features = tf.parse_single_example(serialized_example,
                                       features={
                                           'val/gender':
                                           tf.FixedLenFeature([], tf.int64),
                                           'val/race':
                                           tf.FixedLenFeature([], tf.int64),
                                           'val/age':
                                           tf.FixedLenFeature([], tf.int64),
                                           'val/image':
                                           tf.FixedLenFeature([], tf.string),
                                           'val/address':
                                           tf.FixedLenFeature([], tf.string)
                                       })

    image = tf.decode_raw(features['val/image'], tf.float32)
    image = tf.cast(image, tf.uint8)
    image.set_shape([200 * 200 * 3])
    image = tf.reshape(image, [200, 200, 3])
    # image = tf.reverse_v2(image, [-1])
    image = tf.image.per_image_standardization(image)

    images = tf.train.shuffle_batch([image],
                                    batch_size=1,
                                    capacity=256,
                                    num_threads=2,
                                    min_after_dequeue=32)

    train_mode = tf.placeholder(tf.bool)
    logits_gender, logits_race, logits_age, end_points, _ = build_model(
        images, train_mode)

    end_points['Predictions/gender'] = tf.nn.softmax(logits_gender,
                                                     name='Predictions/gender')
    end_points['Predictions/race'] = tf.nn.softmax(logits_race,
                                                   name='Predictions/race')
    end_points['Predictions/age'] = tf.nn.softmax(logits_age,
                                                  name='Predictions/age')
    predictions1 = tf.argmax(end_points['Predictions/gender'], -1)
    predictions2 = tf.argmax(end_points['Predictions/race'], -1)
    predictions3 = tf.argmax(end_points['Predictions/age'], -1)

    pr1 = tf.to_float(tf.to_int32(predictions1))
    pr2 = tf.to_float(tf.to_int32(predictions2))
    pr3 = tf.to_float(tf.to_int32(predictions3))

    with tf.Session() as sess:
        #
        # init_op = tf.group(tf.local_variables_initializer())
        # sess.run(init_op)

        saver = tf.train.Saver(max_to_keep=100)
        ckpt = tf.train.get_checkpoint_state(model_dir)

        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            print("restore and start evaluation!")

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

        pred1, pred2 = [], []

        # for step in range(num_steps_per_epoch):

        acc1, acc2, acc3 = sess.run([pr1, pr2, pr3], {train_mode: False})

        print(acc1, acc2, acc3)

        # Log some information
        # logging.info('Step %s: gender Accuracy: %.4f race Accuracy: %.4f loss: %.4f  (%.2f sec/step)'step, acc1, acc2, l, time_elapsed)

        # writer.add_summary(curr_summary, step)

        # pred1.append(acc1)
        # pred2.append(acc2)

        # coord.request_stop()
        # coord.join(threads)

        # saver.save(sess, final_checkpoint_file)
        sess.close()

        # average_acc1 = np.mean(accuracies1)
        # average_acc2 = np.mean(accuracies2)
        # average_loss = np.mean(loss_list)

        gender = int(acc1)
        race = int(acc2)
        age = int(acc3)
        global Result
        Result = [gender, race, age, num_face]
Пример #2
0
def predictor(image_tru):
    if len(sys.argv) != 1:
        print('python predictor.py file_path_to_image')

    else:
        #image_path = sys.argv[1]
        #image_path = '/home/pagand/PycharmProjects/RaceRec/crop_part1/19_1_4_20170103233712235.jpg.chip.jpg'

        # image_path = project_dir + '23_1_0_20170103180703224.jpg'
        # image_path = project_dir + '111.jpg'
        # image_path = project_dir + '03.jpg'
        #image_path = project_dir + 'pedram.jpg'
        # image = img
        #image = load_image(image_path)
        # image = align_face(image_tru)
        image = image_tru

        ######**********************************************************#####
        #Encode
        data_file = 'tmp_aug.tfrecords'
        writer = tf.python_io.TFRecordWriter(data_file)
        # image = load_image('./')
        gender = np.array([0])
        race = np.array([0])
        addrs = ['0']
        feature = {
            'val/gender': _int64_feature(gender.astype(np.int8)),
            'val/race': _int64_feature(race.astype(np.int8)),
            'val/image': _bytes_feature(tf.compat.as_bytes(image.tostring())),
            'val/address': _bytes_feature(os.path.basename(addrs[0].encode()))
        }
        example = tf.train.Example(features=tf.train.Features(feature=feature))
        writer.write(example.SerializeToString())
        writer.close()
        sys.stdout.flush()
        # decode
        tf.reset_default_graph()
        filename_queue = tf.train.string_input_producer([data_file])
        reader = tf.TFRecordReader()
        _, serialized_example = reader.read(filename_queue)

        features = tf.parse_single_example(serialized_example,
                                           features={
                                               'val/gender':
                                               tf.FixedLenFeature([],
                                                                  tf.int64),
                                               'val/race':
                                               tf.FixedLenFeature([],
                                                                  tf.int64),
                                               'val/image':
                                               tf.FixedLenFeature([],
                                                                  tf.string),
                                               'val/address':
                                               tf.FixedLenFeature([],
                                                                  tf.string)
                                           })
        # features = tf.parse_single_example(
        #     example,
        #     features={'val/gender': tf.FixedLenFeature([], tf.int64),
        #               'val/race': tf.FixedLenFeature([], tf.int64),
        #               'val/image': tf.FixedLenFeature([], tf.string),
        #               'val/address': tf.FixedLenFeature([], tf.string)})
        image = tf.decode_raw(features['val/image'], tf.float32)
        image = tf.cast(image, tf.uint8)
        image.set_shape([200 * 200 * 3])
        image = tf.reshape(image, [200, 200, 3])
        # image = tf.reverse_v2(image, [-1])
        image = tf.image.per_image_standardization(image)

        images = tf.train.shuffle_batch([image],
                                        batch_size=1,
                                        capacity=256,
                                        num_threads=2,
                                        min_after_dequeue=32)

        train_mode = tf.placeholder(tf.bool)
        logits_gender, logits_race, end_points, _ = build_model(
            images, train_mode)

        end_points['Predictions/gender'] = tf.nn.softmax(
            logits_gender, name='Predictions/gender')
        end_points['Predictions/race'] = tf.nn.softmax(logits_race,
                                                       name='Predictions/race')
        predictions1 = tf.argmax(end_points['Predictions/gender'], -1)
        predictions2 = tf.argmax(end_points['Predictions/race'], -1)

        pr1 = tf.to_float(tf.to_int32(predictions1))
        pr2 = tf.to_float(tf.to_int32(predictions2))

        with tf.Session() as sess:
            #
            # init_op = tf.group(tf.local_variables_initializer())
            # sess.run(init_op)

            saver = tf.train.Saver(max_to_keep=100)
            ckpt = tf.train.get_checkpoint_state(model_dir)

            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
                print("restore and start evaluation!")

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

            pred1, pred2 = [], []

            #for step in range(num_steps_per_epoch):

            acc1, acc2 = sess.run([pr1, pr2], {train_mode: False})

            # Log some information
            #logging.info('Step %s: gender Accuracy: %.4f race Accuracy: %.4f loss: %.4f  (%.2f sec/step)'step, acc1, acc2, l, time_elapsed)

            #writer.add_summary(curr_summary, step)

            # pred1.append(acc1)
            # pred2.append(acc2)

            # coord.request_stop()
            # coord.join(threads)

            # saver.save(sess, final_checkpoint_file)
            sess.close()

            #average_acc1 = np.mean(accuracies1)
            #average_acc2 = np.mean(accuracies2)
            #average_loss = np.mean(loss_list)

            # logging.info('Average gender Accuracy: %s', average_acc1)
            # print('Average gender accuracy: ', int(np.mean(acc1)))
            # print('Average gender accuracy: ', int(np.mean(acc2)))
            gender = int(np.mean(acc1))
            race = int(np.mean(acc1))
            # logging.info('Average race Accuracy: %s', average_acc2)
            #logging.info('Average loss: %s', average_loss)

        #######***************************************************8######
        # pred_gender, pred_race = run(image)
        # pred_gender, pred_race = run(image)
        #print(pred_gender, pred_race)
        # print('You are a ', PGender[pred_gender[0]], ' and your race is ', PRase[pred_race[0]])
        # draw_label(image, )
        return gender, race