Пример #1
0
def expand(inputs, num_outputs, **kwargs):
    with tf.variable_scope('expand'):
        e1x1 = conv2d(inputs, num_outputs, filter_size=(
            1, 1), stride=(1, 1), name='1x1', **kwargs)
        e3x3 = conv2d(inputs, num_outputs,
                      filter_size=(3, 3), name='3x3', **kwargs)
    return tf.concat(3, [e1x1, e3x3])
Пример #2
0
def conv2d_same(inputs,
                num_outputs,
                kernel_size,
                stride,
                rate=1,
                scope=None,
                **common_args):
    if stride == 1:
        return conv2d(inputs,
                      num_outputs,
                      filter_size=(kernel_size, kernel_size),
                      stride=(stride, stride),
                      dilation_rate=rate,
                      padding='SAME',
                      name=scope,
                      **common_args)
    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 conv2d(inputs,
                      num_outputs,
                      filter_size=(kernel_size, kernel_size),
                      stride=(stride, stride),
                      dilation_rate=rate,
                      padding='VALID',
                      name=scope,
                      **common_args)
Пример #3
0
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 = tf.Graph()
    with g.as_default():
        # An input test image with unknown spatial resolution.
        x = tf.placeholder(tf.float32, (None, None, None, 1),
                           name='input_image')
        # Two convolutional layers, where the first one has non-square kernel.
        l1 = conv2d(x,
                    1,
                    False,
                    None,
                    filter_size=[3, 5],
                    stride=2,
                    name='L1',
                    padding='VALID')
        l2 = conv2d(l1,
                    1,
                    False,
                    None,
                    filter_size=[3, 1],
                    stride=2,
                    name='L2',
                    padding='VALID')
        # ReLU.
        tf.nn.relu(l2, name='output')
    return g
Пример #4
0
def model(is_training, resue, num_classes=5):
    common_args = common_layer_args(is_training, reuse)
    conv_args = make_args(
        untie_biases=True, batch_norm=batch_norm, **common_args)
    logit_args = make_args(activation=prelu, **common_args)

    inputs = input((None, crop_size[1], crop_size[0], 3), **common_args)

    net = conv2d(inputs, 32, filter_size=(3, 3), stride=(
        2, 2), name='conv1', **conv_params)
    net = conv2d(net, 64, name='conv2', **conv_params)
    net = bottleneck_v1(net, num_unit=128, name='block_v1_1', **conv_args)
    net = bottleneck_v1(net, num_unit=256, name='block_v1_2', **conv_args)
    net = bottleneck_v1(net, num_unit=728, name='block_v1_3', **conv_args)

    for i in range(8):
        prefix = 'block_v2_' + str(i + 5)
        net = bottleneck_v2(net, num_unit=728, name=prefix, **kwargs)

    net = bottleneck_v1(net, num_unit=1024, name='block_v1_4', **conv_args)
    net = separable_conv2d(net, 1536, filter_size=(3, 3), stride=(1, 1),
                           name='sconv1', **kwargs)
    net = separable_conv2d(net, 2048, filter_size=(3, 3), stride=(1, 1),
                           name='sconv2', **kwargs)
    with tf.variable_scope('Logits'):
        net = avg_pool_2d(net, net.get_shape()[1:3], name='AvgPool_1a')
        net = dropout(
            net, is_training, drop_p=1 - dropout_keep_prob, name='Dropout_1b')
        logits = fully_connected(net, num_classes,
                                 name='logits', **logit_args)
        predictions = softmax(logits, name='predictions', **common_args)
    return end_points(is_training)
Пример #5
0
def model(inputs, is_training, reuse, num_classes=2):
    common_args = common_layer_args(is_training, reuse)
    conv1 = conv2d(inputs, 32, name='conv1', activation=prelu, **common_args)
    conv1 = conv2d(conv1, 32, name='conv2', activation=prelu, **common_args)
    fc1 = fc(conv1, num_classes, name='logits', **common_args)
    prediction = softmax(fc1, name='prediction', **common_args)
    return end_points(is_training)
