File size: 8,167 Bytes
f4d00e0
 
 
 
 
 
 
 
 
 
 
 
672795c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4751b7
 
672795c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4d00e0
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
297
---
license: apache-2.0
datasets:
- dzungpham/font-diffusion-generated-data
language:
- en
library_name: diffusers
tags:
- font-diffusion
- image-to-image
- contrastive-learning
- diffusers
- font-generation
- character-synthesis
- style-transfer
- dpm-solver
---
# Model Card for FontDiffuser

## Model Details

### Model Type
- **Architecture**: Diffusion-based Font Generation Model
- **Framework**: PyTorch + Hugging Face Diffusers
- **Scheduler**: DPM-Solver++ (configurable: dpmsolver++ / dpmsolver)
- **Guidance**: Classifier-free guidance
- **Base Model**: FontDiffuser with Content and Style Encoders

### Model Components
1. **UNet**: Main diffusion model for image generation
2. **Content Encoder**: Extracts character structure information
3. **Style Encoder**: Extracts font style features
4. **DDPM/DPM Scheduler**: Noise scheduling for diffusion process

### Training Configuration
- **Resolution**: 96Γ—96 pixels
- **Batch Size**: 4-8 (configurable)
- **Inference Steps**: 15 (default, configurable)
- **Guidance Scale**: 7.5 (default, configurable)
- **Precision**: FP32/FP16 (optional)
- **Device**: CUDA/GPU recommended

## Model Usage

### Installation
```bash
pip install diffusers torch torchvision safetensors
pip install lpips scikit-image pytorch-fid  # Optional: for evaluation
```

### Basic Generation
```python
from sample_batch import (
    FontManager, 
    batch_generate_images,
    load_fontdiffuser_pipeline
)
from argparse import Namespace

# Initialize font manager
font_manager = FontManager("path/to/font.ttf")

# Load pipeline
args = Namespace(
    ckpt_dir="path/to/checkpoints",
    device="cuda",
    num_inference_steps=15,
    guidance_scale=7.5,
    batch_size=4,
    # ... other args
)
pipe = load_fontdiffuser_pipeline(args)

# Generate images
characters = ['A', 'B', 'C', 'δΈ­', 'ε›½']
style_paths = ['style1.png', 'style2.png']

results = batch_generate_images(
    pipe, characters, style_paths,
    output_dir="output",
    args=args,
    evaluator=evaluator,
    font_manager=font_manager
)
```

### Batch Generation with Checkpointing
```bash
python sample_batch.py \
  --characters "characters.txt" \
  --start_line 1 \
  --end_line 100 \
  --style_images "styles/" \
  --ttf_path "fonts/myfont.ttf" \
  --ckpt_dir "checkpoints/" \
  --output_dir "my_dataset/train_original" \
  --batch_size 4 \
  --num_inference_steps 15 \
  --guidance_scale 7.5 \
  --save_interval 10 \
  --device cuda
```

### Resume from Checkpoint
```bash
python sample_batch.py \
  --characters "characters.txt" \
  --style_images "styles/" \
  --ttf_path "fonts/myfont.ttf" \
  --ckpt_dir "checkpoints/" \
  --output_dir "my_dataset/train_original" \
  --resume_from "my_dataset/train_original/results_checkpoint.json"
```

## Model Performance

### Supported Tasks
- βœ… Single-character font generation
- βœ… Multi-character batch generation
- βœ… Multi-font support
- βœ… Multi-style transfer
- βœ… Index-based tracking for large-scale generation
- βœ… Checkpoint and resume support

### Output Format
```
output_dir/
β”œβ”€β”€ ContentImage/              # Single set of content (character) images
β”‚   β”œβ”€β”€ char0.png
β”‚   β”œβ”€β”€ char1.png
β”‚   └── ...
β”œβ”€β”€ TargetImage/               # Generated font images organized by style
β”‚   β”œβ”€β”€ style0/
β”‚   β”‚   β”œβ”€β”€ style0+char0.png
β”‚   β”‚   β”œβ”€β”€ style0+char1.png
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ style1/
β”‚   β”‚   └── ...
β”‚   └── ...
β”œβ”€β”€ results.json               # Comprehensive generation metadata
β”œβ”€β”€ results_checkpoint.json    # Intermediate checkpoint (if save_interval > 0)
└── results_interrupted.json   # Emergency checkpoint (if interrupted)
```

