cut it out什么意思_cutout例句

cut it out什么意思_cutout例句1.几种数据增强的比较Mixup:将随机的两张样本按比例混合,分类的结果按比例分配;Cutout:随机的将样本中的部分区域cut掉,并且填充0像素值,分类的结果不变;CutMix:就是将一部分区域cut掉但不填充0像素而是随机填充训练集中的其他数据的区域像素值,分类结果按一定的比例分配区别上述三种数据增强的区别:cutout和cutmix就是填充区域像素值的区别;mixup和cutmix是混合两种…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

1.几种数据增强的比较

Mixup:将随机的两张样本按比例混合,分类的结果按比例分配;

Cutout:随机的将样本中的部分区域cut掉,并且填充0像素值,分类的结果不变;

CutMix:就是将一部分区域cut掉但不填充0像素而是随机填充训练集中的其他数据的区域像素值,分类结果按一定的比例分配

cut it out什么意思_cutout例句

区别

上述三种数据增强的区别:cutout和cutmix就是填充区域像素值的区别;mixup和cutmix是混合两种样本方式上的区别:mixup是将两张图按比例进行插值来混合样本,cutmix是采用cut部分区域再补丁的形式去混合图像,不会有图像混合后不自然的情形。

优点

(1)在训练过程中不会出现非信息像素,从而能够提高训练效率;

(2)保留了regional dropout的优势,能够关注目标的non-discriminative parts;

(3)通过要求模型从局部视图识别对象,对cut区域中添加其他样本的信息,能够进一步增强模型的定位能力;

(4)不会有图像混合后不自然的情形,能够提升模型分类的表现;

(5)训练和推理代价保持不变。

2.What does model learn with CutMix?

作者通过热力图,给出了结果。CutMix的操作使得模型能够从一幅图像上的局部视图上识别出两个目标,提高训练的效率。由图可以看出,Cutout能够使得模型专注于目标较难区分的区域(腹部),但是有一部分区域是没有任何信息的,会影响训练效率;Mixup的话会充分利用所有的像素信息,但是会引入一些非常不自然的伪像素信息。

cut it out什么意思_cutout例句

3. 查看CutMix代码

“””输入为:样本的size和生成的随机lamda值”””

def rand_bbox(size, lam):

W = size[2]

H = size[3]

“””1.论文里的公式2,求出B的rw,rh”””

cut_rat = np.sqrt(1. – lam)

cut_w = np.int(W * cut_rat)

cut_h = np.int(H * cut_rat)

# uniform

“””2.论文里的公式2,求出B的rx,ry(bbox的中心点)”””

cx = np.random.randint(W)

cy = np.random.randint(H)

#限制坐标区域不超过样本大小

