Datasets:
File size: 10,146 Bytes
a0baaca |
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 |
"""
Shape Polygons Dataset - Usage Examples
This script demonstrates various ways to load and use the Shape Polygons Dataset.
"""
import os
import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
# =============================================================================
# Basic Usage: Loading Metadata and Images
# =============================================================================
def load_dataset(data_dir=".", split="train"):
"""Load metadata and return as pandas DataFrame."""
metadata_path = os.path.join(data_dir, split, "metadata.csv")
return pd.read_csv(metadata_path)
def load_image(data_dir, split, filename):
"""Load a single image from the dataset."""
img_path = os.path.join(data_dir, split, "images", filename)
return Image.open(img_path)
# =============================================================================
# Example 1: Explore Dataset Statistics
# =============================================================================
def explore_statistics(data_dir="."):
"""Print dataset statistics."""
print("=" * 50)
print("Shape Polygons Dataset Statistics")
print("=" * 50)
for split in ["train", "test"]:
df = load_dataset(data_dir, split)
print(f"\n{split.upper()} Split:")
print(f" Total images: {len(df)}")
print(f"\n Vertices distribution:")
for v in range(3, 9):
count = len(df[df["vertices"] == v])
print(f" {v} vertices: {count} ({count/len(df)*100:.1f}%)")
print(f"\n Size statistics:")
print(f" Min: {df['size'].min():.4f}")
print(f" Max: {df['size'].max():.4f}")
print(f" Mean: {df['size'].mean():.4f}")
# =============================================================================
# Example 2: Visualize Sample Images
# =============================================================================
def visualize_samples(data_dir=".", n_samples=12, split="train"):
"""Visualize random samples from the dataset."""
df = load_dataset(data_dir, split)
samples = df.sample(n=min(n_samples, len(df)))
n_cols = 4
n_rows = (len(samples) + n_cols - 1) // n_cols
fig, axes = plt.subplots(n_rows, n_cols, figsize=(12, 3 * n_rows))
axes = axes.flatten() if n_samples > 1 else [axes]
for idx, (_, row) in enumerate(samples.iterrows()):
img = load_image(data_dir, split, row["filename"])
axes[idx].imshow(img)
axes[idx].set_title(f"{row['vertices']} vertices\nsize={row['size']:.2f}")
axes[idx].axis("off")
# Hide empty subplots
for idx in range(len(samples), len(axes)):
axes[idx].axis("off")
plt.tight_layout()
plt.savefig("samples_visualization.png", dpi=150, bbox_inches="tight")
print(f"Saved visualization to 'samples_visualization.png'")
plt.show()
# =============================================================================
# Example 3: Visualize by Shape Type
# =============================================================================
def visualize_by_shape_type(data_dir=".", split="train"):
"""Show one example of each shape type."""
df = load_dataset(data_dir, split)
shape_names = {
3: "Triangle",
4: "Quadrilateral",
5: "Pentagon",
6: "Hexagon",
7: "Heptagon",
8: "Octagon"
}
fig, axes = plt.subplots(2, 3, figsize=(12, 8))
axes = axes.flatten()
for idx, vertices in enumerate(range(3, 9)):
sample = df[df["vertices"] == vertices].iloc[0]
img = load_image(data_dir, split, sample["filename"])
axes[idx].imshow(img)
axes[idx].set_title(f"{shape_names[vertices]}\n({vertices} vertices)")
axes[idx].axis("off")
plt.suptitle("Shape Types in Dataset", fontsize=14, fontweight="bold")
plt.tight_layout()
plt.savefig("shape_types.png", dpi=150, bbox_inches="tight")
print(f"Saved visualization to 'shape_types.png'")
plt.show()
# =============================================================================
# Example 4: PyTorch Dataset Class
# =============================================================================
# Optional imports for PyTorch functionality
try:
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
PYTORCH_AVAILABLE = True
except ImportError:
PYTORCH_AVAILABLE = False
class ShapePolygonsDataset:
"""PyTorch Dataset for Shape Polygons.
Requires: torch, torchvision
"""
def __init__(self, root_dir, split="train", transform=None, task="classification"):
"""
Args:
root_dir: Root directory of the dataset
split: "train" or "test"
transform: Optional torchvision transforms
task: "classification" for vertex count, "regression" for size prediction, "multi" for all properties
"""
if not PYTORCH_AVAILABLE:
raise ImportError("PyTorch is required. Install with: pip install torch torchvision")
self.root_dir = root_dir
self.split = split
self.transform = transform
self.task = task
self.metadata = pd.read_csv(os.path.join(root_dir, split, "metadata.csv"))
if self.transform is None:
self.transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
])
def __len__(self):
return len(self.metadata)
def __getitem__(self, idx):
row = self.metadata.iloc[idx]
img_path = os.path.join(self.root_dir, self.split, "images", row["filename"])
image = Image.open(img_path).convert("RGB")
if self.transform:
image = self.transform(image)
if self.task == "classification":
# Class label: 0-5 for 3-8 vertices
label = torch.tensor(row["vertices"] - 3, dtype=torch.long)
elif self.task == "regression":
# Predict size
label = torch.tensor(row["size"], dtype=torch.float32)
elif self.task == "multi":
# Multi-task: return all properties
label = {
"vertices": torch.tensor(row["vertices"] - 3, dtype=torch.long),
"size": torch.tensor(row["size"], dtype=torch.float32),
"angle": torch.tensor(row["angle"], dtype=torch.float32),
"center": torch.tensor([row["center_x"], row["center_y"]], dtype=torch.float32),
"color": torch.tensor([row["color_r"], row["color_g"], row["color_b"]], dtype=torch.float32)
}
else:
raise ValueError(f"Unknown task: {self.task}")
return image, label
def demo_pytorch_dataloader(data_dir="."):
"""Demonstrate PyTorch DataLoader usage."""
if not PYTORCH_AVAILABLE:
print("PyTorch is not installed. Install with: pip install torch torchvision")
return
print("Creating PyTorch Dataset and DataLoader...")
dataset = ShapePolygonsDataset(data_dir, split="train", task="classification")
dataloader = DataLoader(dataset, batch_size=32, shuffle=True, num_workers=0)
# Get one batch
images, labels = next(iter(dataloader))
print(f"Batch shape: {images.shape}")
print(f"Labels shape: {labels.shape}")
print(f"Label values (vertices - 3): {labels[:10].tolist()}")
print(f"Actual vertex counts: {[l + 3 for l in labels[:10].tolist()]}")
# =============================================================================
# Example 5: Color Analysis
# =============================================================================
def analyze_colors(data_dir=".", split="train"):
"""Analyze color distribution in the dataset."""
df = load_dataset(data_dir, split)
fig, axes = plt.subplots(1, 3, figsize=(15, 4))
colors = ["red", "green", "blue"]
columns = ["color_r", "color_g", "color_b"]
for idx, (color, col) in enumerate(zip(colors, columns)):
axes[idx].hist(df[col], bins=50, color=color, alpha=0.7, edgecolor="black")
axes[idx].set_xlabel(f"{color.capitalize()} Value")
axes[idx].set_ylabel("Frequency")
axes[idx].set_title(f"{color.capitalize()} Channel Distribution")
plt.suptitle(f"Color Distribution in {split.capitalize()} Set", fontsize=14, fontweight="bold")
plt.tight_layout()
plt.savefig("color_distribution.png", dpi=150, bbox_inches="tight")
print(f"Saved visualization to 'color_distribution.png'")
plt.show()
# =============================================================================
# Main
# =============================================================================
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Shape Polygons Dataset Examples")
parser.add_argument("--data-dir", type=str, default=".", help="Path to dataset root")
parser.add_argument(
"--example",
type=str,
choices=["stats", "samples", "shapes", "pytorch", "colors", "all"],
default="all",
help="Which example to run"
)
args = parser.parse_args()
examples = {
"stats": ("Dataset Statistics", lambda: explore_statistics(args.data_dir)),
"samples": ("Sample Visualization", lambda: visualize_samples(args.data_dir)),
"shapes": ("Shape Types", lambda: visualize_by_shape_type(args.data_dir)),
"pytorch": ("PyTorch DataLoader Demo", lambda: demo_pytorch_dataloader(args.data_dir)),
"colors": ("Color Analysis", lambda: analyze_colors(args.data_dir)),
}
if args.example == "all":
for name, (desc, func) in examples.items():
print(f"\n{'=' * 50}")
print(f"Example: {desc}")
print("=" * 50)
func()
else:
name = args.example
desc, func = examples[name]
print(f"Running Example: {desc}")
func()
|