神經網絡加載數據 自建數據集 官方數據集 pytorch 顯示數據集

1.官方的數據集 MNIST

使用torchvision.datasets 里面有很多數據集供選擇

import torch
import torchvision
from torchvision import transforms, models
batch_size = 32 
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize(mean=(0.5),std=(0.5)),
])
train_data = torchvision.datasets.MNIST('./mn',train=True,download=True,transform=transform)data_loader_train = torch.utils.data.DataLoader(dataset=train_data,batch_size= batch_size,shuffle=True)test_data = torchvision.datasets.MNIST('./mn',train=False,download=True,transform=transform)data_loader_test = torch.utils.data.DataLoader(dataset=test_data,batch_size= batch_size,shuffle=True)
next(iter(data_loader_train))  # 用于查看數據

2.自建的數據集

讀取單個數據文件

device=('cuda' if torch.cuda.is_available() else 'cpu')def load_img(image_path,transform=None,max_size=None,shape=None):image=Image.open(image_path)if max_size:scale=max_size/max(image.size)size=np.array(image.size)*scaleimage=image.resize(size.astype(int),Image.ANTIALIAS)if shape:image=image.resize(shape)if transform:image=transform(image).unsqueeze(0)return image.to(device)transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize(mean=[0.485,0.456,0.406],std=[0.229,0.224,0.225]),
])content = load_img('image/content.jpg',transform,max_size=400)
style = load_img('image/style.jpg',transform,max_size=400)

多張圖片的情況 ImageFloder

這個時候需要把不同label 的數據放到不同的文件夾,ImageFolder 會自動加上標簽,

from torchvison import datasets
data_dir = './data'
all_imgs=datasets.ImageFolder(os.path.join(data_dir,"train"),transforms.Compose([transforms.RandomResizedCrop(input_size),transforms.RandomHorizontalFlip(),transforms.ToTensor(),
]))
loader = torch.utils.data.DataLoader(all_imgs,batch_size=batch_size,shuffle=True)
img=next(iter(loader))[0]unloader=transforms.ToPILImage()def imshow(tensor,title=None):image=tensor.cpu().clone()image=image.squeeze(0)image=unloader(image)plt.imshow(image)if title is not None:plt.title(title)plt.pause(0.001)plt.figure()
imshow(img[31],title='image')

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/535256.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/535256.shtml
英文地址,請注明出處:http://en.pswp.cn/news/535256.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Juypter 打開其他路徑文件

juypter 打開默認的C盤路徑 假如我們想要進入其他盤(D,E,F)時,我們需要進入命令行,然后cd 到其他盤,再在命令行中運行juypter 過程如下 >>>cd D:/train >>>juypter notebook如果顯示jupyter noteboo…

離線安裝 Pytorch 1.2.0 torchvision 0.3.0

1.進入官網 https://download.pytorch.org/whl/torch_stable.html 選擇自己合適的下載下來,然后把下載下來的文件 放入服務器的指定文件夾中 2.cd 到指定文件夾 運行 pip3 install torch-1.0.0-cp36-cp36m-linux_x86_64.whlpip install torchvision-0.2.0-py2.…

Python報錯:PermissionError: [Errno 13] Permission denied 解決方案詳解

出現錯誤的原因: 1.文件找不到, 2.文件被占用, 3.文件無權限訪問, 4.打開的不是文件,而是一個目錄, 我就是這個問題,打開的是一個目錄

pytorch 查看參數是否被訓練 require_grad()

遍歷named_parameters()中的所有的參數,只打印那些param.requires_gradTrue的變量。 for name, param in model.named_parameters():if param.requires_grad:print(name)

Ubuntu18.0.1 安裝 anaconda conda cudnn pytorch-gpu

安裝顯卡驅動 查看驅動版本 cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2 安裝anaconda 下載: 獲取anaconda在清華鏡像站的網址,然后在服務器端wget 網址就行了。 清華鏡像站中anaconda的所有版本的網址:https://mirror…

win10 利用安裝包 安裝 ssh

下載ssh https://github.com/PowerShell/Win32-OpenSSH/releases 然后把它放入 C:\Program Files 中 添加環境變量 高級系統設置-》高級-》環境變量-》 是用戶變量 不是系統變量 選擇 path- 然后點擊編輯,然后新建, 把剛才的地址放進去 C:\Program F…

Latex 表格 行合并,列合并,控制行間距 單元格寬度

