Exemplo n.º 1
0
def generate_caption(indIm,
                     names,
                     model,
                     image_model,
                     caption_representations,
                     texts,
                     n=10):
    # generate image representation for new image
    image_filename = dataset_path + names[indIm]
    print(names[indIm])
    y_true = indicesByID(indIm, names)
    features = extract_features_file(model, image_filename)
    image_representation = image_model.predict(np.array([features]))
    # compute score of all captions in the dataset
    scores = np.dot(caption_representations, image_representation.T).flatten()
    # compute indices of n best captions
    indices = np.argpartition(scores, -n)[-n:]
    indices = indices[np.argsort(scores[indices])]
    # display them
    y_pred = []
    for i in [int(x) for x in reversed(indices)]:
        print(i, texts[i])
        y_pred.append(i)
    # print('y_pred : ', y_pred)
    print('y_true : ', y_true)
    print('y_pred : ', y_pred)
    y_pred = np.array(y_pred)
    ranking_metrics = BinaryMetrics(y_true, y_pred)
    print('k = ', n)
    print('precision = ', ranking_metrics.precision_at(n))
    print('recall = ', ranking_metrics.recall_at(n))
    print('r precision = ', ranking_metrics.r_precision())
    print('_______________________________')
Exemplo n.º 2
0
def stats_generate_caption(names,
                           model,
                           image_model,
                           caption_representations,
                           texts,
                           n=500):
    prec = []
    recall = []
    r_prec = []
    for indIm in range(0, 100, 5):
        thisPrec = []
        thisRecall = []
        thisRPrec = []
        image_filename = dataset_path + names[indIm]
        print(names[indIm])
        y_true = indicesByID(indIm, names)
        features = extract_features_file(model, image_filename)
        image_representation = image_model.predict(np.array([features]))
        scores = np.dot(caption_representations,
                        image_representation.T).flatten()
        indices = np.argpartition(scores, -n)[-n:]
        indices = indices[np.argsort(scores[indices])]
        y_pred = []
        for i in [int(x) for x in reversed(indices)]:
            # print(i, texts[i])
            y_pred.append(i)
        y_pred = np.array(y_pred)
        ranking_metrics = BinaryMetrics(y_true, y_pred)
        thisRange = range(5, 500, 10)
        for k in thisRange:
            thisPrec.append(ranking_metrics.precision_at(k))
            thisRecall.append(ranking_metrics.recall_at(k))
        prec.append(thisPrec)
        recall.append(thisRecall)
    prec = pd.DataFrame(prec, columns=thisRange)
    recall = pd.DataFrame(recall, columns=thisRange)
    prec_moy = prec.mean()
    prec_moy = prec_moy
    recall_moy = recall.mean()
    df = pd.DataFrame({
        'Nbr de prédictions': thisRange,
        'recall_moyen': recall_moy,
        'prec_moy': prec_moy
    })
    plt.plot('Nbr de prédictions',
             'recall_moyen',
             data=df,
             color='skyblue',
             linewidth=2)
    plt.plot('Nbr de prédictions',
             'prec_moy',
             data=df,
             color='olive',
             linewidth=2)
    plt.legend()
    plt.savefig('drive/MyDrive/benoit/figures/recall_prec')
Exemplo n.º 3
0
def searchImagesFromImage(image_filename, model, image_model, image_representations, n=10) :
    thisFeatures = extract_features_file(model, image_filename)
    image_representation = image_model.predict(np.array([thisFeatures]))
    scores = np.dot(image_representations,image_representation.T).flatten()
    indices = np.argpartition(scores, -n)[-n:]
    indices = indices[np.argsort(scores[indices])]
    for i in [int(x) for x in reversed(indices)]:
        print(scores[i], images[i])
        cetteImage=images[i]
        cetteImage=cetteImage[:-2]
def extract_features_dir(model, directory):
    # extract features from each photo
    all_features = []
    for name in listdir(directory):
        # load an image from file
        filename = path.join(directory, name)
        features = extract_features_file(model, filename)
        features = np.squeeze(features)
        all_features.append(features)
        print('>%s' % name)
    return all_features
Exemplo n.º 5
0
def generate_caption(indIm, names, model, image_model, caption_representations, texts, n=10):
    # generate image representation for new image
    image_filename = dataset_path + names[indIm]
    print(names[indIm])
    y_true=indicesByID(indIm, names)
    features = extract_features_file(model, image_filename)
    image_representation = image_model.predict(np.array([features]))
    # compute score of all captions in the dataset
    scores = np.dot(caption_representations,image_representation.T).flatten()
    # compute indices of n best captions
    indices = np.argpartition(scores, -n)[-n:]
    indices = indices[np.argsort(scores[indices])]
    # display them
    y_pred=[]
    for i in [int(x) for x in reversed(indices)]:
        print(i, texts[i])
Exemplo n.º 6
0
def searchTextFromImageAndText(image_filename,
                               model,
                               image_model,
                               vocab,
                               caption,
                               caption_model,
                               ImageAndTextRepresentation,
                               texts,
                               features,
                               n=30):
    features = extract_features_file(model, image_filename)
    imageEncoded = image_model.predict(np.array([features]))
    textEncoded = caption_model.predict(preprocess_texts([caption], vocab))
    imageAndTextEncoded = np.concatenate((imageEncoded[0], textEncoded[0]))
    scores = np.dot(ImageAndTextRepresentation,
                    imageAndTextEncoded.T).flatten()
    indices = np.argpartition(scores, -n)[-n:]
    indices = indices[np.argsort(scores[indices])]
    for i in [int(x) for x in reversed(indices)]:
        print(scores[i], texts[i])