File size: 10,083 Bytes
4e4b650
 
 
 
 
 
 
d81760a
 
 
4e4b650
 
 
d81760a
4e4b650
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d81760a
 
 
 
 
 
 
 
 
 
 
 
 
 
4e4b650
d81760a
 
 
4e4b650
d81760a
4e4b650
 
d81760a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e4b650
d81760a
4e4b650
d81760a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e4b650
d81760a
9cd6532
01eb3dc
d81760a
9cd6532
d81760a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e4b650
d81760a
 
4e4b650
d81760a
 
 
 
 
 
 
 
 
 
 
 
4e4b650
 
d81760a
4e4b650
d81760a
 
 
4e4b650
 
 
d81760a
 
 
 
 
 
4e4b650
 
d81760a
 
 
4e4b650
 
d81760a
 
 
 
 
 
 
4e4b650
d81760a
4e4b650
 
d81760a
 
4e4b650
 
d81760a
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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from model import DesignModel
from PIL import Image
import numpy as np
from typing import List
import random
import time
import torch
from diffusers.pipelines.controlnet import StableDiffusionControlNetInpaintPipeline
from diffusers import ControlNetModel, UniPCMultistepScheduler, AutoPipelineForText2Image
from transformers import AutoImageProcessor, UperNetForSemanticSegmentation, AutoModelForDepthEstimation
import logging
import os
from datetime import datetime
import gc

# Set up logging
log_dir = "logs"
os.makedirs(log_dir, exist_ok=True)
log_file = os.path.join(log_dir, f"prod_model_{datetime.now().strftime('%Y%m%d')}.log")

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler(log_file),
        logging.StreamHandler()
    ]
)

