js在自定义对象中添加数据_用于自定义对象检测的数据增强

js在自定义对象中添加数据

So, If you are here then you might be enthusiast towards learning data augmentation, Object detection, machine learning, deep learning or image processing. And, you might have worked on image classification task where you might have done the data augmentation steps.

因此,如果您在这里,那么您可能会更热衷于学习数据增强,对象检测,机器学习,深度学习或图像处理。 并且,您可能已经完成了图像分类任务,其中您可能已经完成了数据扩充步骤。

Here is a demo video of Custom Object detection that I have trained with 200 Labeled image data and after augmentation the final images were 1400

这是一个自定义对象检测的演示视频,我用200个带标签的图像数据进行了训练,增强后的最终图像为1400个

那么,什么是“数据增强”? (So, what is “Data Augmentation” ?)

Data augmentation is a strategy that enables practitioners to significantly increase the diversity of data available for training models, without actually collecting new data. Data augmentation techniques such as cropping, padding, and horizontal flipping are commonly used to train large neural networks.

数据扩充 是一种策略,使从业人员可以显着增加 可用于训练模型 数据 的多样性 ,而无需实际收集新 数据 诸如裁剪,填充和水平翻转之类的 数据增强 技术通常用于训练大型神经网络。

So, now you have idea what is data augmentation.

所以,现在您知道什么是数据扩充。

If you have worked on data augmentation in Image Classification problems you might aware of some data augmentation steps like:

如果您曾从事图像分类问题中的数据增强工作,则可能会注意到一些数据增强步骤,例如:

  • Image Rotation

    影像旋转
  • Add Noise

    增加噪音
  • Image Flipping

    图像翻转
  • And, many others are there

    还有很多其他的

Now, you might have thought how we can use these techniques for Object detection?? hmm The challenge is :

现在,您可能已经想到了如何使用这些技术进行对象检测? 嗯,挑战是:

  • Yes, we can try these mentioned methods and then we can again do the image labeling for all the newly created image. But, Seriously? are you going to repeat those labeling process for all the same but altered images? Big NO

    是的,我们可以尝试这些提到的方法,然后我们可以再次为所有新创建的图像进行图像标记。 但是, 认真吗? 您要为所有相同但已更改的图像重复这些标记过程吗? 大NO

So, here is a way:

因此,这是一种方法:

  • Why not we can just apply transformation for both the images and labels together? BOOM!! Yes, we will do the same transformation for image and label.

    为什么不将图像和标签一起应用变换呢? 繁荣!! 是的,我们将对图像和标签进行相同的转换。

怎么样 (How)

Here is the piece of code that can help us. :)

这是可以帮助我们的代码。 :)

We will use Jupyter Notebook for running the below

我们将使用Jupyter Notebook运行以下内容

# Importing Libraries
import os
import numpy as np
import cv2
import argparse
import time
from tqdm import tqdm#convert from Yolo_mark to opencv format
def yoloFormattocv(x1, y1, x2, y2, H, W):
bbox_width = x2 * W
bbox_height = y2 * H
center_x = x1 * W
center_y = y1 * H
voc = []
voc.append(center_x - (bbox_width / 2))
voc.append(center_y - (bbox_height / 2))
voc.append(center_x + (bbox_width / 2))
voc.append(center_y + (bbox_height / 2))
return [int(v) for v in voc]

Convert from opencv format to yolo format

从opencv格式转换为yolo格式

# I
# Convert from opencv format to yolo format
# H,W is the image height and width
def cvFormattoYolo(corner, H, W):
bbox_W = corner[3] - corner[1]
bbox_H = corner[4] - corner[2]
center_bbox_x = (corner[1] + corner[3]) / 2
center_bbox_y = (corner[2] + corner[4]) / 2
return corner[0], round(center_bbox_x / W, 6),
round(center_bbox_y / H, 6),
round(bbox_W / W, 6),
round(bbox_H / H, 6)

Here we will rotate bounding box that we have created using labelImg

在这里,我们将旋转使用labelImg创建的边界框

