Esempio n. 1
0
def predict():
    # initialize the data dictionary that will be returned from the
    # view
    data = {"success": False}
    # ensure an image was properly uploaded to our endpoint
    if flask.request.method == "POST":
        if flask.request.files.get("image"):
            # read the image in PIL format
            image = flask.request.files["image"].read()
            image = Image.open(io.BytesIO(image))

            # preprocess the image and prepare it for classification
            image = prepare_image(image, target=(224, 224))

            # classify the input image and then initialize the list
            # of predictions to return to the client
            preds = model.predict(image)
            results = decode_predictions(preds)
            data["predictions"] = []

            # loop over the results and add them to the list of
            # returned predictions
            for (imagenetID, label, prob) in results[0]:
                r = {"label": label, "probability": float(prob)}
                data["predictions"].append(r)

            # indicate that the request was a success
            data["success"] = True

    # return the data dictionary as a JSON response
    return flask.jsonify(data)
Esempio n. 2
0
def returnPredictionsandGenerateHeatMap(image, inputModel, numResults):
    preprocessed_input = load_image(image)
    predictions = inputModel.predict(preprocessed_input)

    predicted_class = np.argmax(predictions)

    lastConvLayers = {"vgg19": 'block5_conv4', "vgg16": "block5_conv3"}

    #remove the heatmap files
    for entry in os.listdir("planck/static"):
        fullPath = os.path.join("planck/static", entry)
        if os.path.isfile(fullPath):
            os.remove(fullPath)

    #heatmap only works for vgg nerual net architectures
    if ((inputModel.name == "vgg19") or (inputModel.name == "vgg16")):
        cam, heatmap = grad_cam(inputModel, preprocessed_input,
                                predicted_class,
                                lastConvLayers[inputModel.name])
        cv2.imwrite("planck/static/outputheatmap.jpg", cam)

        register_gradient()
        guided_model = modify_backprop(inputModel, 'GuidedBackProp')
        saliency_fn = compile_saliency_function(guided_model)
        saliency = saliency_fn([preprocessed_input, 0])
        gradcam = saliency[0] * heatmap[..., np.newaxis]
        cv2.imwrite("planck/static/guidedoutput.jpg", deprocess_image(gradcam))

    return decode_predictions(predictions, top=numResults)[0]
Esempio n. 3
0
def predict(request):
    if request.method == 'POST' and request.FILES['image']:
        myfile = request.FILES['image']
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)
        # img=Document(document=myfile)
        # img.save()
        uploaded_file_url = fs.url(filename)
        # print(uploaded_file_url)
        
        import keras.backend.tensorflow_backend as tb
        tb._SYMBOLIC_SCOPE.value = True
        
        img='.'+uploaded_file_url
        # test_image=[]
        img=image.load_img(img,target_size=(224,224,3))
        img=image.img_to_array(img)
        img=img/255
        img=img.reshape(1,224,224,3)
        # test_image.append(img)
        # img=np.array(img)
        # img=preprocess_input(img)
        # plt.imshow(img.reshape(224,224,3))
        
        pred=model.predict(img)
        predictions=decode_predictions(pred,top=3)
        predictions=np.array(predictions)[0][0:][0:,1:]
        ans=1
        # print(predictions)
        # return render(request, 'home.html', {
        #     'uploaded_file_url': uploaded_file_url
        # })
        context={'predictions':predictions,'image':uploaded_file_url,'ans':ans}
    return render(request,'home.html',context)
Esempio n. 4
0
def make_prediction(filepath):
    def crop_with_ratio(img, size=(224, 224)):
        # function that takes in input image and crops and resizes to maintain aspect ratio
        height, width, chans = image.shape
        if height == width:
            return scipy.misc.imresize(image, size)
        ratio = height / width
        if ratio < 1.0:  # width is greater than height
            diff = width - height
            border_size = diff // 2
            return scipy.misc.imresize(image[:, border_size:-border_size, :],
                                       size)
        else:
            diff = height - width
            border_size = diff // 2
            return scipy.misc.imresize(image[border_size:-border_size, :, :],
                                       size)

    image = imread(filepath)
    image = crop_with_ratio(image)
    image = np.expand_dims(image, 0).astype('float32')
    image = vgg19.preprocess_input(image)

    preds = model.predict(image)
    decodes = vgg19.decode_predictions(preds)
    for p in decodes:
        print(p, '--------')
