zoukankan      html  css  js  c++  java
  • pytorch~改变tensor的shape

    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
    
    w_size = 1024
    h_size = 256
    input_label = torch.zeros([input_labe.shape[0],input_labe.shape[1],h_size,w_size], dtype=torch.float32,device=input_labe.device)
    for i in range(input_labe.shape[0]):            
         f_label = input_labe [i,:,:,:]
         f_label_img = tensor2im(f_label)
         f_label_img = cv2.resize(f_label_img,(w_size,h_size))
         input_label[i,:,:,:] = im2tensor(f_label_img, normalize=True)   
    

      

  • 相关阅读:
    [原创]MYSQL的简单入门
    [原创]关于ORACLE的使用入门
    [原创]关于数据库优化
    [原创]MYSQL中利用外键实现级联删除和更新
    [原创]mybatis详解说明
    [原创]关于mybatis中一级缓存和二级缓存的简单介绍
    [原创]mybatis中整合ehcache缓存框架的使用
    maven添加仓库没有的jar包
    PHP源码安装
    MySQL远程登陆解决
  • 原文地址:https://www.cnblogs.com/wjjcjj/p/14428457.html
Copyright © 2011-2022 走看看