-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinference.py
77 lines (64 loc) · 2.23 KB
/
inference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# USAGE:
# python inference.py --image_url "https://i.imgur.com/4ujXoav.jpeg" --edit_prompt "change the bottle to a firecracker"
import argparse
import torch
from open_generative_fill import config
from open_generative_fill.lm_models import run_lm_model
from open_generative_fill.load_data import load_image
from open_generative_fill.vision_models import (
run_caption_model,
run_inpainting_pipeline,
run_segmentaiton_pipeline,
)
def load_arguments(parser):
parser = argparse.ArgumentParser(
description="Inference for the Open Generative Fill"
)
parser.add_argument(
"--image_url", type=str, help="The image url to use for inference"
)
parser.add_argument(
"--edit_prompt", type=str, help="The edit prompt to use for inference"
)
return parser.parse_args()
if __name__ == "__main__":
# Get the arguments from the command line
parser = argparse.ArgumentParser(
description="Inference for the Open Generative Fill"
)
arguments = load_arguments(parser)
# Set the device
device = "cuda" if torch.cuda.is_available() else "cpu"
# Load the image from the url and get the text prompt
image_url = arguments.image_url
image = load_image(image_url=image_url, image_size=config.IMAGE_SIZE)
edit_prompt = arguments.edit_prompt
# Image captioning
caption = run_caption_model(
model_id=config.CAPTION_MODEL_ID, image=image, device=device
)
# Language model
to_replace, replaced_caption = run_lm_model(
model_id=config.LANGUAGE_MODEL_ID,
caption=caption,
edit_prompt=edit_prompt,
device=device,
)
# Segmentation pipeline
segmentation_mask = run_segmentaiton_pipeline(
detection_model_id=config.DETECTION_MODEL_ID,
segmentation_model_id=config.SEGMENTATION_MODEL_ID,
to_replace=to_replace,
image=image,
device=device,
)
# Inpainting pipeline
output = run_inpainting_pipeline(
inpainting_model_id=config.INPAINTING_MODEL_ID,
image=image,
mask=segmentation_mask,
replaced_caption=replaced_caption,
image_size=config.IMAGE_SIZE,
generator=torch.Generator().manual_seed(17),
device=device,
)