Skip to content

Commit

Permalink
add random seed
Browse files Browse the repository at this point in the history
  • Loading branch information
staoxiao committed Nov 4, 2024
1 parent fd7987c commit 4bb793f
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 71 deletions.
2 changes: 2 additions & 0 deletions OmniGen/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class OmniGenCache(DynamicCache):
def __init__(self,
num_tokens_for_img: int, offload_kv_cache: bool=False) -> None:
if not torch.cuda.is_available():
print("No avaliable GPU, offload_kv_cache wiil be set to False, which will result in large memory usage and time cost when input multiple images!!!")
offload_kv_cache = False
raise RuntimeError("OffloadedCache can only be used with a GPU")
super().__init__()
self.original_device = []
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ We showcase some examples in [inference.ipynb](inference.ipynb). And in [inferen

Here is the illustrations of OmniGen's capabilities:
- You can control the image generation flexibly via OmniGen
![demo](./imgs/demo_cases2.png)
![demo](./imgs/demo_cases.png)
- Referring Expression Generation: You can input multiple images and use simple, general language to refer to the objects within those images. OmniGen can automatically recognize the necessary objects in each image and generate new images based on them. No additional operations, such as image cropping or face detection, are required.
![demo](./imgs/referring2.png)
![demo](./imgs/referring.png)

If you are not entirely satisfied with certain functionalities or wish to add new capabilities, you can try [fine-tuning OmniGen](#6-finetune).

Expand Down
105 changes: 37 additions & 68 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gradio as gr
from PIL import Image
import os

import random
import spaces

from OmniGen import OmniGenPipeline
Expand All @@ -12,12 +12,15 @@

@spaces.GPU(duration=160)
def generate_image(text, img1, img2, img3, height, width, guidance_scale, img_guidance_scale, inference_steps, seed, separate_cfg_infer, offload_model,
use_input_image_size_as_output, max_input_image_size):
use_input_image_size_as_output, max_input_image_size, randomize_seed):
input_images = [img1, img2, img3]
# Delete None
input_images = [img for img in input_images if img is not None]
if len(input_images) == 0:
input_images = None

if randomize_seed:
seed = random.randint(0, 10000000)

output = pipe(
prompt=text,
Expand Down Expand Up @@ -51,12 +54,9 @@ def get_example():
1024,
2.5,
1.6,
50,
0,
True,
False,
False,
1024,
False,
],
[
"The woman in <img><|image_1|></img> waves her hand happily in the crowd",
Expand All @@ -67,12 +67,9 @@ def get_example():
1024,
2.5,
1.9,
50,
128,
True,
False,
False,
1024,
False,
],
[
"A man in a black shirt is reading a book. The man is the right man in <img><|image_1|></img>.",
Expand All @@ -83,12 +80,9 @@ def get_example():
1024,
2.5,
1.6,
50,
0,
True,
False,
False,
1024,
False,
],
[
"Two woman are raising fried chicken legs in a bar. A woman is <img><|image_1|></img>. The other woman is <img><|image_2|></img>.",
Expand All @@ -99,12 +93,9 @@ def get_example():
1024,
2.5,
1.8,
50,
168,
True,
False,
False,
1024,
False,
],
[
"A man and a short-haired woman with a wrinkled face are standing in front of a bookshelf in a library. The man is the man in the middle of <img><|image_1|></img>, and the woman is oldest woman in <img><|image_2|></img>",
Expand All @@ -115,12 +106,9 @@ def get_example():
1024,
2.5,
1.6,
50,
60,
True,
False,
False,
1024,
False,
],
[
"A man and a woman are sitting at a classroom desk. The man is the man with yellow hair in <img><|image_1|></img>. The woman is the woman on the left of <img><|image_2|></img>",
Expand All @@ -131,12 +119,9 @@ def get_example():
1024,
2.5,
1.8,
50,
66,
True,
False,
False,
1024,
False,
],
[
"The flower <img><|image_1|><\/img> is placed in the vase which is in the middle of <img><|image_2|><\/img> on a wooden table of a living room",
Expand All @@ -147,12 +132,9 @@ def get_example():
1024,
2.5,
1.6,
50,
0,
True,
False,
False,
1024,
False,
],
[
"<img><|image_1|><img>\n Remove the woman's earrings. Replace the mug with a clear glass filled with sparkling iced cola.",
Expand All @@ -163,12 +145,9 @@ def get_example():
None,
2.5,
1.6,
50,
222,
True,
False,
True,
1024,
False,
],
[
"Detect the skeleton of human in this image: <img><|image_1|></img>.",
Expand All @@ -179,12 +158,9 @@ def get_example():
None,
2.0,
1.6,
50,
0,
True,
False,
True,
1024,
False,
],
[
"Generate a new photo using the following picture and text as conditions: <img><|image_1|><img>\n A young boy is sitting on a sofa in the library, holding a book. His hair is neatly combed, and a faint smile plays on his lips, with a few freckles scattered across his cheeks. The library is quiet, with rows of shelves filled with books stretching out behind him.",
Expand All @@ -195,12 +171,9 @@ def get_example():
None,
2,
1.6,
50,
42,
True,
False,
True,
1024,
False,
],
[
"Following the pose of this image <img><|image_1|><img>, generate a new photo: A young boy is sitting on a sofa in the library, holding a book. His hair is neatly combed, and a faint smile plays on his lips, with a few freckles scattered across his cheeks. The library is quiet, with rows of shelves filled with books stretching out behind him.",
Expand All @@ -211,12 +184,9 @@ def get_example():
None,
2.0,
1.6,
50,
123,
True,
False,
True,
1024,
False,
],
[
"Following the depth mapping of this image <img><|image_1|><img>, generate a new photo: A young girl is sitting on a sofa in the library, holding a book. His hair is neatly combed, and a faint smile plays on his lips, with a few freckles scattered across his cheeks. The library is quiet, with rows of shelves filled with books stretching out behind him.",
Expand All @@ -227,12 +197,9 @@ def get_example():
None,
2.0,
1.6,
50,
1,
True,
False,
True,
1024,
False,
],
[
"<img><|image_1|><\/img> What item can be used to see the current time? Please remove it.",
Expand All @@ -243,12 +210,9 @@ def get_example():
None,
2.5,
1.6,
50,
0,
True,
False,
True,
1024,
False,
],
[
"According to the following examples, generate an output for the input.\nInput: <img><|image_1|></img>\nOutput: <img><|image_2|></img>\n\nInput: <img><|image_3|></img>\nOutput: ",
Expand All @@ -259,20 +223,25 @@ def get_example():
224,
2.5,
1.6,
50,
1,
True,
False,
False,
768,
False,
],
]
return case

