(betas, model, x_T, steps, model_kwargs)
| 242 | |
| 243 | |
| 244 | def dpm_solver(betas, model, x_T, steps, model_kwargs): |
| 245 | # You need to firstly define your model and the extra inputs of your model, |
| 246 | # And initialize an `x_T` from the standard normal distribution. |
| 247 | # `model` has the format: model(x_t, t_input, **model_kwargs). |
| 248 | # If your model has no extra inputs, just let model_kwargs = {}. |
| 249 | |
| 250 | # If you use discrete-time DPMs, you need to further define the |
| 251 | # beta arrays for the noise schedule. |
| 252 | |
| 253 | # model = .... |
| 254 | # model_kwargs = {...} |
| 255 | # x_T = ... |
| 256 | # betas = .... |
| 257 | |
| 258 | # 1. Define the noise schedule. |
| 259 | noise_schedule = NoiseScheduleVP(schedule='discrete', betas=betas) |
| 260 | |
| 261 | # 2. Convert your discrete-time `model` to the continuous-time |
| 262 | # noise prediction model. Here is an example for a diffusion model |
| 263 | # `model` with the noise prediction type ("noise") . |
| 264 | model_fn = model_wrapper( |
| 265 | model, |
| 266 | noise_schedule, |
| 267 | model_type="noise", # or "x_start" or "v" or "score" |
| 268 | model_kwargs=model_kwargs, |
| 269 | ) |
| 270 | |
| 271 | # 3. Define dpm-solver and sample by singlestep DPM-Solver. |
| 272 | # (We recommend singlestep DPM-Solver for unconditional sampling) |
| 273 | # You can adjust the `steps` to balance the computation |
| 274 | # costs and the sample quality. |
| 275 | dpm_solver = DPM_Solver(model_fn, noise_schedule, algorithm_type="dpmsolver++", |
| 276 | correcting_x0_fn="dynamic_thresholding") |
| 277 | # Can also try |
| 278 | # dpm_solver = DPM_Solver(model_fn, noise_schedule, algorithm_type="dpmsolver++") |
| 279 | |
| 280 | # You can use steps = 10, 12, 15, 20, 25, 50, 100. |
| 281 | # Empirically, we find that steps in [10, 20] can generate quite good samples. |
| 282 | # And steps = 20 can almost converge. |
| 283 | x_sample = dpm_solver.sample( |
| 284 | x_T, |
| 285 | steps=steps, |
| 286 | order=1, |
| 287 | skip_type="time_uniform", |
| 288 | method="singlestep", |
| 289 | ) |
| 290 | return x_sample |
no test coverage detected