def infer_shape_conv_pool_shape(self, op): input_shape = self._output_shape_cache[op.input[0]] output_shape = np.zeros_like(input_shape) if op.type == MaceOp.Pooling: filter_shape = list( ConverterUtil.get_arg(op, MaceKeyword.mace_kernel_str).ints) if ConverterUtil.data_format(op) == DataFormat.NCHW: filter_shape = [input_shape[1], input_shape[1]] + filter_shape if ConverterUtil.get_arg(op, MaceKeyword.mace_global_pooling_str) \ is not None: filter_shape[2] = input_shape[2] filter_shape[3] = input_shape[3] else: # NHWC filter_shape = filter_shape + [input_shape[1], input_shape[1]] if ConverterUtil.get_arg(op, MaceKeyword.mace_global_pooling_str) \ is not None: filter_shape[0] = input_shape[1] filter_shape[1] = input_shape[2] else: filter_shape = self._output_shape_cache[op.input[1]] paddings = ConverterUtil.get_arg( op, MaceKeyword.mace_padding_values_str).ints # noqa strides = ConverterUtil.get_arg(op, MaceKeyword.mace_strides_str).ints dilations_arg = ConverterUtil.get_arg(op, MaceKeyword.mace_dilations_str) if dilations_arg is not None: dilations = dilations_arg.ints else: dilations = [1, 1] if op.type == MaceOp.Pooling: round_func = math.ceil else: round_func = math.floor output_shape[0] = input_shape[0] if ConverterUtil.data_format(op) == DataFormat.NCHW \ and ConverterUtil.filter_format(self._net) == DataFormat.OIHW: # noqa # filter format: OIHW if op.type == MaceOp.DepthwiseConv2d.name: output_shape[1] = filter_shape[0] * filter_shape[1] else: output_shape[1] = filter_shape[0] output_shape[2] = int( round_func((input_shape[2] + paddings[0] - filter_shape[2] - (filter_shape[2] - 1) * (dilations[0] - 1)) / float(strides[0]))) + 1 output_shape[3] = int( round_func((input_shape[3] + paddings[1] - filter_shape[3] - (filter_shape[3] - 1) * (dilations[1] - 1)) / float(strides[1]))) + 1 else: mace_check( False, "Mace can only infer shape for" " NCHW input and OIHW filter") self.add_output_shape(op, [output_shape])
def infer_shape_deconv(self, op): input_shape = self._output_shape_cache[op.input[0]] output_shape = np.zeros_like(input_shape) filter_shape = self._output_shape_cache[op.input[1]] paddings = ConverterUtil.get_arg( op, MaceKeyword.mace_padding_values_str).ints # noqa strides = ConverterUtil.get_arg(op, MaceKeyword.mace_strides_str).ints dilations_arg = ConverterUtil.get_arg(op, MaceKeyword.mace_dilations_str) if dilations_arg is not None: dilations = dilations_arg.ints else: dilations = [1, 1] round_func = math.floor group_arg = ConverterUtil.get_arg(op, MaceKeyword.mace_group_str) output_shape[0] = input_shape[0] if ConverterUtil.data_format(op) == DataFormat.NCHW \ and ConverterUtil.filter_format(self._net) == DataFormat.OIHW: # noqa # filter format: IOHW output_shape[1] = filter_shape[1] if group_arg is not None and group_arg.i > 1: output_shape[1] = group_arg.i * filter_shape[1] output_shape[2] = int( round_func((input_shape[2] - 1) * strides[0] + (filter_shape[2] - 1) * (dilations[0] - 1) + filter_shape[2] - paddings[0])) output_shape[3] = int( round_func((input_shape[3] - 1) * strides[1] + (filter_shape[3] - 1) * (dilations[1] - 1) + filter_shape[3] - paddings[1])) else: mace_check( False, "Mace can only infer shape for" " NCHW input and OIHW filter") print("deconv layer %s (%s) input:%s filter:%s output:%s" % (op.name, op.type, input_shape, filter_shape, output_shape)) self.add_output_shape(op, [output_shape])
def infer_shape_conv2d_pool(self, op): input_shape = self._output_shape_cache[op.input[0]] output_shape = np.zeros_like(input_shape) if not op.type == MaceOp.Pooling: filter_shape = self._output_shape_cache[op.input[1]] paddings = ConverterUtil.get_arg( op, MaceKeyword.mace_padding_values_str).ints # noqa strides = ConverterUtil.get_arg(op, MaceKeyword.mace_strides_str).ints dilations_arg = ConverterUtil.get_arg(op, MaceKeyword.mace_dilations_str) if dilations_arg is not None: dilations = dilations_arg.ints else: dilations = [1, 1] # MACE pooling has no dilation if op.type == MaceOp.Pooling.name: kernels = ConverterUtil.get_arg(op, MaceKeyword.mace_kernel_str).ints if ConverterUtil.get_arg( op, MaceKeyword.mace_global_pooling_str) is not None: kernels[0] = input_shape[2] kernels[1] = input_shape[3] round_func = math.floor round_mode_arg = ConverterUtil.get_arg(op, MaceKeyword.mace_round_mode_str) if round_mode_arg is not None and \ round_mode_arg.i == RoundMode.CEIL.value: round_func = math.ceil # N_o = N_i output_shape[0] = input_shape[0] mace_check( ConverterUtil.data_format(op) == DataFormat.NCHW and ConverterUtil.filter_format(self._mace_net_def) == DataFormat.OIHW, "MACE can only infer shape for NCHW input and OIHW filter") # noqa # get C_{out} if op.type == MaceOp.DepthwiseConv2d.name: # 1CHW for depthwise, here C=C_{out} output_shape[1] = filter_shape[1] elif op.type == MaceOp.Conv2D.name: # filter format: OIHW output_shape[1] = filter_shape[0] else: output_shape[1] = input_shape[1] # H_{out} p, d, s = paddings[0], dilations[0], strides[0] k = kernels[0] if op.type == MaceOp.Pooling.name \ else filter_shape[2] # https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html output_shape[2] = int( round_func( float(input_shape[2] + p - d * (k - 1) - 1) / float(s) + 1)) # W_{out} p, d, s = paddings[1], dilations[1], strides[1] k = kernels[1] if op.type == MaceOp.Pooling.name \ else filter_shape[3] output_shape[3] = int( round_func( float(input_shape[3] + p - d * (k - 1) - 1) / float(s) + 1)) self.add_output_shape(op, [output_shape])