forked from TeamSemiSuperCV/semi-super
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
114 lines (99 loc) · 4.24 KB
/
data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# coding=utf-8
# Copyright 2020 The SimCLR Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific simclr governing permissions and
# limitations under the License.
# ==============================================================================
"""Data pipeline."""
import functools
from absl import flags
from absl import logging
import data_util
import tensorflow.compat.v2 as tf
import tensorflow_datasets as tfds
FLAGS = flags.FLAGS
def build_input_fn(builder, global_batch_size, topology, is_training):
"""Build input function.
Args:
builder: TFDS builder for specified dataset.
global_batch_size: Global batch size.
topology: An instance of `tf.tpu.experimental.Topology` or None.
is_training: Whether to build in training mode.
Returns:
A function that accepts a dict of params and returns a tuple of images and
features, to be used as the input_fn in TPUEstimator.
"""
def _input_fn(input_context):
"""Inner input function."""
batch_size = input_context.get_per_replica_batch_size(global_batch_size)
logging.info('Global batch size: %d', global_batch_size)
logging.info('Per-replica batch size: %d', batch_size)
preprocess_fn_pretrain = get_preprocess_fn(is_training, is_pretrain=True)
preprocess_fn_finetune = get_preprocess_fn(is_training, is_pretrain=False)
num_classes = builder.info.features['label'].num_classes
def map_fn(image, label):
"""Produces multiple transformations of the same batch."""
if is_training and FLAGS.train_mode == 'pretrain':
xs = []
for _ in range(2): # Two transformations
xs.append(preprocess_fn_pretrain(image))
image = tf.concat(xs, -1)
else:
image = preprocess_fn_finetune(image)
label = tf.one_hot(label, num_classes)
return image, label
logging.info('num_input_pipelines: %d', input_context.num_input_pipelines)
dataset = builder.as_dataset(
split=FLAGS.train_split if is_training else FLAGS.eval_split,
shuffle_files=is_training,
as_supervised=True,
# Passing the input_context to TFDS makes TFDS read different parts
# of the dataset on different workers. We also adjust the interleave
# parameters to achieve better performance.
read_config=tfds.ReadConfig(
interleave_cycle_length=32,
interleave_block_length=1,
input_context=input_context))
if FLAGS.cache_dataset:
dataset = dataset.cache()
if is_training:
options = tf.data.Options()
options.experimental_deterministic = False
options.experimental_slack = True
dataset = dataset.with_options(options)
buffer_multiplier = 50 if FLAGS.image_size <= 32 else 10
dataset = dataset.shuffle(batch_size * buffer_multiplier)
dataset = dataset.repeat(-1)
dataset = dataset.map(
map_fn, num_parallel_calls=tf.data.experimental.AUTOTUNE)
dataset = dataset.batch(batch_size, drop_remainder=is_training)
dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE)
return dataset
return _input_fn
def build_distributed_dataset(builder, batch_size, is_training, strategy,
topology):
input_fn = build_input_fn(builder, batch_size, topology, is_training)
return strategy.distribute_datasets_from_function(input_fn)
def get_preprocess_fn(is_training, is_pretrain):
"""Get function that accepts an image and returns a preprocessed image."""
# Disable test cropping for small images (e.g. CIFAR) or when test_crop is set to False by user
if FLAGS.image_size <= 32 or FLAGS.test_crop == False:
test_crop = False
else:
test_crop = True
return functools.partial(
data_util.preprocess_image,
height=FLAGS.image_size,
width=FLAGS.image_size,
is_training=is_training,
color_distort=is_pretrain,
test_crop=test_crop)