File size: 8,910 Bytes
7e328d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6f74e6
 
66a46f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6f74e6
 
 
 
 
 
 
 
 
 
 
 
7e328d3
c6f74e6
 
 
 
 
 
 
 
7e328d3
c6f74e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e328d3
c6f74e6
 
 
 
 
 
 
 
 
 
 
 
 
 
7e328d3
c6f74e6
 
 
 
 
 
 
 
 
 
7e328d3
c6f74e6
 
 
7e328d3
c6f74e6
 
 
 
 
7e328d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
import torch
from PIL import Image
import numpy as np
from PIL import Image
from omegaconf import OmegaConf
import os
import cv2
from diffusers import DDIMScheduler, UniPCMultistepScheduler
from diffusers.models import UNet2DConditionModel
from ref_encoder.latent_controlnet import ControlNetModel
from ref_encoder.adapter import *
from ref_encoder.reference_unet import ref_unet
from utils.pipeline import StableHairPipeline
from utils.pipeline_cn import StableDiffusionControlNetPipeline

def concatenate_images(image_files, output_file, type="pil"):
    if type == "np":
        image_files = [Image.fromarray(img) for img in image_files]
    images = image_files  # list
    max_height = max(img.height for img in images)
    images = [img.resize((img.width, max_height)) for img in images]
    total_width = sum(img.width for img in images)
    combined = Image.new('RGB', (total_width, max_height))
    x_offset = 0
    for img in images:
        combined.paste(img, (x_offset, 0))
        x_offset += img.width
    combined.save(output_file)

