服务佳的小企业网站建设,用jsp做的网站首页,滁州注册公司流程和费用,青岛网站制作工具一、Pytorch中如何加载数据 在Pytorch中涉及到如何读取数据#xff0c;主要是两个类一个类是Dataset、Dataloader Dataset 提供一种方式获取数据#xff0c;及其对应的label。主要包含以下两个功能#xff1a; 如何获取每一个数据以及label 告诉我们总共有多少的数据
Datal…一、Pytorch中如何加载数据 在Pytorch中涉及到如何读取数据主要是两个类一个类是Dataset、Dataloader Dataset 提供一种方式获取数据及其对应的label。主要包含以下两个功能 如何获取每一个数据以及label 告诉我们总共有多少的数据
Dataloader可以对数据进行打包为后面的网络提供不同的数据形式。
二、Tensorboard的使用用来观察训练结果
from torch.utils.tensorboard import SummaryWriterwriter SummaryWriter(log)# writer.add_image()for i in range(100):writer.add_scalar(yx, i, i)writer.close()在Terminal中先切换到conda activate pytorch 使用命令 tensorboard --logdirlogs
TensorBoard的使用 1、使用add_image()方法
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image
# 利用openCV中的numpy库可以获得numpy型的图片writer SummaryWriter(log)
img_path ../dataset/bees/26589803_5ba7000313.jpg
img_PIL Image.open(img_path) # 打开图片
img_array np.array(img_PIL) # 图片转换
print(type(img_array)) # 打印图片类型
print(img_array.shape) # 打印图片格式writer.add_images(test, img_array, 2, dataformatsHWC) # 根据img_array.shape来指定如果不指定dataformats就会报错
# y 2x
for i in range(100):writer.add_scalar(y2x, 2*i, i)writer.close()三、Transforms的使用 transform表示对图片进行一些变换 python的用法 - tensor数据类型 通过transform.ToTensor去解决两个问题 transforms该如何使用Python 为什么我们需要Tensor的数据类型
from torchvision import transforms
from PIL import Imageimg_path ../dataset/bees/10870992_eebeeb3a12.jpg
img Image.open(img_path)
tensor_trans transforms.ToTensor()
tensor_img tensor_trans(img)
print(tensor_img)运行后的结果
D:\tools\anaconda\envs\pytorch\python.exe D:/code/captcha_ocr-main/learn/transforms.py
tensor([[[0.5725, 0.5725, 0.5725, ..., 0.5686, 0.5725, 0.5765],[0.5725, 0.5725, 0.5725, ..., 0.5686, 0.5725, 0.5765],[0.5686, 0.5686, 0.5725, ..., 0.5686, 0.5725, 0.5765],...,[0.5490, 0.5647, 0.5725, ..., 0.6314, 0.6235, 0.6118],[0.5608, 0.5765, 0.5843, ..., 0.5961, 0.5843, 0.5765],[0.5725, 0.5843, 0.5922, ..., 0.5647, 0.5529, 0.5490]],[[0.4471, 0.4471, 0.4471, ..., 0.4235, 0.4275, 0.4314],[0.4471, 0.4471, 0.4471, ..., 0.4235, 0.4275, 0.4314],[0.4431, 0.4431, 0.4471, ..., 0.4235, 0.4275, 0.4314],...,[0.4000, 0.4157, 0.4235, ..., 0.4706, 0.4627, 0.4510],[0.4118, 0.4275, 0.4353, ..., 0.4431, 0.4314, 0.4235],[0.4235, 0.4353, 0.4431, ..., 0.4118, 0.4000, 0.3961]],[[0.2471, 0.2471, 0.2471, ..., 0.2588, 0.2627, 0.2667],[0.2471, 0.2471, 0.2471, ..., 0.2588, 0.2627, 0.2667],[0.2431, 0.2431, 0.2471, ..., 0.2588, 0.2627, 0.2667],...,[0.2157, 0.2314, 0.2392, ..., 0.2510, 0.2431, 0.2314],[0.2275, 0.2431, 0.2510, ..., 0.2196, 0.2078, 0.2000],[0.2392, 0.2510, 0.2588, ..., 0.1961, 0.1843, 0.1804]]])Process finished with exit code 0加载tensor类型的图片
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
img_path ../dataset/bees/10870992_eebeeb3a12.jpg
img Image.open(img_path)
writer SummaryWriter(log)
tensor_trans transforms.ToTensor()
tensor_img tensor_trans(img)
writer.add_image(Tensor_img, tensor_img)
writer.close()四、常见的transforms类的使用
ToTensor类 将PIL图片转换成tensor图片。
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
# Tensor的使用
writer SummaryWriter(log)
img_path ../dataset/bees/10870992_eebeeb3a12.jpg
img Image.open(img_path)
print(img)
tensor_trans transforms.ToTensor() # 创建ToTensor()对象
tensor_img tensor_trans(img) # 传入图片参数将PIL图片转换成tensor图片
writer.add_image(Tensor_img, tensor_img)
writer.close()2. Normalize类
对tensor类型的图片进行归一化处理。 Normalize的使用归一化处理 公式output[channel] (input[channel] - mean[channel]) / std[channel]
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter# Tensor的使用
writer SummaryWriter(log)
img_path ../dataset/bees/10870992_eebeeb3a12.jpg
img Image.open(img_path)
print(img)tensor_trans transforms.ToTensor()
tensor_img tensor_trans(img)writer.add_image(Tensor_img, tensor_img)# Normalize的使用
print(tensor_img[0][0][0]) # 归一化处理之前的数据
trans_norm transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_norm trans_norm(tensor_img)
print(img_norm[0][0][0]) # 归一化处理后的结果
writer.add_image(Normalize, img_norm)writer.close()3. Resize类 重置图片大小。
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter# Tensor的使用
writer SummaryWriter(log)
img_path ../dataset/bees/10870992_eebeeb3a12.jpg
img Image.open(img_path)
print(img)tensor_trans transforms.ToTensor()
tensor_img tensor_trans(img)writer.add_image(Tensor_img, tensor_img)print(tensor_img[0][0][0]) # 归一化处理之前的数据
trans_norm transforms.Normalize([1, 3, 5], [3, 2, 1])
img_norm trans_norm(tensor_img)
print(img_norm[0][0][0]) # 归一化处理后的结果
writer.add_image(Normalize, img_norm)writer.close()# Resize的使用:重置图片大小
print(img.size) # (500, 464)
trans_resize transforms.Resize((512, 512))img_resize trans_resize(img)
print(img_resize) # PIL.Image.Image image modeRGB size512x512 at 0x2A17E774248 img_resize tensor_trans(img_resize)
writer.add_image(Resize, img_resize, 0)
print( img_resize)
writer.close() 4. Compose的使用 等比例缩放。 Compose的使用整体缩放不改变高宽比例 Compose()中的参数需要的是一个列表列表中的数据需要的是transforms类型。 即 Compose([transforms参数1, transforms参数2, …])
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter# Tensor的使用
writer SummaryWriter(log)
img_path ../dataset/bees/10870992_eebeeb3a12.jpg
img Image.open(img_path)
print(img)tensor_trans transforms.ToTensor()
tensor_img tensor_trans(img)writer.add_image(Tensor_img, tensor_img)print(tensor_img[0][0][0]) # 归一化处理之前的数据
trans_norm transforms.Normalize([1, 3, 5], [3, 2, 1])
img_norm trans_norm(tensor_img)
# print(img_norm[0][0][0]) # 归一化处理后的结果
writer.add_image(Normalize, img_norm)# Resize的使用:重置图片大小
print(img.size) # (500, 464)
trans_resize transforms.Resize((512, 512))img_resize trans_resize(img)
print(img_resize) # PIL.Image.Image image modeRGB size512x512 at 0x2A17E774248# Compose的使用
img_resize tensor_trans(img_resize)
writer.add_image(Resize, img_resize, 0)
# print(img_resize)
trans_resize_2 transforms.Resize(1024)
trans_compose transforms.Compose([trans_resize_2, tensor_trans])
img_resize_2 trans_compose(img)
writer.add_image(Resize, img_resize_2, 1)writer.close()5.RandomCrop类的使用
随机裁剪。
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter# Tensor的使用
writer SummaryWriter(log)
img_path ../dataset/bees/10870992_eebeeb3a12.jpg
img Image.open(img_path)
print(img)tensor_trans transforms.ToTensor()
tensor_img tensor_trans(img)writer.add_image(Tensor_img, tensor_img)print(tensor_img[0][0][0]) # 归一化处理之前的数据
trans_norm transforms.Normalize([1, 3, 5], [3, 2, 1])
img_norm trans_norm(tensor_img)
# print(img_norm[0][0][0]) # 归一化处理后的结果
writer.add_image(Normalize, img_norm)# Resize的使用:重置图片大小
print(img.size) # (500, 464)
trans_resize transforms.Resize((512, 512))img_resize trans_resize(img)
print(img_resize) # PIL.Image.Image image modeRGB size512x512 at 0x2A17E774248# Compose的使用
img_resize tensor_trans(img_resize)
writer.add_image(Resize, img_resize, 0)
# print(img_resize)
trans_resize_2 transforms.Resize(1024)
trans_compose transforms.Compose([trans_resize_2, tensor_trans])
img_resize_2 trans_compose(img)
writer.add_image(Resize, img_resize_2, 1)# RendomCrop类的使用随机裁剪
# trans_random transforms.RandomCrop(512)
trans_random transforms.RandomCrop(1000, 500)
trans_compose_2 transforms.Compose([trans_random, tensor_trans])
for i in range(10):img_crop trans_compose_2(img)# writer.add_image(RancomCrop, img_crop, i)writer.add_image(RancomCropHW, img_crop, i)writer.close() 文章转载自: http://www.morning.stflb.cn.gov.cn.stflb.cn http://www.morning.wchcx.cn.gov.cn.wchcx.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.dbxss.cn.gov.cn.dbxss.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn http://www.morning.nfbxgtj.com.gov.cn.nfbxgtj.com http://www.morning.gfpyy.cn.gov.cn.gfpyy.cn http://www.morning.jmdpp.cn.gov.cn.jmdpp.cn http://www.morning.fbjqq.cn.gov.cn.fbjqq.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.rbmnq.cn.gov.cn.rbmnq.cn http://www.morning.pzdxg.cn.gov.cn.pzdxg.cn http://www.morning.hqykb.cn.gov.cn.hqykb.cn http://www.morning.xjmyq.com.gov.cn.xjmyq.com http://www.morning.sbrpz.cn.gov.cn.sbrpz.cn http://www.morning.brld.cn.gov.cn.brld.cn http://www.morning.cnbdn.cn.gov.cn.cnbdn.cn http://www.morning.wrtbx.cn.gov.cn.wrtbx.cn http://www.morning.qxwgx.cn.gov.cn.qxwgx.cn http://www.morning.dpbgw.cn.gov.cn.dpbgw.cn http://www.morning.bgkk.cn.gov.cn.bgkk.cn http://www.morning.bpncd.cn.gov.cn.bpncd.cn http://www.morning.qkxt.cn.gov.cn.qkxt.cn http://www.morning.sbpt.cn.gov.cn.sbpt.cn http://www.morning.jbblf.cn.gov.cn.jbblf.cn http://www.morning.niukaji.com.gov.cn.niukaji.com http://www.morning.wklrz.cn.gov.cn.wklrz.cn http://www.morning.wfjyn.cn.gov.cn.wfjyn.cn http://www.morning.kaoshou.net.gov.cn.kaoshou.net http://www.morning.tqdqc.cn.gov.cn.tqdqc.cn http://www.morning.nnykz.cn.gov.cn.nnykz.cn http://www.morning.xirfr.cn.gov.cn.xirfr.cn http://www.morning.xykst.cn.gov.cn.xykst.cn http://www.morning.syglx.cn.gov.cn.syglx.cn http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn http://www.morning.beiyishengxin.cn.gov.cn.beiyishengxin.cn http://www.morning.tcfhs.cn.gov.cn.tcfhs.cn http://www.morning.tmtrl.cn.gov.cn.tmtrl.cn http://www.morning.ryznd.cn.gov.cn.ryznd.cn http://www.morning.swlwf.cn.gov.cn.swlwf.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.mgbsp.cn.gov.cn.mgbsp.cn http://www.morning.ypdmr.cn.gov.cn.ypdmr.cn http://www.morning.zpqk.cn.gov.cn.zpqk.cn http://www.morning.zzfjh.cn.gov.cn.zzfjh.cn http://www.morning.yqjjn.cn.gov.cn.yqjjn.cn http://www.morning.gpnfg.cn.gov.cn.gpnfg.cn http://www.morning.wyrsn.cn.gov.cn.wyrsn.cn http://www.morning.xlwpz.cn.gov.cn.xlwpz.cn http://www.morning.tphjl.cn.gov.cn.tphjl.cn http://www.morning.qqrlz.cn.gov.cn.qqrlz.cn http://www.morning.itvsee.com.gov.cn.itvsee.com http://www.morning.bnfsw.cn.gov.cn.bnfsw.cn http://www.morning.gthwz.cn.gov.cn.gthwz.cn http://www.morning.rnwt.cn.gov.cn.rnwt.cn http://www.morning.rkqzx.cn.gov.cn.rkqzx.cn http://www.morning.tnqk.cn.gov.cn.tnqk.cn http://www.morning.wdxr.cn.gov.cn.wdxr.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.mxhgy.cn.gov.cn.mxhgy.cn http://www.morning.ckntb.cn.gov.cn.ckntb.cn http://www.morning.fjzlh.cn.gov.cn.fjzlh.cn http://www.morning.tnhmp.cn.gov.cn.tnhmp.cn http://www.morning.wqnc.cn.gov.cn.wqnc.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.qrsrs.cn.gov.cn.qrsrs.cn http://www.morning.nlffl.cn.gov.cn.nlffl.cn http://www.morning.gpryk.cn.gov.cn.gpryk.cn http://www.morning.lhgkr.cn.gov.cn.lhgkr.cn http://www.morning.jfjpn.cn.gov.cn.jfjpn.cn http://www.morning.dmtbs.cn.gov.cn.dmtbs.cn http://www.morning.lkpzx.cn.gov.cn.lkpzx.cn http://www.morning.wsnbg.cn.gov.cn.wsnbg.cn http://www.morning.tjmfz.cn.gov.cn.tjmfz.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.wjhpg.cn.gov.cn.wjhpg.cn http://www.morning.rkzb.cn.gov.cn.rkzb.cn http://www.morning.yodajy.cn.gov.cn.yodajy.cn