|
1 | 1 | # Quick run
|
2 | 2 |
|
3 |
| -After installing MMagic successfully, now you are able to play with MMagic! |
4 |
| - |
5 |
| -To synthesize an image of a church, you only need several lines of codes by MMagic! |
| 3 | +After installing MMagic successfully, now you are able to play with MMagic! To generate an image from text, you only need several lines of codes by MMagic! |
6 | 4 |
|
7 | 5 | ```python
|
8 |
| -from mmagic.apis import init_model, sample_unconditional_model |
9 |
| - |
10 |
| -config_file = 'configs/styleganv2/stylegan2_c2_8xb4-800kiters_lsun-church-256x256.py' |
11 |
| -# you can download this checkpoint in advance and use a local file path. |
12 |
| -checkpoint_file = 'https://download.openmmlab.com/mmediting/stylegan2/official_weights/stylegan2-church-config-f-official_20210327_172657-1d42b7d1.pth' |
13 |
| -device = 'cuda:0' |
14 |
| -# init a generative model |
15 |
| -model = init_model(config_file, checkpoint_file, device=device) |
16 |
| -# sample images |
17 |
| -fake_imgs = sample_unconditional_model(model, 4) |
| 6 | +from mmagic.apis import MMagicInferencer |
| 7 | +sd_inferencer = MMagicInferencer(model_name='stable_diffusion') |
| 8 | +text_prompts = 'A panda is having dinner at KFC' |
| 9 | +result_out_dir = 'output/sd_res.png' |
| 10 | +sd_inferencer.infer(text=text_prompts, result_out_dir=result_out_dir) |
18 | 11 | ```
|
19 | 12 |
|
20 | 13 | Or you can just run the following command.
|
21 | 14 |
|
22 | 15 | ```bash
|
23 |
| -python demo/mmagic_inference_demo_demo.py \ |
24 |
| -configs/styleganv2/stylegan2_c2_lsun-church_256_b4x8_800k.py \ |
25 |
| -https://download.openmmlab.com/mmediting/stylegan2/official_weights/stylegan2-church-config-f-official_20210327_172657-1d42b7d1.pth |
26 |
| - |
| 16 | +python demo/mmagic_inference_demo.py \ |
| 17 | + --model-name stable_diffusion \ |
| 18 | + --text "A panda is having dinner at KFC" \ |
| 19 | + --result-out-dir ./output/sd_res.png |
27 | 20 | ```
|
28 | 21 |
|
29 |
| -You will see a new image `unconditional_samples.png` in folder `work_dirs/demos/`, which contained generated samples. |
| 22 | +You will see a new image `sd_res.png` in folder `output/`, which contained generated samples. |
30 | 23 |
|
31 | 24 | What's more, if you want to make these photos much more clear,
|
32 | 25 | you only need several lines of codes for image super-resolution by MMagic!
|
33 | 26 |
|
34 | 27 | ```python
|
35 |
| -import mmcv |
36 |
| -from mmagic.apis import init_model, restoration_inference |
37 |
| -from mmagic.utils import tensor2img |
38 |
| - |
| 28 | +from mmagic.apis import MMagicInferencer |
39 | 29 | config = 'configs/esrgan/esrgan_x4c64b23g32_1xb16-400k_div2k.py'
|
40 | 30 | checkpoint = 'https://download.openmmlab.com/mmediting/restorers/esrgan/esrgan_x4c64b23g32_1x16_400k_div2k_20200508-f8ccaf3b.pth'
|
41 | 31 | img_path = 'tests/data/image/lq/baboon_x4.png'
|
42 |
| -model = init_model(config, checkpoint) |
43 |
| -output = restoration_inference(model, img_path) |
44 |
| -output = tensor2img(output) |
45 |
| -mmcv.imwrite(output, 'output.png') |
| 32 | +editor = MMagicInferencer('esrgan', model_config=config, model_ckpt=checkpoint) |
| 33 | +output = editor.infer(img=img_path,result_out_dir='output.png') |
46 | 34 | ```
|
47 | 35 |
|
48 | 36 | Now, you can check your fancy photos in `output.png`.
|
0 commit comments