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

# + colab={} colab_type="code" id="9pJf0euIbbKq"
import logging
import os
import sys

import numpy as np
from matplotlib.pyplot import imshow
# %matplotlib inline

import torch

import concreteness, mirflickr

# +
# Setup logging. We'll use DEBUG as logging level by default, but feel free to bump it down.
logging_level = logging.DEBUG
logging_format = '%(asctime)s [%(levelname)s] %(message)s'
logging.basicConfig(level=logging_level, format=logging_format, stream=sys.stdout)

log = logging.getLogger()

# + colab={} colab_type="code" id="vu_KqmU2DUld"
# Edit these to point to your dataset and cache directories
DATASET_DIR = "/home/victor/data/mirflickr/"
CACHE_DIR = "/home/victor/ml/concreteness/data/cache"

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

# Based on the values above, we'll figure out other specific paths
images_directory = os.path.join(DATASET_DIR, "images")
tags_directory = os.path.join(DATASET_DIR, "tags")
vectors_file = os.path.join(CACHE_DIR, "vectors.pt")
annoy_index_file = os.path.join(CACHE_DIR, "index.ann")

# + colab={} colab_type="code" id="576z96UveheO"
# Number of NNS to use during search
K = 50
# -

# Load the Mirflickr dataset
dataset = mirflickr.MirflickrImagesDataset(images_directory, tags_directory,
                                           transform=concreteness.get_tensor_for_image)

# Using a ResNet50, build image vectors for the dataset
if os.path.isfile(vectors_file):
    img_vectors = torch.load(vectors_file)
    log.info("Loaded image vectors.")
else:
    log.info("Building image vectors.")
    img_vectors = concreteness.build_image_vectors(dataset)
    log.info("Built image vectors.")
    torch.save(img_vectors, vectors_file)
    log.info("Saved image vectors to %s", vectors_file)

# Build NNS
nns = concreteness.build_nns(img_vectors, K, annoy_index_file=annoy_index_file)


# Add helper to show images. We'll use it to verify our computed NNS
def show_image(dataset, image_index):
    image = dataset.get_pil_image(image_index)
    imshow(np.asarray(image))


# We'll pick an image and display it along with two of its nearest neighbors.
# Feel free to change the image_index to see other examples.
image_index = 1
original_image, neighbor, another_neighbor = list(nns[image_index])[image_index:image_index + 3]

show_image(dataset, image_index)

show_image(dataset, neighbor)

show_image(dataset, another_neighbor)

log.info("Computing concreteness.")
concreteness_dict = concreteness.get_concreteness(dataset, nns, K)
sorted_concreteness = sorted(concreteness_dict.items(), key=lambda x: x[1], reverse=True)
log.info("Done!")

sorted_concreteness

cheetah_images = dataset.images_by_tag['cheetah']
cheetah_images

show_image(dataset, 6725)

show_image(dataset, 18498)

StarcoderdataJupyterScriptsDedupFiltered