Code Monkey home page Code Monkey logo

image_embeddings's Introduction

image_embeddings

pypi ci

Using efficientnet to provide embeddings for retrieval. Read the blog post at https://medium.com/@rom1504/image-embeddings-ed1b194d113e

Why this repo ? Embeddings are a widely used technique that is well known in scientific circles. But it seems to be underused and not very well known for most engineers. I want to show how easy it is to represent things as embeddings, and how many application this unlocks. Checkout the demo first!

knn example

Workflow

  1. download some pictures
  2. run inference on them to get embeddings
  3. simple knn example, to understand what's the point : click on some pictures and see KNN

Simple Install

Run pip install image_embeddings

Example workflow

  1. run image_embeddings save_examples_to_folder --images_count=1000 --output_folder=tf_flower_images, this will retrieve 1000 image files from https://www.tensorflow.org/datasets/catalog/tf_flowers (but you can also pick any other dataset)
  2. produce tf records with image_embeddings write_tfrecord --image_folder=tf_flower_images --output_folder=tf_flower_tf_records --shards=10
  3. run the inference with image_embeddings run_inference --tfrecords_folder=tf_flower_tf_records --output_folder=tf_flower_embeddings
  4. run a random knn search on them image_embeddings random_search --path=tf_flower_embeddings

Optionally if you want to use the embeddings in numpy (in other languages), run image_embeddings embeddings_to_numpy --input_path=tf_flower_embeddings --output_path=tf_flower_numpy. In particular this can be used in the web demo

$ image_embeddings random_search --path=tf_flower_embeddings
image_roses_261
160.83 image_roses_261
114.36 image_roses_118
102.77 image_roses_537
92.95 image_roses_659
88.49 image_roses_197

Explore the Simple notebook for more details.

You can try it locally or try it in colab

The From scratch notebook provides an explanation on how to build this from scratch.

API

image_embeddings.downloader

Downloader from tensorflow datasets. Any other set of images could be used instead

image_embeddings.downloader.save_examples_to_folder(output_folder, images_count=1000, dataset="tf_flowers")

Save https://www.tensorflow.org/datasets/catalog/tf_flowers to folder Also works with other tf datasets

image_embeddings.inference

Create tf recors from images files, and apply inference with an efficientnet model. Other models could be used.

image_embeddings.inference.write_tfrecord(image_folder, output_folder, num_shards=100)

Write tf records from an image folders

image_embeddings.inference.run_inference(tfrecords_folder, output_folder, batch_size=1000)

Run inference on provided tf records and save to folder the embeddings

image_embeddings.knn

Convenience methods to read, build indices and apply search on them. These methods are provided as example. Use faiss directly for bigger datasets.

image_embeddings.knn.read_embeddings(path)

Run embeddings from path and return a tuple with

  • embeddings as a numpy matrix
  • an id to name dictionary
  • a name to id dictionary

image_embeddings.knn.build_index(emb)

Build a simple faiss inner product index using the provided matrix of embeddings

image_embeddings.knn.search(index, id_to_name, emb, k=5)

Search the query embeddings and return an array of (distance, name) images

image_embeddings.knn.display_picture(image_path, image_name)

Display one picture from the given path and image name in jupyter

image_embeddings.knn.display_results(image_path, results)

Display the results from search method

image_embeddings.knn.random_search(path)

Load the embeddings, apply a random search on them and display the result

image_embeddings.knn.embeddings_to_numpy(input_path, output_folder)

Load the embeddings from the input folder as parquet and save them as

  • json for the id -> name mapping
  • numpy for the embeddings

Particularly useful to read the embeddings from other languages

Advanced Installation

Prerequisites

Make sure you use python>=3.6 and an up-to-date version of pip and setuptools

python --version
pip install -U pip setuptools

It is recommended to install image_embeddings in a new virtual environment. For example

python3 -m venv image_embeddings_env
source image_embeddings_env/bin/activate
pip install -U pip setuptools
pip install image_embeddings

Using Pip

pip install image_embeddings

From Source

First, clone the image_embeddings repo on your local machine with

git clone https://github.com/rom1504/image_embeddings.git
cd image_embeddings
make install

To install development tools and test requirements, run

make install-dev