def predict(filename):
    img = Image.open(filename)
    img = img.resize((224, 224), Image.ANTIALIAS)
    input = img_to_array(img)
    input = np.expand_dims(input, axis=0)
    input = preprocess_input(input)
    output = decode_predictions(model.predict(input), top=3)
    print(output)
def predict(image_path):
    """Use VGG19 to label image"""
    img = image.load_img(image_path, target_size=image_size)
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    predictions = model.predict(x)
    plt.imshow(img)
    return (decode_predictions(predictions, top=1))
Esempio n. 7
0
def predict(image):
    model = VGG19()

    pred = model.predict(image)
    decoded_predictions = decode_predictions(pred, top=10)
    response = 'VGG19 predictions:   ' + str(decoded_predictions[0][0:5])
    print(response)
    np.argmax(pred[0])
    return response
Esempio n. 8
0
def decodepred(network_name, preds):
    if (network_name == "ResNet50"):
        preds = resnet50.decode_predictions(preds, top=3)[0]
    elif (network_name == "MobileNetV2"):
        preds = mobilenetv2.decode_predictions(preds, top=3)[0]
    elif (network_name == "VGG19"):
        preds = vgg19.decode_predictions(preds, top=3)[0]
    elif (network_name == "SqueezeNet"):
        preds = imagenet_utils.decode_predictions(preds, top=3)[0]
    return x
Esempio n. 9
0
def predict(file_list):
    """
    do prediction of files in file_list
    :param file_list: files
    :return: prediction result
    """
    input_data = get_input_data(file_list)
    vgg19_model = VGG19()
    predict_rates = vgg19_model.predict(input_data)
    result = decode_predictions(predict_rates,1)
    print(result)
Esempio n. 10
0
def run(image_bytes):
    image_bytes = json.loads(image_bytes)['data']  #str to string
    image_bytes = image_bytes.encode('utf-8')
    image = Image.frombytes('RGBA', (224, 224), image_bytes, 'raw')
    image = image.resize((224, 224), Image.ANTIALIAS)
    image = img_to_array(image)
    image = image[:, :, 0:3]
    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
    image = preprocess_input(image)
    yhat = model.predict(image)
    label = decode_predictions(yhat)
    label = label[0][0][1]
    return (label)
Esempio n. 11
0
 def image_id(image):
     # load model
     model = VGG19()
     # load/resize/preprocess test image
     img = load_img(image, target_size=(224, 224))
     img = img_to_array(img)
     img = img.reshape((1, img.shape[0], img.shape[1], img.shape[2]))
     img = preprocess_input(img)
     # predict probabilities for each class
     yhat = model.predict(img)
     label = decode_predictions(yhat)
     # find the class with the highest probability and print out likelihood
     label = label[0][0]
     print(label[1] + ', ' + '%.2f' % (label[2] * 100) + '% likelihood')
