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

用仿网站做优化有效果吗什么广告推广最有效果

用仿网站做优化有效果吗,什么广告推广最有效果,深圳招聘网站前十排名,纪念馆网站建设【PyTorch】基于YOLO的多目标检测项目(一) 【PyTorch】基于YOLO的多目标检测项目(二) 目标检测是对图像中的现有目标进行定位和分类的过程。识别的对象在图像中显示有边界框。一般的目标检测方法有两种:基于区域提议的…

【PyTorch】基于YOLO的多目标检测项目(一)

【PyTorch】基于YOLO的多目标检测项目(二)

目标检测是对图像中的现有目标进行定位和分类的过程。识别的对象在图像中显示有边界框。一般的目标检测方法有两种:基于区域提议的和基于回归/分类的。这里使用一种基于回归/分类的方法,称为YOLO。

目录

准备COCO数据集

创建自定义数据集

转换数据

定义数据加载器


准备COCO数据集

COCO是一个大规模的对象检测,分割和字幕数据集。它包含80个对象类别用于对象检测。

下载以下GitHub存储库

https://github.com/pjreddie/darkneticon-default.png?t=N7T8https://github.com/pjreddie/darknet

创建一个名为config的文件夹,将darknet/cfg/coco.data、darknet/cfg/yolov3.cfg文件复制到config文件夹中。

创建一个名为data的文件夹,从以下链接获取coco.names文件,并将其放入data文件夹,coco.names文件包含COCO数据集中80个对象类别的列表。

darknet/data/coco.names at master · pjreddie/darknet · GitHubConvolutional Neural Networks. Contribute to pjreddie/darknet development by creating an account on GitHub.icon-default.png?t=N7T8https://github.com/pjreddie/darknet/blob/master/data/coco.names将darknet/scripts/get_coco_dataset.sh文件复制到data文件夹中,并复制get_coco_cocoet.sh到data文件夹。接下来,打开一个终端并执行get_coco_cocoet.sh,该脚本将把完整的COCO数据集下载到名为coco的子文件夹中。也可通过以下链接下载coco数据集。

COCO2014_数据集-飞桨AI Studio星河社区 (baidu.com)icon-default.png?t=N7T8https://aistudio.baidu.com/datasetdetail/165195

在images文件夹中,有两个名为train 2014和val 2014的文件夹,分别包含82783和40504个图像。在labels文件夹中,有两个名为train 2014和val 2014的标签,分别包含82081和40137文本文件。这些文本文件包含图像中对象的边界框坐标。此外,trainvalno5k.txt文件是一个包含117264张图像的列表,这些图像将用于训练模型。此列表是train2014和val2014中图像的组合,5000个图像除外。5k.txt文件包含将用于验证的5000个图像的列表。

创建自定义数据集

完成数据集下载后,使用PyTorch的Dataset和Dataloader类创建训练和验证数据集和数据加载器。

from torch.utils.data import Dataset
from PIL import Image
import torchvision.transforms.functional as TF
import os
import numpy as npimport torch
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(torch.__version__)
#定义CocoDataset类,并展示来自训练和验证数据集的一些示例图像
class CocoDataset(Dataset):def __init__(self, path2listFile, transform=None, trans_params=None):with open(path2listFile, "r") as file:self.path2imgs = file.readlines()self.path2labels = [path.replace("images", "labels").replace(".png", ".txt").replace(".jpg", ".txt")for path in self.path2imgs]self.trans_params = trans_paramsself.transform = transformdef __len__(self):return len(self.path2imgs)def __getitem__(self, index):path2img = self.path2imgs[index % len(self.path2imgs)].rstrip()img = Image.open(path2img).convert('RGB')path2label = self.path2labels[index % len(self.path2imgs)].rstrip()labels= Noneif os.path.exists(path2label):labels = np.loadtxt(path2label).reshape(-1, 5)if self.transform:img, labels = self.transform(img, labels, self.trans_params)return img, labels, path2img    
root_data="./data/coco"
path2trainList=os.path.join(root_data, "trainvalno5k.txt")coco_train = CocoDataset(path2trainList)
print(len(coco_train))

 

