Example #1
0
def cache_images_dir(dir, max_n=1000, replace=False):

    # Inception
    inception.maybe_download()
    model = inception.Inception()

    # Storage
    file_path_cache = os.path.join(dir + '/images_features.pkl')

    if replace:
        os.remove(file_path_cache)

    print("Processing Inception transfer-values...")

    dir_im = dir + '/pics'

    n_total_images = sum([len(files) for r, d, files in os.walk(dir_im)])
    n_total_images = min([max_n, n_total_images])
    print('Fetching %d images in %s ...' % (n_total_images, dir_im))
    images = np.zeros((n_total_images, 192, 256, 3), dtype=np.float32)
    id = []
    index = 0
    n_err = 0
    for d in os.listdir(dir_im):
        if index >= max_n:
            break
        d = dir_im + '/' + d
        for image_name in os.listdir(d):
            if index >= max_n:
                break
            image_path = d + '/' + image_name
            try:
                image_data = (misc.imread(image_path)[:, :, :3]).astype(
                    np.float32)
                images[
                    index, :, :, :] = image_data  # (n, height, width, channels)
                id.append(os.path.splitext(image_name)[0])
                index += 1
            except OSError as err:
                print(err)
                n_err += 1
    if n_err > 0:
        images = np.delete(images, range(n_total_images - n_err,
                                         n_total_images), 0)

    id = np.array(id)

    transfer_values = inception.transfer_values_cache(
        cache_path=file_path_cache, images=images, model=model)

    return transfer_values, id
Example #2
0
def test(queries=list(), location='./test'):
    """
    Test your system with the input. For each input, generate a list of IDs that is returned
    :param queries: list of image-IDs. Each element is assumed to be an entry in the test set. Hence, the image
    with id <id> is located on my computer at './test/pics/<id>.jpg'. Make sure this is the file you work with...
    :param location: The location of the test data folder hierarchy
    :return: a dictionary with keys equal to the images in the queries - list, and values a list of image-IDs
    retrieved for that input
    """

    # # ##### The following is an example implementation -- that would lead to 0 points  in the evaluation :-)
    # my_return_dict = {}
    #
    # # Load the dictionary with all training files. This is just to get a hold of which
    # # IDs are there; will choose randomly among them
    # training_labels = pickle.load(open('./train/pickle/combined.pickle', 'rb'))
    # training_labels = list(training_labels.keys())
    #
    # for query in queries:
    #
    #     # This is the image. Just opening if here for the fun of it; not used later
    #     image = Image.open(location + '/pics/' + query + '.jpg')
    #     image.show()
    #
    #     # Generate a random list of 50 entries
    #     cluster = [training_labels[random.randint(0, len(training_labels) - 1)] for idx in range(50)]
    #     my_return_dict[query] = cluster

    my_return_dict = {}

    # Inception
    inception.maybe_download()
    model = inception.Inception()

    for query in queries:

        # Data
        image_path = location + '/pics/' + query + '.jpg'
        image_data = (misc.imread(image_path)[:, 0:192, :3]).astype(np.float32)
        image_tranfer_values = model.transfer_values(image_path, image_data)

        # Inference
        my_return_dict[query] = infere(image_tranfer_values)

    return my_return_dict
def main():
    # command line arguments
    img_dir = sys.argv[1]
    save_path = sys.argv[2]

    # geting list of images from specified dir
    imgs = get_file_list(img_dir)

    print("Number of images: ", len(imgs))

    # loading Inception
    inception.maybe_download()
    # creating an Inception instance
    model = inception.Inception()

    write_output_to_pickle(imgs, img_dir, model, save_path=save_path)

    model.close()
Example #4
0
def load_inception_model():
    # make directory if not exist
    if not os.path.isdir("data"):
        os.mkdir("data")
    if not os.path.isdir("data/CIFAR-10"):
        os.mkdir("data/CIFAR-10")
    if not os.path.isdir("inception"):
        os.mkdir("inception")
    if not os.path.isdir("img"):
        os.mkdir("img")
    if not os.path.isdir("graphs"):
        os.mkdir("graphs")

    # load inception model
    inception.maybe_download(
    )  # download inception data (85MB) if not exist in inception directory
    model = inception.Inception()  # load inception model

    return model
Example #5
0
def main():

    img_path = sys.argv[1]

    data, img_lst = load_data_np("../data/transfer_layer")

    t = build_Index(data, len(data[0]))

    save_index(t, path)

    n = gc.collect()

    inception.maybe_download()

    incept = inception.Inception()

    vector = get_tl_vector(incept, img_path)

    x = t.get_nns_by_vector(vector, n=50)

    print(x)