Esempio n. 12
0
def CAM(imgPath):
    img_path = imgPath  # '../input/flowers-recognition/flowers/sunflower/151898652_b5f1c70b98_n.jpg'
    import os.path
    # print("os.path.exists ",os.path.exists(imgPath))
    org_img = cv2.imread(img_path)

    img = image.load_img(img_path, target_size=(224, 224))

    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)

    x = preprocess_input(x)

    model = VGG19(weights='imagenet')
    preds = model.predict(x)
    cam_predictions = pd.DataFrame(decode_predictions(preds, top=3)[0], columns=['col1', 'category', 'probability']).iloc[:, 1:]

    argmax = np.argmax(preds[0])
    output = model.output[:, argmax]

    last_conv_layer = model.get_layer('block5_conv4')
    grads = K.gradients(output, last_conv_layer.output)[0]
    pooled_grads = K.mean(grads, axis=(0, 1, 2))
    iterate = K.function([model.input], [pooled_grads, last_conv_layer.output[0]])
    pooled_grads_value, conv_layer_output_value = iterate([x])
    for i in range(512):
        conv_layer_output_value[:, :, i] *= pooled_grads_value[i]

    heatmap = np.mean(conv_layer_output_value, axis=-1)
    heatmap = np.maximum(heatmap, 0)
    heatmap /= np.max(heatmap)

    img = cv2.imread(img_path)

    heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0]))
    heatmap = np.uint8(255 * heatmap)
    heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
    hif = .8
    superimposed_img = heatmap * hif + img

    import matplotlib.image as mpimg

    output = 'media/cam_output.jpeg'
    cv2.imwrite(output, superimposed_img)
    img = mpimg.imread(output)

    # plt.imshow(img)

    return output
Esempio n. 13
0
def predict_class(image):
    '''
    Predict and render the class of a given image
    '''
    # predict the probability across all output classes
    yhat = model.predict(image)
    # convert the probabilities to class labels
    label = decode_predictions(yhat)
    # retrieve the most likely result, e.g. highest probability
    label = label[0][0]
    # return the classification
    prediction = label[1]
    percentage = '%.2f%%' % (label[2] * 100)

    return prediction, percentage
Esempio n. 14
0
def check(model, quanti_layer, x):
    j = 0
    for i in quanti_layer:
        checkfile = open('check_specific.txt', 'a')
        check_model = swap_layer(model, j, i)
        print("{}, {}complete".format(j, i))
        yhat = check_model.predict(x)
        # convert the probabilities to class labels
        label = decode_predictions(yhat)
        # print the classification
        checkfile.write('0~%s layer, %s bit : %s (%.2f%%) %s (%.2f%%) %s (%.2f%%)\n' 
                % (j, i, label[0][0][1], label[0][0][2]*100, label[0][1][1], label[0][1][2]*100, label[0][2][1], label[0][2][2]*100))
        # model.set_weights(weights)
        checkfile.close()
        j += 1
Esempio n. 15
0
def predict(image_path):

    image_size = (224, 224)
    model = model()

    """Use VGG19 to label image"""
    img = image.load_img(image_path, target_size=image_size)
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    prediction = model.predict(x)
    predictions = decode_predictions(prediction, top=1)[0]
    predicted = predictions[0][1]
    print('Predicted:', predicted)
    predicted_clean = predicted.replace('_',' ')
    return predicted_clean
def cam(img_path):
    from keras.applications.vgg19 import VGG19
    import matplotlib.image as mpimg
    from keras import backend as K
    import matplotlib.pyplot as plt
    get_ipython().run_line_magic('matplotlib', 'inline')
    K.clear_session()
    
    model = VGG19(weights='imagenet')
    img=mpimg.imread(img_path)
    plt.imshow(img)
    from keras.preprocessing import image
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    from keras.applications.vgg19 import preprocess_input
    x = preprocess_input(x)
    preds = model.predict(x)
    predictions = pd.DataFrame(decode_predictions(preds, top=3)[0],columns=['col1','category','probability']).iloc[:,1:]
    argmax = np.argmax(preds[0])
    output = model.output[:, argmax]
    last_conv_layer = model.get_layer('block5_conv3')
    grads = K.gradients(output, last_conv_layer.output)[0]
    pooled_grads = K.mean(grads, axis=(0, 1, 2))
    iterate = K.function([model.input], [pooled_grads, last_conv_layer.output[0]])
    pooled_grads_value, conv_layer_output_value = iterate([x])
    for i in range(512):
        conv_layer_output_value[:, :, i] *= pooled_grads_value[i]
    heatmap = np.mean(conv_layer_output_value, axis=-1)
    heatmap = np.maximum(heatmap, 0)
    heatmap /= np.max(heatmap)
    import cv2
    img = cv2.imread(img_path)
    heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0]))
    heatmap = np.uint8(255 * heatmap)
    heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
    hif = .8
    superimposed_img = heatmap * hif + img
    output = 'output.jpeg'
    cv2.imwrite(output, superimposed_img)
    img=mpimg.imread(output)
    plt.imshow(img)
    plt.axis('off')
    plt.title(predictions.loc[0,'category'].upper())
    return None
