Spaces:
Paused
Paused
Basic outpainting pipeline with live camera feed
Browse files- app.py +69 -16
- requirements.txt +1 -0
app.py
CHANGED
@@ -1,35 +1,88 @@
|
|
1 |
import gradio
|
2 |
import torch
|
3 |
-
import
|
|
|
4 |
from torchvision import transforms
|
5 |
-
|
6 |
from diffusers import StableDiffusionInpaintPipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
#revision="fp16",
|
10 |
-
#torch_dtype=torch.float16,
|
11 |
-
safety_checker=lambda images, **kwargs: (images, False))
|
12 |
|
13 |
-
|
|
|
|
|
14 |
#generator = torch.Generator(device).manual_seed(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
negative_prompt=negativePrompt,
|
19 |
image=inputImage,
|
20 |
mask_image=mask,
|
21 |
guidance_scale=guidanceScale,
|
22 |
-
num_inference_steps=numInferenceSteps
|
|
|
|
|
|
|
23 |
|
24 |
prompt = gradio.Textbox(label="Prompt", placeholder="A person in a room", lines=3)
|
25 |
negativePrompt = gradio.Textbox(label="Negative Prompt", placeholder="Text", lines=3)
|
26 |
-
|
27 |
-
inputImage = gradio.Image(label="Input
|
28 |
-
#inputFeed = gradio.Image(label="Input Feed", source="webcam", streaming=True)
|
29 |
mask = gradio.Image(label="Mask", type="pil")
|
30 |
outputImage = gradio.Image(label="Extrapolated Field of View")
|
31 |
-
guidanceScale = gradio.Slider(label="Guidance Scale", maximum=1, value
|
32 |
-
numInferenceSteps = gradio.Slider(label="Number of Inference Steps", maximum=100, value
|
|
|
|
|
|
|
33 |
|
34 |
-
|
|
|
35 |
ux.launch()
|
|
|
1 |
import gradio
|
2 |
import torch
|
3 |
+
import numpy
|
4 |
+
from PIL import Image
|
5 |
from torchvision import transforms
|
6 |
+
#from torchvision import transforms
|
7 |
from diffusers import StableDiffusionInpaintPipeline
|
8 |
+
#from diffusers import StableDiffusionUpscalePipeline
|
9 |
+
#from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
|
10 |
+
from diffusers import DPMSolverMultistepScheduler
|
11 |
+
|
12 |
+
deviceStr = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
device = torch.device(deviceStr)
|
14 |
+
|
15 |
+
if deviceStr == "cuda":
|
16 |
+
pipeline = StableDiffusionInpaintPipeline.from_pretrained("runwayml/stable-diffusion-inpainting",
|
17 |
+
revision="fp16",
|
18 |
+
torch_dtype=torch.float16,
|
19 |
+
safety_checker=lambda images, **kwargs: (images, False))
|
20 |
+
else:
|
21 |
+
pipeline = StableDiffusionInpaintPipeline.from_pretrained("runwayml/stable-diffusion-inpainting",
|
22 |
+
revision="fp16",
|
23 |
+
torch_dtype=torch.float16,
|
24 |
+
safety_checker=lambda images, **kwargs: (images, False))
|
25 |
|
26 |
+
#superresolutionPipe = StableDiffusionUpscalePipeline.from_pretrained("stabilityai/stable-diffusion-x4-upscaler")
|
|
|
|
|
|
|
27 |
|
28 |
+
pipeline.to(device)
|
29 |
+
pipeline.enable_xformers_memory_efficient_attention()
|
30 |
+
#pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config)
|
31 |
#generator = torch.Generator(device).manual_seed(seed)
|
32 |
+
latents = torch.randn((1, 4, 64, 64), device=device)
|
33 |
+
schedulers = [
|
34 |
+
"DDIMScheduler", "LMSDiscreteScheduler", "PNDMScheduler"
|
35 |
+
]
|
36 |
+
latentNoiseInputs = [
|
37 |
+
"Uniform", "Low Discrepency Sequence"
|
38 |
+
]
|
39 |
+
|
40 |
+
imageSize = (512, 512, 3)
|
41 |
+
imageSize2 = (512, 512)
|
42 |
+
#lastImage = Image.new(mode="RGB", size=(imageSize[0], imageSize[1]))
|
43 |
+
|
44 |
+
def diffuse(prompt, negativePrompt, inputImage, mask, guidanceScale, numInferenceSteps, seed, noiseScheduler, latentNoise):
|
45 |
+
#width = inputImage.size[1]
|
46 |
+
#height = 512
|
47 |
+
#print(inputImage.size)
|
48 |
+
#image = numpy.resize(inputImage, imageSize)
|
49 |
+
#pilImage.thumbnail(imageSize2)
|
50 |
+
|
51 |
+
#transforms.Resize(imageSize2)(inputImage)
|
52 |
|
53 |
+
#pilImage = Image.fromarray(inputImage)
|
54 |
+
#pilImage.resize(imageSize2)
|
55 |
+
#imageArray = numpy.asarray(pilImage)
|
56 |
+
|
57 |
+
#inputImage = torch.nn.functional.interpolate(inputImage, size=imageSize)
|
58 |
+
|
59 |
+
if mask is None:
|
60 |
+
return inputImage
|
61 |
+
|
62 |
+
generator = torch.Generator(device).manual_seed(seed)
|
63 |
+
|
64 |
+
newImage = pipeline(prompt=prompt,
|
65 |
negative_prompt=negativePrompt,
|
66 |
image=inputImage,
|
67 |
mask_image=mask,
|
68 |
guidance_scale=guidanceScale,
|
69 |
+
num_inference_steps=numInferenceSteps,
|
70 |
+
generator=generator).images[0]
|
71 |
+
|
72 |
+
return newImage
|
73 |
|
74 |
prompt = gradio.Textbox(label="Prompt", placeholder="A person in a room", lines=3)
|
75 |
negativePrompt = gradio.Textbox(label="Negative Prompt", placeholder="Text", lines=3)
|
76 |
+
#inputImage = gradio.Image(label="Input Image", type="pil")
|
77 |
+
inputImage = gradio.Image(label="Input Feed", source="webcam", shape=[512,512], streaming=True)
|
|
|
78 |
mask = gradio.Image(label="Mask", type="pil")
|
79 |
outputImage = gradio.Image(label="Extrapolated Field of View")
|
80 |
+
guidanceScale = gradio.Slider(label="Guidance Scale", maximum=1, value=0.75)
|
81 |
+
numInferenceSteps = gradio.Slider(label="Number of Inference Steps", maximum=100, value=25)
|
82 |
+
seed = gradio.Slider(label="Generator Seed", maximum=1000, value=512)
|
83 |
+
noiseScheduler = gradio.Dropdown(schedulers, label="Noise Scheduler", value="DDIMScheduler")
|
84 |
+
latentNoise = gradio.Dropdown(latentNoiseInputs, label="Latent Noise", value="Iniform")
|
85 |
|
86 |
+
inputs=[prompt, negativePrompt, inputImage, mask, guidanceScale, numInferenceSteps, seed, noiseScheduler, latentNoise]
|
87 |
+
ux = gradio.Interface(fn=diffuse, title="View Diffusion", inputs=inputs, outputs=outputImage, live=True)
|
88 |
ux.launch()
|
requirements.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
Pillow
|
2 |
torch
|
3 |
torchvision
|
|
|
1 |
+
numpy
|
2 |
Pillow
|
3 |
torch
|
4 |
torchvision
|