<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
import os.path from os import listdir import numpy as np import pandas as pd from PIL import Image import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from torch.nn import AdaptiveAvgPool2d from torch.utils.data.sampler import SubsetRandomSampler from torch.utils.data import Dataset import torchvision.transforms as transforms from sklearn.model_selection import train_test_split
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') normalize = transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] ) transform = transforms.Compose([transforms.ToTensor(), normalize]) # 轉換
class DogDataset(Dataset): # 定義變數 def __init__(self, img_paths, img_labels, size_of_images): self.img_paths = img_paths self.img_labels = img_labels self.size_of_images = size_of_images # 多少長圖片 def __len__(self): return len(self.img_paths) # 開啟每組圖片並處理每張圖片 def __getitem__(self, index): PIL_IMAGE = Image.open(self.img_paths[index]).resize(self.size_of_images) TENSOR_IMAGE = transform(PIL_IMAGE) label = self.img_labels[index] return TENSOR_IMAGE, label print(len(listdir(r'C:UsersAIAXITDesktopDeepLearningProjectDeep_Learning_Datadog-breed-identificationtrain'))) print(len(pd.read_csv(r'C:UsersAIAXITDesktopDeepLearningProjectDeep_Learning_Datadog-breed-identificationlabels.csv'))) print(len(listdir(r'C:UsersAIAXITDesktopDeepLearningProjectDeep_Learning_Datadog-breed-identificationtest'))) train_paths = [] test_paths = [] labels = [] # 訓練集圖片路徑 train_paths_lir = r'C:UsersAIAXITDesktopDeepLearningProjectDeep_Learning_Datadog-breed-identificationtrain' for path in listdir(train_paths_lir): train_paths.append(os.path.join(train_paths_lir, path)) # 測試集圖片路徑 labels_data = pd.read_csv(r'C:UsersAIAXITDesktopDeepLearningProjectDeep_Learning_Datadog-breed-identificationlabels.csv') labels_data = pd.DataFrame(labels_data) # 把字元標籤離散化,因為資料有120種狗,不離散化後面把資料給模型時會報錯:字元標籤過多。把字元標籤從0-119編號 size_mapping = {} value = 0 size_mapping = dict(labels_data['breed'].value_counts()) for kay in size_mapping: size_mapping[kay] = value value += 1 # print(size_mapping) labels = labels_data['breed'].map(size_mapping) labels = list(labels) # print(labels) print(len(labels)) # 劃分訓練集和測試集 X_train, X_test, y_train, y_test = train_test_split(train_paths, labels, test_size=0.2) train_set = DogDataset(X_train, y_train, (32, 32)) test_set = DogDataset(X_test, y_test, (32, 32)) train_loader = torch.utils.data.DataLoader(train_set, batch_size=64) test_loader = torch.utils.data.DataLoader(test_set, batch_size=64)
class LeNet(nn.Module): def __init__(self): super(LeNet, self).__init__() self.features = nn.Sequential( nn.Conv2d(in_channels=3, out_channels=6, kernel_size=5), nn.ReLU(), nn.AvgPool2d(kernel_size=2, stride=2), nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5), nn.ReLU(), nn.AvgPool2d(kernel_size=2, stride=2) ) self.classifier = nn.Sequential( nn.Linear(16 * 5 * 5, 120), nn.ReLU(), nn.Linear(120, 84), nn.ReLU(), nn.Linear(84, 120) ) def forward(self, x): batch_size = x.shape[0] x = self.features(x) x = x.view(batch_size, -1) x = self.classifier(x) return x model = LeNet().to(device) criterion = nn.CrossEntropyLoss().to(device) optimizer = optim.Adam(model.parameters()) TRAIN_LOSS = [] # 損失 TRAIN_ACCURACY = [] # 準確率
def train(epoch): model.train() epoch_loss = 0.0 # 損失 correct = 0 # 精確率 for batch_index, (Data, Label) in enumerate(train_loader): # 扔到GPU中 Data = Data.to(device) Label = Label.to(device) output_train = model(Data) # 計算損失 loss_train = criterion(output_train, Label) epoch_loss = epoch_loss + loss_train.item() # 計算精確率 pred = torch.max(output_train, 1)[1] train_correct = (pred == Label).sum() correct = correct + train_correct.item() # 梯度歸零、反向傳播、更新引數 optimizer.zero_grad() loss_train.backward() optimizer.step() print('Epoch: ', epoch, 'Train_loss: ', epoch_loss / len(train_set), 'Train correct: ', correct / len(train_set))
和訓練集差不多。
def test(): model.eval() correct = 0.0 test_loss = 0.0 with torch.no_grad(): for Data, Label in test_loader: Data = Data.to(device) Label = Label.to(device) test_output = model(Data) loss = criterion(test_output, Label) pred = torch.max(test_output, 1)[1] test_correct = (pred == Label).sum() correct = correct + test_correct.item() test_loss = test_loss + loss.item() print('Test_loss: ', test_loss / len(test_set), 'Test correct: ', correct / len(test_set))
epoch = 10 for n_epoch in range(epoch): train(n_epoch) test()
到此這篇關於pytorch實現影象識別(實戰)的文章就介紹到這了,更多相關pytorch實現影象識別內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45