img should be PIL Image. Got <class ‘numpy.ndarray‘>

x33g5p2x  于2021-12-05 转载在 Go  
字(0.8k)|赞(0)|评价(0)|浏览(616)

img should be PIL Image. Got <class 'numpy.ndarray'>

解决方法1:

from PIL import Image PIL_image = Image.fromarray(ndarray_image) #这里ndarray_image为原来的numpy数组类型的输入

解决方法2:

import math
import random

import torch

# -*- coding: utf-8 -*-

import numpy as np
import cv2
from torchvision.transforms import transforms

def __resize(img):
    data=random.randrange(84,128,1)
    temp=img.resize((data,data))
    tempp=temp.resize((128,128))
    return tempp
transformx=transforms.Lambda(lambda img: __resize(img))

if __name__ == '__main__':
     train_transform = transforms.Compose([
          transforms.RandomChoice([transforms.ToPILImage(),
          transforms.ColorJitter(brightness=(0.6, 1.4)),
          transforms.RandomResizedCrop((128, 128), scale=(0.9, 1.0), ratio=(1.0, 1.0), interpolation=3), transforms.RandomApply([transformx], p=0.15)]), transforms.ToTensor(),
          transforms.Normalize(mean=0.5, std=0.5)])

相关文章