|
| 1 | +<a href="https://github.com/facebookresearch/perception_models?tab=readme-ov-file#perception-encoder-pe" target="_blank">Perception Encoder</a> is a computer vision model that can measure the similarity between text and images, as well as compute useful text and image embeddings. |
| 2 | + |
| 3 | +Perception Encoder embeddings can be used for, among other things: |
| 4 | + |
| 5 | +- Image classification |
| 6 | +- Image clustering |
| 7 | +- Gathering images for model training that are sufficiently dissimilar from existing samples |
| 8 | +- Content moderation |
| 9 | + |
| 10 | +With Inference, you can calculate PE embeddings for images and text in real-time. |
| 11 | + |
| 12 | +In this guide, we will show: |
| 13 | + |
| 14 | +1. How to classify video frames with PE in real time, and; |
| 15 | +2. How to calculate PE image and text embeddings for use in clustering and comparison. |
| 16 | + |
| 17 | +## How can I use PE in `inference`? |
| 18 | + |
| 19 | +- directly from `inference[transformers]` package, integrating the model directly into your code |
| 20 | +- using `inference` HTTP API (hosted locally, or on the Roboflow platform), integrating via HTTP protocol |
| 21 | + - using `inference-sdk` package (`pip install inference-sdk`) and [`InferenceHTTPClient`](../inference_helpers/inference_sdk.md) |
| 22 | + - creating custom code to make HTTP requests (see [API Reference](/api)) |
| 23 | + |
| 24 | +## Supported PE versions |
| 25 | + |
| 26 | +- `perception_encoder/PE-Core-B16-224` |
| 27 | +- `perception_encoder/PE-Core-L14-336` |
| 28 | +- `perception_encoder/PE-Core-G14-448` |
| 29 | + |
| 30 | +We currently only support the CLIP interface for PE models. We don't support the language or spatial aligned models yet. |
| 31 | + |
| 32 | +## Classify Video Frames |
| 33 | + |
| 34 | +With PE, you can classify images and video frames without training a model. This is because PE has been pre-trained to recognize many different objects. |
| 35 | + |
| 36 | +To use PE to classify video frames, you need a prompt. In the example below, we will use the prompt "an image of a guy with a beard holding a can of sparkling water". |
| 37 | + |
| 38 | +We can compare the similarity of the prompt to each video frame and use that to classify the video frame. |
| 39 | + |
| 40 | +Below is a demo of PE classifying video frames in real time. The code for the example is below the image. |
| 41 | + |
| 42 | +<img src="https://storage.googleapis.com/com-roboflow-marketing/inference/pe2.png" alt="Perception Encoder demo" width="800"/> |
| 43 | + |
| 44 | + |
| 45 | +First, install the Inference transformers extension: |
| 46 | + |
| 47 | +``` |
| 48 | +pip install "inference[transformers]" |
| 49 | +``` |
| 50 | + |
| 51 | +Then, create a new Python file and add the following code: |
| 52 | + |
| 53 | +```python |
| 54 | +import cv2 |
| 55 | +import inference |
| 56 | +from inference.core.utils.postprocess import cosine_similarity |
| 57 | + |
| 58 | +from inference.models import PerceptionEncoder |
| 59 | +pe = PerceptionEncoder(model_id="perception_encoder/PE-Core-B16-224", device="mps") # `model_id` has default, but here is how to test other versions |
| 60 | + |
| 61 | +prompt = "an image of a guy with a beard holding a can of sparkling water" |
| 62 | +text_embedding = pe.embed_text(prompt) |
| 63 | + |
| 64 | +def render(result, image): |
| 65 | + # get the cosine similarity between the prompt & the image |
| 66 | + similarity = cosine_similarity(result["embeddings"][0], text_embedding[0]) |
| 67 | + |
| 68 | + # scale the result to 0-100 based on heuristic (~the best & worst values I've observed) |
| 69 | + range = (0.15, 0.40) |
| 70 | + similarity = (similarity-range[0])/(range[1]-range[0]) |
| 71 | + similarity = max(min(similarity, 1), 0)*100 |
| 72 | + |
| 73 | + # print the similarity |
| 74 | + text = f"{similarity:.1f}%" |
| 75 | + cv2.putText(image, text, (10, 310), cv2.FONT_HERSHEY_SIMPLEX, 12, (255, 255, 255), 30) |
| 76 | + cv2.putText(image, text, (10, 310), cv2.FONT_HERSHEY_SIMPLEX, 12, (206, 6, 103), 16) |
| 77 | + |
| 78 | + # print the prompt |
| 79 | + cv2.putText(image, prompt, (20, 1050), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 10) |
| 80 | + cv2.putText(image, prompt, (20, 1050), cv2.FONT_HERSHEY_SIMPLEX, 2, (206, 6, 103), 5) |
| 81 | + |
| 82 | + # display the image |
| 83 | + cv2.imshow("PE", image) |
| 84 | + cv2.waitKey(1) |
| 85 | + |
| 86 | +# start the stream |
| 87 | +inference.Stream( |
| 88 | + source="webcam", |
| 89 | + model=pe, |
| 90 | + output_channel_order="BGR", |
| 91 | + use_main_thread=True, |
| 92 | + on_prediction=render |
| 93 | +) |
| 94 | +``` |
| 95 | + |
| 96 | +Run the code to use Perception Encoder on your webcam. |
| 97 | + |
| 98 | +**Note:** The model will take a minute or two to load. You will not see output while the model is loading. |
| 99 | + |
| 100 | +## API Compatibility |
| 101 | + |
| 102 | +The Perception Encoder model uses the **same API as CLIP**. This means you can use all the same methods and request/response formats as you would with CLIP, including `embed_text`, `embed_image`, and `compare`. |
| 103 | + |
| 104 | +For more details and advanced usage, see the [CLIP documentation](./clip.md). |
0 commit comments