class Grid(object):
def __init__(self, d1, d2, rotate=1, ratio=0.5, mode=0, prob=0.8):
self.d1 = d1
self.d2 = d2
self.rotate = rotate
self.ratio = ratio
self.mode = mode
self.st_prob = self.prob = prob
def set_prob(self, epoch, max_epoch):
self.prob = self.st_prob *min(1, epoch / max_epoch)
def __call__(self, img):if np.random.rand()> self.prob:return img
h = img.size(1)
w = img.size(2)
# 1.5* h,1.5* w works fine with the squared images
# But with rectangular input, the mask might not be able to recover back to the input image shape
# A square mask with edge length equal to the diagnoal of the input image
# will be able to cover all the image spot after the rotation. This is also the minimum square.
hh = math.ceil((math.sqrt(h * h + w * w)))
d = np.random.randint(self.d1, self.d2)
# d = self.d
# maybe use ceil? but i guess no big difference
self.l = math.ceil(d * self.ratio)
mask = np.ones((hh, hh), np.float32)
st_h = np.random.randint(d)
st_w = np.random.randint(d)for i inrange(-1, hh // d + 1):
s = d * i + st_h
t = s + self.l
s =max(min(s, hh),0)
t =max(min(t, hh),0)
mask[s:t,:]*=0for i inrange(-1, hh // d + 1):
s = d * i + st_w
t = s + self.l
s =max(min(s, hh),0)
t =max(min(t, hh),0)
mask[:, s:t]*=0
r = np.random.randint(self.rotate)
mask = Image.fromarray(np.uint8(mask))
mask = mask.rotate(r)
mask = np.asarray(mask)
mask = mask[(hh - h)// 2:(hh - h) // 2 + h, (hh - w) // 2:(hh - w) // 2 + w]
mask = torch.from_numpy(mask)if self.mode ==1:
mask =1- mask
mask = mask.expand_as(img)
img = img * mask
return img
class GridMask(nn.Module):
def __init__(self, d1=56, d2=128, rotate=360, ratio=0.4, mode=1, prob=0.8):super(GridMask, self).__init__()
self.rotate = rotate
self.ratio = ratio
self.mode = mode
self.st_prob = prob
self.grid =Grid(d1, d2, rotate, ratio, mode, prob)
def set_prob(self, epoch, max_epoch):
self.grid.set_prob(epoch, max_epoch)
def forward(self, x):if not self.training:return x
# n, c, h, w = x.size()
# y =[]
# for i inrange(n):
# y.append(self.grid(x[i]))
#
# y = torch.cat(y).view(n, c, h, w)return self.grid(x)
Open World Object Detection in the Era of Foundation Models 摘要介绍相关工作开放词汇物体检测开放世界目标检测类无关的目标检测3.真实世界目标检测基准3.1 数据集细节3.2 基准架构3.3 什么是一个未知对象4. 利用基准模型用于开放世界目标检测4.1 背景4.2 属性生成4.3 属性…