当前位置:网站首页>Pytorch (environment, tensorboard, transforms, torchvision, dataloader)
Pytorch (environment, tensorboard, transforms, torchvision, dataloader)
2022-06-26 05:40:00 【Yuetun】
Table of contents title
Software use anacoder and pycharm
Environment configuration and installation
solve pip The problem of slow installation
install anaconda
conda create -n pytorch python=3.6
Active version
conda activate pytorch
View Toolkit
install pytorch
pytorch Official website
Installation instructions
conda install pytorch torchvision cudatoolkit=9.2 -c pytorch -c defaults -c numba/1abe1/dev
Install the appropriate version according to the official website
CommandNotFoundError: No command ‘conda creat’. Did you mean ‘conda create’?
Get into python Environmental Science
Test local gpu Whether it can be pytorch Use
>>> import torch
>>> torch.cuda.is_available()
pycharm install
To configure anaconda
error Cannot run program “D:…\venv\Scripts\python.exe“ (in directory ): CreateProcess error=2
jupyter
Get into Anaconda Prompt terminal
conda activate pytorch
jupyter notebook
python Study
python Learn two magic weapons
dir() function , It can let us know what is in the toolbox and the partition in the toolbox .
help() function , Let us know how each tool is used , How to use the tool .
pycharm
data
Download datasets
jupyvter Use
pycharm
image Use
example
from torch.utils.data import Dataset
from PIL import Image
import os
class MyData(Dataset):
def __init__(self,root_dir,label_dir):
self.root_dir=root_dir
self.label_dir=label_dir
self.path=os.path.join(self.root_dir,self.label_dir)
self.img_path=os.listdir(self.path)
def __getitem__(self, idx):
img_name=self.img_path[idx]
img_item_path=os.path.join(self.root_dir,self.label_dir,img_name)
img=Image.open(img_item_path)
label=self.label_dir
return img,label
def __len__(self):
return len(self.img_path)
ants_dataset=MyData("dataset/train","ants")
Dataset addition
On the basis of the above
ants_dataset=MyData("dataset/train","ants")
bees_dataset=MyData("dataset/train","bees")
train_dataset=ants_dataset+bees_dataset
establish label
import os
root_dir = "dataset/train"
target_dir = "ants_image"
img_path = os.listdir(os.path.join(root_dir,target_dir))
label=target_dir.split("_")[0]
out_dir = "ants_label"
for i in img_path:
file_name = i.split( "_jpg ")[0]
with open(os.path.join(root_dir,out_dir,"{}.txt".format(file_name)),'w') as f:
f.write(label)
tensorboard
Tensorboard It was Google TensorFlow Visual tools , It can be used to record training data 、 Evaluation data 、 Network structure 、 Image, etc , And you can web On display , It is very helpful to observe the process of neural network .
install
pip install tensorboard
SummaryWriter
add_scalar
from torch.utils.tensorboard import SummaryWriter
writer=SummaryWriter("logs")
for i in range(100):
writer.add_scalar("y=2x",2*i,i)# In turn, is tag,y,x
writer.close()
AttributeError:module ‘distutils‘ has no attribute ‘version
add_image
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image
writer=SummaryWriter("logs")
image_path='dataset/train/bees_image/17209602_fe5a5a746f.jpg'
img_PIL=Image.open(image_path)
img_array=np.array(img_PIL)
print(type(img_array))
print(img_array.shape)
writer.add_image("test",img_array,3,dataformats='HWC')# Mark , picture , step , Format
for i in range(100):
writer.add_scalar("y=2x",2*i,i)# In turn, is tag,y,x
writer.close()
transforms
transforms.ToTensor Picture format conversion tensor type
from torchvision import transforms
from PIL import Image
img_path="dataset/train/ants_image/7759525_1363d24e88.jpg"
img=Image.open(img_path)
print(img,"\n")
tensor_trans=transforms.ToTensor()
tensor_img=tensor_trans(img)
print(tensor_img)
install opencv-python
pip --default-timeout=300 install opencv-python -i https://pypi.douban.com/simple
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host=‘files.pythonhosted.org’, port=443): Read timed out
Mirror image
torch.Tensor Exhibition add_image
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
img_path="dataset/train/ants_image/7759525_1363d24e88.jpg"
img=Image.open(img_path)
writer=SummaryWriter("logs")
tensor_trans=transforms.ToTensor()
tensor_img=tensor_trans(img)
writer.add_image("Tensor_img",tensor_img)
writer.close()
Add __call__
class person:
def __call__(self,name):
print( " __call__"+" Hello " + name)
def hello(self,name):
print( "hello"+ name )
person = person( )
person("zhangsan")
person.hello("lisi")
Easy to use
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
writer=SummaryWriter("logs")
img=Image.open("dataset/train/ants_image/5650366_e22b7e1065.jpg")
print(img)
# 1.ToTensor
trans_totensor=transforms.ToTensor()
img_tensor=trans_totensor(img)
writer.add_image("ToTensor",img_tensor)
# 2.Normalize normalization
print(img_tensor[0][0][0])
# output = (input - mean) / std
# mean: The mean value of each channel
# std: Standard deviation of each channel rgb Three
trans_norm=transforms.Normalize([2,6,5],[3,2,1])
img_norm=trans_norm(img_tensor)
writer.add_image("Normalize",img_norm,2)
print(img_norm[0][0][0])
#3.resize
print(img.size)
trans_resize=transforms.Resize((512,512))
#img PIL->resize->img_resize PIL
img_resize=trans_resize(img)
# img_resize PIL ->totensor->img_resize tensor
img_resize=trans_totensor(img_resize)
writer.add_image("Resize",img_resize,0)
print(img_resize)
#4. compose resize-2
trans_resize_2=transforms.Resize(256)
#PIL->PIL->tensor
trans_compose=transforms.Compose([trans_resize_2,trans_totensor])
img_resize_2=trans_compose(img)
writer.add_image("Resize",img_resize_2,1)
#5. Then cut
trans_random=transforms.RandomCrop((100,200))
trans_compose_2=transforms.Compose([trans_random,trans_totensor])
for i in range(10):
writer.add_image("RandomCrop",trans_compose_2(img),i)
writer.close()
torchvision Data sets using
import torchvision
# Download datasets
train_set=torchvision.datasets.CIFAR10(root="./data_set_train",train=True,download=True)
test_set=torchvision.datasets.CIFAR10(root="./data_set_test",train=False,download=True)
# A category of a dataset
print(train_set[0])
# // The category name attribute in the dataset
print(train_set.classes)
# Return picture and target value
img,target=train_set[0]
print(img)
print(target)
print(train_set.classes[target])
img.show()
Transforming data tensor type
import torchvision
dataset_tansform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor()
])
train_set=torchvision.datasets.CIFAR10(root="./data_set_train",train=True,transform=dataset_tansform,download=True)
test_set=torchvision.datasets.CIFAR10(root="./data_set_test",train=False,transform=dataset_tansform,download=True)
print(test_set[0])
import torchvision
from torch.utils.tensorboard import SummaryWriter
# / Type conversion
dataset_tansform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor()
])
train_set=torchvision.datasets.CIFAR10(root="./data_set_train",train=True,transform=dataset_tansform,download=True)
test_set=torchvision.datasets.CIFAR10(root="./data_set_test",train=False,transform=dataset_tansform,download=True)
# View the type conversion results
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()
dataloader Use
import torchvision
from torch.utils.data import DataLoader
# Prepared dataset
test_data=torchvision.datasets.CIFAR10("./data_set_test",train=False,transform=torchvision.transforms.ToTensor())
# Test the first picture and target
test_loader=DataLoader(dataset=test_data,batch_size=4,shuffle=True,num_workers=0,drop_last=False)
img,target= test_data[0]
print(img.shape)
print(target)
for data in test_loader:
imggs,targets=data
print(imggs.shape)
print(targets)
SummaryWriter Look at the dataset
import torchvision
from torch.utils.data import DataLoader
# Prepared dataset
from torch.utils.tensorboard import SummaryWriter
test_data=torchvision.datasets.CIFAR10("./data_set_test",train=False,transform=torchvision.transforms.ToTensor())
# Test the first picture and target, Parameters : Data sets , Quantity per piece , Shuffle or not ,0, Do you want the last remainder
test_loader=DataLoader(dataset=test_data,batch_size=4,shuffle=True,num_workers=0,drop_last=False)
img,target= test_data[0]
print(img.shape)
print(target)
writer=SummaryWriter("dataloader")
step=0
for data in test_loader:
imgs,targets=data
print(imgs.shape)
print(targets)
writer.add_images("test_data",imgs,step)
step+=1
writer.close()
边栏推荐
猜你喜欢
Learn cache lines and pseudo sharing of JVM slowly
The State Council issued a document to improve the application of identity authentication and electronic seals, and strengthen the construction of Digital Government
Installation and deployment of alluxio
As promised: Mars, the mobile terminal IM network layer cross platform component library used by wechat, has been officially open source
Mongodb image configuration method
基于SDN的DDoS攻击缓解
cartographer_ pose_ graph_ 2d
Redis usage and memory optimization
Command line interface of alluxio
【ARM】讯为rk3568开发板buildroot添加桌面应用
随机推荐
June 3 is a happy day
vscode config
【MYSQL】MySQL 百万级数据量分页查询方法及其优化
cartographer_ fast_ correlative_ scan_ matcher_ 2D branch and bound rough matching
定位设置水平,垂直居中(多种方法)
状态模式,身随心变
1212312321
The model defined (modified) in pytoch loads some required pre training model parameters and freezes them
Daily production training report (17)
Redis installation on Linux
11 IO frame
【活动推荐】云原生、产业互联网、低代码、Web3、元宇宙……哪个是 2022 年架构热点?...
Chapter 9 setting up structured logging (I)
Win socket programming (Mengxin initial battle)
Redis usage and memory optimization
最后一次飞翔
The most refined language interprets the event dispatcher (also known as the event scheduler)
The use of loops in SQL syntax
Life is so fragile
[activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022