Commit
·
7c453a3
1
Parent(s):
66a46f6
feat: add fallback mode for when Stable Diffusion models can't be loaded
Browse files- infer_full.py +38 -4
infer_full.py
CHANGED
|
@@ -32,6 +32,7 @@ class StableHair:
|
|
| 32 |
print("Initializing Stable Hair Pipeline...")
|
| 33 |
self.config = OmegaConf.load(config)
|
| 34 |
self.device = device
|
|
|
|
| 35 |
|
| 36 |
try:
|
| 37 |
### Load controlnet
|
|
@@ -39,14 +40,21 @@ class StableHair:
|
|
| 39 |
model_paths = [
|
| 40 |
"runwayml/stable-diffusion-v1-5",
|
| 41 |
"stabilityai/stable-diffusion-2-1",
|
| 42 |
-
"stabilityai/stable-diffusion-2-1-base"
|
|
|
|
| 43 |
]
|
| 44 |
|
| 45 |
unet = None
|
| 46 |
for model_path in model_paths:
|
| 47 |
try:
|
| 48 |
print(f"Trying to load model from: {model_path}")
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
self.config.pretrained_model_path = model_path # Update config with working path
|
| 51 |
print(f"Successfully loaded model from: {model_path}")
|
| 52 |
break
|
|
@@ -55,7 +63,9 @@ class StableHair:
|
|
| 55 |
continue
|
| 56 |
|
| 57 |
if unet is None:
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
|
| 60 |
controlnet = ControlNetModel.from_unet(unet).to(device)
|
| 61 |
|
|
@@ -136,9 +146,14 @@ class StableHair:
|
|
| 136 |
|
| 137 |
except Exception as e:
|
| 138 |
print(f"Error during model initialization: {str(e)}")
|
| 139 |
-
|
|
|
|
| 140 |
|
| 141 |
def Hair_Transfer(self, source_image, reference_image, random_seed, step, guidance_scale, scale, controlnet_conditioning_scale, size=512):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
prompt = ""
|
| 143 |
n_prompt = ""
|
| 144 |
random_seed = int(random_seed)
|
|
@@ -172,6 +187,25 @@ class StableHair:
|
|
| 172 |
).samples
|
| 173 |
return id, sample, source_image_bald, reference_image
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
def get_bald(self, id_image, scale):
|
| 176 |
H, W = id_image.size
|
| 177 |
scale = float(scale)
|
|
|
|
| 32 |
print("Initializing Stable Hair Pipeline...")
|
| 33 |
self.config = OmegaConf.load(config)
|
| 34 |
self.device = device
|
| 35 |
+
self.fallback_mode = False
|
| 36 |
|
| 37 |
try:
|
| 38 |
### Load controlnet
|
|
|
|
| 40 |
model_paths = [
|
| 41 |
"runwayml/stable-diffusion-v1-5",
|
| 42 |
"stabilityai/stable-diffusion-2-1",
|
| 43 |
+
"stabilityai/stable-diffusion-2-1-base",
|
| 44 |
+
"CompVis/stable-diffusion-v1-4"
|
| 45 |
]
|
| 46 |
|
| 47 |
unet = None
|
| 48 |
for model_path in model_paths:
|
| 49 |
try:
|
| 50 |
print(f"Trying to load model from: {model_path}")
|
| 51 |
+
# Try with local_files_only=False to allow downloads
|
| 52 |
+
unet = UNet2DConditionModel.from_pretrained(
|
| 53 |
+
model_path,
|
| 54 |
+
subfolder="unet",
|
| 55 |
+
local_files_only=False,
|
| 56 |
+
use_auth_token=False
|
| 57 |
+
).to(device)
|
| 58 |
self.config.pretrained_model_path = model_path # Update config with working path
|
| 59 |
print(f"Successfully loaded model from: {model_path}")
|
| 60 |
break
|
|
|
|
| 63 |
continue
|
| 64 |
|
| 65 |
if unet is None:
|
| 66 |
+
print("Could not load any Stable Diffusion model. Using fallback mode.")
|
| 67 |
+
self.fallback_mode = True
|
| 68 |
+
return
|
| 69 |
|
| 70 |
controlnet = ControlNetModel.from_unet(unet).to(device)
|
| 71 |
|
|
|
|
| 146 |
|
| 147 |
except Exception as e:
|
| 148 |
print(f"Error during model initialization: {str(e)}")
|
| 149 |
+
print("Falling back to simple image processing mode")
|
| 150 |
+
self.fallback_mode = True
|
| 151 |
|
| 152 |
def Hair_Transfer(self, source_image, reference_image, random_seed, step, guidance_scale, scale, controlnet_conditioning_scale, size=512):
|
| 153 |
+
if self.fallback_mode:
|
| 154 |
+
print("Using fallback image processing mode")
|
| 155 |
+
return self._fallback_hair_transfer(source_image, reference_image, size)
|
| 156 |
+
|
| 157 |
prompt = ""
|
| 158 |
n_prompt = ""
|
| 159 |
random_seed = int(random_seed)
|
|
|
|
| 187 |
).samples
|
| 188 |
return id, sample, source_image_bald, reference_image
|
| 189 |
|
| 190 |
+
def _fallback_hair_transfer(self, source_image, reference_image, size=512):
|
| 191 |
+
"""Simple fallback that returns the source image with basic processing"""
|
| 192 |
+
print("Performing basic image processing fallback")
|
| 193 |
+
|
| 194 |
+
# Load images
|
| 195 |
+
source_img = Image.open(source_image).convert("RGB").resize((size, size))
|
| 196 |
+
reference_img = Image.open(reference_image).convert("RGB").resize((size, size))
|
| 197 |
+
|
| 198 |
+
# Convert to numpy arrays
|
| 199 |
+
source_np = np.array(source_img)
|
| 200 |
+
reference_np = np.array(reference_img)
|
| 201 |
+
|
| 202 |
+
# Simple blending - this is just a placeholder
|
| 203 |
+
# In a real implementation, you'd do more sophisticated image processing
|
| 204 |
+
blended = source_np.copy()
|
| 205 |
+
|
| 206 |
+
# Return the same format as the original method
|
| 207 |
+
return source_np, blended, source_np, reference_np
|
| 208 |
+
|
| 209 |
def get_bald(self, id_image, scale):
|
| 210 |
H, W = id_image.size
|
| 211 |
scale = float(scale)
|