示例#1
0
def get_data(train_or_test):
    isTrain = train_or_test == 'train'
    ds = dataset.ILSVRC12(args.data,
                          train_or_test,
                          shuffle=True if isTrain else False)
    meta = dataset.ILSVRCMeta()
    pp_mean = meta.get_per_pixel_mean()

    if isTrain:
        # TODO use the augmentor in GoogleNet
        augmentors = [
            imgaug.Resize((256, 256)),
            imgaug.Brightness(30, False),
            imgaug.Contrast((0.8, 1.2), True),
            imgaug.MapImage(lambda x: x - pp_mean),
            imgaug.RandomCrop((224, 224)),
            imgaug.Flip(horiz=True),
        ]
    else:
        augmentors = [
            imgaug.Resize((256, 256)),
            imgaug.MapImage(lambda x: x - pp_mean),
            imgaug.CenterCrop((224, 224)),
        ]
    ds = AugmentImageComponent(ds, augmentors, copy=False)
    ds = BatchData(ds, BATCH_SIZE, remainder=not isTrain)
    if isTrain:
        ds = PrefetchDataZMQ(ds, 6)
    return ds
def get_data(train_or_test):
    isTrain = train_or_test == 'train'

    ds = dataset.ILSVRC12(args.data, train_or_test,
                          shuffle=True if isTrain else False)
    meta = dataset.ILSVRCMeta()
    pp_mean = meta.get_per_pixel_mean()
    pp_mean_299 = cv2.resize(pp_mean, (299, 299))

    if isTrain:
        class Resize(imgaug.ImageAugmentor):
            def __init__(self):
                self._init(locals())

            def _augment(self, img, _):
                h, w = img.shape[:2]
                size = 299
                scale = self.rng.randint(size, 340) * 1.0 / min(h, w)
                scaleX = scale * self.rng.uniform(0.85, 1.15)
                scaleY = scale * self.rng.uniform(0.85, 1.15)
                desSize = map(int, (max(size, min(w, scaleX * w)),
                                    max(size, min(h, scaleY * h))))
                dst = cv2.resize(img, tuple(desSize), interpolation=cv2.INTER_CUBIC)
                return dst

        augmentors = [
            Resize(),
            imgaug.Rotation(max_deg=10),
            imgaug.RandomApplyAug(imgaug.GaussianBlur(3), 0.5),
            imgaug.Brightness(30, True),
            imgaug.Gamma(),
            imgaug.Contrast((0.8, 1.2), True),
            imgaug.RandomCrop((299, 299)),
            imgaug.RandomApplyAug(imgaug.JpegNoise(), 0.8),
            imgaug.RandomApplyAug(imgaug.GaussianDeform(
                [(0.2, 0.2), (0.2, 0.8), (0.8, 0.8), (0.8, 0.2)],
                (299, 299), 0.2, 3), 0.1),
            imgaug.Flip(horiz=True),
            imgaug.MapImage(lambda x: x - pp_mean_299),
        ]
    else:
        def resize_func(im):
            h, w = im.shape[:2]
            scale = 340.0 / min(h, w)
            desSize = map(int, (max(299, min(w, scale * w)),
                                max(299, min(h, scale * h))))
            im = cv2.resize(im, tuple(desSize), interpolation=cv2.INTER_CUBIC)
            return im
        augmentors = [
            imgaug.MapImage(resize_func),
            imgaug.CenterCrop((299, 299)),
            imgaug.MapImage(lambda x: x - pp_mean_299),
        ]
    ds = AugmentImageComponent(ds, augmentors, copy=False)
    ds = BatchData(ds, BATCH_SIZE, remainder=not isTrain)
    if isTrain:
        ds = PrefetchDataZMQ(ds, min(12, multiprocessing.cpu_count()))
    return ds
