Spaces:
Sleeping
Sleeping
Commit
·
0154081
1
Parent(s):
5fd5d22
Comprehensive model and library optimization - all components verified and error-handled
Browse files- app.py +12 -0
- runners/simple_runner.py +129 -75
app.py
CHANGED
|
@@ -33,6 +33,18 @@ def ensure_weights():
|
|
| 33 |
need = [
|
| 34 |
"pretrained_models/sfe_editor_light.pt",
|
| 35 |
"pretrained_models/stylegan2-ffhq-config-f.pt",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
]
|
| 37 |
|
| 38 |
# Check if any of the needed files exist
|
|
|
|
| 33 |
need = [
|
| 34 |
"pretrained_models/sfe_editor_light.pt",
|
| 35 |
"pretrained_models/stylegan2-ffhq-config-f.pt",
|
| 36 |
+
"pretrained_models/e4e_ffhq_encode.pt",
|
| 37 |
+
"pretrained_models/stylegan2-ffhq-config-f.pkl",
|
| 38 |
+
"pretrained_models/shape_predictor_68_face_landmarks.dat",
|
| 39 |
+
"pretrained_models/fs3.npy",
|
| 40 |
+
"pretrained_models/delta_mapper.pt",
|
| 41 |
+
"pretrained_models/iresnet50-7f187506.pth",
|
| 42 |
+
"pretrained_models/model_ir_se50.pth",
|
| 43 |
+
"pretrained_models/CurricularFace_Backbone.pth",
|
| 44 |
+
"pretrained_models/face_parsing.farl.lapa.main_ema_136500_jit191.pt",
|
| 45 |
+
"pretrained_models/mobilenet0.25_Final.pth",
|
| 46 |
+
"pretrained_models/moco_v2_800ep_pretrain.pt",
|
| 47 |
+
"pretrained_models/79999_iter.pth",
|
| 48 |
]
|
| 49 |
|
| 50 |
# Check if any of the needed files exist
|
runners/simple_runner.py
CHANGED
|
@@ -136,15 +136,31 @@ class SimpleRunner:
|
|
| 136 |
editor_ckpt_pth: str,
|
| 137 |
simple_config_pth: str = "configs/simple_inference.yaml"
|
| 138 |
):
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
def edit(
|
| 150 |
self,
|
|
@@ -159,72 +175,110 @@ class SimpleRunner:
|
|
| 159 |
save_e4e=False,
|
| 160 |
save_inversion=False
|
| 161 |
):
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
def available_editings(self):
|
| 230 |
edits_types = []
|
|
|
|
| 136 |
editor_ckpt_pth: str,
|
| 137 |
simple_config_pth: str = "configs/simple_inference.yaml"
|
| 138 |
):
|
| 139 |
+
print(f"Initializing SimpleRunner with checkpoint: {editor_ckpt_pth}")
|
| 140 |
+
|
| 141 |
+
try:
|
| 142 |
+
config = OmegaConf.load(simple_config_pth)
|
| 143 |
+
config.model.checkpoint_path = editor_ckpt_pth
|
| 144 |
+
config.methods_args.fse_full = {}
|
| 145 |
+
print("Configuration loaded successfully")
|
| 146 |
+
|
| 147 |
+
self.inference_runner = FSEInferenceRunner(config)
|
| 148 |
+
print("FSEInferenceRunner created")
|
| 149 |
+
|
| 150 |
+
self.inference_runner.setup()
|
| 151 |
+
print("Inference runner setup completed")
|
| 152 |
+
|
| 153 |
+
self.inference_runner.method.eval()
|
| 154 |
+
print("Model set to evaluation mode")
|
| 155 |
+
|
| 156 |
+
self.inference_runner.method.decoder = self.inference_runner.method.decoder.float()
|
| 157 |
+
print("Decoder converted to float precision")
|
| 158 |
+
|
| 159 |
+
print("SimpleRunner initialization completed successfully")
|
| 160 |
+
|
| 161 |
+
except Exception as e:
|
| 162 |
+
print(f"Error during SimpleRunner initialization: {e}")
|
| 163 |
+
raise
|
| 164 |
|
| 165 |
def edit(
|
| 166 |
self,
|
|
|
|
| 175 |
save_e4e=False,
|
| 176 |
save_inversion=False
|
| 177 |
):
|
| 178 |
+
try:
|
| 179 |
+
print(f"Starting edit: {editing_name} with power {edited_power}")
|
| 180 |
+
print(f"Input image: {orig_img_pth}")
|
| 181 |
+
print(f"Output path: {save_pth}")
|
| 182 |
+
print(f"Face alignment: {align}")
|
| 183 |
+
print(f"Use mask: {use_mask}")
|
| 184 |
+
|
| 185 |
+
save_pth = Path(save_pth)
|
| 186 |
+
save_pth_dir = save_pth.parents[0]
|
| 187 |
+
save_pth_dir.mkdir(parents=True, exist_ok=True)
|
| 188 |
+
aligned_image_pth = orig_img_pth
|
| 189 |
+
|
| 190 |
+
if align:
|
| 191 |
+
print(f"Running face alignment on {orig_img_pth}")
|
| 192 |
+
try:
|
| 193 |
+
aligned_image, unalign_dict = run_alignment(orig_img_pth)
|
| 194 |
+
save_align_pth = save_pth.parents[0] / (save_pth.stem + "_aligned.jpg")
|
| 195 |
+
print(f"Save aligned image to {save_align_pth}")
|
| 196 |
+
aligned_image.convert('RGB').save(save_align_pth)
|
| 197 |
+
aligned_image_pth = save_align_pth
|
| 198 |
+
print(f"Face alignment completed. Using aligned image: {aligned_image_pth}")
|
| 199 |
+
except Exception as e:
|
| 200 |
+
print(f"Face alignment failed: {e}")
|
| 201 |
+
print("Continuing without alignment...")
|
| 202 |
+
align = False
|
| 203 |
+
|
| 204 |
+
if use_mask and mask_path is None:
|
| 205 |
+
print("Preparing mask")
|
| 206 |
+
try:
|
| 207 |
+
mask_path = extract_mask(aligned_image_pth, save_pth.parents[0], trash=mask_trashold)
|
| 208 |
+
print("Mask extraction completed")
|
| 209 |
+
except Exception as e:
|
| 210 |
+
print(f"Mask extraction failed: {e}")
|
| 211 |
+
print("Continuing without mask...")
|
| 212 |
+
use_mask = False
|
| 213 |
+
|
| 214 |
+
if use_mask and mask_path is not None:
|
| 215 |
+
print(f"Using mask from {mask_path}")
|
| 216 |
+
mask = Image.open(mask_path).convert("RGB")
|
| 217 |
+
transform = transforms.ToTensor()
|
| 218 |
+
mask = transform(mask).unsqueeze(0).to(self.inference_runner.device)
|
| 219 |
+
else:
|
| 220 |
+
mask = None
|
| 221 |
+
|
| 222 |
+
print("Loading and preprocessing image")
|
| 223 |
+
orig_img = Image.open(aligned_image_pth).convert("RGB")
|
| 224 |
+
transform_dict = transforms_registry["face_1024"]().get_transforms()
|
| 225 |
+
orig_img = transform_dict["test"](orig_img).unsqueeze(0)
|
| 226 |
+
|
| 227 |
+
device = self.inference_runner.device
|
| 228 |
+
print(f"Using device: {device}")
|
| 229 |
+
|
| 230 |
+
print("Running image inversion")
|
| 231 |
+
inv_images, inversion_results = self.inference_runner._run_on_batch(orig_img.to(device))
|
| 232 |
+
print("Image inversion completed")
|
| 233 |
+
|
| 234 |
+
print(f"Running editing: {editing_name}")
|
| 235 |
+
edited_image = self.inference_runner._run_editing_on_batch(
|
| 236 |
+
method_res_batch=inversion_results,
|
| 237 |
+
editing_name=editing_name,
|
| 238 |
+
editing_degrees=[edited_power],
|
| 239 |
+
mask=mask,
|
| 240 |
+
return_e4e=save_e4e
|
| 241 |
+
)
|
| 242 |
+
print("Editing completed")
|
| 243 |
+
|
| 244 |
+
if save_inversion:
|
| 245 |
+
save_inv_pth = save_pth.parents[0] / (save_pth.stem + "_inversion.jpg")
|
| 246 |
+
inv_image = tensor2im(inv_images[0].cpu())
|
| 247 |
+
inv_image.save(save_inv_pth)
|
| 248 |
+
|
| 249 |
+
if save_e4e:
|
| 250 |
+
edited_image, e4e_inv, e4e_edit = edited_image
|
| 251 |
+
|
| 252 |
+
save_e4e_inv_pth = save_pth.parents[0] / (save_pth.stem + "_e4e_inversion.jpg")
|
| 253 |
+
e4e_inv_image = tensor2im(e4e_inv[0].cpu())
|
| 254 |
+
e4e_inv_image.save(save_e4e_inv_pth)
|
| 255 |
+
|
| 256 |
+
save_e4e_edit_pth = save_pth.parents[0] / (save_pth.stem + "_e4e_edit.jpg")
|
| 257 |
+
e4e_edit_image = tensor2im(e4e_edit[0].cpu())
|
| 258 |
+
e4e_edit_image.save(save_e4e_edit_pth)
|
| 259 |
+
|
| 260 |
+
print("Converting and saving final result")
|
| 261 |
+
edited_image = tensor2im(edited_image[0][0].cpu())
|
| 262 |
+
edited_image.save(save_pth)
|
| 263 |
+
print(f"Final result saved to: {save_pth}")
|
| 264 |
+
|
| 265 |
+
if align:
|
| 266 |
+
try:
|
| 267 |
+
unaligned_path = save_pth.parents[0] / (save_pth.stem + "_unaligned.jpg")
|
| 268 |
+
unalign(edited_image, unalign_dict, orig_img_pth, unaligned_path)
|
| 269 |
+
print("Unalign completed")
|
| 270 |
+
except Exception as e:
|
| 271 |
+
print(f"Unalign failed: {e}")
|
| 272 |
+
print("Using aligned result as final output")
|
| 273 |
+
|
| 274 |
+
print("Edit process completed successfully")
|
| 275 |
+
return edited_image
|
| 276 |
+
|
| 277 |
+
except Exception as e:
|
| 278 |
+
print(f"Error during edit process: {e}")
|
| 279 |
+
import traceback
|
| 280 |
+
traceback.print_exc()
|
| 281 |
+
raise
|
| 282 |
|
| 283 |
def available_editings(self):
|
| 284 |
edits_types = []
|