Ejemplo n.º 1
0
    def conv_param(self, num_output, kernel_size, stride=(1, 1), pad=(0, 0),
                   weight_filler_type='xavier', bias_filler_type='constant',
                   bias_term=True, dilation=None, groups=None):
        """
        add a conv_param layer if you spec the layer type "Convolution"
        Args:
            num_output: a int
            kernel_size: int list
            stride: a int list
            weight_filler_type: the weight filer type
            bias_filler_type: the bias filler type
        """
        if self.type not in ['Convolution','Deconvolution']:
            raise TypeError('the layer type must be Convolution or Deconvolution if you want set conv param')
        conv_param = pb.ConvolutionParameter()
        conv_param.num_output = num_output
        if kernel_size[0] == kernel_size[1]:
            conv_param.kernel_size.extend(pair_reduce(kernel_size))
        else:
            conv_param.kernel_h = kernel_size[0]
            conv_param.kernel_w = kernel_size[1]

        if stride[0] == stride[1]:
            conv_param.stride.extend(pair_reduce(stride))
        else:
            conv_param.stride_h = stride[0]
            conv_param.stride_w = stride[1]

        if pad[0] == pad[1]:
            conv_param.pad.extend(pair_reduce(pad))
        else:
            conv_param.pad_h = pad[0]
            conv_param.pad_w = pad[1]

        conv_param.bias_term = bias_term
        conv_param.weight_filler.type = weight_filler_type
        if bias_term:
            conv_param.bias_filler.type = bias_filler_type
        if dilation:
            conv_param.dilation.extend(pair_reduce(dilation))
        if groups:
            conv_param.group=groups
        self.param.convolution_param.CopyFrom(conv_param)
    def _optimize3d_lasttwo_l(self, name, imgs, responses, sample_indices, H):
        """ Jaderberg's method split a conv layer(d, c, k, k) into 2 layers:
            l1(V) :  (d__, c, k, 1)
            l2_(H): (d, d__, 1, k)
           Here we used [1] method to split `l2_(d, d__, 1, k)` into 2 layers:
            l2: (d_, d__, 1, k)
            l3: (d, d_, 1, 1)
        @Parameters:
            name: The name of the layer to be approximated
            imgs: Image data used to retrieve response
            responses:      The responses from the `old_net` for specified layers
                            {"blob_name": responses(d, num_images*H'*W'*sample_ratio)}
            sample_indices: The sample indices when extract response from the oldnet
                            {"blob_name": [[indics for batch1], [..bathc2], [.]]}
            H:              ndarray with shape (d, d__, 1, k)
        """
        # ===============================================================
        # Construct a tmp net which has a layer W_(d, d__, k, 1), The blobs
        # data of this net is construct from V in jaderberg_result and
        # b from original_layer

        tmp_b = self.o_net.params[name][1].data
        input_shape = self.n_net.blobs[self.layer_map[name][0]].data.shape
        l2 = self.n_net_layer_dict[self.layer_map[name][1]][1]
        pad = l2.convolution_param.pad
        conv_param = caffe_pb2.ConvolutionParameter()
        conv_param.kernel_size.extend(H.shape[2::])
        conv_param.pad.extend(pad)
        conv_param.num_output = H.shape[0]
        tmp_net, tmp_net_param = construct_one_layer_tmp_net(
            conv_param, input_shape, H, tmp_b)
        # Get the approximated response Y_ = WX_ + b, and X_ is the response
        # of the layer with name `self.laye_map[name][0]`
        Y_ = self.get_approximated_response(tmp_net, self.layer_map[name][0],
                                            imgs, sample_indices[name])
        d_ = l2.convolution_param.num_output
        Y = responses[name]
        M, P, Q_h, b = self.init_from_linear_case(Y, d_)
        M, P, Q_h, b = self.alternate_solve(M, b, Y, Y_, d_)
        self._handle_3d_decomp_2l(name, Q_h, H, tmp_b)
        self._handle_3d_decomp_3l(name, P, b)
Ejemplo n.º 3
0
 def conv_param(self,
                num_output,
                kernel_size,
                stride=(1),
                pad=(0, ),
                weight_filler_type='xavier',
                bias_filler_type='constant',
                bias_term=True,
                dilation=None,
                groups=None):
     """
     add a conv_param layer if you spec the layer type "Convolution"
     Args:
         num_output: a int
         kernel_size: int list
         stride: a int list
         weight_filler_type: the weight filer type
         bias_filler_type: the bias filler type
     Returns:
     """
     if self.type != 'Convolution' and self.type != 'Deconvolution':
         raise TypeError(
             'the layer type must be Convolution/Deconvolution if you want set conv param'
         )
     conv_param = pb.ConvolutionParameter()
     conv_param.num_output = num_output
     conv_param.kernel_size.extend(pair_reduce(kernel_size))
     conv_param.stride.extend(pair_reduce(stride))
     conv_param.pad.extend(pair_reduce(pad))
     conv_param.bias_term = bias_term
     conv_param.weight_filler.type = weight_filler_type
     if bias_term:
         conv_param.bias_filler.type = bias_filler_type
     if dilation:
         conv_param.dilation.extend(pair_reduce(dilation))
     if groups:
         conv_param.group = groups
     self.param.convolution_param.CopyFrom(conv_param)
Ejemplo n.º 4
0
def _convolution(opr, context):
    def expand(x):
        if isinstance(x, (list, tuple)):
            return x
        elif isinstance(x, int):
            return x, x
        else:
            raise TypeError(
                "get error type! got {} expect int or tuple[int,..]".format(
                    type(x)))

    ph, pw = expand(opr.padding)
    sh, sw = expand(opr.stride)
    param_W = opr.inp_tensors[1].np_data
    kh, kw = param_W.shape[-2:]
    group = opr.groups
    dilation_h, dilation_w = expand(opr.dilation)
    assert (
        dilation_h == dilation_w
    ), "caffe accept one dilation, so dilation_h and dilation_w must equal"
    param_W = param_W.reshape((-1, ) + param_W.shape[-3:])
    bias_term = len(opr.inp_tensors) > 2
    blobs = [
        context.gen_blob_proto(param_W),
    ]
    param = cp.ConvolutionParameter(
        num_output=opr.out_tensors[0].shape[1],
        pad_h=ph,
        pad_w=pw,
        stride_h=sh,
        stride_w=sw,
        kernel_h=kh,
        kernel_w=kw,
        dilation=[dilation_h],
        group=group,
        bias_term=bias_term,
    )
    top = [context.set_blob_name(opr.out_tensors[0], opr.out_tensors[0].name)]
    if isinstance(opr, Deconv2dOpr):
        layer_type = "Deconvolution"
        context.set_blob_name(opr.inp_tensors[1], opr.inp_tensors[0].name)
        bottom = [context.get_blob_name(opr.inp_tensors[1])]
    else:
        layer_type = "Convolution"
        bottom = [context.get_blob_name(opr.inp_tensors[0])]

    if len(opr.inp_tensors) > 2:
        blobs.append(
            context.gen_blob_proto(opr.inp_tensors[2].np_data.reshape(-1, )))
        assert bias_term == True
    else:
        assert bias_term == False

    context.add_layer(
        cp.LayerParameter(
            bottom=bottom,
            top=top,
            name=opr.out_tensors[0].name,
            type=layer_type,
            blobs=blobs,
            convolution_param=param,
        ))