Ejemplo n.º 1
0
def get_real_activations(batch_size,
                         num_batches,
                         shuffle_buffer_size=100000,
                         split='validation',
                         get_logits=False):
    """Fetches batches inception pools and images.

  NOTE: This function runs inference on an Inception network, so it would be
  more efficient to run this on GPU or TPU than on CPU.

  Args:
    batch_size: The number of elements in a single minibatch.
    num_batches: The number of batches to fetch at a time.
    shuffle_buffer_size: The number of records to load before shuffling. Larger
        means more likely randomization.
    split: Shuffle if 'train', else deterministic.
    get_logits: If `True`, return (logits, pools). Otherwise just return pools.

  Returns:
    A Tensor of `real_pools` or (`real_logits`, `real_pools`) with batch
    dimension (batch_size * num_batches).
  """
    ds = data_provider.provide_dataset(batch_size, shuffle_buffer_size, split)
    ds = ds.map(lambda img, lbl: img)  # Remove labels.
    return get_activations_from_dataset(ds, num_batches, get_logits)
Ejemplo n.º 2
0
def train_eval_input_fn(mode, params):
    """Mode-aware input function."""
    is_train = mode == tf.estimator.ModeKeys.TRAIN
    split = 'train' if is_train else 'validation'

    if params['tpu_params'].use_tpu_estimator:
        bs = params['batch_size']
    else:
        bs = {
            tf.estimator.ModeKeys.TRAIN: params['train_batch_size'],
            tf.estimator.ModeKeys.EVAL: params['eval_batch_size'],
            tf.estimator.ModeKeys.PREDICT: params['predict_batch_size'],
        }[mode]

    if params['debug_params'].fake_data:
        fake_noise = tf.zeros([bs, params['z_dim']])
        if mode == tf.estimator.ModeKeys.PREDICT:
            return tf.data.Dataset.from_tensors(fake_noise).repeat()
        fake_imgs = tf.zeros([bs, 128, 128, 3])
        fake_lbls = tf.zeros([bs], dtype=tf.int32)
        ds = tf.data.Dataset.from_tensors((fake_noise, {
            'images': fake_imgs,
            'labels': fake_lbls
        }))
        ds = ds.repeat()
        _verify_dataset_shape(ds, params['z_dim'])
        return ds

    num_towers = 1

    def _make_noise(_):
        noise = gen_module.make_z_normal(num_towers, bs, params['z_dim'])
        return noise[0]  # one tower

    noise_ds = tf.data.Dataset.from_tensors(0).repeat().map(_make_noise)
    if mode == tf.estimator.ModeKeys.PREDICT:
        return noise_ds

    images_ds = data_provider.provide_dataset(
        bs, shuffle_buffer_size=params['shuffle_buffer_size'], split=split)
    images_ds = images_ds.map(lambda img, lbl: {
        'images': img,
        'labels': lbl
    })  # map to dict.

    ds = tf.data.Dataset.zip((noise_ds, images_ds))
    _verify_dataset_shape(ds, params['z_dim'])
    return ds
