forked from open-mmlab/mmagic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
175 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Diffusers Pipeline (2023) | ||
|
||
> [Diffusers Pipeline](https://github.com/huggingface/diffusers) | ||
> **Task**: Diffusers Pipeline | ||
<!-- [ALGORITHM] --> | ||
|
||
## Abstract | ||
|
||
<!-- [ABSTRACT] --> | ||
|
||
We support diffusers pipelines for users to conveniently use diffusers to do inferece in our repo. | ||
|
||
## Configs | ||
|
||
| Model | Dataset | Download | | ||
| :---------------------------------------: | :-----: | :------: | | ||
| [diffusers pipeline](./sd_xl_pipeline.py) | - | - | | ||
|
||
## Quick Start | ||
|
||
```python | ||
from mmagic.apis import MMagicInferencer | ||
|
||
# Create a MMEdit instance and infer | ||
editor = MMagicInferencer(model_name='diffusers_pipeline') | ||
text_prompts = 'Japanese anime style, girl, beautiful, cute, colorful, best quality, extremely detailed' | ||
negative_prompt = 'bad face, bad hands' | ||
result_out_dir = 'resources/output/text2image/sd_xl_japanese.png' | ||
editor.infer(text=text_prompts, | ||
negative_prompt=negative_prompt, | ||
result_out_dir=result_out_dir) | ||
``` | ||
|
||
## Citation | ||
|
||
```bibtex | ||
@misc{von-platen-etal-2022-diffusers, | ||
author = {Patrick von Platen and Suraj Patil and Anton Lozhkov and Pedro Cuenca and Nathan Lambert and Kashif Rasul and Mishig Davaadorj and Thomas Wolf}, | ||
title = {Diffusers: State-of-the-art diffusion models}, | ||
year = {2022}, | ||
publisher = {GitHub}, | ||
journal = {GitHub repository}, | ||
howpublished = {\url{https://github.com/huggingface/diffusers}} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Collections: | ||
- Name: Diffusers Pipeline | ||
Paper: | ||
Title: Diffusers Pipeline | ||
URL: https://github.com/huggingface/diffusers | ||
README: configs/diffusers_pipeline/README.md | ||
Task: | ||
- diffusers pipeline | ||
Year: 2023 | ||
Models: | ||
- Config: configs/diffusers_pipeline/sd_xl_pipeline.py | ||
In Collection: Diffusers Pipeline | ||
Name: sd_xl_pipeline | ||
Results: | ||
- Dataset: '-' | ||
Metrics: {} | ||
Task: Diffusers Pipeline |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# config for model | ||
|
||
model = dict( | ||
type='DiffusionPipeline', | ||
from_pretrained='stabilityai/stable-diffusion-xl-base-1.0' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
import os | ||
from typing import Dict, List | ||
|
||
import numpy as np | ||
from mmengine import mkdir_or_exist | ||
from PIL.Image import Image | ||
from torchvision.utils import save_image | ||
|
||
from .base_mmagic_inferencer import BaseMMagicInferencer, InputsType, PredType | ||
|
||
|
||
class DiffusersPipelineInferencer(BaseMMagicInferencer): | ||
"""inferencer that predicts with text2image models.""" | ||
|
||
func_kwargs = dict( | ||
preprocess=['text', 'negative_prompt'], | ||
forward=[], | ||
visualize=['result_out_dir'], | ||
postprocess=[]) | ||
|
||
extra_parameters = dict(height=None, width=None) | ||
|
||
def preprocess(self, | ||
text: InputsType, | ||
negative_prompt: InputsType = None) -> Dict: | ||
"""Process the inputs into a model-feedable format. | ||
Args: | ||
text(InputsType): text input for text-to-image model. | ||
negative_prompt(InputsType): negative prompt. | ||
Returns: | ||
result(Dict): Results of preprocess. | ||
""" | ||
result = self.extra_parameters | ||
result['prompt'] = text | ||
|
||
if negative_prompt: | ||
result['negative_prompt'] = negative_prompt | ||
|
||
return result | ||
|
||
def forward(self, inputs: InputsType) -> PredType: | ||
"""Forward the inputs to the model.""" | ||
images = self.model(**inputs).images | ||
|
||
return images | ||
|
||
def visualize(self, | ||
preds: PredType, | ||
result_out_dir: str = None) -> List[np.ndarray]: | ||
"""Visualize predictions. | ||
Args: | ||
preds (List[Union[str, np.ndarray]]): Forward results | ||
by the inferencer. | ||
result_out_dir (str): Output directory of image. | ||
Defaults to ''. | ||
Returns: | ||
List[np.ndarray]: Result of visualize | ||
""" | ||
if result_out_dir: | ||
mkdir_or_exist(os.path.dirname(result_out_dir)) | ||
if type(preds) is list: | ||
preds = preds[0] | ||
if type(preds) is Image: | ||
preds.save(result_out_dir) | ||
else: | ||
save_image(preds, result_out_dir, normalize=True) | ||
|
||
return preds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters