ofrecord_util.py 7.3 KB
Newer Older
S
ShawnXuan 已提交
1 2 3 4 5 6 7 8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import oneflow as flow

def add_ofrecord_args(parser):
    parser.add_argument("--image_size", type=int, default=224, required=False, help="image size")
S
ShawnXuan 已提交
9
    parser.add_argument("--resize_shorter", type=int, default=256, required=False, help="resize shorter for validation")
S
ShawnXuan 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22
    parser.add_argument("--train_data_dir", type=str, default=None, help="train dataset directory")
    parser.add_argument("--train_data_part_num", type=int, default=256, help="train data part num")
    parser.add_argument("--val_data_dir", type=str, default=None, help="val dataset directory")
    parser.add_argument("--val_data_part_num", type=int, default=256, help="val data part num")
    return parser


def load_imagenet(args, batch_size, data_dir, data_part_num, codec):
    image_blob_conf = flow.data.BlobConf(
        "encoded",
        shape=(args.image_size, args.image_size, 3),
        dtype=flow.float,
        codec=codec,
S
bgr2rgb  
ShawnXuan 已提交
23 24 25
        preprocessors=[flow.data.NormByChannelPreprocessor(args.rgb_mean[::-1],
                                                           args.rgb_std[::-1])],
        #preprocessors=[flow.data.NormByChannelPreprocessor(args.rgb_mean, args.rgb_std)], #bgr2rgb
S
ShawnXuan 已提交
26 27 28 29 30 31 32 33 34 35 36 37
    )

    label_blob_conf = flow.data.BlobConf(
        "class/label", shape=(), dtype=flow.int32, codec=flow.data.RawCodec()
    )

    return flow.data.decode_ofrecord(
        data_dir,
        (label_blob_conf, image_blob_conf),
        batch_size=batch_size,
        data_part_num=data_part_num,
        part_name_suffix_length=5,
S
modify  
ShawnXuan 已提交
38 39
        #shuffle = True,
        #buffer_size=32768,
S
ShawnXuan 已提交
40 41 42 43 44 45 46 47
        name="decode",
    )


def load_imagenet_for_training(args):
    total_device_num = args.num_nodes * args.gpu_num_per_node
    train_batch_size = total_device_num * args.batch_size_per_device
    codec=flow.data.ImageCodec([
S
bgr2rgb  
ShawnXuan 已提交
48
        #flow.data.ImagePreprocessor('bgr2rgb'),
S
modify  
ShawnXuan 已提交
49
        #flow.data.ImageCropWithRandomSizePreprocessor(area=(0.08, 1)),
S
ShawnXuan 已提交
50 51 52
        flow.data.ImageResizePreprocessor(args.image_size, args.image_size),
        flow.data.ImagePreprocessor('mirror'),
    ])
S
ShawnXuan 已提交
53
    return load_imagenet(args, train_batch_size, args.train_data_dir, args.train_data_part_num,
S
ShawnXuan 已提交
54 55 56
                         codec)


S
ShawnXuan 已提交
57
def load_imagenet_for_validation(args):
S
ShawnXuan 已提交
58 59 60 61
    total_device_num = args.num_nodes * args.gpu_num_per_node
    val_batch_size = total_device_num * args.val_batch_size_per_device
    codec=flow.data.ImageCodec(
        [
S
bgr2rgb  
ShawnXuan 已提交
62
            #flow.data.ImagePreprocessor('bgr2rgb'),
S
ShawnXuan 已提交
63 64 65
            # flow.data.ImageTargetResizePreprocessor(resize_shorter=256),
            # flow.data.ImageCenterCropPreprocessor(args.image_size, args.image_size),
            flow.data.ImageResizePreprocessor(args.image_size, args.image_size),
S
ShawnXuan 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        ]
    )
    return load_imagenet(args, val_batch_size, args.val_data_dir, args.val_data_part_num, codec)


def load_synthetic(args):
    total_device_num = args.num_nodes * args.gpu_num_per_node
    batch_size = total_device_num * args.batch_size_per_device
    label = flow.data.decode_random(
        shape=(),
        dtype=flow.int32,
        batch_size=batch_size,
        initializer=flow.zeros_initializer(flow.int32),
    )

    image = flow.data.decode_random(
        shape=(args.image_size, args.image_size, 3), dtype=flow.float, batch_size=batch_size
    )

S
ShawnXuan 已提交
85
    return label, image
S
ShawnXuan 已提交
86 87 88 89 90 91 92


def load_imagenet_for_training2(args):
    total_device_num = args.num_nodes * args.gpu_num_per_node
    train_batch_size = total_device_num * args.batch_size_per_device

    color_space = 'RGB'
S
ShawnXuan 已提交
93
    with flow.fixed_placement("cpu", "0:0-{}".format(args.gpu_num_per_node - 1)):
