【pytorch】【常用】【供个人粘贴使用】pytorch导入数据(带数据增强)

这篇博客介绍了如何在PyTorch中进行数据增强,包括numpy数组的水平和垂直随机翻转、尺寸缩放、随机裁剪等操作。同时强调了在将numpy数组转换为Tensor时可能遇到的问题,如transpose后的shape转换以及数据类型的调整。此外,还提到了数据载入过程中图像和标签的正确处理方式,如将图像从float64转换为float32,标签由int转为long。
摘要由CSDN通过智能技术生成
1 导入库
import torch
import torchvision.transforms as transforms
import os
import numpy as np
from skimage import io, transform
from torch.utils.data import Dataset,DataLoader
2 数据增强【调用numpy的函数,数据类型是np.ndarray】

1)水平随机翻转

class RandomHorizontalFlip():
    """
    Args:  p (float): probability of the image being flipped. Default value is 0.5
    """
    def __init__(self, p=0.5):
        self.p = p

    def __call__(self, image):
        """
        input: image (array): Image to be flipped.
        Returns: image(array): Randomly flipped image.
        """
        if torch.rand(1) < self.p:
            return np.fliplr(image)
        return image

2)垂直随机翻转

class RandomVerticleFlip():
    """
    Args:  p (float): probability of the image being flipped. Default value is 0.5
    """
    def __init__(self, p=0.5):
        self.p = p

    def __call__(self, image):
        """
        input: image (array): Image to be flipped.
        Returns: image(array): Randomly flipped image.
        """
        if torch.rand(1) < self.p:
            return np.flipup(image)
        return image载入数据

3)改变尺寸,缩放

class Rescale(object):#1)
    "use this class before 'To tensor'"
    '''
    size:接受一个元组(a,b)
    factor:int或者float,<1表示缩小,>1表示放大
    '''
    #将其短边统一变成600
    def __init__(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值