zoukankan      html  css  js  c++  java
  • python常用转换numpy和PIL互转,tensor和numpy互转

    Deeplearning中常用转换速查

    1、numpy和PIL互转

    from PIL import Image
    import numpy as np
    import cv2
    img = cv2.imread('image.jpg')
    np.size(img,0) #0,1,2
    print(type(img))
    
    #numpy to PIL
    pil_img= Image.fromarray(img)
    print(type(pil_img))
    
    #PIL to numpy
    np_img= np.array(pil_img)
    print(type(np_img))

    2、tensor(pytorch)和numpy互转

    import torch
    import numpy as np
    
    #tensor to numpy
    torch_img = torch.ones(5)
    print(torch_img)
    torch_img+=1
    np_img = torch_img.numpy()
    print(torch_img, np_img)
    
    #numpy to tensor
    np_img = np.ones(9)
    print(np_img)
    torch_img = torch.from_numpy(np_img)
    np_img+=1
    print(np_img, torch_img)
    #tensor to numpy
  • 相关阅读:
    poj 2251
    poj 1321
    poj 2777
    poj 3468
    poj 2318
    javascript
    buhui
    swift 构造器
    mac上不了网
    字体
  • 原文地址:https://www.cnblogs.com/dzzy/p/15222222.html
Copyright © 2011-2022 走看看