bbx1 = np.clip(cx – cut_w // 2, 0, W)

bby1 = np.clip(cy – cut_h // 2, 0, H)

bbx2 = np.clip(cx + cut_w // 2, 0, W)

bby2 = np.clip(cy + cut_h // 2, 0, H)

“””3.返回剪裁B区域的坐标值”””

return bbx1, bby1, bbx2, bby2

整体流程

“””train.py 220-244行”””

for i, (input, target) in enumerate(train_loader):

# measure data loading time

data_time.update(time.time() – end)

input = input.cuda()

target = target.cuda()

r = np.random.rand(1)

if args.beta > 0 and r < args.cutmix_prob:

# generate mixed sample

“””1.设定lamda的值,服从beta分布”””

lam = np.random.beta(args.beta, args.beta)

“””2.找到两个随机样本”””

rand_index = torch.randperm(input.size()[0]).cuda()

target_a = target#一个batch

target_b = target[rand_index] #batch中的某一张

“””3.生成剪裁区域B”””

bbx1, bby1, bbx2, bby2 = rand_bbox(input.size(), lam)

“””4.将原有的样本A中的B区域,替换成样本B中的B区域”””

input[:, :, bbx1:bbx2, bby1:bby2] = input[rand_index, :, bbx1:bbx2, bby1:bby2]

# adjust lambda to exactly match pixel ratio

“””5.根据剪裁区域坐标框的值调整lam的值”””

lam = 1 – ((bbx2 – bbx1) * (bby2 – bby1) / (input.size()[-1] * input.size()[-2]))

# compute output

“””6.将生成的新的训练样本丢到模型中进行训练”””

output = model(input)

“””7.按lamda值分配权重”””

loss = criterion(output, target_a) * lam + criterion(output, target_b) * (1. – lam)

else:

# compute output

output = model(input)

loss = criterion(output, target)

3. 查看CutOut代码

import torch

import numpy as np

class Cutout(object):

“””Randomly mask out one or more patches from an image.

Args:

n_holes (int): Number of patches to cut out of each image.

length (int): The length (in pixels) of each square patch.

“””

def __init__(self, n_holes, length):

self.n_holes = n_holes

self.length = length

def __call__(self, img):

“””

Args:

img (Tensor): Tensor image of size (C, H, W).

Returns:

Tensor: Image with n_holes of dimension length x length cut out of it.

“””

h = img.size(1)

w = img.size(2)

mask = np.ones((h, w), np.float32)

for n in range(self.n_holes):

y = np.random.randint(h)

x = np.random.randint(w)

y1 = np.clip(y – self.length // 2, 0, h)

y2 = np.clip(y + self.length // 2, 0, h)

x1 = np.clip(x – self.length // 2, 0, w)

x2 = np.clip(x + self.length // 2, 0, w)

mask[y1: y2, x1: x2] = 0.

mask = torch.from_numpy(mask)

mask = mask.expand_as(img)

img = img * mask

return img

4.Mosaic数据增强方法

Yolov4的mosaic数据增强参考了CutMix数据增强方式,理论上具有一定的相似性。CutMix数据增强方式利用两张图片进行拼接,但是mosaic利用了四张图片,根据论文所说其拥有一个巨大的优点是丰富检测物体的背景,且在BN计算的时候一下子会计算四张图片的数据。

实现思路

1.每次读取四张图片

cut it out什么意思_cutout例句

2.分别对四张图片进行翻转、缩放、色域变化等,并且按照四个方向位置摆好。

cut it out什么意思_cutout例句

3.进行图片的组合和框的组合

cut it out什么意思_cutout例句

全部代码

from PIL import Image, ImageDraw

import numpy as np

from matplotlib.colors import rgb_to_hsv, hsv_to_rgb

import math

def rand(a=0, b=1):

return np.random.rand()*(b-a) + a

def merge_bboxes(bboxes, cutx, cuty):

merge_bbox = []

for i in range(len(bboxes)):

for box in bboxes[i]:

tmp_box = []

x1,y1,x2,y2 = box[0], box[1], box[2], box[3]

if i == 0:

if y1 > cuty or x1 > cutx:

continue

if y2 >= cuty and y1 <= cuty:

y2 = cuty

if y2-y1 < 5:

continue

if x2 >= cutx and x1 <= cutx:

x2 = cutx

if x2-x1 < 5:

continue

if i == 1:

if y2 < cuty or x1 > cutx:

continue

if y2 >= cuty and y1 <= cuty:

y1 = cuty

if y2-y1 < 5:

continue

if x2 >= cutx and x1 <= cutx:

x2 = cutx

if x2-x1 < 5:

continue

if i == 2:

if y2 < cuty or x2 < cutx:

continue

if y2 >= cuty and y1 <= cuty:

y1 = cuty

if y2-y1 < 5:

continue

if x2 >= cutx and x1 <= cutx:

x1 = cutx

if x2-x1 < 5:

continue

if i == 3:

if y1 > cuty or x2 < cutx:

continue

if y2 >= cuty and y1 <= cuty:

y2 = cuty

if y2-y1 < 5:

continue

if x2 >= cutx and x1 <= cutx:

x1 = cutx

if x2-x1 < 5:

continue

tmp_box.append(x1)

tmp_box.append(y1)

tmp_box.append(x2)

tmp_box.append(y2)

tmp_box.append(box[-1])

merge_bbox.append(tmp_box)

return merge_bbox

def get_random_data(annotation_line, input_shape, random=True, hue=.1, sat=1.5, val=1.5, proc_img=True):

”’random preprocessing for real-time data augmentation”’

h, w = input_shape

min_offset_x = 0.4

min_offset_y = 0.4

scale_low = 1-min(min_offset_x,min_offset_y)

scale_high = scale_low+0.2

image_datas = []

box_datas = []

index = 0

place_x = [0,0,int(w*min_offset_x),int(w*min_offset_x)]

place_y = [0,int(h*min_offset_y),int(w*min_offset_y),0]

for line in annotation_line:

# 每一行进行分割

line_content = line.split()

# 打开图片

image = Image.open(line_content[0])

image = image.convert(“RGB”)

# 图片的大小

iw, ih = image.size

# 保存框的位置

box = np.array([np.array(list(map(int,box.split(‘,’)))) for box in line_content[1:]])

# image.save(str(index)+”.jpg”)

# 是否翻转图片

flip = rand()<.5>

if flip and len(box)>0:

image = image.transpose(Image.FLIP_LEFT_RIGHT)

box[:, [0,2]] = iw – box[:, [2,0]]

# 对输入进来的图片进行缩放

new_ar = w/h

scale = rand(scale_low, scale_high)

if new_ar < 1:

nh = int(scale*h)

nw = int(nh*new_ar)

else:

nw = int(scale*w)

nh = int(nw/new_ar)

image = image.resize((nw,nh), Image.BICUBIC)

# 进行色域变换

hue = rand(-hue, hue)

sat = rand(1, sat) if rand()<.5 else sat>

val = rand(1, val) if rand()<.5 else val>

x = rgb_to_hsv(np.array(image)/255.)

x[…, 0] += hue

x[…, 0][x[…, 0]>1] -= 1

x[…, 0][x[…, 0]<0] += 1

x[…, 1] *= sat

x[…, 2] *= val

x[x>1] = 1

x[x<0] = 0

image = hsv_to_rgb(x)

image = Image.fromarray((image*255).astype(np.uint8))

# 将图片进行放置,分别对应四张分割图片的位置

dx = place_x[index]

dy = place_y[index]

new_image = Image.new(‘RGB’, (w,h), (128,128,128))

new_image.paste(image, (dx, dy))

image_data = np.array(new_image)/255

# Image.fromarray((image_data*255).astype(np.uint8)).save(str(index)+”distort.jpg”)

index = index + 1

box_data = []

# 对box进行重新处理

if len(box)>0:

np.random.shuffle(box)

box[:, [0,2]] = box[:, [0,2]]*nw/iw + dx

box[:, [1,3]] = box[:, [1,3]]*nh/ih + dy

box[:, 0:2][box[:, 0:2]<0] = 0

box[:, 2][box[:, 2]>w] = w

box[:, 3][box[:, 3]>h] = h

box_w = box[:, 2] – box[:, 0]

box_h = box[:, 3] – box[:, 1]

box = box[np.logical_and(box_w>1, box_h>1)]

box_data = np.zeros((len(box),5))

box_data[:len(box)] = box

image_datas.append(image_data)

box_datas.append(box_data)

img = Image.fromarray((image_data*255).astype(np.uint8))

for j in range(len(box_data)):

thickness = 3

left, top, right, bottom = box_data[j][0:4]

draw = ImageDraw.Draw(img)

for i in range(thickness):

draw.rectangle([left + i, top + i, right – i, bottom – i],outline=(255,255,255))

img.show()

# 将图片分割,放在一起

cutx = np.random.randint(int(w*min_offset_x), int(w*(1 – min_offset_x)))

cuty = np.random.randint(int(h*min_offset_y), int(h*(1 – min_offset_y)))

new_image = np.zeros([h,w,3])

new_image[:cuty, :cutx, :] = image_datas[0][:cuty, :cutx, :]

new_image[cuty:, :cutx, :] = image_datas[1][cuty:, :cutx, :]

new_image[cuty:, cutx:, :] = image_datas[2][cuty:, cutx:, :]

new_image[:cuty, cutx:, :] = image_datas[3][:cuty, cutx:, :]

# 对框进行进一步的处理

new_boxes = merge_bboxes(box_datas, cutx, cuty)

return new_image, new_boxes

def normal_(annotation_line, input_shape):

”’random preprocessing for real-time data augmentation”’

line = annotation_line.split()

image = Image.open(line[0])

box = np.array([np.array(list(map(int,box.split(‘,’)))) for box in line[1:]])

iw, ih = image.size

image = image.transpose(Image.FLIP_LEFT_RIGHT)

box[:, [0,2]] = iw – box[:, [2,0]]

return image, box

if __name__ == “__main__”:

with open(“2007_train.txt”) as f:

lines = f.readlines()

a = np.random.randint(0,len(lines))

# index = 0

# line_all = lines[a:a+4]

# for line in line_all:

# image_data, box_data = normal_(line,[416,416])

# img = image_data

# for j in range(len(box_data)):

# thickness = 3

# left, top, right, bottom = box_data[j][0:4]

# draw = ImageDraw.Draw(img)

# for i in range(thickness):

# draw.rectangle([left + i, top + i, right – i, bottom – i],outline=(255,255,255))

# img.show()

# # img.save(str(index)+”box.jpg”)

# index = index+1

line = lines[a:a+4]

image_data, box_data = get_random_data(line,[416,416])

img = Image.fromarray((image_data*255).astype(np.uint8))

for j in range(len(box_data)):

thickness = 3

left, top, right, bottom = box_data[j][0:4]

draw = ImageDraw.Draw(img)

for i in range(thickness):

draw.rectangle([left + i, top + i, right – i, bottom – i],outline=(255,255,255))

img.show()

# img.save(“box_all.jpg”)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/193682.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ghost备份还原系统步骤_win10如何备份完整系统

    ghost备份还原系统步骤_win10如何备份完整系统Ghost在XP时代可以说是装机必备,因为Ghost使用简单、快捷,直到现在仍然受到强力的追捧。说到备份和还原操作系统,Ghost绝对是一把好手,简单的操作、快速的恢复,让你的电脑重新焕发活力。工具/原料:带有PE的U盘方法/步骤:用启动盘启动电脑,使它进入PE系统,双击桌面上的Ghost备份还原图标。备份系统1.单击Local—->Partition—->ToImage2.选择系统所在的硬盘(这里显示的是硬件的硬盘列表)…

    2025年9月19日
    4
  • 国外android逆向的论坛,初探android逆向

    国外android逆向的论坛,初探android逆向好久没有更新博客了。一直在想要更新点什么样子的干货。最近看了一点有关于逆向的文章,感觉还不错。对于“安卓开发没人要了”这种话,我也很无奈,最近的RN,包括kotlin的出现,还有Flutter框架的出现。这些东西的出现感觉都像是意味着,往后的移动端开发不需要双倍的人员了。最近突然对逆向萌生了一点点兴趣,关于逆向的东西依旧有很多,很多apk对进行加固,加壳等等,这里的入门仅仅是对于没有加密等操作的…

    2025年6月3日
    2
  • Oracle11g软硬件基本要求,Oracle 11g的安装

    Oracle11g软硬件基本要求,Oracle 11g的安装Oracle11g有基本安装和高级安装两种方式。两种方式对硬件要求也不相同,oracle11g软件非常大,对硬件要求很高。目前只是讲述在windows环境下的安装,Linux环境下安装以后会讲,下表给出了安装Oracle11g所需的硬件配置。系统要求说明CPU最低主频550MHZ以上内存1GB以上虚拟内存物理内存的2倍磁盘空间基本安装需4.55G,高级安装需4.92G一、Windows环境下安装…

    2022年7月25日
    27
  • 【数据结构与算法】深入浅出递归和迭代的通用转换思想[通俗易懂]

    【数据结构与算法】深入浅出递归和迭代的通用转换思想[通俗易懂]递归和递归的深入浅出一般来说,能用迭代的地方就不要用递归!理论上讲,所有的递归和迭代之间都能相互转换!(一)何为迭代?首先我们来看下面这段简单的代码:intsum(intn){intsum=0;for(inti=1;i<=n;i++)sum+=n;//求解1~n的和returnsum;}从上述例子中,从1一直加到n,每一次的和都

    2022年6月5日
    34
  • gis中char是什么字段_gis中字段类型char

    gis中char是什么字段_gis中字段类型char维护一个字符串集合,支持两种操作:I x 向集合中插入一个字符串 x;Q x 询问一个字符串在集合中出现了多少次。共有 N 个操作,输入的字符串总长度不超过 105,字符串仅包含小写英文字母。输入格式第一行包含整数 N,表示操作数。接下来 N 行,每行包含一个操作指令,指令为 I x 或 Q x 中的一种。输出格式对于每个询问指令 Q x,都要输出一个整数作为结果,表示 x 在集合中出现的次数。每个结果占一行。数据范围1≤N≤2∗104输入样例:5I abcQ abcQ ab

    2022年8月8日
    3
  • Java初学者常用的开发工具都有哪些?[通俗易懂]

    Java初学者常用的开发工具都有哪些?[通俗易懂]俗话说的好“工欲善其事必先利其器”,要想学好java这门语言,选择一款好用顺手的开发工具是必不可少的。java初学者常用源码编辑工具介绍java源代码本质上其实就是普通的文本文件,所以理论上来说任何可以编辑文本文件的编辑器都可以作为我们的java代码编辑工具。比如:WINDOWS记事本、写字板、word等。但是这些简单工具没有语法的高亮提示、自动完成等功能,这些功能的缺失会大大降低代码的编写效率。学习开发时一般我们不会选用这些简…

    2022年7月7日
    17

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号