jazzsaxmafia / Weakly_detector

Tensorflow implementation of "Learning Deep Features for Discriminative Localization"
361 stars 87 forks source link

Cannot obtain right activation map. #6

Closed chrisrn closed 7 years ago

chrisrn commented 7 years ago

I have trained inception v3 network and I have achieved high precision. I am cutting the last 3 inception modules of the network and the output of the last module is batch_size x 17 x 17 x 768. In the next steps I am using your technique to get activation map of each image but results are bad. The output classes are only 2 and I want to have right activation maps only for the one class. Can you tell me what am I missing? Here is the code:

` def conv2d(input_image, name): filter_shape = [3, 3, 768, 1024]
with tf.name_scope('MyConv'): w = tf.Variable(tf.random_uniform(filter_shape, minval=0.0, maxval=0.01), name='weights') b = tf.Variable(tf.zeros(filter_shape[-1]), name='biases')

    conv = tf.nn.conv2d(input_image, w, [1,1,1,1], padding='SAME')
    outputs = tf.nn.bias_add(conv, b)

return outputs

`

`def add_final_training_ops(net, ground_truth_input, class_count, final_tensor_name):

# Convolutional layer
convoluted = conv2d(net, 'NewConvLayer')

# GAP layer
gap = tf.reduce_mean(convoluted, [1, 2])

# Organizing the following ops as `final_training_ops` so they're easier
# to see in TensorBoard
layer_name = 'final_training_ops'
with tf.name_scope(layer_name):
    with tf.name_scope('weights'):
        layer_weights = tf.Variable(tf.random_uniform([1024, class_count], minval=0.0, maxval=0.01),
                                                    name='final_weights')

    with tf.name_scope('Wx_plus_b'):
        logits = tf.matmul(gap, layer_weights)

final_tensor = tf.nn.softmax(logits, name=final_tensor_name)

return final_tensor, layer_weights, convoluted`

`def heat_map(image, softmax_weights, labels): img_resize = tf.image.resize_bilinear(image, [299, 299]) with tf.variable_scope("GAP", reuse=True): label_w = tf.gather(tf.transpose(softmax_weights), labels) label_w = tf.reshape(label_w, [-1, 1024, 1]) # [batch_size, 1024, 1]

img_resize = tf.reshape(img_resize, [-1, 299299, 1024]) # [batch_size, 299299, 1024] activation = tf.batch_matmul(img_resize, label_w) activation = tf.reshape(activation, [-1, 299, 299]) return activation`

`final_tensor, softmax_weights, convoluted = add_final_training_ops(inception_7, labels, num_classes, 'final_result')

    prediction = tf.argmax(final_tensor, 1)
    activation_map = heat_map(convoluted, softmax_weights, labels)`