def forward(self, x, pool_indices=None, out_shapes=None): pool_indices = to_list(copy(pool_indices), self.num_layers)[::-1] shapes = to_list(copy(out_shapes), self.num_layers)[::-1] for i, conv in enumerate(self.convs): x = conv(x, pool_indices[i], shapes[i]) if isinstance(x, tuple): x, pool_indices[i], shapes[i] = x if self.return_pool_data: return x, pool_indices, shapes return x
def read_labels(dataset=None, key=None, filepath=None, verbose=False): """ Loads or collects the labels in a dataset. Args: dataset: lazy dataset providing example dicts key: key of the labels in an example dict filepath: file to load/store the labels from/at verbose: Returns: """ if filepath and Path(filepath).exists(): with filepath.open() as fid: labels = json.load(fid) if verbose: print(f'Restored labels from {filepath}') else: labels = set() for example in dataset: labels.update(to_list(example[key])) labels = sorted(labels) if filepath: with filepath.open('w') as fid: json.dump(labels, fid, sort_keys=True, indent=4) if verbose: print(f'Saved labels to {filepath}') return labels
def forward(self, x, size): sides = to_list(self.side, x.dim() - 2) sizes = to_list(size, x.dim() - 2) slc = [slice(None)] * x.dim() for i, (side, size) in enumerate(zip(sides, sizes)): idx = 2 + i if side is None or size < 1: continue elif side == 'front': slc[idx] = slice(size, x.shape[idx]) elif side == 'both': slc[idx] = slice(size // 2, -math.ceil(size / 2)) elif side == 'end': slc[idx] = slice(0, -size) else: raise ValueError x = x[tuple(slc)] return x
def forward(self, x, size): sides = to_list(self.side, x.dim() - 2) sizes = to_list(size, x.dim() - 2) pad = [] for side, size in list(zip(sides, sizes))[::-1]: if side is None or size < 1: pad.extend([0, 0]) elif side == 'front': pad.extend([size, 0]) elif side == 'both': pad.extend([size // 2, math.ceil(size / 2)]) elif side == 'end': pad.extend([0, size]) else: raise ValueError(f'pad side {side} unknown') x = F.pad(x, tuple(pad), mode=self.mode) return x
def get_out_shape(self, in_shape): if self.is_transpose: raise NotImplementedError else: out_shape = np.array(in_shape) out_shape_ = out_shape - (np.array(self.dilation) * (np.array(self.kernel_size) - 1)) out_shape = np.where( [pad is None for pad in to_list(self.padding)], out_shape_, out_shape) out_shape = np.ceil(out_shape / np.array(self.stride)) if self.pooling is not None: out_shape = out_shape / np.array(self.pool_size) out_shape_ = np.floor(out_shape) out_shape = np.where( [pad is None for pad in to_list(self.padding)], out_shape_, out_shape) out_shape = np.ceil(out_shape) return out_shape.astype(np.int64)
def pad(self, x): if self.is_transpose: return x padding = [pad is not None for pad in to_list(self.padding)] if any(padding): size = (np.array(self.dilation) * (np.array(self.kernel_size) - 1) - ((np.array(x.shape[2:]) - 1) % np.array(self.stride))).tolist() x = Pad(side=self.padding)(x, size=size) if not all(padding): size = ((np.array(x.shape[2:]) - np.array(self.kernel_size)) % np.array(self.stride)).tolist() x = Cut(side=('both' if not pad else None for pad in padding))(x, size) return x
def unpad(self, y, out_shape=None): if out_shape is not None: assert self.is_transpose size = np.array(y.shape[2:]) - np.array(out_shape) padding = [ 'both' if side is None else side for side in to_list(self.padding) ] if any(size > 0): y = Cut(side=padding)(y, size=size) if any(size < 0): y = Pad(side=padding, mode='constant')(y, size=-size) elif self.is_transpose and self.padding is not None: size = (np.array(self.dilation) * (np.array(self.kernel_size) - 1) - np.array(self.stride) + 1).tolist() y = Cut(side=self.padding)(y, size=size) return y
def __init__(self, in_channels, hidden_channels, out_channels, kernel_size, num_layers, dropout=0., padding='both', dilation=1, stride=1, norm=None, activation='relu', gated=False, pooling='max', pool_size=1, return_pool_data=False): super().__init__() self.in_channels = in_channels self.num_layers = num_layers num_hidden_layers = num_layers - int(out_channels is not None) self.hidden_channels = to_list(hidden_channels, num_hidden_layers) self.kernel_sizes = to_list(kernel_size, num_layers) self.paddings = to_list(padding, num_layers) self.dilations = to_list(dilation, num_layers) self.strides = to_list(stride, num_layers) self.poolings = to_list(pooling, num_layers) self.pool_sizes = to_list(pool_size, num_layers) self.out_channels = out_channels self.return_pool_data = return_pool_data convs = list() for i in range(num_hidden_layers): hidden_channels = self.hidden_channels[i] convs.append( self.conv_cls(in_channels=in_channels, out_channels=hidden_channels, kernel_size=self.kernel_sizes[i], dilation=self.dilations[i], stride=self.strides[i], padding=self.paddings[i], norm=norm, dropout=dropout, activation=activation, gated=gated, pooling=self.poolings[i], pool_size=self.pool_sizes[i], return_pool_data=return_pool_data)) in_channels = hidden_channels if self.out_channels is not None: convs.append( self.conv_cls(in_channels=in_channels, out_channels=out_channels, kernel_size=self.kernel_sizes[-1], dilation=self.dilations[-1], stride=self.strides[-1], padding=self.paddings[-1], norm=None, dropout=dropout, activation='identity', gated=False, pooling=self.poolings[-1], pool_size=self.pool_sizes[-1], return_pool_data=return_pool_data)) self.convs = nn.ModuleList(convs)
def to_pair(x): return tuple(to_list(x, 2))