我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用tensorflow.space_to_depth()。
def yolo_body(inputs, num_anchors, num_classes): """Create YOLO_V2 model CNN body in Keras.""" darknet = Model(inputs, darknet_body()(inputs)) conv20 = compose( DarknetConv2D_BN_Leaky(1024, (3, 3)), DarknetConv2D_BN_Leaky(1024, (3, 3)))(darknet.output) conv13 = darknet.layers[43].output conv21 = DarknetConv2D_BN_Leaky(64, (1, 1))(conv13) # TODO: Allow Keras Lambda to use func arguments for output_shape? conv21_reshaped = Lambda( space_to_depth_x2, output_shape=space_to_depth_x2_output_shape, name='space_to_depth')(conv21) x = concatenate([conv21_reshaped, conv20]) x = DarknetConv2D_BN_Leaky(1024, (3, 3))(x) x = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1))(x) return Model(inputs, x)
def yolo_body(inputs, num_anchors, num_classes): """Create YOLO_V2 model CNN body in Keras.""" darknet = Model(inputs, darknet_body()(inputs)) conv13 = darknet.get_layer('batchnormalization_13').output conv20 = compose( DarknetConv2D_BN_Leaky(1024, 3, 3), DarknetConv2D_BN_Leaky(1024, 3, 3))(darknet.output) # TODO: Allow Keras Lambda to use func arguments for output_shape? conv13_reshaped = Lambda( space_to_depth_x2, output_shape=space_to_depth_x2_output_shape, name='space_to_depth')(conv13) # Concat conv13 with conv20. x = merge([conv13_reshaped, conv20], mode='concat') x = DarknetConv2D_BN_Leaky(1024, 3, 3)(x) x = DarknetConv2D(num_anchors * (num_classes + 5), 1, 1)(x) return Model(inputs, x)
def yolo_v2(inputs, num_classes, is_training, num_anchors=5, scope='yolo_v2'): with tf.variable_scope(scope, 'yolo_v2', [inputs]) as sc: end_points_collection = sc.name + '_end_points' with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d], outputs_collections=end_points_collection): net = slim.conv2d(inputs, 32, scope='layer_0') net = slim.max_pool2d(net, scope='layer_1') net = slim.conv2d(net, 64, scope='layer_2') net = slim.max_pool2d(net, scope='layer_3') net = slim.conv2d(net, 128, scope='layer_4') net = slim.conv2d(net, 64, kernel_size=[1, 1], scope='layer_5') net = slim.conv2d(net, 128, scope='layer_6') net = slim.max_pool2d(net, scope='layer_7') net = slim.conv2d(net, 256, scope='layer_8') net = slim.conv2d(net, 128, kernel_size=[1, 1], scope='layer_9') net = slim.conv2d(net, 256, scope='layer_10') net = slim.max_pool2d(net, scope='layer_11') net = slim.conv2d(net, 512, scope='layer_12') net = slim.conv2d(net, 256, kernel_size=[1, 1], scope='layer_13') net = slim.conv2d(net, 512, scope='layer_14') net = slim.conv2d(net, 256, kernel_size=[1, 1], scope='layer_15') net = slim.conv2d(net, 512, scope='layer_16') path_1 = tf.space_to_depth(net, block_size=2, name='path_1') net = slim.max_pool2d(net, scope='layer_17') net = slim.conv2d(net, 1024, scope='layer_18') net = slim.conv2d(net, 512, kernel_size=[1, 1], scope='layer_19') net = slim.conv2d(net, 1024, scope='layer_20') net = slim.conv2d(net, 512, kernel_size=[1, 1], scope='layer_21') net = slim.conv2d(net, 1024, scope='layer_22') net = slim.conv2d(net, 1024, scope='layer_23') net = slim.conv2d(net, 1024, scope='layer_24') path_2 = net net = tf.concat([path_1, path_2], 3, name='concat2path') net = slim.conv2d(net, 1024, scope='layer_25') net = slim.conv2d(net, (num_classes + 5) * num_anchors, kernel_size=[1, 1], scope='layer_26') end_points = slim.utils.convert_collection_to_dict(end_points_collection) return net, end_points
def get_tf_predictions_reorganize(X, params): Hin = params["H"] Win = params["W"] Cin = params["C"] with tf.Graph().as_default(), tf.Session() as sess: x = tf.placeholder(tf.float32, shape=(1,Hin,Win,Cin)) if params["mode"] == 'SPACE_TO_DEPTH': y = tf.space_to_depth(x, params["block_size"]) else: y = tf.depth_to_space(x, params["block_size"]) return sess.run(y,feed_dict={x: X})
def space_to_depth_x2(x): """Thin wrapper for Tensorflow space_to_depth with block_size=2.""" # Import currently required to make Lambda work. # See: https://github.com/fchollet/keras/issues/5088#issuecomment-273851273 import tensorflow as tf return tf.space_to_depth(x, block_size=2)
def space_to_depth_x2_output_shape(input_shape): """Determine space_to_depth output shape for block_size=2. Note: For Lambda with TensorFlow backend, output shape may not be needed. """ return (input_shape[0], input_shape[1] // 2, input_shape[2] // 2, 4 * input_shape[3]) if input_shape[1] else (input_shape[0], None, None, 4 * input_shape[3])
def forward_and_jacobian(self, x, sum_log_det_jacobians, z): xs = int_shape(x) assert xs[1] % 2 == 0 and xs[2] % 2 == 0 y = tf.space_to_depth(x, 2) if z is not None: z = tf.space_to_depth(z, 2) return y,sum_log_det_jacobians, z
def call(self, inputs, **kwargs): return tf.space_to_depth(inputs, self.block_size)
def build_graph(input_tensor, train_flag, start_filter_size, n_anchors, n_classes): # preprocessing mean = tf.constant(np.load('rgb_mean.npy'), dtype=tf.float32) x = (input_tensor - mean) / 255 with tf.name_scope('Block_1'): x = get_block(x, start_filter_size, train_flag, maxpool=True) with tf.name_scope('Block_2'): x = get_block(x, start_filter_size * 2 ** 1, train_flag, maxpool=True) with tf.name_scope('bigBlock_1'): x = get_block(x, start_filter_size * 2 ** 2, train_flag) x = get_block(x, start_filter_size * 2 ** 1, train_flag, kernel=(1, 1)) x = get_block(x, start_filter_size * 2 ** 2, train_flag, maxpool=True) with tf.name_scope('bigBlock_2'): x = get_block(x, start_filter_size * 2 ** 3, train_flag) x = get_block(x, start_filter_size * 2 ** 2, train_flag, kernel=(1, 1)) x = get_block(x, start_filter_size * 2 ** 3, train_flag, maxpool=True) with tf.name_scope('doubleBigBlock_1'): x = get_block(x, start_filter_size * 2 ** 4, train_flag) x = get_block(x, start_filter_size * 2 ** 3, train_flag, kernel=(1, 1)) x = get_block(x, start_filter_size * 2 ** 4, train_flag) x = get_block(x, start_filter_size * 2 ** 3, train_flag, kernel=(1, 1)) x = get_block(x, start_filter_size * 2 ** 4, train_flag) with tf.name_scope('passThrough'): y = get_block(x, start_filter_size * 2 ** 1, train_flag) y = tf.space_to_depth(y, 2) with tf.name_scope('doubleBigBlock_2'): x = tf.layers.max_pooling2d(x, (2, 2), (2, 2), padding='same') x = get_block(x, start_filter_size * 2 ** 5, train_flag) x = get_block(x, start_filter_size * 2 ** 4, train_flag, kernel=(1, 1)) x = get_block(x, start_filter_size * 2 ** 5, train_flag) x = get_block(x, start_filter_size * 2 ** 4, train_flag, kernel=(1, 1)) x = get_block(x, start_filter_size * 2 ** 5, train_flag) with tf.name_scope('Block_3'): x = get_block(x, start_filter_size * 2 ** 5, train_flag) with tf.name_scope('Block_4'): x = get_block(x, start_filter_size * 2 ** 5, train_flag) x = tf.concat([x, y], axis=3) with tf.name_scope('Block_6'): x = get_block(x, start_filter_size * 2 ** 5, train_flag) with tf.name_scope('Prediction'): x = get_block(x, n_anchors * (n_classes + 5), train_flag, kernel=(1, 1)) return x
def read_and_batchify_image(image_path, shape, image_type="jpg"): """Return the original image as read from image_path and the image splitted as a batch tensor. Args: image_path: image path shape: batch shape, like: [no_patches_per_side**2, patch_side, patch_side, 3] image_type: image type Returns: original_image, patches where original image is a tensor in the format [widht, height 3] and patches is a tensor of processed images, ready to be classified, with size [batch_size, w, h, 3]""" original_image = read_image(image_path, 3, image_type) # extract values from shape patch_side = shape[1] no_patches_per_side = int(math.sqrt(shape[0])) resized_input_side = patch_side * no_patches_per_side resized_image = resize_bl(original_image, resized_input_side) resized_image = tf.expand_dims(resized_image, 0) patches = tf.space_to_depth(resized_image, patch_side) print(patches) patches = tf.squeeze(patches, [0]) #4,4,192*192*3 print(patches) patches = tf.reshape(patches, [no_patches_per_side**2, patch_side, patch_side, 3]) print(patches) patches_a = tf.split(0, no_patches_per_side**2, patches) print(patches_a) normalized_patches = [] for patch in patches_a: patch_as_input_image = zm_mp( tf.reshape(tf.squeeze(patch, [0]), [patch_side, patch_side, 3])) print(patch_as_input_image) normalized_patches.append(patch_as_input_image) # the last patch is not a "patch" but the whole image resized to patch_side² x 3 # to give a glance to the whole image, in parallel with the patch analysis normalized_patches.append(zm_mp(resize_bl(original_image, patch_side))) batch_of_patches = tf.pack(normalized_patches) return tf.image.convert_image_dtype(original_image, tf.uint8), batch_of_patches