S
ShawnXuan 已提交
94
        ofrecord = flow.data.ofrecord_reader(args.train_data_dir,
S
ShawnXuan 已提交
95 96
                                             batch_size=train_batch_size,
                                             data_part_num=args.train_data_part_num,
S
ShawnXuan 已提交
97
                                             part_name_suffix_length=5,
S
ShawnXuan 已提交
98
                                             random_shuffle = True,
S
ShawnXuan 已提交
99
                                             shuffle_after_epoch=True)
S
ShawnXuan 已提交
100
        image = flow.data.OFRecordImageDecoderRandomCrop(ofrecord, "encoded", #seed=seed,
S
ShawnXuan 已提交
101 102
                                                         color_space=color_space)
        label = flow.data.OFRecordRawDecoder(ofrecord, "class/label", shape=(), dtype=flow.int32)
S
ShawnXuan 已提交
103
        rsz = flow.image.Resize(image, resize_x=args.image_size, resize_y=args.image_size,
S
ShawnXuan 已提交
104 105
                                color_space=color_space)

S
ShawnXuan 已提交
106
        rng = flow.random.CoinFlip(batch_size=train_batch_size)#, seed=seed)
S
ShawnXuan 已提交
107 108 109
        normal = flow.image.CropMirrorNormalize(rsz, mirror_blob=rng, color_space=color_space,
            mean=args.rgb_mean, std=args.rgb_std, output_dtype = flow.float)
        return label, normal
S
ShawnXuan 已提交
110

S
ShawnXuan 已提交
111 112 113 114 115
def load_imagenet_for_validation2(args):
    total_device_num = args.num_nodes * args.gpu_num_per_node
    val_batch_size = total_device_num * args.val_batch_size_per_device

    color_space = 'RGB'
S
ShawnXuan 已提交
116
    with flow.fixed_placement("cpu", "0:0-{}".format(args.gpu_num_per_node - 1)):
S
ShawnXuan 已提交
117
        ofrecord = flow.data.ofrecord_reader(args.val_data_dir,
S
ShawnXuan 已提交
118 119
                                             batch_size=val_batch_size,
                                             data_part_num=args.val_data_part_num,
S
ShawnXuan 已提交
120 121
                                             part_name_suffix_length=5,
                                             shuffle_after_epoch=False)
S
ShawnXuan 已提交
122
        image = flow.data.OFRecordImageDecoder(ofrecord, "encoded", color_space=color_space)
S
ShawnXuan 已提交
123
        label = flow.data.OFRecordRawDecoder(ofrecord, "class/label", shape=(), dtype=flow.int32)
S
ShawnXuan 已提交
124
        rsz = flow.image.Resize(image, resize_shorter=args.resize_shorter, color_space=color_space)
S
ShawnXuan 已提交
125

S
ShawnXuan 已提交
126 127
        normal = flow.image.CropMirrorNormalize(rsz, color_space=color_space,
            crop_h = args.image_size, crop_w = args.image_size, crop_pos_y = 0.5, crop_pos_x = 0.5,
S
ShawnXuan 已提交
128 129
            mean=args.rgb_mean, std=args.rgb_std, output_dtype = flow.float)
        return label, normal
S
ShawnXuan 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

if __name__ == "__main__":
    import os
    import config as configs
    from util import Summary, InitNodes, Metric
    from job_function_util import get_val_config
    parser = configs.get_parser()
    args = parser.parse_args()
    configs.print_args(args)

    flow.config.gpu_device_num(args.gpu_num_per_node)
    flow.config.enable_debug_mode(True)
    @flow.function(get_val_config(args))
    def IOTest():
        if args.train_data_dir:
            assert os.path.exists(args.train_data_dir)
            print("Loading data from {}".format(args.train_data_dir))
            (labels, images) = load_imagenet_for_training(args)
            #(labels, images) = load_imagenet_for_training2(args)
        else:
            print("Loading synthetic data.")
            (labels, images) = load_synthetic(args)
S
modify  
ShawnXuan 已提交
152
        outputs = {"images":images, "labels": labels}
S
ShawnXuan 已提交
153 154 155 156 157 158 159
        return outputs

    total_device_num = args.num_nodes * args.gpu_num_per_node
    train_batch_size = total_device_num * args.batch_size_per_device
    summary = Summary(args.log_dir, args, filename='io_test.csv')
    metric = Metric(desc='io_test', calculate_batches=args.loss_print_every_n_iter,
                    summary=summary, save_summary_steps=args.loss_print_every_n_iter,
S
modify  
ShawnXuan 已提交
160
                    batch_size=train_batch_size, prediction_key=None)
S
ShawnXuan 已提交
161 162
    for i in range(1000):
        IOTest().async_get(metric.metric_cb(0, i))