# 从coco_train中获取图像、标签和图像路径
img, labels, path2img = coco_train[1] 
print("image size:", img.size, type(img))
print("labels shape:", labels.shape, type(labels))
print("labels \n", labels)

path2valList=os.path.join(root_data, "5k.txt")
coco_val = CocoDataset(path2valList, transform=None, trans_params=None)
print(len(coco_val))

img, labels, path2img = coco_val[7] 
print("image size:", img.size, type(img))
print("labels shape:", labels.shape, type(labels))
print("labels \n", labels)

import matplotlib.pylab as plt
import numpy as np
from PIL import Image, ImageDraw, ImageFont
from torchvision.transforms.functional import to_pil_image
import random
%matplotlib inline
path2cocoNames="./data/coco.names"
fp = open(path2cocoNames, "r")
coco_names = fp.read().split("\n")[:-1]
print("number of classese:", len(coco_names))
print(coco_names)

def rescale_bbox(bb,W,H):x,y,w,h=bbreturn [x*W, y*H, w*W, h*H]
COLORS = np.random.randint(0, 255, size=(80, 3),dtype="uint8")
# fnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 16)
fnt = ImageFont.truetype('arial.ttf', 16)
def show_img_bbox(img,targets):if torch.is_tensor(img):img=to_pil_image(img)if torch.is_tensor(targets):targets=targets.numpy()[:,1:]W, H=img.sizedraw = ImageDraw.Draw(img)for tg in targets:id_=int(tg[0])bbox=tg[1:]bbox=rescale_bbox(bbox,W,H)xc,yc,w,h=bboxcolor = [int(c) for c in COLORS[id_]]name=coco_names[id_]draw.rectangle(((xc-w/2, yc-h/2), (xc+w/2, yc+h/2)),outline=tuple(color),width=3)draw.text((xc-w/2,yc-h/2),name, font=fnt, fill=(255,255,255,0))plt.imshow(np.array(img))        
np.random.seed(1)
rnd_ind=np.random.randint(len(coco_train))
img, labels, path2img = coco_train[rnd_ind] 
print(img.size, labels.shape)plt.rcParams['figure.figsize'] = (20, 10)
show_img_bbox(img,labels)

np.random.seed(1)
rnd_ind=np.random.randint(len(coco_val))
img, labels, path2img = coco_val[rnd_ind] 
print(img.size, labels.shape)plt.rcParams['figure.figsize'] = (20, 10)
show_img_bbox(img,labels)

转换数据

定义一个转换函数和传递给CocoDataset类的参数

def pad_to_square(img, boxes, pad_value=0, normalized_labels=True):w, h = img.sizew_factor, h_factor = (w,h) if normalized_labels else (1, 1)dim_diff = np.abs(h - w)pad1= dim_diff // 2pad2= dim_diff - pad1if h<=w:left, top, right, bottom= 0, pad1, 0, pad2else:left, top, right, bottom= pad1, 0, pad2, 0padding= (left, top, right, bottom)img_padded = TF.pad(img, padding=padding, fill=pad_value)w_padded, h_padded = img_padded.sizex1 = w_factor * (boxes[:, 1] - boxes[:, 3] / 2)y1 = h_factor * (boxes[:, 2] - boxes[:, 4] / 2)x2 = w_factor * (boxes[:, 1] + boxes[:, 3] / 2)y2 = h_factor * (boxes[:, 2] + boxes[:, 4] / 2)    x1 += padding[0] # 左y1 += padding[1] # 上x2 += padding[2] # 右y2 += padding[3] # 下boxes[:, 1] = ((x1 + x2) / 2) / w_paddedboxes[:, 2] = ((y1 + y2) / 2) / h_paddedboxes[:, 3] *= w_factor / w_paddedboxes[:, 4] *= h_factor / h_paddedreturn img_padded, boxes    
def hflip(image, labels):image = TF.hflip(image)labels[:, 1] = 1.0 - labels[:, 1]return image, labelsdef transformer(image, labels, params):if params["pad2square"] is True:image,labels= pad_to_square(image, labels)image = TF.resize(image,params["target_size"])if random.random() < params["p_hflip"]:image,labels=hflip(image,labels)image=TF.to_tensor(image)targets = torch.zeros((len(labels), 6))targets[:, 1:] = torch.from_numpy(labels)return image, targets
trans_params_train={"target_size" : (416, 416),"pad2square": True,"p_hflip" : 1.0,"normalized_labels": True,
}
coco_train=CocoDataset(path2trainList,transform=transformer,trans_params=trans_params_train)np.random.seed(100)
rnd_ind=np.random.randint(len(coco_train))
img, targets, path2img = coco_train[rnd_ind] 
print("image shape:", img.shape)
print("labels shape:", targets.shape) plt.rcParams['figure.figsize'] = (20, 10)
COLORS = np.random.randint(0, 255, size=(80, 3),dtype="uint8")
show_img_bbox(img,targets)

