Skip to content

Commit 2de36fa

Browse files
authored
minor fix in controlnet flax example (#2986)
* fix the error when push_to_hub but not log validation * contronet_from_pt & controlnet_revision * add intermediate checkpointing to the guide
1 parent e405264 commit 2de36fa

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

examples/controlnet/README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,12 @@ Then cd in the example folder and run
320320
pip install -U -r requirements_flax.txt
321321
```
322322

323+
If you want to use Weights and Biases logging, you should also install `wandb` now
324+
325+
```bash
326+
pip install wandb
327+
```
328+
323329
Now let's downloading two conditioning images that we will use to run validation during the training in order to track our progress
324330

325331
```
@@ -389,4 +395,17 @@ Note, however, that the performance of the TPUs might get bottlenecked as stream
389395

390396
* [Webdataset](https://webdataset.github.io/webdataset/)
391397
* [TorchData](https://github.com/pytorch/data)
392-
* [TensorFlow Datasets](https://www.tensorflow.org/datasets/tfless_tfds)
398+
* [TensorFlow Datasets](https://www.tensorflow.org/datasets/tfless_tfds)
399+
400+
When work with a larger dataset, you may need to run training process for a long time and it’s useful to save regular checkpoints during the process. You can use the following argument to enable intermediate checkpointing:
401+
402+
```bash
403+
--checkpointing_steps=500
404+
```
405+
This will save the trained model in subfolders of your output_dir. Subfolder names is the number of steps performed so far; for example: a checkpoint saved after 500 training steps would be saved in a subfolder named 500
406+
407+
You can then start your training from this saved checkpoint with
408+
409+
```bash
410+
--controlnet_model_name_or_path="./control_out/500"
411+
```

examples/controlnet/train_controlnet_flax.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,16 @@ def log_validation(controlnet, controlnet_params, tokenizer, args, rng, weight_d
154154

155155
def save_model_card(repo_id: str, image_logs=None, base_model=str, repo_folder=None):
156156
img_str = ""
157-
for i, log in enumerate(image_logs):
158-
images = log["images"]
159-
validation_prompt = log["validation_prompt"]
160-
validation_image = log["validation_image"]
161-
validation_image.save(os.path.join(repo_folder, "image_control.png"))
162-
img_str += f"prompt: {validation_prompt}\n"
163-
images = [validation_image] + images
164-
image_grid(images, 1, len(images)).save(os.path.join(repo_folder, f"images_{i}.png"))
165-
img_str += f"![images_{i})](./images_{i}.png)\n"
157+
if image_logs is not None:
158+
for i, log in enumerate(image_logs):
159+
images = log["images"]
160+
validation_prompt = log["validation_prompt"]
161+
validation_image = log["validation_image"]
162+
validation_image.save(os.path.join(repo_folder, "image_control.png"))
163+
img_str += f"prompt: {validation_prompt}\n"
164+
images = [validation_image] + images
165+
image_grid(images, 1, len(images)).save(os.path.join(repo_folder, f"images_{i}.png"))
166+
img_str += f"![images_{i})](./images_{i}.png)\n"
166167

167168
yaml = f"""
168169
---
@@ -213,6 +214,17 @@ def parse_args():
213214
action="store_true",
214215
help="Load the pretrained model from a PyTorch checkpoint.",
215216
)
217+
parser.add_argument(
218+
"--controlnet_revision",
219+
type=str,
220+
default=None,
221+
help="Revision of controlnet model identifier from huggingface.co/models.",
222+
)
223+
parser.add_argument(
224+
"--controlnet_from_pt",
225+
action="store_true",
226+
help="Load the controlnet model from a PyTorch checkpoint.",
227+
)
216228
parser.add_argument(
217229
"--tokenizer_name",
218230
type=str,
@@ -731,7 +743,10 @@ def main():
731743
if args.controlnet_model_name_or_path:
732744
logger.info("Loading existing controlnet weights")
733745
controlnet, controlnet_params = FlaxControlNetModel.from_pretrained(
734-
args.controlnet_model_name_or_path, from_pt=True, dtype=jnp.float32
746+
args.controlnet_model_name_or_path,
747+
revision=args.controlnet_revision,
748+
from_pt=args.controlnet_from_pt,
749+
dtype=jnp.float32,
735750
)
736751
else:
737752
logger.info("Initializing controlnet weights from unet")
@@ -1021,6 +1036,8 @@ def cumul_grad_step(grad_idx, loss_grad_rng):
10211036
if jax.process_index() == 0:
10221037
if args.validation_prompt is not None:
10231038
image_logs = log_validation(controlnet, state.params, tokenizer, args, validation_rng, weight_dtype)
1039+
else:
1040+
image_logs = None
10241041

10251042
controlnet.save_pretrained(
10261043
args.output_dir,

0 commit comments

Comments
 (0)