### Results Metadata Structure
```json
{
  "generations": [
    {
      "character": "A",
      "char_index": 0,
      "style": "style0",
      "style_index": 0,
      "font": "Arial",
      "style_path": "path/to/style0.png",
      "output_path": "TargetImage/style0/style0+char0.png"
    }
  ],
  "metrics": {
    "lpips": {"mean": 0.25, "std": 0.08, "min": 0.1, "max": 0.5},
    "ssim": {"mean": 0.82, "std": 0.05, "min": 0.7, "max": 0.95},
    "fid": {"mean": 15.3, "std": 2.1},
    "inference_times": [
      {
        "style": "style0",
        "style_index": 0,
        "font": "Arial",
        "total_time": 2.45,
        "num_images": 100,
        "time_per_image": 0.0245
      }
    ]
  },
  "fonts": ["Arial", "Times New Roman"],
  "characters": ["A", "B", "C"],
  "styles": ["style0", "style1"],
  "total_chars": 3,
  "total_styles": 2,
  "total_possible_pairs": 6
}
```

## Evaluation Metrics

### Supported Metrics
- **LPIPS**: Learned perceptual image patch similarity (lower is better)
- **SSIM**: Structural similarity index (higher is better)
- **FID**: FrΓ©chet Inception Distance (lower is better)
- **Inference Time**: Per-image generation time

### Generate with Evaluation
```bash
python sample_batch.py \
  --characters "characters.txt" \
  --style_images "styles/" \
  --ttf_path "fonts/myfont.ttf" \
  --ckpt_dir "checkpoints/" \
  --output_dir "my_dataset/train_original" \
  --evaluate \
  --ground_truth_dir "ground_truth/" \
  --compute_fid
```

## Dataset

### Dataset Source
- **Name**: font-diffusion-generated-data
- **Link**: https://huggingface.co/datasets/dzungpham/font-diffusion-generated-data
- **Format**: ContentImage + TargetImage per style
- **Supports**: Multi-font, multi-character, multi-style generation

### Dataset Structure
```
FontDiffusion Dataset/
β”œβ”€β”€ train_original/
β”‚   β”œβ”€β”€ ContentImage/          # Character structure images
β”‚   β”œβ”€β”€ TargetImage/           # Style-specific font renderings
β”‚   └── results.json
β”œβ”€β”€ val_original/
└── test_original/
```

## Training & Fine-tuning

### Fine-tuning from Checkpoint
```bash
python my_train.py \
  --ckpt_dir "checkpoints/" \
  --data_dir "my_dataset/train_original" \
  --output_dir "finetuned_ckpt/" \
  --num_epochs 5 \
  --learning_rate 1e-4 \
  --batch_size 4
```

### Convert & Upload Fine-tuned Models
```bash
python finetune_and_upload.py \
  --ckpt_dir "finetuned_ckpt/" \
  --hf_token "hf_xxxxx" \
  --hf_repo_id "username/font-diffusion-finetuned" \
  --num_epochs 5
```

## Technical Features

### Optimizations
- βœ… **Batch Processing**: Process multiple characters per style
- βœ… **Memory Efficiency**: Attention slicing (optional)
- βœ… **FP16 Support**: Reduced precision for faster inference
- βœ… **Torch Compile**: Optional model compilation
- βœ… **Channels Last Format**: Memory-optimized tensor layout
- βœ… **XFormers Support**: Fast attention implementation

### Robustness
- βœ… **Checkpoint & Resume**: Resume from interruptions
- βœ… **Index-based Tracking**: Handle large character sets (100K+)
- βœ… **Multi-font Support**: Process characters across multiple fonts
- βœ… **Error Recovery**: Graceful handling of missing fonts
- βœ… **Automatic Indexing**: Consistent char_index and style_index

### Monitoring
- βœ… **Weights & Biases Integration**: Real-time tracking
- βœ… **Progress Bars**: Detailed generation progress
- βœ… **Checkpoint Saving**: Periodic intermediate saves
- βœ… **Quality Metrics**: LPIPS, SSIM, FID computation

## Known Limitations

- Requires CUDA-capable GPU for practical generation speeds
- Characters must exist in at least one loaded font
- Style images should be normalized (96Γ—96 or resizable)
- Very large character sets (>100K) may require memory optimization
- FID computation requires representative ground truth dataset

## Citation

```bibtex
@article{fontdiffuser2023,
  title={FontDiffuser: One-Shot Font Generation via Denoising Diffusion with Multi-Scale Content Aggregation and Style Contrastive Learning},
  author={Zhenhua Yang, Dezhi Peng, Yuxin Kong, Yuyi Zhang, Cong Yao, Lianwen Jin},
  year={2023}
}
```

## License

This model is licensed under the Apache License 2.0. See LICENSE file for details.

## Contact & Support

For issues, questions, or contributions:
- GitHub: [FontDiffusion Repository]
- Hugging Face: [Model Card]
- Dataset: https://huggingface.co/datasets/dzungpham/font-diffusion-generated-data

---