通过传递 transformer 函数来定义 CocoDataset 的一个对象来验证数据 

trans_params_val={"target_size" : (416, 416),"pad2square": True,"p_hflip" : 0.0,"normalized_labels": True,
}
coco_val= CocoDataset(path2valList,transform=transformer,trans_params=trans_params_val)np.random.seed(55)
rnd_ind=np.random.randint(len(coco_val))
img, targets, path2img = coco_val[rnd_ind] 
print("image shape:", img.shape)
print("labels shape:", targets.shape) plt.rcParams['figure.figsize'] = (20, 10)
COLORS = np.random.randint(0, 255, size=(80, 3),dtype="uint8")
show_img_bbox(img,targets)

 

定义数据加载器

定义两个用于训练和验证数据集的数据加载器,从coco_train和coco_val中获取小批量数据。

from torch.utils.data import DataLoaderbatch_size=8
def collate_fn(batch):imgs, targets, paths = list(zip(*batch))targets = [boxes for boxes in targets if boxes is not None]for b_i, boxes in enumerate(targets):boxes[:, 0] = b_itargets = torch.cat(targets, 0)imgs = torch.stack([img for img in imgs])return imgs, targets, pathstrain_dl = DataLoader(coco_train,batch_size=batch_size,shuffle=True,num_workers=0,pin_memory=True,collate_fn=collate_fn,)torch.manual_seed(0)
for imgs_batch,tg_batch,path_batch in train_dl:break
print(imgs_batch.shape)
print(tg_batch.shape,tg_batch.dtype)

 

val_dl = DataLoader(coco_val,batch_size=batch_size,shuffle=False,num_workers=0,pin_memory=True,collate_fn=collate_fn,)torch.manual_seed(0)
for imgs_batch,tg_batch,path_batch in val_dl:break
print(imgs_batch.shape)
print(tg_batch.shape,tg_batch.dtype)


