File size: 8,775 Bytes
e2c8c12
8bcf79a
4ef6571
 
 
 
e2c8c12
4ef6571
e2c8c12
321374b
8bcf79a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
---
title: Dressify - Production-Ready Outfit Recommendation
emoji: πŸ†
colorFrom: purple
colorTo: green
sdk: gradio
sdk_version: "5.44.1"
app_file: app.py
pinned: false
---

# Dressify - Production-Ready Outfit Recommendation System

A **research-grade, self-contained** outfit recommendation service that automatically downloads the Polyvore dataset, trains state-of-the-art models, and provides a sophisticated Gradio interface for wardrobe uploads and outfit generation.

## πŸš€ Features

- **Self-Contained**: No external dependencies or environment variables needed
- **Auto-Dataset Preparation**: Downloads and processes Stylique/Polyvore dataset automatically
- **Research-Grade Models**: ResNet50 item embedder + ViT outfit compatibility encoder
- **Advanced Training**: Triplet loss with semi-hard negative mining, mixed precision
- **Production UI**: Gradio interface with wardrobe upload, outfit preview, and JSON export
- **REST API**: FastAPI endpoints for embedding and composition
- **Auto-Bootstrap**: Background training and model reloading

## πŸ—οΈ Architecture

### Data Pipeline
1. **Dataset Download**: Automatically fetches Stylique/Polyvore from HF Hub
2. **Image Processing**: Unzips images.zip and organizes into structured format
3. **Split Generation**: Creates train/val/test splits (70/15/15) with deterministic RNG
4. **Triplet Mining**: Generates item triplets and outfit triplets for training

### Model Architecture
- **Item Embedder**: ResNet50 + projection head β†’ 512D L2-normalized embeddings
- **Outfit Encoder**: Transformer encoder β†’ outfit-level compatibility scoring
- **Loss Functions**: Triplet margin loss with cosine distance and semi-hard mining

### Training Pipeline
- Mixed precision training with channels-last memory format
- Automatic checkpointing and best model saving
- Validation metrics and early stopping
- Background training with model reloading

## πŸš€ Quick Start

### 1. Deploy to Hugging Face Space
```bash
# Upload this entire folder as a Space
# The system will automatically:
# - Download Polyvore dataset
# - Prepare splits and triplets
# - Train models (if no checkpoints exist)
# - Launch Gradio UI + FastAPI
```

### 2. Local Development
```bash
# Clone and setup
git clone <repo>
cd recomendation
pip install -r requirements.txt

# Launch app (auto-downloads dataset)
python app.py
```

## πŸ“ Project Structure

```
recomendation/
β”œβ”€β”€ app.py                       # FastAPI + Gradio app (main entry)
β”œβ”€β”€ inference.py                 # Inference service with model loading
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ resnet_embedder.py      # ResNet50 + projection head
β”‚   └── vit_outfit.py           # Transformer encoder for outfits
β”œβ”€β”€ data/
β”‚   └── polyvore.py             # PyTorch datasets for training
β”œβ”€β”€ scripts/
β”‚   └── prepare_polyvore.py     # Dataset preparation and splits
β”œβ”€β”€ utils/
β”‚   β”œβ”€β”€ data_fetch.py           # HF dataset downloader
β”‚   β”œβ”€β”€ transforms.py            # Image transforms
β”‚   └── export.py               # Model export utilities
β”œβ”€β”€ train_resnet.py              # ResNet training script
β”œβ”€β”€ train_vit_triplet.py        # ViT triplet training script
β”œβ”€β”€ requirements.txt             # Dependencies
β”œβ”€β”€ Dockerfile                   # Container deployment
└── README.md                    # This file
```

## 🎯 Model Performance

### Expected Metrics (Research-Grade)
- **Item Embedder**: Triplet accuracy > 85%, validation loss < 0.1
- **Outfit Encoder**: Compatibility AUC > 0.8, precision > 0.75
- **Inference Speed**: < 100ms per outfit on GPU, < 500ms on CPU

### Training Time
- **Item Embedder**: ~2-4 hours on L4 GPU (full dataset)
- **Outfit Encoder**: ~1-2 hours on L4 GPU (with precomputed embeddings)

## 🎨 Gradio Interface

### Features
- **Wardrobe Upload**: Multi-file drag & drop with previews
- **Outfit Generation**: Top-N recommendations with compatibility scores
- **Preview Stitching**: Visual outfit composition
- **JSON Export**: Structured data for integration
- **Training Monitor**: Real-time training progress and metrics
- **Status Dashboard**: Bootstrap and training status