示例#3
0
def get_data(batch, augmentors):
    """
    Sec 3, Remark 4:
    Use a single random shuffling of the training data (per epoch) that is divided amongst all k workers.
    Here we do not follow the paper because it does not seem to make a difference.
    """
    ds = dataset.ILSVRC12(args.data, 'train', shuffle=True)
    ds = AugmentImageComponent(ds, augmentors, copy=False)
    ds = BatchData(ds, batch, remainder=False)
    ds = PrefetchDataZMQ(ds, min(50, mp.cpu_count()))
    return ds
def get_data(batch, augmentors, workers):
    """
    Sec 3, Remark 4:
    Use a single random shuffling of the training data (per epoch) that is divided amongst all k workers.

    NOTE: Here we do not follow the paper, but it makes little differences.
    """
    ds = dataset.ILSVRC12(args.data, 'train', shuffle=True)
    ds = AugmentImageComponent(ds, augmentors, copy=False)
    ds = PrefetchDataZMQ(ds, workers)
    ds = BatchData(ds, batch, remainder=False)
    return ds
示例#5
0
def get_imagenet_dataflow(datadir,
                          name,
                          batch_size,
                          augmentors=None,
                          parallel=None):
    """
    Args:
        augmentors (list[imgaug.Augmentor]): Defaults to `fbresnet_augmentor(isTrain)`

    Returns: A DataFlow which produces BGR images and labels.

    See explanations in the tutorial:
    http://tensorpack.readthedocs.io/tutorial/efficient-dataflow.html
    """
    assert name in ['train', 'val', 'test']
    isTrain = name == 'train'
    assert datadir is not None
    if augmentors is None:
        augmentors = fbresnet_augmentor(isTrain)
    assert isinstance(augmentors, list)
    if parallel is None:
        parallel = min(40,
                       multiprocessing.cpu_count() //
                       2)  # assuming hyperthreading

    if isTrain:
        ds = dataset.ILSVRC12(datadir, name, shuffle=True)
        ds = AugmentImageComponent(ds, augmentors, copy=False)
        if parallel < 16:
            logger.warn(
                "DataFlow may become the bottleneck when too few processes are used."
            )
        ds = PrefetchDataZMQ(ds, parallel)
        ds = BatchData(ds, batch_size, remainder=False)
    else:
        ds = dataset.ILSVRC12Files(datadir, name, shuffle=False)
        aug = imgaug.AugmentorList(augmentors)

        def mapf(dp):
            fname, cls = dp
            im = cv2.imread(fname, cv2.IMREAD_COLOR)
            im = aug.augment(im)
            return im, cls

        ds = MultiThreadMapData(ds,
                                parallel,
                                mapf,
                                buffer_size=2000,
                                strict=True)
        ds = BatchData(ds, batch_size, remainder=True)
        ds = PrefetchDataZMQ(ds, 1)
    return ds
示例#6
0
def get_data(train_or_test):
    # completely copied from imagenet-resnet.py example
    isTrain = train_or_test == 'train'

    datadir = args.data
    ds = dataset.ILSVRC12(datadir, train_or_test, shuffle=isTrain)
    augmentors = fbresnet_augmentor(isTrain)
    augmentors.append(imgaug.ToUint8())

    ds = AugmentImageComponent(ds, augmentors, copy=False)
    if isTrain:
        ds = MultiProcessRunnerZMQ(ds, min(25, multiprocessing.cpu_count()))
    ds = BatchData(ds, BATCH_SIZE, remainder=not isTrain)
    return ds
示例#7
0
def eval_on_ILSVRC12(model_path, data_dir):
    ds = dataset.ILSVRC12(data_dir, 'val', shuffle=False)
    ds = AugmentImageComponent(ds, get_inference_augmentor())
    ds = BatchData(ds, 192, remainder=True)
    pred_config = PredictConfig(model=Model(),
                                session_init=get_model_loader(model_path),
                                input_names=['input', 'label'],
                                output_names=['wrong-top1', 'wrong-top5'])
    pred = SimpleDatasetPredictor(pred_config, ds)
    acc1, acc5 = RatioCounter(), RatioCounter()
    for o in pred.get_result():
        batch_size = o[0].shape[0]
        acc1.feed(o[0].sum(), batch_size)
        acc5.feed(o[1].sum(), batch_size)
    print("Top1 Error: {}".format(acc1.ratio))
    print("Top5 Error: {}".format(acc5.ratio))
