def _block(self, inputs, filter, layer_name): with tf.name_scope(layer_name): inputs = common._conv2d_fixed_padding(inputs, filter[0], 1, 1, layer_name + '_conv1') inputs = common._conv2d_fixed_padding(inputs, filter[1], 3, 1, layer_name + '_conv2') inputs = common._conv2d_fixed_padding(inputs, 64, 3, 2, layer_name + '_conv3') return inputs
def _extract_block(self, inputs, layer_name): with tf.name_scope(layer_name): inputs = common._conv2d_fixed_padding(inputs=inputs, filters=32, kernel_size=3, strides=1, layer_name=layer_name + '_conv1') for i in range(2): name = layer_name + 'res_block1' + str(i) inputs = self._darknet53_block(inputs, 16, name) inputs = common._conv2d_fixed_padding(inputs=inputs, filters=128, kernel_size=3, strides=2, layer_name=layer_name + '_conv2') for i in range(4): name = layer_name + 'res_block2' + str(i) inputs = self._darknet53_block(inputs, 64, name) inputs = common._conv2d_fixed_padding(inputs=inputs, filters=512, kernel_size=3, strides=2, layer_name=layer_name + '_conv3') for i in range(4): name = layer_name + 'res_block3' + str(i) inputs = self._darknet53_block(inputs, 256, name) inputs = common._conv2d_fixed_padding(inputs=inputs, filters=128, kernel_size=3, strides=2, layer_name=layer_name + '_conv4') for i in range(2): name = layer_name + 'res_block4' + str(i) inputs = self._darknet53_block(inputs, 64, name) inputs = common._conv2d_fixed_padding(inputs=inputs, filters=4, kernel_size=3, strides=2, layer_name=layer_name + '_conv5') inputs = tf.reshape(inputs, (-1, 1, 1, 64)) inputs = tf.tile(inputs, multiples=[1, 16, 16, 1]) return inputs
def _darknet53_block(self, inputs, filters, name=None): """ implement residuals block in darknet53 """ shortcut = inputs inputs = common._conv2d_fixed_padding(inputs, filters * 1, 1, 1, name + '_conv1') inputs = common._conv2d_fixed_padding(inputs, filters * 2, 3, 1, name + '_conv2') inputs = inputs + shortcut return inputs
def transform_layer(self, inputs, scope): with tf.name_scope(scope): inputs = common._conv2d_fixed_padding(inputs, DEEPTH, 1, strides=1, layer_name="trans_1") inputs = common._conv2d_fixed_padding(inputs, DEEPTH, 3, strides=1, layer_name="trans_2") return inputs
def transition_layer(self, inputs, filters, scope): with tf.name_scope(scope): inputs = common._conv2d_fixed_padding(inputs, filters, 1, strides=1, layer_name="trans_3") return inputs
def _yolo_block(self, inputs: tf.Tensor, filters: int, layer_name: str): """ :param inputs: :param filters: :param layer_name: :return: """ inputs = common._conv2d_fixed_padding(inputs=inputs, filters=filters * 1, kernel_size=1, layer_name=layer_name + "_a") inputs = common._conv2d_fixed_padding(inputs=inputs, filters=filters * 2, kernel_size=3, layer_name=layer_name + "_b") inputs = common._conv2d_fixed_padding(inputs=inputs, filters=filters * 1, kernel_size=1, layer_name=layer_name + "_c") inputs = common._conv2d_fixed_padding(inputs=inputs, filters=filters * 2, kernel_size=3, layer_name=layer_name + "_d") inputs = common._conv2d_fixed_padding(inputs=inputs, filters=filters * 1, kernel_size=1, layer_name=layer_name + "_e") route = inputs inputs = common._conv2d_fixed_padding(inputs=inputs, filters=filters * 2, kernel_size=3, layer_name=layer_name + "_f") return route, inputs
def forward( self, room_zid: tf.Tensor, room_cid: tf.Tensor, room_mid: tf.Tensor, room_scid: tf.Tensor, room_distance: tf.Tensor, zone_zid: tf.Tensor, zone_cid: tf.Tensor, zone_mid: tf.Tensor, ): """ Creates YOLO v3 model. """ batch_norm_params = { 'decay': self._BATCH_NORM_DECAY, 'epsilon': 1e-05, 'scale': True, 'is_training': self.trainable, 'fused': None, } with slim.arg_scope( [slim.conv2d, slim.batch_norm, common._fixed_padding]): with slim.arg_scope( [slim.conv2d], normalizer_fn=slim.batch_norm, normalizer_params=batch_norm_params, weights_regularizer=slim.l2_regularizer(0.0005), biases_initializer=None, activation_fn=lambda x: tf.nn.leaky_relu( x, alpha=self._LEAKY_RELU)): with tf.variable_scope('darknet-53'): route_1, route_2, inputs = darknet53( room_zid=room_zid, room_cid=room_cid, room_mid=room_mid, room_scid=room_scid, zone_zid=zone_zid, zone_cid=zone_cid, zone_mid=zone_mid, room_distance=room_distance, furniture_nums=self.furniture_nums, furniture_embedding_size=self.furniture_embedding_size ).outputs print("yolo:{0}".format(inputs)) with tf.variable_scope('yolo-v3'): route, inputs = self._yolo_block(inputs=inputs, filters=512, layer_name="route1") inputs = common._conv2d_fixed_padding( inputs, 512, 1, layer_name="route1_conv") upsample_size = route_2.get_shape().as_list() inputs = self._upsample(inputs, upsample_size) inputs = tf.concat([inputs, route_2], axis=3) route, inputs = self._yolo_block(inputs=inputs, filters=256, layer_name="route2") inputs = common._conv2d_fixed_padding( inputs, 64, 1, layer_name="route2_conv") upsample_size = route_1.get_shape().as_list() inputs = self._upsample(inputs, upsample_size) inputs = tf.concat([inputs, route_1], axis=3, name='concat_2') route, inputs = self._yolo_block(inputs=inputs, filters=4, layer_name="route3") feature_map_3 = tf.identity(route, name='feature_map_3') return feature_map_3
def forward(self, room_zid: tf.Tensor, room_cid: tf.Tensor, room_mid: tf.Tensor, room_scid: tf.Tensor, zone_zid: tf.Tensor, zone_cid: tf.Tensor, zone_mid: tf.Tensor, room_distance: tf.Tensor = None): room_embedding_list = EmbeddingLayers( input_tensor_list=[ room_zid, room_cid, room_mid, room_scid, room_distance ], vocab_size_list=[200, self.furniture_nums, 200, 200, 200], num_unit_list=[self.furniture_embedding_size] * 6, scopes=[ "zid_table", "cid_table", "mid_table", "scid_table", "distance_table" ], trainable=True, dtype=tf.float32) room_embedding = tf.concat(values=room_embedding_list, axis=-1) zone_embedding_list = EmbeddingLayers( input_tensor_list=[zone_zid, zone_cid, zone_mid], vocab_size_list=[200, self.furniture_nums, 200], num_unit_list=[self.furniture_embedding_size] * 3, scopes=["zid_table", "cid_table", "scid_table"], trainable=True, dtype=tf.float32) zone_embedding = tf.concat(values=zone_embedding_list, axis=-1) zone_feature = self._extract_block(zone_embedding, 'zone_extract') room_feature = self._block(room_embedding, [16, 32], 'room') for i in range(2): name = 'res_block1' + str(i) room_feature = self._darknet53_block(room_feature, 32, name) room_feature = common._conv2d_fixed_padding(inputs=room_feature, filters=64, kernel_size=3, strides=2, layer_name="forward_a") inputs = tf.concat([zone_feature, room_feature], axis=3, name='concat_zone_room') for i in range(8): name = 'res_block2' + str(i) inputs = self._darknet53_block(inputs, 64, name) route_1 = inputs inputs = common._conv2d_fixed_padding(inputs=inputs, filters=512, kernel_size=3, strides=2, layer_name="forward_b") for i in range(8): name = 'res_block3' + str(i) inputs = self._darknet53_block(inputs, 256, name) route_2 = inputs inputs = common._conv2d_fixed_padding(inputs=inputs, filters=1024, kernel_size=3, strides=2, layer_name="forward_c") for i in range(4): name = 'res_block4' + str(i) inputs = self._darknet53_block(inputs, 512, name) return route_1, route_2, inputs