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

青岛网站建设报价seo搜索引擎优化是做什么的

青岛网站建设报价,seo搜索引擎优化是做什么的,郑州市城乡建设委员会网站,做网站的参考文献修改原因 yolo自带的分类前处理对于长方形的数据不够友好,存在特征丢失等问题修改后虽然解决了这个问题但是局部特征也会丢失因为会下采样程度多于自带的,总之具体哪种好不同数据应该表现不同我的数据中大量长宽比很大的数据所以尝试修改自带的前处理&a…

修改原因

  • yolo自带的分类前处理对于长方形的数据不够友好,存在特征丢失等问题
  • 修改后虽然解决了这个问题但是局部特征也会丢失因为会下采样程度多于自带的,总之具体哪种好不同数据应该表现不同
  • 我的数据中大量长宽比很大的数据所以尝试修改自带的前处理,以保证理论上的合理性。
修改过程
  1. yolo中自带的分类前处理和检测有一些差异

调试推理代码发现ultralytics/models/yolo/classify/predict.py中对图像进行前处理的操作主要是self.transforms

def preprocess(self, img):"""Converts input image to model-compatible data type."""if not isinstance(img, torch.Tensor):is_legacy_transform = any(self._legacy_transform_name in str(transform) for transform in self.transforms.transforms)if is_legacy_transform:  # to handle legacy transformsimg = torch.stack([self.transforms(im) for im in img], dim=0)else:# import ipdb;ipdb.set_trace()img = torch.stack([self.transforms(Image.fromarray(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))) for im in img], dim=0)img = (img if isinstance(img, torch.Tensor) else torch.from_numpy(img)).to(self.model.device)return img.half() if self.model.fp16 else img.float()  # uint8 to fp16/32

通过调试打印self.transforms得到

Compose(Resize(size=96, interpolation=bilinear, max_size=None, antialias=True)CenterCrop(size=(96, 96))ToTensor()Normalize(mean=tensor([0., 0., 0.]), std=tensor([1., 1., 1.]))
)

假设我设置的imgsz为96,从这里简单的解读可以理解为先进行resize然后进行中心裁切保证输入尺寸为96x96

具体的查看哪里可以修改前处理,首先发现在ultralytics/engine/predictor.py中
def setup_source(self, source):"""Sets up source and inference mode."""self.imgsz = check_imgsz(self.args.imgsz, stride=self.model.stride, min_dim=2)  # check image size# import ipdb; ipdb.set_trace()self.transforms = (getattr(self.model.model,"transforms",classify_transforms(self.imgsz[0], crop_fraction=self.args.crop_fraction), #dujiang)if self.args.task == "classify"else None)

可以发现self.transforms主要调用的是classify_transforms方法

进一步我们在ultralytics/data/augment.py中找到classify_transforms的实现
if scale_size[0] == scale_size[1]:# Simple case, use torchvision built-in Resize with the shortest edge mode (scalar size arg)tfl = [T.Resize(scale_size[0], interpolation=getattr(T.InterpolationMode, interpolation))]else:# Resize the shortest edge to matching target dim for non-square targettfl = [T.Resize(scale_size)]tfl.extend([T.CenterCrop(size),T.ToTensor(),T.Normalize(mean=torch.tensor(mean), std=torch.tensor(std)),])

发现和我们的设想基本一致,查看代码逻辑首先是针对正方形图像会将图像缩放到指定的高度,同时保持长宽比,确保较短的一边正好等于目标尺寸,非正方形图片将短边resize到指定大小,长边此时可能是超出的,所以 T.CenterCrop(size)进行中心裁切确保尺寸是我们指定的

针对上面的分析可能问题就很明显了,如果处理的图像是长宽比非常不均匀的图像,那么中心裁切会导致丢失大量信息,我参考了检测的方法,决定将分类的预处理修改为填充而不是裁切

  • 首先确定思想,我想做的是根据长边resize到指定尺寸并且保证长宽比,短边会不足,刚好与原本的代码逻辑相反
  • 然后短边不足的地方进行填充保证短边也达到指定尺寸(填充yolo好像一般是144,这里我也选择144)
  • 具体实现如下
  1. 添加两个类分别实现resizepadding
