text
stringlengths
1
93.6k
given_img_np = cv2.resize(given_img_np_, dsize=(w, h), interpolation=cv2.INTER_CUBIC)
if b_enforce_connect:
spix_index_np = spix_index_np.astype(np.int64)
segment_size = (given_img_np_.shape[0] * given_img_np_.shape[1]) / (int(n_spixels) * 1.0)
min_size = int(0.06 * segment_size)
max_size = int(3 * segment_size)
spix_index_np = enforce_connectivity(spix_index_np[None, :, :], min_size, max_size)[0]
cur_max = np.max(given_img_np)
spixel_bd_image = mark_boundaries(given_img_np/cur_max, spix_index_np.astype(int), color = (0,1,1)) #cyna
return (cur_max*spixel_bd_image).astype(np.float32).transpose(2,0,1), spix_index_np #
# ============ accumulate Q =============================
def spixlIdx(args, b_train = False):
# code modified from ssn
if b_train:
n_spixl_h = int(np.floor(args.train_img_height / args.downsize))
n_spixl_w = int(np.floor(args.train_img_width / args.downsize))
else:
n_spixl_h = int(np.floor(args.input_img_height / args.downsize))
n_spixl_w = int(np.floor(args.input_img_width / args.downsize))
spix_values = np.int32(np.arange(0, n_spixl_w * n_spixl_h).reshape((n_spixl_h, n_spixl_w)))
spix_idx_tensor = shift9pos(spix_values)
torch_spix_idx_tensor = torch.from_numpy(
np.tile(spix_idx_tensor, (args.batch_size, 1, 1, 1))).type(torch.float).cuda()
return torch_spix_idx_tensor
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def __repr__(self):
return '{:.3f} ({:.3f})'.format(self.val, self.avg)
def batch2img(img):
b,_,h,w = img.shape
tmp = img.permute(0,2,3,1)
for i in range(b):
if i ==0:
tmp_stack = tmp[i,:,:,:]
else:
tmp_stack = torch.cat([tmp_stack,tmp[i,:,:,:]],dim=-2)
return tmp_stack
def build_LABXY_feat(label_in, XY_feat):
img_lab = label_in.clone().type(torch.float)
b, _, curr_img_height, curr_img_width = XY_feat.shape
scale_img = F.interpolate(img_lab, size=(curr_img_height,curr_img_width), mode='nearest')
LABXY_feat = torch.cat([scale_img, XY_feat],dim=1)
return LABXY_feat
def rgb2Lab_torch(img_in, mean_values = None):
# self implemented function that convert RGB image to LAB
# inpu img intense should be [0,1] float b*3*h*w
assert img_in.min() >= 0 and img_in.max()<=1
img= (img_in.clone() + mean_values.cuda()).clamp(0, 1)
mask = img > 0.04045
img[mask] = torch.pow((img[mask] + 0.055) / 1.055, 2.4)
img[~mask] /= 12.92
xyz_from_rgb = torch.tensor([[0.412453, 0.357580, 0.180423],
[0.212671, 0.715160, 0.072169],
[0.019334, 0.119193, 0.950227]]).cuda()
rgb = img.permute(0,2,3,1)
xyz_img = torch.matmul(rgb, xyz_from_rgb.transpose_(0,1))
xyz_ref_white = torch.tensor([0.95047, 1., 1.08883]).cuda()
# scale by CIE XYZ tristimulus values of the reference white point
lab = xyz_img / xyz_ref_white
# Nonlinear distortion and linear transformation
mask = lab > 0.008856
lab[mask] = torch.pow(lab[mask], 1. / 3.)