def inference(images, keep_conv, keep_hidden):
    '''
    アーキテクチャの定義、グラフのビルド
    '''
    # spatial transformer
    reshape = tf.reshape(images, [-1, 1600])
    with tf.variable_scope('spatial_transformer') as scope:
        weights_loc1 = _variable_with_weight_decay('weights_loc1',
                                                   shape=[1600, 20],
                                                   stddev=0.01,
                                                   wd=0.0)
        biases_loc1 = _variable_on_cpu('biases_loc1', [20],
                                       tf.constant_initializer(0.1))
        weights_loc2 = _variable_with_weight_decay('weights_loc2',
                                                   shape=[20, 6],
                                                   stddev=0.01,
                                                   wd=0.0)
        initial = np.array([[1., 0, 0], [0, 1., 0]
                            ])  # Use identity transformation as starting point
        initial = initial.astype('float32')
        initial = initial.flatten()
        biases_loc2 = tf.Variable(initial_value=initial, name='biases_loc2')

        # define the two layer localisation network
        h_fc_loc1 = tf.nn.tanh(tf.matmul(reshape, weights_loc1) + biases_loc1)
        # We can add dropout for regularizing and to reduce overfitting like so:
        h_fc_loc1_drop = tf.nn.dropout(h_fc_loc1, keep_conv)
        # Second layer
        h_fc_loc2 = tf.nn.tanh(
            tf.matmul(h_fc_loc1_drop, weights_loc2) + biases_loc2)

        # Transformer layer
        hidden_trans = spatial.transformer(images,
                                           h_fc_loc2,
                                           downsample_factor=1)
        _activation_summary(hidden_trans)

    # conv1
    with tf.variable_scope('conv1') as scope:
        kernel = _variable_with_weight_decay(
            'weights',
            shape=[3, 3, 1, 16],
            stddev=0.01,
            wd=0.0  # not use weight decay
        )
        conv = tf.nn.conv2d(hidden_trans, kernel, [1, 2, 2, 1], padding='SAME')
        conv1 = tf.nn.relu(conv, name=scope.name)
        _activation_summary(conv1)

    # conv2
    with tf.variable_scope('conv2') as scope:
        kernel = _variable_with_weight_decay(
            'weights',
            shape=[3, 3, 16, 16],
            stddev=0.01,
            wd=0.0  # not use weight decay
        )
        conv = tf.nn.conv2d(conv1, kernel, [1, 2, 2, 1], padding='SAME')
        biases = _variable_on_cpu('biases', [16], tf.constant_initializer(0.1))
        bias = tf.nn.bias_add(conv, biases)
        conv2 = tf.nn.relu(bias, name=scope.name)
        _activation_summary(conv2)

    # local3 fc
    with tf.variable_scope('local3') as scope:
        reshape = tf.reshape(conv2, [-1, 10 * 10 * 16])
        reshape = tf.nn.dropout(reshape, keep_conv)
        weights = _variable_with_weight_decay('weights',
                                              shape=[10 * 10 * 16, 1024],
                                              stddev=0.01,
                                              wd=0.04)
        biases = _variable_on_cpu('biases', [1024],
                                  tf.constant_initializer(0.1))
        local3 = tf.nn.relu(
            tf.add(tf.matmul(reshape, weights), biases, name=scope.name))
        _activation_summary(local3)

    dropout3 = tf.nn.dropout(local3, keep_hidden)

    # softmax
    with tf.variable_scope('softmax_linear') as scope:
        weights = _variable_with_weight_decay('weights', [1024, NUM_CLASSES],
                                              stddev=0.01,
                                              wd=0.0)
        biases = _variable_on_cpu('biases', [NUM_CLASSES],
                                  tf.constant_initializer(0.1))
        softmax_linear = tf.add(tf.matmul(dropout3, weights),
                                biases,
                                name=scope.name)
        _activation_summary(softmax_linear)

    return softmax_linear
