Add the model card template (#43) · huggingface/diffusers@8cba133 · GitHub
Skip to content

Commit 8cba133

Browse files
anton-lpatil-suraj
andauthored
Add the model card template (#43)
* add a metrics logger * fix LatentDiffusionUncondPipeline * add VQModel in init * add image logging to tensorboard * switch manual templates to the modelcards package * hide ldm example Co-authored-by: patil-suraj <surajp815@gmail.com>
1 parent f47066f commit 8cba133

9 files changed

Lines changed: 184 additions & 80 deletions

File tree

File renamed without changes.

‎examples/train_unconditional.py‎

Lines changed: 83 additions & 58 deletions

‎setup.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@
8787
"regex!=2019.12.17",
8888
"requests",
8989
"torch>=1.4",
90+
"tensorboard",
91+
"modelcards=0.1.4"
9092
]
9193

9294
# this is a lookup table with items like:
@@ -172,6 +174,8 @@ def run(self):
172174
deps["requests"],
173175
deps["torch"],
174176
deps["Pillow"],
177+
deps["tensorboard"],
178+
deps["modelcards"],
175179
]
176180

177181
setup(

‎src/diffusers/dependency_versions_table.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
"regex": "regex!=2019.12.17",
1414
"requests": "requests",
1515
"torch": "torch>=1.4",
16+
"tensorboard": "tensorboard",
1617
}

‎src/diffusers/hub_utils.py‎

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,17 @@
1919
from pathlib import Path
2020
from typing import Optional
2121

22-
import yaml
2322
from diffusers import DiffusionPipeline
2423
from huggingface_hub import HfFolder, Repository, whoami
24+
from modelcards import CardData, ModelCard
2525

2626
from .utils import logging
2727

2828

2929
logger = logging.get_logger(__name__)
3030

3131

32-
AUTOGENERATED_TRAINER_COMMENT = """
33-
<!-- This model card has been generated automatically according to the information the Trainer had access to. You
34-
should probably proofread and complete it, then remove this comment. -->
35-
"""
32+
MODEL_CARD_TEMPLATE_PATH = Path(__file__).parent / "utils" / "model_card_template.md"
3633

3734

3835
def get_full_repo_name(model_id: str, organization: Optional[str] = None, token: Optional[str] = None):
@@ -152,17 +149,36 @@ def create_model_card(args, model_name):
152149
if args.local_rank not in [-1, 0]:
153150
return
154151

155-
# TODO: replace this placeholder model card generation
156-
model_card = ""
157-
158-
metadata = {"license": "apache-2.0", "tags": ["pytorch", "diffusers"]}
159-
metadata = yaml.dump(metadata, sort_keys=False)
160-
if len(metadata) > 0:
161-
model_card = f"---\n{metadata}---\n"
162-
163-
model_card += AUTOGENERATED_TRAINER_COMMENT
164-
165-
model_card += f"\n# {model_name}\n\n"
166-
167-
with open(os.path.join(args.output_dir, "README.md"), "w") as f:
168-
f.write(model_card)
152+
repo_name = get_full_repo_name(model_name, token=args.hub_token)
153+
154+
model_card = ModelCard.from_template(
155+
card_data=CardData( # Card metadata object that will be converted to YAML block
156+
language="en",
157+
license="apache-2.0",
158+
library_name="diffusers",
159+
tags=[],
160+
datasets=args.dataset,
161+
metrics=[],
162+
),
163+
template_path=MODEL_CARD_TEMPLATE_PATH,
164+
model_name=model_name,
165+
repo_name=repo_name,
166+
dataset_name=args.dataset,
167+
learning_rate=args.learning_rate,
168+
train_batch_size=args.train_batch_size,
169+
eval_batch_size=args.eval_batch_size,
170+
gradient_accumulation_steps=args.gradient_accumulation_steps,
171+
adam_beta1=args.adam_beta1,
172+
adam_beta2=args.adam_beta2,
173+
adam_weight_decay=args.adam_weight_decay,
174+
adam_epsilon=args.adam_epsilon,
175+
lr_scheduler=args.lr_scheduler,
176+
lr_warmup_steps=args.lr_warmup_steps,
177+
ema_inv_gamma=args.ema_inv_gamma,
178+
ema_power=args.ema_power,
179+
ema_max_decay=args.ema_max_decay,
180+
mixed_precision=args.mixed_precision,
181+
)
182+
183+
card_path = os.path.join(args.output_dir, "README.md")
184+
model_card.save(card_path)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .pipeline_latent_diffusion_uncond import LatentDiffusionUncondPipeline
1+
from .pipeline_latent_diffusion_uncond import LatentDiffusionUncondPipeline, VQModel

‎src/diffusers/schedulers/scheduling_ddim.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,14 @@ def step(self, residual, sample, t, num_inference_steps, eta, use_clipped_residu
145145

146146
return pred_prev_sample
147147

148+
def add_noise(self, original_samples, noise, timesteps):
149+
sqrt_alpha_prod = self.alphas_cumprod[timesteps] ** 0.5
150+
sqrt_alpha_prod = self.match_shape(sqrt_alpha_prod, original_samples)
151+
sqrt_one_minus_alpha_prod = (1 - self.alphas_cumprod[timesteps]) ** 0.5
152+
sqrt_one_minus_alpha_prod = self.match_shape(sqrt_one_minus_alpha_prod, original_samples)
153+
154+
noisy_samples = sqrt_alpha_prod * original_samples + sqrt_one_minus_alpha_prod * noise
155+
return noisy_samples
156+
148157
def __len__(self):
149158
return self.config.timesteps

‎src/diffusers/schedulers/scheduling_ddpm.py‎

Lines changed: 1 addition & 2 deletions

0 commit comments

Comments
 (0)