Esempio n. 17
0
def check_each_layer(model, mask_array, x):
    origin_weights = model.get_weights()
    num_of_layer = len(origin_weights) // 2 
    
    for i in range(num_of_layer):
        checkfile = open('check_each_layer.txt', 'a')
        for j in mask_array:
            check_model = swap_layer(model, i, j)
            print("{}, {}complete".format(i, j))
            yhat = check_model.predict(x)
            # convert the probabilities to class labels
            label = decode_predictions(yhat)
            # print the classification
            checkfile.write('%s layer, %s bit : %s (%.2f%%) %s (%.2f%%) %s (%.2f%%)\n'
                 % (i, j, label[0][0][1], label[0][0][2]*100, label[0][1][1], label[0][1][2]*100, label[0][2][1], label[0][2][2]*100))
            model.set_weights(origin_weights)
        checkfile.write('\n')
        checkfile.close()
Esempio n. 18
0
def image_prediction(image):
    MODEL = VGG19()
    try:
        image = cv2.imread(image)
        image = cv2.resize(image, (224, 224))
        image = image.reshape(
            (1, image.shape[0], image.shape[1], image.shape[2]))

        yhat = MODEL.predict(image)
        label = decode_predictions(yhat)
        label = label[0][0]
        label, conf = label[1], label[2] * 100
        results = [label, conf]

    except Exception as e:
        results = "Please check the image."

    return results
def main():
    # Load  the trained model.
    model = VGG19(weights='imagenet')

    print_layers(model)

    # load and preprocess image
    img_path = 'elephant.jpg'
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    # predict the class probabilities
    preds = model.predict(x)

    # decode the results into a list of tuples (class, description, probability)
    # (one such list for each sample in the batch)
    print('--------------------------------')
    print('Predicted:', decode_predictions(preds, top=3)[0])
Esempio n. 20
0
def run(image_bytes):

    image_bytes = json.loads(image_bytes)['data'][0]
    image_bytes = image_bytes.encode('utf-8')
    encode_len = len(image_bytes)
    print(encode_len)

    image = Image.frombytes('RGBA', (1315, 640), image_bytes, 'raw')

    image = image.resize((224, 224), Image.ANTIALIAS)
    image = img_to_array(image)
    image = image[:, :, 0:3]
    print(image.shape)
    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
    image = preprocess_input(image)

    model = VGG19()
    yhat = model.predict(image)
    label = decode_predictions(yhat)
    label = label[0][0]
    return label
Esempio n. 21
0
def predict(image_path):
    """Use VGG19 to label image"""
    # Load the VGG19 model
    # https://keras.io/applications/#VGG19
    model = VGG19(include_top=True, weights='imagenet')

    # Define default image size for VGG19
    image_size = (224, 224)
    img = image.load_img(image_path, target_size=image_size)
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    prediction = model.predict(x)
    predictions = decode_predictions(prediction, top=1)[0]
    predicted = predictions[0][1]
    print('Predicted:', predicted)
    predicted_clean = predicted.replace('_', ' ')
    #https://github.com/RasaHQ/rasa_nlu/issues/3102
    K.clear_session()
    if (predicted_clean == 'Siamese cat'):
        predicted_clean = 'Siamese'
    return predicted_clean
