Python機(jī)器學(xué)習(xí)之基于Pytorch實(shí)現(xiàn)貓狗分類
一、環(huán)境配置
安裝Anaconda
具體安裝過(guò)程,請(qǐng)點(diǎn)擊本文
配置Pytorch
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple torch pip install -i https://pypi.tuna.tsinghua.edu.cn/simple torchvision
二、數(shù)據(jù)集的準(zhǔn)備
1.數(shù)據(jù)集的下載
kaggle網(wǎng)站的數(shù)據(jù)集下載地址:
https://www.kaggle.com/lizhensheng/-2000
2.數(shù)據(jù)集的分類
將下載的數(shù)據(jù)集進(jìn)行解壓操作,然后進(jìn)行分類
分類如下(每個(gè)文件夾下包括cats和dogs文件夾)
三、貓狗分類的實(shí)例
導(dǎo)入相應(yīng)的庫(kù)
# 導(dǎo)入庫(kù) import torch.nn.functional as F import torch.optim as optim import torch import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data import torch.utils.data.distributed import torchvision.transforms as transforms import torchvision.datasets as datasets
設(shè)置超參數(shù)
# 設(shè)置超參數(shù)
#每次的個(gè)數(shù)
BATCH_SIZE = 20
#迭代次數(shù)
EPOCHS = 10
#采用cpu還是gpu進(jìn)行計(jì)算
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
圖像處理與圖像增強(qiáng)
# 數(shù)據(jù)預(yù)處理
transform = transforms.Compose([
transforms.Resize(100),
transforms.RandomVerticalFlip(),
transforms.RandomCrop(50),
transforms.RandomResizedCrop(150),
transforms.ColorJitter(brightness=0.5, contrast=0.5, hue=0.5),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
讀取數(shù)據(jù)集和導(dǎo)入數(shù)據(jù)
# 讀取數(shù)據(jù)
dataset_train = datasets.ImageFolder('E:\\Cat_And_Dog\\kaggle\\cats_and_dogs_small\\train', transform)
print(dataset_train.imgs)
# 對(duì)應(yīng)文件夾的label
print(dataset_train.class_to_idx)
dataset_test = datasets.ImageFolder('E:\\Cat_And_Dog\\kaggle\\cats_and_dogs_small\\validation', transform)
# 對(duì)應(yīng)文件夾的label
print(dataset_test.class_to_idx)
# 導(dǎo)入數(shù)據(jù)
train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=BATCH_SIZE, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset_test, batch_size=BATCH_SIZE, shuffle=True)
定義網(wǎng)絡(luò)模型
# 定義網(wǎng)絡(luò)
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 3)
self.max_pool1 = nn.MaxPool2d(2)
self.conv2 = nn.Conv2d(32, 64, 3)
self.max_pool2 = nn.MaxPool2d(2)
self.conv3 = nn.Conv2d(64, 64, 3)
self.conv4 = nn.Conv2d(64, 64, 3)
self.max_pool3 = nn.MaxPool2d(2)
self.conv5 = nn.Conv2d(64, 128, 3)
self.conv6 = nn.Conv2d(128, 128, 3)
self.max_pool4 = nn.MaxPool2d(2)
self.fc1 = nn.Linear(4608, 512)
self.fc2 = nn.Linear(512, 1)
def forward(self, x):
in_size = x.size(0)
x = self.conv1(x)
x = F.relu(x)
x = self.max_pool1(x)
x = self.conv2(x)
x = F.relu(x)
x = self.max_pool2(x)
x = self.conv3(x)
x = F.relu(x)
x = self.conv4(x)
x = F.relu(x)
x = self.max_pool3(x)
x = self.conv5(x)
x = F.relu(x)
x = self.conv6(x)
x = F.relu(x)
x = self.max_pool4(x)
# 展開(kāi)
x = x.view(in_size, -1)
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
x = torch.sigmoid(x)
return x
modellr = 1e-4
# 實(shí)例化模型并且移動(dòng)到GPU
model = ConvNet().to(DEVICE)
# 選擇簡(jiǎn)單暴力的Adam優(yōu)化器,學(xué)習(xí)率調(diào)低
optimizer = optim.Adam(model.parameters(), lr=modellr)
調(diào)整學(xué)習(xí)率
def adjust_learning_rate(optimizer, epoch):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
modellrnew = modellr * (0.1 ** (epoch // 5))
print("lr:",modellrnew)
for param_group in optimizer.param_groups:
param_group['lr'] = modellrnew
定義訓(xùn)練過(guò)程
# 定義訓(xùn)練過(guò)程
def train(model, device, train_loader, optimizer, epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device).float().unsqueeze(1)
optimizer.zero_grad()
output = model(data)
# print(output)
loss = F.binary_cross_entropy(output, target)
loss.backward()
optimizer.step()
if (batch_idx + 1) % 10 == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, (batch_idx + 1) * len(data), len(train_loader.dataset),
100. * (batch_idx + 1) / len(train_loader), loss.item()))
# 定義測(cè)試過(guò)程
def val(model, device, test_loader):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device).float().unsqueeze(1)
output = model(data)
# print(output)
test_loss += F.binary_cross_entropy(output, target, reduction='mean').item()
pred = torch.tensor([[1] if num[0] >= 0.5 else [0] for num in output]).to(device)
correct += pred.eq(target.long()).sum().item()
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
定義保存模型和訓(xùn)練
# 訓(xùn)練
for epoch in range(1, EPOCHS + 1):
adjust_learning_rate(optimizer, epoch)
train(model, DEVICE, train_loader, optimizer, epoch)
val(model, DEVICE, test_loader)
torch.save(model, 'E:\\Cat_And_Dog\\kaggle\\model.pth')
訓(xùn)練結(jié)果
四、實(shí)現(xiàn)分類預(yù)測(cè)測(cè)試
準(zhǔn)備預(yù)測(cè)的圖片進(jìn)行測(cè)試
from __future__ import print_function, division
from PIL import Image
from torchvision import transforms
import torch.nn.functional as F
import torch
import torch.nn as nn
import torch.nn.parallel
# 定義網(wǎng)絡(luò)
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 3)
self.max_pool1 = nn.MaxPool2d(2)
self.conv2 = nn.Conv2d(32, 64, 3)
self.max_pool2 = nn.MaxPool2d(2)
self.conv3 = nn.Conv2d(64, 64, 3)
self.conv4 = nn.Conv2d(64, 64, 3)
self.max_pool3 = nn.MaxPool2d(2)
self.conv5 = nn.Conv2d(64, 128, 3)
self.conv6 = nn.Conv2d(128, 128, 3)
self.max_pool4 = nn.MaxPool2d(2)
self.fc1 = nn.Linear(4608, 512)
self.fc2 = nn.Linear(512, 1)
def forward(self, x):
in_size = x.size(0)
x = self.conv1(x)
x = F.relu(x)
x = self.max_pool1(x)
x = self.conv2(x)
x = F.relu(x)
x = self.max_pool2(x)
x = self.conv3(x)
x = F.relu(x)
x = self.conv4(x)
x = F.relu(x)
x = self.max_pool3(x)
x = self.conv5(x)
x = F.relu(x)
x = self.conv6(x)
x = F.relu(x)
x = self.max_pool4(x)
# 展開(kāi)
x = x.view(in_size, -1)
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
x = torch.sigmoid(x)
return x
# 模型存儲(chǔ)路徑
model_save_path = 'E:\\Cat_And_Dog\\kaggle\\model.pth'
# ------------------------ 加載數(shù)據(jù) --------------------------- #
# Data augmentation and normalization for training
# Just normalization for validation
# 定義預(yù)訓(xùn)練變換
# 數(shù)據(jù)預(yù)處理
transform_test = transforms.Compose([
transforms.Resize(100),
transforms.RandomVerticalFlip(),
transforms.RandomCrop(50),
transforms.RandomResizedCrop(150),
transforms.ColorJitter(brightness=0.5, contrast=0.5, hue=0.5),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
class_names = ['cat', 'dog'] # 這個(gè)順序很重要,要和訓(xùn)練時(shí)候的類名順序一致
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# ------------------------ 載入模型并且訓(xùn)練 --------------------------- #
model = torch.load(model_save_path)
model.eval()
# print(model)
image_PIL = Image.open('E:\\Cat_And_Dog\\kaggle\\cats_and_dogs_small\\test\\cats\\cat.1500.jpg')
#
image_tensor = transform_test(image_PIL)
# 以下語(yǔ)句等效于 image_tensor = torch.unsqueeze(image_tensor, 0)
image_tensor.unsqueeze_(0)
# 沒(méi)有這句話會(huì)報(bào)錯(cuò)
image_tensor = image_tensor.to(device)
out = model(image_tensor)
pred = torch.tensor([[1] if num[0] >= 0.5 else [0] for num in out]).to(device)
print(class_names[pred])
預(yù)測(cè)結(jié)果


實(shí)際訓(xùn)練的過(guò)程來(lái)看,整體看準(zhǔn)確度不高。而經(jīng)過(guò)測(cè)試發(fā)現(xiàn),該模型只能對(duì)于貓進(jìn)行識(shí)別,對(duì)于狗則會(huì)誤判。
五、參考資料
到此這篇關(guān)于Python機(jī)器學(xué)習(xí)之基于Pytorch實(shí)現(xiàn)貓狗分類的文章就介紹到這了,更多相關(guān)Pytorch實(shí)現(xiàn)貓狗分類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python+selenium對(duì)table表和分頁(yè)處理
這篇文章主要介紹了python+selenium對(duì)table表和分頁(yè)處理,文章內(nèi)容只要包括bulabula2022、table表分頁(yè)處理、網(wǎng)頁(yè)table所有內(nèi)容循環(huán)處理等相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-01-01
python 彈窗提示警告框MessageBox的實(shí)例
今天小編就為大家分享一篇python 彈窗提示警告框MessageBox的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
Python下實(shí)現(xiàn)的RSA加密/解密及簽名/驗(yàn)證功能示例
這篇文章主要介紹了Python下實(shí)現(xiàn)的RSA加密/解密及簽名/驗(yàn)證功能,結(jié)合具體實(shí)例形式分析了Python中RSA加密、解密的實(shí)現(xiàn)方法及簽名、驗(yàn)證功能的使用技巧,需要的朋友可以參考下2017-07-07
python實(shí)戰(zhàn)游戲之史上最難最虐的掃雷游戲沒(méi)有之一
這篇文章主要介紹了使用 python 實(shí)現(xiàn)掃雷游戲,不同于傳統(tǒng)過(guò)時(shí)的掃雷,今天我們用 Python 增加了新花樣,文中給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
python項(xiàng)目--使用Tkinter的日歷GUI應(yīng)用程序
在 Python 中,我們可以使用 Tkinter 制作 GUI。如果你非常有想象力和創(chuàng)造力,你可以用 Tkinter 做出很多有趣的東西,希望本篇文章能夠幫到你2021-08-08
PyTorch之torch.randn()如何創(chuàng)建正態(tài)分布隨機(jī)數(shù)
這篇文章主要介紹了PyTorch之torch.randn()如何創(chuàng)建正態(tài)分布隨機(jī)數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
python cookie反爬處理的實(shí)現(xiàn)
這篇文章主要介紹了python cookie反爬處理的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
python實(shí)現(xiàn)將列表中各個(gè)值快速賦值給多個(gè)變量
這篇文章主要介紹了python實(shí)現(xiàn)將列表中各個(gè)值快速賦值給多個(gè)變量,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04