class ProductionDesignModel(DesignModel):
    def __init__(self):
        """Initialize the production model with advanced architecture"""
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
        self.dtype = torch.float16 if self.device == "cuda" else torch.float32
        
        # Setup logging
        logging.basicConfig(filename=f'logs/prod_model_{time.strftime("%Y%m%d")}.log',
                          level=logging.INFO,
                          format='%(asctime)s - %(levelname)s - %(message)s')
        
        self.seed = 323*111
        self.neg_prompt = "window, door, low resolution, banner, logo, watermark, text, deformed, blurry, out of focus, surreal, ugly, beginner"
        self.control_items = ["windowpane;window", "door;double;door"]
        self.additional_quality_suffix = "interior design, 4K, high resolution, photorealistic"
        
        try:
            logging.info(f"Initializing models on {self.device} with {self.dtype}")
            self._initialize_models()
            logging.info("Models initialized successfully")
        except Exception as e:
            logging.error(f"Error initializing models: {e}")
            raise

    def _initialize_models(self):
        """Initialize all required models and pipelines"""
        # Initialize ControlNet models
        self.controlnet_depth = ControlNetModel.from_pretrained(
            "controlnet_depth", torch_dtype=self.dtype, use_safetensors=True
        )
        self.controlnet_seg = ControlNetModel.from_pretrained(
            "own_controlnet", torch_dtype=self.dtype, use_safetensors=True
        )

        # Initialize main pipeline
        self.pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
            "SG161222/Realistic_Vision_V5.1_noVAE",
            controlnet=[self.controlnet_depth, self.controlnet_seg],
            safety_checker=None,
            torch_dtype=self.dtype
        )

        # Setup IP-Adapter
        self.pipe.load_ip_adapter("h94/IP-Adapter", subfolder="models",
                                weight_name="ip-adapter_sd15.bin")
        self.pipe.set_ip_adapter_scale(0.4)
        self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
        self.pipe = self.pipe.to(self.device)

        # Initialize guide pipeline
        self.guide_pipe = AutoPipelineForText2Image.from_pretrained(
            "segmind/SSD-1B",
            torch_dtype=self.dtype,
            use_safetensors=True,
            variant="fp16"
        ).to(self.device)

        # Initialize segmentation and depth models
        self.seg_processor, self.seg_model = self._init_segmentation()
        self.depth_processor, self.depth_model = self._init_depth()
        self.depth_model = self.depth_model.to(self.device)

    def _init_segmentation(self):
        """Initialize segmentation models"""
        processor = AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-small")
        model = UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-small")
        return processor, model

    def _init_depth(self):
        """Initialize depth estimation models"""
        processor = AutoImageProcessor.from_pretrained(
            "LiheYoung/depth-anything-large-hf",
            torch_dtype=self.dtype
        )
        model = AutoModelForDepthEstimation.from_pretrained(
            "LiheYoung/depth-anything-large-hf",
            torch_dtype=self.dtype
        )
        return processor, model

    def _get_depth_map(self, image: Image.Image) -> Image.Image:
        """Generate depth map for input image"""
        image_to_depth = self.depth_processor(images=image, return_tensors="pt").to(self.device)
        with torch.inference_mode():
            depth_map = self.depth_model(**image_to_depth).predicted_depth

        width, height = image.size
        depth_map = torch.nn.functional.interpolate(
            depth_map.unsqueeze(1).float(),
            size=(height, width),
            mode="bicubic",
            align_corners=False,
        )
        depth_min = torch.amin(depth_map, dim=[1, 2, 3], keepdim=True)
        depth_max = torch.amax(depth_map, dim=[1, 2, 3], keepdim=True)
        depth_map = (depth_map - depth_min) / (depth_max - depth_min)
        image = torch.cat([depth_map] * 3, dim=1)

        image = image.permute(0, 2, 3, 1).cpu().numpy()[0]
        return Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))

    def _segment_image(self, image: Image.Image) -> Image.Image:
        """Generate segmentation map for input image"""
        pixel_values = self.seg_processor(image, return_tensors="pt").pixel_values
        with torch.inference_mode():
            outputs = self.seg_model(pixel_values)

        seg = self.seg_processor.post_process_semantic_segmentation(
            outputs, target_sizes=[image.size[::-1]])[0]
        color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8)
        
        # You'll need to implement the palette mapping here
        # This is a placeholder - you should implement proper color mapping
        for label in range(seg.max() + 1):
            color_seg[seg == label, :] = [label * 30 % 255] * 3
            
        return Image.fromarray(color_seg).convert('RGB')

    def _resize_image(self, image: Image.Image, target_size: int) -> Image.Image:
        """Resize image while maintaining aspect ratio"""
        width, height = image.size
        if width > height:
            new_width = target_size
            new_height = int(height * (target_size / width))
        else:
            new_height = target_size
            new_width = int(width * (target_size / height))
        return image.resize((new_width, new_height), Image.LANCZOS)

    def _flush(self):
        """Clear CUDA cache"""
        gc.collect()
        if torch.cuda.is_available():
            torch.cuda.empty_cache()

    def generate_design(self, image: Image.Image, prompt: str, **kwargs) -> List[Image.Image]:
        """
        Generate design variations based on input image and prompt
        """
        try:
            # Set seed
            seed_param = kwargs.get('seed')
            base_seed = int(time.time()) if seed_param is None else int(seed_param)
            self.generator = torch.Generator(device=self.device).manual_seed(base_seed)
            
            # Get parameters
            num_variations = kwargs.get('num_variations', 1)
            guidance_scale = float(kwargs.get('guidance_scale', 10.0))
            num_steps = int(kwargs.get('num_steps', 50))
            strength = float(kwargs.get('strength', 0.9))
            img_size = int(kwargs.get('img_size', 768))

            logging.info(f"Generating design with parameters: guidance_scale={guidance_scale}, "
                        f"num_steps={num_steps}, strength={strength}, img_size={img_size}")

            # Prepare prompt
            pos_prompt = f"{prompt}, {self.additional_quality_suffix}"

            # Process input image
            orig_size = image.size
            input_image = self._resize_image(image, img_size)
            
            # Generate depth map
            depth_map = self._get_depth_map(input_image)
            
            # Generate segmentation
            seg_map = self._segment_image(input_image)

            # Generate IP-adapter reference image
            self._flush()
            ip_image = self.guide_pipe(
                pos_prompt,
                num_inference_steps=num_steps,
                negative_prompt=self.neg_prompt,
                generator=self.generator
            ).images[0]

            # Generate variations
            variations = []
            for i in range(num_variations):
                try:
                    self._flush()
                    variation = self.pipe(
                        prompt=pos_prompt,
                        negative_prompt=self.neg_prompt,
                        num_inference_steps=num_steps,
                        strength=strength,
                        guidance_scale=guidance_scale,
                        generator=self.generator,
                        image=input_image,
                        ip_adapter_image=ip_image,
                        control_image=[depth_map, seg_map],
                        controlnet_conditioning_scale=[0.5, 0.5]
                    ).images[0]
                    
                    # Resize back to original size
                    variation = variation.resize(orig_size, Image.LANCZOS)
                    variations.append(variation)
                    
                except Exception as e:
                    logging.error(f"Error generating variation {i}: {e}")
                    continue

            if not variations:
                logging.warning("No variations were generated successfully")
                return [image]  # Return original image if no variations were generated

            return variations

        except Exception as e:
            logging.error(f"Error in generate_design: {e}")
            return [image]  # Return original image in case of error

    def __del__(self):
        """Cleanup when the model is deleted"""
        self._flush()