直接上圖

選擇圖片,打開本地圖片,進行識別,這個識別訓練的是mnist資料集,所以這個白色區域內的數字會先經過黑白像素轉后,傳入訓練好的模型中識別,識別結果在右側顯示,
訓練的神經網路結構代碼如下:
class LeNet(nn.Module):
def __init__(self, num_classes=10):
super(LeNet, self).__init__()
self.conv1 = nn.Sequential( # input_size=(1*28*28)
nn.Conv2d(1, 6, 5, 1, 2), # padding=2保證輸入輸出尺寸相同
nn.ReLU(), # input_size=(6*28*28)
nn.MaxPool2d(kernel_size=2, stride=2), # output_size=(6*14*14)
)
self.conv2 = nn.Sequential(
nn.Conv2d(6, 16, 5),
nn.ReLU(), # input_size=(16*10*10)
nn.MaxPool2d(2, 2) # output_size=(16*5*5)
)
self.fc1 = nn.Sequential(
nn.Linear(16 * 5 * 5, 120),
nn.ReLU()
)
self.fc2 = nn.Sequential(
nn.Linear(120, 84),
nn.ReLU()
)
self.fc3 = nn.Linear(84, 10)
# 定義前向傳播程序,輸入為x
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
# nn.Linear()的輸入輸出都是維度為一的值,所以要把多維度的tensor展平成一維
x = x.view(x.size()[0], -1)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
return x
mnist手寫資料集影像大小都是28*28的大小,
有需要寫影像識別、Gan、nlp代碼的可以聯系我扣扣:334542894
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/291592.html
標籤:其他
上一篇:OpenCV中的快速特征檢測——FAST(Features from Accelerated Segment Test)
下一篇:LabVIEW目標物件分類識別