Пример #6
0
def model(is_training, reuse, dropout_keep_prob=0.5):
    common_args = common_layer_args(is_training, reuse)
    conv_args = make_args(batch_norm=True, activation=prelu, w_init=initz.he_normal(
        scale=1), untie_biases=False, **common_args)
    pool_args = make_args(padding='SAME', **common_args)
    inputs = input((None, crop_size[1], crop_size[0], 3), **common_args)
    with tf.variable_scope('squeezenet', values=[inputs]):
        net = conv2d(inputs, 96, stride=(2, 2), name='conv1', **conv_args)
        net = max_pool(net, name='maxpool1', **pool_args)
        net = fire_module(net, 16, 64, name='fire2', **conv_args)
        net = fire_module(net, 16, 64, name='fire3', **conv_args)
        net = fire_module(net, 32, 128, name='fire4', **conv_args)
        net = max_pool(net, name='maxpool4', **pool_args)
        net = fire_module(net, 32, 128, name='fire5', **conv_args)
        net = fire_module(net, 48, 192, name='fire6', **conv_args)
        net = fire_module(net, 48, 192, name='fire7', **conv_args)
        net = fire_module(net, 64, 256, name='fire8', **conv_args)
        net = max_pool(net,  name='maxpool8', **pool_args)
        net = fire_module(net, 64, 256, name='fire9', **conv_args)
        # Reversed avg and conv layers per 'Network in Network'
        net = dropout(net, drop_p=1 - dropout_keep_prob,
                      name='dropout6', **common_args)
        net = conv2d(net, 10, filter_size=(1, 1), name='conv10', **conv_args)
        logits = global_avg_pool(net, name='logits', **pool_args)
        predictions = softmax(logits, name='predictions', **common_args)
        return end_points(is_training)
Пример #7
0
def conv_block(inputs, num_filters, drop_p=None, block_name='block', **kwargs):
  inter_filters = num_filters * 4
  x = conv2d(inputs, inter_filters, filter_size=(1, 1), name=block_name + "_conv1_1", **kwargs)
  if drop_p:
    is_training = kwargs.get('is_training')
    x = dropout(x, is_training, drop_p=drop_p, name=block_name + "_conv_dropout")
  x = conv2d(x, num_filters, name=block_name + "_conv1_2", **kwargs)
  return x
def create_test_network():
    """Convolutional neural network for test.

    Returns:
      name_to_node: Dict keyed by node name, each entry containing the node's
        NodeDef.
    """
    g = tf.Graph()
    with g.as_default():
        # An input test image with unknown spatial resolution.
        x = tf.placeholder(dtypes.float32, (None, None, None, 1),
                           name='input_image')
        # Left branch before first addition.
        l1 = conv2d(x,
                    1,
                    False,
                    None,
                    filter_size=1,
                    stride=4,
                    name='L1',
                    padding='VALID')
        # Right branch before first addition.
        l2_pad = tf.pad(x, [[0, 0], [1, 0], [1, 0], [0, 0]], name='L2_pad')
        l2 = conv2d(l2_pad,
                    1,
                    False,
                    None,
                    filter_size=3,
                    stride=2,
                    name='L2',
                    padding='VALID')
        l3 = max_pool(l2, filter_size=3, stride=2, name='L3', padding='SAME')
        # First addition.
        l4 = tf.nn.relu(l1 + l3, name='L4_relu')
        # Left branch after first addition.
        l5 = conv2d(l4,
                    1,
                    False,
                    None,
                    filter_size=1,
                    stride=2,
                    name='L5',
                    padding='SAME')
        # Right branch after first addition.
        l6 = conv2d(l4,
                    1,
                    False,
                    None,
                    filter_size=3,
                    stride=2,
                    name='L6',
                    padding='SAME')
        # Final addition.
        tf.add(l5, l6, name='L7_add')

    name_to_node = receptive_field.parse_graph_nodes(g.as_graph_def())
    return g, name_to_node
def model(x, y, batch_size, is_training=True, reuse=None):
    with tf.variable_scope('model', reuse=reuse):
        x_tensor = tf.reshape(x, [-1, 28, 28, 1])

        fc1 = fc(x, 20, is_training, reuse, name='fc1', activation=None)
        fc1 = tf.tanh(fc1)
        fc1 = dropout(fc1, is_training, drop_p=0.5)
        fc2 = fc(fc1, 6, is_training, reuse, use_bias=False, name='fc2')
        initial = np.array([[1., 0, 0], [0, 1., 0]])
        initial = initial.astype('float32')
        initial = initial.flatten()
        fc2_b = tf.Variable(initial_value=initial, name='fc2/b')

        fc2 = tf.nn.bias_add(fc2, bias=fc2_b)
        fc2 = tf.tanh(fc2)
        h_trans = spatialtransformer(x_tensor, fc2, batch_size=batch_size)

        conv1 = conv2d(h_trans,
                       16,
                       is_training,
                       reuse,
                       activation=prelu,
                       name='conv1')
        conv2 = conv2d(conv1,
                       16,
                       is_training,
                       reuse,
                       stride=(2, 2),
                       activation=prelu,
                       name='conv2')
        fcmain = fc(conv2,
                    1024,
                    is_training,
                    reuse,
                    name='fc',
                    activation=prelu)
        fcmain = dropout(fcmain, is_training, drop_p=0.5)
        logits = fc(fcmain,
                    10,
                    is_training,
                    reuse,
                    name='logits',
                    activation=None)
        prediction = softmax(logits, 'prediction')

        loss = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y))
        opt = tf.train.AdamOptimizer()
        optimizer = opt.minimize(loss)
        # grads = opt.compute_gradients(loss, [fc2_b])

        correct_prediction = tf.equal(tf.argmax(prediction, 1),
                                      tf.argmax(y, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))

        return accuracy, loss, optimizer