def inference(images, keep_conv, keep_hidden):
    '''
    アーキテクチャの定義、グラフのビルド
    '''
    # spatial transformer
    reshape = tf.reshape(images, [-1, 224 * 224 * 3])
    with tf.variable_scope('spatial_transformer') as scope:
        # localisation net
        weights_loc1 = _variable_with_weight_decay('weights_loc1',
                                                   shape=[224 * 224 * 3, 20],
                                                   stddev=0.01,
                                                   wd=0.0)
        biases_loc1 = _variable_on_gpu('biases_loc1', [20],
                                       tf.constant_initializer(0.1))
        # output 6 dimentional vector to compute sampling grid
        weights_loc2 = _variable_with_weight_decay('weights_loc2',
                                                   shape=[20, 6],
                                                   stddev=0.01,
                                                   wd=0.0)
        # Use identity transformation as starting point
        initial = np.array([[1., 0, 0], [0, 1., 0]])
        initial = initial.astype('float32')
        initial = initial.flatten()
        biases_loc2 = tf.Variable(initial_value=initial, name='biases_loc2')

        # define the two layer localisation network
        h_fc_loc1 = tf.nn.tanh(tf.matmul(reshape, weights_loc1) + biases_loc1)
        # We can add dropout for regularizing and to reduce overfitting like so:
        h_fc_loc1_drop = tf.nn.dropout(h_fc_loc1, keep_conv)
        # Second layer
        h_fc_loc2 = tf.nn.tanh(
            tf.matmul(h_fc_loc1_drop, weights_loc2) + biases_loc2)

        # Transformer layer
        hidden_trans = spatial.transformer(images,
                                           h_fc_loc2,
                                           downsample_factor=1)
        _activation_summary(hidden_trans)

    print "=" * 100
    print images.get_shape()
    print "=" * 100
    # conv1
    with tf.variable_scope('conv1') as scope:
        kernel = _variable_with_weight_decay(
            'weights',
            shape=[11, 11, 3, 64],
            stddev=0.01,
            wd=0.0  # not use weight decay
        )
        conv = tf.nn.conv2d(hidden_trans, kernel, [1, 4, 4, 1], padding='SAME')
        biases = _variable_on_gpu('biases', [64], tf.constant_initializer(0.1))
        bias = tf.nn.bias_add(conv, biases)
        conv1 = tf.nn.relu(bias, name=scope.name)
        _activation_summary(conv1)

    print "=" * 100
    print conv1.get_shape()
    print "=" * 100

    # pool1 kernel 3x3, stride 2, output_map 27x27x64
    pool1 = tf.nn.max_pool(conv1,
                           ksize=[1, 3, 3, 1],
                           strides=[1, 2, 2, 1],
                           padding='VALID',
                           name='pool1')

    # norm1 kernel 5x5 stride 1, output_map 27x27x64
    norm1 = tf.nn.local_response_normalization(pool1,
                                               5,
                                               bias=1.0,
                                               alpha=2e-05,
                                               beta=0.75,
                                               name='norm1')

    print "=" * 100
    print norm1.get_shape()
    print "=" * 100

    # conv2 kernel 5x5, stride 1, output_map 27x27x192, af ReLu
    with tf.variable_scope('conv2') as scope:
        kernel = _variable_with_weight_decay(
            'weights',
            shape=[5, 5, 64, 192],
            stddev=0.01,
            wd=0.0  # not use weight decay
        )
        conv = tf.nn.conv2d(norm1, kernel, [1, 1, 1, 1], padding='SAME')
        biases = _variable_on_gpu('biases', [192],
                                  tf.constant_initializer(0.1))
        bias = tf.nn.bias_add(conv, biases)
        conv2 = tf.nn.relu(bias, name=scope.name)
        _activation_summary(conv2)

    print "=" * 100
    print conv2.get_shape()
    print "=" * 100

    # pool2 kernel 3x3, stride 2, output_map 13x13x256
    pool2 = tf.nn.max_pool(conv2,
                           ksize=[1, 3, 3, 1],
                           strides=[1, 2, 2, 1],
                           padding='VALID',
                           name='pool2')

    # norm2 kernel 5x5, stride 1, output_map 13x13x256
    norm2 = tf.nn.local_response_normalization(pool2,
                                               5,
                                               bias=1.0,
                                               alpha=2e-05,
                                               beta=0.75,
                                               name='norm2')

    # conv3 kernel 3x3, stride 1, output_map 13x13x384
    with tf.variable_scope('conv3') as scope:
        kernel = _variable_with_weight_decay('weights',
                                             shape=[3, 3, 192, 384],
                                             stddev=1e-4,
                                             wd=0.0)
        conv = tf.nn.conv2d(norm2, kernel, [1, 1, 1, 1], padding='SAME')
        biases = _variable_on_gpu('biases', [384],
                                  tf.constant_initializer(0.1))
        bias = tf.nn.bias_add(conv, biases)
        conv3 = tf.nn.relu(bias, name=scope.name)
        _activation_summary(conv3)

    # conv4 kernel 3x3, stride 1, output_map 13x13x384
    with tf.variable_scope('conv4') as scope:
        kernel = _variable_with_weight_decay('weights',
                                             shape=[3, 3, 384, 384],
                                             stddev=1e-4,
                                             wd=0.0)
        conv = tf.nn.conv2d(conv3, kernel, [1, 1, 1, 1], padding='SAME')
        biases = _variable_on_gpu('biases', [384],
                                  tf.constant_initializer(0.1))
        bias = tf.nn.bias_add(conv, biases)
        conv4 = tf.nn.relu(bias, name=scope.name)
        _activation_summary(conv4)

    # conv5 kernel 3x3, stride 1, output_map 13x13x256
    with tf.variable_scope('conv5') as scope:
        kernel = _variable_with_weight_decay('weights',
                                             shape=[3, 3, 384, 256],
                                             stddev=1e-4,
                                             wd=0.0)
        conv = tf.nn.conv2d(conv4, kernel, [1, 1, 1, 1], padding='SAME')
        biases = _variable_on_gpu('biases', [256],
                                  tf.constant_initializer(0.1))
        bias = tf.nn.bias_add(conv, biases)
        conv5 = tf.nn.relu(bias, name=scope.name)
        _activation_summary(conv5)

    print "=" * 100
    print conv5.get_shape()
    print "=" * 100

    # pool5 kernel 3x3, stride 2, output_map 6x6x256
    pool5 = tf.nn.max_pool(conv5,
                           ksize=[1, 3, 3, 1],
                           strides=[1, 2, 2, 1],
                           padding='VALID',
                           name='pool5')

    # fc6 output_map 1x1x4096
    with tf.variable_scope('fc6'):
        input_shape = pool5.get_shape()
        pool5_flat = tf.reshape(pool5, [-1, 6 * 6 * 256])
        weights = _variable_with_weight_decay('weights', [6 * 6 * 256, 4096],
                                              stddev=0.01,
                                              wd=0.04)
        biases = _variable_on_gpu('biases', 4096, tf.constant_initializer(0.1))
        fc6 = tf.nn.relu_layer(pool5_flat, weights, biases, name=scope.name)
        _activation_summary(fc6)

    # fc6_dropout dropout
    fc6_dropout = tf.nn.dropout(fc6, keep_hidden)

    # fc7 output_map 1x1x4096
    with tf.variable_scope('fc7'):
        input_shape = fc6_dropout.get_shape()
        inputs_fc7, dim = (fc6_dropout, int(input_shape[-1]))
        weights = _variable_with_weight_decay('weights', [4096, 4096],
                                              stddev=0.01,
                                              wd=0.04)
        biases = _variable_on_gpu('biases', 4096, tf.constant_initializer(0.1))
        fc7 = tf.nn.relu_layer(inputs_fc7, weights, biases, name=scope.name)
        _activation_summary(fc7)

    # fc7_dropout dropout
    fc7_dropout = tf.nn.dropout(fc7, keep_hidden)

    # fc8(softmax) output_map 1x1xNUM_CLASSES
    with tf.variable_scope('softmax_linear') as scope:
        weights = _variable_with_weight_decay('weights', [4096, NUM_CLASSES],
                                              stddev=0.01,
                                              wd=0.04)
        biases = _variable_on_gpu('biases', [NUM_CLASSES],
                                  tf.constant_initializer(0.1))
        softmax_linear = tf.nn.xw_plus_b(fc7_dropout,
                                         weights,
                                         biases,
                                         name=scope.name)
        _activation_summary(softmax_linear)

    return softmax_linear, hidden_trans
