def _block(self, filters): x1 = DarkConv( filters=filters // 2, kernel_size=(1,1), strides=(1,1), padding="same", use_bn= True, kernel_initializer=self._kernel_initializer, bias_initializer=self._bias_initializer, norm_moment=self._norm_moment, norm_epsilon=self._norm_epsilon, activation=self._activation, leaky_alpha=self._leaky_alpha) x2 = DarkConv( filters=filters, kernel_size=(3,3), strides=(1,1), padding="same", use_bn= True, kernel_initializer=self._kernel_initializer, bias_initializer=self._bias_initializer, norm_moment=self._norm_moment, norm_epsilon=self._norm_epsilon, activation=self._activation, leaky_alpha=self._leaky_alpha) return [x1, x2]
def block(inputs): x_route = DarkConv(filters=filters // 2, kernel_size=(1, 1), strides=(1, 1), padding="same", activation="leaky")(inputs) x = DarkConv(filters=filters // 4, kernel_size=(1, 1), strides=(1, 1), padding="same", activation="leaky")(x_route) x = ks.layers.UpSampling2D(size=2)(x) return x_route, x
def build(self, input_shape): if self._downsample: self._dconv = DarkConv(filters=self._filters, kernel_size=(3, 3), strides=(2, 2), padding='same', use_bias=self._use_bias, kernel_initializer=self._kernel_initializer, bias_initializer=self._bias_initializer, use_bn=self._use_bn, use_sync_bn=self._use_sync_bn, norm_moment=self._norm_moment, norm_epsilon=self._norm_epsilon, activation=self._conv_activation, leaky_alpha=self._leaky_alpha) else: self._dconv = Identity() self._conv1 = DarkConv(filters=self._filters // 2, kernel_size=(1, 1), strides=(1, 1), padding='same', use_bias=self._use_bias, kernel_initializer=self._kernel_initializer, bias_initializer=self._bias_initializer, use_bn=self._use_bn, use_sync_bn=self._use_sync_bn, norm_moment=self._norm_moment, norm_epsilon=self._norm_epsilon, activation=self._conv_activation, leaky_alpha=self._leaky_alpha) self._conv2 = DarkConv(filters=self._filters, kernel_size=(3, 3), strides=(1, 1), padding='same', use_bias=self._use_bias, kernel_initializer=self._kernel_initializer, bias_initializer=self._bias_initializer, use_bn=self._use_bn, use_sync_bn=self._use_sync_bn, norm_moment=self._norm_moment, norm_epsilon=self._norm_epsilon, activation=self._conv_activation, leaky_alpha=self._leaky_alpha) self._shortcut = ks.layers.Add() self._activation_fn = ks.layers.Activation( activation=self._sc_activation) super().build(input_shape) return
def block(inputs): x = DarkConv(filters=filters // 2, kernel_size=(1, 1), strides=(1, 1), padding="same", activation="leaky")(inputs) return x, None
def _get_layers(self): routes = dict() upsamples = dict() prediction_heads = dict() for i, filters in enumerate(self._filters): if i == 0 and self._model_name == "spp": routes[filters] = DarkRouteProcess( filters=filters, repetitions=self._repetitions + 1, insert_spp=True) else: routes[filters] = DarkRouteProcess( filters=filters // self._mod, repetitions=self._repetitions, insert_spp=False) if i != len(self._filters) - 1: upsamples[filters] = DarkUpsampleRoute(filters=filters // (4 * self._mod)) self.mod = 1 prediction_heads[filters] = DarkConv(filters=self.pred_depth, kernel_size=(1, 1), strides=(1, 1), padding="same", use_bn=False, activation=None) return routes, upsamples, prediction_heads
def block(inputs): x = DarkConv(filters=filters // 2, kernel_size=(1, 1), strides=(1, 1), padding="same", activation="leaky", l2_regularization=self._weight_decay)(inputs) return x, None
def call(inputs): if downsample: sample = DarkConv(filters=filters, kernel_size=(3, 3), strides=(2, 2), padding='same')(inputs) else: sample = inputs x = DarkConv(filters=filters // 2, kernel_size=(1, 1), strides=(1, 1), padding='same')(sample) x = DarkConv(filters=filters, kernel_size=(3, 3), strides=(1, 1), padding='same')(x) x = ks.layers.Add()([x, sample]) x = ks.layers.Activation(activation='relu')(x) return x
def to_tf(self, tensors): from yolo.modeling.building_blocks import DarkConv layer = DarkConv( filters=self.filters, kernel_size=(self.size, self.size), strides=(self.stride, self.stride), padding='same', # TODO: THIS ONE dilation_rate=(self.dilation, self.dilation), use_bn=bool(self.batch_normalize), activation=activation_function_dn_to_keras_name(self.activation), ) # TODO: Where does groups go return layer(tensors[-1]), layer
def _spp(self, filters): x1 = DarkConv( filters=filters // 2, kernel_size=(1,1), strides=(1,1), padding="same", use_bn= True, kernel_initializer=self._kernel_initializer, bias_initializer=self._bias_initializer, norm_moment=self._norm_moment, norm_epsilon=self._norm_epsilon, activation=self._activation, leaky_alpha=self._leaky_alpha) # repalce with spp x2 = DarkSpp([5, 9, 13]) return [x1, x2]
def _get_attributes(self, input_shape): """ use config dictionary to generate all important attributes for head construction """ inputs = collections.OrderedDict() input_shapes = collections.OrderedDict() routes = collections.OrderedDict() resamples = collections.OrderedDict() prediction_heads = collections.OrderedDict() start_width = input_shape[1] if input_shape[1] != None: start_width = start_width // 8 start_height = input_shape[2] if input_shape[2] != None: start_height = start_height // 8 for i, (key, path_keys) in enumerate(self._cfg_dict.items()): inputs[key] = ks.layers.Input( shape=[start_width, start_height, path_keys["depth"]]) input_shapes[key] = tf.TensorSpec( [None, start_width, start_height, path_keys["depth"]]) if type(path_keys["resample"]) != type(None): args = path_keys["resample_conditions"] layer = ks.utils.get_registered_object(path_keys["resample"]) resamples[key] = layer(**args) args = path_keys["processor_conditions"].copy() args['l2_regularization'] = self._weight_decay layer = ks.utils.get_registered_object(path_keys["processor"]) # print(path_keys["processor"], ks.utils.get_registered_object(path_keys["processor"])) routes[key] = layer(**args) args = path_keys["output_conditions"].copy() args['l2_regularization'] = self._weight_decay prediction_heads[key] = DarkConv(filters=self._conv_depth + path_keys["output-extras"], **args) if start_width != None: start_width //= 2 if start_height != None: start_height //= 2 # print(routes) # print(resamples) return inputs, input_shapes, routes, resamples, prediction_heads
def test_gradient_pass_though(self, filters): loss = ks.losses.MeanSquaredError() optimizer = ks.optimizers.SGD() with tf.device("/CPU:0"): test_layer = DarkConv(filters, kernel_size=(3, 3), padding="same") init = tf.random_normal_initializer() x = tf.Variable( initial_value=init(shape=(1, 224, 224, 3), dtype=tf.float32)) y = tf.Variable( initial_value=init(shape=(1, 224, 224, filters), dtype=tf.float32)) with tf.GradientTape() as tape: x_hat = test_layer(x) grad_loss = loss(x_hat, y) grad = tape.gradient(grad_loss, test_layer.trainable_variables) optimizer.apply_gradients(zip(grad, test_layer.trainable_variables)) self.assertNotIn(None, grad) return
def test_pass_through(self, kernel_size, padding, strides): if padding == "same": pad_const = 1 else: pad_const = 0 x = ks.Input(shape=(224, 224, 3)) test_layer = DarkConv(filters=64, kernel_size=kernel_size, padding=padding, strides=strides, trainable=False) outx = test_layer(x) print(outx.shape.as_list()) test = [ None, int((224 - kernel_size[0] + (2 * pad_const)) / strides[0] + 1), int((224 - kernel_size[1] + (2 * pad_const)) / strides[1] + 1), 64 ] print(test) self.assertAllEqual(outx.shape.as_list(), test) return
def build(self, input_shape): self.routes = dict() self.upsamples = dict() self.prediction_heads = dict() self.filters = list(reversed(list(input_shape.keys()))) if self._model_type == "tiny": filter_mod = 2 else: filter_mod = 1 for i, key in enumerate(self.filters): if i == 0 and self._model_type == "spp": self.routes[key] = DarkRouteProcess( filters=key, repetitions=self._repetitions + 1, insert_spp=True) else: self.routes[key] = DarkRouteProcess( filters=key // filter_mod, repetitions=self._repetitions, insert_spp=False) if i != len(self.filters) - 1: self.upsamples[key] = DarkUpsampleRoute(filters=key // (4 * filter_mod)) filter_mod = 1 self.prediction_heads[key] = DarkConv(filters=255, kernel_size=(1, 1), strides=(1, 1), padding="same", activation=None) # print(self.routes) # print(self.upsamples) # print(self.prediction_heads) return