亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解Pytorch中Dataset的使用

 更新時間:2022年12月29日 14:48:53   作者:LRJ-jonas  
這篇文章主要為大家詳細(xì)介紹了如何加載并處理TorchVision的FashionMNIST Dataset,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下

此案例教我們加載并處理TorchVision的FashionMNIST Dataset

root 目錄是 train/test data 存儲的地方

download=True 如果root目錄沒有,則從網(wǎng)上下載

transform and target_transform specify the feature and label transformations

import torch
from torch.utils.data import Dataset
from torchvision import datasets
from torchvision.transforms import ToTensor
import matplotlib.pyplot as plt
 
 
training_data = datasets.FashionMNIST(
    root="data",
    train=True,
    download=True,
    transform=ToTensor()
)
 
test_data = datasets.FashionMNIST(
    root="data",
    train=False,
    download=True,
    transform=ToTensor()
)

運行得到的結(jié)果是這樣的:

遍歷并可視化數(shù)據(jù)集

給數(shù)據(jù)集手動加上序號sample_idx,并用matplotlib進行繪制:

labels_map = {
    0: "T-Shirt",
    1: "Trouser",
    2: "Pullover",
    3: "Dress",
    4: "Coat",
    5: "Sandal",
    6: "Shirt",
    7: "Sneaker",
    8: "Bag",
    9: "Ankle Boot",
}
figure = plt.figure(figsize=(8, 8))
cols, rows = 3, 3
for i in range(1, cols * rows + 1):
    sample_idx = torch.randint(len(training_data), size=(1,)).item()
    img, label = training_data[sample_idx]
    figure.add_subplot(rows, cols, i)
    plt.title(labels_map[label])
    plt.axis("off")
    plt.imshow(img.squeeze(), cmap="gray")
plt.show()

traning_data

torch.randint(len(training_data), size=(1,)).item()

為我的文件自定義一個Dataset

一個自定義的Dataset必須有三個函數(shù):__init__, __len__, and __getitem__

圖片存儲在img_dir

import os
import pandas as pd
from torchvision.io import read_image
 
class CustomImageDataset(Dataset):
    def __init__(self, annotations_file, img_dir, transform=None, target_transform=None):
        self.img_labels = pd.read_csv(annotations_file)
        self.img_dir = img_dir
        self.transform = transform
        self.target_transform = target_transform
 
    def __len__(self):
        return len(self.img_labels)
 
    def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx, 0])
        image = read_image(img_path)
        label = self.img_labels.iloc[idx, 1]
        if self.transform:
            image = self.transform(image)
        if self.target_transform:
            label = self.target_transform(label)
        return image, label

到此這篇關(guān)于詳解Pytorch中Dataset的使用的文章就介紹到這了,更多相關(guān)Pytorch Dataset使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Flask搭建Web應(yīng)用程序的方法示例

    Flask搭建Web應(yīng)用程序的方法示例

    Flask是一個使用Python編寫的輕量級Web應(yīng)用框架,本文我們將介紹一個使用Flask逐步搭建Web應(yīng)用程序的簡單入門示例,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • pytorch單元測試的實現(xiàn)示例

    pytorch單元測試的實現(xiàn)示例

    單元測試是一種軟件測試方法,本文主要介紹了pytorch單元測試的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-04-04
  • python裝飾器初探(推薦)

    python裝飾器初探(推薦)

    下面小編就為大家?guī)硪黄猵ython裝飾器初探(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-07-07
  • Python函數(shù)可變參數(shù)定義及其參數(shù)傳遞方式實例詳解

    Python函數(shù)可變參數(shù)定義及其參數(shù)傳遞方式實例詳解

    這篇文章主要介紹了Python函數(shù)可變參數(shù)定義及其參數(shù)傳遞方式,以實例形式較為詳細(xì)的分析了Python函數(shù)參數(shù)的使用技巧,需要的朋友可以參考下
    2015-05-05
  • Python 必須了解的5種高級特征

    Python 必須了解的5種高級特征

    Python 多好用不用多說,大家看看自己用的語言就知道了。但是 Python 隱藏的高級功能你都 get 了嗎?本文中,作者列舉了 Python 中五種略高級的特征以及它們的使用方法,快來一探究竟吧!
    2020-09-09
  • Flask框架路由和視圖用法實例分析

    Flask框架路由和視圖用法實例分析

    這篇文章主要介紹了Flask框架路由和視圖用法,結(jié)合實例形式分析了Flask路由和視圖相關(guān)原理、定義與使用方法,需要的朋友可以參考下
    2019-11-11
  • 基于Python爬取51cto博客頁面信息過程解析

    基于Python爬取51cto博客頁面信息過程解析

    這篇文章主要介紹了基于Python爬取51cto博客頁面信息過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • 最新評論