# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

# +
import keras
import os
from params import get_params
from sklearn import preprocessing
import sklearn.preprocessing
import numpy as np

import matplotlib.pyplot as plt

from keras.models import Model
from keras.preprocessing import image
from keras.applications.vgg19 import VGG19
from keras.applications.vgg19 import preprocess_input

from PIL import Image, ImageOps

import pickle
# -

descriptors_val = pickle.load(open("save_val.p", "rb"))

descriptors_train = pickle.load(open("save_train.p", "rb"))

dic_val = pickle.load(open("save_dic_val.p", "rb"))

dic_train = pickle.load(open("save_dic_train.p", "rb"))

imagen = pickle.load(open("save_img.p", "rb"))

imagen3 = pickle.load(open("save_img3.p", "rb"))

x_train = np.reshape(descriptors_train, (1194,4096)) 

mp=1
x_val = np.reshape(descriptors_val[mp], (1,4096)) 

x_train = sklearn.preprocessing.normalize(x_train, norm='l2', axis=1, copy=True, return_norm=False)

x_val = sklearn.preprocessing.normalize(x_val, norm='l2', axis=1, copy=True, return_norm=False)

descriptors_traint = x_train.transpose()

similarities=np.matmul(x_val,descriptors_traint)

ranks = np.argsort(similarities, axis=1)[:,::-1]

# get the original images for visualization
x_train_images = []
x_val_images = []

x_val_images.append(np.array(imagen[mp]))

b = 0
for b in range(1194): 
    x_train_images.append(np.array(imagen3[b]))

h,w = (224, 224)
new_image= Image.new('RGB', (h*6,w*1))

# Visualize ranks of the 10 queries
offset = 10 # it will show results from query #'offset' to #offset+10
relnotrel = []
for q in range(1):
    ranks_q = ranks[q*(offset+1),:]
    relevant = dic_val[mp]
    for i in range(1194):
        if relevant == dic_train[ranks_q[i]]:
            new_image.paste(ImageOps.expand(Image.fromarray(x_train_images[ranks_q[i]]), border=10, fill='green'), (h*(1+i),w*q))
            relnotrel.append(1)
        else:
            new_image.paste(ImageOps.expand(Image.fromarray(x_train_images[ranks_q[i]]), border=10, fill='red'), (h*(1+i),w*q))
            relnotrel.append(0)
            
  # visualize query
    ima_q = Image.fromarray(x_val_images[0])
    ima_q = ImageOps.expand(ima_q, border=10, fill='blue')

    new_image.paste(ima_q, (0,w*q))

plt.imshow(new_image)
plt.axis('off')
plt.show()

new_image.save('imagen1.png')

accu = 0
accu_final=0

numRel = 0

graphic = []
for k in range(len(relnotrel)):

        # If the value is 1
        if relnotrel[k] == 1:

            # We add 1 to the number of correct instances
            numRel = numRel + 1

            # We calculate the precision at k (+1 because we start at 0)
            # and we accumulate it
            accu += float( numRel )/ float(k+1)
            if numRel == 3:
                accu_final = accu
                num = k
        graphic.append(float( numRel )/ float(k+1))

accu_final / float(3)*100

plt.plot(graphic)

Recall = 0

numRel2 = 0

len_final = 0

graphic2 = []
for k2 in range(len(relnotrel)):

        # If the value is 1
        if relnotrel[k2] == 1:

            # We add 1 to the number of correct instances
            numRel2 = numRel2 + 1
            
            if numRel2 == 3:
                Recall = float( numRel2 )/ float(numRel)
        graphic2.append(float( numRel2 )/ float(numRel))

plt.plot(graphic2)

Recall * 100

graphic[0] = 1.0
graphic2[0] = 1.0

plt.plot(graphic2[1:(num+1)],graphic[1:(num+1)])
plt.title('Precision-Recall')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.show()

StarcoderdataJupyterScriptsDedupFiltered