def pixel_shuffle(input, upscale_factor): _tuple = ntuple(2) upscale_factor = _tuple(upscale_factor) batch_size, in_channel, in_height, in_width = input.size() out_channel = in_channel // (upscale_factor[0] * upscale_factor[1]) out_height = in_height * upscale_factor[0] out_width = in_width * upscale_factor[1] input_view = input.reshape(batch_size, out_channel, upscale_factor[0], upscale_factor[1], in_height, in_width) shuffle_out = input_view.permute(0, 1, 4, 2, 5, 3).reshape(batch_size, out_channel, out_height, out_width) return shuffle_out
def pixel_unshuffle(input, downscale_factor): _tuple = ntuple(2) downscale_factor = _tuple(downscale_factor) batch_size, in_channel, in_height, in_width = input.size() out_channel = in_channel * (downscale_factor[0] * downscale_factor[1]) out_height = in_height // downscale_factor[0] out_width = in_width // downscale_factor[1] input_view = input.reshape(batch_size, in_channel, out_height, downscale_factor[0], out_width, downscale_factor[1]) shuffle_out = input_view.permute(0, 1, 3, 5, 2, 4).reshape(batch_size, out_channel, out_height, out_width) return shuffle_out
def make_cell(self): cell_info = copy.deepcopy(self.cell_info) cell = nn.ModuleList([nn.ModuleDict({}) for _ in range(cell_info['num_layers'])]) _ntuple = ntuple(cell_info['num_layers']) cell_info['sharing_rates'] = _ntuple(cell_info['sharing_rates']) for i in range(cell_info['num_layers']): cell_in_info = {'cell': 'oConv2d', 'input_size': (cell_info['input_size'], cell_info['output_size']), 'output_size': ((cell_info['output_size'],) * 3,(cell_info['output_size'],) * 3), 'kernel_size': cell_info['kernel_size'], 'stride': cell_info['stride'], 'padding': cell_info['padding'], 'sharing_rates': cell_info['sharing_rates'][i], 'bias': cell_info['bias'], 'normalization': 'none', 'activation': 'none'} cell[i]['in'] = Cell(cell_in_info) cell[i]['activation'] = Cell({'cell': 'Activation', 'mode': cell_info['activation']}) cell[i]['dropout'] = Cell({'cell': 'Dropout', 'p': cell_info['dropout']}) cell_info['input_size'] = cell_info['output_size'] return cell
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, sharing_rates=0, bias=True): _nutple = ntuple(2) kernel_size = _nutple(kernel_size) stride = _nutple(stride) padding = _nutple(padding) dilation = _nutple(dilation) super(oConv2d, self).__init__(in_channels, out_channels, kernel_size, stride, padding, dilation, False, _nutple(0), sharing_rates, bias)
def append(self, result, tag, n=1, mean=True): for k in result: name = '{}/{}'.format(tag, k) self.tracker[name] = result[k] if mean: if isinstance(result[k], Number): self.counter[name] += n self.mean[name] = ((self.counter[name] - n) * self.mean[name] + n * result[k]) / self.counter[name] elif isinstance(result[k], Iterable): if name not in self.mean: self.counter[name] = [0 for _ in range(len(result[k]))] self.mean[name] = [0 for _ in range(len(result[k]))] _ntuple = ntuple(len(result[k])) n = _ntuple(n) for i in range(len(result[k])): self.counter[name][i] += n[i] self.mean[name][i] = ((self.counter[name][i] - n[i]) * self.mean[name][i] + n[i] * result[k][i]) / self.counter[name][i] else: raise ValueError('Not valid data type') return
parser.add_argument('-p', '--prefix', default='model', type=str) parser.add_argument('-V', '--vocab', default='abcd', type=str) parser.add_argument('-g', '--target', default='redrum', type=str) parser.add_argument('-T', '--test_length', action='store_true') parser.add_argument('-c', '--checkpoint', default=500, type=int) parser.add_argument('-P', '--plot', action='store_true') args = parser.parse_args() if args.sample_fn == 'identity': SAMPLE_FN = u.identity elif args.sample_fn == 'reverse': SAMPLE_FN = u.reverse elif args.sample_fn == 'double': SAMPLE_FN = u.double elif args.sample_fn == 'triple': SAMPLE_FN = lambda x: u.ntuple(x, n=3) elif args.sample_fn == 'quadruple': SAMPLE_FN = lambda x: u.ntuple(x, n=4) elif args.sample_fn == 'quintuple': SAMPLE_FN = lambda x: u.ntuple(x, n=5) elif args.sample_fn == 'reversedouble': SAMPLE_FN = u.reversedouble elif args.sample_fn == 'skipchar1': SAMPLE_FN = lambda x: u.skipchar(x, skip=1) elif args.sample_fn == 'skipchar2': SAMPLE_FN = lambda x: u.skipchar(x, skip=2) elif args.sample_fn == 'skipchar3': SAMPLE_FN = lambda x: u.skipchar(x, skip=3) else: raise ValueError('non exisiting fn [%s]' % args.sample_fn)