def se_resnet_layer(self, x, in_channel, out_channel, stride, dim_match, block_name): conv_name_base = 'res_' + block_name + '_branch' bn_name_base = 'bn_' + block_name + '_branch' prelu_name_base = 'prelu_' + block_name + '_branch' se_name_base = 'se_' + block_name + '_branch' short_cut = x if not dim_match: short_cut = common_layers.conv2d(short_cut, conv_name_base + '1', (1, 1), in_channel, out_channel, stride) short_cut = tf.layers.batch_normalization( short_cut, axis=-1, momentum=0.9, training=self.train, name=bn_name_base + '1') x = tf.layers.batch_normalization( x, axis=-1, momentum=0.9, training=self.train, name=bn_name_base + '2a') x = common_layers.conv2d(x, conv_name_base + '2a', (3, 3), in_channel, out_channel, [1, 1]) x = tf.layers.batch_normalization( x, axis=-1, momentum=0.9, training=self.train, name=bn_name_base + '2b') x = self.prelu_layer(x, name=prelu_name_base + '2b') x = common_layers.conv2d(x, conv_name_base + '2b', (3, 3), out_channel, out_channel, stride) x = tf.layers.batch_normalization( x, axis=-1, momentum=0.9, training=self.train, name=bn_name_base + '2c') res = self.se_moudle(x, out_channel, 16, name=se_name_base) return tf.add(short_cut, res, name='add_' + block_name)
def resnet_layer(self, x, in_channel, out_channel, stride, dim_match, block_name): conv_name_base = 'res' + block_name + '_branch' bn_name_base = 'bn' + block_name + '_branch' prelu_name_base = 'prelu' + block_name + '_branch' short_cut = x if not dim_match: short_cut = common_layers.conv2d(short_cut, conv_name_base + '1', filter_size=(1, 1), in_channels=in_channel, out_channels=out_channel, strides=stride, bias=False) short_cut = tf.layers.batch_normalization(short_cut, axis=-1, momentum=0.9, training=self.train, name=bn_name_base + '1') x = tf.layers.batch_normalization(x, axis=-1, momentum=0.9, training=self.train, name=bn_name_base + '2a') x = common_layers.conv2d(x, conv_name_base + '2a', (3, 3), in_channel, out_channel, [1, 1], bias=False) x = tf.layers.batch_normalization(x, axis=-1, momentum=0.9, training=self.train, name=bn_name_base + '2b') x = self.prelu_layer(x, name=prelu_name_base + '2b') x = common_layers.conv2d(x, conv_name_base + '2b', (3, 3), out_channel, out_channel, stride, bias=False) res = tf.layers.batch_normalization(x, axis=-1, momentum=0.9, training=self.train, name=bn_name_base + '2c') return tf.add(short_cut, res, name='add_' + block_name)
def resnet(self, inputs): ''' resnet_block. ''' layers_list = self.netconf['layers_list'] logging.info("layers_list : {}".format(layers_list)) filters_list = self.netconf['filters_list'] logging.info("filters_list : {}".format(filters_list)) strides_list = self.netconf['strides_list'] logging.info("strides_list : {}".format(strides_list)) block_mode = self.netconf['block_mode'] logging.info("block_mode : {}".format(block_mode)) with tf.variable_scope('resnet'): x = tf.identity(inputs) with tf.variable_scope('input_layer'): x = common_layers.conv2d(x, 'input_conv', (3, 3), self.input_channels, filters_list[0], [1, 1], bias=False) x = tf.layers.batch_normalization(x, axis=-1, momentum=0.9, training=self.train, name='input_bn') x = self.prelu_layer(x, 'input_prelu') for index, layer_num in enumerate(layers_list): unit_name = 'resblock-' + str(index + 1) with tf.variable_scope(unit_name): x = self.resnet_block(x, block_mode, layer_num, filters_list[index], filters_list[index + 1], strides_list[index]) return x
def conv_block(self, inputs, depthwise=False): ''' 2D conv layers. ''' filters = self.netconf['filters'] logging.info("filters : {}".format(filters)) filters_size = self.netconf['filter_size'] logging.info("filters_size : {}".format(filters_size)) filters_strides = self.netconf['filter_stride'] logging.info("filters_strides : {}".format(filters_strides)) pools_size = self.netconf['pool_size'] logging.info("pools_size : {}".format(pools_size)) layer_num = len(filters) assert layer_num == len(filters_size) assert layer_num == len(filters_strides) assert layer_num == len(pools_size) channels = [self.input_channels] + filters logging.info("channels : {}".format(channels)) downsample_input_len = self.input_len with tf.variable_scope('cnn'): x = tf.identity(inputs) for index, filt in enumerate(filters): unit_name = 'unit-' + str(index + 1) with tf.variable_scope(unit_name): if depthwise: x = tf.layers.separable_conv2d( x, filters=filt, kernel_size=filters_size[index], strides=filters_strides[index], padding='same', name=unit_name) else: cnn_name = 'cnn-' + str(index + 1) x = common_layers.conv2d(x, cnn_name, filters_size[index], channels[index], channels[index + 1], filters_strides[index]) x = tf.nn.relu(x) if self.netconf['use_bn']: bn_name = 'bn' + str(index + 1) x = tf.layers.batch_normalization(x, axis=-1, momentum=0.9, training=self.train, name=bn_name) if self.netconf['use_dropout']: x = tf.layers.dropout(x, self.netconf['dropout_rate'], training=self.train) x = common_layers.max_pool(x, pools_size[index], pools_size[index]) downsample_input_len = downsample_input_len / pools_size[ index][0] return x, downsample_input_len