Exemple #1
0
    def generate_map(image):
        image = load_single_image(image)
        hyper = HyperParams(verbose=False)
        images_tf = tf.placeholder(
            tf.float32, [None, hyper.image_h, hyper.image_w, hyper.image_c],
            name="images")
        class_tf = tf.placeholder(tf.int64, [None], name='class')
        cnn = CNN()
        if hyper.fine_tuning:
            cnn.load_vgg_weights()

        conv_last, gap, class_prob = cnn.build(images_tf)
        classmap = cnn.get_classmap(class_tf, conv_last)

        with tf.Session() as sess:
            tf.train.Saver().restore(sess, hyper.model_path)
            conv_last_val, class_prob_val = sess.run(
                [conv_last, class_prob], feed_dict={images_tf: image})

            # use argsort instead of argmax to get all the classes
            class_predictions_all = class_prob_val.argsort(axis=1)

            roi_map = None
            for i in range(-1 * hyper.top_k, 0):

                current_class = class_predictions_all[:, i]
                classmap_vals = sess.run(classmap,
                                         feed_dict={
                                             class_tf: current_class,
                                             conv_last: conv_last_val
                                         })
                normalized_classmap = normalize(classmap_vals[0])

                if roi_map is None:
                    roi_map = 1.2 * normalized_classmap
                else:
                    # simple exponential ranking
                    roi_map = (roi_map + normalized_classmap) / 2
            roi_map = normalize(roi_map)

        # Plot the heatmap on top of image
        fig, ax = plt.subplots(1, 1, figsize=(12, 9))
        ax.margins(0)
        plt.axis('off')
        plt.imshow(roi_map, cmap=plt.cm.jet, interpolation='nearest')
        plt.imshow(image[0], alpha=0.4)

        # save the plot and the map
        if not os.path.exists('static/output'):
            os.makedirs('static/output')
        plt.savefig('static/output/overlayed_heatmap.png')
        skimage.io.imsave('static/output/msroi_map.jpg', roi_map)
def compression_engine(img):

    image = load_single_image(img)

    print("INPUT IMAGE ARRAY ",image.shape)

    hyper = HyperParams(verbose=False)
    images_tf = tf.placeholder(tf.float32, [None, hyper.image_h, hyper.image_w, hyper.image_c], name="images")
    class_tf  = tf.placeholder(tf.int64, [None], name='class')

    cnn = CNN()
    if hyper.fine_tuning:
        cnn.load_vgg_weights()

    conv_last, gap, class_prob = cnn.build(images_tf)
    classmap = cnn.get_classmap(class_tf, conv_last)

    with tf.Session() as sess:
        tf.train.Saver().restore( sess, hyper.model_path )
        conv_last_val, class_prob_val = sess.run([conv_last, class_prob], feed_dict={images_tf: image})

        # use argsort instead of argmax to get all the classes
        class_predictions_all = class_prob_val.argsort(axis=1)

        roi_map = None
        for i in range(-1 * hyper.top_k,0):

            current_class = class_predictions_all[:,i]
            classmap_vals = sess.run(classmap, feed_dict={class_tf: current_class, conv_last: conv_last_val})
            normalized_classmap = normalize(classmap_vals[0])

            if roi_map is None:
                roi_map = 1.2 * normalized_classmap
            else:
                # simple exponential ranking
                roi_map = (roi_map + normalized_classmap)/2
        roi_map = normalize(roi_map)

    # save the plot and the map
    skimage.io.imsave( 'msroi_map.jpg', roi_map )
 




    original = Image.open(img)

    #print("ORIGINAL : ",original)
    sal = Image.open('msroi_map.jpg')

    make_quality_compression(original,sal,img,original)