class StableHair:
    def __init__(self, config="stable_hair/configs/hair_transfer.yaml", device="cuda", weight_dtype=torch.float16) -> None:
        print("Initializing Stable Hair Pipeline...")
        self.config = OmegaConf.load(config)
        self.device = device

        try:
            ### Load controlnet
            # Try multiple model paths in case of access issues
            model_paths = [
                "runwayml/stable-diffusion-v1-5",
                "stabilityai/stable-diffusion-2-1", 
                "stabilityai/stable-diffusion-2-1-base"
            ]
            
            unet = None
            for model_path in model_paths:
                try:
                    print(f"Trying to load model from: {model_path}")
                    unet = UNet2DConditionModel.from_pretrained(model_path, subfolder="unet").to(device)
                    self.config.pretrained_model_path = model_path  # Update config with working path
                    print(f"Successfully loaded model from: {model_path}")
                    break
                except Exception as e:
                    print(f"Failed to load {model_path}: {str(e)}")
                    continue
            
            if unet is None:
                raise Exception("Could not load any Stable Diffusion model")
            
            controlnet = ControlNetModel.from_unet(unet).to(device)
            
            # Try to load custom controlnet weights, fallback to default if not available
            controlnet_path = os.path.join(self.config.pretrained_folder, self.config.controlnet_path)
            if os.path.exists(controlnet_path):
                print(f"Loading custom controlnet from {controlnet_path}")
                _state_dict = torch.load(controlnet_path)
                controlnet.load_state_dict(_state_dict, strict=False)
            else:
                print(f"Custom controlnet not found at {controlnet_path}, using default")
            
            controlnet.to(weight_dtype)

            ### >>> create pipeline >>> ###
            self.pipeline = StableHairPipeline.from_pretrained(
                self.config.pretrained_model_path,
                controlnet=controlnet,
                safety_checker=None,
                torch_dtype=weight_dtype,
            ).to(device)
            self.pipeline.scheduler = UniPCMultistepScheduler.from_config(self.pipeline.scheduler.config)

            ### load Hair encoder/adapter
            self.hair_encoder = ref_unet.from_pretrained(self.config.pretrained_model_path, subfolder="unet").to(device)
            
            # Try to load custom encoder weights, fallback to default if not available
            encoder_path = os.path.join(self.config.pretrained_folder, self.config.encoder_path)
            if os.path.exists(encoder_path):
                print(f"Loading custom encoder from {encoder_path}")
                _state_dict = torch.load(encoder_path)
                self.hair_encoder.load_state_dict(_state_dict, strict=False)
            else:
                print(f"Custom encoder not found at {encoder_path}, using default")
            
            self.hair_adapter = adapter_injection(self.pipeline.unet, device=self.device, dtype=torch.float16, use_resampler=False)
            
            # Try to load custom adapter weights, fallback to default if not available
            adapter_path = os.path.join(self.config.pretrained_folder, self.config.adapter_path)
            if os.path.exists(adapter_path):
                print(f"Loading custom adapter from {adapter_path}")
                _state_dict = torch.load(adapter_path)
                self.hair_adapter.load_state_dict(_state_dict, strict=False)
            else:
                print(f"Custom adapter not found at {adapter_path}, using default")

            ### load bald converter
            bald_converter = ControlNetModel.from_unet(unet).to(device)
            
            # Try to load custom bald converter weights, fallback to default if not available
            bald_converter_path = self.config.bald_converter_path
            if os.path.exists(bald_converter_path):
                print(f"Loading custom bald converter from {bald_converter_path}")
                _state_dict = torch.load(bald_converter_path)
                bald_converter.load_state_dict(_state_dict, strict=False)
            else:
                print(f"Custom bald converter not found at {bald_converter_path}, using default")
            
            bald_converter.to(dtype=weight_dtype)
            del unet

            ### create pipeline for hair removal
            self.remove_hair_pipeline = StableDiffusionControlNetPipeline.from_pretrained(
                self.config.pretrained_model_path,
                controlnet=bald_converter,
                safety_checker=None,
                torch_dtype=weight_dtype,
            )
            self.remove_hair_pipeline.scheduler = UniPCMultistepScheduler.from_config(
                self.remove_hair_pipeline.scheduler.config)
            self.remove_hair_pipeline = self.remove_hair_pipeline.to(device)

            ### move to fp16
            self.hair_encoder.to(weight_dtype)
            self.hair_adapter.to(weight_dtype)

            print("Initialization Done!")
            
        except Exception as e:
            print(f"Error during model initialization: {str(e)}")
            raise Exception(f"Model initialization failed: {str(e)}")

    def Hair_Transfer(self, source_image, reference_image, random_seed, step, guidance_scale, scale, controlnet_conditioning_scale, size=512):
        prompt = ""
        n_prompt = ""
        random_seed = int(random_seed)
        step = int(step)
        guidance_scale = float(guidance_scale)
        scale = float(scale)

        # load imgs
        source_image = Image.open(source_image).convert("RGB").resize((size, size))
        id = np.array(source_image)
        reference_image = np.array(Image.open(reference_image).convert("RGB").resize((size, size)))
        source_image_bald = np.array(self.get_bald(source_image, scale=0.9))
        H, W, C = source_image_bald.shape

        # generate images
        set_scale(self.pipeline.unet, scale)
        generator = torch.Generator(device="cuda")
        generator.manual_seed(random_seed)
        sample = self.pipeline(
            prompt,
            negative_prompt=n_prompt,
            num_inference_steps=step,
            guidance_scale=guidance_scale,
            width=W,
            height=H,
            controlnet_condition=source_image_bald,
            controlnet_conditioning_scale=controlnet_conditioning_scale,
            generator=generator,
            reference_encoder=self.hair_encoder,
            ref_image=reference_image,
        ).samples
        return id, sample, source_image_bald, reference_image

    def get_bald(self, id_image, scale):
        H, W = id_image.size
        scale = float(scale)
        image = self.remove_hair_pipeline(
            prompt="",
            negative_prompt="",
            num_inference_steps=30,
            guidance_scale=1.5,
            width=W,
            height=H,
            image=id_image,
            controlnet_conditioning_scale=scale,
            generator=None,
        ).images[0]

        return image


if __name__ == '__main__':
    model = StableHair(config="./configs/hair_transfer.yaml", weight_dtype=torch.float32)
    kwargs = OmegaConf.to_container(model.config.inference_kwargs)
    id, image, source_image_bald, reference_image = model.Hair_Transfer(**kwargs)
    os.makedirs(model.config.output_path, exist_ok=True)
    output_file = os.path.join(model.config.output_path, model.config.save_name)
    concatenate_images([id, source_image_bald, reference_image, (image*255.).astype(np.uint8)], output_file=output_file, type="np")