Test

To run unit tests in your current environment, run

make test

To run lint + unit tests in a fresh virtual environment, run

make venv-lint-test

Lint

To run black --check:

make lint

To auto-format the code using black

make black

Tasks

image_embeddings's People

Contributors

dependabot[bot] avatar rom1504 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

image_embeddings's Issues

batch mode and multithread

Hello,

Thx for the package ?
How to setup batch mode and multithread easily ?
(inference on CPU).

  1. Think one should be able load some small fine tuner head for quick fine tuning
    and save the model back.

image extension .jpeg hardcoded

Hi

You have hardcoded extension .jpeg which makes it harder to try other images

<ipython-input-42-a444c2d0177a> in <module>
      1 p=1
      2 print(id_to_name[p])
----> 3 image_embeddings.knn.display_picture(path_images, id_to_name[p])
      4 results = image_embeddings.knn.search(index, id_to_name, embeddings[p])
      5 image_embeddings.knn.display_results(path_images, results)

~/miniconda3/envs/tracker/lib/python3.8/site-packages/image_embeddings/knn/knn.py in display_picture(image_path, image_name)
     54 
     55 def display_picture(image_path, image_name):
---> 56     display(Image(filename=f"{image_path}/{image_name}.jpeg"))
     57 
     58 

inference

should come with :

  • decent setup.py + requirements.txt setup
  • a notebook for usage
  • some instructions in the readme
  • how to make tfrecords for it (read/write) : include mapping (id vs string)

Flexibility in choosing models for image embeddings

Hi,
In the API: image_embeddings.inference.run_inference(tfrecords_folder, output_folder, batch_size=1000), can we have a model parameter such that we can use the same API to get embeddings from any model we choose.
The default model can be EfficientNetB0 and we can modify it to compare between different versions.
(Label: Enhancement)

ValueError: Expect x to be a non-empty array or dataset.

I am trying to create an embedding for some google images I downloaded. This is my structure:

image

When I execute this

image_embeddings.inference.write_tfrecord(image_folder="tmp/test_images",
                                          output_folder="tmp/test_tensors",
                                          num_shards=10)

image_embeddings.inference.run_inference(tfrecords_folder="tmp/test_tensors",
                                         output_folder="tmp/test_output",
                                         batch_size=1000)

[id_to_name2, name_to_id2, embeddings2] = image_embeddings.knn.read_embeddings("tmp/test_output")
index2 = image_embeddings.knn.build_index(embeddings2)

I get

ValueError: Expect x to be a non-empty array or dataset.

Althought it fails, files are generated:

image

But if I try to search with that embedding in another index of images that I have,

results = image_embeddings.knn.search(another_index, id_to_name2, embeddings2[0], k=1)
results = [i for i in results if i[1]!=id_to_name2[p]]
image_embeddings.knn.display_results(JPEG_FOLDER, results)

I get:

KeyError: 36

I tried different numbers of shards and different numbers of batches. None of them work, what could the reason be?

Full traces:

ValueError                                Traceback (most recent call last)
<ipython-input-40-405d145f78be> in <module>()
     15 image_embeddings.inference.run_inference(tfrecords_folder="tmp/test_tensors",
     16                                          output_folder="tmp/test_output",
---> 17                                          batch_size=1000)
     18 
     19 [id_to_name2, name_to_id2, embeddings2] = image_embeddings.knn.read_embeddings("tmp/test_output")

3 frames
/usr/local/lib/python3.7/dist-packages/image_embeddings/inference/inference.py in run_inference(tfrecords_folder, output_folder, batch_size)
    154     Path(output_folder).mkdir(parents=True, exist_ok=True)
    155     model = EfficientNetB0(weights="imagenet", include_top=False, pooling="avg")
--> 156     tfrecords_to_write_embeddings(tfrecords_folder, output_folder, model, batch_size)

/usr/local/lib/python3.7/dist-packages/image_embeddings/inference/inference.py in tfrecords_to_write_embeddings(tfrecords_folder, output_folder, model, batch_size)
     90     for shard_id, tfrecord in enumerate(tfrecords):
     91         shard = read_tfrecord(tfrecord)