Пример #10
0
def bottleneck(inputs,
               depth,
               depth_bottleneck,
               stride,
               rate=1,
               scope=None,
               **common_args):
    conv_args_common = make_args(use_bias=False,
                                 batch_norm=batch_norm_tf,
                                 batch_norm_args=batch_norm_params,
                                 **common_args)
    conv_args_relu = make_args(activation=relu, **conv_args_common)
    conv_args_none = make_args(activation=None, **conv_args_common)
    with tf.variable_scope(scope, 'bottleneck_v1', [inputs]) as sc:
        depth_in = utils.last_dimension(inputs.get_shape(), min_rank=4)
        if depth == depth_in:
            shortcut = subsample(inputs, stride, 'shortcut', **common_args)
        else:
            shortcut = conv2d(inputs,
                              depth,
                              filter_size=(1, 1),
                              stride=(stride, stride),
                              name='shortcut',
                              **conv_args_none)

        residual = conv2d(inputs,
                          depth_bottleneck,
                          filter_size=(1, 1),
                          stride=(1, 1),
                          name='conv1',
                          **conv_args_relu)
        residual = conv2d_same(residual,
                               depth_bottleneck,
                               3,
                               stride,
                               rate=rate,
                               scope='conv2',
                               **conv_args_relu)
        residual = conv2d(residual,
                          depth,
                          filter_size=(1, 1),
                          stride=(1, 1),
                          name='conv3',
                          **conv_args_none)

        # not in endpoints. does that matter?
        output = tf.nn.relu(shortcut + residual)

        return _collect_named_outputs(common_args['outputs_collections'],
                                      sc.name, output)
Пример #11
0
def bottleneck_v1(inputs, num_unit=128, name=None, **kwargs):
    is_training = kwargs.get('is_training')
    reuse = kwargs.get('reuse')
    with tf.variable_scope(name, 'bottleneck_v2', [inputs]):
        residual = conv2d(inputs,
                          num_unit,
                          filter_size=(1, 1),
                          stride=(2, 2),
                          name='conv_res',
                          **kwargs)
        net = tf.nn.relu(inputs)
        net = separable_conv2d(net,
                               num_unit,
                               filter_size=(3, 3),
                               stride=(1, 1),
                               name='sconv1',
                               **kwargs)
        net = separable_conv2d(net,
                               num_unit,
                               is_training,
                               reuse,
                               filter_size=(3, 3),
                               stride=(1, 1),
                               batch_norm=True,
                               activation=None,
                               name='sconv2')

        net = max_pool(net, name='maxpool')
        output = net + residual
        return output
Пример #12
0
def _attention(query,
               attn_states,
               is_training,
               reuse,
               attn_size,
               attn_vec_size,
               attn_length,
               trainable=True,
               name='attention'):
    with tf.variable_scope(name, reuse=reuse):
        v = tf.get_variable(name="V",
                            shape=[attn_vec_size],
                            trainable=trainable)
        attn_states_reshaped = tf.reshape(
            attn_states, shape=[-1, attn_length, 1, attn_size])
        attn_conv = conv2d(attn_states_reshaped,
                           attn_vec_size,
                           is_training,
                           reuse,
                           filter_size=(1, 1),
                           stride=(1, 1),
                           trainable=trainable,
                           use_bias=False)
        y = _linear(query, attn_vec_size, reuse)
        y = tf.reshape(y, [-1, 1, 1, attn_vec_size])
        s = tf.reduce_sum(v * tf.tanh(attn_conv + y), [2, 3])
        a = softmax(s)
        d = tf.reduce_sum(
            tf.reshape(a, [-1, attn_length, 1, 1]) * attn_states_reshaped,
            [1, 2])
        new_attns = tf.reshape(d, [-1, attn_size])
        new_attn_states = tf.slice(attn_states, [0, 1, 0], [-1, -1, -1])
        return new_attns, new_attn_states
Пример #13
0
def trans_block(inputs, num_filters, drop_p=None, block_name='trans', **kwargs):
  x = conv2d(inputs, num_filters, filter_size=(1, 1), name=block_name + "_conv1_1", **kwargs)
  if drop_p:
    is_training = kwargs.get('is_training')
    x = dropout(x, is_training, drop_p=drop_p, name=block_name + "_trans_dropout")
  x = rms_pool_2d(x, name=block_name + "rms_pool1", padding='SAME')
  return x