Exemple #3
0
def compression_engine(img):

    image = load_single_image(img)

    #print("INPUT IMAGE ARRAY ",image.shape)

    hyper = HyperParams(verbose=False)
    images_tf = tf.placeholder(tf.float32, [None, hyper.image_h, hyper.image_w, hyper.image_c], name="images")
    class_tf  = tf.placeholder(tf.int64, [None], name='class')

    cnn = CNN()
    if hyper.fine_tuning:
        cnn.load_vgg_weights()

    conv_last, gap, class_prob = cnn.build(images_tf)
    classmap = cnn.get_classmap(class_tf, conv_last)

    with tf.Session() as sess:
        tf.train.Saver().restore( sess, hyper.model_path )
        conv_last_val, class_prob_val = sess.run([conv_last, class_prob], feed_dict={images_tf: image})

        # use argsort instead of argmax to get all the classes
        class_predictions_all = class_prob_val.argsort(axis=1)

        roi_map = None
        for i in range(-1 * hyper.top_k,0):

            current_class = class_predictions_all[:,i]
            classmap_vals = sess.run(classmap, feed_dict={class_tf: current_class, conv_last: conv_last_val})
            normalized_classmap = normalize(classmap_vals[0])

            if roi_map is None:
                roi_map = 1.2 * normalized_classmap
            else:
                # simple exponential ranking
                roi_map = (roi_map + normalized_classmap)/2
        roi_map = normalize(roi_map)


    # Plot the heatmap on top of image
    fig, ax = plt.subplots(1, 1, figsize=(12, 9))
    ax.margins(0)
    plt.axis('off')
    plt.imshow( roi_map, cmap=plt.cm.jet, interpolation='nearest' )
    plt.imshow( image[0], alpha=0.4)

    # save the plot and the map
    if not os.path.exists('output'):
        os.makedirs('output')
    plt.savefig('output/overlayed_heatmap.png')
    skimage.io.imsave( 'msroi_map.jpg', roi_map )
    plt.clf()
    print("MSROI TYPE : ",type(roi_map))
    plt.close()




    from glob import glob
    # make the output directory to store the Q level images,

    if not os.path.exists(output_directory):
        os.makedirs(output_directory)


    original = Image.open(img)

    #print("ORIGINAL : ",original)
    sal = Image.open('msroi_map.jpg')

    out_name = make_quality_compression(original,sal,img,original)

    return out_name
from __future__ import division


import os, sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.style.use('ggplot')

from util import load_single_image, normalize

if os.path.exists(sys.argv[1]): 
    image = load_single_image(sys.argv[1])
    folder = sys.argv[1]
    folder = folder[:-18]
else: 
    raise Exception("Image file " + sys.argv[1] + " does not exist")

# this is to ensure fast failure, why load other modules if no input file
# matplotlib has to be loaded before inorder to change backend

import pandas as pd
import numpy as np
from model import CNN
from params import HyperParams
import skimage.io


import tensorflow.compat.v1 as tf
tf.disable_v2_behavior() 
hyper = HyperParams(verbose=False)
Exemple #5
0
class_tf = tf.placeholder(tf.int64, [None], name='class')

cnn = CNN()
if hyper.fine_tuning:
    cnn.load_vgg_weights()

conv_last, gap, class_prob = cnn.build(images_tf)
classmap = cnn.get_classmap(class_tf, conv_last)

image_dir_path = r'/content/colaba/MyDrive/colab/image-compression-master/images_directory/kodak/'
image_dir = os.listdir(image_dir_path)
for image_name in image_dir:
    print(image_name)
    image_name_path = image_dir_path + image_name
    if os.path.exists(image_name_path):
        image = load_single_image(image_name_path)
    else:
        raise Exception("Image file " + image_name_path + " does not exist")

    with tf.Session() as sess:
        tf.train.Saver().restore(sess, hyper.model_path)
        conv_last_val, class_prob_val = sess.run([conv_last, class_prob],
                                                 feed_dict={images_tf: image})

        # use argsort instead of argmax to get all the classes
        class_predictions_all = class_prob_val.argsort(axis=1)

        roi_map = None
        for i in range(-1 * hyper.top_k, 0):

            current_class = class_predictions_all[:, i]