Example #6
0
def test_dataset(pickle_f):
    inception.maybe_download()
    model = inception.Inception()

    images, labels = pickle.load(
                open( expanduser("~/adversary/data/pickles/" + pickle_f), "rb"))

    pred = inception.process_images(fn=model.classify, images=images)
    
    #picks the category with the highest score
    labels_true = numpy.argmax(labels, axis=1)
    labels_pred = numpy.argmax(pred, axis=1)
    
    conf_matrix = confusion_matrix(labels_true, labels_pred) 
    
    accuracy = accuracy_score(labels_true, labels_pred)
    
    f = open(expanduser("~/adversary/data/results/" + pickle_f), 'w')
    f.write("Accuracy =  " + str(accuracy) + '\n')  # python will convert \n to os.linesep
    f.write("Confusion Matrix: " + str(conf_matrix) + '\n')
    f.close()

    model.close()
Example #7
0
from os.path import expanduser
import sys

sys.path.insert(0, expanduser('~/adversary/src/models'))
sys.path.insert(0, expanduser('~/adversary/src/utils/Hvass_Lab'))

# Functions and classes for loading and using the Inception model.
import inception

# specify the location where we should keep all the files
# associated with the inception model
inception.data_dir = expanduser("~/adversary/src/models/inception")

#Download the data for the Inception model if it doesn't already
# exist in the directory. It is 85 MB.
inception.maybe_download()

#Load the Inception model so it is ready for classifying images.
model = inception.Inception()

#Get a reference to the input tensor for the Inception model.
resized_image = model.resized_image

#Get a reference to the output of the softmax-classifier for the Inception model.
y_pred = model.y_pred

#Get a reference to the unscaled output of the softmax-classifier for the Inception
# model. These are often called 'logits'. The logits are necessary because we will
# add a new loss-function to the graph, which requires these unscaled outputs.
y_logits = model.y_logits
def main(splitnum):

    test, train, val = transfer.preprocessing(
        "/home/runefeather/Desktop/Classwork/AI/Project/breakhissplits_v2/train_val_test_60_12_28/"
    )
    transfer.split(test, train, val, str(splitnum))

    tumordata.add_data_path(splitnum)

    tumordata.start()

    print(tumordata.data_path)

    class_names = tumordata.load_class_names()
    images_train, cls_train, labels_train = tumordata.load_training_data()
    images_test, cls_test, labels_test = tumordata.load_testing_data()

    print("Size of:")
    print("- Training-set:\t\t{}".format(len(images_train)))
    print("- Test-set:\t\t{}".format(len(images_test)))

    # inception dir
    inception.data_dir = 'inception/'

    # download the model
    inception.maybe_download()

    # load model
    model = inception.Inception()

    # caches for training and test sets
    file_path_cache_train = os.path.join(tumordata.data_path,
                                         'inception_tumordata_train.pkl')
    file_path_cache_test = os.path.join(tumordata.data_path,
                                        'inception_tumordata_test.pkl')

    print("Processing Inception transfer-values for training-images ...")

    # If transfer-values have already been calculated then reload them,
    # otherwise calculate them and save them to a cache-file.
    transfer_values_train = transfer_values_cache(
        cache_path=file_path_cache_train, images=images_train, model=model)

    print("Processing Inception transfer-values for test-images ...")

    # If transfer-values have already been calculated then reload them,
    # otherwise calculate them and save them to a cache-file.
    transfer_values_test = transfer_values_cache(
        cache_path=file_path_cache_test, images=images_test, model=model)

    transfer_len = model.transfer_len

    x = tf.placeholder(tf.float32, shape=[None, transfer_len], name='x')

    y_true = tf.placeholder(tf.float32,
                            shape=[None, num_classes],
                            name='y_true')

    y_true_cls = tf.argmax(y_true, dimension=1)

    # Wrap the transfer-values as a Pretty Tensor object.
    x_pretty = pt.wrap(x)

    with pt.defaults_scope(activation_fn=tf.nn.relu):
        y_pred, loss = x_pretty.\
            fully_connected(size=1024, name='layer_fc1').\
            softmax_classifier(num_classes=num_classes, labels=y_true)

    global_step = tf.Variable(initial_value=0,
                              name='global_step',
                              trainable=False)

    optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(
        loss, global_step)

    y_pred_cls = tf.argmax(y_pred, dimension=1)

    correct_prediction = tf.equal(y_pred_cls, y_true_cls)

    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    session = tf.Session()
    session.run(tf.global_variables_initializer())

    print_test_accuracy(session, x, y_true, y_pred_cls, transfer_values_test,
                        labels_test, cls_test)

    session = optimize(5000, session, x, y_true, global_step, optimizer,
                       accuracy, transfer_values_train, labels_train)

    print_test_accuracy(session, x, y_true, y_pred_cls, transfer_values_test,
                        labels_test, cls_test)

    model.close()
    session.close()
