Spaces:
Sleeping
Sleeping
Upload README.md
Browse files
README.md
CHANGED
|
@@ -1,13 +1,106 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: CLIP Search Edit
|
| 3 |
-
emoji: π
|
| 4 |
-
colorFrom: gray
|
| 5 |
-
colorTo: purple
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 5.49.1
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
license: mit
|
| 11 |
-
---
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: CLIP Search Edit
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.49.1
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# CLIP Search & Edit Engine
|
| 14 |
+
|
| 15 |
+
A multimodal application combining semantic image search with lightweight, text-guided image editing. This project utilizes OpenAI's CLIP model for retrieving images from the Flickr30k dataset and a custom FiLM-conditioned U-Net for performing style transfer based on text prompts.
|
| 16 |
+
|
| 17 |
+
## π Features
|
| 18 |
+
|
| 19 |
+
* **Semantic Image Search**: Search through thousands of images in the Flickr30k dataset using natural language queries (e.g., "a dog on a boat", "neon lights").
|
| 20 |
+
* **Text-Guided Image Editing**: Apply artistic styles (Sketch, Van Gogh, Cyberpunk) to images using a lightweight U-Net architecture conditioned on CLIP text embeddings.
|
| 21 |
+
* **Efficiency**: Includes benchmarking tools to measure model FLOPs, parameters, and inference speed.
|
| 22 |
+
* **Streamed Dataset**: Uses Deep Lake to stream dataset images, eliminating the need for massive local downloads.
|
| 23 |
+
|
| 24 |
+
## π οΈ Installation
|
| 25 |
+
|
| 26 |
+
### Prerequisites
|
| 27 |
+
* Python 3.8+
|
| 28 |
+
* CUDA-enabled GPU (recommended for faster indexing and inference)
|
| 29 |
+
* **Git Xet**: Required for cloning large files from Hugging Face.
|
| 30 |
+
|
| 31 |
+
### Steps
|
| 32 |
+
|
| 33 |
+
1. **Setup Git Xet and Clone:**
|
| 34 |
+
To clone the repository from Hugging Face Spaces, ensure `git-xet` is installed to handle large files.
|
| 35 |
+
|
| 36 |
+
<pre><code># Install git-xet (macOS example)
|
| 37 |
+
brew install git-xet
|
| 38 |
+
git xet install
|
| 39 |
+
|
| 40 |
+
# Clone the repository
|
| 41 |
+
git clone https://huggingface.co/spaces/CISC473-Group19/CLIP-Search-Edit</code></pre>
|
| 42 |
+
|
| 43 |
+
*Alternative: Clone without large files (pointers only):*
|
| 44 |
+
<pre><code>GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/spaces/CISC473-Group19/CLIP-Search-Edit</code></pre>
|
| 45 |
+
|
| 46 |
+
2. **Install dependencies:**
|
| 47 |
+
<pre><code>cd CLIP-Search-Edit
|
| 48 |
+
pip install -r requirements.txt</code></pre>
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
## βοΈ Setup & Usage
|
| 52 |
+
|
| 53 |
+
### 1. Build the Search Index
|
| 54 |
+
Before using the search functionality, you must index the dataset. This script downloads image features from Flickr30k and saves them locally.
|
| 55 |
+
|
| 56 |
+
<pre><code>python indexer.py</code></pre>
|
| 57 |
+
|
| 58 |
+
* **Note:** This will create a file named `flickr_embeddings.pt` containing the CLIP embeddings for the images.
|
| 59 |
+
* **Config:** You can adjust `INDEX_LIMIT` in `indexer.py` to change the number of images indexed (default is 5,000).
|
| 60 |
+
|
| 61 |
+
### 2. Download/Place Model Weights
|
| 62 |
+
The image editing module requires pre-trained U-Net weights. Ensure the following files are in your root directory (or update the paths in `app.py`):
|
| 63 |
+
* `unet_charcoal-sketch.pth`
|
| 64 |
+
* `unet_van-gogh-painting.pth`
|
| 65 |
+
* `unet_neon-cyberpunk.pth`
|
| 66 |
+
|
| 67 |
+
### 3. Run the Application
|
| 68 |
+
Launch the Gradio web interface:
|
| 69 |
+
|
| 70 |
+
<pre><code>python app.py</code></pre>
|
| 71 |
+
|
| 72 |
+
* Open the link provided in the terminal (usually `http://127.0.0.1:7860`).
|
| 73 |
+
* **Tab 1 (Retrieval):** Enter a text query to find semantically related images.
|
| 74 |
+
* **Tab 2 (Image Editing):** Upload an image and select a style to transform it.
|
| 75 |
+
|
| 76 |
+
## π Benchmarking & Evaluation
|
| 77 |
+
|
| 78 |
+
* **Measure Efficiency:**
|
| 79 |
+
Run the benchmark script to calculate the U-Net model's parameters, FLOPs, and FPS on CPU/GPU.
|
| 80 |
+
<pre><code>python benchmark_efficiency.py</code></pre>
|
| 81 |
+
|
| 82 |
+
* **Measure Search Recall:**
|
| 83 |
+
Calculate retrieval metrics (R@1, R@5, R@10) for the indexed dataset.
|
| 84 |
+
<pre><code>python measure_recall.py</code></pre>
|
| 85 |
+
|
| 86 |
+
## π Project Structure
|
| 87 |
+
|
| 88 |
+
| File | Description |
|
| 89 |
+
| :--- | :--- |
|
| 90 |
+
| `app.py` | Main entry point. Launches the Gradio UI for Search and Editing. |
|
| 91 |
+
| `indexer.py` | Generates CLIP embeddings for the dataset and saves them to `flickr_embeddings.pt`. |
|
| 92 |
+
| `dataset.py` | Handles streaming images from the Deep Lake Flickr30k dataset. |
|
| 93 |
+
| `cnn1.py` | Defines the lightweight `UNet` architecture with FiLM layers for text conditioning. |
|
| 94 |
+
| `clip_styler.py` | Logic for applying style transfer using the U-Net and CLIP models. |
|
| 95 |
+
| `style_net.py` | Alternative/Legacy ResNet-based style network architecture. |
|
| 96 |
+
| `util.py` | Utility functions for image normalization, loading, and loss calculations. |
|
| 97 |
+
| `requirements.txt` | List of Python dependencies. |
|
| 98 |
+
|
| 99 |
+
## π§ Model Architecture
|
| 100 |
+
|
| 101 |
+
The editing module uses a custom **U-Net** (`cnn1.py`) enhanced with **FiLM (Feature-wise Linear Modulation)** layers.
|
| 102 |
+
1. **Encoder:** Extracts image features.
|
| 103 |
+
2. **FiLM Layers:** Modulate the feature maps based on the CLIP text embedding of the target style (e.g., "charcoal sketch").
|
| 104 |
+
3. **Decoder:** Reconstructs the stylized image.
|
| 105 |
+
|
| 106 |
+
This approach allows for fast, text-controllable style transfer without requiring heavy diffusion models.
|