File size: 15,114 Bytes
466ea14 |
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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
import chromadb
import logging
import open_clip
import torch
from PIL import Image
import numpy as np
from transformers import pipeline
import requests
import io
import json
import uuid
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm
import os
from io import BytesIO
from chromadb.utils.embedding_functions import OpenCLIPEmbeddingFunction
from chromadb.utils.data_loaders import ImageLoader
# ๋ก๊น
์ค์
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('fashion_db_creation.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
def load_models():
try:
logger.info("Loading models...")
# CLIP ๋ชจ๋ธ
model, _, preprocess_val = open_clip.create_model_and_transforms('hf-hub:Marqo/marqo-fashionSigLIP')
# ์ธ๊ทธ๋ฉํ
์ด์
๋ชจ๋ธ
segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ๋ฅผ ์ํ transforms ์ถ๊ฐ
from torchvision import transforms
resize_transform = transforms.Compose([
transforms.Resize((224, 224)), # CLIP ์
๋ ฅ ํฌ๊ธฐ์ ๋ง์ถค
transforms.ToTensor(),
])
return model, preprocess_val, segmenter, device, resize_transform
except Exception as e:
logger.error(f"Error loading models: {e}")
raise
def process_segmentation(image, segmenter):
"""Segmentation processing"""
try:
output = segmenter(image)
if not output:
logger.warning("No segments found in image")
return None
segment_sizes = [np.sum(seg['mask']) for seg in output]
if not segment_sizes:
return None
largest_idx = np.argmax(segment_sizes)
mask = output[largest_idx]['mask']
if not isinstance(mask, np.ndarray):
mask = np.array(mask)
if len(mask.shape) > 2:
mask = mask[:, :, 0]
mask = mask.astype(float)
logger.info(f"Successfully created mask with shape {mask.shape}")
return mask
except Exception as e:
logger.error(f"Segmentation error: {str(e)}")
return None
def load_image_from_url(url, max_retries=3):
for attempt in range(max_retries):
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
img = Image.open(BytesIO(response.content)).convert('RGB')
return img
except Exception as e:
logger.warning(f"Attempt {attempt + 1} failed: {str(e)}")
if attempt < max_retries - 1:
time.sleep(1)
else:
logger.error(f"Failed to load image from {url} after {max_retries} attempts")
return None
def extract_features(image, mask, model, preprocess_val, device):
"""Advanced feature extraction with mask-based attention"""
try:
img_array = np.array(image)
mask = np.expand_dims(mask, axis=2)
mask_3channel = np.repeat(mask, 3, axis=2)
# 1. ์๋ณธ ์ด๋ฏธ์ง์์ ํน์ง ์ถ์ถ
image_tensor_original = preprocess_val(image).unsqueeze(0).to(device)
# 2. ๋ง์คํฌ๋ ์ด๋ฏธ์ง(ํฐ์ ๋ฐฐ๊ฒฝ) ํน์ง ์ถ์ถ
masked_img_white = img_array * mask_3channel + (1 - mask_3channel) * 255
image_masked_white = Image.fromarray(masked_img_white.astype(np.uint8))
image_tensor_masked = preprocess_val(image_masked_white).unsqueeze(0).to(device)
# 3. ์๋ฅ ๋ถ๋ถ๋ง ํฌ๋กญํ ๋ฒ์ ํน์ง ์ถ์ถ
bbox = get_bbox_from_mask(mask) # ๋ง์คํฌ๋ก๋ถํฐ ๊ฒฝ๊ณ์์ ์ถ์ถ
cropped_img = crop_and_resize(img_array * mask_3channel, bbox)
image_cropped = Image.fromarray(cropped_img.astype(np.uint8))
image_tensor_cropped = preprocess_val(image_cropped).unsqueeze(0).to(device)
with torch.no_grad():
# ์ธ ๊ฐ์ง ๋ฒ์ ์ ํน์ง ์ถ์ถ
features_original = model.encode_image(image_tensor_original)
features_masked = model.encode_image(image_tensor_masked)
features_cropped = model.encode_image(image_tensor_cropped)
# ๊ฐ์ค์น๋ฅผ ์ฌ์ฉํ ํน์ง ๊ฒฐํฉ
combined_features = (
0.2 * features_original +
0.3 * features_masked +
0.5 * features_cropped
)
# ์ ๊ทํ
combined_features /= combined_features.norm(dim=-1, keepdim=True)
return combined_features.cpu().numpy().flatten()
except Exception as e:
logger.error(f"Feature extraction error: {e}")
return None
def get_bbox_from_mask(mask):
"""๋ง์คํฌ๋ก๋ถํฐ ๊ฒฝ๊ณ์์ ์ขํ ์ถ์ถ"""
rows = np.any(mask, axis=1)
cols = np.any(mask, axis=0)
rmin, rmax = np.where(rows)[0][[0, -1]]
cmin, cmax = np.where(cols)[0][[0, -1]]
# ์ฌ์ ๊ณต๊ฐ ์ถ๊ฐ
padding = 10
rmin = max(rmin - padding, 0)
rmax = min(rmax + padding, mask.shape[0])
cmin = max(cmin - padding, 0)
cmax = min(cmax + padding, mask.shape[1])
return rmin, rmax, cmin, cmax
def crop_and_resize(image, bbox):
"""๊ฒฝ๊ณ์์๋ก ์ด๋ฏธ์ง ํฌ๋กญ ๋ฐ ๋ฆฌ์ฌ์ด์ฆ"""
rmin, rmax, cmin, cmax = bbox
cropped = image[rmin:rmax, cmin:cmax]
# PIL์ ์ฌ์ฉํ์ฌ ์ ์ฌ๊ฐํ์ผ๋ก ๋ฆฌ์ฌ์ด์ฆ
size = max(cropped.shape[:2])
square_img = np.full((size, size, 3), 255, dtype=np.uint8)
start_h = (size - cropped.shape[0]) // 2
start_w = (size - cropped.shape[1]) // 2
square_img[start_h:start_h+cropped.shape[0],
start_w:start_w+cropped.shape[1]] = cropped
return square_img
def process_item(item, model, preprocess_val, segmenter, device, resize_transform):
"""Process single item from JSON data"""
try:
# ์ด๋ฏธ์ง URL ์ถ์ถ
if '์ด๋ฏธ์ง ๋งํฌ' in item:
image_url = item['์ด๋ฏธ์ง ๋งํฌ']
elif '์ด๋ฏธ์ง URL' in item:
image_url = item['์ด๋ฏธ์ง URL']
else:
logger.warning(f"No image URL found in item")
return None
# ๋ฉํ๋ฐ์ดํฐ ์์ฑ
metadata = create_metadata(item)
# ์ด๋ฏธ์ง ๋ค์ด๋ก๋
image = load_image_from_url(image_url)
if image is None:
logger.warning(f"Failed to load image from {image_url}")
return None
# ์ธ๊ทธ๋ฉํ
์ด์
์ํ
mask = process_segmentation(image, segmenter)
if mask is None:
logger.warning(f"Failed to create segmentation mask for {image_url}")
return None
# ์๋ก์ด ํน์ง ์ถ์ถ ๋ฐฉ์ ์ ์ฉ
try:
features = extract_features(image, mask, model, preprocess_val, device)
if features is None:
raise ValueError("Feature extraction failed")
# ๋๋ฒ๊น
์ฉ ์ด๋ฏธ์ง ์ ์ฅ (์ ํ์ฌํญ)
# save_debug_images(image, mask, image_url)
except Exception as e:
logger.error(f"Feature extraction failed for {image_url}: {str(e)}")
return None
return {
'id': metadata['product_id'],
'embedding': features.tolist(),
'metadata': metadata,
'image_uri': image_url
}
except Exception as e:
logger.error(f"Error processing item: {str(e)}")
return None
# ๋๋ฒ๊น
์ฉ ์ด๋ฏธ์ง ์ ์ฅ ํจ์ (์ ํ์ฌํญ)
def save_debug_images(image, mask, url):
try:
debug_dir = "debug_images"
os.makedirs(debug_dir, exist_ok=True)
# URL์์ ํ์ผ๋ช
์ถ์ถ
filename = url.split('/')[-1].split('?')[0]
# ์๋ณธ, ๋ง์คํฌ, ์ฒ๋ฆฌ๋ ์ด๋ฏธ์ง ์ ์ฅ
image.save(f"{debug_dir}/original_{filename}")
mask_img = Image.fromarray((mask * 255).astype(np.uint8))
mask_img.save(f"{debug_dir}/mask_{filename}")
except Exception as e:
logger.warning(f"Failed to save debug images: {str(e)}")
def create_metadata(item):
"""Create standardized metadata from different JSON formats"""
metadata = {}
# ์ํ ID ์ฒ๋ฆฌ ๊ฐ์
if '๏ปฟ์ํ ID' in item: # ๋ฌด์ ์ฌ ํ์
metadata['product_id'] = item['๏ปฟ์ํ ID']
else:
# 11๋ฒ๊ฐ/G๋ง์ผ์ ๊ฒฝ์ฐ ์ํ๋ช
๊ณผ URL๋ก ์ ๋ํฌํ ID ์์ฑ
unique_string = f"{item.get('์ํ๋ช
', '')}{item.get('์ด๋ฏธ์ง URL', '')}"
metadata['product_id'] = str(hash(unique_string))
# ๋๋จธ์ง ๋ฉํ๋ฐ์ดํฐ ์ฒ๋ฆฌ
metadata['brand'] = item.get('๋ธ๋๋๋ช
', 'unknown')
metadata['name'] = item.get('์ ํ๋ช
') or item.get('์ํ๋ช
', 'unknown')
metadata['price'] = (item.get('์ ๊ฐ') or item.get('๊ฐ๊ฒฉ') or
item.get('ํ๋งค๊ฐ', 'unknown'))
metadata['discount'] = item.get('ํ ์ธ์จ', 'unknown')
if '์นดํ
๊ณ ๋ฆฌ' in item:
if isinstance(item['์นดํ
๊ณ ๋ฆฌ'], list):
metadata['category'] = '/'.join(item['์นดํ
๊ณ ๋ฆฌ'])
else:
metadata['category'] = item['์นดํ
๊ณ ๋ฆฌ']
else:
# 11๋ฒ๊ฐ/G๋ง์ผ์ ๊ฒฝ์ฐ ์ํ๋ช
์์ ์นดํ
๊ณ ๋ฆฌ ์ถ์ถ ์๋
name = metadata['name'].lower()
categories = ['์ํผ์ค', '์
์ธ ', '๋ธ๋ผ์ฐ์ค', '๋ํธ', '๊ฐ๋๊ฑด',
'์ค์ปคํธ', 'ํฌ์ธ ', '์
์
', '์์ฐํฐ', '์์ผ']
found_categories = [cat for cat in categories if cat in name]
metadata['category'] = '/'.join(found_categories) if found_categories else 'unknown'
metadata['image_url'] = (item.get('์ด๋ฏธ์ง ๋งํฌ') or
item.get('์ด๋ฏธ์ง URL', 'unknown'))
# ์ผํ๋ชฐ ์ถ์ฒ ์ถ๊ฐ
if '์ด๋ฏธ์ง ๋งํฌ' in item:
metadata['source'] = 'musinsa'
elif 'cdn.011st.com' in metadata['image_url']:
metadata['source'] = '11st'
elif 'gmarket' in metadata['image_url']:
metadata['source'] = 'gmarket'
else:
metadata['source'] = 'unknown'
return metadata
def create_multimodal_fashion_db(json_files):
try:
logger.info("Starting multimodal fashion database creation")
# ๋ชจ๋ธ ๋ก๋
model, preprocess_val, segmenter, device, resize_transform = load_models()
# ChromaDB ์ค์
client = chromadb.PersistentClient(path="./fashion_multimodal_db")
# Multimodal collection ์์ฑ
embedding_function = OpenCLIPEmbeddingFunction()
data_loader = ImageLoader()
try:
client.delete_collection("fashion_multimodal")
logger.info("Deleted existing collection")
except:
logger.info("No existing collection to delete")
collection = client.create_collection(
name="fashion_multimodal",
embedding_function=embedding_function,
data_loader=data_loader,
metadata={"description": "Fashion multimodal collection with advanced feature extraction"}
)
# ์ฒ๋ฆฌ ๊ฒฐ๊ณผ ํต๊ณ
stats = {
'total_processed': 0,
'successful': 0,
'failed': 0,
'feature_extraction_failed': 0
}
# JSON ํ์ผ๋ค ์ฒ๋ฆฌ
for json_file in json_files:
with open(json_file, 'r', encoding='utf-8') as f:
data = json.load(f)
logger.info(f"Processing {len(data)} items from {json_file}")
with ThreadPoolExecutor(max_workers=4) as executor:
futures = []
for item in data:
future = executor.submit(
process_item,
item, model, preprocess_val, segmenter, device, resize_transform
)
futures.append(future)
processed_items = []
for future in tqdm(futures, desc=f"Processing {json_file}"):
stats['total_processed'] += 1
result = future.result()
if result is not None:
processed_items.append(result)
stats['successful'] += 1
else:
stats['failed'] += 1
# ๋ฐฐ์น๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ถ๊ฐ
if processed_items:
try:
collection.add(
ids=[item['id'] for item in processed_items],
embeddings=[item['embedding'] for item in processed_items],
metadatas=[item['metadata'] for item in processed_items],
uris=[item['image_uri'] for item in processed_items]
)
except Exception as e:
logger.error(f"Failed to add batch to collection: {str(e)}")
stats['failed'] += len(processed_items)
stats['successful'] -= len(processed_items)
# ์ต์ข
ํต๊ณ ์ถ๋ ฅ
logger.info("Processing completed:")
logger.info(f"Total processed: {stats['total_processed']}")
logger.info(f"Successful: {stats['successful']}")
logger.info(f"Failed: {stats['failed']}")
return stats['successful'] > 0
except Exception as e:
logger.error(f"Database creation error: {str(e)}")
return False
if __name__ == "__main__":
json_files = [
'./musinsa_ranking_images_category_0920.json',
'./11st/11st_bagaccessory_20241017_172846.json',
'./11st/11st_best_abroad_bagaccessory_20241017_173300.json',
'./11st/11st_best_abroad_fashion_20241017_173144.json',
'./11st/11st_best_abroad_luxury_20241017_173343.json',
'./11st/11st_best_men_20241017_172534.json',
'./11st/11st_best_women_20241017_172127.json',
'./gmarket/gmarket_best_accessory_20241015_155921.json',
'./gmarket/gmarket_best_bag_20241015_155811.json',
'./gmarket/gmarket_best_brand_20241015_155530.json',
'./gmarket/gmarket_best_casual_20241015_155421.json',
'./gmarket/gmarket_best_men_20241015_155025.json',
'./gmarket/gmarket_best_shoe_20241015_155613.json',
'./gmarket/gmarket_best_women_20241015_154206.json'
]
success = create_multimodal_fashion_db(json_files)
if success:
print("Successfully created multimodal fashion database!")
else:
print("Failed to create database. Check the logs for details.") |