| import torch |
| import torch.nn.functional as F |
| import numpy as np |
|
|
|
|
| def eightway_activation(x): |
| """Retrieves neighboring pixels/features on the eight corners from |
| a 3x3 patch. |
| |
| Args: |
| x: A tensor of size [batch_size, height_in, width_in, channels] |
| |
| Returns: |
| A tensor of size [batch_size, height_in, width_in, channels, 8] |
| """ |
| |
| shape_x = list(x.shape) |
| if len(shape_x) != 4: |
| raise ValueError('Only support for 4-D tensors!') |
|
|
| |
| x = F.pad(x, |
| pad=(0, 0, 1, 1, 1, 1, 0, 0), |
| mode='reflect') |
| |
| x_groups = [ |
| x[:, 1:-1, :-2, :].clone(), |
| x[:, 1:-1, 2:, :].clone(), |
| x[:, :-2, 1:-1, :].clone(), |
| x[:, 2:, 1:-1, :].clone(), |
| x[:, :-2, :-2, :].clone(), |
| x[:, 2:, :-2, :].clone(), |
| x[:, :-2, 2:, :].clone(), |
| x[:, 2:, 2:, :].clone() |
| ] |
| output = [ |
| torch.unsqueeze(c, dim=-1) for c in x_groups |
| ] |
| output = torch.cat(output, dim=-1) |
|
|
| return output |
|
|
|
|
| def eightcorner_activation(x, size): |
| """Retrieves neighboring pixels one the eight corners from a |
| (2*size+1)x(2*size+1) patch. |
| |
| Args: |
| x: A tensor of size [batch_size, height_in, width_in, channels] |
| size: A number indicating the half size of a patch. |
| |
| Returns: |
| A tensor of size [batch_size, height_in, width_in, channels, 8] |
| """ |
| |
| shape_x = list(x.shape) |
| if len(shape_x) != 4: |
| raise ValueError('Only support for 4-D tensors!') |
| n, c, h, w = shape_x |
|
|
| |
| p = size |
| x_pad = F.pad(x, |
| pad=(p, p, p, p, 0, 0, 0, 0), |
| mode='constant', |
| value=0) |
|
|
| |
| x_groups = [] |
| for st_y in range(0, 2 * size + 1, size): |
| for st_x in range(0, 2 * size + 1, size): |
| if st_y == size and st_x == size: |
| |
| continue |
|
|
| x_neighbor = x_pad[:, :, st_y:st_y + h, st_x:st_x + w].clone() |
| x_groups.append(x_neighbor) |
|
|
| output = [torch.unsqueeze(c, dim=-1) for c in x_groups] |
| output = torch.cat(output, dim=-1) |
|
|
| return output |
|
|
|
|
| def ignores_from_label(labels, num_classes, size, ignore_index): |
| """Retrieves ignorable pixels from the ground-truth labels. |
| |
| This function returns a binary map in which 1 denotes ignored pixels |
| and 0 means not ignored ones. For those ignored pixels, they are not |
| only the pixels with label value >= num_classes, but also the |
| corresponding neighboring pixels, which are on the the eight cornerls |
| from a (2*size+1)x(2*size+1) patch. |
| |
| Args: |
| labels: A tensor of size [batch_size, height_in, width_in], indicating |
| semantic segmentation ground-truth labels. |
| num_classes: A number indicating the total number of valid classes. The |
| labels ranges from 0 to (num_classes-1), and any value >= num_classes |
| would be ignored. |
| size: A number indicating the half size of a patch. |
| |
| Return: |
| A tensor of size [batch_size, height_in, width_in, 8] |
| """ |
| |
| shape_lab = list(labels.shape) |
| if len(shape_lab) != 3: |
| raise ValueError('Only support for 3-D label tensors!') |
| n, h, w = shape_lab |
|
|
| |
| |
| ignore = (labels == ignore_index) |
|
|
| |
| p = size |
| ignore_pad = F.pad(ignore, |
| pad=(p, p, p, p, 0, 0), |
| mode='constant', |
| value=1) |
|
|
| |
| |
| |
| |
| ignore_groups = [] |
| for st_y in range(2 * size, -1, -size): |
| for st_x in range(2 * size, -1, -size): |
| if st_y == size and st_x == size: |
| continue |
| ignore_neighbor = ignore_pad[:, st_y:st_y + h, st_x:st_x + w].clone() |
| mask = ignore_neighbor | ignore |
| ignore_groups.append(mask) |
|
|
| ig = 0 |
| for st_y in range(0, 2 * size + 1, size): |
| for st_x in range(0, 2 * size + 1, size): |
| if st_y == size and st_x == size: |
| continue |
| ignore_neighbor = ignore_pad[:, st_y:st_y + h, st_x:st_x + w].clone() |
| mask = ignore_neighbor | ignore_groups[ig] |
| ignore_groups[ig] = mask |
| ig += 1 |
|
|
| ignore_groups = [ |
| torch.unsqueeze(c, dim=-1) for c in ignore_groups |
| ] |
| ignore = torch.cat(ignore_groups, dim=-1) |
|
|
| return ignore |
|
|
|
|
| def edges_from_label(labels, size, ignore_class=255): |
| """Retrieves edge positions from the ground-truth labels. |
| |
| This function computes the edge map by considering if the pixel values |
| are equal between the center and the neighboring pixels on the eight |
| corners from a (2*size+1)*(2*size+1) patch. Ignore edges where the any |
| of the paired pixels with label value >= num_classes. |
| |
| Args: |
| labels: A tensor of size [batch_size, height_in, width_in], indicating |
| semantic segmentation ground-truth labels. |
| size: A number indicating the half size of a patch. |
| ignore_class: A number indicating the label value to ignore. |
| |
| Return: |
| A tensor of size [batch_size, height_in, width_in, 1, 8] |
| """ |
| |
| shape_lab = list(labels.shape) |
| if len(shape_lab) != 4: |
| raise ValueError('Only support for 4-D label tensors!') |
| n, h, w, c = shape_lab |
|
|
| |
| p = size |
| labels_pad = F.pad( |
| labels, pad=(0, 0, p, p, p, p, 0, 0), |
| mode='constant', |
| value=ignore_class) |
|
|
| |
| edge_groups = [] |
| for st_y in range(0, 2 * size + 1, size): |
| for st_x in range(0, 2 * size + 1, size): |
| if st_y == size and st_x == size: |
| continue |
| labels_neighbor = labels_pad[:, st_y:st_y + h, st_x:st_x + w] |
| edge = labels_neighbor != labels |
| edge_groups.append(edge) |
|
|
| edge_groups = [ |
| torch.unsqueeze(c, dim=-1) for c in edge_groups |
| ] |
| edge = torch.cat(edge_groups, dim=-1) |
|
|
| return edge |
|
|