def build_net(self, Iter):
     im, bboxes, nums = Iter.get_next()
     im.set_shape(
         tf.TensorShape(
             [None, self.config.img_size, self.config.img_size, 3]))
     im = im / 255 * 2 - 1
     batch_m = tf.shape(im)[0]
     with slim.arg_scope(
             mobilenet_v1.mobilenet_v1_arg_scope(
                 is_training=self.config.is_train,
                 batch_norm_decay=0.9997,
                 stddev=0.03)):
         logits, end_points = mobilenet_v1.mobilenet_v1(
             im, min_depth=16, is_training=self.config.is_train)
         var_pre = tf.global_variables()[1:]
         net11 = end_points['Conv2d_11_pointwise']
         net13 = end_points['Conv2d_13_pointwise']
         net14 = new_conv2d(net13, 512, 2, 'conv14')
         net15 = new_conv2d(net14, 256, 2, 'conv15')
         net16 = new_conv2d(net15, 256, 2, 'conv16')
         net17 = new_conv2d(net16, 128, 2, 'conv17')
     weights_init = tf.truncated_normal_initializer(stddev=0.03)
     net = [net11, net13, net14, net15, net16, net17]
     with slim.arg_scope([slim.conv2d],
                         activation_fn=None,
                         weights_initializer=weights_init,
                         weights_regularizer=slim.l2_regularizer(
                             self.config.weight_decay),
                         normalizer_fn=None):
         for i in range(len(self.config.num_anchors)):
             with tf.variable_scope('BoxPredictor_%d' % i):
                 with tf.variable_scope('ClassPredictor'):
                     net_cls = slim.conv2d(
                         net[i], self.config.num_anchors[i] *
                         (self.config.num_cls + 1), [1, 1])
                 with tf.variable_scope('BoxEncodingPredictor'):
                     net_box = slim.conv2d(
                         net[i],
                         self.config.num_anchors[i] * 4,
                         [1, 1],
                     )
                 net_cls = tf.reshape(
                     net_cls,
                     (batch_m, -1, self.config.num_cls + 1),
                 )
                 net_box = tf.reshape(
                     net_box,
                     (batch_m, -1, 4),
                 )
                 net[i] = tf.concat([net_cls, net_box], axis=-1)
     net = tf.concat(net, axis=1, name='net')
     loss, Num = self.get_loss(net, bboxes, nums)
     return tf.reduce_sum(loss) / tf.reduce_sum(Num + 1e-10), var_pre
