欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-03-25 10:53:40
...

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)])