def create_test_network_3(): """Misaligned network for test. The graph corresponds to the example from the first figure in go/cnn-rf-computation#arbitrary-computation-graphs Returns: g: Tensorflow graph object (Graph proto). """ g = ops.Graph() with g.as_default(): # An input test image with unknown spatial resolution. x = array_ops.placeholder(dtypes.float32, (None, None, None, 1), name='input_image') # Left branch. l1_pad = array_ops.pad(x, [[0, 0], [2, 1], [2, 1], [0, 0]]) l1 = slim.conv2d(l1_pad, 1, [5, 5], stride=2, scope='L1', padding='VALID') # Right branch. l2 = slim.conv2d(x, 1, [3, 3], stride=1, scope='L2', padding='VALID') l3 = slim.conv2d(l2, 1, [3, 3], stride=1, scope='L3', padding='VALID') # Addition. nn.relu(l1 + l3, name='output') return g
def create_test_network(): """Convolutional neural network for test. Returns: g: Tensorflow graph object (Graph proto). """ g = ops.Graph() with g.as_default(): # An input test image with unknown spatial resolution. x = array_ops.placeholder(dtypes.float32, (None, None, None, 1), name='input_image') # Left branch before first addition. l1 = slim.conv2d(x, 1, [1, 1], stride=4, scope='L1', padding='VALID') # Right branch before first addition. l2_pad = array_ops.pad(x, [[0, 0], [1, 0], [1, 0], [0, 0]], name='L2_pad') l2 = slim.conv2d(l2_pad, 1, [3, 3], stride=2, scope='L2', padding='VALID') l3 = slim.max_pool2d(l2, [3, 3], stride=2, scope='L3', padding='SAME') # First addition. l4 = nn.relu(l1 + l3, name='L4_relu') # Left branch after first addition. l5 = slim.conv2d(l4, 1, [1, 1], stride=2, scope='L5', padding='SAME') # Right branch after first addition. l6 = slim.conv2d(l4, 1, [3, 3], stride=2, scope='L6', padding='SAME') # Final addition. gen_math_ops.add(l5, l6, name='L7_add') return g
def create_test_network_9(): """Aligned network for test, including an intermediate addition. The graph is the same as create_test_network_8(), except that VALID padding is changed to SAME. Returns: g: Tensorflow graph object (Graph proto). """ g = ops.Graph() with g.as_default(): # An input test image with unknown spatial resolution. x = array_ops.placeholder(dtypes.float32, (None, None, None, 1), name='input_image') # Left branch before first addition. l1 = slim.conv2d(x, 1, [1, 1], stride=4, scope='L1', padding='SAME') # Right branch before first addition. l2 = slim.conv2d(x, 1, [3, 3], stride=2, scope='L2', padding='SAME') l3 = slim.conv2d(l2, 1, [1, 1], stride=2, scope='L3', padding='SAME') # First addition. l4 = nn.relu(l1 + l3) # Left branch after first addition. l5 = slim.conv2d(l4, 1, [1, 1], stride=2, scope='L5', padding='SAME') # Right branch after first addition. l6 = slim.conv2d(l4, 1, [3, 3], stride=2, scope='L6', padding='SAME') # Final addition. nn.relu(l5 + l6, name='output') return g
def create_test_network_7(): """Aligned network for test, with a control dependency. The graph is similar to create_test_network_1(), except that it includes an assert operation on the left branch. Returns: g: Tensorflow graph object (Graph proto). """ g = ops.Graph() with g.as_default(): # An 8x8 test image. x = array_ops.placeholder(dtypes.float32, (1, 8, 8, 1), name='input_image') # Left branch. l1 = slim.conv2d(x, 1, [1, 1], stride=4, scope='L1', padding='VALID') l1_shape = array_ops.shape(l1) assert_op = control_flow_ops.Assert(gen_math_ops.equal(l1_shape[1], 2), [l1_shape], summarize=4) # Right branch. l2_pad = array_ops.pad(x, [[0, 0], [1, 0], [1, 0], [0, 0]]) l2 = slim.conv2d(l2_pad, 1, [3, 3], stride=2, scope='L2', padding='VALID') l3 = slim.conv2d(l2, 1, [1, 1], stride=2, scope='L3', padding='VALID') # Addition. with ops.control_dependencies([assert_op]): nn.relu(l1 + l3, name='output') return g
def create_test_network_6(): """Aligned network with dropout for test. The graph is similar to create_test_network_1(), except that the right branch has dropout normalization. Returns: g: Tensorflow graph object (Graph proto). """ g = ops.Graph() with g.as_default(): # An input test image with unknown spatial resolution. x = array_ops.placeholder(dtypes.float32, (None, None, None, 1), name='input_image') # Left branch. l1 = slim.conv2d(x, 1, [1, 1], stride=4, scope='L1', padding='VALID') # Right branch. l2_pad = array_ops.pad(x, [[0, 0], [1, 0], [1, 0], [0, 0]]) l2 = slim.conv2d(l2_pad, 1, [3, 3], stride=2, scope='L2', padding='VALID') l3 = slim.conv2d(l2, 1, [1, 1], stride=2, scope='L3', padding='VALID') dropout = slim.dropout(l3) # Addition. nn.relu(l1 + dropout, name='output') return g
def create_test_network_4(): """Misaligned network for test. The graph corresponds to a variation from the example from the second figure in go/cnn-rf-computation#arbitrary-computation-graphs. Layer 2 uses 'SAME' padding, which makes its padding dependent on the input image dimensionality. In this case, the effective padding will be undetermined, and the utility is not able to check the network alignment. Returns: g: Tensorflow graph object (Graph proto). """ g = ops.Graph() with g.as_default(): # An input test image with unknown spatial resolution. x = array_ops.placeholder(dtypes.float32, (None, None, None, 1), name='input_image') # Left branch. l1 = slim.conv2d(x, 1, [1, 1], stride=4, scope='L1', padding='VALID') # Right branch. l2 = slim.conv2d(x, 1, [3, 3], stride=2, scope='L2', padding='SAME') l3 = slim.conv2d(l2, 1, [1, 1], stride=2, scope='L3', padding='VALID') # Addition. nn.relu(l1 + l3, name='output') return g
def create_test_network(placeholder_resolution, convert_variables_to_constants): """Convolutional neural network for test. Args: placeholder_resolution: Resolution to use for input placeholder. Used for height and width dimensions. convert_variables_to_constants: Whether to convert variables to constants. Returns: name_to_node: Dict keyed by node name, each entry containing the node's NodeDef. """ g = ops.Graph() sess = session.Session(graph=g) with g.as_default(): # An input test image with unknown spatial resolution. x = array_ops.placeholder( dtypes.float32, (1, placeholder_resolution, placeholder_resolution, 1), name='input_image') # Left branch before first addition. l1 = slim.conv2d(x, 1, [1, 1], stride=4, scope='L1', padding='VALID') # Right branch before first addition. l2_pad = array_ops.pad(x, [[0, 0], [1, 0], [1, 0], [0, 0]], name='L2_pad') l2 = slim.conv2d(l2_pad, 1, [3, 3], stride=2, scope='L2', padding='VALID') l3 = slim.max_pool2d(l2, [3, 3], stride=2, scope='L3', padding='SAME') # First addition. l4 = nn.relu(l1 + l3, name='L4_relu') # Left branch after first addition. l5 = slim.conv2d(l4, 1, [1, 1], stride=2, scope='L5', padding='SAME') # Right branch after first addition. l6 = slim.conv2d(l4, 1, [3, 3], stride=2, scope='L6', padding='SAME') # Final addition. gen_math_ops.add(l5, l6, name='L7_add') if convert_variables_to_constants: sess.run(variables.global_variables_initializer()) graph_def = graph_util.convert_variables_to_constants( sess, g.as_graph_def(), ['L7_add']) else: graph_def = g.as_graph_def() name_to_node = graph_compute_order.parse_graph_nodes(graph_def) return name_to_node
def create_test_network_2(): """Aligned network for test. The graph corresponds to a variation to the example from the second figure in go/cnn-rf-computation#arbitrary-computation-graphs. Layers 2 and 3 are changed to max-pooling operations. Since the functionality is the same as convolution, the network is aligned and the receptive field size is the same as from the network created using create_test_network_1(). Returns: g: Tensorflow graph object (Graph proto). """ g = ops.Graph() with g.as_default(): # An input test image with unknown spatial resolution. x = array_ops.placeholder(dtypes.float32, (None, None, None, 1), name='input_image') # Left branch. l1 = slim.conv2d(x, 1, [1, 1], stride=4, scope='L1', padding='VALID') # Right branch. l2_pad = array_ops.pad(x, [[0, 0], [1, 0], [1, 0], [0, 0]]) l2 = slim.max_pool2d(l2_pad, [3, 3], stride=2, scope='L2', padding='VALID') l3 = slim.max_pool2d(l2, [1, 1], stride=2, scope='L3', padding='VALID') # Addition. nn.relu(l1 + l3, name='output') return g
def create_test_network_8(): """Aligned network for test, including an intermediate addition. The graph is similar to create_test_network_1(), except that it includes a few more layers on top. The added layers compose two different branches whose receptive fields are different. This makes this test case more challenging; in particular, this test fails if a naive DFS-like algorithm is used for RF computation. Returns: g: Tensorflow graph object (Graph proto). """ g = ops.Graph() with g.as_default(): # An input test image with unknown spatial resolution. x = array_ops.placeholder(dtypes.float32, (None, None, None, 1), name='input_image') # Left branch before first addition. l1 = slim.conv2d(x, 1, [1, 1], stride=4, scope='L1', padding='VALID') # Right branch before first addition. l2_pad = array_ops.pad(x, [[0, 0], [1, 0], [1, 0], [0, 0]]) l2 = slim.conv2d(l2_pad, 1, [3, 3], stride=2, scope='L2', padding='VALID') l3 = slim.conv2d(l2, 1, [1, 1], stride=2, scope='L3', padding='VALID') # First addition. l4 = nn.relu(l1 + l3) # Left branch after first addition. l5 = slim.conv2d(l4, 1, [1, 1], stride=2, scope='L5', padding='VALID') # Right branch after first addition. l6_pad = array_ops.pad(l4, [[0, 0], [1, 0], [1, 0], [0, 0]]) l6 = slim.conv2d(l6_pad, 1, [3, 3], stride=2, scope='L6', padding='VALID') # Final addition. nn.relu(l5 + l6, name='output') return g
def create_test_network_5(): """Single-path network for testing non-square kernels. The graph is similar to the right branch of the graph from create_test_network_1(), except that the kernel sizes are changed to be non-square. Returns: g: Tensorflow graph object (Graph proto). """ g = ops.Graph() with g.as_default(): # An input test image with unknown spatial resolution. x = array_ops.placeholder(dtypes.float32, (None, None, None, 1), name='input_image') # Two convolutional layers, where the first one has non-square kernel. l1 = slim.conv2d(x, 1, [3, 5], stride=2, scope='L1', padding='VALID') l2 = slim.conv2d(l1, 1, [3, 1], stride=2, scope='L2', padding='VALID') # ReLU. nn.relu(l2, name='output') return g
def build_layer_fn(x, w_initializer, b_initializer): var_collection = { 'weights': ['SLIM_CONV2D_WEIGHTS'], 'biases': ['SLIM_CONV2D_BIASES'] } net = slim.conv2d( x, 3, 3, weights_initializer=w_initializer, biases_initializer=b_initializer, variables_collections=var_collection) weight_vars = ops.get_collection('SLIM_CONV2D_WEIGHTS') self.assertEquals(1, len(weight_vars)) bias_vars = ops.get_collection('SLIM_CONV2D_BIASES') self.assertEquals(1, len(bias_vars)) expected_normalized_vars = {'slim.conv2d.weights': weight_vars[0]} expected_not_normalized_vars = {'slim.conv2d.bias': bias_vars[0]} return net, expected_normalized_vars, expected_not_normalized_vars