目錄
1. 整體流程
1)實體化模型
2)加載模型
3)輸入影像
4)輸出分類結果
5)完整代碼
2. 處理影像
1) How can i convert an RGB image into grayscale in Python?
2)PIL 處理影像的基本操作
3)影像通道數的理解
4)Convert 3 channel image to 2 channel
5)影像通道轉換
6)將所有的影像合并為一個numpy陣列
7)torch.from_numpy VS torch.Tensor
8)torch.squeeze() 和torch.unsqueeze()
3.issue
1)TypeError: 'module' object is not callable
2)TypeError: 'collections.OrderedDict' object is not callable
3) TypeError: __init__() missing 1 required positional argument: 'XX'
4) RuntimeError: Error(s) in loading state_dict for PythonNet: Missing key(s) in state_dict:
5) RuntimeError: Expected 4-dimensional input for 4-dimensional weight [128, 1, 3, 3], but got 2-dimensional input of size [480, 640] instead
6)RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same or input should be a MKLDNN tensor and weight is a dense tensor
1. 整體流程
1)實體化模型
Assume that the content of YourClass.py is:
class YourClass:
# ......
If you use:
from YourClassParentDir import YourClass # means YourClass
from model import PythonNet
net= PythonNet(T=16).eval().cuda()
2)加載模型
import torch
net.load_state_dict(torch.load('checkpoint_max.pth'),False)
3)輸入影像
目的:從檔案夾中加載所有影像組合為一個 numpy array 作為模型輸入
原始影像輸入維度:(480,640)
目標影像輸入維度:(16,1,128,128)
import glob
from PIL import Image
import numpy as np
from torch.autograd import Variable
#獲取影像路徑
filelist = glob.glob('./testdata/a/*.jpg')
#打開影像open('frame_path')--》轉換為灰度圖convert('L')--》縮放影像resize((width, height)) --》合并檔案夾中的所有影像為一個numpy array
x = np.array([np.array(Image.open(frame).convert('L').resize((128,128))) for frame in filelist])
#用torch.from_numpy這個方法將numpy類轉換成tensor類
x = torch.from_numpy(x).type(torch.FloatTensor).cuda()
#擴充資料維度
x = Variable(torch.unsqueeze(x,dim=1).float(),requires_grad=False)
4)輸出分類結果
outputs = net(x)
_, predicted = torch.max(outputs,1)
torch.max()這個函式回傳輸入張量中所有元素的最大值,
回傳一個命名元組(values,indices),其中values是給定維dim中輸入張量的每一行的最大值,indices是找到的每個最大值的索引位置(argmax),也就是說,回傳的第一個值是對應影像在類別中的最大概率值,第二個值是最大概率值的對應類別,
Pytorch 分類問題輸出結果的資料整理方式:_, predicted = torch.max(outputs.data, 1) - stardsd - 博客園
5)完整代碼
from PIL import Image
from torch.autograd import Variable
import numpy as np
import torch
import glob
from model import PythonNet
##############處理輸入影像#######################################
#獲取影像路徑
filelist = glob.glob('./testdata/a/*.jpg')
#打開影像open('frame_path')--》轉換為灰度圖convert('L')--》縮放影像resize((width, height)) --》合并檔案夾中的所有影像為一個numpy array
x = np.array([np.array(Image.open(frame).convert('L').resize((128,128))) for frame in filelist])
#用torch.from_numpy這個方法將numpy類轉換成tensor類
x = torch.from_numpy(x).type(torch.FloatTensor).cuda()
#擴充資料維度
x = Variable(torch.unsqueeze(x,dim=1).float(),requires_grad=False)
#############定義預測函式#######################################
def predict(x):
net= PythonNet(T=16).eval().cuda()
net.load_state_dict(torch.load('checkpoint_max.pth'),False)
outputs = net(x)
_, predicted = torch.max(outputs,1)
print("_:",_)
print("predicted:",predicted)
print("outputs:",outputs)
############輸入影像進行預測#####################################
predict(x)
2. 處理影像
1) How can i convert an RGB image into grayscale in Python?
matplotlib - How can I convert an RGB image into grayscale in Python? - Stack Overflow
2)PIL 處理影像的基本操作
python——PIL Image處理影像_aaon22357的博客-CSDN博客
3)影像通道數的理解
關于影像通道的理解 | TinaCristal's Blog
4)Convert 3 channel image to 2 channel
python - I have converted 3 channel RGB image into 2 channels grayscale image, How to decrease greyscale channels to 1? - Stack Overflow
5)影像通道轉換
影像通道轉換——從np.ndarray的[w, h, c]轉為Tensor的[c, w, h]_莫邪莫急的博客-CSDN博客
6)將所有的影像合并為一個numpy陣列
python — 如何在numpy陣列中加載多個影像?
7)torch.from_numpy VS torch.Tensor
torch.from_numpy VS torch.Tensor_麥克斯韋惡魔的博客-CSDN博客
8)torch.squeeze() 和torch.unsqueeze()
pytorch學習 中 torch.squeeze() 和torch.unsqueeze()的用法_xiexu911的博客-CSDN博客_torch.unsqueeze
3.issue
1)TypeError: 'module' object is not callable
python - TypeError: 'module' object is not callable - Stack Overflow
2)TypeError: 'collections.OrderedDict' object is not callable
pytorch加載模型報錯TypeError: ‘collections.OrderedDict‘ object is not callable_xiaoqiaoliushuiCC的博客-CSDN博客
3) TypeError: __init__() missing 1 required positional argument: 'XX'
Python成功解決TypeError: __init__() missing 1 required positional argument: ‘comment‘_肥鼠路易的博客-CSDN博客
4) RuntimeError: Error(s) in loading state_dict for PythonNet: Missing key(s) in state_dict:
pytorch加載模型報錯RuntimeError: Error(s) in loading state_dict for SSD:Missing key(s) in state_dict:... - 代碼先鋒網
5) RuntimeError: Expected 4-dimensional input for 4-dimensional weight [128, 1, 3, 3], but got 2-dimensional input of size [480, 640] instead
RuntimeError: Expected 4-dimensional input for 4-dimensional weight 64 3 3, but got 3-dimensional in_Steven_ycs的博客-CSDN博客
6)RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same or input should be a MKLDNN tensor and weight is a dense tensor
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.DoubleTensor) should be the_一千零一夜的博客-CSDN博客
??????
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/328225.html
標籤:其他