Esempio n. 22
0
    def runs(self, path):
        model = VGG19(weights='imagenet')
        img_path = path
        img = image.load_img(img_path, target_size=(224, 224))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        predict = model.predict(x)
        
        dec = decode_predictions(predict, top=3)[0]
        aiArr = []
        aiObj = {}
        # print(dec)
        for item in dec:
            aiObj = {
                "id" : item[0],
                "name" : str(item[1]).replace("_", " "),
                "zh_name" : googletranslate(item[1]),
                "accurate" : str(item[2])
            }
            aiArr.append(aiObj)

        return json.dumps(aiArr)
Esempio n. 23
0
def predict(image_path):
    # Load and resize the image using PIL.
    img = PIL.Image.open(image_path)
    img_resized = img.resize(input_shape, PIL.Image.LANCZOS)

    # Plot the image.
    plt.imshow(img_resized)
    plt.show()

    # Convert the PIL image to a numpy-array with the proper shape.
    img_array = np.expand_dims(np.array(img_resized), axis=0)

    # Use the VGG16 model to make a prediction.
    # This outputs an array with 1000 numbers corresponding to
    # the classes of the ImageNet-dataset.
    pred = pre_model.predict(img_array)
    
    # Decode the output of the VGG16 model.
    pred_decoded = decode_predictions(pred)[0]

    # Print the predictions.
    for code, name, score in pred_decoded:
        print("{0:>6.2%} : {1}".format(score, name))
Esempio n. 24
0
def predict():
	img_path = input(' Please input picture file to predict: ')
	if not os.path.exists(img_path):
		print(" file not exist!")
		return
	try:
		img = Image.open(img_path)
		ori_w,ori_h = img.size
		new_w = 224.0;
		new_h = 224.0;
		if ori_w > ori_h:
			bs = 224.0 / ori_h;
			new_w = ori_w * bs
			weight = int(new_w)
			height = int(new_h)
			img = img.resize( (weight, height), Image.BILINEAR )
			region = ( weight / 2 - 112, 0, weight / 2 + 112, height)
			img = img.crop( region )
		else:
			bs = 224.0 / ori_w;
			new_h = ori_h * bs
			weight = int(new_w)
			height = int(new_h)
			img = img.resize( (weight, height), Image.BILINEAR )
			region = ( 0, height / 2 - 112 , weight, height / 2 + 112  )
			img = img.crop( region )
		x = np.array( img, dtype = 'float32' )
		x[:, :, 0] = x[:, :, 0] - 123.680
		x[:, :, 1] = x[:, :, 1] - 116.779
		x[:, :, 2] = x[:, :, 2] - 103.939
		x = np.expand_dims(x, axis=0)
		results = model.predict(x)
		print(' Predicted: ', decode_predictions(results, top=1)[0])
	except Exception as e:
		print(img_path)
		pass
	return
Esempio n. 25
0
image_dir = sys.argv[1]
output_csv = sys.argv[2]

# load model
model = VGG19()

with open(output_csv, 'w', newline='') as result_csv:

    result_writer = csv.writer(result_csv, quoting=csv.QUOTE_MINIMAL)
    result_writer.writerow(['ISBN', 'Class'])

    isbn = []
    images = []

    for name in getImages(image_dir):
        image = load_img(name, target_size=(224, 224))
        image = img_to_array(image)
        images.append(np.expand_dims(image, axis=0))
        isbn.append(name[:-4])

    images = np.concatenate(images, axis=0)
    images = preprocess_input(images)
    pred = model.predict(images)
    labels = decode_predictions(pred)

    labels = [ label[0][1] for label in labels ]
    label_to_class = { l : float(i) for i, l in enumerate(set(labels)) }

    for book_isbn, label in zip(isbn, labels):
        result_writer.writerow([book_isbn, label_to_class[label]])
Esempio n. 26
0
# -*- coding: utf-8 -*-

"""

Version:    2019/07/11

Author:     wangyi

Desc: vgg19,keras官方的案例
run通过

"""
from keras.applications.vgg19 import VGG19
from keras.preprocessing import image
from keras.applications.vgg19 import preprocess_input, decode_predictions
import numpy as np