示例#8
0
def get_imagenet_dataflow(datadir,
                          is_train,
                          batch_size,
                          augmentors,
                          parallel=None):
    """
    See explanations in the tutorial:
    http://tensorpack.readthedocs.io/en/latest/tutorial/efficient-dataflow.html
    """
    assert datadir is not None
    assert isinstance(augmentors, list)
    if parallel is None:
        parallel = min(40,
                       multiprocessing.cpu_count() //
                       2)  # assuming hyperthreading
    if is_train:
        ds = dataset.ILSVRC12(datadir, "train", shuffle=True)
        ds = AugmentImageComponent(ds, augmentors, copy=False)
        if parallel < 16:
            logging.warning(
                "DataFlow may become the bottleneck when too few processes are used."
            )
        ds = PrefetchDataZMQ(ds, parallel)
        ds = BatchData(ds, batch_size, remainder=False)
    else:
        ds = dataset.ILSVRC12Files(datadir, "val", shuffle=False)
        aug = imgaug.AugmentorList(augmentors)

        def mapf(dp):
            fname, cls = dp
            im = cv2.imread(fname, cv2.IMREAD_COLOR)
            im = np.flip(im, axis=2)
            # print("fname={}".format(fname))
            im = aug.augment(im)
            return im, cls

        ds = MultiThreadMapData(ds,
                                parallel,
                                mapf,
                                buffer_size=2000,
                                strict=True)
        # ds = MapData(ds, mapf)
        ds = BatchData(ds, batch_size, remainder=True)
        ds = PrefetchDataZMQ(ds, 1)
        # ds = PrefetchData(ds, 1)
    return ds
示例#9
0
    def get_data(self, name, num_gpu):
        gpu_batch = self.batch_size // num_gpu

        assert name in ['train', 'val', 'test']
        isTrain = name == 'train'

        augmentors = fbresnet_augmentor(isTrain)
        assert isinstance(augmentors, list)

        parallel = min(40,
                       multiprocessing.cpu_count() //
                       2)  # assuming hyperthreading

        if isTrain:
            ds = dataset.ILSVRC12(self.datadir,
                                  name,
                                  shuffle=True,
                                  dir_structure='train')
            ds = AugmentImageComponent(ds, augmentors, copy=False)
            ds = MultiProcessRunnerZMQ(ds, parallel)
            ds = BatchData(ds, gpu_batch, remainder=False)
            #ds = QueueInput(ds)
        else:
            ds = dataset.ILSVRC12Files(self.datadir,
                                       name,
                                       shuffle=False,
                                       dir_structure='train')
            aug = imgaug.AugmentorList(augmentors)

            def mapf(dp):
                fname, cls = dp
                im = cv2.imread(fname, cv2.IMREAD_COLOR)
                im = aug.augment(im)
                return im, cls

            ds = MultiThreadMapData(ds,
                                    parallel,
                                    mapf,
                                    buffer_size=2000,
                                    strict=True)
            ds = BatchData(ds, gpu_batch, remainder=True)
            ds = MultiProcessRunnerZMQ(ds, 1)

            if num_gpu == 1:
                ds = QueueInput(ds)
        return ds
示例#10
0
def get_data(train_or_test, option):
    isTrain = train_or_test == 'train'

    datadir = option.data
    if option.final_size == 64:
        ds = dataset.tinyImagenetHaS(datadir,
                                     train_or_test,
                                     'all',
                                     shuffle=isTrain)
    elif option.final_size == 224:
        ds = dataset.ILSVRC12(datadir, train_or_test, shuffle=isTrain)
    augmentors = fbresnet_augmentor(isTrain, option=option)
    augmentors.append(imgaug.ToUint8())
    ds = AugmentImageComponent(ds, augmentors, copy=False)
    if isTrain:
        ds = PrefetchDataZMQ(ds, min(25, multiprocessing.cpu_count()))
    ds = BatchData(ds, int(option.batch), remainder=not isTrain)
    return ds