class yoloRotatebbox:
def __init__(self, filename, image_ext, angle):
assert os.path.isfile(filename + image_ext)
assert os.path.isfile(filename + '.txt')
self.filename = filename
self.image_ext = image_ext
self.angle = angle
# Read image using cv2
self.image = cv2.imread(self.filename + self.image_ext, 1)
rotation_angle = self.angle * np.pi / 180
self.rot_matrix = np.array(
[[np.cos(rotation_angle), -np.sin(rotation_angle)], [np.sin(rotation_angle), np.cos(rotation_angle)]]) def rotateYolobbox(self): new_height, new_width = self.rotate_image().shape[:2] f = open(self.filename + '.txt', 'r') f1 = f.readlines() new_bbox = [] H, W = self.image.shape[:2] for x in f1:
bbox = x.strip('\n').split(' ')
if len(bbox) > 1:
(center_x, center_y, bbox_width, bbox_height) = yoloFormattocv(float(bbox[1]), float(bbox[2]),
float(bbox[3]), float(bbox[4]), H, W) upper_left_corner_shift = (center_x - W / 2, -H / 2 + center_y)
upper_right_corner_shift = (bbox_width - W / 2, -H / 2 + center_y)
lower_left_corner_shift = (center_x - W / 2, -H / 2 + bbox_height)
lower_right_corner_shift = (bbox_width - W / 2, -H / 2 + bbox_height) new_lower_right_corner = [-1, -1]
new_upper_left_corner = [] for i in (upper_left_corner_shift, upper_right_corner_shift, lower_left_corner_shift,
lower_right_corner_shift):
new_coords = np.matmul(self.rot_matrix, np.array((i[0], -i[1])))
x_prime, y_prime = new_width / 2 + new_coords[0], new_height / 2 - new_coords[1]
if new_lower_right_corner[0] < x_prime:
new_lower_right_corner[0] = x_prime
if new_lower_right_corner[1] < y_prime:
new_lower_right_corner[1] = y_prime if len(new_upper_left_corner) > 0:
if new_upper_left_corner[0] > x_prime:
new_upper_left_corner[0] = x_prime
if new_upper_left_corner[1] > y_prime:
new_upper_left_corner[1] = y_prime
else:
new_upper_left_corner.append(x_prime)
new_upper_left_corner.append(y_prime)
# print(x_prime, y_prime) new_bbox.append([bbox[0], new_upper_left_corner[0], new_upper_left_corner[1],
new_lower_right_corner[0], new_lower_right_corner[1]])
return new_bbox def rotate_image(self):
"""
Rotates an image (angle in degrees) and expands image to avoid cropping
"""
height, width = self.image.shape[:2] # image shape has 3 dimensions
image_center = (width / 2,
height / 2) # getRotationMatrix2D needs coordinates in reverse order (width, height) compared to shape rotation_mat = cv2.getRotationMatrix2D(image_center, self.angle, 1.) # rotation calculates the cos and sin, taking absolutes of those.
abs_cos = abs(rotation_mat[0, 0])
abs_sin = abs(rotation_mat[0, 1]) # find the new width and height bounds
bound_w = int(height * abs_sin + width * abs_cos)
bound_h = int(height * abs_cos + width * abs_sin) # subtract old image center (bringing image back to origin) and adding the new image center coordinates
rotation_mat[0, 2] += bound_w / 2 - image_center[0]
rotation_mat[1, 2] += bound_h / 2 - image_center[1] # rotate image with the new bounds and translated rotation matrix
rotated_mat = cv2.warpAffine(self.image, rotation_mat, (bound_w, bound_h))
return rotated_mat

Now, we will call out methods to do the task. Here, I am using Image Rotation. Other augmentation methods can be as usual.

现在,我们将调出方法来完成任务。 在这里,我正在使用图像旋转。 其他增强方法可以像往常一样。

if __name__ == "__main__":
angels=[45,90,135,180,225,270,315]
for filename in tqdm(os.listdir()):
file =filename.split(".")
if(file[-1]=="jpg"):
image_name=file[0]
image_ext="."+file[1]
else:
continue
for angle in angels:
im = yoloRotatebbox(image_name, image_ext, angle)
bbox = im.rotateYolobbox()
image = im.rotate_image() # to write rotateed image to disk
cv2.imwrite(image_name+'_' + str(angle) + '.jpg', image) file_name = image_name+'_' + str(angle) + '.txt'
#print("For angle "+str(angle))
if os.path.exists(file_name):
os.remove(file_name) # to write the new rotated bboxes to file
for i in bbox:
with open(file_name, 'a') as fout:
fout.writelines(
' '.join(map(str, cvFormattoYolo(i, im.rotate_image().shape[0], im.rotate_image().shape[1]))) + '\n')

So, here it is we have used 250 images to create 1400 images for Yolov4 without creating bounding box for all the augmented images. :)

因此,这里我们使用250张图像为Yolov4创建1400张图像,而没有为所有增强图像创建边界框。 :)

翻译自: https://medium.com/swlh/data-augmentation-for-custom-object-detection-15674966e0c8

js在自定义对象中添加数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值