深度学习初学者,如何下载常用公开数据集并使用呢?
深度學習初學者,如何下載常用公開數(shù)據(jù)集并使用呢?
- 1.前言
- 2.官方文檔怎樣看
- 3.動手寫代碼
- 4.如何可視化
- 遇到問題:ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1131)
1.前言
剛開始進行深度學習的時候,難免要用到一些公開數(shù)據(jù)集,現(xiàn)在閑來無事,記錄一下如何快速下載一些經(jīng)典數(shù)據(jù)集。通過官方文檔學習,是一些大牛們掛在嘴邊經(jīng)常推薦的方法,那么我們本篇博客就從官方文檔開始學習。
因為我是做CV方向的,所以用TorchVision這個庫舉例。來自官網(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,大家應該都不陌生。一會兒會以CIFAR舉例,記錄一下我的過程。
2.官方文檔怎樣看
首先我們看一下CIFAR這個類的文檔:
參數(shù):
root:表示將下載的數(shù)據(jù)集放在哪個目錄
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:是否為訓練數(shù)據(jù)集
train (bool, optional): If True, creates dataset from training set, otherwise creates from test set.transform:一個將圖像進行預處理、返回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.動手寫代碼
示例代碼
# 導入torchvision包 import torchvision# 對原始圖像進行數(shù)據(jù)處理的函數(shù) dataset_transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor() ])# 生成訓練數(shù)據(jù)集和測試數(shù)據(jù)集 # 訓練數(shù)據(jù)集 存放在根目錄的dataset文件夾下,作為訓練數(shù)據(jù)集,并下載 train_set = torchvision.datasets.CIFAR10(root="./dataset", train=True, transform=dataset_transform, download=True) # 測試數(shù)據(jù)集 存放在根目錄的dataset文件夾下,不作為訓練數(shù)據(jù)集,并下載 test_set = torchvision.datasets.CIFAR10(root="./dataset", train=False, transform=dataset_transform, download=True)print(test_set[0])然后我們右鍵運行,進行下載
可以看到數(shù)據(jù)集已經(jīng)開始下載了,但是因為是從toronto.edu下載,速度很慢。教你一個更快的方法:我們終止運行,復制這個鏈接,用迅雷下載,很快就好了。然后將下載好的.gz文件進行解壓,放到我們創(chuàng)建的dataset目錄下:
重新run,就可以正常使用數(shù)據(jù)集了。
4.如何可視化
我用tensorboard進行了可視化,大家感興趣可以研究一下tensorboard這個庫。
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()在瀏覽器上就可以看到圖像啦:
遇到問題:ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1131)
如果在下載中遇到同樣的問題,需要導入ssl:
import ssl ssl._create_default_https_context = ssl._create_unverified_context說在最后的話:編寫實屬不易,若喜歡或者對你有幫助記得點贊 + 關注或者收藏哦~
總結(jié)
以上是生活随笔為你收集整理的深度学习初学者,如何下载常用公开数据集并使用呢?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 口碑最好的国产蓝牙耳机,南卡和OPPO哪
- 下一篇: 梳理百年深度学习发展史-七月在线机器学习