---> 92         embeddings = images_to_embeddings(model, shard, batch_size)
     93         print("")
     94         print("Shard " + str(shard_id) + " done after " + str(int(time.time() - start)) + "s")

/usr/local/lib/python3.7/dist-packages/image_embeddings/inference/inference.py in images_to_embeddings(model, dataset, batch_size)
    117 
    118 def images_to_embeddings(model, dataset, batch_size):
--> 119     return model.predict(dataset.batch(batch_size).map(lambda image_raw, image_name: image_raw), verbose=1)
    120 
    121 

/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
   1740             callbacks.on_predict_batch_end(end_step, {'outputs': batch_outputs})
   1741       if batch_outputs is None:
-> 1742         raise ValueError('Expect x to be a non-empty array or dataset.')
   1743       callbacks.on_predict_end()
   1744     all_outputs = nest.map_structure_up_to(batch_outputs, concat, outputs)

ValueError: Expect x to be a non-empty array or dataset.
KeyError                                  Traceback (most recent call last)
<ipython-input-58-ad901d8c8c41> in <module>()
----> 1 results = image_embeddings.knn.search(index, id_to_name2, embeddings2[0], k=1)
      2 results = [i for i in results if i[1]!=id_to_name2[p]]
      3 image_embeddings.knn.display_results(JPEG_FOLDER, results)

1 frames
/usr/local/lib/python3.7/dist-packages/image_embeddings/knn/knn.py in search(index, id_to_name, emb, k)
     50 def search(index, id_to_name, emb, k=5):
     51     D, I = index.search(np.expand_dims(emb, 0), k)  # actual search
---> 52     return list(zip(D[0], [id_to_name[x] for x in I[0]]))
     53 
     54 

/usr/local/lib/python3.7/dist-packages/image_embeddings/knn/knn.py in <listcomp>(.0)
     50 def search(index, id_to_name, emb, k=5):
     51     D, I = index.search(np.expand_dims(emb, 0), k)  # actual search
---> 52     return list(zip(D[0], [id_to_name[x] for x in I[0]]))
     53 
     54 

KeyError: 36

Video datasets

Hello,

I would like to get the embeddings for each frame of a video dataset.
I'm using CV2 to get the frames, so I end up with frames of (720, 1280, 3). Being the first two dimensions the resolution and 3 the color channels.

How can I get the embedding of each frame?
I don't see a function to load new datasets in the .ipynb from scratch.

Thanks

In some folders I am getting this error OverflowError: cannot convert float infinity to integer

Hi @rom1504

First of all thanks for the great work that you've done, I am facing this error is some of the folders that I am getting running similarity search in. Please help

 File "/home/cctvserver/Documents/Work/SimilaritySearch/Embeddings/image_embeddings-master/image_embeddings/inference/inference.py", line 92, in tfrecords_to_write_embeddings
    embeddings = images_to_embeddings(model, shard, batch_size)
  File "/home/cctvserver/Documents/Work/SimilaritySearch/Embeddings/image_embeddings-master/image_embeddings/inference/inference.py", line 119, in images_to_embeddings
    return model.predict(dataset.batch(batch_size).map(lambda image_raw, image_name: image_raw), verbose=1)
  File "/home/cctvserver/miniconda3/envs/tracker/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 130, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/home/cctvserver/miniconda3/envs/tracker/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1613, in predict
    callbacks.on_predict_end()
  File "/home/cctvserver/miniconda3/envs/tracker/lib/python3.8/site-packages/tensorflow/python/keras/callbacks.py", line 582, in on_predict_end
    callback.on_predict_end(logs)
  File "/home/cctvserver/miniconda3/envs/tracker/lib/python3.8/site-packages/tensorflow/python/keras/callbacks.py", line 979, in on_predict_end
    self._finalize_progbar(logs)
  File "/home/cctvserver/miniconda3/envs/tracker/lib/python3.8/site-packages/tensorflow/python/keras/callbacks.py", line 1026, in _finalize_progbar
    self.progbar.update(self.seen, list(logs.items()), finalize=True)
  File "/home/cctvserver/miniconda3/envs/tracker/lib/python3.8/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 581, in update
    numdigits = int(np.log10(self.target)) + 1
OverflowError: cannot convert float infinity to integer

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.