imagenet_dataset.py 1.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# 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 language governing permissions and
# limitations under the License.

L
LielinJiang 已提交
15 16 17 18 19 20
import os
import cv2
import math
import random
import numpy as np

21 22
from hapi.datasets import DatasetFolder
from hapi.vision.transforms import transforms
L
LielinJiang 已提交
23
from paddle import fluid
L
LielinJiang 已提交
24 25


L
LielinJiang 已提交
26
class ImageNetDataset(DatasetFolder):
L
LielinJiang 已提交
27
    def __init__(self, path, mode='train'):
L
LielinJiang 已提交
28
        super(ImageNetDataset, self).__init__(path)
L
LielinJiang 已提交
29
        self.mode = mode
L
LielinJiang 已提交
30 31 32

        normalize = transforms.Normalize(
            mean=[123.675, 116.28, 103.53], std=[58.395, 57.120, 57.375])
L
LielinJiang 已提交
33
        if self.mode == 'train':
L
LielinJiang 已提交
34 35 36 37
            self.transform = transforms.Compose([
                transforms.RandomResizedCrop(224),
                transforms.RandomHorizontalFlip(),
                transforms.Permute(mode='CHW'), normalize
L
LielinJiang 已提交
38 39
            ])
        else:
L
LielinJiang 已提交
40 41 42 43
            self.transform = transforms.Compose([
                transforms.Resize(256), transforms.CenterCrop(224),
                transforms.Permute(mode='CHW'), normalize
            ])
L
LielinJiang 已提交
44 45

    def __getitem__(self, idx):
L
LielinJiang 已提交
46 47
        img_path, label = self.samples[idx]
        img = cv2.imread(img_path).astype(np.float32)
48 49
        label = np.array([label])
        return self.transform(img, label)
L
LielinJiang 已提交
50 51 52

    def __len__(self):
        return len(self.samples)