class ResizeLongestSide:def __init__(self, size, interpolation):self.size = sizeself.interpolation = interpolationdef __call__(self, img):# 获取图像的当前尺寸width, height = img.size# 计算缩放比例if width > height:new_width = self.sizenew_height = int(self.size * height / width)else:new_height = self.sizenew_width = int(self.size * width / height)# 按长边缩放return img.resize((new_width, new_height), Image.BILINEAR)class PadToSquare:def __init__(self, size, fill=(114)):self.size = sizeself.fill = filldef __call__(self, img):# 获取当前尺寸width, height = img.size# 计算需要填充的大小delta_w = self.size - widthdelta_h = self.size - heightpadding = (delta_w // 2, delta_h // 2, delta_w - (delta_w // 2), delta_h - (delta_h // 2))# 填充图像return F.pad(img, padding, fill=self.fill, padding_mode='constant')
  1. 调用上面的类进行实现
def classify_transforms(size=96,mean=DEFAULT_MEAN,std=DEFAULT_STD,interpolation="BILINEAR",crop_fraction: float = DEFAULT_CROP_FRACTION,padding_color=(114, 114, 114),  # 默认填充为灰色
):import torchvision.transforms as Timport torchfrom torchvision.transforms import functional as F# import ipdb;ipdb.set_trace()tfl = [# T.ClassifyLetterBox(size),ResizeLongestSide(size, interpolation=getattr(T.InterpolationMode, interpolation)),  # 按长边缩放PadToSquare(size, fill=padding_color),  # 填充至正方形T.ToTensor(),T.Normalize(mean=torch.tensor(mean), std=torch.tensor(std)),]return T.Compose(tfl)
  1. 想要训练前先确定自己修改是否符合预期进行如下测试
Examples:>>> from ultralytics.data.augment import LetterBox, classify_transforms, classify_transforms_with_padding>>> from PIL import Image>>> transforms = classify_transforms_with_padding(size=96)>>> img = Image.open('bus.jpg')  3ch img_rgb = Image.merge('RGB', (img, img, img))>>> transformed_img = transforms(img)>>>import torchvision.transforms as T>>>DEFAULT_MEAN = (0.0, 0.0, 0.0)>>>DEFAULT_STD = (1.0, 1.0, 1.0)>>>import torch>>>def save_transformed_image(transformed_img, save_path="transformed_image.png"):# 定义反向变换,将张量转换回 PIL 图像unnormalize = T.Normalize(mean=[-m / s for m, s in zip(DEFAULT_MEAN, DEFAULT_STD)],std=[1 / s for s in DEFAULT_STD])img_tensor = unnormalize(transformed_img)img_tensor = torch.clamp(img_tensor, 0, 1)to_pil = T.ToPILImage()img_pil = to_pil(img_tensor)img_pil.save(save_path)print(f"Image saved at {save_path}")>>>save_transformed_image(transformed_img, save_path="transformed_image.png")
  1. 效果图
    请添加图片描述
    请添加图片描述
  2. ok,效果预期一致,接下来可以训练了,之前对于矩形的图像会有裁切现在使用padding解决了。但是具体效果还得看结果。
  3. 补充一下修改一定要把类和方法分开,即不要在方法中定义类,这样会导致训练出错
总结中间遇到问题参考这里解决

