def main(args):
    np.random.seed(seed=args.seed)
    random.seed(args.seed)
    train_set = facenet.get_dataset(args.data_dir)
    result_filename = os.path.join(os.path.expanduser(args.data_dir), 'statistics.txt')
    with tf.Graph().as_default():
        tf.set_random_seed(args.seed)
        (image_list, _) = facenet.get_image_paths_and_labels(train_set)
        nrof_images = len(image_list)
        assert (nrof_images > 0), 'The dataset should not be empty'
        input_queue = tf.train.string_input_producer(image_list, num_epochs=None, shuffle=False, seed=None, capacity=32)
        nrof_preprocess_threads = 4
        images = []
        for _ in range(nrof_preprocess_threads):
            filename = input_queue.dequeue()
            file_contents = tf.read_file(filename)
            image = tf.image.decode_image(file_contents)
            image = tf.image.resize_image_with_crop_or_pad(image, 160, 160)
            image.set_shape((args.image_size, args.image_size, 3))
            image = tf.cast(image, tf.float32)
            images.append((image,))
        image_batch = tf.train.batch_join(images, batch_size=100, allow_smaller_final_batch=True)
        (m, v) = tf.nn.moments(image_batch, [1, 2])
        mean = tf.reduce_mean(m, 0)
        variance = tf.reduce_mean(v, 0)
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess.run(tf.global_variables_initializer())
        tf.train.start_queue_runners(sess=sess)
        with sess.as_default():
            print('Running training')
            nrof_batches = (nrof_images // args.batch_size)
            means = np.zeros(shape=(nrof_batches, 3), dtype=np.float32)
            variances = np.zeros(shape=(nrof_batches, 3), dtype=np.float32)
            for i in range(nrof_batches):
                (means[i, :], variances[i, :]) = sess.run([mean, variance])
                if (((i + 1) % 10) == 0):
                    print(('Batch: %5d/%5d, Mean: %s,  Variance: %s' % ((i + 1), nrof_batches, np.array_str(np.mean(means[:i, :], axis=0)), np.array_str(np.mean(variances[:i, :], axis=0)))))
            dataset_mean = np.mean(means, axis=0)
            dataset_variance = np.mean(variances, axis=0)
            print(('Final mean: %s' % np.array_str(dataset_mean)))
            print(('Final variance: %s' % np.array_str(dataset_variance)))
            with open(result_filename, 'w') as text_file:
                print(('Writing result to %s' % result_filename))
                text_file.write(('Mean: %.5f, %.5f, %.5f\n' % (dataset_mean[0], dataset_mean[1], dataset_mean[2])))
                text_file.write(('Variance: %.5f, %.5f, %.5f\n' % (dataset_variance[0], dataset_variance[1], dataset_variance[2])))
def main(args):
    np.random.seed(seed=args.seed)
    random.seed(args.seed)
    train_set = facenet.get_dataset(args.data_dir)
    result_filename = os.path.join(os.path.expanduser(args.data_dir), 'statistics.txt')
    with tf.Graph().as_default():
        tf.set_random_seed(args.seed)
        (image_list, _) = facenet.get_image_paths_and_labels(train_set)
        nrof_images = len(image_list)
        assert (nrof_images > 0), 'The dataset should not be empty'
        input_queue = tf.train.string_input_producer(image_list, num_epochs=None, shuffle=False, seed=None, capacity=32)
        nrof_preprocess_threads = 4
        images = []
        for _ in range(nrof_preprocess_threads):
            filename = input_queue.dequeue()
            file_contents = tf.read_file(filename)
            image = tf.image.decode_image(file_contents)
            image = tf.image.resize_image_with_crop_or_pad(image, 160, 160)
            image.set_shape((args.image_size, args.image_size, 3))
            image = tf.cast(image, tf.float32)
            images.append((image,))
        image_batch = tf.train.batch_join(images, batch_size=100, allow_smaller_final_batch=True)
        (m, v) = tf.nn.moments(image_batch, [1, 2])
        mean = tf.reduce_mean(m, 0)
        variance = tf.reduce_mean(v, 0)
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess.run(tf.global_variables_initializer())
        tf.train.start_queue_runners(sess=sess)
        with sess.as_default():
            print('Running training')
            nrof_batches = (nrof_images // args.batch_size)
            means = np.zeros(shape=(nrof_batches, 3), dtype=np.float32)
            variances = np.zeros(shape=(nrof_batches, 3), dtype=np.float32)
            for i in range(nrof_batches):
                (means[i, :], variances[i, :]) = sess.run([mean, variance])
                if (((i + 1) % 10) == 0):
                    print(('Batch: %5d/%5d, Mean: %s,  Variance: %s' % ((i + 1), nrof_batches, np.array_str(np.mean(means[:i, :], axis=0)), np.array_str(np.mean(variances[:i, :], axis=0)))))
            dataset_mean = np.mean(means, axis=0)
            dataset_variance = np.mean(variances, axis=0)
            print(('Final mean: %s' % np.array_str(dataset_mean)))
            print(('Final variance: %s' % np.array_str(dataset_variance)))
            with open(result_filename, 'w') as text_file:
                print(('Writing result to %s' % result_filename))
                text_file.write(('Mean: %.5f, %.5f, %.5f\n' % (dataset_mean[0], dataset_mean[1], dataset_mean[2])))
                text_file.write(('Variance: %.5f, %.5f, %.5f\n' % (dataset_variance[0], dataset_variance[1], dataset_variance[2])))