Пример #14
0
def squeeze(inputs, num_outputs, **kwargs):
    return conv2d(inputs,
                  num_outputs,
                  filter_size=(1, 1),
                  stride=(1, 1),
                  name='squeeze',
                  **kwargs)
Пример #15
0
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 = tf.Graph()
    with g.as_default():
        # An 8x8 test image.
        x = tf.placeholder(tf.float32, (1, 8, 8, 1), name='input_image')
        # Left branch.
        l1 = conv2d(x,
                    1,
                    False,
                    None,
                    filter_size=1,
                    stride=4,
                    name='L1',
                    padding='VALID')
        l1_shape = tf.shape(l1)
        assert_op = tf.Assert(tf.equal(l1_shape[1], 2), [l1_shape],
                              summarize=4)
        # Right branch.
        l2_pad = tf.pad(x, [[0, 0], [1, 0], [1, 0], [0, 0]])
        l2 = conv2d(l2_pad,
                    1,
                    False,
                    None,
                    filter_size=3,
                    stride=2,
                    name='L2',
                    padding='VALID')
        l3 = conv2d(l2,
                    1,
                    False,
                    None,
                    filter_size=1,
                    stride=2,
                    name='L3',
                    padding='VALID')
        # Addition.
        with tf.control_dependencies([assert_op]):
            tf.nn.relu(l1 + l3, name='output')
    return g
Пример #16
0
def model(inputs,
          is_training,
          reuse,
          input_size=image_size[0],
          drop_p_conv=0.0,
          drop_p_trans=0.0,
          n_filters=64,
          n_layers=[1, 2, 2, 3],
          num_classes=5, **kwargs):
  common_args = common_layer_args(is_training, reuse)
  conv_args = make_args(
      batch_norm=True,
      activation=prelu,
      w_init=initz.he_normal(scale=1),
      untie_biases=True,
      **common_args)
  fc_args = make_args(activation=prelu, w_init=initz.he_normal(scale=1), **common_args)
  logit_args = make_args(activation=None, w_init=initz.he_normal(scale=1), **common_args)
  pred_args = make_args(activation=prelu, w_init=initz.he_normal(scale=1), **common_args)
  pool_args = make_args(padding='SAME', filter_size=(2, 2), stride=(2, 2), **common_args)

  x = conv2d(inputs, 48, filter_size=(7, 7), name="conv1", **conv_args)
  x = max_pool(x, name='pool1', **pool_args)
  x = conv2d(x, 64, name="conv2_1", **conv_args)
  x = conv2d(x, 64, name="conv2_2", **conv_args)
  x = max_pool(x, name='pool2', **pool_args)

  # 112
  for block_idx in range(3):
    x, n_filters = dense_block(
        x,
        n_filters,
        num_layers=n_layers[block_idx],
        drop_p=drop_p_conv,
        block_name='dense_' + str(block_idx),
        **conv_args)
    x = trans_block(
        x, n_filters, drop_p=drop_p_trans, block_name='trans_' + str(block_idx), **conv_args)

  x, n_filters = dense_block(
      x, n_filters, num_layers=n_layers[3], drop_p=drop_p_trans, block_name='dense_3', **conv_args)
  # 8
  x = global_avg_pool(x, name='avgpool_1a_8x8')
  logits = fully_connected(x, n_output=num_classes, name="logits", **logit_args)

  predictions = softmax(logits, name='predictions', **common_args)
  return end_points(is_training)
Пример #17
0
def conv2d_same(inputs, num_outputs, kernel_size, stride, rate=1, name=None, **kwargs):
    """Strided 2-D convolution with 'SAME' padding.

    When stride > 1, then we do explicit zero-padding, followed by conv2d with
    'VALID' padding.

    Note that

       net = conv2d_same(inputs, num_outputs, 3, stride=stride)

    is equivalent to

       net = slim.conv2d(inputs, num_outputs, 3, stride=1, padding='SAME')
       net = subsample(net, factor=stride)

    whereas

       net = slim.conv2d(inputs, num_outputs, 3, stride=stride, padding='SAME')

    is different when the input's height or width is even, which is why we add the
    current function. For more details, see ResnetUtilsTest.testConv2DSameEven().

    Args:
      inputs: A 4-D tensor of size [batch, height_in, width_in, channels].
      num_outputs: An integer, the number of output filters.
      kernel_size: An int with the kernel_size of the filters.
      stride: An integer, the output stride.
      rate: An integer, rate for atrous convolution.
      name: name.

    Returns:
      output: A 4-D tensor of size [batch, height_out, width_out, channels] with
        the convolution output.
    """
    if stride == 1:
        return conv2d(inputs, num_outputs, filter_size=(kernel_size, kernel_size), stride=(1, 1), dilaton=rate,
                      padding='SAME', name=name, **kwargs)
    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 conv2d(inputs, num_outputs, kernel_size, stride=stride,
                      dilation=rate, padding='VALID', name=name, **kwargs)