Exemplo n.º 2
0
    def build_net(self, ):
        self.im_input = tf.placeholder(tf.string, name='input')
        im, b_scale, = self.handle_im(self.im_input, size=self.config.img_size)
        im.set_shape(tf.TensorShape([None, self.config.img_size, self.config.img_size, 3]))
        im = im / 255 * 2 - 1


        batch_m = tf.shape(im)[0]

        with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope(is_training=self.config.is_train, batch_norm_decay=0.9)):
            logits, end_points = mobilenet_v1.mobilenet_v1(im, min_depth=16, is_training=self.config.is_train)
            var_pre = tf.global_variables()[1:]
            net11 = end_points['Conv2d_11_pointwise']
            net13 = end_points['Conv2d_13_pointwise']
            net14 = new_conv2d(net13, 512, 2, 'conv14')
            net15 = new_conv2d(net14, 256, 2, 'conv15')
            net16 = new_conv2d(net15, 256, 2, 'conv16')
            net17 = new_conv2d(net16, 128, 2, 'conv17')
        weights_init = tf.truncated_normal_initializer(stddev=0.03)
        net = [net11, net13, net14, net15, net16, net17]
        with slim.arg_scope([slim.conv2d], activation_fn=None, weights_initializer=weights_init,
                            weights_regularizer=slim.l2_regularizer(self.config.weight_decay), normalizer_fn=None):
            for i in range(len(self.config.num_anchors)):
                with  tf.variable_scope('BoxPredictor_%d' % i):
                    with tf.variable_scope('ClassPredictor'):
                        net_cls = slim.conv2d(net[i], self.config.num_anchors[i] * (self.config.num_cls + 1), [1, 1])
                    with tf.variable_scope('BoxEncodingPredictor'):
                        net_box = slim.conv2d(net[i], self.config.num_anchors[i] * 4, [1, 1], )
                    net_cls = tf.reshape(net_cls, (batch_m, -1, self.config.num_cls + 1), )
                    net_box = tf.reshape(net_box, (batch_m, -1, 4), )
                    net[i] = tf.concat([net_cls, net_box], axis=-1)
        net = tf.concat(net, axis=1, name='net')

        print(net)
        pre_bboxes = loc2bbox(net[..., -4:] * tf.constant([0.1, 0.1, 0.2, 0.2], dtype=tf.float32), self.anchors)
        pre = predict(pre_bboxes[0], net[0][..., :-4], size=self.config.img_size, c_thresh=1e-2,num_cls=self.config.num_cls)
        self.result = tf.concat([pre[..., :4] * b_scale, pre[..., 4:]], axis=-1, name='out')
        print(self.result)
    def build_net(self, ):
        self.im_input = tf.placeholder(tf.string, name='input')
        im, b_scale, = self.handle_im(self.im_input, size=self.config.img_size)
        im.set_shape(
            tf.TensorShape(
                [None, self.config.img_size, self.config.img_size, 3]))
        im = im / 255 * 2 - 1

        batch_m = tf.shape(im)[0]

        with slim.arg_scope(
                mobilenet_v1.mobilenet_v1_arg_scope(
                    is_training=self.config.is_train, batch_norm_decay=0.9)):
            logits, end_points = mobilenet_v1.mobilenet_v1(
                im, min_depth=16, is_training=self.config.is_train)
            var_pre = tf.global_variables()[1:]
            net11 = end_points['Conv2d_11_pointwise']
            net13 = end_points['Conv2d_13_pointwise']
            net14 = new_conv2d(net13, 512, 2, 'conv14')
            net15 = new_conv2d(net14, 256, 2, 'conv15')
            net16 = new_conv2d(net15, 256, 2, 'conv16')
            net17 = new_conv2d(net16, 128, 2, 'conv17')

        with tf.variable_scope('detect_cls'):
            with slim.arg_scope([slim.conv2d],
                                activation_fn=None,
                                weights_regularizer=slim.l2_regularizer(
                                    self.config.weight_decay)):
                net11_cls = slim.conv2d(net11,
                                        self.config.num_anchors[0] *
                                        (self.config.num_cls + 1), [1, 1],
                                        scope='net11')
                net13_cls = slim.conv2d(net13,
                                        self.config.num_anchors[1] *
                                        (self.config.num_cls + 1), [1, 1],
                                        scope='net13')
                net14_cls = slim.conv2d(net14,
                                        self.config.num_anchors[2] *
                                        (self.config.num_cls + 1), [1, 1],
                                        scope='net14')
                net15_cls = slim.conv2d(net15,
                                        self.config.num_anchors[3] *
                                        (self.config.num_cls + 1), [1, 1],
                                        scope='net15')
                net16_cls = slim.conv2d(net16,
                                        self.config.num_anchors[4] *
                                        (self.config.num_cls + 1), [1, 1],
                                        scope='net16')
                net17_cls = slim.conv2d(net17,
                                        self.config.num_anchors[5] *
                                        (self.config.num_cls + 1), [1, 1],
                                        scope='net17')

        with tf.variable_scope('detect_box'):
            with slim.arg_scope([slim.conv2d],
                                activation_fn=None,
                                weights_regularizer=slim.l2_regularizer(
                                    self.config.weight_decay)):
                net11_box = slim.conv2d(net11,
                                        self.config.num_anchors[0] * 4, [1, 1],
                                        scope='net11')
                net13_box = slim.conv2d(net13,
                                        self.config.num_anchors[1] * 4, [1, 1],
                                        scope='net13')
                net14_box = slim.conv2d(net14,
                                        self.config.num_anchors[2] * 4, [1, 1],
                                        scope='net14')
                net15_box = slim.conv2d(net15,
                                        self.config.num_anchors[3] * 4, [1, 1],
                                        scope='net15')
                net16_box = slim.conv2d(net16,
                                        self.config.num_anchors[4] * 4, [1, 1],
                                        scope='net16')
                net17_box = slim.conv2d(net17,
                                        self.config.num_anchors[5] * 4, [1, 1],
                                        scope='net17')

        net11 = tf.concat([net11_cls, net11_box], axis=-1)
        net13 = tf.concat([net13_cls, net13_box], axis=-1)
        net14 = tf.concat([net14_cls, net14_box], axis=-1)
        net15 = tf.concat([net15_cls, net15_box], axis=-1)
        net16 = tf.concat([net16_cls, net16_box], axis=-1)
        net17 = tf.concat([net17_cls, net17_box], axis=-1)

        net11 = tf.reshape(
            net11,
            (batch_m, -1, self.config.num_cls + 1 + 4),
        )
        net13 = tf.reshape(
            net13,
            (batch_m, -1, self.config.num_cls + 1 + 4),
        )
        net14 = tf.reshape(
            net14,
            (batch_m, -1, self.config.num_cls + 1 + 4),
        )
        net15 = tf.reshape(
            net15,
            (batch_m, -1, self.config.num_cls + 1 + 4),
        )
        net16 = tf.reshape(
            net16,
            (batch_m, -1, self.config.num_cls + 1 + 4),
        )
        net17 = tf.reshape(
            net17,
            (batch_m, -1, self.config.num_cls + 1 + 4),
        )

        net = tf.concat((net11, net13, net14, net15, net16, net17),
                        axis=1,
                        name='net')

        print(net)
        pre_bboxes = loc2bbox(
            net[..., -4:] *
            tf.constant([0.1, 0.1, 0.2, 0.2], dtype=tf.float32), self.anchors)
        pre = predict(pre_bboxes[0],
                      net[0][..., :-4],
                      size=self.config.img_size,
                      c_thresh=1e-2,
                      num_cls=self.config.num_cls)
        self.result = tf.concat([pre[..., :4] * b_scale, pre[..., 4:]],
                                axis=-1,
                                name='out')
        print(self.result)
    def build_net(self, Iter):
        im, bboxes, nums = Iter.get_next()
        im.set_shape(
            tf.TensorShape(
                [None, self.config.img_size, self.config.img_size, 3]))
        im = im / 255 * 2 - 1
        batch_m = tf.shape(im)[0]
        with slim.arg_scope(
                mobilenet_v1.mobilenet_v1_arg_scope(
                    is_training=self.config.is_train,
                    batch_norm_decay=0.9997,
                    stddev=0.03)):
            logits, end_points = mobilenet_v1.mobilenet_v1(
                im, min_depth=16, is_training=self.config.is_train)
            var_pre = tf.global_variables()[1:]
            net11 = end_points['Conv2d_11_pointwise']
            net13 = end_points['Conv2d_13_pointwise']
            net14 = new_conv2d(net13, 512, 2, 'conv14')
            net15 = new_conv2d(net14, 256, 2, 'conv15')
            net16 = new_conv2d(net15, 256, 2, 'conv16')
            net17 = new_conv2d(net16, 128, 2, 'conv17')
        weights_init = tf.truncated_normal_initializer(stddev=0.03)
        with tf.variable_scope('detect_cls'):
            with slim.arg_scope([slim.conv2d],
                                activation_fn=None,
                                weights_initializer=weights_init,
                                weights_regularizer=slim.l2_regularizer(
                                    self.config.weight_decay)):
                net11_cls = slim.conv2d(net11,
                                        self.config.num_anchors[0] *
                                        (self.config.num_cls + 1), [1, 1],
                                        scope='net11')
                net13_cls = slim.conv2d(net13,
                                        self.config.num_anchors[1] *
                                        (self.config.num_cls + 1), [1, 1],
                                        scope='net13')
                net14_cls = slim.conv2d(net14,
                                        self.config.num_anchors[2] *
                                        (self.config.num_cls + 1), [1, 1],
                                        scope='net14')
                net15_cls = slim.conv2d(net15,
                                        self.config.num_anchors[3] *
                                        (self.config.num_cls + 1), [1, 1],
                                        scope='net15')
                net16_cls = slim.conv2d(net16,
                                        self.config.num_anchors[4] *
                                        (self.config.num_cls + 1), [1, 1],
                                        scope='net16')
                net17_cls = slim.conv2d(net17,
                                        self.config.num_anchors[5] *
                                        (self.config.num_cls + 1), [1, 1],
                                        scope='net17')

        with tf.variable_scope('detect_box'):
            with slim.arg_scope([slim.conv2d],
                                activation_fn=None,
                                weights_initializer=weights_init,
                                weights_regularizer=slim.l2_regularizer(
                                    self.config.weight_decay)):
                net11_box = slim.conv2d(net11,
                                        self.config.num_anchors[0] * 4, [1, 1],
                                        scope='net11')
                net13_box = slim.conv2d(net13,
                                        self.config.num_anchors[1] * 4, [1, 1],
                                        scope='net13')
                net14_box = slim.conv2d(net14,
                                        self.config.num_anchors[2] * 4, [1, 1],
                                        scope='net14')
                net15_box = slim.conv2d(net15,
                                        self.config.num_anchors[3] * 4, [1, 1],
                                        scope='net15')
                net16_box = slim.conv2d(net16,
                                        self.config.num_anchors[4] * 4, [1, 1],
                                        scope='net16')
                net17_box = slim.conv2d(net17,
                                        self.config.num_anchors[5] * 4, [1, 1],
                                        scope='net17')

        net11 = tf.concat([net11_cls, net11_box], axis=-1)
        net13 = tf.concat([net13_cls, net13_box], axis=-1)
        net14 = tf.concat([net14_cls, net14_box], axis=-1)
        net15 = tf.concat([net15_cls, net15_box], axis=-1)
        net16 = tf.concat([net16_cls, net16_box], axis=-1)
        net17 = tf.concat([net17_cls, net17_box], axis=-1)

        net11 = tf.reshape(
            net11,
            (batch_m, -1, self.config.num_cls + 1 + 4),
        )
        net13 = tf.reshape(
            net13,
            (batch_m, -1, self.config.num_cls + 1 + 4),
        )
        net14 = tf.reshape(
            net14,
            (batch_m, -1, self.config.num_cls + 1 + 4),
        )
        net15 = tf.reshape(
            net15,
            (batch_m, -1, self.config.num_cls + 1 + 4),
        )
        net16 = tf.reshape(
            net16,
            (batch_m, -1, self.config.num_cls + 1 + 4),
        )
        net17 = tf.reshape(
            net17,
            (batch_m, -1, self.config.num_cls + 1 + 4),
        )

        net = tf.concat((net11, net13, net14, net15, net16, net17),
                        axis=1,
                        name='net')
        loss, Num = self.get_loss(net, bboxes, nums)
        return tf.reduce_sum(loss) / tf.reduce_sum(Num + 1e-10), var_pre