最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PyTorch如何創(chuàng)建自己的數(shù)據(jù)集

 更新時間:2022年11月28日 15:06:09   作者:ZQ_ZHU  
這篇文章主要介紹了PyTorch如何創(chuàng)建自己的數(shù)據(jù)集,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

PyTorch創(chuàng)建自己的數(shù)據(jù)集

圖片文件在同一的文件夾下

思路是繼承 torch.utils.data.Dataset,并重點重寫其 __getitem__方法,示例代碼如下:

class ImageFolder(Dataset):
? ? def __init__(self, folder_path):
? ? ? ? self.files = sorted(glob.glob('%s/*.*' % folder_path))

? ? def __getitem__(self, index):
? ? ? ? path = self.files[index % len(self.files)]
? ? ? ? img = np.array(Image.open(path))
? ? ? ? h, w, c = img.shape
? ? ? ? pad = ((40, 40), (4, 4), (0, 0))

? ? ? ? # img = np.pad(img, pad, 'constant', constant_values=0) / 255
? ? ? ? img = np.pad(img, pad, mode='edge') / 255.0
? ? ? ? img = torch.from_numpy(img).float()
? ? ? ? patches = np.reshape(img, (3, 10, 128, 11, 128))
? ? ? ? patches = np.transpose(patches, (0, 1, 3, 2, 4))

? ? ? ? return img, patches, path

? ? def __len__(self):
? ? ? ? return len(self.files)

圖片文件在不同的文件夾下

比如我們有數(shù)據(jù)如下:

─── data
├── train
│ ├── 0.jpg
│ └── 1.jpg
├── test
│ ├── 0.jpg
│ └── 1.jpg
└── val
├── 1.jpg
└── 2.jpg

此時我們只需要將以上代碼稍作修改即可,修改的代碼如下:

self.files = sorted(glob.glob('%s/**/*.*' % folder_path, recursive=True))

其他代碼不變。

pytorch常用數(shù)據(jù)集的使用

對于pytorch數(shù)據(jù)集的使用,示例代碼如下:

from torch.utils.tensorboard import SummaryWriter
from torchvision.transforms import Compose
from torchvision import transforms
import torchvision
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

dataset_transform = Compose([transforms.ToTensor()])


# 關(guān)于官方數(shù)據(jù)集的使用還是關(guān)鍵要看pytorch的官方文檔
train_set = torchvision.datasets.CIFAR10(root="./CIFAR10",train=True,transform=dataset_transform,download=True)
test_set = torchvision.datasets.CIFAR10(root="./CIFAR10",train=False,transform=dataset_transform,download=True)

# 查看測試數(shù)據(jù)集中的第一個數(shù)據(jù)
# print(test_set[0])
# 查看測試數(shù)據(jù)集中的分類情況
# print(test_set.classes)
#
# 取出第一個數(shù)據(jù)中的圖片(img)和分類結(jié)果(target)
# img,target = test_set[0]
# 查看圖片數(shù)據(jù)的類型
# print(img)
# print(target)
# 輸出類別
# print(test_set.classes[target])
# 查看圖片
# img.show()

# 使用tensorboard顯示tensor數(shù)據(jù)類型的圖片
writer = SummaryWriter("logs")
for i in range(10):
	# 取出數(shù)據(jù)中的圖片(img)和分類結(jié)果(target)
    img,target = test_set[i]
    writer.add_image("test_set",img,i)

writer.close()

上述代碼運行結(jié)果在tensorboard可視化:

代碼

train_set = torchvision.datasets.CIFAR10(root="./CIFAR10",train=True,transform=dataset_transform,download=True)

常用參數(shù)講解

  • root:根目錄,存放數(shù)據(jù)集的位置
  • train:若為True,則劃分為訓(xùn)練數(shù)據(jù)集,若為False,則劃分為測試數(shù)據(jù)集
  • transform:指定輸入數(shù)據(jù)集處理方式
  • download:若為True,則會將數(shù)據(jù)集下載到root指定的目錄下,否則不會下載

官方文檔對參數(shù)的解釋:

root (string) – Root directory of dataset where directory cifar-10-batches-py exists or will be saved to if download is set to True.

train (bool, optional) – If True, creates dataset from training set, otherwise creates from test set.

transform (callable, optional) – A function/transform that takes in an PIL image and returns a transformed version. E.g, transforms.RandomCrop

target_transform (callable, optional) – A function/transform that takes in the target and transforms it.

download (bool, optional) – If true, downloads the dataset from the internet and puts it in root directory. If dataset is already downloaded, it is not downloaded again.

注意:

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Pytorch之保存讀取模型實例

    Pytorch之保存讀取模型實例

    今天小編就為大家分享一篇Pytorch之保存讀取模型實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python中pyplot直方圖的繪制方式

    python中pyplot直方圖的繪制方式

    這篇文章主要介紹了python中pyplot直方圖的繪制方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • python爬蟲 requests-html的使用

    python爬蟲 requests-html的使用

    這篇文章主要介紹了python爬蟲 requests-html的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • python調(diào)用win32接口進行截圖的示例

    python調(diào)用win32接口進行截圖的示例

    這篇文章主要介紹了python調(diào)用win32接口進行截圖的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-11-11
  • 完美解決pycharm導(dǎo)入自己寫的py文件爆紅問題

    完美解決pycharm導(dǎo)入自己寫的py文件爆紅問題

    今天小編就為大家分享一篇完美解決pycharm導(dǎo)入自己寫的py文件爆紅問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • 用Python實現(xiàn)zip密碼破解實例

    用Python實現(xiàn)zip密碼破解實例

    大家好,本篇文章主要講的是用Python實現(xiàn)zip密碼破解實例,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • python 遠程統(tǒng)計文件代碼分享

    python 遠程統(tǒng)計文件代碼分享

    享一個Python獲取遠程文件大小的函數(shù)代碼,簡單實用,是學(xué)習(xí)Python編程的基礎(chǔ)實例。
    2015-05-05
  • 利用Python創(chuàng)建位置生成器的示例詳解

    利用Python創(chuàng)建位置生成器的示例詳解

    在這篇文章中,我們將探索如何利用Python在美國各地城市的地圖數(shù)據(jù)和公共電動自行車訂閱源上訓(xùn)練一個快速生成的對抗網(wǎng)絡(luò)(GAN)模型,需要的可以參考一下
    2022-06-06
  • Python PaddlePaddle機器學(xué)習(xí)之求解線性模型

    Python PaddlePaddle機器學(xué)習(xí)之求解線性模型

    這篇文章主要介紹了Python PaddlePaddle機器學(xué)習(xí)之求解線性模型,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • 使用Protocol Buffers的C語言拓展提速Python程序的示例

    使用Protocol Buffers的C語言拓展提速Python程序的示例

    這篇文章主要介紹了使用Protocol Buffers的C語言拓展提速Python程序的示例,使用C拓展Python是Python編程進階中的重要技巧,需要的朋友可以參考下
    2015-04-04

最新評論

商城县| 西乌珠穆沁旗| 文成县| 乳源| 峡江县| 汤阴县| 咸宁市| 开江县| 玛纳斯县| 东海县| 太和县| 平陆县| 舒兰市| 澎湖县| 高要市| 宁海县| 隆安县| 宣恩县| 大同市| 北川| 尼玛县| 长丰县| 阿城市| 南澳县| 罗田县| 霍邱县| 旬邑县| 五莲县| 贺兰县| 荔浦县| 万源市| 衡水市| 洪湖市| 临漳县| 东方市| 望江县| 灵山县| 革吉县| 古田县| 宁阳县| 沅江市|