Spaces:
Sleeping
Sleeping
File size: 6,473 Bytes
be5c319 |
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 |
"""
Download Sample Images for ViT Auditing Toolkit
This Python script downloads free sample images from Unsplash for testing.
"""
import os
import urllib.request
from pathlib import Path
# Color codes for terminal output
GREEN = "\033[92m"
BLUE = "\033[94m"
RED = "\033[91m"
RESET = "\033[0m"
def download_image(url, filepath, description):
"""Download an image from URL to filepath."""
try:
print(f"{BLUE}π₯ Downloading:{RESET} {description}")
# Create directory if it doesn't exist
os.makedirs(os.path.dirname(filepath), exist_ok=True)
# Download the image
urllib.request.urlretrieve(url, filepath)
# Check if file was created
if os.path.exists(filepath):
file_size = os.path.getsize(filepath) / 1024 # KB
print(f"{GREEN}β
Saved:{RESET} {filepath} ({file_size:.1f} KB)\n")
return True
else:
print(f"{RED}β Failed to save:{RESET} {filepath}\n")
return False
except Exception as e:
print(f"{RED}β Error:{RESET} {str(e)}\n")
return False
def main():
"""Main function to download all sample images."""
print("πΌοΈ Downloading sample images for ViT Auditing Toolkit...\n")
# Base directory
base_dir = "examples"
# Create directories
directories = [
"basic_explainability",
"counterfactual",
"calibration",
"bias_detection",
"general",
]
for directory in directories:
os.makedirs(os.path.join(base_dir, directory), exist_ok=True)
# Image download list: (url, filepath, description)
images = [
# Basic Explainability
(
"https://images.unsplash.com/photo-1574158622682-e40e69881006?w=800&q=80",
f"{base_dir}/basic_explainability/cat_portrait.jpg",
"Cat Portrait",
),
(
"https://images.unsplash.com/photo-1543466835-00a7907e9de1?w=800&q=80",
f"{base_dir}/basic_explainability/dog_portrait.jpg",
"Dog Portrait",
),
(
"https://images.unsplash.com/photo-1444464666168-49d633b86797?w=800&q=80",
f"{base_dir}/basic_explainability/bird_flying.jpg",
"Bird Flying",
),
(
"https://images.unsplash.com/photo-1583121274602-3e2820c69888?w=800&q=80",
f"{base_dir}/basic_explainability/sports_car.jpg",
"Sports Car",
),
(
"https://images.unsplash.com/photo-1509042239860-f550ce710b93?w=800&q=80",
f"{base_dir}/basic_explainability/coffee_cup.jpg",
"Coffee Cup",
),
# Counterfactual Analysis
(
"https://images.unsplash.com/photo-1494790108377-be9c29b29330?w=800&q=80",
f"{base_dir}/counterfactual/face_portrait.jpg",
"Face Portrait",
),
(
"https://images.unsplash.com/photo-1552519507-da3b142c6e3d?w=800&q=80",
f"{base_dir}/counterfactual/car_side.jpg",
"Car Side View",
),
(
"https://images.unsplash.com/photo-1480714378408-67cf0d13bc1b?w=800&q=80",
f"{base_dir}/counterfactual/building.jpg",
"Building Architecture",
),
(
"https://images.unsplash.com/photo-1490750967868-88aa4486c946?w=800&q=80",
f"{base_dir}/counterfactual/flower.jpg",
"Flower",
),
# Calibration
(
"https://images.unsplash.com/photo-1583511655857-d19b40a7a54e?w=800&q=80",
f"{base_dir}/calibration/clear_panda.jpg",
"Clear Panda Image",
),
(
"https://images.unsplash.com/photo-1425082661705-1834bfd09dca?w=800&q=80",
f"{base_dir}/calibration/outdoor_scene.jpg",
"Outdoor Scene",
),
(
"https://images.unsplash.com/photo-1519389950473-47ba0277781c?w=800&q=80",
f"{base_dir}/calibration/workspace.jpg",
"Workspace Scene",
),
# Bias Detection
(
"https://images.unsplash.com/photo-1601758228041-f3b2795255f1?w=800&q=80",
f"{base_dir}/bias_detection/dog_daylight.jpg",
"Dog in Daylight",
),
(
"https://images.unsplash.com/photo-1596492784531-6e6eb5ea9993?w=800&q=80",
f"{base_dir}/bias_detection/cat_indoor.jpg",
"Cat Indoors",
),
(
"https://images.unsplash.com/photo-1530595467537-0b5996c41f2d?w=800&q=80",
f"{base_dir}/bias_detection/bird_outdoor.jpg",
"Bird Outdoors",
),
(
"https://images.unsplash.com/photo-1449844908441-8829872d2607?w=800&q=80",
f"{base_dir}/bias_detection/urban_scene.jpg",
"Urban Environment",
),
# General
(
"https://images.unsplash.com/photo-1565299624946-b28f40a0ae38?w=800&q=80",
f"{base_dir}/general/pizza.jpg",
"Pizza",
),
(
"https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&q=80",
f"{base_dir}/general/mountain.jpg",
"Mountain Landscape",
),
(
"https://images.unsplash.com/photo-1593642632823-8f785ba67e45?w=800&q=80",
f"{base_dir}/general/laptop.jpg",
"Laptop",
),
(
"https://images.unsplash.com/photo-1555041469-a586c61ea9bc?w=800&q=80",
f"{base_dir}/general/chair.jpg",
"Modern Chair",
),
]
# Download all images
successful = 0
failed = 0
print("=" * 50)
print("Starting downloads...\n")
for url, filepath, description in images:
if download_image(url, filepath, description):
successful += 1
else:
failed += 1
# Summary
print("=" * 50)
print(f"{GREEN}β
Download complete!{RESET}")
print("=" * 50)
print(f"\nπ Summary:")
print(f" β
Successful: {successful}")
print(f" β Failed: {failed}")
print(f"\nπ Image count by category:")
for directory in directories:
path = Path(base_dir) / directory
image_count = len(list(path.glob("*.jpg")))
print(f" - {directory}: {image_count} images")
print(f"\nπ Ready to test! Run: python app.py\n")
if __name__ == "__main__":
main()
|