{"id":31580547,"url":"https://github.com/anto18671/fairface-diffusion","last_synced_at":"2025-10-05T21:14:54.249Z","repository":{"id":307879985,"uuid":"1030972108","full_name":"anto18671/fairface-diffusion","owner":"anto18671","description":"A diffusion model trained on FairFace with discrete conditioning on age, gender, and race. Generates high-quality face images using a custom UNet and DDPM scheduler.","archived":false,"fork":false,"pushed_at":"2025-08-02T18:25:50.000Z","size":915,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-10-04T05:52:58.679Z","etag":null,"topics":["conditional-diffusion","diffusion-models","fairface-dataset"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anto18671.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-08-02T18:18:12.000Z","updated_at":"2025-08-02T18:27:49.000Z","dependencies_parsed_at":"2025-08-02T20:42:56.886Z","dependency_job_id":"dde15f86-078d-4f90-94ce-ff909f997a0c","html_url":"https://github.com/anto18671/fairface-diffusion","commit_stats":null,"previous_names":["anto18671/fairface-diffusion"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/anto18671/fairface-diffusion","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anto18671%2Ffairface-diffusion","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anto18671%2Ffairface-diffusion/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anto18671%2Ffairface-diffusion/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anto18671%2Ffairface-diffusion/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anto18671","download_url":"https://codeload.github.com/anto18671/fairface-diffusion/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anto18671%2Ffairface-diffusion/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278520426,"owners_count":26000476,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-05T02:00:06.059Z","response_time":54,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["conditional-diffusion","diffusion-models","fairface-dataset"],"created_at":"2025-10-05T21:14:48.701Z","updated_at":"2025-10-05T21:14:54.231Z","avatar_url":"https://github.com/anto18671.png","language":"Python","readme":"# 🧠 FairFace Conditioned Diffusion Model\n\nThis project trains a conditional diffusion model on the [FairFace](https://huggingface.co/datasets/HuggingFaceM4/FairFace) dataset using discrete attributes: **age**, **gender**, and **race**.\n\nEach sample is conditioned on one of `9 × 2 × 7 = 126` unique combinations from the dataset's categorical metadata.\n\n---\n\n## 📊 Conditioning Setup\n\nWe compute a single integer condition ID from:\n\n- **Age**: 9 classes\n- **Gender**: 2 classes\n- **Race**: 7 classes\n\nThe combined ID is:\n\n```\ncondition_id = (age_id * 14) + (gender_id * 7) + race_id\n```\n\nEach condition is embedded with `nn.Embedding(126, 512)` and passed into a cross-attention UNet.\n\n---\n\n## 🏗️ Model Architecture\n\nThe model is a `UNet2DConditionModel` from 🤗 `diffusers`:\n\n- **Input**: 3-channel RGB (128×128)\n- **Cross-attention**: 512-dim from conditional embeddings\n- **Scheduler**: DDPM with 1000 timesteps\n- **Mixed precision**: Enabled via `torch.amp`\n\n---\n\n## 🖼️ Generated Samples by Epoch\n\nSamples below show **4 different conditions** (0–3) generated from models trained for increasing epochs.\n\n| Sample                              |\n| ----------------------------------- |\n| ![](./assets/grid_epochs_10_90.png) |\n|                                     |\n\n---\n\n## 🧪 Dataset Preparation\n\n```python\nfrom datasets import load_dataset, concatenate_datasets\n\n# Load both 0.25 and 1.25 splits\ndataset_025_train = load_dataset(\"HuggingFaceM4/FairFace\", \"0.25\", split=\"train\")\ndataset_025_val   = load_dataset(\"HuggingFaceM4/FairFace\", \"0.25\", split=\"validation\")\ndataset_125_train = load_dataset(\"HuggingFaceM4/FairFace\", \"1.25\", split=\"train\")\ndataset_125_val   = load_dataset(\"HuggingFaceM4/FairFace\", \"1.25\", split=\"validation\")\n\n# Merge all splits\nmerged_dataset = concatenate_datasets([\n    dataset_025_train, dataset_025_val,\n    dataset_125_train, dataset_125_val\n])\n```\n\n---\n\n## 🧬 Training Loop\n\n```python\nfor images, cond_ids in dataloader:\n    # Encode condition\n    emb = cond_emb(cond_ids).unsqueeze(1)\n\n    # Noise injection\n    t = torch.randint(0, 1000, (images.size(0),), device=device)\n    noise = torch.randn_like(images)\n    noisy = scheduler.add_noise(images, noise, t)\n\n    # Denoise\n    pred = unet(noisy, t, encoder_hidden_states=emb).sample\n    loss = F.mse_loss(pred, noise)\n\n    # Backprop\n    scaler.scale(loss).backward()\n    scaler.step(optimizer)\n    scaler.update()\n```\n\nEach epoch:\n\n- Trains on \\~200,000 images\n- Saves a model checkpoint\n- Generates and saves 3 sample images\n\n---\n\n## 💾 Checkpoint Structure\n\nTrained models are saved as:\n\n```\n./models/unet_epoch_{EPOCH}.pt\n```\n\nSamples are saved as:\n\n```\n./models/sample_epoch_{EPOCH}_{i}.png\n```\n\n---\n\n## 🧠 Sample Generation During Inference\n\n```python\nx = torch.randn(1, 3, size, size).to(device)\n\nfor t in reversed(range(1000)):\n    t_tensor = torch.tensor([t], device=device)\n    pred = unet(x, t_tensor, encoder_hidden_states=emb).sample\n    x = scheduler.step(pred, t, x).prev_sample\n```\n\nThese are used to produce a visual progression of model capability over time.\n\n---\n\n## ✅ Results Summary\n\n- ✔️ Supports 126 discrete conditionings\n- ⚡ Mixed-precision training with `torch.amp`\n- 💾 Saved checkpoints and visual samples per epoch\n- 📊 Structured dataset merging and label encoding\n\n---\n\n## 📁 Outputs\n\n```bash\n./models/\n├── unet_epoch_10.pt\n├── unet_epoch_20.pt\n├── ...\n├── sample_epoch_10_0.png\n├── sample_epoch_10_1.png\n├── ...\n./assets/\n└── grid_epochs_10_90.png\n```\n\nHere’s the **MIT License** section formatted in markdown, including a direct link to the full license text:\n\n---\n\n## 📄 License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanto18671%2Ffairface-diffusion","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanto18671%2Ffairface-diffusion","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanto18671%2Ffairface-diffusion/lists"}