示例#11
0
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', help='the physical ids of GPUs to use')
    parser.add_argument('--load', help='load a npz pretrained model')
    parser.add_argument('--data', help='ILSVRC dataset dir')
    parser.add_argument(
        '--dorefa',
        help=
        'number of bits for W,A,G, separated by comma. Defaults to \'1,4,32\'',
        default='1,4,32')
    parser.add_argument(
        '--run',
        help='run on a list of images with the pretrained model',
        nargs='*')
    parser.add_argument('--eval', action='store_true')
    args = parser.parse_args()

    BITW, BITA, BITG = map(int, args.dorefa.split(','))

    if args.gpu:
        os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu

    if args.eval:
        ds = dataset.ILSVRC12(args.data, 'val', shuffle=False)
        ds = AugmentImageComponent(ds, get_inference_augmentor())
        ds = BatchData(ds, 192, remainder=True)
        eval_on_ILSVRC12(Model(), get_model_loader(args.load), ds)
    elif args.run:
        assert args.load.endswith('.npz')
        run_image(Model(), DictRestore(dict(np.load(args.load))), args.run)
示例#12
0
def get_data(batch, augmentors):
    ds = dataset.ILSVRC12(args.data, 'train', shuffle=True)
    ds = AugmentImageComponent(ds, augmentors, copy=False)
    ds = BatchData(ds, batch, remainder=False)
    ds = PrefetchDataZMQ(ds, min(50, mp.cpu_count()))
    return ds
示例#13
0
def get_input_imagenet():
    train = dataset.ILSVRC12("/datasets/ImageNet/ILSVRC/Data/CLS-LOC",
                             "train",
                             dir_structure="train",
                             shuffle=True)
    test = dataset.ILSVRC12("/datasets/ImageNet/ILSVRC/Data/CLS-LOC",
                            "val",
                            dir_structure="train",
                            shuffle=False)

    # Copied from tensorpack examples:
    # https://github.com/tensorpack/tensorpack/blob/master/examples/ImageNetModels/imagenet_utils.py

    train_augmentors = imgaug.AugmentorList([
        imgaug.GoogleNetRandomCropAndResize(interp=cv2.INTER_CUBIC),
        # It's OK to remove the following augs if your CPU is not fast enough.
        # Removing brightness/contrast/saturation does not have a significant effect on accuracy.
        # Removing lighting leads to a tiny drop in accuracy.
        imgaug.RandomOrderAug([
            imgaug.BrightnessScale((0.6, 1.4), clip=False),
            imgaug.Contrast((0.6, 1.4), rgb=False, clip=False),
            imgaug.Saturation(0.4, rgb=False),
            # rgb-bgr conversion for the constants copied from fb.resnet.torch
            imgaug.Lighting(
                0.1,
                eigval=np.asarray([0.2175, 0.0188, 0.0045][::-1]) * 255.0,
                eigvec=np.array(
                    [[-0.5675, 0.7192, 0.4009], [-0.5808, -0.0045, -0.8140],
                     [-0.5836, -0.6948, 0.4203]],
                    dtype='float32')[::-1, ::-1])
        ]),
        imgaug.Flip(horiz=True),
    ])

    test_augmentors = imgaug.AugmentorList([
        imgaug.ResizeShortestEdge(256, interp=cv2.INTER_CUBIC),
        imgaug.CenterCrop((224, 224)),
    ])

    def preprocess(augmentors):
        def apply(x):
            image, label = x
            onehot = np.zeros(1000)
            onehot[label] = 1.0
            image = augmentors.augment(image)
            return image, onehot

        return apply

    parallel = min(40,
                   multiprocessing.cpu_count() // 2)  # assuming hyperthreading
    train = MapData(train, preprocess(train_augmentors))
    train = PrefetchDataZMQ(train, parallel)

    test = MultiThreadMapData(test,
                              parallel,
                              preprocess(test_augmentors),
                              strict=True)
    test = PrefetchDataZMQ(test, 1)

    return train, test, ((224, 224, 3), (1000, ))