Пример #18
0
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 = tf.Graph()
    with g.as_default():
        # An input test image with unknown spatial resolution.
        x = tf.placeholder(tf.float32, (None, None, None, 1),
                           name='input_image')
        # Left branch.
        l1 = conv2d(x,
                    1,
                    False,
                    None,
                    filter_size=1,
                    stride=4,
                    name='L1',
                    padding='VALID')
        # Right branch.
        l2 = conv2d(x,
                    1,
                    False,
                    None,
                    filter_size=3,
                    stride=2,
                    name='L2',
                    padding='SAME')
        l3 = conv2d(l2,
                    1,
                    False,
                    None,
                    filter_size=1,
                    stride=2,
                    name='L3',
                    padding='VALID')
        # Addition.
        tf.nn.relu(l1 + l3, name='output')
    return g
def model(is_training, reuse):
    common_args = common_layer_args(is_training, reuse)
    conv_args = make_args(batch_norm=None, activation=prelu, **common_args)
    fc_args = make_args(activation=prelu, **common_args)
    logit_args = make_args(activation=None, **common_args)

    x = input((None, crop_size[1], crop_size[0], 1), **common_args)
    x = conv2d(x, 32, name='conv1_1', **conv_args)
    x = conv2d(x, 32, name='conv1_2', **conv_args)
    x = max_pool(x, name='pool1', **common_args)
    x = dropout(x, drop_p=0.25, name='dropout1', **common_args)
    x = fully_connected(x, n_output=128, name='fc1', **fc_args)
    x = dropout(x, drop_p=0.5, name='dropout2', **common_args)
    logits = fully_connected(x, n_output=36, name="logits", **logit_args)
    predictions = softmax(logits, name='predictions', **common_args)

    return end_points(is_training)
Пример #20
0
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 = tf.Graph()
    with g.as_default():
        # An input test image with unknown spatial resolution.
        x = tf.placeholder(tf.float32, (None, None, None, 1),
                           name='input_image')
        # Left branch.
        l1 = conv2d(x,
                    1,
                    False,
                    None,
                    filter_size=1,
                    stride=4,
                    name='L1',
                    padding='VALID')
        # Right branch.
        l2_pad = tf.pad(x, [[0, 0], [1, 0], [1, 0], [0, 0]])
        l2 = conv2d(l2_pad,
                    1,
                    False,
                    None,
                    filter_size=3,
                    stride=2,
                    name='L2',
                    padding='VALID')
        l3 = conv2d(l2,
                    1,
                    False,
                    None,
                    filter_size=1,
                    stride=2,
                    name='L3',
                    padding='VALID')
        dropout = tf.nn.dropout(l3, 0.5, name='dropout')
        # Addition.
        tf.nn.relu(l1 + dropout, name='output')
    return g
Пример #21
0
def squeeze(inputs, num_outputs, **kwargs):
    return conv2d(inputs,
                  num_outputs,
                  filter_size=(1, 1),
                  stride=(1, 1),
                  name='squeeze',
                  batch_norm=True,
                  activation=prelu,
                  **kwargs)
Пример #22
0
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 = tf.Graph()
    with g.as_default():
        # An input test image with unknown spatial resolution.
        x = tf.placeholder(tf.float32, (None, None, None, 1),
                           name='input_image')
        # Left branch.
        l1_pad = tf.pad(x, [[0, 0], [2, 1], [2, 1], [0, 0]])
        l1 = conv2d(l1_pad,
                    1,
                    False,
                    None,
                    filter_size=5,
                    stride=2,
                    name='L1',
                    padding='VALID')
        # Right branch.
        l2 = conv2d(x,
                    1,
                    False,
                    None,
                    filter_size=3,
                    stride=1,
                    name='L2',
                    padding='VALID')
        l3 = conv2d(l2,
                    1,
                    False,
                    None,
                    filter_size=3,
                    stride=1,
                    name='L3',
                    padding='VALID')
        # Addition.
        tf.nn.relu(l1 + l3, name='output')
    return g