def inference(images, keep_conv, keep_hidden):
    '''
    アーキテクチャの定義、グラフのビルド
    '''
    # spatial transformer
    reshape = tf.reshape(images, [-1, 224*224*3])
    with tf.variable_scope('spatial_transformer') as scope:
        # localisation net
        weights_loc1 = _variable_with_weight_decay(
            'weights_loc1',
            shape=[224*224*3, 20],
            stddev=0.01,
            wd=0.0
        )
        biases_loc1 = _variable_on_gpu('biases_loc1', [20], tf.constant_initializer(0.1))
        # output 6 dimentional vector to compute sampling grid
        weights_loc2 = _variable_with_weight_decay(
            'weights_loc2',
            shape=[20, 6],
            stddev=0.01,
            wd=0.0
        )
        # Use identity transformation as starting point
        initial = np.array([[1., 0, 0], [0, 1., 0]])
        initial = initial.astype('float32')
        initial = initial.flatten()
        biases_loc2 = tf.Variable(initial_value=initial, name='biases_loc2')
        
        # define the two layer localisation network
        h_fc_loc1 = tf.nn.tanh(tf.matmul(reshape, weights_loc1) + biases_loc1)
        # We can add dropout for regularizing and to reduce overfitting like so:
        h_fc_loc1_drop = tf.nn.dropout(h_fc_loc1, keep_conv)
        # Second layer
        h_fc_loc2 = tf.nn.tanh(tf.matmul(h_fc_loc1_drop, weights_loc2) + biases_loc2)
       
        # Transformer layer 
        hidden_trans = spatial.transformer(images, h_fc_loc2, downsample_factor=1)
        _activation_summary(hidden_trans)

    print "="*100
    print images.get_shape()
    print "="*100
    # conv1
    with tf.variable_scope('conv1') as scope:
        kernel = _variable_with_weight_decay(
            'weights',
            shape=[11, 11, 3, 64],
            stddev=0.01,
            wd=0.0 # not use weight decay
        )
        conv = tf.nn.conv2d(hidden_trans, kernel, [1, 4, 4, 1], padding='SAME')
        biases = _variable_on_gpu('biases', [64], tf.constant_initializer(0.1))
        bias = tf.nn.bias_add(conv, biases)
        conv1 = tf.nn.relu(bias, name=scope.name)
        _activation_summary(conv1)

    print "="*100
    print conv1.get_shape()
    print "="*100
 

    # pool1 kernel 3x3, stride 2, output_map 27x27x64
    pool1 = tf.nn.max_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool1')

    # norm1 kernel 5x5 stride 1, output_map 27x27x64
    norm1 = tf.nn.local_response_normalization(pool1, 5, bias=1.0, alpha=2e-05, beta=0.75, name='norm1')

    print "="*100
    print norm1.get_shape()
    print "="*100
 

    # conv2 kernel 5x5, stride 1, output_map 27x27x192, af ReLu
    with tf.variable_scope('conv2') as scope:
        kernel = _variable_with_weight_decay(
            'weights',
            shape=[5, 5, 64, 192],
            stddev=0.01,
            wd=0.0 # not use weight decay
        )
        conv = tf.nn.conv2d(norm1, kernel, [1, 1, 1, 1], padding='SAME')
        biases = _variable_on_gpu('biases', [192], tf.constant_initializer(0.1))
        bias = tf.nn.bias_add(conv, biases)
        conv2 = tf.nn.relu(bias, name=scope.name)
        _activation_summary(conv2)

    print "="*100
    print conv2.get_shape()
    print "="*100
 

    # pool2 kernel 3x3, stride 2, output_map 13x13x256
    pool2 = tf.nn.max_pool(conv2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool2')
    
    # norm2 kernel 5x5, stride 1, output_map 13x13x256
    norm2 = tf.nn.local_response_normalization(pool2, 5, bias=1.0, alpha=2e-05, beta=0.75, name='norm2')

    # conv3 kernel 3x3, stride 1, output_map 13x13x384
    with tf.variable_scope('conv3') as scope:
        kernel = _variable_with_weight_decay(
            'weights',
            shape=[3, 3, 192, 384],
            stddev=1e-4,
            wd=0.0)
        conv = tf.nn.conv2d(norm2, kernel, [1, 1, 1, 1], padding='SAME')
        biases = _variable_on_gpu('biases', [384], tf.constant_initializer(0.1))
        bias = tf.nn.bias_add(conv, biases)
        conv3 = tf.nn.relu(bias, name=scope.name)
        _activation_summary(conv3)

    # conv4 kernel 3x3, stride 1, output_map 13x13x384
    with tf.variable_scope('conv4') as scope:
        kernel = _variable_with_weight_decay(
            'weights',
            shape=[3, 3, 384, 384],
            stddev=1e-4,
            wd=0.0)
        conv = tf.nn.conv2d(conv3, kernel, [1, 1, 1, 1], padding='SAME')
        biases = _variable_on_gpu('biases', [384], tf.constant_initializer(0.1))
        bias = tf.nn.bias_add(conv, biases)
        conv4 = tf.nn.relu(bias, name=scope.name)
        _activation_summary(conv4)

    # conv5 kernel 3x3, stride 1, output_map 13x13x256
    with tf.variable_scope('conv5') as scope:
        kernel = _variable_with_weight_decay(
            'weights',
            shape=[3, 3, 384, 256],
            stddev=1e-4,
            wd=0.0)
        conv = tf.nn.conv2d(conv4, kernel, [1, 1, 1, 1], padding='SAME')
        biases = _variable_on_gpu('biases', [256], tf.constant_initializer(0.1))
        bias = tf.nn.bias_add(conv, biases)
        conv5 = tf.nn.relu(bias, name=scope.name)
        _activation_summary(conv5)

    print "="*100
    print conv5.get_shape()
    print "="*100
 

    # pool5 kernel 3x3, stride 2, output_map 6x6x256
    pool5 = tf.nn.max_pool(conv5, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool5')
    
    # fc6 output_map 1x1x4096
    with tf.variable_scope('fc6'):
        input_shape = pool5.get_shape()
        pool5_flat = tf.reshape(pool5, [-1, 6*6*256])
        weights = _variable_with_weight_decay(
            'weights',
            [6*6*256, 4096],
            stddev=0.01,
            wd=0.04)
        biases = _variable_on_gpu('biases', 4096, tf.constant_initializer(0.1))
        fc6 = tf.nn.relu_layer(pool5_flat, weights, biases, name=scope.name)
        _activation_summary(fc6)

    # fc6_dropout dropout
    fc6_dropout = tf.nn.dropout(fc6, keep_hidden)

    # fc7 output_map 1x1x4096
    with tf.variable_scope('fc7'):
        input_shape = fc6_dropout.get_shape()
        inputs_fc7, dim = (fc6_dropout, int(input_shape[-1]))
        weights = _variable_with_weight_decay(
            'weights',
            [4096, 4096],
            stddev=0.01,
            wd=0.04)
        biases = _variable_on_gpu('biases', 4096, tf.constant_initializer(0.1))
        fc7 = tf.nn.relu_layer(inputs_fc7, weights, biases, name=scope.name)
        _activation_summary(fc7)

    # fc7_dropout dropout
    fc7_dropout = tf.nn.dropout(fc7, keep_hidden)

    # fc8(softmax) output_map 1x1xNUM_CLASSES
    with tf.variable_scope('softmax_linear') as scope:
        weights = _variable_with_weight_decay(
            'weights',
            [4096, NUM_CLASSES],
            stddev=0.01,
            wd=0.04)
        biases = _variable_on_gpu('biases', [NUM_CLASSES], tf.constant_initializer(0.1))
        softmax_linear = tf.nn.xw_plus_b(fc7_dropout, weights, biases, name=scope.name)
        _activation_summary(softmax_linear)
    
    return softmax_linear, hidden_trans
def inference(images, keep_conv, keep_hidden):
    '''
    アーキテクチャの定義、グラフのビルド
    '''
    # spatial transformer
    reshape = tf.reshape(images, [-1, 1600])
    with tf.variable_scope('spatial_transformer') as scope:
        weights_loc1 = _variable_with_weight_decay(
            'weights_loc1',
            shape=[1600, 20],
            stddev=0.01,
            wd=0.0
        )
        biases_loc1 = _variable_on_cpu('biases_loc1', [20], tf.constant_initializer(0.1))
        weights_loc2 = _variable_with_weight_decay(
            'weights_loc2',
            shape=[20, 6],
            stddev=0.01,
            wd=0.0
        )
        initial = np.array([[1., 0, 0], [0, 1., 0]]) # Use identity transformation as starting point
        initial = initial.astype('float32')
        initial = initial.flatten()
        biases_loc2 = tf.Variable(initial_value=initial, name='biases_loc2')
        
        # define the two layer localisation network
        h_fc_loc1 = tf.nn.tanh(tf.matmul(reshape, weights_loc1) + biases_loc1)
        # We can add dropout for regularizing and to reduce overfitting like so:
        h_fc_loc1_drop = tf.nn.dropout(h_fc_loc1, keep_conv)
        # Second layer
        h_fc_loc2 = tf.nn.tanh(tf.matmul(h_fc_loc1_drop, weights_loc2) + biases_loc2)
       
        # Transformer layer 
        hidden_trans = spatial.transformer(images, h_fc_loc2, downsample_factor=1)
        _activation_summary(hidden_trans)

    # conv1
    with tf.variable_scope('conv1') as scope:
        kernel = _variable_with_weight_decay(
            'weights',
            shape=[3, 3, 1, 16],
            stddev=0.01,
            wd=0.0 # not use weight decay
        )
        conv = tf.nn.conv2d(hidden_trans, kernel, [1, 2, 2, 1], padding='SAME')
        conv1 = tf.nn.relu(conv, name=scope.name)
        _activation_summary(conv1)

    # conv2
    with tf.variable_scope('conv2') as scope:
        kernel = _variable_with_weight_decay(
            'weights',
            shape=[3, 3, 16, 16],
            stddev=0.01,
            wd=0.0 # not use weight decay
        )
        conv = tf.nn.conv2d(conv1, kernel, [1, 2, 2, 1], padding='SAME')
        biases = _variable_on_cpu('biases', [16], tf.constant_initializer(0.1))
        bias = tf.nn.bias_add(conv, biases)
        conv2 = tf.nn.relu(bias, name=scope.name)
        _activation_summary(conv2)
    
    # local3 fc
    with tf.variable_scope('local3') as scope:
        reshape = tf.reshape(conv2, [-1, 10*10*16])
        reshape = tf.nn.dropout(reshape, keep_conv) 
        weights = _variable_with_weight_decay(
            'weights',
            shape=[10*10*16, 1024],
            stddev=0.01,
            wd=0.04
        )
        biases = _variable_on_cpu('biases', [1024], tf.constant_initializer(0.1))
        local3 = tf.nn.relu(tf.add(tf.matmul(reshape, weights), biases, name=scope.name))
        _activation_summary(local3)

    dropout3 = tf.nn.dropout(local3, keep_hidden)

    # softmax
    with tf.variable_scope('softmax_linear') as scope:
        weights = _variable_with_weight_decay(
            'weights',
            [1024, NUM_CLASSES],
            stddev=0.01,
            wd=0.0
        )
        biases = _variable_on_cpu('biases', [NUM_CLASSES], tf.constant_initializer(0.1))
        softmax_linear = tf.add(tf.matmul(dropout3, weights), biases, name=scope.name)
        _activation_summary(softmax_linear)

    return softmax_linear