Example #9
0
from reverse_img_query import load_data_np, \
    build_Index, \
    get_tl_vector, \
    get_ol_vector
from inception import Inception, maybe_download
import gc

from pathing_utils import path_to_image_frontend, \
    path_to_static, \
    path_to_images_folder_absolute, \
    image_name_from_path,\
    path_to_uploads

from utils import image_from_base64str

maybe_download()
inception_layer_path = path_to_static() + "inception_output_layer2"
inverted_index_path = path_to_static() + "inverted_index"
vocab_path = path_to_static() + "vocab.csv"
word2vec_vocab_path = path_to_static() + "word2vecvocab.csv"
word_vec_path = path_to_static() + "GoogleNews-vectors-negative300.bin.gz"
print("READ inception_layer_path")
inverted_index = pd.read_pickle(inverted_index_path)
df_in = pd.read_pickle(inception_layer_path)
df_in.columns = ['img', 'output_layer', 'scores']
# print("DROP column - output_layer")
df_in.drop('output_layer', axis=1, inplace=True)
# print(df_in.head())
known_words = defaultdict(list)
annoy_index = None
inception_object = None
Example #10
0
def Appliquer_inception(image_path):
	inception.maybe_download()
	model = inception.Inception()
	resized_image = model.get_resized_image(image_path=image_path)
	classify(image_path="images/Velo9.jpg")
