自定义mindspore中数据增强方法

自定义mindspore中数据增强方法

原图:自定义mindspore中数据增强方法_第1张图片
RandomCrop可视化

    data = vision.RandomCrop(size=(250, 250))(data)
    cv2.imwrite('randomo_crop.jpg', data)

自定义mindspore中数据增强方法_第2张图片
创建自己的数据增强方法DrawCircleCenter;

class DrawCircleCenter(object):
    def __init__(self, r=50, color=(0, 0, 255)):
        self.r = r
        self.color = color

    def __call__(self, img):
        (x, y) = (round(img.shape[1] / 2), round(img.shape[0] / 2))
        cv2.circle(img, (x, y), self.r, self.color, 3)
        return img

使用该方法;

    data = DrawCircleCenter()(data)
    cv2.imwrite('DrawCircleCenter.jpg', data)

自定义mindspore中数据增强方法_第3张图片
全部代码:

import mindspore.dataset.vision as vision
import cv2


class DrawCircleCenter(object):
    def __init__(self, r=50, color=(0, 0, 255)):
        self.r = r
        self.color = color

    def __call__(self, img):
        (x, y) = (round(img.shape[1] / 2), round(img.shape[0] / 2))
        cv2.circle(img, (x, y), self.r, self.color, 3)
        return img


if __name__ == '__main__':
    cat_path = './cat.jpeg'

    with open(cat_path, "rb") as f:
        data = f.read()
    data = vision.Decode()(data)
    cv2.imwrite('decode.jpg', data)
    data = vision.RandomCrop(size=(250, 250))(data)
    cv2.imwrite('randomo_crop.jpg', data)
    data = vision.Resize(size=(224, 224))(data)
    data = DrawCircleCenter()(data)
    cv2.imwrite('DrawCircleCenter.jpg', data)
    data = vision.Normalize(mean=[0.485 * 255, 0.456 * 255, 0.406 * 255],
                            std=[0.229 * 255, 0.224 * 255, 0.225 * 255])(data)
    cv2.imwrite('Normalize.jpg', data)

你可能感兴趣的:(python,人工智能)