예제 #1
0
def Map(inputs, scope='map',is_training=True, reuse=False):
    
    vgg = vgg16.Vgg16()
    vgg.build(inputs)
    net = vgg.pool4

    with tf.variable_scope(scope,reuse = reuse) as scope:
        net = conv(net,[3, 3, 512, 512],[1, 1, 1, 1],is_training,reuse,padding='SAME',scope='conv1')
        
        net = conv(net,[3, 3, 512, 512],[1, 1, 1, 1],is_training,reuse,padding='SAME',scope='conv2')
        
        net = conv(net,[3, 3, 512, 512],[1, 1, 1, 1],is_training,reuse,padding='SAME',scope='conv3')

        net = tf.nn.max_pool(net,[1,2,2,1],[1,2,2,1],'SAME')
    return net
예제 #2
0
def guided_BP(image, label_id=-1):
    g = tf.get_default_graph()
    with g.gradient_override_map({'Relu': 'GuidedRelu'}):
        label_vector = tf.placeholder("float", [None, 1000])
        input_image = tf.placeholder("float", [None, 224, 224, 3])

        vgg = vgg16.Vgg16()
        with tf.name_scope("content_vgg"):
            vgg.build(input_image)

        cost = vgg.fc8 * label_vector

        # Guided backpropagtion back to input layer
        gb_grad = tf.gradients(cost, input_image)[0]

        init = tf.global_variables_initializer()

    # Run tensorflow
    with tf.Session(graph=g) as sess:
        sess.run(init)
        output = [0.0] * vgg.prob.get_shape().as_list()[
            1]  #one-hot embedding for desired class activations
        if label_id == -1:
            prob = sess.run(vgg.prob, feed_dict={input_image: image})

            vgg_utils.print_prob(prob[0], './synset.txt')

            #creating the output vector for the respective class
            index = np.argmax(prob)
            print "Predicted_class: ", index
            output[index] = 1.0

        else:
            output[label_id] = 1.0
        output = np.array(output)
        gb_grad_value = sess.run(gb_grad,
                                 feed_dict={
                                     input_image: image,
                                     label_vector: output.reshape((1, -1))
                                 })

    return gb_grad_value[0]
예제 #3
0
import tensorflow as tf
import numpy as np
from models import vgg16
from services import data
from config import config


img = data.load_image('test_images/2.jpg')
batch = img.reshape((1, 224, 224, 3))

with tf.device('/cpu:0'):
    with tf.Session() as sess:
        images = tf.placeholder('float', [1, 224, 224, 3])
        feed_dict = {images: batch}

        vgg = vgg16.Vgg16(config.ic_models['vgg16'])
        with tf.name_scope('content_vgg'):
            vgg.build(images)

        res = sess.run(vgg.prob, feed_dict=feed_dict)
        prob = res[0]
        pred = np.argsort(prob)[::-1]
        synset = [l.strip() for l in open('data/synset.txt').readlines()]

        prediction = synset[pred[0]]
        confidence = prob[pred[0]]
        print 'Prediction is ', prediction, '\nConfidence is ', confidence