Ejemplo n.º 3
0
def train_eval_input_fn(mode, params, restrict_classes=None, shift_classes=0):
    """Mode-aware input function.

  restrict_classes: for use with intra fid
  shift_classes: for use with restrict_classes
  """
    is_train = mode == tf.estimator.ModeKeys.TRAIN
    split = 'train' if is_train else flags.FLAGS.dataset_val_split_name

    if params['tpu_params'].use_tpu_estimator:
        bs = params['batch_size']
    else:
        bs = {
            tf.estimator.ModeKeys.TRAIN: params['train_batch_size'],
            tf.estimator.ModeKeys.EVAL: params['eval_batch_size'],
            tf.estimator.ModeKeys.PREDICT: params['predict_batch_size'],
        }[mode]

    if params['debug_params'].fake_data:
        fake_noise = tf.zeros([bs, params['z_dim']])
        if mode == tf.estimator.ModeKeys.PREDICT:
            return tf.data.Dataset.from_tensors(fake_noise).repeat()
        fake_imgs = tf.zeros(
            [bs, flags.FLAGS.image_size, flags.FLAGS.image_size, 3])
        fake_lbls = tf.zeros([bs], dtype=tf.int32)
        ds = tf.data.Dataset.from_tensors((fake_noise, {
            'images': fake_imgs,
            'labels': fake_lbls
        }))
        ds = ds.repeat()
        _verify_dataset_shape(ds, params['z_dim'])
        return ds

    num_towers = 1

    def _make_noise(_):
        noise = gen_module.make_z_normal(num_towers, bs, params['z_dim'])
        return noise[0]  # one tower

    noise_ds = tf.data.Dataset.from_tensors(0).repeat().map(_make_noise)
    if mode == tf.estimator.ModeKeys.PREDICT and not flags.FLAGS.mode == 'gen_images':
        return noise_ds

    images_ds = data_provider.provide_dataset(
        bs,
        shuffle_buffer_size=params['shuffle_buffer_size'],
        split=split,
        restrict_classes=restrict_classes)

    if flags.FLAGS.unlabelled_dataset_name is not None:
        unl_images_ds = data_provider_unlabelled.provide_dataset(
            bs,
            shuffle_buffer_size=params['shuffle_buffer_size'],
            split=flags.FLAGS.unlabelled_dataset_split_name)

        images_ds = tf.data.Dataset.zip((images_ds, unl_images_ds))
        images_ds = images_ds.map(
            lambda img_lab_tup, unl_img: {
                'images': img_lab_tup[0],
                'labels': img_lab_tup[1],
                'unlabelled_images': unl_img
            })  # map to dict.
    else:
        images_ds = images_ds.map(lambda img, lbl: {
            'images': img,
            'labels': lbl
        })  # map to dict.

    ds = tf.data.Dataset.zip((noise_ds, images_ds))
    if restrict_classes is not None or flags.FLAGS.mode == 'intra_fid_eval':
        ds = ds.map(lambda noise_ds, images_ds:
                    ({
                        'z': noise_ds,
                        'labels': images_ds['labels'] - shift_classes
                    }, {
                        'images': images_ds['images'],
                        'labels': images_ds['labels'] - shift_classes
                    }))
    elif flags.FLAGS.mode == 'gen_images':

        def _make_labels_for_class(y):
            return gen_module.make_one_batch_constant_labels(bs, y)

        labels_todo = [900]

        #labels_todo = list(range(flags.FLAGS.num_classes))
        # hack to print your favorite classes
        # labels_todo = list(sorted([130,96,90,88,164,175,281,289,290,292,294,323,441,475,555,581,607,654,661,663,688,779] * 5))

        # okay, this is a ugly papercut, but an easy one-line modification of the above
        def _make_one_batch_unif_random_labels(index):
            del index
            return gen_module.make_class_labels(bs, flags.FLAGS.num_classes)

        if flags.FLAGS.gen_images_uniform_random_labels:
            labs_ds = tf.data.Dataset.from_tensor_slices(
                labels_todo).repeat().map(_make_one_batch_unif_random_labels)
        else:
            labs_ds = tf.data.Dataset.from_tensor_slices(
                labels_todo).repeat().map(_make_labels_for_class)

        ds = tf.data.Dataset.zip((noise_ds, images_ds, labs_ds))
        ds = ds.map(lambda noise_ds_, images_ds_, labs_ds_: {
            'z': noise_ds_,
            'labels': labs_ds_
        })  # fake data only
        # ds = ds.map(lambda noise_ds_, images_ds_, labs_ds_: ({'z': noise_ds_, 'labels': labs_ds_}, images_ds_) )
    else:
        _verify_dataset_shape(ds, params['z_dim'])
    return ds