Пример #23
0
def model(inputs, is_training, reuse, num_classes=10, dropout_keep_prob=0.5):
    common_args = common_layer_args(is_training, reuse)
    conv_args = make_args(batch_norm=True,
                          activation=prelu,
                          w_init=initz.he_normal(scale=1),
                          untie_biases=False,
                          **common_args)
    conv_args_fm = make_args(w_init=initz.he_normal(scale=1),
                             untie_biases=False,
                             **common_args)
    pool_args = make_args(padding='SAME', **common_args)
    with tf.variable_scope('squeezenet', values=[inputs]):
        net = separable_conv2d(inputs,
                               256,
                               stride=(2, 2),
                               name='conv1',
                               **conv_args)
        # net = conv2d(inputs, 96, stride=(2, 2), name='conv1', **conv_args)
        net = max_pool(net, name='maxpool1', **pool_args)
        net = fire_module(net, 16, 64, name='fire2', **conv_args_fm)
        net = bottleneck_simple(net, 16, 64, name='fire3', **conv_args_fm)
        net = batch_norm(net,
                         activation_fn=tf.nn.relu,
                         name='fire3_bn',
                         is_training=is_training,
                         reuse=reuse)
        net = fire_module(net, 32, 128, name='fire4', **conv_args_fm)
        net = max_pool(net, name='maxpool4', **pool_args)
        net = bottleneck_simple(net, 32, 128, name='fire5', **conv_args_fm)
        net = batch_norm(net,
                         activation_fn=tf.nn.relu,
                         name='fire5_bn',
                         is_training=is_training,
                         reuse=reuse)
        net = fire_module(net, 48, 192, name='fire6', **conv_args_fm)
        net = bottleneck_simple(net, 48, 192, name='fire7', **conv_args_fm)
        net = batch_norm(net,
                         activation_fn=tf.nn.relu,
                         name='fire7_bn',
                         is_training=is_training,
                         reuse=reuse)
        net = fire_module(net, 64, 256, name='fire8', **conv_args_fm)
        net = max_pool(net, name='maxpool8', **pool_args)
        net = dropout(net,
                      drop_p=1 - dropout_keep_prob,
                      name='dropout6',
                      **common_args)
        net = conv2d(net,
                     num_classes,
                     filter_size=(1, 1),
                     name='conv10',
                     **conv_args_fm)
        logits = global_avg_pool(net, name='logits', **pool_args)
        predictions = softmax(logits, name='predictions', **common_args)
        return end_points(is_training)
Пример #24
0
def block17(net, scale=1.0, name='block17', **kwargs):
    is_training = kwargs.get('is_training')
    reuse = kwargs.get('reuse')
    activation = kwargs.get('activation')
    with tf.variable_scope(name, 'block17'):
        with tf.variable_scope('Branch_0'):
            tower_conv = conv2d(net,
                                192,
                                filter_size=(1, 1),
                                name='Conv2d_1x1',
                                **kwargs)
        with tf.variable_scope('Branch_1'):
            tower_conv1_0 = conv2d(net,
                                   128,
                                   filter_size=(1, 1),
                                   name='Conv2d_0a_1x1',
                                   **kwargs)
            tower_conv1_1 = conv2d(tower_conv1_0,
                                   160,
                                   filter_size=(1, 7),
                                   name='Conv2d_0b_1x7',
                                   **kwargs)
            tower_conv1_2 = conv2d(tower_conv1_1,
                                   192,
                                   filter_size=(7, 1),
                                   name='Conv2d_0c_7x1',
                                   **kwargs)
        mixed = tf.concat([tower_conv, tower_conv1_2], 3)
        up = conv2d(mixed,
                    net.get_shape()[3],
                    is_training,
                    reuse,
                    filter_size=(1, 1),
                    batch_norm=None,
                    activation=None,
                    name='Conv2d_1x1')
        net += scale * up
        if activation:
            net = activation(net, reuse)
    return net
