def cnn7(unscaled_images, **conv_kwargs): """ Network 96x96: model/SeparableConv2d/depthwise_weights:0 (8, 8, 4, 1) model/SeparableConv2d/pointwise_weights:0 (1, 1, 4, 32) model/SeparableConv2d/biases:0 (32,) model/SeparableConv2d_1/depthwise_weights:0 (4, 4, 32, 1) model/SeparableConv2d_1/pointwise_weights:0 (1, 1, 32, 64) model/SeparableConv2d_1/biases:0 (64,) model/SeparableConv2d_2/depthwise_weights:0 (3, 3, 64, 1) model/SeparableConv2d_2/pointwise_weights:0 (1, 1, 64, 48) model/SeparableConv2d_2/biases:0 (48,) model/fc1/w:0 (6912, 512) model/fc1/b:0 (512,) model/v/w:0 (512, 1) model/v/b:0 (1,) model/pi/w:0 (512, 7) model/pi/b:0 (7,) Trainable variables: 3550296 """ with slim.arg_scope([slim.conv2d, slim.separable_conv2d], activation_fn=tf.nn.relu, weights_initializer=tf.contrib.layers.variance_scaling_initializer()): scaled_images = tf.cast(unscaled_images, tf.float32) / 255. activ = tf.nn.relu h = slim.separable_conv2d(scaled_images, 32, 8, 1, 4) h2 = slim.separable_conv2d(h, 64, 4, 1, 2) h3 = slim.separable_conv2d(h2, 48, 3, 1, 1) h3 = conv_to_fc(h3) return activ(fc(h3, 'fc1', nh=512, init_scale=np.sqrt(2)))
def mobilenet_separabe(images: tf.Tensor, num_classes: int, depth_multiplier: float, is_training: bool): flower_point = [ 'Conv2d_0_depthwise', 'Conv2d_0_pointwise', 'Conv2d_1_depthwise', 'Conv2d_1_pointwise', 'Final' ] with slim.arg_scope(mobilenet_v1_arg_scope(is_training=is_training)): nets, endpoints = mobilenet_v1_base(images, depth_multiplier=depth_multiplier) # add the new layer with tf.variable_scope('Flowers'): with slim.arg_scope([slim.batch_norm], is_training=is_training, center=True, scale=True, decay=0.9997, epsilon=0.001): with slim.arg_scope( [slim.conv2d, slim.separable_conv2d], normalizer_fn=slim.batch_norm, activation_fn=None, ): if depth_multiplier == 1.0 or depth_multiplier == 0.75: # nets= [?,8,10,1024] nets = slim.conv2d(nets, 512, (3, 3), padding='SAME') nets = slim.batch_norm(nets) else: pass # nets=(?, 8, 10, 512) nets = slim.separable_conv2d(nets, None, (3, 3), stride=2, scope=flower_point[0]) nets = tf.nn.relu6(nets, name=flower_point[0] + '/relu6') endpoints[flower_point[0]] = nets # nets = (?, 4, 5, 512) nets = slim.conv2d(nets, 256, (1, 1), scope=flower_point[1]) nets = tf.nn.relu6(nets, name=flower_point[1] + '/relu6') endpoints[flower_point[1]] = nets # nets = (?, 4, 5, 256) nets = slim.separable_conv2d(nets, None, (3, 3), scope=flower_point[2]) nets = tf.nn.relu6(nets, name=flower_point[2] + '/relu6') endpoints[flower_point[2]] = nets # nets = (?, 4, 5, 256) nets = slim.conv2d(nets, 128, (1, 1), scope=flower_point[3]) nets = tf.nn.relu6(nets, name=flower_point[3] + '/relu6') endpoints[flower_point[3]] = nets # nets = (?, 4, 5, 128) nets = slim.conv2d(nets, 5, (3, 3), normalizer_fn=None, activation_fn=None, scope=flower_point[4]) endpoints[flower_point[4]] = nets # nets = (?, 4, 5, 5) # tf.contrib.layers.softmax(nets) return nets, endpoints
def dem(self, feature, anchor_num, is_training=False): arg_scope = self.get_arg_scopes(is_training) with tf.variable_scope(self.scope, reuse=tf.AUTO_REUSE): with ut.tf_ops.set_arg_scope(arg_scope): feature = slim.separable_conv2d(feature, num_outputs=32, scope='dem%d_log_conv0' % anchor_num) logit = slim.separable_conv2d(feature, num_outputs=2, activation_fn=None, scope='dem%d_log_conv1' % anchor_num) cls = slim.softmax(logit)[..., 1] feature = slim.separable_conv2d(feature, num_outputs=32, scope='dem%d_reg_conv0' % anchor_num) reg = slim.separable_conv2d(feature, num_outputs=4, activation_fn=None, scope='dem%d_reg_conv1' % anchor_num) return [logit, cls, reg]
def __call__(self, fms, training=True): arg_scope = resnet_arg_scope(bn_is_training=training, ) with slim.arg_scope(arg_scope): with tf.variable_scope('CenternetHead'): # c2, c3, c4, c5 = fms # deconv_feature=c5 deconv_feature = self._unet_magic(fms) ##### kps = slim.separable_conv2d( deconv_feature, cfg.DATA.num_class, [3, 3], stride=1, activation_fn=None, normalizer_fn=None, weights_initializer=tf.initializers.random_normal( stddev=0.001), biases_initializer=tf.initializers.constant(-2.19), scope='centernet_cls_output') wh = slim.separable_conv2d( deconv_feature, 4, [3, 3], stride=1, activation_fn=None, normalizer_fn=None, weights_initializer=tf.initializers.random_normal( stddev=0.001), biases_initializer=tf.initializers.constant(0), scope='centernet_wh_output') return kps, wh * 16
def _stacked_separable_conv(net, stride, operation, filter_size): """Takes in an operations and parses it to the correct sep operation.""" num_layers, kernel_size = _operation_to_info(operation) for layer_num in range(num_layers - 1): net = tf.nn.relu(net) net = slim.separable_conv2d( net, filter_size, kernel_size, depth_multiplier=1, scope='separable_{0}x{0}_{1}'.format(kernel_size, layer_num + 1), stride=stride) net = batch_norm( net, scope='bn_sep_{0}x{0}_{1}'.format(kernel_size, layer_num + 1)) stride = 1 net = tf.nn.relu(net) net = slim.separable_conv2d( net, filter_size, kernel_size, depth_multiplier=1, scope='separable_{0}x{0}_{1}'.format(kernel_size, num_layers), stride=stride) net = batch_norm( net, scope='bn_sep_{0}x{0}_{1}'.format(kernel_size, num_layers)) return net
def fem(self, inputs, is_training=False): arg_scope = self.get_arg_scopes(is_training) with tf.variable_scope(self.scope, reuse=tf.AUTO_REUSE): with ut.tf_ops.set_arg_scope(arg_scope): # FEM32 net = slim.separable_conv2d(inputs, num_outputs=32, stride=2, depth_multiplier=32, scope='fem_conv0') net = slim.separable_conv2d(net, num_outputs=64, stride=1, scope='fem_conv1') net = slim.separable_conv2d(net, num_outputs=64, stride=2, scope='fem_conv2') b = slim.separable_conv2d(net, num_outputs=64, stride=1, scope='fem_conv3') a = slim.separable_conv2d(b, num_outputs=64, stride=2, scope='fem_conv4') return [a, b]
def _head_conv(fms,dim,child_scope): with tf.variable_scope(scope + child_scope): x,y,z,l=fms x = slim.max_pool2d(x, kernel_size=3, stride=1, padding='SAME') x = slim.separable_conv2d(x, dim // 4, kernel_size=[3, 3], stride=1, scope='branchx_3x3_pre', activation_fn=tf.nn.relu, normalizer_fn=None, biases_initializer=tf.initializers.constant(0.), ) y = slim.conv2d(y, dim // 4, kernel_size=[1, 1], stride=1, scope='branchy_3x3_pre', activation_fn=tf.nn.relu, normalizer_fn=None, biases_initializer=tf.initializers.constant(0.), ) z = slim.separable_conv2d(z, dim // 4, kernel_size=[3, 3], stride=1, scope='branchz_3x3_pre', activation_fn=tf.nn.relu, normalizer_fn=None, biases_initializer=tf.initializers.constant(0.), ) l = slim.separable_conv2d(l, dim // 4, kernel_size=[5, 5], stride=1, scope='branchse_5x5_pre', activation_fn=tf.nn.relu, normalizer_fn=None, biases_initializer=tf.initializers.constant(0.), ) fm = tf.concat([x, y, z, l], axis=3) ###128 dims return fm
def shuffle_block_v1(inputs, channels, stride, groups, depth_multiplier=1, scope=None): with tf.variable_scope(scope): prev_net = slim.separable_conv2d(inputs, None, [3, 3], depth_multiplier=depth_multiplier, stride=stride, activation_fn=None, scope='DWconv_skip') prev_net = slim.conv2d(prev_net, channels, 1, 1, scope='conv0/1x1') net = slim.conv2d(inputs, channels, 1, 1, scope='conv1/1x1') net = slim.separable_conv2d(net, None, [3, 3], depth_multiplier=depth_multiplier, stride=stride, activation_fn=None, scope='DWconv') net = slim.conv2d(net, channels, 1, 1, scope='conv2/1x1') net = tf.concat([prev_net, net], axis=3, name='concat') net = channel_shuffle(net, groups, scope="channelchuffle") return net
def large_conv(feature, scope): branch_1 = slim.separable_conv2d(feature, 16, [1, 7], padding='SAME', activation_fn=tf.nn.relu, scope='lateral_1_1/res{}'.format(5 - scope)) branch_1 = slim.separable_conv2d(branch_1, 16, [7, 1], padding='SAME', activation_fn=None, scope='lateral_1_2/res{}'.format(5 - scope)) branch_2 = slim.separable_conv2d(feature, 16, [7, 1], padding='SAME', activation_fn=tf.nn.relu, scope='lateral_2_1/res{}'.format(5 - scope)) branch_2 = slim.separable_conv2d(branch_2, 16, [1, 7], padding='SAME', activation_fn=None, scope='lateral_2_2/res{}'.format(5 - scope)) return tf.add(branch_1, branch_2, name='lateral/res{}'.format(5 - scope))
def yoloconv(images: tf.Tensor, depth_multiplier: float, is_training: bool): flower_point = ['Conv2d_0_depthwise', 'Conv2d_0_pointwise', 'Conv2d_1_depthwise', 'Conv2d_1', 'Final'] with slim.arg_scope(mobilenet_v1_arg_scope(is_training=is_training)): nets, endpoints = mobilenet_v1_base(images, depth_multiplier=depth_multiplier) # add the new layer with tf.variable_scope('Yolo'): with slim.arg_scope([slim.batch_norm], is_training=is_training, center=True, scale=True, decay=0.9997, epsilon=0.001): with slim.arg_scope([slim.conv2d, slim.separable_conv2d], padding='SAME', normalizer_fn=slim.batch_norm, activation_fn=None): # (?, 7, 10, 512) nets = slim.separable_conv2d(nets, None, (3, 3), scope=flower_point[0]) nets = tf.nn.relu6(nets, name=flower_point[0]+'/relu6') endpoints[flower_point[0]] = nets # (?, 7, 10, 512) nets = slim.conv2d(nets, 256, (1, 1), scope=flower_point[1]) nets = tf.nn.relu6(nets, name=flower_point[1]+'/relu6') endpoints[flower_point[1]] = nets # nets = (?, 7, 10, 256) nets = slim.separable_conv2d(nets, None, (3, 3), scope=flower_point[2]) nets = tf.nn.relu6(nets, name=flower_point[2]+'/relu6') endpoints[flower_point[2]] = nets # nets = (?, 7, 10, 128) nets = slim.conv2d(nets, 128, (3, 3), scope=flower_point[3]) nets = tf.nn.relu6(nets, name=flower_point[3]+'/relu6') endpoints[flower_point[3]] = nets # nets = (?, 7, 10, 128) nets = slim.conv2d(nets, 5, (3, 3), normalizer_fn=None, activation_fn=None, scope=flower_point[4]) endpoints[flower_point[4]] = nets # nets = (?, 7, 10, 125) return nets, endpoints
def basic_block(self, _input, out_channels, stride, c=0): with tf.variable_scope("basic_block_%d" % c): in_channels = _input.get_shape().as_list()[-1] net = slim.batch_norm(_input) net = slim.separable_conv2d(net, out_channels, [3, 3], depth_multiplier=1, stride=stride) # net = slim.conv2d(net, out_channels, [3, 3], stride=stride) net = slim.batch_norm(net) net = tf.nn.relu(net) net = slim.separable_conv2d(net, out_channels, [3, 3], depth_multiplier=1) net = slim.batch_norm(net) if stride == 2: shortcut = slim.avg_pool2d(_input, [2, 2]) else: shortcut = _input shortcut = tf.pad( shortcut, [[0, 0], [0, 0], [0, 0], [0, out_channels - in_channels]]) return net + shortcut
def basic_unit_with_downsampling(x, out_channels=None, downsample=True): in_channels = x.shape[3].value out_channels = 2 * in_channels if out_channels is None else out_channels stride = 2 if downsample else 1 # paradoxically, it sometimes doesn't downsample y = slim.conv2d(x, in_channels, (1, 1), stride=1, scope='conv1x1_before') y = slim.separable_conv2d(y, None, (3, 3), stride=stride, depth_multiplier=1, activation_fn=None, scope='depthwise') y = slim.conv2d(y, out_channels // 2, (1, 1), stride=1, scope='conv1x1_after') with tf.variable_scope('second_branch'): x = slim.separable_conv2d(x, None, (3, 3), stride=stride, depth_multiplier=1, activation_fn=None, scope='depthwise') x = slim.conv2d(x, out_channels // 2, (1, 1), stride=1, scope='conv1x1_after') return x, y
def separable_conv2d_same(inputs, kernel_size, stride, rate=1, scope=None): """Strided 2-D separable convolution with 'SAME' padding. Args: inputs: A 4-D tensor of size [batch, height_in, width_in, channels]. kernel_size: An int with the kernel_size of the filters. stride: An integer, the output stride. rate: An integer, rate for atrous convolution. scope: Scope. Returns: output: A 4-D tensor of size [batch, height_out, width_out, channels] with the convolution output. """ # By passing filters=None # separable_conv2d produces only a depth-wise convolution layer if stride == 1: return slim.separable_conv2d(inputs, None, kernel_size, depth_multiplier=1, stride=1, rate=rate, padding='SAME', scope=scope) else: kernel_size_effective = kernel_size + (kernel_size - 1) * (rate - 1) pad_total = kernel_size_effective - 1 pad_beg = pad_total // 2 pad_end = pad_total - pad_beg inputs = tf.pad(inputs, [[0, 0], [pad_beg, pad_end], [pad_beg, pad_end], [0, 0]]) return slim.separable_conv2d(inputs, None, kernel_size, depth_multiplier=1, stride=stride, rate=rate, padding='VALID', scope=scope)
def separable_conv2d_same(inputs, kernel_size, stride, rate=1, scope=None): """Strided 2-D separable convolution with 'SAME' padding. Args: inputs: A 4-D tensor of size [batch, height_in, width_in, channels]. kernel_size: An int with the kernel_size of the filters. stride: An integer, the output stride. rate: An integer, rate for atrous convolution. scope: Scope. Returns: output: A 4-D tensor of size [batch, height_out, width_out, channels] with the convolution output. """ # By passing filters=None # separable_conv2d produces only a depth-wise convolution layer if stride == 1: return slim.separable_conv2d(inputs, None, kernel_size, depth_multiplier=1, stride=1, rate=rate, padding='SAME', scope=scope) else: kernel_size_effective = kernel_size + (kernel_size - 1) * (rate - 1) pad_total = kernel_size_effective - 1 pad_beg = pad_total // 2 pad_end = pad_total - pad_beg inputs = tf.pad(inputs, [[0, 0], [pad_beg, pad_end], [pad_beg, pad_end], [0, 0]]) return slim.separable_conv2d(inputs, None, kernel_size, depth_multiplier=1, stride=stride, rate=rate, padding='VALID', scope=scope)
def large_conv(feature, scope, kernel_size=5, tmp_channel=128, out_channel=n_chanel): branch_1 = slim.separable_conv2d(feature, tmp_channel, [1, kernel_size], padding='SAME', activation_fn=tf.nn.relu, scope='lateral_1_1/res{}'.format(5 - scope)) branch_1 = slim.separable_conv2d(branch_1, out_channel, [kernel_size, 1], padding='SAME', activation_fn=None, scope='lateral_1_2/res{}'.format(5 - scope)) branch_2 = slim.separable_conv2d(feature, tmp_channel, [kernel_size, 1], padding='SAME', activation_fn=tf.nn.relu, scope='lateral_2_1/res{}'.format(5 - scope)) branch_2 = slim.separable_conv2d(branch_2, out_channel, [1, kernel_size], padding='SAME', activation_fn=None, scope='lateral_2_2/res{}'.format(5 - scope)) return tf.add(branch_1, branch_2, name='lateral/res{}'.format(5 - scope))
def CNN7(unscaled_images,index,filmObj): with slim.arg_scope([slim.conv2d, slim.separable_conv2d], activation_fn=tf.nn.relu, weights_initializer=tf.contrib.layers.variance_scaling_initializer()): scaled_images = tf.cast(unscaled_images, tf.float32) / 255. activ = tf.nn.relu # w_1 = tf.slice(filmObj.film_w_1,index*32,[32]) b_1 = tf.slice(filmObj.film_b_1,index*32,[32]) # w_2 = tf.slice(filmObj.film_w_2,index*64,[64]) b_2 = tf.slice(filmObj.film_b_2,index*64,[64]) # w_3 = tf.slice(filmObj.film_w_3,index*48,[48]) b_3 = tf.slice(filmObj.film_b_3,index*48,[48]) h = slim.separable_conv2d(scaled_images, 32, 8, 1, 4) # h = tf.math.add(tf.multiply(h, temp['weights_1']), temp['bias_1']) h = tf.math.add(h, b_1) h2 = slim.separable_conv2d(h, 64, 4, 1, 2) # h2 = tf.math.add(tf.multiply(h2, temp['weights_2']), temp['bias_2']) h2 = tf.math.add(h2, b_2) h3 = slim.separable_conv2d(h2, 48, 3, 1, 1) # h3 = tf.math.add(tf.multiply(h3, temp['weights_3']), temp['bias_3']) h3 = tf.math.add(h3, b_3) h3 = conv_to_fc(h3) return activ(fc(h3, 'fc1', nh=512, init_scale=np.sqrt(2)))
def ContextModule_dwlite(inputs, depth=256, scope='ContextModule'): with tf.variable_scope(scope): atrous_pool_block_1 = slim.separable_conv2d( inputs, num_outputs=depth // 4, kernel_size=[3, 3], depth_multiplier=1, rate=1 ) #DepthwiseSeparableConvBlock(inputs, depth//4, rate=1, scope='dw1') atrous_pool_block_2 = slim.separable_conv2d( inputs, num_outputs=depth // 4, kernel_size=[3, 3], depth_multiplier=1, rate=2 ) #DepthwiseSeparableConvBlock(inputs, depth//4, rate=2, scope='dw2') atrous_pool_block_3 = slim.separable_conv2d( inputs, num_outputs=depth // 4, kernel_size=[3, 3], depth_multiplier=1, rate=3 ) #DepthwiseSeparableConvBlock(inputs, depth//4, rate=4, scope='dw3') atrous_pool_block_4 = slim.separable_conv2d( inputs, num_outputs=depth // 4, kernel_size=[3, 3], depth_multiplier=1, rate=4 ) #DepthwiseSeparableConvBlock(inputs, depth//4, rate=8, scope='dw4') net = tf.concat((atrous_pool_block_1, atrous_pool_block_2, atrous_pool_block_3, atrous_pool_block_4), axis=3) return net + inputs
def newdrive(features, keep_prob, num_final_neurons, num_full_final_neurons, is_training): print("Using New Drive - Separable, Raspberry Pi Model") f = tf.reshape(features, [-1, 100, 120, 1]) print(f.shape) c = slim.conv2d(f, 32, [7, 1], activation_fn=None) c = slim.batch_norm(c, is_training=is_training, decay=0.95) c = tf.nn.relu(c) print(c.shape) c = slim.separable_conv2d(c, 64, [1, 7], 1, activation_fn=None) c = slim.batch_norm(c, is_training=is_training, decay=0.95) c = tf.nn.relu(c) c = tf.nn.max_pool(c, [1, 1, 3, 1], [1, 1, 3, 1], "VALID") print(c.shape) c = slim.separable_conv2d(c, 96, [1, 7], 1, activation_fn=None) c = slim.batch_norm(c, is_training=is_training, decay=0.95) c = tf.nn.relu(c) c = tf.nn.max_pool(c, [1, 1, 4, 1], [1, 1, 4, 1], "VALID") print(c.shape) c = slim.separable_conv2d(c, 256, [1, 10], 1, activation_fn=None, padding="VALID") c = slim.batch_norm(c, is_training=is_training, decay=0.95) c = tf.nn.relu(c) print(c.shape) c = slim.separable_conv2d(c, 256, [7, 1], 1, activation_fn=None) c = slim.batch_norm(c, is_training=is_training, decay=0.95) c = tf.nn.relu(c) c = tf.nn.max_pool(c, [1, c.shape[1], 1, 1], [1, c.shape[1], 1, 1], "VALID") c = tf.contrib.layers.flatten(c) print(c.shape) fc = slim.fully_connected(c, 256, activation_fn=None) fc = slim.batch_norm(fc, is_training=is_training, decay=0.95) fc = tf.nn.relu(fc) fc = tf.nn.dropout(fc, keep_prob) print(fc.shape) final_layer = slim.fully_connected(fc, num_final_neurons, activation_fn=None) print(final_layer.shape) full_final_layer = slim.fully_connected(fc, num_final_neurons, activation_fn=None) return final_layer, full_final_layer, fc
def ResnetBlock(x, dim, ksize, scope='rb'): with tf.variable_scope(scope): net = slim.separable_conv2d(x, dim, [ksize, ksize], scope='dw_conv1') net = slim.separable_conv2d(net, dim, [ksize, ksize], activation_fn=None, scope='dw_conv2') net = net + x return net
def mobile_block(inputs, num_output, kernel_size, stride=1, t=6, scope=None): with tf.variable_scope(scope, default_name='mobile_block'): k = inputs.shape[-1].value outputs = slim.conv2d(inputs, t * k, 1, stride=1, data_format='NHWC', scope='expand_conv') if k != num_output: shortcut = slim.conv2d(inputs, num_output, 1, stride=stride, activation_fn=None, scope='shortcut') else: shortcut = inputs if stride > 1: pad_wh = math.floor(kernel_size / 2) outputs = tf.pad( outputs, [[0, 0], [pad_wh, pad_wh], [pad_wh, pad_wh], [0, 0]]) outputs = slim.separable_conv2d(outputs, None, kernel_size, 1, stride=stride, padding='VALID', scope='depth_conv') else: outputs = slim.separable_conv2d(outputs, None, kernel_size, 1, stride=stride, padding='SAME', scope='depth_conv') outputs = slim.conv2d(outputs, num_output, 1, stride=1, data_format='NHWC', activation_fn=None, normalizer_params={ 'param_initializers': { 'gamma': tf.constant_initializer(0.01) } }, scope='point_conv') return outputs + shortcut
def VGG12(net, keep_prob): net = slim.conv2d(net, 32, [3, 3], stride=1, scope="conv1") net = slim.max_pool2d(net, [2, 2], scope='pool1') net = slim.conv2d(net, 64, [3, 3], stride=1, scope="conv2") net = slim.max_pool2d(net, [2, 2], scope='pool2') net = slim.repeat(net, 2, slim.conv2d, 128, [3, 3], scope='conv3') #net = slim.separable_conv2d(net, None, [3, 3], depth_multiplier=1, stride=1) #net = slim.conv2d(net, 128, [1, 1], stride=1) #net = slim.separable_conv2d(net, None, [3, 3], depth_multiplier=1, stride=1) #net = slim.conv2d(net, 128, [1, 1], stride=1) net = slim.max_pool2d(net, [2, 2], scope='pool3') #net = slim.repeat(net, 2, slim.conv2d, 256, [3, 3], scope='conv4') net = slim.separable_conv2d(net, None, [3, 3], depth_multiplier=1, stride=1) net = slim.conv2d(net, 256, [1, 1], stride=1) net = slim.separable_conv2d(net, None, [3, 3], depth_multiplier=1, stride=1) net = slim.conv2d(net, 256, [1, 1], stride=1) net = slim.max_pool2d(net, [2, 2], scope='pool4') #net = slim.repeat(net, 2, slim.conv2d, 256, [3, 3], scope='conv5') net = slim.separable_conv2d(net, None, [3, 3], depth_multiplier=1, stride=1) net = slim.conv2d(net, 256, [1, 1], stride=1) net = slim.separable_conv2d(net, None, [3, 3], depth_multiplier=1, stride=1) net = slim.conv2d(net, 256, [1, 1], stride=1) net = slim.max_pool2d(net, [2, 2], scope='pool5') net = slim.flatten(net, scope="flat1") net = slim.fully_connected(net, 1024, scope='fc1') net = slim.dropout(net, keep_prob, scope='drop1') net = slim.fully_connected(net, 1024, scope='fc2') net = slim.dropout(net, keep_prob, scope='drop2') net = slim.fully_connected(net, 2, activation_fn=None, scope='fc3') return net
def mobilenetv3_small_minimalistic(image, is_training=True): arg_scope = training_scope(weight_decay=cfg.TRAIN.weight_decay_factor, is_training=is_training) with tf.contrib.slim.arg_scope(arg_scope): if cfg.DATA.channel == 1: if cfg.MODEL.global_stride == 8: stride = 2 else: stride = 1 image = slim.separable_conv2d(image, 3, [3, 3], stride=stride, padding='SAME', scope='preconv') final_feature, endpoints = mobilnet_v3.small_minimalistic( image, depth_multiplier=1.0, is_training=is_training, base_only=True, finegrain_classification_mode=False) extern_conv = slim.separable_conv2d(final_feature, 128, [3, 3], stride=2, padding='SAME', scope='extern1') extern_conv = slim.separable_conv2d(extern_conv, 96, [3, 3], padding='SAME', scope='extern2') extern_conv = slim.separable_conv2d(extern_conv, 128, [3, 3], padding='SAME', scope='extern3') for k, v in endpoints.items(): print('mobile backbone output:', k, v) mobilebet_fms = [ endpoints['layer_3/expansion_output'], endpoints['layer_5/expansion_output'], endpoints['layer_9/expansion_output'], #final_feature, extern_conv ] return mobilebet_fms
def cpm(product,scope,dim): with tf.variable_scope(scope): # eyes_1 = slim.separable_conv2d(product, dim // 2, [1, 1], stride=1, rate=1, activation_fn=tf.nn.relu, # scope='eyes_1') eyes_2 = slim.separable_conv2d(product, dim // 8, [3, 3], stride=1, rate=2, activation_fn=tf.nn.relu, scope='eyes_2') eyes_3 = slim.separable_conv2d(eyes_2, dim // 8, [3, 3], stride=1, rate=2, activation_fn=tf.nn.relu, scope='eyes_3') fme_res = tf.concat([product, eyes_2, eyes_3], axis=3) return fme_res
def depthwise_conv_bn(x, kernel_size, stride=1, dilation=1, depthType='sep'): with tf.variable_scope(None, 'depthwise_conv_bn'): n, h, w, c = x.get_shape().as_list() if depthType == 'sep': x = slim.separable_conv2d(x, None, kernel_size, depth_multiplier=1, stride=stride, rate=dilation, activation_fn=None, biases_initializer=None) elif depthType == 'dep': w = tf.get_variable( 'w', [3, 3, c, 1], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.02)) x = tf.nn.depthwise_conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME', rate=None, name=None) elif depthType == 'combine': if (c >= 512) or (c < 512 and h < 128): x = slim.separable_conv2d(x, None, kernel_size, depth_multiplier=1, stride=stride, rate=dilation, activation_fn=None, biases_initializer=None) else: w = tf.get_variable( 'w', [3, 3, c, 1], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.02)) x = tf.nn.depthwise_conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME', rate=None, name=None) x = slim.batch_norm(x, activation_fn=None, fused=False) return x
def first_shufflenet_unit(inputs, groups, num_outputs): #get inputs channel inputs_channels = inputs.shape.as_list()[3] out_channels = num_outputs - inputs_channels outputs = slim.conv2d(inputs, out_channels, kernel_size=1) outputs = slim.batch_norm(outputs) outputs = tf.nn.relu(outputs) outputs = slim.separable_conv2d(outputs, None, kernel_size=3, depth_multiplier=1, stride=2) outputs = slim.batch_norm(outputs) outputs = slim.conv2d(outputs, out_channels, kernel_size=1) outputs = slim.batch_norm(outputs) output_bypass = slim.avg_pool2d(inputs, kernel_size=3, padding='SAME') result = tf.concat([output_bypass, outputs], 3) return result
def shufflenet_unit(inputs, groups, stride): #get inputs channels inputs_channels = inputs.shape.as_list()[3] outputs = group_conv_1x1(inputs, inputs_channels, groups) outputs = slim.batch_norm(outputs) outputs = tf.nn.relu(outputs) outputs = channel_shuffle(outputs, groups) outputs = slim.separable_conv2d(outputs, None, kernel_size=3, depth_multiplier=1, stride=stride) outputs = slim.batch_norm(outputs) outputs = group_conv_1x1(outputs, inputs_channels, groups) outputs = slim.batch_norm(outputs) if stride < 2: result = tf.add(inputs, outputs) else: output_bypass = slim.avg_pool2d(inputs, kernel_size=3, padding='SAME') result = tf.concat([output_bypass, outputs], 3) result = tf.nn.relu(result) return result
def conv2d(x, num_ch, k_size=3, s_size=1, scope='conv_2d', DW=True): with tf.variable_scope(name_or_scope=scope): if DW: out = slim.separable_conv2d(x, num_outputs=num_ch, kernel_size=[k_size, k_size], stride=s_size) else: out = slim.conv2d(x, num_outputs=num_ch, kernel_size=[k_size, k_size], stride=s_size) return out
def _upsample_resize(self, fm, k_size=5, dim=256, scope='upsample'): upsampled_conv = slim.separable_conv2d(fm, dim, [k_size, k_size], padding='SAME', scope=scope) upsampled_conv = tf.keras.layers.UpSampling2D(data_format='channels_last')(upsampled_conv) return upsampled_conv
def separable_conv2d_same(inputs,kernel_size,stride,rate=1,scope=None): if stride == 1: # depth_multiplier: 卷积乘子,即每个输入通道经过卷积后的输出通道数。 return slim.separable_conv2d(inputs,None,kernel_size, depth_multiplier=1,stride=1,rate=rate, padding='SAME',scope=scope) else: kernel_size_effective = kernel_size + (kernel_size-1)*(rate-1) pad_total = kernel_size_effective - 1 pad_beg = pad_total //2 pad_end = pad_total - pad_beg inputs = tf.pad(inputs, [[0,0],[pad_beg,pad_end],[pad_beg,pad_end],[0,0]]) return slim.separable_conv2d(inputs,None,kernel_size, depth_multiplier=1,stride=stride,rate=rate, padding='VALID',scope=scope)
def template(self, x): out_size = 32 expand_size = 64 kernel_size = [3, 3] activation_fn = tf.nn.relu6 out = slim.conv2d(x, out_size, kernel_size, activation_fn=activation_fn, scope='conv') out = slim.fully_connected(x, out_size, activation_fn=activation_fn, scope="fc_layer") out = slim.separable_conv2d(x, out_size, kernel_size, 1, activation_fn=activation_fn, scope='dwise') out = tf.reduce_mean(x, [1, 2], name="AGPool") out = slim.conv2d_transpose(x, out_size, kernel_size, scope='transpose') out = tf.image.resize_images(x, [300, 300], method=0)
def inverted_block(neural_net, input_filter, output_filter, expand, stride): # fundamental network struture of inverted_residual_block res_block = neural_net # pointwise conv2d, expand feature up to 6 times ( recorded in mobilenetv2 paper ) res_block = slim.conv2d(inputs=res_block, num_outputs=input_filter * expand, kernel_size=[1, 1]) # depthwise conv2d res_block = slim.separable_conv2d(inputs=res_block, num_outputs=None, kernel_size=[3, 3], stride=stride) res_block = slim.conv2d(inputs=res_block, num_outputs=output_filter, kernel_size=[1, 1], activation_fn=None) # stride 2 blocks if stride == 2: return res_block # stride 1 block else: if input_filter != output_filter: neural_net = slim.conv2d(inputs=neural_net, num_outputs=output_filter, kernel_size=[1, 1], activation_fn=None) return tf.add(res_block, neural_net)
def __init__(self, num_classes, input_tensor, is_training=False, data_format='NHWC', priors_rule='object_detection_api', priors=[], mobilenet_version='v2', depth_multiplier=1.0, min_depth=16, weight_regularization=4e-5): """ Args: num_classes: Number of classes including a background class. input_tensor: Input 4D tensor. is_training: Is training or inference stage. data_format: 'NHWC' or 'NCHW'. priors_rule: 'caffe', 'object_detection_api', 'custom'. priors: List of list of prior sizes (relative sizes). Only for priors_rule='custom'. mobilenet_version: 'v1' or 'v2'. depth_multiplier: MobileNet depth multiplier. min_depth: Minimum channels count in MobileNet. weight_regularization: l2 weight regularization scale. """ assert data_format in ['NHWC', 'NCHW'] assert priors_rule in ['caffe', 'object_detection_api', 'custom'] self.data_format = data_format if self.data_format == 'NCHW': input_tensor = tf.transpose(input_tensor, [0, 3, 1, 2]) self.input_shape = input_tensor.get_shape().as_list() self.input_tensor = input_tensor if self.data_format == 'NCHW': spatial_dim_axis = [2, 3] elif self.data_format == 'NHWC': spatial_dim_axis = [1, 2] self.version = mobilenet_version super(MobileNetSSD, self).__init__(num_classes=num_classes, input_shape=self.input_shape, data_format=data_format) self.is_training = is_training if mobilenet_version == 'v2': mobilenet_base = mobilenet_v2_base base_scope = mobilenet_v2.training_scope base_layers = ['layer_7/output', 'layer_15/expansion_output', 'layer_19'] elif mobilenet_version == 'v1': mobilenet_base = mobilenet_v1_base base_scope = mobilenet.training_scope base_layers = ['Conv2d_5_pointwise', 'Conv2d_11_pointwise', 'Conv2d_13_pointwise'] else: tf.logging.error('Wrong MobileNet version = {}'.format(mobilenet_version)) exit(0) def scope_fn(): batch_norm_params = { 'is_training': self.is_training, 'center': True, 'scale': True, 'decay': 0.9997, 'epsilon': 0.001, 'fused': True, 'data_format': data_format } affected_ops = [slim.conv2d, slim.separable_conv2d, slim.conv2d_transpose] with (slim.arg_scope([slim.batch_norm], **batch_norm_params)): with slim.arg_scope( affected_ops, weights_regularizer=slim.l2_regularizer(scale=float(weight_regularization)), weights_initializer=tf.truncated_normal_initializer(mean=0, stddev=0.03), activation_fn=tf.nn.relu6, normalizer_fn=slim.batch_norm) as sc: return sc with slim.arg_scope(base_scope(is_training=None)): with slim.arg_scope(scope_fn()): _, image_features = mobilenet_base( self.input_tensor, final_endpoint=base_layers[-1], depth_multiplier=depth_multiplier, min_depth=min_depth, use_explicit_padding=False, is_training=self.is_training) head_feature_map_names = base_layers[-2:] head_feature_map_tensors = [image_features[name] for name in head_feature_map_names] feature_map = image_features[base_layers[-1]] depths = [512, 256, 256, 128] depths = [int(d * depth_multiplier) for d in depths] with tf.variable_scope('extra_features'): with slim.arg_scope(scope_fn()): for i, depth in enumerate(depths): intermediate_layer = slim.conv2d(feature_map, int(depth / 2), [1, 1], stride=1, scope='intermediate_{0}'.format(i + 1)) # feature_map = slim.conv2d(intermediate_layer, depth, [3, 3], stride=2, scope='feature_map_{0}'.format(i + 1)) feature_map = slim.separable_conv2d( intermediate_layer, None, [3, 3], depth_multiplier=1, padding='SAME', stride=2, scope='feature_map_dw_{0}'.format(i + 1)) output_feature_name = 'feature_map_{0}'.format(i + 1) feature_map = slim.conv2d( feature_map, int(depth), [1, 1], padding='SAME', stride=1, scope=output_feature_name) head_feature_map_names.append(output_feature_name) head_feature_map_tensors.append(feature_map) variances = [0.1, 0.1, 0.2, 0.2] if priors_rule == 'caffe': scale = [0.2, 0.35, 0.5, 0.65, 0.8, 0.95] dicts = self._create_caffe_priors(self.input_shape, spatial_dim_axis, scale, variances, head_feature_map_tensors, head_feature_map_names) elif priors_rule == 'object_detection_api': scale = [0.2, 0.35, 0.5, 0.65, 0.8, 0.95, 1.] dicts = self._create_obj_det_priors(self.input_shape, spatial_dim_axis, scale, variances, head_feature_map_tensors, head_feature_map_names) elif priors_rule == 'custom': assert len(priors) == len(head_feature_map_tensors) dicts = self._create_custom_priors(self.input_shape, spatial_dim_axis, priors, variances, head_feature_map_tensors, head_feature_map_names) with slim.arg_scope(scope_fn()): self.create_heads(head_feature_map_tensors, dicts)