在输入网络之前对图片的预处理代码

from PIL import Image
import numpy as np

def letterbox_image(image, size):
    iw, ih = image.size
    w, h = size
    scale = min(w/iw, h/ih)
    nw = int(iw*scale)
    nh = int(ih*scale)

    image = image.resize((nw,nh), Image.BICUBIC)
    new_image = Image.new('RGB', size, (128,128,128))
    new_image.paste(image, ((w-nw)//2, (h-nh)//2))
    return new_image

def preprocess_image(image):
    mean = [0.40789655, 0.44719303, 0.47026116]
    std = [0.2886383, 0.27408165, 0.27809834]
    return ((np.float32(image) / 255.) - mean) / std

def normalizeimage(imgpath,w,h):
    image = Image.open(imgpath)
    crop_img = letterbox_image(image, [w,h])#pad成正方形
    # 将RGB转化成BGR,这是因为原始的centernet_hourglass权值是使用BGR通道的图片训练的
    photo = np.array(crop_img, dtype=np.float32)[:, :, ::-1]
    # 图片预处理,归一化
    photo = np.reshape(np.transpose(preprocess_image(photo), (2, 0, 1)),
                       [1, 3, w, h])
    return photo

imgpath = 'img/street.jpg'
print(normalizeimage(imgpath,512,512).shape)
class _ReweightedDataset(object):
    def __init__(self, res, img_dir, sources, weights, split):
        self.res = tuple(res)

        self.num_samples = []
        self.paths = []
        for src in sources:
            paths = sorted([x for x in glob.glob(os.path.join(img_dir, src, '**'), recursive=True) if is_image_file(x)])
            self.num_samples.append(len(paths))
            self.paths += paths
        self.weights = weights
        self.split = split

        print('Loading dataset:', split, sources, self.num_samples)

    def __getitem__(self, index):
        if self.split == 'train':
            cls_id = random.randint(0, 3)
            cumsum = np.cumsum(self.weights)
            which = np.sum(cumsum/cumsum[-1] < random.random())
            index = sum(self.num_samples[:which]) + random.randint(0, self.num_samples[which]-1)
        else:
            cls_id = index % 4
            index = index // 4

        img = cv2.imread(self.paths[index])
        img = np.rot90(img, k=cls_id)

        if self.split == 'train':
            img = self.aug(img)
        img = self.transform(img)

        return {'input': img, 'target': cls_id}

    def __len__(self):
        return 10000 if self.split == 'train' else sum(self.num_samples)*4

    def transform(self, img):
        op = transforms.Compose([
            lambda x: cv2.resize(x, self.res, interpolation=cv2.INTER_AREA),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
        ])
        return op(img)

    def aug(self, img):
        op = iaa.Sequential([
            iaa.PerspectiveTransform(scale=(0, 0.1), keep_size=False),
            iaa.Crop(percent=(0, 0.1), keep_size=False),
            iaa.Sometimes(
                0.5,
                iaa.GaussianBlur(sigma=(0, 0.5))
            ),
            iaa.LinearContrast((0.75, 1.5)),
            iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
            iaa.Multiply((0.8, 1.2), per_channel=0.2),
        ], random_order=True)
        return op(image=img)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值