예제 #4
0
def grad_CAM_plus(filename, label_id, output_filename):
    g = tf.get_default_graph()
    init = tf.global_variables_initializer()

    # Run tensorflow
    sess = tf.Session()

    #define your tensor placeholders for, labels and images
    label_vector = tf.placeholder("float", [None, 1000])
    input_image = tf.placeholder("float", [1, 224, 224, 3])
    label_index = tf.placeholder("int64", ())

    #load vgg16 model
    vgg = vgg16.Vgg16()
    with tf.name_scope("content_vgg"):
        vgg.build(input_image)
    #prob = tf.placeholder("float", [None, 1000])

    #get the output neuron corresponding to the class of interest (label_id)
    cost = vgg.fc8 * label_vector

    # Get last convolutional layer gradients for generating gradCAM++ visualization
    target_conv_layer = vgg.conv5_3
    target_conv_layer_grad = tf.gradients(cost, target_conv_layer)[0]

    #first_derivative
    first_derivative = tf.exp(cost)[0][label_index] * target_conv_layer_grad

    #second_derivative
    second_derivative = tf.exp(
        cost)[0][label_index] * target_conv_layer_grad * target_conv_layer_grad

    #triple_derivative
    triple_derivative = tf.exp(cost)[0][
        label_index] * target_conv_layer_grad * target_conv_layer_grad * target_conv_layer_grad

    sess.run(init)

    img1 = vgg_utils.load_image(filename)

    output = [0.0] * vgg.prob.get_shape().as_list()[
        1]  #one-hot embedding for desired class activations
    #creating the output vector for the respective class

    if label_id == -1:
        prob_val = sess.run(vgg.prob, feed_dict={input_image: [img1]})
        vgg_utils.print_prob(prob_val[0], './synset.txt')
        #creating the output vector for the respective class
        index = np.argmax(prob_val)
        orig_score = prob_val[0][index]
        print "Predicted_class: ", index
        output[index] = 1.0
        label_id = index
    else:
        output[label_id] = 1.0
    output = np.array(output)
    print label_id
    conv_output, conv_first_grad, conv_second_grad, conv_third_grad = sess.run(
        [
            target_conv_layer, first_derivative, second_derivative,
            triple_derivative
        ],
        feed_dict={
            input_image: [img1],
            label_index: label_id,
            label_vector: output.reshape((1, -1))
        })

    global_sum = np.sum(conv_output[0].reshape(
        (-1, conv_first_grad[0].shape[2])),
                        axis=0)

    alpha_num = conv_second_grad[0]
    alpha_denom = conv_second_grad[0] * 2.0 + conv_third_grad[
        0] * global_sum.reshape((1, 1, conv_first_grad[0].shape[2]))
    alpha_denom = np.where(alpha_denom != 0.0, alpha_denom,
                           np.ones(alpha_denom.shape))
    alphas = alpha_num / alpha_denom

    weights = np.maximum(conv_first_grad[0], 0.0)
    #normalizing the alphas

    alpha_normalization_constant = np.sum(np.sum(alphas, axis=0), axis=0)

    alphas /= alpha_normalization_constant.reshape(
        (1, 1, conv_first_grad[0].shape[2]))

    deep_linearization_weights = np.sum((weights * alphas).reshape(
        (-1, conv_first_grad[0].shape[2])),
                                        axis=0)
    #print deep_linearization_weights
    grad_CAM_map = np.sum(deep_linearization_weights * conv_output[0], axis=2)

    # Passing through ReLU
    cam = np.maximum(grad_CAM_map, 0)
    cam = cam / np.max(cam)  # scale 0 to 1.0

    cam = resize(cam, (224, 224))
    # Passing through ReLU
    cam = np.maximum(grad_CAM_map, 0)
    cam = cam / np.max(cam)  # scale 0 to 1.0
    cam = resize(cam, (224, 224))

    gb = guided_BP([img1], label_id)
    visualize(img1, cam, output_filename, gb)
    return cam
예제 #5
0
image_list, label_list = zip(*c)


config_dict["classes"] = dict(zip(classes, range(len(classes))))
n_classes = len(set(label_list))

with open(os.path.join(MODEL_PATH, "{}_config.yml".format(MODEL_NAME)), "w") as f:
    yaml.dump(config_dict, f, default_flow_style=False)

g = utils.input_image_generator(image_list, label_list, IMAGE_SIZE, RATIO, N_SLICES)

image = next(g)[0][0]

if MODEL_NAME == "vgg16":
    from models import vgg16
    model = vgg16.Vgg16(input_shape=image.shape, n_classes=n_classes)
elif MODEL_NAME == "resnet50":
    from models import resnet50
    model = resnet50.ResNet50(input_shape=image.shape, n_classes=n_classes)
elif MODEL_NAME == "fastnet":
    from models import simplenet
    model = simplenet.get_model(input_shape=image.shape, n_classes=n_classes)
else:
    from models import inceptionv3
    model = inceptionv3.IncveptionV3(input_shape=image.shape, n_classes=n_classes)

print("Compiling Model...")
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])