深度学习初学者,如何下载常用公开数据集并使用呢?
深度學(xué)習(xí)初學(xué)者,如何下載常用公開數(shù)據(jù)集并使用呢?
- 1.前言
- 2.官方文檔怎樣看
- 3.動(dòng)手寫代碼
- 4.如何可視化
- 遇到問(wèn)題:ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1131)
1.前言
剛開始進(jìn)行深度學(xué)習(xí)的時(shí)候,難免要用到一些公開數(shù)據(jù)集,現(xiàn)在閑來(lái)無(wú)事,記錄一下如何快速下載一些經(jīng)典數(shù)據(jù)集。通過(guò)官方文檔學(xué)習(xí),是一些大牛們掛在嘴邊經(jīng)常推薦的方法,那么我們本篇博客就從官方文檔開始學(xué)習(xí)。
因?yàn)槲沂亲鯟V方向的,所以用TorchVision這個(gè)庫(kù)舉例。來(lái)自官網(wǎng):This library is part of the [PyTorch](http://pytorch.org/) project. PyTorch is an open source machine learning framework.
The [torchvision] package consists of popular datasets, model architectures, and common image transformations for computer vision.
包括很多流行數(shù)據(jù)集,如我們常見的CIFAR,COCO和MINST,大家應(yīng)該都不陌生。一會(huì)兒會(huì)以CIFAR舉例,記錄一下我的過(guò)程。
2.官方文檔怎樣看
首先我們看一下CIFAR這個(gè)類的文檔:
參數(shù):
root:表示將下載的數(shù)據(jù)集放在哪個(gè)目錄
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:是否為訓(xùn)練數(shù)據(jù)集
train (bool, optional): If True, creates dataset from training set, otherwise creates from test set.transform:一個(gè)將圖像進(jìn)行預(yù)處理、返回transform的函數(shù)
A function/transform that takes in an PIL image and returns a transformed version.download:是否下載數(shù)據(jù)集,
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.3.動(dòng)手寫代碼
示例代碼
# 導(dǎo)入torchvision包 import torchvision# 對(duì)原始圖像進(jìn)行數(shù)據(jù)處理的函數(shù) dataset_transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor() ])# 生成訓(xùn)練數(shù)據(jù)集和測(cè)試數(shù)據(jù)集 # 訓(xùn)練數(shù)據(jù)集 存放在根目錄的dataset文件夾下,作為訓(xùn)練數(shù)據(jù)集,并下載 train_set = torchvision.datasets.CIFAR10(root="./dataset", train=True, transform=dataset_transform, download=True) # 測(cè)試數(shù)據(jù)集 存放在根目錄的dataset文件夾下,不作為訓(xùn)練數(shù)據(jù)集,并下載 test_set = torchvision.datasets.CIFAR10(root="./dataset", train=False, transform=dataset_transform, download=True)print(test_set[0])然后我們右鍵運(yùn)行,進(jìn)行下載
可以看到數(shù)據(jù)集已經(jīng)開始下載了,但是因?yàn)槭菑膖oronto.edu下載,速度很慢。教你一個(gè)更快的方法:我們終止運(yùn)行,復(fù)制這個(gè)鏈接,用迅雷下載,很快就好了。然后將下載好的.gz文件進(jìn)行解壓,放到我們創(chuàng)建的dataset目錄下:
重新run,就可以正常使用數(shù)據(jù)集了。
4.如何可視化
我用tensorboard進(jìn)行了可視化,大家感興趣可以研究一下tensorboard這個(gè)庫(kù)。
import torchvision from torch.utils.tensorboard import SummaryWriter import ssl ssl._create_default_https_context = ssl._create_unverified_contextdataset_transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor() ])# 返回類型 train_set = torchvision.datasets.CIFAR10(root="./dataset", train=True, transform=dataset_transform, download=True) test_set = torchvision.datasets.CIFAR10(root="./dataset", train=False, transform=dataset_transform, download=True)print(test_set[0]) writer = SummaryWriter("p10") for i in range(10):img, target = test_set[i]writer.add_image("test_set", img, i)writer.close()在瀏覽器上就可以看到圖像啦:
遇到問(wèn)題:ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1131)
如果在下載中遇到同樣的問(wèn)題,需要導(dǎo)入ssl:
import ssl ssl._create_default_https_context = ssl._create_unverified_context說(shuō)在最后的話:編寫實(shí)屬不易,若喜歡或者對(duì)你有幫助記得點(diǎn)贊 + 關(guān)注或者收藏哦~
總結(jié)
以上是生活随笔為你收集整理的深度学习初学者,如何下载常用公开数据集并使用呢?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 口碑最好的国产蓝牙耳机,南卡和OPPO哪
- 下一篇: CCNA培训课程表