当前位置: 首页 > news >正文

YOLOv5 分类模型的预处理

YOLOv5 分类模型的预处理

flyfish

版本 6.2

将整个代码简化成如下代码

imgsz=224
file = "/home/a/Pictures/1.jpg"
transforms = classify_transforms(imgsz)
im = cv2.cvtColor(cv2.imread(file), cv2.COLOR_BGR2RGB)
print(im.shape)im = transforms(im)
print(im.shape)im = im.unsqueeze(0).to("cpu")
print(im.shape)

(511, 306, 3) H,W,C顺序
torch.Size([3, 224, 224]) 经过transforms后
torch.Size([1, 3, 224, 224]) 通过unsqueeze扩展增加一维,最后是NCHW的维度进入模型

图像经过了如下变换

def classify_transforms(size=224):# Transforms to apply if albumentations not installedreturn T.Compose([T.ToTensor(), T.Resize(size), T.CenterCrop(size), T.Normalize(IMAGENET_MEAN, IMAGENET_STD)])

最重要的是这两个

T.Resize(size)
T.CenterCrop(size)

分步演示

T.Resize(size) 图像的缩放
看一个参数还是两个参数
如果是一个参数int,那么图像的较小边将与该参数匹配,然后进行缩放,高宽比例不变。
如果是(h, w),那么图像就缩放到(h, w)大小。
举个例子
如果 height > width 那么图片会被缩放到 (size * height / width, size).比例不变

参考:https://pytorch.org/vision/main/generated/torchvision.transforms.Resize.html

import matplotlib.pyplot as plt
from PIL import Image
from torchvision import transformsfile_path = "./1.jpg"
img = Image.open(file)
print("Original:", img.size)

用了PIL库 是 宽w,高h 顺序

在这里插入图片描述

原始大小 宽w,高h Original: (306, 511)

trans0 = transforms.Compose([transforms.Resize(imgsz)]) 
after0 = trans0(img)print("Resize:", after0.size)
after0.save('2.jpg')

在这里插入图片描述

经过Resize之后 宽w,高h Resize: (224, 374)

trans1 = transforms.Compose([transforms.CenterCrop(imgsz)])
after1 = trans1(after0)
print("CenterCrop:", after1.size)
after1.save('3.jpg')

在这里插入图片描述

经过中心剪裁后 CenterCrop: (224, 224)

http://www.lryc.cn/news/214413.html

相关文章:

  • 25 行为型模式-备忘录模式
  • 物联网AI MicroPython传感器学习 之 SHT3X温湿度传感器
  • int* p = new int[5]; int *p = new int[5]();delete[] p; delete p;区别是什么?
  • 数据结构|基础知识定义
  • 物联网AI MicroPython传感器学习 之 MFRC522 RFID射频IC卡感应模块
  • 搭建ES集群
  • Tomcat的日志接收文件catalina.out nohup.out说明
  • 手机ip地址切换后有什么影响
  • C++ 赋值运算重载,const成员,取地址及const取地址操作符重载
  • 嵌入式Linux系统的闪存设备和文件系统学习纪要
  • android 8.1 disable unsupported sensor
  • 二、类与对象(一)
  • 写给所有的程序员,或者努力生活的你。
  • pytorch 笔记:GRU
  • Kubernetes - Ingress HTTP 升级 HTTPS 配置解决方案(新版本v1.21+)
  • Verilog:写流水灯时遇到的问题
  • 操作系统第四章-存储器管理
  • org.springframework.cloud:spring-cloud-starter-openfeign:jar is missing详解
  • Netty第一部
  • 【设计模式】第11节:结构型模式之“装饰器模式”
  • Spire.doc读取模板文档,并在书签处插入内容
  • 性能测试实施流程,5个阶段给老板安排的明明白白!
  • 【教程】R语言生物群落(生态)数据统计分析与绘图
  • 数据库-用户权限管理
  • 十一、W5100S/W5500+RP2040树莓派Pico<ARP 地址解析>
  • 可以直接在线制作电子画册的网站
  • SortableJS:vuedraggable实现元素拖放排序
  • 跟着Nature Communications学作图:纹理柱状图+添加显著性标签!
  • 88. 合并两个有序数组、Leetcode的Python实现
  • 视频列表:点击某个视频进行播放,其余视频全部暂停(同时只播放一个视频)