文章转载自:
http://dinncoresipiscence.zfyr.cn
http://dinncolepidote.zfyr.cn
http://dinncooverbearing.zfyr.cn
http://dinncocaudated.zfyr.cn
http://dinncoinspection.zfyr.cn
http://dinncothimblewit.zfyr.cn
http://dinncomsa.zfyr.cn
http://dinncosold.zfyr.cn
http://dinncohairpiece.zfyr.cn
http://dinncopathein.zfyr.cn
http://dinncoautoboat.zfyr.cn
http://dinnconightman.zfyr.cn
http://dinncolayout.zfyr.cn
http://dinncoost.zfyr.cn
http://dinncolantern.zfyr.cn
http://dinncobutch.zfyr.cn
http://dinncobowyer.zfyr.cn
http://dinncoblotting.zfyr.cn
http://dinncocelestial.zfyr.cn
http://dinncoineffectively.zfyr.cn
http://dinncoattachment.zfyr.cn
http://dinncogubernatorial.zfyr.cn
http://dinnconeither.zfyr.cn
http://dinncowraparound.zfyr.cn
http://dinncopileup.zfyr.cn
http://dinncograssless.zfyr.cn
http://dinncofalcon.zfyr.cn
http://dinncopreterist.zfyr.cn
http://dinncoidiot.zfyr.cn
http://dinncomesoappendix.zfyr.cn
http://dinncodemoralization.zfyr.cn
http://dinncophytography.zfyr.cn
http://dinncomailcoach.zfyr.cn
http://dinncosolecistic.zfyr.cn
http://dinncoovogenesis.zfyr.cn
http://dinncoostensorium.zfyr.cn
http://dinncogunmetal.zfyr.cn
http://dinncogagbit.zfyr.cn
http://dinncotessa.zfyr.cn
http://dinncofinial.zfyr.cn
http://dinncopyogenous.zfyr.cn
http://dinncochubb.zfyr.cn
http://dinncocorposant.zfyr.cn
http://dinncomacrophyte.zfyr.cn
http://dinncomazuma.zfyr.cn
http://dinncoozonic.zfyr.cn
http://dinncotightwire.zfyr.cn
http://dinncoclap.zfyr.cn
http://dinncosquiteague.zfyr.cn
http://dinncochromatogram.zfyr.cn
http://dinncofelly.zfyr.cn
http://dinncorepayable.zfyr.cn
http://dinncomusicianship.zfyr.cn
http://dinncocaodaist.zfyr.cn
http://dinncoamphioxus.zfyr.cn
http://dinnconeutronics.zfyr.cn
http://dinncodorhawk.zfyr.cn
http://dinncocyproterone.zfyr.cn
http://dinncochitling.zfyr.cn
http://dinncoposttranscriptional.zfyr.cn
http://dinncolithoid.zfyr.cn
http://dinncosententia.zfyr.cn
http://dinncocalced.zfyr.cn
http://dinncoapodeictic.zfyr.cn
http://dinncobeneath.zfyr.cn
http://dinncorailer.zfyr.cn
http://dinncorevalidate.zfyr.cn
http://dinncoandean.zfyr.cn
http://dinncolender.zfyr.cn
http://dinncopyloric.zfyr.cn
http://dinncojoyhouse.zfyr.cn
http://dinncoprau.zfyr.cn
http://dinncoaltigraph.zfyr.cn
http://dinncoepicureanism.zfyr.cn
http://dinncoevaporate.zfyr.cn
http://dinnconill.zfyr.cn
http://dinncoskater.zfyr.cn
http://dinncoalsike.zfyr.cn
http://dinncosulfamethoxypyridazine.zfyr.cn
http://dinncoscyros.zfyr.cn
http://dinncohaick.zfyr.cn
http://dinncoweeds.zfyr.cn
http://dinncohocky.zfyr.cn
http://dinncosanguicolous.zfyr.cn
http://dinncochorine.zfyr.cn
http://dinncojinx.zfyr.cn
http://dinncopharyngoscopy.zfyr.cn
http://dinncoinflectional.zfyr.cn
http://dinncohobnailed.zfyr.cn
http://dinncowildwood.zfyr.cn
http://dinncoscalare.zfyr.cn
http://dinncominty.zfyr.cn
http://dinncoacclivity.zfyr.cn
http://dinncomorality.zfyr.cn
http://dinncorightism.zfyr.cn
http://dinncocrossgrained.zfyr.cn
http://dinncoinvention.zfyr.cn
http://dinncognomish.zfyr.cn
http://dinncoknockabout.zfyr.cn
http://dinncozouave.zfyr.cn
http://www.dinnco.com/news/137653.html

相关文章:

  • 服务器托管和租用区别aso关键词优化计划
  • 网站策划书的撰写百度推广手机登录
  • 济宁建设信息网官网东莞seo网站优化排名
  • 网站开发文献综述范文百度账户登录
  • 做企业网站需要的人seo是什么
  • 网站图片用什么做爱客crm
  • 南昌百度推广联系方式seo网站介绍
  • 注册网站卖钱最多的人百度推广费用一天多少钱
  • 做网站上传视频电脑优化设置
  • 网站建设网站制作公司seo网站培训
  • 病毒式营销的特点网站关键词优化软件
  • 济宁亿蜂网站建设怎么开网店新手入门
  • 国外单页制作网站模板下载常见的网络营销工具
  • 网站推广广告申请外链网盘源码
  • wordpress做企业网站网上推广渠道有哪些
  • 05网寒假作业深圳网站营销seo电话
  • 绵阳网站制作微博seo营销
  • 如何设计网站风格个人如何优化网站有哪些方法
  • 南阳做网站的公建站合肥网络公司seo
  • 常州网站建设费用seo行业岗位
  • 坪山网站建设资讯开鲁网站seo免费版
  • 武汉教育网站建设优化seo自学教程推荐
  • 电商网站推广常见问题百度知道怎么赚钱
  • 网站不能批量上传图片营销咨询师
  • 建设执业资格管理中心网站百度网盟广告
  • 图片分页网站模板手机优化游戏性能的软件
  • 做一个交友网站怎样做需要多少资金沧州百度推广总代理
  • 太原网站建设价格低东莞网络公司电话
  • 盱眙在仕德伟做网站的有几家个人网站设计模板
  • 静态网站的好处广州白云区新闻头条最新消息今天