Exemple #1
0
    def middle_flow(self, data):
        block_num = self.bottleneck_params["middle_flow"][0]
        strides = self.bottleneck_params["middle_flow"][1]
        chns = self.bottleneck_params["middle_flow"][2]
        strides = check_data(strides, block_num)
        chns = check_data(chns, block_num)

        # params to control your flow
        s = self.stride
        block_point = self.block_point
        output_stride = self.output_stride
        with scope("middle_flow"):
            for i in range(block_num):
                block_point = block_point + 1
                with scope("block" + str(i + 1)):
                    stride = strides[i] if check_stride(
                        s * strides[i], output_stride) else 1
                    data, short_cuts = self.xception_block(data,
                                                           chns[i],
                                                           [1, 1, strides[i]],
                                                           skip_conv=False)
                    s = s * stride
                    if check_points(block_point, self.decode_points):
                        self.short_cuts[block_point] = short_cuts[1]

        self.stride = s
        self.block_point = block_point
        return data
Exemple #2
0
 def xception_block(self,
                    input,
                    channels,
                    strides=1,
                    filters=3,
                    dilation=1,
                    skip_conv=True,
                    has_skip=True,
                    activation_fn_in_separable_conv=False):
     repeat_number = 3
     channels = check_data(channels, repeat_number)
     filters = check_data(filters, repeat_number)
     strides = check_data(strides, repeat_number)
     data = input
     results = []
     for i in range(repeat_number):
         with scope('separable_conv' + str(i + 1)):
             if not activation_fn_in_separable_conv:
                 data = relu(data)
                 data = separate_conv(data,
                                      channels[i],
                                      strides[i],
                                      filters[i],
                                      dilation=dilation,
                                      eps=1e-3)
             else:
                 data = separate_conv(data,
                                      channels[i],
                                      strides[i],
                                      filters[i],
                                      dilation=dilation,
                                      act=relu,
                                      eps=1e-3)
             results.append(data)
     if not has_skip:
         return data, results
     if skip_conv:
         param_attr = fluid.ParamAttr(
             name=name_scope + 'weights',
             regularizer=None,
             initializer=fluid.initializer.TruncatedNormal(loc=0.0,
                                                           scale=0.09))
         with scope('shortcut'):
             skip = bn(conv(input,
                            channels[-1],
                            1,
                            strides[-1],
                            groups=1,
                            padding=0,
                            param_attr=param_attr),
                       eps=1e-3)
     else:
         skip = input
     return data + skip, results
Exemple #3
0
    def entry_flow(self, data):
        param_attr = fluid.ParamAttr(
            name=name_scope + 'weights',
            regularizer=None,
            initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=0.09))
        with scope("entry_flow"):
            with scope("conv1"):
                data = bn_relu(conv(data,
                                    32,
                                    3,
                                    stride=2,
                                    padding=1,
                                    param_attr=param_attr),
                               eps=1e-3)
            with scope("conv2"):
                data = bn_relu(conv(data,
                                    64,
                                    3,
                                    stride=1,
                                    padding=1,
                                    param_attr=param_attr),
                               eps=1e-3)

        # get entry flow params
        block_num = self.bottleneck_params["entry_flow"][0]
        strides = self.bottleneck_params["entry_flow"][1]
        chns = self.bottleneck_params["entry_flow"][2]
        strides = check_data(strides, block_num)
        chns = check_data(chns, block_num)

        # params to control your flow
        s = self.stride
        block_point = self.block_point
        output_stride = self.output_stride
        with scope("entry_flow"):
            for i in range(block_num):
                block_point = block_point + 1
                with scope("block" + str(i + 1)):
                    stride = strides[i] if check_stride(
                        s * strides[i], output_stride) else 1
                    data, short_cuts = self.xception_block(
                        data, chns[i], [1, 1, stride])
                    s = s * stride
                    if check_points(block_point, self.decode_points):
                        self.short_cuts[block_point] = short_cuts[1]

        self.stride = s
        self.block_point = block_point
        return data
Exemple #4
0
    def __call__(
        self,
        input,
    ):
        self.stride = 2
        self.block_point = 0
        self.short_cuts = dict()
        with scope(self.backbone):
            # Entry flow
            data = self.entry_flow(input)
            if check_points(self.block_point, self.end_points):
                return data, self.short_cuts

            # Middle flow
            data = self.middle_flow(data)
            if check_points(self.block_point, self.end_points):
                return data, self.short_cuts

            # Exit flow
            data = self.exit_flow(data)
            if check_points(self.block_point, self.end_points):
                return data, self.short_cuts

        if self.num_classes is not None:
            data = fluid.layers.reduce_mean(data, [2, 3], keep_dim=True)
            data = fluid.layers.dropout(data, 0.5)
            stdv = 1.0 / math.sqrt(data.shape[1] * 1.0)
            with scope("logit"):
                out = fluid.layers.fc(
                    input=data,
                    size=self.num_classes,
                    act='softmax',
                    param_attr=fluid.param_attr.ParamAttr(
                        name='weights',
                        initializer=fluid.initializer.Uniform(-stdv, stdv)),
                    bias_attr=fluid.param_attr.ParamAttr(name='bias'))

            return out
        else:
            return data
Exemple #5
0
    def exit_flow(self, data):
        block_num = self.bottleneck_params["exit_flow"][0]
        strides = self.bottleneck_params["exit_flow"][1]
        chns = self.bottleneck_params["exit_flow"][2]
        strides = check_data(strides, block_num)
        chns = check_data(chns, block_num)

        assert (block_num == 2)
        # params to control your flow
        s = self.stride
        block_point = self.block_point
        output_stride = self.output_stride
        with scope("exit_flow"):
            with scope('block1'):
                block_point += 1
                stride = strides[0] if check_stride(s * strides[0],
                                                    output_stride) else 1
                data, short_cuts = self.xception_block(data, chns[0],
                                                       [1, 1, stride])
                s = s * stride
                if check_points(block_point, self.decode_points):
                    self.short_cuts[block_point] = short_cuts[1]
            with scope('block2'):
                block_point += 1
                stride = strides[1] if check_stride(s * strides[1],
                                                    output_stride) else 1
                data, short_cuts = self.xception_block(
                    data,
                    chns[1], [1, 1, stride],
                    dilation=2,
                    has_skip=False,
                    activation_fn_in_separable_conv=True)
                s = s * stride
                if check_points(block_point, self.decode_points):
                    self.short_cuts[block_point] = short_cuts[1]

        self.stride = s
        self.block_point = block_point
        return data