### Usage Flow
1. Upload wardrobe images (minimum 4 items recommended)
2. Set context (occasion, weather, style preferences)
3. Generate outfits (default: top-3)
4. View stitched previews and download JSON

## πŸ”Œ API Endpoints

### FastAPI Server
```bash
# Health check
GET /health

# Image embedding
POST /embed
{
  "images": ["base64_image_1", "base64_image_2"]
}

# Outfit composition
POST /compose
{
  "items": [
    {"id": "item_1", "embedding": [0.1, 0.2, ...], "category": "upper"},
    {"id": "item_2", "embedding": [0.3, 0.4, ...], "category": "bottom"}
  ],
  "context": {"occasion": "casual", "num_outfits": 3}
}

# Model artifacts
GET /artifacts
```

## πŸš€ Deployment

### Hugging Face Space
1. Upload this folder as a Space
2. Set Space type to "Gradio"
3. The system auto-bootstraps on first run
4. Models train automatically if no checkpoints exist
5. UI becomes available once training completes

### Docker
```bash
# Build and run
docker build -t dressify .
docker run -p 7860:7860 -p 8000:8000 dressify

# Access
# Gradio: http://localhost:7860
# FastAPI: http://localhost:8000
```

## πŸ“ˆ Training & Evaluation

### Training Commands
```bash
# Quick training (3 epochs each)
# This runs automatically on Space startup

# Manual training
python train_resnet.py --data_root data/Polyvore --epochs 20
python train_vit_triplet.py --data_root data/Polyvore --epochs 30
```

### Evaluation Metrics
- **Item Level**: Triplet accuracy, embedding quality, retrieval metrics
- **Outfit Level**: Compatibility AUC, precision/recall, diversity scores
- **System Level**: Inference latency, memory usage, throughput

## πŸ”¬ Research Features

### Advanced Training
- Semi-hard negative mining for better triplet selection
- Mixed precision training with autocast
- Channels-last memory format for CUDA optimization
- Curriculum learning with difficulty progression

### Model Variants
- **Standard**: ResNet50 + medium transformer (balanced)
- **Research**: ResNet101 + large transformer (high performance)

## 🀝 Integration

### Next.js + Supabase
```typescript
// Upload wardrobe
const uploadWardrobe = async (images: File[]) => {
  const formData = new FormData();
  images.forEach(img => formData.append('images', img));
  
  const response = await fetch('/api/wardrobe/upload', {
    method: 'POST',
    body: formData
  });
  
  return response.json();
};

// Generate outfits
const generateOutfits = async (wardrobe: WardrobeItem[]) => {
  const response = await fetch('/api/outfits/generate', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ wardrobe, context: { occasion: 'casual' } })
  });
  
  return response.json();
};
```

### Database Schema
```sql
-- User wardrobe table
CREATE TABLE user_wardrobe (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID REFERENCES auth.users(id),
  image_url TEXT NOT NULL,
  category TEXT,
  embedding VECTOR(512),
  created_at TIMESTAMP DEFAULT NOW()
);

-- Outfit recommendations
CREATE TABLE outfit_recommendations (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID REFERENCES auth.users(id),
  outfit_items JSONB NOT NULL,
  compatibility_score FLOAT,
  context JSONB,
  created_at TIMESTAMP DEFAULT NOW()
);

-- RLS policies
ALTER TABLE user_wardrobe ENABLE ROW LEVEL SECURITY;
ALTER TABLE outfit_recommendations ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Users can view own wardrobe" ON user_wardrobe
  FOR SELECT USING (auth.uid() = user_id);

CREATE POLICY "Users can insert own wardrobe" ON user_wardrobe
  FOR INSERT WITH CHECK (auth.uid() = user_id);
```

## πŸ§ͺ Testing

### Smoke Tests
```bash
# Dataset preparation
python scripts/prepare_polyvore.py --root data/Polyvore --random_split

# Training loops
python train_resnet.py --epochs 1 --batch_size 8
python train_vit_triplet.py --epochs 1 --batch_size 4
```

## πŸ“š References

- **Dataset**: [Stylique/Polyvore](https://huggingface.co/datasets/Stylique/Polyvore)
- **Reference Space**: [Stylique/recomendation](https://huggingface.co/spaces/Stylique/recomendation)
- **Research Papers**: Triplet loss, transformer encoders, outfit compatibility

## πŸ“„ License

MIT License - see LICENSE file for details.

## 🀝 Contributing

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests
5. Submit a pull request

## πŸ“ž Support

- **Issues**: GitHub Issues
- **Discussions**: GitHub Discussions
- **Documentation**: This README + inline code comments

---

**Built with ❀️ for the fashion AI community**