Example #11
0
def main(splitnum, finalimgpath):

    # print stuff
    print("=========================================")
    print(splitnum)

    train_batch_size = 64

    def random_batch():
        # Number of images (transfer-values) in the training-set.
        num_images = len(transfer_values_train)

        # Create a random index.
        idx = np.random.choice(num_images,
                               size=train_batch_size,
                               replace=False)

        # Use the random index to select random x and y-values.
        # We use the transfer-values instead of images as x-values.
        x_batch = transfer_values_train[idx]
        y_batch = labels_train[idx]

        return x_batch, y_batch

    def optimize(num_iterations):
        # Start-time used for printing time-usage below.
        start_time = time.time()

        # Else, save for the first time
        saver = tf.train.Saver(tf.all_variables(), max_to_keep=100)
        session_list = []
        test_acc = []

        for i in range(num_iterations):
            # Get a batch of training examples.
            # x_batch now holds a batch of images (transfer-values) and
            # y_true_batch are the true labels for those images.
            x_batch, y_true_batch = random_batch()

            # Put the batch into a dict with the proper names
            # for placeholder variables in the TensorFlow graph.
            feed_dict_train = {x: x_batch, y_true: y_true_batch}

            # Run the optimizer using this batch of training data.
            # TensorFlow assigns the variables in feed_dict_train
            # to the placeholder variables and then runs the optimizer.
            # We also want to retrieve the global_step counter.
            i_global, _ = session.run([global_step, optimizer],
                                      feed_dict=feed_dict_train)

            # Print status to screen every 100 iterations (and last).
            if (i_global % 100 == 0) or (i == num_iterations - 1):
                savepath = saver.save(session,
                                      'checkpoints\\split1\\model',
                                      global_step=i_global)
                session_list.append(savepath)

                # Calculate the accuracy on the training-batch.
                batch_acc = session.run(accuracy, feed_dict=feed_dict_train)

                # Test accuracy with session
                correct, cls_pred = predict_cls_test()
                acc, num_correct = classification_accuracy(correct)

                # Print status.
                # msg = "Global Step: {0:>6}, Training Batch Accuracy: {1:>6.1%}"
                # print(msg.format(i_global, batch_acc))
                # print("Testing Accuracy: ", round(acc*100, 2))

                # save test accuracy
                test_acc.append(round(acc * 100, 2))
                # print("========================================================")

        # Ending time.
        end_time = time.time()

        # Difference between start and end-times.
        time_dif = end_time - start_time

        # Print the time-usage.
        print("Time usage: " + str(timedelta(seconds=int(round(time_dif)))))

        max_acc = max(test_acc)

        print("MAX: ", max_acc)
        # print(list(test_acc.values()).index(max_acc))
        print(test_acc.index(max_acc))
        # print(mydict.values().index(max(test_acc.values()))) #
        pth = session_list[test_acc.index(max_acc)]

        saver.restore(session, pth)
        return

    def plot_example_errors(cls_pred, correct):
        # This function is called from print_test_accuracy() below.

        # cls_pred is an array of the predicted class-number for
        # all images in the test-set.

        # correct is a boolean array whether the predicted class
        # is equal to the true class for each image in the test-set.

        # Negate the boolean array.
        incorrect = (correct == False)

        # Get the images from the test-set that have been
        # incorrectly classified.
        images = images_test[incorrect]

        # Get the predicted classes for those images.
        cls_pred = cls_pred[incorrect]

        # Get the true classes for those images.
        cls_true = cls_test[incorrect]

        n = min(9, len(images))

        # Plot the first n images.
        plot_images(images=images[0:n],
                    cls_true=cls_true[0:n],
                    cls_pred=cls_pred[0:n])

    def plot_confusion_matrix(cls_pred):
        # This is called from print_test_accuracy() below.

        # cls_pred is an array of the predicted class-number for
        # all images in the test-set.

        # Get the confusion matrix using sklearn.
        cm = confusion_matrix(
            y_true=cls_test,  # True class for test-set.
            y_pred=cls_pred)  # Predicted class.

        # Print the confusion matrix as text.
        for i in range(num_classes):
            # Append the class-name to each line.
            class_name = "({}) {}".format(i, class_names[i])
            print(cm[i, :], class_name)

        # Print the class-numbers for easy reference.
        class_numbers = [" ({0})".format(i) for i in range(num_classes)]
        print("".join(class_numbers))

    # Split the data-set in batches of this size to limit RAM usage.
    batch_size = 256

    def predict_cls(transfer_values, labels, cls_true):
        # Number of images.
        num_images = len(transfer_values)

        # Allocate an array for the predicted classes which
        # will be calculated in batches and filled into this array.
        cls_pred = np.zeros(shape=num_images, dtype=np.int)

        # Now calculate the predicted classes for the batches.
        # We will just iterate through all the batches.

        # The starting index for the next batch is denoted i.
        i = 0

        while i < num_images:
            # The ending index for the next batch is denoted j.
            j = min(i + batch_size, num_images)

            # Create a feed-dict with the images and labels
            # between index i and j.
            feed_dict = {x: transfer_values[i:j], y_true: labels[i:j]}

            # Calculate the predicted class using TensorFlow.
            cls_pred[i:j] = session.run(y_pred_cls, feed_dict=feed_dict)

            # Set the start-index for the next batch to the
            # end-index of the current batch.
            i = j

        # Create a boolean array whether each image is correctly classified.
        correct = (cls_true == cls_pred)

        return correct, cls_pred

    def predict_one_image(imgarr):
        # Number of images.
        num_images = 1

        label = np.zeros(shape=[0, 2], dtype=np.int)

        # Allocate an array for the predicted classes which
        # will be calculated in batches and filled into this array.
        cls_pred = np.zeros(shape=num_images, dtype=np.int)

        feed_dict = {x: imgarr, y_true: label}
        cls_pred = session.run(y_pred_cls, feed_dict=feed_dict)

        return cls_pred

    def predict_cls_test():
        return predict_cls(transfer_values=transfer_values_test,
                           labels=labels_test,
                           cls_true=cls_test)

    def classification_accuracy(correct):
        # When averaging a boolean array, False means 0 and True means 1.
        # So we are calculating: number of True / len(correct) which is
        # the same as the classification accuracy.

        # Return the classification accuracy
        # and the number of correct classifications.
        return correct.mean(), correct.sum()

    def print_test_accuracy(show_example_errors=False,
                            show_confusion_matrix=False):

        # For all the images in the test-set,
        # calculate the predicted classes and whether they are correct.
        correct, cls_pred = predict_cls_test()

        # Classification accuracy and the number of correct classifications.
        acc, num_correct = classification_accuracy(correct)

        # Number of images being classified.
        num_images = len(correct)

        # Print the accuracy.
        msg = "Accuracy on Test-Set: {0:.1%} ({1} / {2})"
        print(msg.format(acc, num_correct, num_images))

        # Plot some examples of mis-classifications, if desired.
        if show_example_errors:
            print("Example errors:")
            plot_example_errors(cls_pred=cls_pred, correct=correct)

        # Plot the confusion matrix, if desired.
        if show_confusion_matrix:
            print("Confusion Matrix:")
            plot_confusion_matrix(cls_pred=cls_pred)

    # =========================================================================================
    # THIS IS WHERE EVERYTHING COMES TOGETHER
    # =========================================================================================

    test, train, val = transfer.preprocessing(
        "D:\\AI Stuff\\aiproject-inception-master\\breakhissplits_v2\\train_val_test_60_12_28\\"
    )
    transfer.split(test, train, val, str(splitnum))

    tumordata.add_data_path(splitnum)

    tumordata.start()

    print(tumordata.data_path)

    class_names = tumordata.load_class_names()
    images_train, cls_train, labels_train = tumordata.load_training_data()
    images_test, cls_test, labels_test = tumordata.load_testing_data()

    print("Size of:")
    print("- Training-set:\t\t{}".format(len(images_train)))
    print("- Test-set:\t\t{}".format(len(images_test)))

    # Image to predict on
    img = Image.open(finalimgpath)
    imgarr = []
    imgarr.append(np.array(img))

    # inception dir
    inception.data_dir = 'inception/'

    # download the model
    inception.maybe_download()

    # load model
    model = inception.Inception()

    # caches for training and test sets
    file_path_cache_train = os.path.join(tumordata.data_path,
                                         'inception_tumordata_train.pkl')
    file_path_cache_test = os.path.join(tumordata.data_path,
                                        'inception_tumordata_test.pkl')

    file_path_cache_single_test = os.path.join(
        tumordata.data_path, 'inception_tumordata_single_test.pkl')

    print("Processing Inception transfer-values for training-images ...")

    # If transfer-values have already been calculated then reload them,
    # otherwise calculate them and save them to a cache-file.
    transfer_values_train = transfer_values_cache(
        cache_path=file_path_cache_train, images=images_train, model=model)

    print("Processing Inception transfer-values for test-images ...")

    # If transfer-values have already been calculated then reload them,
    # otherwise calculate them and save them to a cache-file.
    transfer_values_test = transfer_values_cache(
        cache_path=file_path_cache_test, images=images_test, model=model)

    transfer_values_single_test = transfer_values_cache(
        cache_path=file_path_cache_single_test, images=imgarr, model=model)

    # print("TRANSFER VALUES TEST: ", transfer_values_test)

    transfer_len = model.transfer_len

    x = tf.placeholder(tf.float32, shape=[None, transfer_len], name='x')
    y_true = tf.placeholder(tf.float32,
                            shape=[None, num_classes],
                            name='y_true')
    y_true_cls = tf.argmax(y_true, dimension=1)

    # x_one = tf.placeholder(tf.float32, shape=[len(imgarr), len(imgarr[0]), 3], name='x_one')
    # y_true_one = tf.placeholder(tf.float32, shape=1, name='y_true_one')
    # y_true_cls_one = tf.argmax(y_true_one, dimension=1)

    # Wrap the transfer-values as a Pretty Tensor object.
    x_pretty = pt.wrap(x)

    with pt.defaults_scope(activation_fn=tf.nn.relu):
        y_pred, loss = x_pretty.\
            fully_connected(size=1024, name='layer_fc1').\
            softmax_classifier(num_classes=num_classes, labels=y_true)

    global_step = tf.Variable(initial_value=0,
                              name='global_step',
                              trainable=False)

    optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(
        loss, global_step)

    y_pred_cls = tf.argmax(y_pred, dimension=1)

    correct_prediction = tf.equal(y_pred_cls, y_true_cls)

    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    session = tf.Session()
    session.run(tf.global_variables_initializer())

    print_test_accuracy(show_example_errors=False, show_confusion_matrix=False)

    optimize(1000)

    print_test_accuracy(show_example_errors=False, show_confusion_matrix=False)
    correct, cls_pred = predict_cls_test()
    acc, num_correct = classification_accuracy(correct)
    # print("acc, num correct: ", acc, num_correct)

    cls_pred = predict_one_image(transfer_values_single_test)
    # print("PREDICTION")
    # print(cls_pred)
    print(">>>>>>>>>>>><<<<<<<<<<<<<<<<")

    # prediction = model.classify(finalimgpath)

    return acc, cls_pred
Example #12
0
def setup():
    cifar10.maybe_download_and_extract()
    inception.maybe_download()