Пример #25
0
def encoder(inputs, is_training, reuse, z_dim=512):
    common_args = common_layer_args(is_training, reuse)
    conv_args = make_args(batch_norm=True,
                          activation=lrelu,
                          w_init=initz.he_normal(scale=1),
                          untie_biases=False,
                          **common_args)
    conv_args_1st = make_args(batch_norm=None,
                              activation=lrelu,
                              w_init=initz.he_normal(scale=1),
                              untie_biases=False,
                              **common_args)
    logits_args = make_args(activation=None,
                            w_init=initz.he_normal(scale=1),
                            **common_args)
    pool_args = make_args(padding='SAME', **common_args)
    end_points = {}
    x = inputs
    end_points['inputs'] = x
    x = dropout(x, drop_p=0.2, name="input_dropout1", **common_args)
    x = conv2d(x,
               96,
               filter_size=(5, 5),
               stride=(2, 2),
               name="e_conv1_1",
               **conv_args_1st)
    end_points['e_conv1_1'] = x
    x = conv2d(x, 96, name="e_conv1_2", **conv_args)
    end_points['e_conv1_2'] = x
    x = conv2d(x, 96, stride=(2, 2), name="e_conv1_3", **conv_args)
    end_points['e_conv1_3'] = x
    x = dropout(x, drop_p=0.2, name="dropout1", **common_args)
    x = conv2d(x, 192, name="e_conv2_1", **conv_args)
    end_points['e_conv2_1'] = x
    x = conv2d(x, 192, name="e_conv2_2", **conv_args)
    end_points['e_conv2_2'] = x
    # x = conv2d(x, 192, stride=(2, 2), name="e_conv2_3", **conv_args)
    # end_points['e_conv2_3'] = x
    x = dropout(x, drop_p=0.2, name="dropout2", **common_args)
    # x = conv2d(x, 192, stride=(2, 2), name="e_conv3_1", **conv_args)
    # end_points['e_conv3_1'] = x
    x = conv2d(x, 192, filter_size=(1, 1), name="e_conv4_1", **conv_args)
    end_points['e_conv4_1'] = x
    x = conv2d(x, 192, filter_size=(1, 1), name="e_conv4_2", **conv_args)
    end_points['e_conv4_2'] = x
    x = global_avg_pool(x, name="global_pool")
    end_points['global_pool'] = x
    logits1 = fully_connected(x, z_dim, name="e_logits1", **logits_args)
    logits2 = fully_connected(x, z_dim, name="e_logits2", **logits_args)
    logits2 = tf.tanh(logits2, name='e_logits2_tanh')
    end_points['e_logits1'] = logits1
    end_points['e_logits2'] = logits2
    return end_points
Пример #26
0
def discriminator(inputs, is_training, reuse, num_classes=1):
    common_args = common_layer_args(is_training, reuse)
    conv_args = make_args(batch_norm=True,
                          activation=lrelu,
                          w_init=initz.he_normal(scale=1),
                          untie_biases=False,
                          **common_args)
    conv_args_1st = make_args(batch_norm=None,
                              activation=lrelu,
                              w_init=initz.he_normal(scale=1),
                              untie_biases=False,
                              **common_args)
    logits_args = make_args(activation=None,
                            w_init=initz.he_normal(scale=1),
                            **common_args)
    pool_args = make_args(padding='SAME', **common_args)
    end_points = {}
    x = inputs
    end_points['inputs'] = x
    x = dropout(x, drop_p=0.2, name="input_dropout1", **common_args)
    x = conv2d(x,
               96,
               filter_size=(5, 5),
               stride=(2, 2),
               name="d_conv1_1",
               **conv_args_1st)
    end_points['d_conv1_1'] = x
    x = conv2d(x, 96, name="d_conv1_2", **conv_args)
    end_points['d_conv1_2'] = x
    x = conv2d(x, 96, stride=(2, 2), name="d_conv1_3", **conv_args)
    end_points['d_conv1_3'] = x
    x = dropout(x, drop_p=0.2, name="dropout1", **common_args)
    x = conv2d(x, 192, name="d_conv2_1", **conv_args)
    end_points['d_conv2_1'] = x
    x = conv2d(x, 192, name="d_conv2_2", **conv_args)
    end_points['d_conv2_2'] = x
    # x = conv2d(x, 192, stride=(2, 2), name="d_conv2_3", **conv_args)
    # end_points['d_conv2_3'] = x
    x = dropout(x, drop_p=0.2, name="dropout2", **common_args)
    # x = conv2d(x, 192, stride=(2, 2), name="d_conv3_1", **conv_args)
    # end_points['d_conv3_1'] = x
    x = conv2d(x, 192, filter_size=(1, 1), name="d_conv4_1", **conv_args)
    end_points['d_conv4_1'] = x
    x = conv2d(x, 192, filter_size=(1, 1), name="d_conv4_2", **conv_args)
    end_points['d_conv4_2'] = x
    x = global_avg_pool(x, name="global_pool")
    end_points['global_pool'] = x
    logits = fully_connected(x, num_classes, name="d_logits", **logits_args)
    end_points['logits'] = logits
    end_points['predictions'] = softmax(logits,
                                        name='predictions',
                                        **common_args)
    return end_points