model = VGG19(weights='imagenet')

img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
# 将结果解码为元组列表 (class, description, probability)
# (一个列表代表批次中的一个样本)
print('Predicted:', decode_predictions(preds, top=3)[0])
# Predicted: [(u'n02504013', u'Indian_elephant', 0.82658225), (u'n01871265', u'tusker', 0.1122357), (u'n02504458', u'African_elephant', 0.061040461)]
Esempio n. 27
0
from keras.applications.vgg19 import VGG19
from keras.preprocessing import image
from keras.applications.vgg19 import preprocess_input
from keras.applications.vgg19 import decode_predictions
import numpy as np
from PIL import Image
import os
from io import BytesIO
import requests

os.environ["TF_CPP_MIN_LOG_LEVEL"] = '1'
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2'
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '3'

model = VGG19(weights='imagenet', include_top=True)

response = requests.get(
    "https://i.pinimg.com/236x/5b/ca/de/5bcade6dd8e19cfc91458ecc66f97fc5.jpg")
img = Image.open(BytesIO(response.content))
img = img.resize((224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

features = model.predict(x)
label = decode_predictions(features)
print(label)
url_link1=urlopen("https://secure.img1-fg.wfcdn.com/im/60243122/resize-h800%5Ecompr-r85/4037/40372281/Corona+Extendable+Dining+Table.jpg")

#(224,224) is the target size of resnet50 model
img=image.load_img(url_link1,target_size=(224,224))

#visuvalising input image
plt.imshow(img)

#preprocessing input image
x=image.img_to_array(img)
x=np.expand_dims(x,axis=0)
x=preprocess_input(x)

fc1_layer=model2.predict(x)

print("predict:",decode_predictions(fc1_layer))

from keras.utils.vis_utils import model_to_dot
from IPython.display import SVG

#model of base_model(complete VGG19)
SVG(model_to_dot(base_model).create(prog='dot',format='svg'))

#below you can see that model contains blocks till 'fc1' layer (model)
#extracted model
SVG(model_to_dot(model).create(prog='dot',format='svg'))

#after addition of dense1 layer for softmax prediction(model+softmax) 
#(extracted layer+ softmax)--->predictions
SVG(model_to_dot(model2).create(prog='dot',format='svg'))
Esempio n. 29
0
def main():
    """ Mostly inspired from 
        https://github.com/totti0223/gradcamplusplus/blob/master/gradcamutils.py#L10 """
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'  # Makes TensorFlow shut up
    args = parse_args()
    logger = get_logger(args.verbose)

    logger.info(
        "Loading the input image and resizing it to (224, 224) as required by the model."
    )
    image = np.array(load_img(args.input, target_size=(224, 224)),
                     dtype=np.uint8)

    logger.info(
        "Pre-processing the image like ImageNet's were when VGG was trained (sub mean from channels) "
        "also add the batch dimension (only one image)")
    image_processed = preprocess_input(np.expand_dims(image, axis=0))

    logger.info("Instantiate the pre-trained VGG19")
    model = VGG19(include_top=True, input_shape=(224, 224, 3))

    logger.info("Gets which class is the most activated")
    prediction = model.predict(image_processed)
    predicted_class = np.argmax(prediction)
    predicted_class_name = decode_predictions(prediction, top=1)[0][0][1]

    logger.info(
        "Gets the tensor object (a scalar here) which is activated when showing the image"
    )
    y_c_tensor = model.output[0, predicted_class]

    logger.info(
        "Gets the tensor object corresponding to the output of the studied convolution layer"
    )
    A_tensor = model.get_layer(args.layer).output

    logger.info("Gets the tensor containing the gradient of y_c w.r.t. A")
    gradient_tensor = K.gradients(y_c_tensor, A_tensor)[0]

    logger.info(
        "Creates a function that takes as input the model's input "
        "and outputs the convolutions' result and the gradient of the prediction w.r.t. it"
    )
    run_graph = K.function([model.input], [A_tensor, gradient_tensor])

    logger.info("Runs the graph using the inputted image")
    A, gradient = run_graph([image_processed])
    A, gradient = A[0], gradient[
        0]  # Gets the result of the first batch dimension

    logger.info("Performs global average pooling onto the activation gradient")
    alpha_c = np.mean(gradient, axis=(0, 1))

    logger.info("Weighs the filters maps with the activation coefficient")
    L_c = np.dot(A, alpha_c)

    logger.info("Resizes the localisation map to match the input image's size")
    L_c = zoom(L_c, 224 / L_c.shape[0])

    logger.info("Plots the original image and the superimposed heat map")
    plt.subplots(nrows=1, ncols=2, dpi=160, figsize=(7, 4))
    plt.subplots_adjust(left=0.01,
                        bottom=0.0,
                        right=0.99,
                        top=0.96,
                        wspace=0.11,
                        hspace=0.2)
    plt.subplot(121)
    plt.title("Original image")
    plt.imshow(image)
    plt.axis("off")
    plt.subplot(122)
    plt.title("{}th dimension ({}) \nw.r.t layer {}".format(
        predicted_class, predicted_class_name, args.layer))
    plt.imshow(image)
    plt.imshow(L_c, alpha=0.5, cmap="jet")
    plt.axis("off")

    if args.output is not None:
        logger.info("Saves the figure under {}".format(args.output))
        plt.savefig(args.output, dpi=300)

    if not args.quiet:
        plt.show()
Esempio n. 30
0
		ori_w,ori_h = img.size
		new_w = 224.0;
		new_h = 224.0;
		if ori_w > ori_h:
			bs = 224.0 / ori_h;
			new_w = ori_w * bs
			weight = int(new_w)
			height = int(new_h)
			img = img.resize( (weight, height), Image.BILINEAR )
			region = ( weight / 2 - 112, 0, weight / 2 + 112, height)
			img = img.crop( region )
		else:
			bs = 224.0 / ori_w;
			new_h = ori_h * bs
			weight = int(new_w)
			height = int(new_h)
			img = img.resize( (weight, height), Image.BILINEAR )
			region = ( 0, height / 2 - 112 , weight, height / 2 + 112  )
			img = img.crop( region )
		x = np.asarray( img, dtype = 'float32' )
		x[:, :, 0] = x[:, :, 0] - 123.680
		x[:, :, 1] = x[:, :, 1] - 116.779
		x[:, :, 2] = x[:, :, 2] - 103.939
		x = np.expand_dims(x, axis=0)
		results = model.predict(x)
		print('Predicted:', decode_predictions(results, top=5)[0])
	except Exception as e:
		pass


	
Esempio n. 31
0
max_norm = 12
i = 0 
for filename in os.listdir(img_dir):
    if not filename.startswith('.'):
        index = (int)(filename.split(".JPEG")[0].split("_")[2])
        #print(index)
        img = image.load_img(img_dir + filename, target_size = (size, size)) # We assume all images have the same dimensions
        img = image.img_to_array(img)
        label = val_annotations_map[index-1]
        i = i + 1
        payload = perturb(img = img, noise = noise_color, norm = max_norm)
        payload = payload.astype('float32')
        payload = cv2.bilateralFilter(payload, 3, 5, 5)        
        prob = model.predict(preprocess_input(payload.astype(np.float).reshape((1, size, size, 3))))
        index = (label-1)
        label_str_list = decode_predictions(prob, top =1)[0]
        label_str = ""
        for item in label_str_list:
            print(item)
            label_str = item[0]
        print("The prediction is %s" %label_str) 
        ground_truth_str = mapping_table[str(label)]
        print("The ground truth is %s" %ground_truth_str)
        if ground_truth_str != label_str:
            j = j + 1
            print("Not equal")
        else:
            print("Equal")
        real_attack_rate = (float)(j)/(float)(i)
        print('%.4f' %real_attack_rate)
last_attack_rate = (float)(j)/(float)(i)