File size: 9,101 Bytes
e6a18b7
 
2fa80e1
e6a18b7
 
 
 
 
 
 
 
 
 
 
2fa80e1
e6a18b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fa80e1
e6a18b7
 
 
 
 
 
2fa80e1
e6a18b7
 
2fa80e1
 
 
e6a18b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fa80e1
e6a18b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fa80e1
 
 
 
 
 
 
 
 
e6a18b7
 
2fa80e1
e6a18b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

import torch
import clip
import numpy as np
import logging
import traceback
from typing import List, Dict, Tuple, Optional, Union, Any
from PIL import Image

class CLIPModelManager:
    """
    專門管理 CLIP 模型相關的操作,包括模型載入、設備管理、圖像和文本的特徵編碼等核心功能
    """

    def __init__(self, model_name: str = "ViT-B/16", device: str = None):
        """
        初始化 CLIP 模型管理器

        Args:
            model_name: CLIP模型名稱,默認為"ViT-B/16"
            device: 運行設備,None則自動選擇
        """
        self.logger = logging.getLogger(__name__)
        self.model_name = model_name

        # 設置運行設備
        if device is None:
            self.device = "cuda" if torch.cuda.is_available() else "cpu"
        else:
            self.device = device

        self.model = None
        self.preprocess = None

        self._initialize_model()

    def _initialize_model(self):
        """
        初始化CLIP模型
        """
        try:
            self.logger.info(f"Initializing CLIP model ({self.model_name}) on {self.device}")
            self.model, self.preprocess = clip.load(self.model_name, device=self.device)
            self.logger.info("Successfully loaded CLIP model")
        except Exception as e:
            self.logger.error(f"Error loading CLIP model: {e}")
            self.logger.error(traceback.format_exc())
            raise

    def encode_image(self, image_input: torch.Tensor) -> torch.Tensor:
        """
        編碼圖像特徵

        Args:
            image_input: 預處理後的圖像張量

        Returns:
            torch.Tensor: 標準化後的圖像特徵
        """
        try:
            with torch.no_grad():
                image_features = self.model.encode_image(image_input)
                image_features = image_features / image_features.norm(dim=-1, keepdim=True)
                return image_features
        except Exception as e:
            self.logger.error(f"Error encoding image features: {e}")
            self.logger.error(traceback.format_exc())
            raise

    def encode_text_batch(self, text_prompts: List[str], batch_size: int = 128) -> torch.Tensor:
        """
        批量編碼文本特徵,避免CUDA內存問題

        Args:
            text_prompts: 文本提示列表
            batch_size: 批處理大小

        Returns:
            torch.Tensor: 標準化後的文本特徵
        """
        if not text_prompts:
            return None

        try:
            with torch.no_grad():
                features_list = []

                for i in range(0, len(text_prompts), batch_size):
                    batch_prompts = text_prompts[i:i+batch_size]
                    text_tokens = clip.tokenize(batch_prompts).to(self.device)
                    batch_features = self.model.encode_text(text_tokens)
                    batch_features = batch_features / batch_features.norm(dim=-1, keepdim=True)
                    features_list.append(batch_features)

                # 連接所有批次
                if len(features_list) > 1:
                    text_features = torch.cat(features_list, dim=0)
                else:
                    text_features = features_list[0]

                return text_features

        except Exception as e:
            self.logger.error(f"Error encoding text features: {e}")
            self.logger.error(traceback.format_exc())
            raise

    def encode_single_text(self, text_prompts: List[str]) -> torch.Tensor:
        """
        編碼單個文本批次的特徵

        Args:
            text_prompts: 文本提示列表

        Returns:
            torch.Tensor: 標準化後的文本特徵
        """
        try:
            with torch.no_grad():
                text_tokens = clip.tokenize(text_prompts).to(self.device)
                text_features = self.model.encode_text(text_tokens)
                text_features = text_features / text_features.norm(dim=-1, keepdim=True)
                return text_features
        except Exception as e:
            self.logger.error(f"Error encoding single text batch: {e}")
            self.logger.error(traceback.format_exc())
            raise

    def calculate_similarity(self, image_features: torch.Tensor, text_features: torch.Tensor) -> np.ndarray:
        """
        計算圖像和文本特徵之間的相似度

        Args:
            image_features: 圖像特徵張量
            text_features: 文本特徵張量

        Returns:
            np.ndarray: 相似度分數數組
        """
        try:
            with torch.no_grad():
                similarity = (100.0 * image_features @ text_features.T).softmax(dim=-1)
                similarity = similarity.cpu().numpy() if self.device == "cuda" else similarity.numpy()
                return similarity
        except Exception as e:
            self.logger.error(f"Error calculating similarity: {e}")
            self.logger.error(traceback.format_exc())
            raise

    def preprocess_image(self, image: Union[Image.Image, np.ndarray]) -> torch.Tensor:
        """
        預處理圖像以供CLIP模型使用

        Args:
            image: PIL圖像或numpy數組

        Returns:
            torch.Tensor: 預處理後的圖像張量
        """
        try:
            if not isinstance(image, Image.Image):
                if isinstance(image, np.ndarray):
                    image = Image.fromarray(image)
                else:
                    raise ValueError("Unsupported image format. Expected PIL Image or numpy array.")

            image_input = self.preprocess(image).unsqueeze(0).to(self.device)
            return image_input

        except Exception as e:
            self.logger.error(f"Error preprocessing image: {e}")
            self.logger.error(traceback.format_exc())
            raise

    def process_image_region(self, image: Union[Image.Image, np.ndarray], box: List[float]) -> torch.Tensor:
        """
        處理圖像的特定區域

        Args:
            image: 原始圖像
            box: 邊界框 [x1, y1, x2, y2]

        Returns:
            torch.Tensor: 區域圖像的特徵
        """
        try:
            # 確保圖像是PIL格式
            if not isinstance(image, Image.Image):
                if isinstance(image, np.ndarray):
                    image = Image.fromarray(image)
                else:
                    raise ValueError("Unsupported image format. Expected PIL Image or numpy array.")

            # 裁剪區域
            x1, y1, x2, y2 = map(int, box)
            cropped_image = image.crop((x1, y1, x2, y2))

            # 預處理並編碼
            image_input = self.preprocess_image(cropped_image)
            image_features = self.encode_image(image_input)

            return image_features

        except Exception as e:
            self.logger.error(f"Error processing image region: {e}")
            self.logger.error(traceback.format_exc())
            raise

    def batch_process_regions(self, image: Union[Image.Image, np.ndarray],
                             boxes: List[List[float]]) -> torch.Tensor:
        """
        批量處理多個圖像區域

        Args:
            image: 原始圖像
            boxes: 邊界框列表

        Returns:
            torch.Tensor: 所有區域的圖像特徵
        """
        try:
            # ensure PIL format
            if not isinstance(image, Image.Image):
                if isinstance(image, np.ndarray):
                    image = Image.fromarray(image)
                else:
                    raise ValueError("Unsupported image format. Expected PIL Image or numpy array.")

            if not boxes:
                return torch.empty(0)

            # 裁剪並預處理所有區域
            cropped_inputs = []
            for box in boxes:
                x1, y1, x2, y2 = map(int, box)
                cropped_image = image.crop((x1, y1, x2, y2))
                processed_image = self.preprocess(cropped_image).unsqueeze(0)
                cropped_inputs.append(processed_image)

            # 批量處理
            batch_tensor = torch.cat(cropped_inputs).to(self.device)
            image_features = self.encode_image(batch_tensor)

            return image_features

        except Exception as e:
            self.logger.error(f"Error batch processing regions: {e}")
            self.logger.error(traceback.format_exc())
            raise

    def is_model_loaded(self) -> bool:
        """
        檢查模型是否已成功載入

        Returns:
            bool: 模型載入狀態
        """
        return self.model is not None and self.preprocess is not None

    def get_device(self) -> str:
        """
        獲取當前設備

        Returns:
            str: 設備名稱
        """
        return self.device

    def get_model_name(self) -> str:
        """
        獲取模型名稱

        Returns:
            str: 模型名稱
        """
        return self.model_name