zoukankan      html  css  js  c++  java
  • pytorch,tensor2im,im2tensor

    def tensor2im(image_tensor, imtype=np.uint8, normalize=True):
        image_numpy = image_tensor.cpu().float().detach().numpy()
        if normalize:
            image_numpy = (image_numpy+1)*255.0*0.5
        else:
            image_numpy = (image_numpy+1)*255.0
        image_numpy = np.clip(image_numpy, 0, 255)    
        blank_image = np.zeros((image_tensor.shape[1],image_tensor.shape[2],image_tensor.shape[0]), np.uint8)
        if image_tensor.shape[0] == 3:  
            blank_image[:,:,0]=image_numpy[2,:,:]
            blank_image[:,:,1]=image_numpy[1,:,:]
            blank_image[:,:,2]=image_numpy[0,:,:]
        else:
            blank_image[:,:,:]=image_numpy[:,:,:]
        return blank_image
    def im2tensor(image_numpy, normalize=True):
        if normalize:
            image_numpy = (image_numpy/255.0)*2.0-1.0
        else:
            image_numpy = image_numpy/255.0
        image_numpy = np.clip(image_numpy, -1, 1)    
        blank_image = np.zeros((image_numpy.shape[2],image_numpy.shape[0],image_numpy.shape[1]))
        if image_numpy.shape[2] == 3:  
            blank_image[2,:,:]=image_numpy[:,:,0]
            blank_image[1,:,:]=image_numpy[:,:,1]
            blank_image[0,:,:]=image_numpy[:,:,2]
        else:
            blank_image[:,:,:]=image_numpy[:,:,:]
        image_tensor = torch.Tensor(blank_image)
        return image_tensor

    调用:

                f_fake_image = tensor2im(fake_image[ii])    
                new_fake_image[ii]=im2tensor(f_fake_image_new, normalize=True)
  • 相关阅读:
    JS判断对象中是否存在某参数
    JS通过url下载文件
    .NET CORE LinQ查询中计算时间差
    C# 判断某个时间是星期几
    C#数组去重
    python Tank
    kubeflannel.yml Tank
    片言只语 Tank
    other Tank
    ERROR大集合 Tank
  • 原文地址:https://www.cnblogs.com/wjjcjj/p/13807098.html
Copyright © 2011-2022 走看看