EfficientDet代码解读-efficientdet

更新时间:2023-06-18 16:21:06 阅读: 评论:0

EfficientDet代码解读-efficientdet efficientdet
datat.py
CocoDatat
init
def__init__(lf, root_dir,t='train2017', transform=None):
<_dir = root_dir # datats/coco/
# datats/coco/ : 这个⽬录下有annotations、train2017、test2017、val2017;
# annotations :这个⽬录下有instances_train2017、instances_test2017、instances_val2017;      lf.t_name =t
< = COCO(os.path._dir,'annotations','instances_'+ lf.t_name +'.json'))
lf.image_ids = ImgIds()
lf.load_class()
load_class
def load_class(lf):
# load class names (name -> label)
# 将所有类别读取出来,并排序。
categories = lf.coco.etCatIds())
categories.sort(key=lambda x: x['id'])
lf.class ={}# {name:cls_len_index} 为每个类别建⽴索引
<_labels ={}# {0:0,1:1,...}({cls_len_index:cate_id})
手撕鸡的做法<_labels_inver ={}# {0:0,1:1,...}({cate_id:cls_len_index})
for c in categories:
<_labels[len(lf.class)]= c['id']
<_labels_inver[c['id']]=len(lf.class)
lf.class[c['name']]=len(lf.class)
# also load the rever (label -> name)
lf.labels ={}# {cls_len_index:name} ⽅便以后根据预测序号找到实际类别名称
for key, value in lf.class.items():
lf.labels[value]= key
load_image
def load_image(lf, image_index):
image_info = lf.coco.loadImgs(lf.image_ids[image_index])[0]#通过image的id找到对应的image_info,
path = os.path._dir, lf.t_name, image_info['file_name'])# image_info中包含了image的file_name        img = cv2.imread(path)# 根据file_name结合路径,就可以做图⽚读取操作
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# 转换图⽚的通道
return img.astype(np.float32)/255.# 图像缩⼩到【0,1】范围内
load_annotations
def load_annotations(lf, image_index):
# get ground truth annotations
# 根据图⽚的索引得到该图⽚所有标注信息的id,
annotations_ids = AnnIds(imgIds=lf.image_ids[image_index], iscrowd=Fal)        annotations = np.zeros((0,5))# 当没有标注信息的时候,直接返回该变量box:4+category_id:1
# some images appear to miss annotations
if len(annotations_ids)==0:
return annotations
# par annotations # 根据标注id找到标注的详细信息。
coco_annotations = lf.coco.loadAnns(annotations_ids)
for idx, a in enumerate(coco_annotations):
# some annotations have basically no width / height, skip them
# 对于没有宽或者⾼的标注,直接跳过
if a['bbox'][2]<1or a['bbox'][3]<1:
continue
annotation = np.zeros((1,5))
annotation[0,:4]= a['bbox']
annotation[0,4]= lf.coco_label_to_label(a['category_id'])
annotations = np.append(annotations, annotation, axis=0)
# transform from [x, y, w, h] to [x1, y1, x2, y2]
annotations[:,2]= annotations[:,0]+ annotations[:,2]
公主礼annotations[:,3]= annotations[:,1]+ annotations[:,3]
return annotations
collater
def collater(data):
imgs =[s['img']for s in data]
annots =[s['annot']for s in data]
scales =[s['scale']for s in data]
imgs = torch.from_numpy(np.stack(imgs, axis=0))
# 这批图⽚中最多annot是多少,之后会对不⾜这个数值的进⾏-1值pad.
max_num_annots =max(annot.shape[0]for annot in annots)
if max_num_annots >0:
annot_padded = s((len(annots), max_num_annots,5))*-1
if max_num_annots >0:
for idx, annot in enumerate(annots):
if annot.shape[0]>0:
annot_padded[idx,:annot.shape[0],:]= annot
el:
annot_padded = s((len(annots),1,5))*-1
imgs = imgs.permute(0,3,1,2)
return{'img': imgs,'annot': annot_padded,'scale': scales}
Resizer
class Resizer(object):
"""Convert ndarrays in sample to Tensors."""
"""
1. 等⽐例缩放长宽,缩放后长宽⽐不变;
2. padding长宽到指定img_size;
3. 按照缩放⽐例修改annots;
"""
def__init__(lf, img_size=512):
lf.img_size = img_size
def__call__(lf, sample):
image, annots = sample['img'], sample['annot']
height, width, _ = image.shape海蜇头怎么做好吃
if height > width:
书犹药也
scale = lf.img_size / height
resized_height = lf.img_size
resized_width =int(width * scale)
el:
scale = lf.img_size / width
resized_height =int(height * scale)
resized_width = lf.img_size
image = size(image,(resized_width, resized_height), interpolation=cv2.INTER_LINEAR) ## padding操作,将图像长宽padding到512⼤⼩。
new_image = np.zeros((lf.img_size, lf.img_size,3))
new_image[0:resized_height,0:resized_width]= image
## 因为缩放了,所以修改标注宽的数值
annots[:,:4]*= scale
素炒西葫芦
return{'img': torch.from_numpy(new_image).to(torch.float32),'annot': torch.from_numpy(annots),'scale': scale}
Augmenter
class Augmenter(object):
"""Convert ndarrays in sample to Tensors."""
"""
1. 数据增强,将图⽚按照第⼆维度反转;
2. 修改annots;
"""
nba历史总得分榜排名def__call__(lf, sample, flip_x=0.5):
if np.random.rand()< flip_x:
image, annots = sample['img'], sample['annot']
眼毛>定风波教案
image = image[:,::-1,:]
rows, cols, channels = image.shape
x1 = annots[:,0].copy()# copy is deepcopy in numpy
x2 = annots[:,2]#.copy()
# x_tmp = x1.deepcopy()
annots[:,0]= cols - x2
annots[:,2]= cols - x1
sample ={'img': image,'annot': annots}
return sample
Normalizer

本文发布于:2023-06-18 16:21:06,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1044246.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:标注   长宽   找到   缩放   类别   数据   时候
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图