GithubHelp home page GithubHelp logo

clip_test's Introduction

CLIP test

This toy web app is leveraging the text-image CLIP model and a Flask web app coded by ThorkildFregi (thanks!).

Dependencies: flask, clip, PIL, torch, numpy

First, launch:

>python3 recurse.py -f static/myImages > static/myImages_directory.txt

if you want to process a folder of images named myImages (subfolders may be used within this folder) stored in the static application folder. It will generate the directory files list and a report (as both text files).

Then the CLIP embeddings are computed (CUDA or CPU processing is supported) with:

>python3 model.py -f myImages

The embeddings are saved in a Torch tensor named after the folder name (in our example, myImages_torch.pt).

Note: join.py can be used to concatenate tensors if you need to process large volumes of images within memory constraint.

Then create an env variable to inform the flask app about your images folder:

export FLASK_ARG="myImages"

Finally, launch the web app:

>flask --app main.py --debug run

and open this URL http://127.0.0.1:5000 in your browser.

Note: the whole workflow can be ran using the bash script run.sh. If the image folder is changed, the entire workflow must be restarted.

The web app displays a random selection of images and a prompt field.

The web app

If the file static/myImages_urls.txt exists, images are linked to their URL. Images with no URL can be mixed (using the character - as URL).

Classification scenario

For this use case, we want to use CLIP as a zero-shot classifier. The images types (classes) we want to classify are described in the static/myImages_labels.csv file as textual captions, e.g.:

Crosswords,a crossword grid or a chess game or a word game printed in a heritage newspaper
Drawing,a monochrome drawing printed in a heritage newspaper
Map,a monochrome map printed in a heritage newspaper
Photo,a black and white picture printed in a heritage newspaper
...

These class names will be listed to the user on the home page and the model will output probabilities against these captions.

The results list shows the most likely images for the requested class, the probability and the second most likely class. In this example, we are looking for drawings in newspapers. If images are sorted into subfolders based on class names, a confusion matrix will be calculated and displayed after the results list.

Classification

Note: if the captions are changed, the flask app must be launched again.

Information retrieval scenario

In this scenario, a free-form query is entered and the result list displays the images ranked in order of probability. In the example below, we are looking for cartoons of people in the street.

Classification

clip_test's People

Contributors

altomator avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

natachagrim

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.