多行合并的時候,需要加入新的usepackage, 不然會報錯 \usepackage{multirow}1.多行合并 \linespread{1.3} %控制行間距 \begin{table*}[t]\centering\caption{Experimental arrangement}\begin{tabular}{p{4.1cm}|l|l} %控制單元格的寬度\hlineSection & con…

win10 使用ssh連接服務器 ‘’Bad owner or permissions on C:\\Users\\Administrator/.ssh/config‘’

這個時候只需要把這個文件夾中的config 文件刪除就行了 因為可能在vscode 中使用config文件導致的不兼容,只需要把config文件刪除就行了

Linux Unbunt 安裝顯卡驅動 簡單方法

1.卸載之前驅動 sudo apt-get --purge remove nvidia-* sudo apt-get --purge remove xserver-xorg-video-nouveau2. 重啟服務器 sudo shutdown 3. 在官網下載驅動 官網下載驅動 然后放大服務器的指定文件夾中 4 .關閉X-service 最好遠程連接服務器安裝,因為要…

Ubuntu 查看磁盤空間 及目錄容量

Df命令是linux系統以磁盤分區為單位查看文件系統,可以加上參數查看磁盤剩余空間: df -hl 顯示格式為: 文件系統 容量 已用 可用 已用% 掛載點 以上面的輸出為例,表示的意思為: HD硬盤接口的第二個硬盤(…

Latex 調整表格大小 表格過大 表格過小

1.表格過大 超出了邊界 \resize{width}{length}{text}調節表格大小 \begin{table*}[h]\centering\small\caption{The face identity evaluation based on Face API}\label{tab:The evaluation based on Face API}\resizebox{\textwidth}{9mm}{\begin{tabular}{c c c c c c c…

Latex 中插入超鏈接 插入網址

1. 使用如下包 \usepackage[colorlinks,linkcolorblue]{hyperref}2. 插入語句 \href{http://v.kuwo.com/}{address}

運行shell腳本報錯:“syntax error near unexpected token fi 的解決方法

用vim打開文件,在命令行模式下輸入 :set ff發現出來的是dos fileformatdos現在通過命令:set ffunix設置我們的代碼模式 set ff unix然后保存代碼 再次查看發現代碼編程了unix

bash --config command not found 報錯

原因是.sh腳本在windows系統下用記事本文件編寫的。不同系統的編碼格式引起的。 1、確保用戶對文件有讀寫及執行權限 oraclelinux-106:~/RMAN/bin> chmod ax test.sh2、然后修改文件格式 (1)使用vi工具 oraclelinux-106:~/RMAN/bin> vi test.sh&a…

Linux與Windows共享文件夾之samba的安裝與使用 資源管理器直接查看服務器文件

安裝Samba sudo apt-get install samba創建共享文件夾 // 創建的目錄即之后能夠在Windows主機上直接訪問的目錄。 // 例如:在用戶gzd的主目錄下新建share文件夾為共享目錄 mkdir /home/gzd/smbshare // 由于Windows下的文件夾需可讀可寫可執行,需更改權…

vscode連接遠程服務器 SSH

1.windows 中安裝SSH 1.使用安裝包安裝 2.在電腦上直接安裝 添加貨刪除程序-》可選功能-》添加功能-》Openssh客戶端 然后安裝即可 2. 下載vscode 官網下載即可 3.添加功能 輸入服務器ip 和用戶名 ![在這里插入圖片描述](https://img-blog.csdnimg.cn/202008241639555…

Linux添加新硬盤-掛載硬盤,設置開機自動掛載 解決/home 空間不足問題

1.添加一個新的硬盤 2. 使用fdisk -l 查看磁盤 發現sdb是我們的新硬盤,然后沒有被掛載 3. 掛載新硬盤 sudo mkfs.ext4 /dev/sdb mount /dev/sdb /mnt4. 把/home 下的文件拷貝到/mnt/home中 cd /mnt sudo mkdir home cd /home sudo cp -r * /mnt/home5.查看/m…

ModuleNotFoundError: No module named ‘torch.utils.serialization‘解決

安裝:torchfile (1)安裝torchfilepip install torchfile (2)將from torch.utils.serialization import load_lua改為import torchfile(3)將load_lua改為torchfile.load

Latex 中設置 表格字體大小,行間距 等

Latex設置表格字體大小格式為: \begin{table}[h] \small %此處寫字體大小控制命令 \begin{tabular} \end{tabular}\end{table}Latex 設置字體大小命令由小到大依次為: \tiny \scriptsize \footnotesize \small \normalsize \large \Large \LARGE \…