Пример #27
0
def block35(net, scale=0.17, name='block35', **kwargs):
    is_training = kwargs.get('is_training')
    reuse = kwargs.get('reuse')
    activation = kwargs.get('activation')
    with tf.variable_scope(name, 'block35'):
        with tf.variable_scope('Branch_0'):
            tower_conv = conv2d(net,
                                32,
                                filter_size=(1, 1),
                                name='Conv2d_1x1',
                                **kwargs)
        with tf.variable_scope('Branch_1'):
            tower_conv1_0 = conv2d(net,
                                   32,
                                   filter_size=(1, 1),
                                   name='Conv2d_0a_1x1',
                                   **kwargs)
            tower_conv1_1 = conv2d(tower_conv1_0,
                                   32,
                                   name='Conv2d_0b_3x3',
                                   **kwargs)
        with tf.variable_scope('Branch_2'):
            tower_conv2_0 = conv2d(net,
                                   32,
                                   filter_size=(1, 1),
                                   name='Conv2d_0a_1x1',
                                   **kwargs)
            tower_conv2_1 = conv2d(tower_conv2_0,
                                   48,
                                   name='Conv2d_0b_3x3',
                                   **kwargs)
            tower_conv2_2 = conv2d(tower_conv2_1,
                                   64,
                                   name='Conv2d_0c_3x3',
                                   **kwargs)
        mixed = tf.concat([tower_conv, tower_conv1_1, tower_conv2_2], 3)
        up = conv2d(mixed,
                    net.get_shape()[3],
                    is_training,
                    reuse,
                    filter_size=(1, 1),
                    batch_norm=None,
                    activation=None,
                    name='Conv2d_1x1')
        net += scale * up
        if activation:
            net = activation(net, reuse)
    return net
Пример #28
0
def resnet_v1(inputs,
              blocks,
              num_classes=None,
              global_pool=True,
              output_stride=None,
              include_root_block=True,
              scope=None,
              **common_args):
    conv_args = make_args(use_bias=False,
                          activation=relu,
                          batch_norm=batch_norm_tf,
                          batch_norm_args=batch_norm_params,
                          **common_args)
    with tf.variable_scope(scope,
                           'resnet_v1', [inputs],
                           reuse=common_args['reuse']) as sc:
        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 = conv2d_same(net, 64, 7, stride=2, scope='conv1', **conv_args)
            net = max_pool(net,
                           filter_size=(3, 3),
                           stride=(2, 2),
                           padding='SAME',
                           name='pool1')
        net = stack_blocks_dense(net, blocks, output_stride, **common_args)
        if global_pool:
            # Global average pooling.
            net = tf.reduce_mean(net, [1, 2], name='pool5', keep_dims=True)
        if num_classes is not None:
            net = conv2d(net,
                         num_classes,
                         filter_size=(1, 1),
                         activation=None,
                         name='logits',
                         **common_args)
            predictions = softmax(net, name='predictions', **common_args)
        return end_points(common_args['is_training'])
Пример #29
0
def model(x, is_training, reuse, num_classes=10, **config):
  common_args = common_layer_args(is_training, reuse)
  logit_args = make_args(activation=None, **common_args)

  if config['max_conv_layers']>0:
    for i in range(1, config['n_conv_layers']+1):
      activation, size, maxpool = layer_config(config, i, layer_type='conv')
      conv_args = make_args(batch_norm=bool(config['batch_norm']), activation=prelu, **common_args)
      x = conv2d(x, size, name='conv{}'.format(i), **conv_args)
      if maxpool:
        x = max_pool(x, name='pool{}'.format(i), **common_args)

  if config['max_fc_layers']>0:
    for i in range(1, config['n_fc_layers']+1):
      activation, size, _dropout = layer_config(config, i, layer_type='fc')
      fc_args = make_args(activation=prelu, **common_args)
      x = fully_connected(x, n_output=size, name='fc{}'.format(i), **fc_args)
      x = dropout(x, drop_p=np.round(_dropout, 2), name='dropout{}'.format(i), **common_args)

  logits = fully_connected(x, n_output=num_classes, name="logits", **logit_args)
  predictions = softmax(logits, name='predictions', **common_args)
  return end_points(is_training)
Пример #30
0
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 = tf.Graph()
    with g.as_default():
        # An input test image with unknown spatial resolution.
        x = tf.placeholder(tf.float32, (None, None, None, 1),
                           name='input_image')
        # Left branch.
        l1 = conv2d(x,
                    1,
                    False,
                    None,
                    filter_size=1,
                    stride=4,
                    name='L1',
                    padding='VALID')
        # Right branch.
        l2_pad = tf.pad(x, [[0, 0], [1, 0], [1, 0], [0, 0]])
        l2 = max_pool(l2_pad,
                      filter_size=3,
                      stride=2,
                      name='L2',
                      padding='VALID')
        l3 = max_pool(l2, filter_size=1, stride=2, name='L3', padding='VALID')
        # Addition.
        tf.nn.relu(l1 + l3, name='output')
    return g