Spaces:
Paused
Paused
File size: 11,207 Bytes
4716563 fac18b7 4716563 fac18b7 4716563 fac18b7 6086b2f 4716563 6086b2f fac18b7 6086b2f fac18b7 6086b2f fac18b7 6086b2f 4716563 fac18b7 4716563 fac18b7 4716563 6086b2f 1f07471 6086b2f 4716563 fac18b7 6086b2f 42733e7 c2644dc 42733e7 6086b2f 42733e7 c2644dc 42733e7 6086b2f 55c158e 42733e7 55c158e 6086b2f 42733e7 6086b2f c2644dc 6086b2f 1f07471 6086b2f 55c158e 6086b2f 1f07471 6086b2f 4716563 fac18b7 6086b2f 3b3cac8 6086b2f 3b3cac8 6086b2f 3b3cac8 6086b2f 3b3cac8 6086b2f 3b3cac8 6086b2f 4716563 |
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 |
import os
import zipfile
from pathlib import Path
from typing import Optional
from huggingface_hub import snapshot_download # type: ignore
def _unzip_images_if_needed(root: str) -> None:
"""
If an archive like images.zip exists in the dataset root, extract it to root/images.
"""
images_dir = os.path.join(root, "images")
if os.path.isdir(images_dir) and any(Path(images_dir).glob("*")):
print(f"β
Images already present in {images_dir}")
return
# Common zip names at root or subfolders
candidates = [os.path.join(root, name) for name in ("images.zip", "polyvore-images.zip", "imgs.zip")]
# Also search recursively for any *images*.zip
for p in Path(root).rglob("*images*.zip"):
candidates.append(str(p))
for zpath in candidates:
if os.path.isfile(zpath):
print(f"π§ Found image archive: {zpath}")
print(f"π Extracting to: {images_dir}")
os.makedirs(images_dir, exist_ok=True)
try:
with zipfile.ZipFile(zpath, "r") as zf:
# Get total size for progress
total_size = sum(f.file_size for f in zf.filelist)
extracted_size = 0
for file_info in zf.filelist:
zf.extract(file_info, images_dir)
extracted_size += file_info.file_size
# Progress update every 100MB
if extracted_size % (100 * 1024 * 1024) < file_info.file_size:
progress = (extracted_size / total_size) * 100
print(f"π¦ Extraction progress: {progress:.1f}%")
print(f"β
Successfully extracted {len(zf.filelist)} files")
return
except Exception as e:
print(f"β Failed to extract {zpath}: {e}")
continue
print("β οΈ No image archive found to extract")
def ensure_dataset_ready() -> Optional[str]:
"""
Self-contained dataset fetcher for the Polyvore dataset from Hugging Face.
- Downloads the dataset repo Stylique/Polyvore into ./data/Polyvore
- Unzips images.zip into ./data/Polyvore/images
- Returns the dataset root path
"""
root = os.path.abspath(os.path.join(os.getcwd(), "data", "Polyvore"))
Path(root).mkdir(parents=True, exist_ok=True)
print(f"π Checking dataset at: {root}")
# Check if we already have the essential files
images_dir = os.path.join(root, "images")
metadata_files = [
"polyvore_item_metadata.json",
"polyvore_outfit_titles.json",
"categories.csv"
]
has_images = os.path.isdir(images_dir) and any(Path(images_dir).glob("*"))
has_metadata = all(os.path.exists(os.path.join(root, f)) for f in metadata_files)
if has_images and has_metadata:
print("β
Dataset already complete - skipping download and extraction")
return root
# Download the HF dataset snapshot into root
try:
print("π₯ Downloading Polyvore dataset from Hugging Face...")
# Only fetch what's needed to run and prepare splits
allow = [
"images.zip",
# root-level (some mirrors place jsons here)
"train.json",
"valid.json",
"test.json",
# official splits often live here
"nondisjoint/train.json",
"nondisjoint/valid.json",
"nondisjoint/test.json",
"disjoint/train.json",
"disjoint/valid.json",
"disjoint/test.json",
# light metadata
"polyvore_item_metadata.json",
"polyvore_outfit_titles.json",
"categories.csv",
]
# Explicit ignores to prevent huge downloads (>10GB)
ignore = [
"**/*hglmm*",
"**/*.tar",
"**/*.tar.gz",
"**/*.7z",
"**/large/**",
]
need_download = not (
has_metadata and (
# any location providing official splits is acceptable
all(os.path.exists(os.path.join(root, f)) for f in ["train.json", "valid.json", "test.json"]) or
all(os.path.exists(os.path.join(root, "nondisjoint", f)) for f in ["train.json", "valid.json", "test.json"]) or
all(os.path.exists(os.path.join(root, "disjoint", f)) for f in ["train.json", "valid.json", "test.json"])
)
)
# Only download if images are missing
if not has_images:
print("π Starting download...")
snapshot_download(
"Stylique/Polyvore",
repo_type="dataset",
local_dir=root,
local_dir_use_symlinks=False,
allow_patterns=allow,
ignore_patterns=ignore,
)
print("β
Download completed")
# Extract images after download
_unzip_images_if_needed(root)
elif not has_metadata:
# Only download metadata if images exist but metadata is missing
print("π₯ Downloading missing metadata files...")
snapshot_download(
"Stylique/Polyvore",
repo_type="dataset",
local_dir=root,
local_dir_use_symlinks=False,
allow_patterns=["polyvore_item_metadata.json", "polyvore_outfit_titles.json", "categories.csv"],
ignore_patterns=ignore,
)
print("β
Metadata download completed")
else:
print("β
All required files already present")
except Exception as e:
print(f"β Failed to download Stylique/Polyvore dataset: {e}")
print("π§ Trying to work with existing files...")
# Check what we have locally
existing_files = []
for file_path in Path(root).rglob("*"):
if file_path.is_file():
existing_files.append(str(file_path.relative_to(root)))
if existing_files:
print(f"π Found {len(existing_files)} existing files:")
for f in sorted(existing_files)[:10]: # Show first 10
print(f" - {f}")
if len(existing_files) > 10:
print(f" ... and {len(existing_files) - 10} more")
else:
print("π No existing files found")
return None
# Unzip images if needed
_unzip_images_if_needed(root)
# Final verification
if os.path.isdir(images_dir) and any(Path(images_dir).glob("*")):
print(f"β
Dataset ready at: {root}")
print(f"π Images: {len(list(Path(images_dir).glob('*')))} files")
# Check metadata
for meta_file in metadata_files:
meta_path = os.path.join(root, meta_file)
if os.path.exists(meta_path):
size_bytes = os.path.getsize(meta_path)
if size_bytes < 1024 * 1024: # Less than 1MB
size_kb = size_bytes / 1024
print(f"π {meta_file}: {size_kb:.1f} KB")
else:
size_mb = size_bytes / (1024 * 1024)
print(f"π {meta_file}: {size_mb:.1f} MB")
else:
print(f"β οΈ Missing: {meta_file}")
return root
else:
print("β Failed to prepare dataset")
return None
def check_dataset_structure(root: str) -> dict:
"""Check the structure of the downloaded dataset."""
structure = {
"root": root,
"images": {"exists": False, "count": 0, "path": os.path.join(root, "images")},
"metadata": {},
"splits": {},
"status": "unknown"
}
# Check images
images_dir = os.path.join(root, "images")
if os.path.isdir(images_dir):
image_files = list(Path(images_dir).glob("*"))
structure["images"]["exists"] = True
structure["images"]["count"] = len(image_files)
structure["images"]["extensions"] = list(set(f.suffix.lower() for f in image_files))
# Check metadata files
metadata_files = [
"polyvore_item_metadata.json",
"polyvore_outfit_titles.json",
"categories.csv"
]
for meta_file in metadata_files:
meta_path = os.path.join(root, meta_file)
if os.path.exists(meta_path):
size_bytes = os.path.getsize(meta_path)
if size_bytes < 1024 * 1024: # Less than 1MB
size_kb = size_bytes / 1024
structure["metadata"][meta_file] = {"exists": True, "size_kb": size_kb}
else:
size_mb = size_bytes / (1024 * 1024)
structure["metadata"][meta_file] = {"exists": True, "size_mb": size_mb}
else:
structure["metadata"][meta_file] = {"exists": False, "size_mb": 0, "size_kb": 0}
# Check for splits
split_locations = [
("root", ["train.json", "valid.json", "test.json"]),
("nondisjoint", ["train.json", "valid.json", "test.json"]),
("disjoint", ["train.json", "valid.json", "test.json"]),
("splits", ["train.json", "valid.json", "test.json"])
]
for location, files in split_locations:
location_path = os.path.join(root, location)
if os.path.exists(location_path):
structure["splits"][location] = {}
for split_file in files:
split_path = os.path.join(location_path, split_file)
if os.path.exists(split_path):
size_bytes = os.path.getsize(split_path)
if size_bytes < 1024 * 1024: # Less than 1MB
size_kb = size_bytes / 1024
structure["splits"][location][split_file] = {"exists": True, "size_kb": size_kb}
else:
size_mb = size_bytes / (1024 * 1024)
structure["splits"][location][split_file] = {"exists": True, "size_mb": size_mb}
else:
structure["splits"][location][split_file] = {"exists": False, "size_mb": 0, "size_kb": 0}
else:
structure["splits"][location] = "directory_not_found"
# Determine overall status
if structure["images"]["exists"] and structure["images"]["count"] > 0:
if any(meta["exists"] for meta in structure["metadata"].values()):
structure["status"] = "ready"
else:
structure["status"] = "partial"
else:
structure["status"] = "incomplete"
return structure
if __name__ == "__main__":
# Test the dataset fetcher
print("π§ͺ Testing Polyvore dataset fetcher...")
root = ensure_dataset_ready()
if root:
print(f"\nπ Dataset structure:")
structure = check_dataset_structure(root)
import json
print(json.dumps(structure, indent=2))
else:
print("β Failed to prepare dataset")
|