Skip to content

Commit

Permalink
[README] Update
Browse files Browse the repository at this point in the history
  • Loading branch information
yeungchenwa committed Dec 16, 2023
1 parent 0a443a9 commit caba1db
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 13 deletions.
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,38 @@ pip install -r requirements.txt
```

## 🏋️ Training
### Training - Phase 1
```bash
sh train_phase_1.sh
```

### Training - Phase 2
Coming Soon...

## 📺 Sampling
coming soon ...
### Step 1 => Prepare the checkpoint
(1) Download the checkpoint or (2) Put your checkpoint to the folder `ckpt/`

### Step 2 => Run the script
(1) Sampling image from content image.
```bash
sh script/sample_content_image.sh
```
(2) Sampling image from content character.
```bash
sh script/sample_content_character.sh
```

## 📱 Run WebUI
coming soon ...
### (1) Sampling by FontDiffuser
```bash
gradio gradio_app.py
```

### (2) Sampling by FontDiffuser and Rendering by ControlNet
```bash
gradio gradio_app_controlnet.py
```

## 🌄 Gallery
coming sonn ...
Expand Down
29 changes: 20 additions & 9 deletions sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ def image_process(args, content_image=None, style_image=None):
return None, None
font = load_ttf(ttf_path=args.ttf_path)
content_image = ttf2im(font=font, char=args.content_character)
content_image_pil = content_image.copy()
else:
content_image = Image.open(args.content_image_path).convert('RGB')
content_image_pil = None
style_image = Image.open(args.style_image_path).convert('RGB')
else:
assert style_image is not None, "The style image should not be None."
Expand All @@ -86,7 +88,7 @@ def image_process(args, content_image=None, style_image=None):
content_image = content_inference_transforms(content_image)[None, :]
style_image = style_inference_transforms(style_image)[None, :]

return content_image, style_image
return content_image, style_image, content_image_pil

def load_fontdiffuer_pipeline(args):
# Load the model state_dict
Expand Down Expand Up @@ -129,9 +131,9 @@ def sampling(args, pipe, content_image=None, style_image=None):
if args.seed:
set_seed(seed=args.seed)

content_image, style_image = image_process(args=args,
content_image=content_image,
style_image=style_image)
content_image, style_image, content_image_pil = image_process(args=args,
content_image=content_image,
style_image=style_image)
if content_image == None:
print(f"The content_character you provided is not in the ttf. \
Please change the content_character or you can change the ttf.")
Expand Down Expand Up @@ -161,11 +163,20 @@ def sampling(args, pipe, content_image=None, style_image=None):
if args.save_image:
print(f"Saving the image ......")
save_single_image(save_dir=args.save_image_dir, image=images[0])
save_image_with_content_style(save_dir=args.save_image_dir,
image=images[0],
content_image_path=args.content_image_path,
style_image_path=args.style_image_path,
resolution=args.resolution)
if args.character_input:
save_image_with_content_style(save_dir=args.save_image_dir,
image=images[0],
content_image_pil=content_image_pil,
content_image_path=None,
style_image_path=args.style_image_path,
resolution=args.resolution)
else:
save_image_with_content_style(save_dir=args.save_image_dir,
image=images[0],
content_image_pil=None,
content_image_path=args.content_image_path,
style_image_path=args.style_image_path,
resolution=args.resolution)
print(f"Finish the sampling process, costing time {end - start}s")
return images[0]

Expand Down
13 changes: 13 additions & 0 deletions scripts/sample_content_character.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
python sample.py \
--ckpt_dir="ckpt/" \
--style_image_path="data_examples/sampling/example_style.jpg" \
--save_image \
--character_input \
--content_character="" \
--save_image_dir="outputs/" \
--device="cuda:0" \
--algorithm_type="dpmsolver++" \
--guidance_type="classifier-free" \
--guidance_scale=7.5 \
--num_inference_steps=20 \
--method="multistep"
1 change: 1 addition & 0 deletions scripts/sample.sh → scripts/sample_content_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ python sample.py \
--ckpt_dir="ckpt/" \
--content_image_path="data_examples/sampling/example_content.jpg" \
--style_image_path="data_examples/sampling/example_style.jpg" \
--save_image \
--save_image_dir="outputs/" \
--device="cuda:0" \
--algorithm_type="dpmsolver++" \
Expand Down
7 changes: 5 additions & 2 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ def save_single_image(save_dir, image):
image.save(save_path)


def save_image_with_content_style(save_dir, image, content_image_path, style_image_path, resolution):
def save_image_with_content_style(save_dir, image, content_image_pil, content_image_path, style_image_path, resolution):

new_image = Image.new('RGB', (resolution*3, resolution))
content_image = Image.open(content_image_path).convert("RGB").resize((resolution, resolution), Image.BILINEAR)
if content_image_pil is not None:
content_image = content_image_pil
else:
content_image = Image.open(content_image_path).convert("RGB").resize((resolution, resolution), Image.BILINEAR)
style_image = Image.open(style_image_path).convert("RGB").resize((resolution, resolution), Image.BILINEAR)

new_image.paste(content_image, (0, 0))
Expand Down

0 comments on commit caba1db

Please sign in to comment.