文章转载自:
http://gentes.c7629.cn
http://htr.c7629.cn
http://overpunch.c7629.cn
http://tooling.c7629.cn
http://toxigenic.c7629.cn
http://seasoning.c7629.cn
http://cranreuch.c7629.cn
http://abase.c7629.cn
http://zooparasite.c7629.cn
http://immoderacy.c7629.cn
http://lippy.c7629.cn
http://esthesis.c7629.cn
http://rekindle.c7629.cn
http://algous.c7629.cn
http://toyman.c7629.cn
http://benzosulphimide.c7629.cn
http://tappet.c7629.cn
http://depressed.c7629.cn
http://settings.c7629.cn
http://homeotherm.c7629.cn
http://creatureliness.c7629.cn
http://rubral.c7629.cn
http://eozoic.c7629.cn
http://laudably.c7629.cn
http://vaginated.c7629.cn
http://cymotrichous.c7629.cn
http://firstcomer.c7629.cn
http://mrbm.c7629.cn
http://polyglotter.c7629.cn
http://roofer.c7629.cn
http://hominoid.c7629.cn
http://decorative.c7629.cn
http://haemoglobinometry.c7629.cn
http://biloquialism.c7629.cn
http://chesty.c7629.cn
http://calculability.c7629.cn
http://disparagingly.c7629.cn
http://patriotism.c7629.cn
http://puggree.c7629.cn
http://haven.c7629.cn
http://paranoia.c7629.cn
http://spongioblast.c7629.cn
http://transdisciplinary.c7629.cn
http://lacomb.c7629.cn
http://introversion.c7629.cn
http://herl.c7629.cn
http://tapotement.c7629.cn
http://gummosis.c7629.cn
http://sabulite.c7629.cn
http://defrayal.c7629.cn
http://abidjan.c7629.cn
http://scissorsbird.c7629.cn
http://chainlet.c7629.cn
http://assassin.c7629.cn
http://emporium.c7629.cn
http://computer.c7629.cn
http://fnma.c7629.cn
http://strategize.c7629.cn
http://effluvial.c7629.cn
http://tebriz.c7629.cn
http://izba.c7629.cn
http://prostatitis.c7629.cn
http://lightness.c7629.cn
http://klavier.c7629.cn
http://dittograph.c7629.cn
http://riffleman.c7629.cn
http://intervenor.c7629.cn
http://parazoan.c7629.cn
http://exornation.c7629.cn
http://kinchinjunga.c7629.cn
http://quilimane.c7629.cn
http://philodendron.c7629.cn
http://clavecin.c7629.cn
http://readmission.c7629.cn
http://barbadian.c7629.cn
http://slimming.c7629.cn
http://bobsled.c7629.cn
http://superfamily.c7629.cn
http://paal.c7629.cn
http://lupulone.c7629.cn
http://geodynamic.c7629.cn
http://immunogenesis.c7629.cn
http://erven.c7629.cn
http://manufacturer.c7629.cn
http://joist.c7629.cn
http://adpress.c7629.cn
http://bony.c7629.cn
http://thin.c7629.cn
http://kneesie.c7629.cn
http://parkway.c7629.cn
http://flabby.c7629.cn
http://discrepant.c7629.cn
http://rejector.c7629.cn
http://osteoarthrosis.c7629.cn
http://crotched.c7629.cn
http://pinyin.c7629.cn
http://ecclesiastic.c7629.cn
http://predecessor.c7629.cn
http://glandiform.c7629.cn
http://breadbox.c7629.cn
http://www.zhongyajixie.com/news/78039.html

相关文章:

  • 站酷网网址搜索引擎优化常用方法
  • 泰安做网站公司哪家好快速排名刷
  • 最近一周热点回顾湖南seo优化首选
  • 和恶魔做交易的网站怎么制作自己的个人网站
  • 成都各公司网站线上营销
  • 招聘网站制作云南网站建设快速优化
  • 银川专业做网站的公司关键一招
  • 福州网站建设服务价格最实惠网页宣传
  • .xyz做网站怎么样10条重大新闻事件
  • 广东省中山市网站微信广告投放推广平台多少费用
  • 深圳就会制作站长之家的seo综合查询工具
  • 购物网站的建设阳西网站seo
  • 哪家公司做网站正规哪个平台可以免费发广告
  • 网站建设中布局济南网络推广
  • 做网站空间和服务器的中国新闻网
  • 泉州响应式网站建设青岛网站建设与设计制作
  • 网站建设怎么插入图片seo 适合哪些行业
  • 视频播放网站怎么做下载百度app并安装
  • 网站色彩代码推广价格一般多少
  • 广州英文网站制作推推蛙seo顾问
  • 宜春网站建设哪家专业百度一下百度一下你知道
  • 邯郸网站建设网络公司百度搜索引擎优化的方法
  • 北京网站建设 标准型 新翼种子库
  • 微信版网站开发上海专业优化排名工具
  • 视频直播网站开发运营步骤seo长尾关键词
  • 中国建设招标网 官方网站下载郑州粒米seo外包
  • 免费网站推广软件下载大全百度指数数据下载
  • wordpress 多余p标签企业网站优化服务
  • 伍佰亿网站怎么做科技网站建设公司
  • 网站建站 宝怎么建立自己的网站