def slim_net_original(image, keep_prob): with arg_scope([layers.conv2d, layers.fully_connected], biases_initializer=tf.random_normal_initializer(stddev=0.1)): # conv2d(inputs, num_outputs, kernel_size, stride=1, padding='SAME', # activation_fn=nn.relu, normalizer_fn=None, normalizer_params=None, # weights_initializer=initializers.xavier_initializer(), weights_regularizer=None, # biases_initializer=init_ops.zeros_initializer, biases_regularizer=None, scope=None): net = layers.conv2d(image, 32, [5, 5], scope='conv1', weights_regularizer=regularizers.l1_regularizer(0.5)) # max_pool(inputs, kernel_size, stride=2, padding='VALID', scope=None) net = layers.max_pool2d(net, 2, scope='pool1') net = layers.conv2d(net, 64, [5, 5], scope='conv2', weights_regularizer=regularizers.l2_regularizer(0.5)) summaries.summarize_tensor(net, tag='conv2') net = layers.max_pool2d(net, 2, scope='pool2') net = layers.flatten(net, scope='flatten1') # fully_connected(inputs, num_outputs, activation_fn=nn.relu, normalizer_fn=None, # normalizer_params=None, weights_initializer=initializers.xavier_initializer(), # weights_regularizer=None, biases_initializer=init_ops.zeros_initializer, # biases_regularizer=None, scope=None): net = layers.fully_connected(net, 1024, scope='fc1') # dropout(inputs, keep_prob=0.5, is_training=True, scope=None) net = layers.dropout(net, keep_prob=keep_prob, scope='dropout1') net = layers.fully_connected(net, 10, scope='fc2') return net
def bottleneck_hole(inputs, depth, depth_bottleneck, stride, rate=2, outputs_collections=None, scope=None): with variable_scope.variable_scope(scope, 'bottleneck_v1', [inputs]) as sc: depth_in = utils.last_dimension(inputs.get_shape(), min_rank=4) if depth == depth_in: shortcut = resnet_utils.subsample(inputs, stride, 'shortcut') else: shortcut = layers.conv2d( inputs, depth, [1, 1], stride=stride, activation_fn=None, scope='shortcut') residual = layers.conv2d( inputs, depth_bottleneck, [1, 1], stride=1, scope='conv1') residual = layers_lib.conv2d(residual, depth_bottleneck, [3, 3], stride=1, rate=rate, padding='SAME', scope='conv2') residual = layers.conv2d( residual, depth, [1, 1], stride=1, activation_fn=None, scope='conv3') output = nn_ops.relu(shortcut + residual) return utils.collect_named_outputs(outputs_collections, sc.name, output)
def dense_block(inputs, depth, depth_bottleneck, stride, name, rate=1): depth_in = inputs.get_shape()[3] if depth == depth_in: if stride == 1: shortcut = inputs else: shortcut = layers.max_pool2d(inputs, [1, 1], stride=factor, scope=name+'_shortcut') else: shortcut = layers.conv2d( inputs, depth, [1, 1], stride=stride, activation_fn=None, scope=name+'_shortcut') if PRINT_LAYER_LOG: print(name+'_shortcut', shortcut.get_shape()) residual = layers.conv2d( inputs, depth_bottleneck, [1, 1], stride=1, scope=name+'_conv1') if PRINT_LAYER_LOG: print(name+'_conv1', residual.get_shape()) residual = resnet_utils.conv2d_same( residual, depth_bottleneck, 3, stride, rate=rate, scope=name+'_conv2') if PRINT_LAYER_LOG: print(name+'_conv2', residual.get_shape()) residual = layers.conv2d( residual, depth, [1, 1], stride=1, activation_fn=None, scope=name+'_conv3') if PRINT_LAYER_LOG: print(name+'_conv3', residual.get_shape()) output = nn_ops.relu(shortcut + residual) return output
def separate_1x1_conv_layer(self, inputs, num_outputs, stride, layer_name, scope): int_scope = scope + '_sep' with arg_scope( [layers.conv2d], trainable=False, normalizer_fn=None, normalizer_params=None, biases_initializer=None, biases_regularizer=None ): #make first layer clean, no BN no biases no activation func K = self._net_desc[layer_name] intermediate = layers.conv2d(inputs, K[0], [1, 1], stride=1, scope=int_scope) with arg_scope([ layers.conv2d ], trainable=False): #make second layer with BN but with no biases net = layers.conv2d(intermediate, num_outputs, [1, 1], stride=stride, scope=scope) return net
def bottleneck(self, inputs, depth, depth_bottleneck, stride, rate=1, outputs_collections=None, scope=None): """Bottleneck residual unit variant with BN after convolutions. This is the original residual unit proposed in [1]. See Fig. 1(a) of [2] for its definition. Note that we use here the bottleneck variant which has an extra bottleneck layer. When putting together two consecutive ResNet blocks that use this unit, one should use stride = 2 in the last unit of the first block. Args: inputs: A tensor of size [batch, height, width, channels]. depth: The depth of the ResNet unit output. depth_bottleneck: The depth of the bottleneck layers. stride: The ResNet unit's stride. Determines the amount of downsampling of the units output compared to its input. rate: An integer, rate for atrous convolution. outputs_collections: Collection to add the ResNet unit output. scope: Optional variable_scope. Returns: The ResNet unit's output. """ with variable_scope.variable_scope(scope, 'bottleneck_v1', [inputs]) as sc: depth_in = utils.last_dimension(inputs.get_shape(), min_rank=4) if depth == depth_in: shortcut = resnet_utils.subsample(inputs, stride, 'shortcut') else: shortcut = layers.conv2d( inputs, depth, [1, 1], stride=stride, activation_fn=None, scope='shortcut') residual = layers.conv2d( inputs, depth_bottleneck, [1, 1], stride=1, scope='conv1') layer_name = LayerName(sc.name + '/conv2', 'net_layer') # name = sc.name.replace(self._resnet_scope,'') + '/conv2' if layer_name in self._comp_weights_dict.keys(): residual = self.separate_conv_layer(residual, depth_bottleneck, 3, stride, rate=rate, layer_name='conv2', full_layer_name=layer_name) else: residual = resnet_utils.conv2d_same(residual, depth_bottleneck, 3, stride, rate=rate, scope='conv2') residual = layers.conv2d( residual, depth, [1, 1], stride=1, activation_fn=None, scope='conv3') output = nn_ops.relu(shortcut + residual) return utils.collect_named_outputs(outputs_collections, sc.name, output)
def _TestDeviceName(self, fn): graph = ops.Graph() with graph.as_default(): batch_size, height, width, depth = 5, 128, 128, 3 inputs = array_ops.zeros((batch_size, height, width, depth)) conv = layers.conv2d(inputs, 32, [5, 5], stride=2, padding='SAME', weights_initializer=self._WeightInit(0.09), activation_fn=None, scope='test') _ = nn_ops.relu6(conv) device_name = '/job:oink/task:0/device:CPU:0' q_graph = fn(graph, device_name_or_function=device_name) orig_variable_names = set([ v.name for v in graph.get_collection(ops.GraphKeys.GLOBAL_VARIABLES) ]) q_variables = q_graph.get_collection(ops.GraphKeys.GLOBAL_VARIABLES) # Ensure that variables were added. self.assertTrue(len(orig_variable_names) < len(q_variables)) # All added variables should have the specified device name. for var in q_variables: if var.name not in orig_variable_names: self.assertEqual(var.device, device_name)
def _LayerWithIdentity(self, input_tensor=None, scope='test', post_activation_bypass=False): """Add a basic conv, identity, batch norm with skip to the default graph.""" batch_size, height, width, depth = 5, 128, 128, 3 if input_tensor is None: input_tensor = array_ops.zeros((batch_size, height, width, depth)) weight_init = init_ops.truncated_normal_initializer with ops.name_scope(scope): output = layers.conv2d(input_tensor, depth, [5, 5], padding='SAME', weights_initializer=weight_init(0.09), activation_fn=None, normalizer_fn=None, biases_initializer=None) output = array_ops.identity(output, name='conv_out') output = layers.batch_norm(output, center=True, scale=True, decay=1.0 - 0.003, fused=True) output = array_ops.identity(output, name='bn_out') if post_activation_bypass: output += input_tensor return output
def _LayerWithActivationProcessing(self, input_tensor=None, scope='test', post_activation_bypass=False): batch_size, height, width, depth = 5, 128, 128, 3 if input_tensor is None: input_tensor = array_ops.zeros((batch_size, height, width, depth)) weight_init = init_ops.truncated_normal_initializer with ops.name_scope(scope): output = layers.conv2d(input_tensor, depth, [5, 5], padding='SAME', weights_initializer=weight_init(0.09), activation_fn=None, normalizer_fn=None, biases_initializer=None) output = layers.batch_norm(output, center=True, scale=True, decay=1.0 - 0.003, fused=True) output = nn_ops.relu6(output) scaled_output1 = math_ops.mul(2.0, output) scaled_output2 = math_ops.mul(3.0, output) output = scaled_output1 + scaled_output2 return output
def _TestDeviceName(self, is_training): graph = ops.Graph() with graph.as_default(): batch_size, height, width, depth = 5, 128, 128, 3 inputs = array_ops.zeros((batch_size, height, width, depth)) conv = layers.conv2d( inputs, 32, [5, 5], stride=2, padding='SAME', weights_initializer=self._WeightInit(0.09), activation_fn=None, scope='test') _ = nn_ops.relu6(conv) device_name = '/job:oink/task:0/device:CPU:0' if is_training: q_graph = quantize_graph.create_training_graph( graph, device_name_or_function=device_name) else: q_graph = quantize_graph.create_eval_graph( graph, device_name_or_function=device_name) orig_variable_names = set( [v.name for v in graph.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)]) q_variables = q_graph.get_collection(ops.GraphKeys.GLOBAL_VARIABLES) # Ensure that variables were added. self.assertTrue(len(orig_variable_names) < len(q_variables)) # All added variables should have the specified device name. for var in q_variables: if var.name not in orig_variable_names: self.assertEqual(var.device, device_name)
def _LayerWithIdentity(self, input_tensor=None, scope='test', post_activation_bypass=False): """Add a basic conv, identity, batch norm with skip to the default graph.""" batch_size, height, width, depth = 5, 128, 128, 3 if input_tensor is None: input_tensor = array_ops.zeros((batch_size, height, width, depth)) weight_init = init_ops.truncated_normal_initializer with ops.name_scope(scope): output = layers.conv2d( input_tensor, depth, [5, 5], padding='SAME', weights_initializer=weight_init(0.09), activation_fn=None, normalizer_fn=None, biases_initializer=None) output = array_ops.identity(output, name='conv_out') output = layers.batch_norm( output, center=True, scale=True, decay=1.0 - 0.003, fused=True) output = array_ops.identity(output, name='bn_out') if post_activation_bypass: output += input_tensor return output
def _LayerWithActivationProcessing(self, input_tensor=None, scope='test', post_activation_bypass=False): batch_size, height, width, depth = 5, 128, 128, 3 if input_tensor is None: input_tensor = array_ops.zeros((batch_size, height, width, depth)) weight_init = init_ops.truncated_normal_initializer with ops.name_scope(scope): output = layers.conv2d( input_tensor, depth, [5, 5], padding='SAME', weights_initializer=weight_init(0.09), activation_fn=None, normalizer_fn=None, biases_initializer=None) output = layers.batch_norm( output, center=True, scale=True, decay=1.0 - 0.003, fused=True) output = nn_ops.relu6(output) scaled_output1 = math_ops.mul(2.0, output) scaled_output2 = math_ops.mul(3.0, output) output = scaled_output1 + scaled_output2 return output
def slim_net_original(image, keep_prob): with arg_scope( [layers.conv2d, layers.fully_connected], biases_initializer=tf.random_normal_initializer(stddev=0.1)): # conv2d(inputs, num_outputs, kernel_size, stride=1, padding='SAME', # activation_fn=nn.relu, normalizer_fn=None, normalizer_params=None, # weights_initializer=initializers.xavier_initializer(), weights_regularizer=None, # biases_initializer=init_ops.zeros_initializer, biases_regularizer=None, scope=None): net = layers.conv2d( image, 32, [5, 5], scope='conv1', weights_regularizer=regularizers.l1_regularizer(0.5)) # max_pool(inputs, kernel_size, stride=2, padding='VALID', scope=None) net = layers.max_pool2d(net, 2, scope='pool1') net = layers.conv2d( net, 64, [5, 5], scope='conv2', weights_regularizer=regularizers.l2_regularizer(0.5)) summaries.summarize_tensor(net, tag='conv2') net = layers.max_pool2d(net, 2, scope='pool2') net = layers.flatten(net, scope='flatten1') # fully_connected(inputs, num_outputs, activation_fn=nn.relu, normalizer_fn=None, # normalizer_params=None, weights_initializer=initializers.xavier_initializer(), # weights_regularizer=None, biases_initializer=init_ops.zeros_initializer, # biases_regularizer=None, scope=None): net = layers.fully_connected(net, 1024, scope='fc1') # dropout(inputs, keep_prob=0.5, is_training=True, scope=None) net = layers.dropout(net, keep_prob=keep_prob, scope='dropout1') net = layers.fully_connected(net, 10, scope='fc2') return net
def _ConvLayer(self): """Add a basic convolution layer to the default graph.""" batch_size, height, width, depth = 5, 128, 128, 3 inputs = array_ops.zeros((batch_size, height, width, depth)) weight_init = init_ops.truncated_normal_initializer conv = layers.conv2d(inputs, 32, [5, 5], stride=2, padding='SAME', weights_initializer=weight_init(0.09), activation_fn=None, scope='test') _ = nn_ops.relu6(conv)
def _ConvLayer(self): """Add a basic convolution layer to the default graph.""" batch_size, height, width, depth = 5, 128, 128, 3 inputs = array_ops.zeros((batch_size, height, width, depth)) weight_init = init_ops.truncated_normal_initializer conv = layers.conv2d( inputs, 32, [5, 5], stride=2, padding='SAME', weights_initializer=weight_init(0.09), activation_fn=None, scope='test') _ = nn_ops.relu6(conv)
def conv2d(inputs, num_outputs, kernel_size, stride=1, rate=1, padding='SAME', explicit_padding=True, activation_fn=nn_ops.relu, normalizer_fn=None, normalizer_params=None, weights_initializer=None, weights_regularizer=None, biases_initializer=tf.zeros_initializer, biases_regularizer=None, outputs_collections=None, scope=None): """ Avoid layer.conv2d / nn.conv2d with same padding!, their same padding mode is strange! when stride is 1, it does equal same padding when stride is > 1, it pads at features's right side """ kernel_size = [kernel_size, kernel_size ] if type(kernel_size) is int else kernel_size rate = [rate, rate] if type(rate) is int else rate if padding == 'SAME' and explicit_padding: inputs = same_padding(inputs, kernel_size, rate) padding = 'VALID' conv = layers_lib.conv2d(inputs, num_outputs, kernel_size, stride=stride, padding=padding, rate=rate, activation_fn=activation_fn, normalizer_fn=normalizer_fn, normalizer_params=normalizer_params, weights_initializer=weights_initializer, weights_regularizer=weights_regularizer, biases_initializer=biases_initializer, biases_regularizer=biases_regularizer, outputs_collections=outputs_collections, scope=scope) return conv
def _ConvLayer( self, input_tensor=None, scope='test', pre_activation_bypass=False, post_activation_bypass=False): """Add a basic convolution layer to the default graph.""" batch_size, height, width, depth = 5, 128, 128, 3 if input_tensor is None: input_tensor = array_ops.zeros((batch_size, height, width, depth)) weight_init = init_ops.truncated_normal_initializer with ops.name_scope(scope): output = layers.conv2d( input_tensor, depth, [5, 5], padding='SAME', weights_initializer=weight_init(0.09), activation_fn=None) if pre_activation_bypass: output += input_tensor output = nn_ops.relu6(output) if post_activation_bypass: output += input_tensor return output
def _TestDefaultGraph(self, fn): with ops.Graph().as_default() as g: batch_size, height, width, depth = 5, 128, 128, 3 inputs = array_ops.zeros((batch_size, height, width, depth)) conv = layers.conv2d( inputs, 32, [5, 5], stride=2, padding='SAME', weights_initializer=self._WeightInit(0.09), activation_fn=None, scope='test') _ = nn_ops.relu6(conv) orig_variable_names = set( [v.name for v in g.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)]) fn() q_variables = g.get_collection(ops.GraphKeys.GLOBAL_VARIABLES) # Ensure that variables were added. self.assertTrue(len(orig_variable_names) < len(q_variables))
def build_layer_fn(x, w_initializer, b_initializer): var_collection = { 'weights': ['CONTRIB_LAYERS_CONV2D_WEIGHTS'], 'biases': ['CONTRIB_LAYERS_CONV2D_BIASES'] } net = contrib_layers.conv2d(x, 3, 3, weights_initializer=w_initializer, biases_initializer=b_initializer, variables_collections=var_collection) weight_vars = ops.get_collection('CONTRIB_LAYERS_CONV2D_WEIGHTS') self.assertEquals(1, len(weight_vars)) bias_vars = ops.get_collection('CONTRIB_LAYERS_CONV2D_BIASES') self.assertEquals(1, len(bias_vars)) expected_normalized_vars = { 'contrib.layers.conv2d.weights': weight_vars[0] } expected_not_normalized_vars = { 'contrib.layers.conv2d.bias': bias_vars[0] } return net, expected_normalized_vars, expected_not_normalized_vars
def conv2d(inputs, num_outputs, kernel_size, stride=1, rate=1, padding='SAME', activation_fn=nn_ops.relu, normalizer_fn=None, normalizer_params=None, weights_initializer=None, weights_regularizer=None, biases_initializer=tf.zeros_initializer, biases_regularizer=None, outputs_collections=None, scope=None): """ use equally padding """ if padding == 'SAME': inputs = same_padding(inputs, [kernel_size, kernel_size], [rate, rate]) conv = layers_lib.conv2d(inputs, num_outputs, kernel_size, stride=stride, padding='VALID', rate=rate, activation_fn=activation_fn, normalizer_fn=normalizer_fn, normalizer_params=normalizer_params, weights_initializer=weights_initializer, weights_regularizer=weights_regularizer, biases_initializer=biases_initializer, biases_regularizer=biases_regularizer, outputs_collections=outputs_collections, scope=scope) return conv
def build_layer_fn(x, w_initializer, b_initializer): var_collection = { 'weights': ['CONTRIB_LAYERS_CONV2D_WEIGHTS'], 'biases': ['CONTRIB_LAYERS_CONV2D_BIASES'] } net = contrib_layers.conv2d( x, 3, 3, weights_initializer=w_initializer, biases_initializer=b_initializer, variables_collections=var_collection) weight_vars = ops.get_collection('CONTRIB_LAYERS_CONV2D_WEIGHTS') self.assertEquals(1, len(weight_vars)) bias_vars = ops.get_collection('CONTRIB_LAYERS_CONV2D_BIASES') self.assertEquals(1, len(bias_vars)) expected_normalized_vars = { 'contrib.layers.conv2d.weights': weight_vars[0] } expected_not_normalized_vars = { 'contrib.layers.conv2d.bias': bias_vars[0] } return net, expected_normalized_vars, expected_not_normalized_vars
def YOLOFLY(max_box_per_image, warm_up_batches, config): labels = list(config['model']['labels']) nb_class = len(labels) anchors = config['model']['anchors'] grid_scales = config['train']['grid_scales'] obj_scale = config['train']['obj_scale'], noobj_scale = config['train']['noobj_scale'], xywh_scale = config['train']['xywh_scale'], class_scale = config['train']['class_scale'] batch_size = config['train']['batch_size'] ignore_thresh = config['train']['ignore_thresh'] max_grid = [ config['model']['max_input_size'], config['model']['max_input_size'] ] input_image = tf.keras.Input(shape=(None, None, 3), name='input_image') true_boxes = tf.keras.Input(shape=(1, 1, 1, max_box_per_image, 4), name='true_boxes') true_yolo_1 = tf.keras.Input(shape=(None, None, len(anchors) // 6, 4 + 1 + nb_class), name='true_yolo_1') true_yolo_2 = tf.keras.Input(shape=(None, None, len(anchors) // 6, 4 + 1 + nb_class), name='true_yolo_2') true_yolo_3 = tf.keras.Input(shape=(None, None, len(anchors) // 6, 4 + 1 + nb_class), name='true_yolo_3') # phase = False phase = tf.placeholder_with_default(False, shape=(), name='phase') normalizer_params = {'is_training': phase, 'scale': True} # tiny dark-net as backbone network y, s_skip_15, s_skip_10 = BaseNet(input_image, normalizer_params) # Layer 16 y = conv2d(y, 256, 3, stride=1, padding='SAME', scope='layer_16', activation_fn=tf.nn.leaky_relu, normalizer_fn=batch_norm, normalizer_params=normalizer_params) s_skip_16 = y # Layer 17 s_skip_15 = conv2d(s_skip_15, 256, 1, stride=1, padding='SAME', scope='layer_17', activation_fn=tf.nn.leaky_relu, normalizer_fn=batch_norm, normalizer_params=normalizer_params) y = conv2d_transpose(y, 256, 3, stride=2, padding='SAME', scope='trans_17', activation_fn=tf.nn.leaky_relu, normalizer_fn=batch_norm, normalizer_params=normalizer_params) y = tf.add(y, s_skip_15) # Layer 18 y = conv2d(y, 256, 3, stride=1, padding='SAME', scope='layer_18', activation_fn=tf.nn.leaky_relu, normalizer_fn=batch_norm, normalizer_params=normalizer_params) s_skip_18 = y # Layer 19 s_skip_10 = conv2d(s_skip_10, 256, 1, stride=1, padding='SAME', scope='layer_19', activation_fn=tf.nn.leaky_relu, normalizer_fn=batch_norm, normalizer_params=normalizer_params) y = conv2d_transpose(y, 256, 3, stride=2, padding='SAME', scope='trans_19', activation_fn=tf.nn.leaky_relu, normalizer_fn=batch_norm, normalizer_params=normalizer_params) y = tf.add(y, s_skip_10) # Layer 20 y = conv2d(y, 256, 3, stride=1, padding='SAME', scope='layer_20', activation_fn=tf.nn.leaky_relu, normalizer_fn=batch_norm, normalizer_params=normalizer_params) # Layer 21 s_pred_yolo_3 = conv2d(y, 3 * (4 + 1 + nb_class), 1, stride=1, padding='SAME', scope='layer_21', activation_fn=None) # Layer Loss 3 s_loss_yolo_3 = CustomLossLayer( anchors[:6], [4 * num for num in max_grid], batch_size, warm_up_batches, ignore_thresh, grid_scales[2], obj_scale, noobj_scale, xywh_scale, class_scale)([input_image, s_pred_yolo_3, true_yolo_3, true_boxes]) # Layer 22 y = conv2d(y, 256, 3, stride=2, padding='SAME', scope='layer_22', activation_fn=tf.nn.leaky_relu, normalizer_fn=batch_norm, normalizer_params=normalizer_params) # Layer 23 s_skip_18 = conv2d(s_skip_18, 256, 1, stride=1, padding='SAME', scope='layer_23', activation_fn=tf.nn.leaky_relu, normalizer_fn=batch_norm, normalizer_params=normalizer_params) y = tf.add(y, s_skip_18) # Layer 24 y = conv2d(y, 256, 3, stride=1, padding='SAME', scope='layer_24', activation_fn=tf.nn.leaky_relu, normalizer_fn=batch_norm, normalizer_params=normalizer_params) # Layer 25 s_pred_yolo_2 = conv2d(y, 3 * (4 + 1 + nb_class), 1, stride=1, padding='SAME', scope='layer_25', activation_fn=None) # Layer Loss 2 s_loss_yolo_2 = CustomLossLayer( anchors[6:12], [2 * num for num in max_grid], batch_size, warm_up_batches, ignore_thresh, grid_scales[1], obj_scale, noobj_scale, xywh_scale, class_scale)([input_image, s_pred_yolo_2, true_yolo_2, true_boxes]) # Layer 26 y = conv2d(y, 256, 3, stride=2, padding='SAME', scope='layer_26', activation_fn=tf.nn.leaky_relu, normalizer_fn=batch_norm, normalizer_params=normalizer_params) # Layer 27 s_skip_16 = conv2d(s_skip_16, 256, 1, stride=1, padding='SAME', scope='layer_27', activation_fn=tf.nn.leaky_relu, normalizer_fn=batch_norm, normalizer_params=normalizer_params) y = tf.add(y, s_skip_16) # Layer 28 y = conv2d(y, 256, 3, stride=1, padding='SAME', scope='layer_28', activation_fn=tf.nn.leaky_relu, normalizer_fn=batch_norm, normalizer_params=normalizer_params) # Layer 29 s_pred_yolo_1 = conv2d(y, 3 * (4 + 1 + nb_class), 1, stride=1, padding='SAME', scope='layer_29', activation_fn=None) # Layer Loss 1 s_loss_yolo_1 = CustomLossLayer( anchors[12:], [1 * num for num in max_grid], batch_size, warm_up_batches, ignore_thresh, grid_scales[0], obj_scale, noobj_scale, xywh_scale, class_scale)([input_image, s_pred_yolo_1, true_yolo_1, true_boxes]) s_output_nodes = [s_pred_yolo_1, s_pred_yolo_2, s_pred_yolo_3] s_loss = tf.reduce_sum([s_loss_yolo_1, s_loss_yolo_2, s_loss_yolo_3], name="loss") return s_loss, s_output_nodes
def vgg_16_small_img(inputs, num_classes=1000, is_training=True, dropout_keep_prob=0.5, spatial_squeeze=True, scope='vgg_16'): # Collect outputs for conv2d, fully_connected and max_pool2d. net = layers_lib.conv2d(inputs, 64, [3, 3], padding="SAME", data_format="NHWC", scope="conv1") net = tf.nn.relu(net, name="relu_conv1") net = layers_lib.conv2d(net, 64, [3, 3], padding="SAME", data_format="NHWC", scope="conv2") net = tf.nn.relu(net, name="relu_conv2") net = layers_lib.max_pool2d(net, [2, 2], scope='pool1') net = layers_lib.conv2d(net, 128, [3, 3], padding="SAME", data_format="NHWC", scope="conv3") net = tf.nn.relu(net, name="relu_conv3") net = layers_lib.conv2d(net, 128, [3, 3], padding="SAME", data_format="NHWC", scope="conv4") net = tf.nn.relu(net, name="relu_conv4") net = layers_lib.max_pool2d(net, [2, 2], scope='pool2') net = layers_lib.conv2d(net, 256, [3, 3], padding="SAME", data_format="NHWC", scope="conv5") net = tf.nn.relu(net, name="relu_conv5") net = layers_lib.conv2d(net, 256, [3, 3], padding="SAME", data_format="NHWC", scope="conv6") net = tf.nn.relu(net, name="relu_conv6") net = layers_lib.conv2d(net, 256, [3, 3], padding="SAME", data_format="NHWC", scope="conv7") net = tf.nn.relu(net, name="relu_conv7") net = layers_lib.max_pool2d(net, [2, 2], scope='pool3') net = layers_lib.conv2d(net, 512, [3, 3], padding="SAME", data_format="NHWC", scope="conv8") net = tf.nn.relu(net, name="relu_conv8") net = layers_lib.conv2d(net, 512, [3, 3], padding="SAME", data_format="NHWC", scope="conv9") net = tf.nn.relu(net, name="relu_conv9") net = layers_lib.conv2d(net, 512, [3, 3], padding="SAME", data_format="NHWC", scope="conv10") net = tf.nn.relu(net, name="relu_conv10") net = layers_lib.max_pool2d(net, [2, 2], scope='pool4') net = layers_lib.conv2d(net, 512, [3, 3], padding="SAME", data_format="NHWC", scope="conv11") net = tf.nn.relu(net, name="relu_conv11") net = layers_lib.conv2d(net, 512, [3, 3], padding="SAME", data_format="NHWC", scope="conv12") net = tf.nn.relu(net, name="relu_conv12") net = layers_lib.conv2d(net, 512, [3, 3], padding="SAME", data_format="NHWC", scope="conv13") net = tf.nn.relu(net, name="relu_conv13") net = layers_lib.max_pool2d(net, [2, 2], scope='pool5') # Use conv2d instead of fully_connected layers. net = layers.conv2d(net, 512, [1, 1], padding='VALID', scope='fc6') net = tf.nn.relu(net, name="relu_fc6") net = layers_lib.dropout(net, dropout_keep_prob, is_training=is_training, scope='dropout6') net = layers.conv2d(net, num_classes, [1, 1], activation_fn=None, normalizer_fn=None, scope='fc8') # Convert end_points_collection into a end_point dict. if spatial_squeeze: net = array_ops.squeeze(net, [1, 2], name='fc8/squeezed') return net
def resnet_v1(inputs, blocks, num_classes=None, is_training=True, global_pool=True, output_stride=None, include_root_block=True, reuse=None, scope=None): """Generator for v1 ResNet models. This function generates a family of ResNet v1 models. See the resnet_v1_*() methods for specific model instantiations, obtained by selecting different block instantiations that produce ResNets of various depths. Training for image classification on Imagenet is usually done with [224, 224] inputs, resulting in [7, 7] feature maps at the output of the last ResNet block for the ResNets defined in [1] that have nominal stride equal to 32. However, for dense prediction tasks we advise that one uses inputs with spatial dimensions that are multiples of 32 plus 1, e.g., [321, 321]. In this case the feature maps at the ResNet output will have spatial shape [(height - 1) / output_stride + 1, (width - 1) / output_stride + 1] and corners exactly aligned with the input image corners, which greatly facilitates alignment of the features to the image. Using as input [225, 225] images results in [8, 8] feature maps at the output of the last ResNet block. For dense prediction tasks, the ResNet needs to run in fully-convolutional (FCN) mode and global_pool needs to be set to False. The ResNets in [1, 2] all have nominal stride equal to 32 and a good choice in FCN mode is to use output_stride=16 in order to increase the density of the computed features at small computational and memory overhead, cf. http://arxiv.org/abs/1606.00915. Args: inputs: A tensor of size [batch, height_in, width_in, channels]. blocks: A list of length equal to the number of ResNet blocks. Each element is a resnet_utils.Block object describing the units in the block. num_classes: Number of predicted classes for classification tasks. If None we return the features before the logit layer. is_training: whether batch_norm layers are in training mode. global_pool: If True, we perform global average pooling before computing the logits. Set to True for image classification, False for dense prediction. output_stride: If None, then the output will be computed at the nominal network stride. If output_stride is not None, it specifies the requested ratio of input to output spatial resolution. include_root_block: If True, include the initial convolution followed by max-pooling, if False excludes it. reuse: whether or not the network and its variables should be reused. To be able to reuse 'scope' must be given. scope: Optional variable_scope. Returns: net: A rank-4 tensor of size [batch, height_out, width_out, channels_out]. If global_pool is False, then height_out and width_out are reduced by a factor of output_stride compared to the respective height_in and width_in, else both height_out and width_out equal one. If num_classes is None, then net is the output of the last ResNet block, potentially after global average pooling. If num_classes is not None, net contains the pre-softmax activations. end_points: A dictionary from components of the network to the corresponding activation. Raises: ValueError: If the target output_stride is not valid. """ with variable_scope.variable_scope( scope, 'resnet_v1', [inputs], reuse=reuse) as sc: end_points_collection = sc.original_name_scope + '_end_points' with arg_scope( [layers.conv2d, bottleneck, resnet_utils.stack_blocks_dense], outputs_collections=end_points_collection): with arg_scope([layers.batch_norm], is_training=is_training): net = inputs if include_root_block: if output_stride is not None: if output_stride % 4 != 0: raise ValueError('The output_stride needs to be a multiple of 4.') output_stride /= 4 net = resnet_utils.conv2d_same(net, 64, 7, stride=2, scope='conv1') net = layers_lib.max_pool2d(net, [3, 3], stride=2, scope='pool1') net = resnet_utils.stack_blocks_dense(net, blocks, output_stride) if global_pool: # Global average pooling. net = math_ops.reduce_mean(net, [1, 2], name='pool5', keep_dims=True) if num_classes is not None: net = layers.conv2d( net, num_classes, [1, 1], activation_fn=None, normalizer_fn=None, scope='logits') # Convert end_points_collection into a dictionary of end_points. end_points = utils.convert_collection_to_dict(end_points_collection) if num_classes is not None: end_points['predictions'] = layers_lib.softmax( net, scope='predictions') return net, end_points