def run_for_examples(text, img1, img2, img3, height, width, guidance_scale, img_guidance_scale, inference_steps, seed, separate_cfg_infer, offload_model,
use_input_image_size_as_output, max_input_image_size):
return generate_image(text, img1, img2, img3, height, width, guidance_scale, img_guidance_scale, inference_steps, seed, separate_cfg_infer, offload_model,
use_input_image_size_as_output, max_input_image_size)
def run_for_examples(text, img1, img2, img3, height, width, guidance_scale, img_guidance_scale, seed, max_input_image_size, randomize_seed):
# 在函数内部设置默认值
inference_steps = 50
separate_cfg_infer = True
offload_model = False
use_input_image_size_as_output = False

return generate_image(
text, img1, img2, img3, height, width, guidance_scale, img_guidance_scale,
inference_steps, seed, separate_cfg_infer, offload_model,
use_input_image_size_as_output, max_input_image_size, randomize_seed
)

description = """
OmniGen is a unified image generation model that you can use to perform various tasks, including but not limited to text-to-image generation, subject-driven generation, Identity-Preserving Generation, and image-conditioned generation.
Expand All @@ -281,17 +250,18 @@ def run_for_examples(text, img1, img2, img3, height, width, guidance_scale, img_
prompt = "A woman holds a bouquet of flowers and faces the camera. Thw woman is \<img\>\<|image_1|\>\</img\>."
Tips:
- For image editing task and controlnet task, we recommend to set the height and width of output image as the same as input image. For example, if you want to edit a 512x512 image, you should set the height and width of output image as 512x512. You also can set the `use_input_image_size_as_output` to automatically set the height and width of output image as the same as input image.
- For out-of-memory or time cost, you can set `offload_model=True` or refer to [./docs/inference.md#requiremented-resources](https://github.com/VectorSpaceLab/OmniGen/blob/main/docs/inference.md#requiremented-resources) to select a appropriate setting.
- If inference time is too long when inputting multiple images, please try to reduce the `max_input_image_size`. For more details please refer to [./docs/inference.md#requiremented-resources](https://github.com/VectorSpaceLab/OmniGen/blob/main/docs/inference.md#requiremented-resources).
- Oversaturated: If the image appears oversaturated, please reduce the `guidance_scale`.
- Not match the prompt: If the image does not match the prompt, please try to increase the `guidance_scale`.
- Low-quality: More detailed prompts will lead to better results.
- Animate Style: If the genereate images is in animate style, you can try to add `photo` to the prompt`.
- Edit generated image. If you generate a image by omnigen and then want to edit it, you cannot use the same seed to edit this image. For example, use seed=0 to generate image, and should use seed=1 to edit this image.
- For image editing tasks, we recommend placing the image before the editing instruction. For example, use `<img><|image_1|></img> remove suit`, rather than `remove suit <img><|image_1|></img>`.
- For image editing task and controlnet task, we recommend to set the height and width of output image as the same as input image. For example, if you want to edit a 512x512 image, you should set the height and width of output image as 512x512. You also can set the `use_input_image_size_as_output` to automatically set the height and width of output image as the same as input image.
HF Spaces often encounter errors due to quota limitations, so recommend to run it locally.
"""

article = """
Expand Down Expand Up @@ -353,6 +323,7 @@ def run_for_examples(text, img1, img2, img3, height, width, guidance_scale, img_
seed_input = gr.Slider(
label="Seed", minimum=0, maximum=2147483647, value=42, step=1
)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)

max_input_image_size = gr.Slider(
label="max_input_image_size", minimum=128, maximum=2048, value=1024, step=16
Expand Down Expand Up @@ -394,6 +365,7 @@ def run_for_examples(text, img1, img2, img3, height, width, guidance_scale, img_
offload_model,
use_input_image_size_as_output,
max_input_image_size,
randomize_seed,
],
outputs=output_image,
)
Expand All @@ -410,12 +382,9 @@ def run_for_examples(text, img1, img2, img3, height, width, guidance_scale, img_
width_input,
guidance_scale_input,
img_guidance_scale_input,
num_inference_steps,
seed_input,
separate_cfg_infer,
offload_model,
use_input_image_size_as_output,
max_input_image_size,
randomize_seed,
],
outputs=output_image,
)
Expand Down
Binary file added imgs/demo_cases.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed imgs/demo_cases2.png
Binary file not shown.
Binary file added imgs/referring.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed imgs/referring2.png
Binary file not shown.
2 changes: 1 addition & 1 deletion inference.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 4bb793f

Please sign in to comment.