xixu-me commited on
Commit
639f152
·
verified ·
1 Parent(s): 4f5b22f

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +1157 -1183
README.md CHANGED
@@ -1,1183 +1,1157 @@
1
- ---
2
- license: mit
3
- viewer: false
4
- task_categories:
5
- - image-classification
6
- task_ids:
7
- - multi-class-image-classification
8
- tags:
9
- - computer-vision
10
- - product-classification
11
- - e-commerce
12
- - retail
13
- - few-shot-learning
14
- - meta-learning
15
- - benchmark
16
- size_categories:
17
- - 100K<n<1M
18
- language:
19
- - en
20
- pretty_name: FSL Product Classification Dataset
21
- configs:
22
- - config_name: default
23
- data_files: "data.tzst"
24
- default: true
25
- dataset_info:
26
- features:
27
- - name: image
28
- dtype: image
29
- - name: label
30
- dtype: int64
31
- - name: class_name
32
- dtype: string
33
- - name: image_id
34
- dtype: string
35
- splits:
36
- - name: train
37
- num_bytes: 9945644054
38
- num_examples: 279747
39
- download_size: 9945644054
40
- dataset_size: 9945644054
41
- ---
42
-
43
- # Few-Shot Learning (FSL) Product Classification Dataset
44
-
45
- ## Dataset Description
46
-
47
- This dataset is designed for **Few-Shot Learning (FSL)** research in product classification tasks. It contains product images organized into 763 distinct classes, with an average of approximately 367 images per class (279,747 total images), making it ideal for training and evaluating few-shot learning algorithms in e-commerce and retail scenarios. Note that class numbers are not continuous.
48
-
49
- ### Key Features
50
-
51
- - **763 product classes** covering diverse product categories
52
- - **279,747 total images** (average of ~367 images per class)
53
- - **High-quality product images** suitable for computer vision research
54
- - **Variable class distribution** with non-continuous class numbers
55
- - **Efficient tzst compression** for reduced storage and faster transfer
56
- - **Compatible with Hugging Face Datasets** library
57
-
58
- ### Dataset Statistics
59
-
60
- - **Total Classes**: 763
61
- - **Total Images**: 279,747
62
- - **Images per Class**: ~367 (average, variable distribution)
63
- - **Class Numbers**: Non-continuous (some class numbers may be missing)
64
- - **Image Format**: PNG
65
- - **Typical Image Size**: 50-100 KB per image
66
- - **Average Images per Class**: 366.6 (279,747 ÷ 763)
67
- - **Compressed Archive Size**: ~9.9 GB (data.tzst)
68
- - **Extraction Requirements**: ~10 GB additional space
69
-
70
- ## Dataset Structure
71
-
72
- The dataset is stored in a compressed tzst archive ([`data.tzst`](data.tzst)) with the following structure:
73
-
74
- ```text
75
- data.tzst
76
- ├── class_0/
77
- │ ├── class_0_0.png
78
- │ ├── class_0_1.png
79
- │ └── ...
80
- ├── class_1/
81
- │ ├── class_1_0.png
82
- │ ├── class_1_1.png
83
- │ └── ...
84
- └── ... (763 total classes with non-continuous numbers)
85
- ```
86
-
87
- **Note**: Class numbers are not continuous. For example, you might have class_0, class_2, class_5, etc., but not class_1, class_3, class_4. The total number of classes is 763.
88
-
89
- ## Usage
90
-
91
- ## Installation and Setup
92
-
93
- ### Quick Start Installation
94
-
95
- ```bash
96
- # Create a new virtual environment (recommended)
97
- python -m venv fsl-env
98
- source fsl-env/bin/activate # On Windows: fsl-env\Scripts\activate
99
-
100
- # Install core dependencies
101
- pip install datasets tzst pillow
102
-
103
- # Install additional dependencies for machine learning
104
- pip install torch torchvision numpy scikit-learn matplotlib seaborn tqdm
105
-
106
- # For Jupyter notebook users
107
- pip install jupyter ipywidgets
108
- ```
109
-
110
- ### Complete Requirements
111
-
112
- Create a `requirements.txt` file with the following dependencies:
113
-
114
- ```text
115
- # Core dependencies
116
- datasets>=2.14.0
117
- tzst>=1.2.8
118
- pillow>=9.0.0
119
-
120
- # Machine learning
121
- torch>=1.9.0
122
- torchvision>=0.10.0
123
- numpy>=1.21.0
124
- scikit-learn>=1.0.0
125
-
126
- # Data analysis and visualization
127
- pandas>=1.3.0
128
- matplotlib>=3.4.0
129
- seaborn>=0.11.0
130
-
131
- # Progress bars and utilities
132
- tqdm>=4.62.0
133
- pathlib>=1.0.1
134
-
135
- # Optional: for advanced few-shot learning
136
- learn2learn>=0.1.7
137
- higher>=0.2.1
138
-
139
- # Optional: for notebook usage
140
- jupyter>=1.0.0
141
- ipywidgets>=7.6.0
142
- ```
143
-
144
- Install all requirements:
145
-
146
- ```bash
147
- pip install -r requirements.txt
148
- ```
149
-
150
- ### Docker Setup (Optional)
151
-
152
- For a containerized environment:
153
-
154
- ```dockerfile
155
- FROM python:3.9-slim
156
-
157
- WORKDIR /app
158
-
159
- # Install system dependencies
160
- RUN apt-get update && apt-get install -y \
161
- git \
162
- wget \
163
- && rm -rf /var/lib/apt/lists/*
164
-
165
- # Copy requirements and install Python dependencies
166
- COPY requirements.txt .
167
- RUN pip install --no-cache-dir -r requirements.txt
168
-
169
- # Copy application code
170
- COPY . .
171
-
172
- # Set environment variables
173
- ENV PYTHONPATH=/app
174
- ENV HF_DATASETS_CACHE=/app/cache
175
-
176
- # Create cache directory
177
- RUN mkdir -p /app/cache
178
-
179
- CMD ["python", "-c", "print('FSL Product Classification environment ready!')"]
180
- ```
181
-
182
- Build and run:
183
-
184
- ```bash
185
- docker build -t fsl-product-classification .
186
- docker run -it --rm -v $(pwd)/data:/app/data fsl-product-classification bash
187
- ```
188
-
189
- ### Loading the Dataset
190
-
191
- #### Option 1: Using Hugging Face Datasets (Recommended)
192
-
193
- ```python
194
- from datasets import load_dataset
195
-
196
- # Load the complete dataset
197
- dataset = load_dataset("xixu-me/fsl-product-classification")
198
-
199
- # Access the training split
200
- train_dataset = dataset["train"]
201
-
202
- # Print dataset information
203
- print(f"Dataset size: {len(train_dataset)}")
204
- print(f"Features: {train_dataset.features}")
205
-
206
- # Access individual samples
207
- sample = train_dataset[0]
208
- print(f"Image ID: {sample['image_id']}")
209
- print(f"Class: {sample['class_name']} (Label: {sample['label']})")
210
- print(f"Image: {sample['image']}") # PIL Image object
211
- ```
212
-
213
- #### Option 2: Manual Extraction and Loading
214
-
215
- ```python
216
- import os
217
- from tzst import extract_archive
218
- from datasets import Dataset, Features, Value, Image, ClassLabel
219
- from PIL import Image as PILImage
220
-
221
- # Extract the dataset archive
222
- extract_archive("data.tzst", "extracted_data/")
223
-
224
- # Create a custom dataset loader
225
- def load_fsl_dataset(data_dir="extracted_data"):
226
- samples = []
227
- class_names = []
228
-
229
- # Scan for class directories
230
- for class_dir in sorted(os.listdir(data_dir)):
231
- if class_dir.startswith("class_"):
232
- class_path = os.path.join(data_dir, class_dir)
233
- if os.path.isdir(class_path):
234
- class_id = int(class_dir.split("_")[1])
235
- class_names.append(class_dir)
236
-
237
- # Load images from this class
238
- for img_file in os.listdir(class_path):
239
- if img_file.endswith('.png'):
240
- img_path = os.path.join(class_path, img_file)
241
- image_id = img_file.replace('.png', '')
242
-
243
- samples.append({
244
- 'image': img_path,
245
- 'label': class_id,
246
- 'class_name': class_dir,
247
- 'image_id': image_id
248
- })
249
-
250
- # Create features definition
251
- features = Features({
252
- 'image': Image(),
253
- 'label': Value('int64'),
254
- 'class_name': Value('string'),
255
- 'image_id': Value('string')
256
- })
257
-
258
- # Create dataset
259
- return Dataset.from_list(samples, features=features)
260
-
261
- # Load the dataset
262
- dataset = load_fsl_dataset()
263
- print(f"Loaded {len(dataset)} samples from {len(set(dataset['class_name']))} classes")
264
- ```
265
-
266
- #### Option 3: Streaming Mode for Large Archives
267
-
268
- For memory-efficient processing of the large archive:
269
-
270
- ```python
271
- from tzst import extract_archive
272
- import tempfile
273
- import os
274
-
275
- # Use streaming extraction for memory efficiency
276
- with tempfile.TemporaryDirectory() as temp_dir:
277
- # Extract with streaming mode
278
- extract_archive("data.tzst", temp_dir, streaming=True)
279
-
280
- # Process extracted data
281
- dataset = load_fsl_dataset(temp_dir)
282
- # ... your processing code here
283
- ```
284
-
285
- ### Data Exploration
286
-
287
- ```python
288
- from collections import Counter
289
- import matplotlib.pyplot as plt
290
-
291
- # Load dataset
292
- dataset = load_dataset("xixu-me/fsl-product-classification")["train"]
293
-
294
- # Analyze class distribution
295
- class_counts = Counter(dataset['class_name'])
296
- print(f"Number of classes: {len(class_counts)}")
297
- print(f"Average images per class: {len(dataset) / len(class_counts):.1f}")
298
-
299
- # Plot class distribution (top 20 classes)
300
- top_classes = class_counts.most_common(20)
301
- classes, counts = zip(*top_classes)
302
-
303
- plt.figure(figsize=(12, 6))
304
- plt.bar(range(len(classes)), counts)
305
- plt.xlabel('Class')
306
- plt.ylabel('Number of Images')
307
- plt.title('Top 20 Classes by Image Count')
308
- plt.xticks(range(len(classes)), [c.replace('class_', '') for c in classes], rotation=45)
309
- plt.tight_layout()
310
- plt.show()
311
-
312
- # Display sample images
313
- import random
314
-
315
- def show_samples(dataset, num_samples=8):
316
- """Display random samples from the dataset"""
317
- indices = random.sample(range(len(dataset)), num_samples)
318
-
319
- fig, axes = plt.subplots(2, 4, figsize=(15, 8))
320
- axes = axes.flatten()
321
-
322
- for i, idx in enumerate(indices):
323
- sample = dataset[idx]
324
- axes[i].imshow(sample['image'])
325
- axes[i].set_title(f"{sample['class_name']}\nID: {sample['image_id']}")
326
- axes[i].axis('off')
327
-
328
- plt.tight_layout()
329
- plt.show()
330
-
331
- # Show sample images
332
- show_samples(dataset)
333
- ```
334
-
335
- ### Few-Shot Learning Setup
336
-
337
- #### Basic Few-Shot Episode Creation
338
-
339
- ```python
340
- import random
341
- from collections import defaultdict
342
- import torch
343
- from torch.utils.data import DataLoader
344
- from datasets import load_dataset
345
-
346
- def create_few_shot_split(dataset, n_way=5, k_shot=5, n_query=15, seed=None):
347
- """
348
- Create a few-shot learning episode
349
-
350
- Args:
351
- dataset: Hugging Face Dataset instance or custom dataset
352
- n_way: Number of classes in the episode
353
- k_shot: Number of support samples per class
354
- n_query: Number of query samples per class
355
- seed: Random seed for reproducibility
356
-
357
- Returns:
358
- support_set, query_set: Lists of (image, label) tuples
359
- """
360
- if seed is not None:
361
- random.seed(seed)
362
-
363
- # Group samples by class
364
- class_samples = defaultdict(list)
365
- for i, sample in enumerate(dataset):
366
- class_samples[sample['label']].append(i)
367
-
368
- # Filter classes with enough samples
369
- valid_classes = [
370
- class_id for class_id, indices in class_samples.items()
371
- if len(indices) >= k_shot + n_query
372
- ]
373
-
374
- if len(valid_classes) < n_way:
375
- raise ValueError(f"Not enough classes with {k_shot + n_query} samples. "
376
- f"Found {len(valid_classes)}, need {n_way}")
377
-
378
- # Sample n_way classes
379
- episode_classes = random.sample(valid_classes, n_way)
380
-
381
- support_set = []
382
- query_set = []
383
-
384
- for new_label, original_class in enumerate(episode_classes):
385
- class_indices = random.sample(class_samples[original_class], k_shot + n_query)
386
-
387
- # Support samples
388
- for idx in class_indices[:k_shot]:
389
- sample = dataset[idx]
390
- support_set.append((sample['image'], new_label, sample['image_id']))
391
-
392
- # Query samples
393
- for idx in class_indices[k_shot:]:
394
- sample = dataset[idx]
395
- query_set.append((sample['image'], new_label, sample['image_id']))
396
-
397
- return support_set, query_set
398
-
399
- # Example usage
400
- dataset = load_dataset("xixu-me/fsl-product-classification")["train"]
401
-
402
- # Create a 5-way 5-shot episode
403
- support_set, query_set = create_few_shot_split(dataset, n_way=5, k_shot=5, n_query=15)
404
-
405
- print(f"Support set: {len(support_set)} samples")
406
- print(f"Query set: {len(query_set)} samples")
407
- ```
408
-
409
- #### Advanced FSL Dataset Class
410
-
411
- ```python
412
- import torch
413
- from torch.utils.data import Dataset
414
- from torchvision import transforms
415
- from PIL import Image
416
- import numpy as np
417
-
418
- class FSLProductDataset(Dataset):
419
- """
420
- Few-Shot Learning Dataset wrapper for product classification
421
- """
422
-
423
- def __init__(self, hf_dataset, transform=None, target_transform=None):
424
- self.dataset = hf_dataset
425
- self.transform = transform or self.get_default_transform()
426
- self.target_transform = target_transform
427
-
428
- # Create label mapping for non-continuous labels
429
- unique_labels = sorted(set(hf_dataset['label']))
430
- self.label_to_idx = {label: idx for idx, label in enumerate(unique_labels)}
431
- self.idx_to_label = {idx: label for label, idx in self.label_to_idx.items()}
432
-
433
- def get_default_transform(self):
434
- """Default image transformations"""
435
- return transforms.Compose([
436
- transforms.Resize((224, 224)),
437
- transforms.ToTensor(),
438
- transforms.Normalize(mean=[0.485, 0.456, 0.406],
439
- std=[0.229, 0.224, 0.225])
440
- ])
441
-
442
- def __len__(self):
443
- return len(self.dataset)
444
-
445
- def __getitem__(self, idx):
446
- sample = self.dataset[idx]
447
- image = sample['image']
448
-
449
- # Convert to PIL Image if needed
450
- if not isinstance(image, Image.Image):
451
- image = Image.fromarray(image)
452
-
453
- # Apply transforms
454
- if self.transform:
455
- image = self.transform(image)
456
-
457
- # Map label to continuous indices
458
- label = self.label_to_idx[sample['label']]
459
-
460
- if self.target_transform:
461
- label = self.target_transform(label)
462
-
463
- return image, label, sample['image_id']
464
-
465
- def get_class_samples(self, class_label):
466
- """Get all samples for a specific class"""
467
- indices = [i for i, sample in enumerate(self.dataset)
468
- if sample['label'] == class_label]
469
- return [self[i] for i in indices]
470
-
471
- def create_episode_dataloader(self, n_way=5, k_shot=5, n_query=15,
472
- batch_size=None, shuffle=True):
473
- """Create a DataLoader for a few-shot episode"""
474
- support_set, query_set = create_few_shot_split(
475
- self.dataset, n_way=n_way, k_shot=k_shot, n_query=n_query
476
- )
477
-
478
- # Convert to tensors
479
- support_images = []
480
- support_labels = []
481
- query_images = []
482
- query_labels = []
483
-
484
- for image, label, _ in support_set:
485
- if isinstance(image, Image.Image):
486
- image = self.transform(image) if self.transform else image
487
- support_images.append(image)
488
- support_labels.append(label)
489
-
490
- for image, label, _ in query_set:
491
- if isinstance(image, Image.Image):
492
- image = self.transform(image) if self.transform else image
493
- query_images.append(image)
494
- query_labels.append(label)
495
-
496
- support_data = (torch.stack(support_images), torch.tensor(support_labels))
497
- query_data = (torch.stack(query_images), torch.tensor(query_labels))
498
-
499
- return support_data, query_data
500
-
501
- # Example usage with PyTorch
502
- transform = transforms.Compose([
503
- transforms.Resize((84, 84)), # Common size for few-shot learning
504
- transforms.ToTensor(),
505
- transforms.Normalize(mean=[0.485, 0.456, 0.406],
506
- std=[0.229, 0.224, 0.225])
507
- ])
508
-
509
- # Load dataset
510
- hf_dataset = load_dataset("xixu-me/fsl-product-classification")["train"]
511
- fsl_dataset = FSLProductDataset(hf_dataset, transform=transform)
512
-
513
- # Create episode data
514
- support_data, query_data = fsl_dataset.create_episode_dataloader(
515
- n_way=5, k_shot=1, n_query=15
516
- )
517
-
518
- print(f"Support images shape: {support_data[0].shape}")
519
- print(f"Support labels shape: {support_data[1].shape}")
520
- print(f"Query images shape: {query_data[0].shape}")
521
- print(f"Query labels shape: {query_data[1].shape}")
522
- ```
523
-
524
- #### Meta-Learning Training Loop
525
-
526
- ```python
527
- import torch.nn as nn
528
- import torch.optim as optim
529
- from tqdm import tqdm
530
-
531
- def train_fsl_model(model, dataset, num_episodes=1000, n_way=5, k_shot=1,
532
- n_query=15, lr=0.001, device='cuda'):
533
- """
534
- Basic training loop for few-shot learning
535
-
536
- Args:
537
- model: Few-shot learning model (e.g., Prototypical Network)
538
- dataset: FSLProductDataset instance
539
- num_episodes: Number of training episodes
540
- n_way, k_shot, n_query: Episode configuration
541
- lr: Learning rate
542
- device: Training device
543
- """
544
- model.to(device)
545
- optimizer = optim.Adam(model.parameters(), lr=lr)
546
- criterion = nn.CrossEntropyLoss()
547
-
548
- model.train()
549
- total_loss = 0
550
- total_acc = 0
551
-
552
- for episode in tqdm(range(num_episodes), desc="Training"):
553
- # Create episode
554
- support_data, query_data = dataset.create_episode_dataloader(
555
- n_way=n_way, k_shot=k_shot, n_query=n_query
556
- )
557
-
558
- support_images, support_labels = support_data
559
- query_images, query_labels = query_data
560
-
561
- # Move to device
562
- support_images = support_images.to(device)
563
- support_labels = support_labels.to(device)
564
- query_images = query_images.to(device)
565
- query_labels = query_labels.to(device)
566
-
567
- # Forward pass
568
- optimizer.zero_grad()
569
- logits = model(support_images, support_labels, query_images)
570
- loss = criterion(logits, query_labels)
571
-
572
- # Backward pass
573
- loss.backward()
574
- optimizer.step()
575
-
576
- # Calculate accuracy
577
- pred = logits.argmax(dim=1)
578
- acc = (pred == query_labels).float().mean()
579
-
580
- total_loss += loss.item()
581
- total_acc += acc.item()
582
-
583
- if (episode + 1) % 100 == 0:
584
- avg_loss = total_loss / 100
585
- avg_acc = total_acc / 100
586
- print(f"Episode {episode + 1}: Loss = {avg_loss:.4f}, Acc = {avg_acc:.4f}")
587
- total_loss = 0
588
- total_acc = 0
589
-
590
- # Example: Simple Prototypical Network
591
- class SimplePrototypicalNetwork(nn.Module):
592
- def __init__(self, backbone):
593
- super().__init__()
594
- self.backbone = backbone
595
-
596
- def forward(self, support_images, support_labels, query_images):
597
- # Encode images
598
- support_features = self.backbone(support_images)
599
- query_features = self.backbone(query_images)
600
-
601
- # Calculate prototypes
602
- n_way = len(torch.unique(support_labels))
603
- prototypes = []
604
-
605
- for class_idx in range(n_way):
606
- class_mask = support_labels == class_idx
607
- class_features = support_features[class_mask]
608
- prototype = class_features.mean(dim=0)
609
- prototypes.append(prototype)
610
-
611
- prototypes = torch.stack(prototypes)
612
-
613
- # Calculate distances and logits
614
- distances = torch.cdist(query_features, prototypes)
615
- logits = -distances # Negative distance as logits
616
-
617
- return logits
618
- ```
619
-
620
- ## Research Applications
621
-
622
- This dataset is particularly well-suited for:
623
-
624
- ### Few-Shot Learning
625
-
626
- - **Meta-learning algorithms** (MAML, Prototypical Networks, Relation Networks)
627
- - **Metric learning approaches** (Siamese Networks, Triplet Networks)
628
- - **Gradient-based meta-learning** methods
629
-
630
- ### Transfer Learning
631
-
632
- - **Pre-training** on large-scale product data
633
- - **Domain adaptation** from general images to products
634
- - **Fine-tuning** strategies for product classification
635
-
636
- ### Computer Vision Research
637
-
638
- - **Product recognition** and retrieval
639
- - **E-commerce applications**
640
- - **Retail automation**
641
- - **Visual search** systems
642
-
643
- ## Benchmark Tasks
644
-
645
- ### Standard Few-Shot Learning Evaluation
646
-
647
- The following benchmarks are recommended for evaluating few-shot learning models on this dataset:
648
-
649
- #### Standard Evaluation Protocol
650
-
651
- ```python
652
- import numpy as np
653
- from sklearn.metrics import accuracy_score, classification_report
654
- import json
655
-
656
- def evaluate_fsl_model(model, dataset, num_episodes=600, n_way=5, k_shot=1,
657
- n_query=15, device='cuda'):
658
- """
659
- Evaluate few-shot learning model using standard protocol
660
-
661
- Returns:
662
- dict: Evaluation results with mean accuracy and confidence interval
663
- """
664
- model.eval()
665
- accuracies = []
666
-
667
- with torch.no_grad():
668
- for _ in tqdm(range(num_episodes), desc="Evaluating"):
669
- # Create episode
670
- support_data, query_data = dataset.create_episode_dataloader(
671
- n_way=n_way, k_shot=k_shot, n_query=n_query
672
- )
673
-
674
- support_images, support_labels = support_data
675
- query_images, query_labels = query_data
676
-
677
- # Move to device
678
- support_images = support_images.to(device)
679
- support_labels = support_labels.to(device)
680
- query_images = query_images.to(device)
681
- query_labels = query_labels.to(device)
682
-
683
- # Predict
684
- logits = model(support_images, support_labels, query_images)
685
- pred = logits.argmax(dim=1)
686
-
687
- # Calculate episode accuracy
688
- acc = (pred == query_labels).float().mean().item()
689
- accuracies.append(acc)
690
-
691
- # Calculate statistics
692
- mean_acc = np.mean(accuracies)
693
- std_acc = np.std(accuracies)
694
- ci_95 = 1.96 * std_acc / np.sqrt(len(accuracies))
695
-
696
- results = {
697
- 'mean_accuracy': mean_acc,
698
- 'std_accuracy': std_acc,
699
- 'confidence_interval_95': ci_95,
700
- 'num_episodes': num_episodes,
701
- 'config': f"{n_way}-way {k_shot}-shot"
702
- }
703
-
704
- return results
705
-
706
- # Benchmark configurations
707
- benchmark_configs = [
708
- {'n_way': 5, 'k_shot': 1, 'n_query': 15}, # 5-way 1-shot
709
- {'n_way': 5, 'k_shot': 5, 'n_query': 15}, # 5-way 5-shot
710
- {'n_way': 10, 'k_shot': 1, 'n_query': 15}, # 10-way 1-shot
711
- {'n_way': 10, 'k_shot': 5, 'n_query': 15}, # 10-way 5-shot
712
- ]
713
-
714
- # Run benchmarks
715
- def run_benchmark_suite(model, dataset, num_episodes=600):
716
- """Run complete benchmark suite"""
717
- results = {}
718
-
719
- for config in benchmark_configs:
720
- config_name = f"{config['n_way']}-way_{config['k_shot']}-shot"
721
- print(f"\nEvaluating {config_name}...")
722
-
723
- result = evaluate_fsl_model(
724
- model, dataset, num_episodes=num_episodes, **config
725
- )
726
- results[config_name] = result
727
-
728
- print(f"Accuracy: {result['mean_accuracy']:.4f} ± {result['confidence_interval_95']:.4f}")
729
-
730
- return results
731
-
732
- # Example usage
733
- # results = run_benchmark_suite(model, test_dataset)
734
- ```
735
-
736
- #### Cross-Domain Evaluation
737
-
738
- ```python
739
- def create_cross_domain_split(dataset, train_ratio=0.6, val_ratio=0.2, test_ratio=0.2, seed=42):
740
- """
741
- Create train/validation/test splits at the class level for cross-domain evaluation
742
-
743
- Args:
744
- dataset: Hugging Face Dataset
745
- train_ratio: Proportion of classes for training
746
- val_ratio: Proportion of classes for validation
747
- test_ratio: Proportion of classes for testing
748
- seed: Random seed
749
-
750
- Returns:
751
- dict: Splits with class indices for each set
752
- """
753
- np.random.seed(seed)
754
-
755
- # Get unique classes
756
- unique_classes = sorted(set(dataset['label']))
757
- n_classes = len(unique_classes)
758
-
759
- # Calculate split sizes
760
- n_train = int(n_classes * train_ratio)
761
- n_val = int(n_classes * val_ratio)
762
- n_test = n_classes - n_train - n_val
763
-
764
- # Shuffle and split classes
765
- shuffled_classes = np.random.permutation(unique_classes)
766
- train_classes = shuffled_classes[:n_train]
767
- val_classes = shuffled_classes[n_train:n_train + n_val]
768
- test_classes = shuffled_classes[n_train + n_val:]
769
-
770
- # Create sample indices for each split
771
- train_indices = [i for i, sample in enumerate(dataset) if sample['label'] in train_classes]
772
- val_indices = [i for i, sample in enumerate(dataset) if sample['label'] in val_classes]
773
- test_indices = [i for i, sample in enumerate(dataset) if sample['label'] in test_classes]
774
-
775
- return {
776
- 'train': {'indices': train_indices, 'classes': train_classes.tolist()},
777
- 'validation': {'indices': val_indices, 'classes': val_classes.tolist()},
778
- 'test': {'indices': test_indices, 'classes': test_classes.tolist()}
779
- }
780
-
781
- # Create cross-domain splits
782
- dataset = load_dataset("xixu-me/fsl-product-classification")["train"]
783
- splits = create_cross_domain_split(dataset)
784
-
785
- print(f"Train classes: {len(splits['train']['classes'])}")
786
- print(f"Validation classes: {len(splits['validation']['classes'])}")
787
- print(f"Test classes: {len(splits['test']['classes'])}")
788
- ```
789
-
790
- ### Performance Baselines
791
-
792
- Expected performance ranges for different few-shot learning approaches:
793
-
794
- | Method | 5-way 1-shot | 5-way 5-shot | 10-way 1-shot | 10-way 5-shot |
795
- |--------|--------------|--------------|----------------|----------------|
796
- | Random Baseline | 20.0% | 20.0% | 10.0% | 10.0% |
797
- | Nearest Neighbor | 35-45% | 55-65% | 25-35% | 45-55% |
798
- | Prototypical Networks | 45-55% | 65-75% | 35-45% | 55-65% |
799
- | MAML | 48-58% | 68-78% | 38-48% | 58-68% |
800
- | Relation Networks | 50-60% | 70-80% | 40-50% | 60-70% |
801
-
802
- ### Utility Functions
803
-
804
- ```python
805
- import os
806
- import json
807
- from pathlib import Path
808
- import matplotlib.pyplot as plt
809
- import seaborn as sns
810
- from collections import Counter
811
-
812
- def dataset_statistics(dataset):
813
- """
814
- Generate comprehensive statistics about the dataset
815
-
816
- Args:
817
- dataset: Hugging Face Dataset or list of samples
818
-
819
- Returns:
820
- dict: Dataset statistics
821
- """
822
- if hasattr(dataset, '__getitem__') and hasattr(dataset, '__len__'):
823
- # Hugging Face Dataset
824
- labels = dataset['label']
825
- class_names = dataset['class_name']
826
- image_ids = dataset['image_id']
827
- else:
828
- # List of samples
829
- labels = [sample['label'] for sample in dataset]
830
- class_names = [sample['class_name'] for sample in dataset]
831
- image_ids = [sample['image_id'] for sample in dataset]
832
-
833
- # Basic statistics
834
- n_samples = len(labels)
835
- n_classes = len(set(labels))
836
- class_counts = Counter(labels)
837
-
838
- # Calculate distribution statistics
839
- counts = list(class_counts.values())
840
- stats = {
841
- 'total_samples': n_samples,
842
- 'total_classes': n_classes,
843
- 'avg_samples_per_class': n_samples / n_classes,
844
- 'min_samples_per_class': min(counts),
845
- 'max_samples_per_class': max(counts),
846
- 'std_samples_per_class': np.std(counts),
847
- 'class_distribution': dict(class_counts)
848
- }
849
-
850
- return stats
851
-
852
- def plot_class_distribution(dataset, top_k=50, figsize=(15, 8)):
853
- """
854
- Plot class distribution
855
-
856
- Args:
857
- dataset: Dataset object
858
- top_k: Number of top classes to show
859
- figsize: Figure size
860
- """
861
- # Get class counts
862
- if hasattr(dataset, '__getitem__'):
863
- class_counts = Counter(dataset['label'])
864
- else:
865
- class_counts = Counter([sample['label'] for sample in dataset])
866
-
867
- # Get top k classes
868
- top_classes = class_counts.most_common(top_k)
869
- labels, counts = zip(*top_classes)
870
-
871
- # Plot
872
- plt.figure(figsize=figsize)
873
- bars = plt.bar(range(len(labels)), counts)
874
- plt.xlabel('Class ID')
875
- plt.ylabel('Number of Samples')
876
- plt.title(f'Class Distribution (Top {top_k} Classes)')
877
- plt.xticks(range(0, len(labels), max(1, len(labels)//10)),
878
- [str(l) for l in labels[::max(1, len(labels)//10)]], rotation=45)
879
-
880
- # Add statistics text
881
- total_samples = sum(counts)
882
- avg_samples = total_samples / len(counts)
883
- plt.text(0.02, 0.98, f'Total Classes: {len(class_counts)}\n'
884
- f'Shown Classes: {len(labels)}\n'
885
- f'Avg Samples/Class: {avg_samples:.1f}',
886
- transform=plt.gca().transAxes, verticalalignment='top',
887
- bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5))
888
-
889
- plt.tight_layout()
890
- plt.show()
891
-
892
- return top_classes
893
-
894
- def save_dataset_info(dataset, output_path="dataset_info.json"):
895
- """
896
- Save dataset information to JSON file
897
-
898
- Args:
899
- dataset: Dataset object
900
- output_path: Path to save the info file
901
- """
902
- stats = dataset_statistics(dataset)
903
-
904
- # Add additional metadata
905
- info = {
906
- 'dataset_name': 'FSL Product Classification Dataset',
907
- 'version': '1.0',
908
- 'statistics': stats,
909
- 'description': 'Few-shot learning dataset for product classification',
910
- 'features': {
911
- 'image': 'PIL Image object',
912
- 'label': 'Class ID (int64)',
913
- 'class_name': 'Class name string',
914
- 'image_id': 'Unique image identifier'
915
- }
916
- }
917
-
918
- # Save to file
919
- with open(output_path, 'w') as f:
920
- json.dump(info, f, indent=2)
921
-
922
- print(f"Dataset info saved to: {output_path}")
923
- return info
924
-
925
- def verify_dataset_integrity(dataset_path="data.tzst"):
926
- """
927
- Verify dataset archive integrity
928
-
929
- Args:
930
- dataset_path: Path to the dataset archive
931
-
932
- Returns:
933
- bool: True if dataset is valid
934
- """
935
- from tzst import test_archive
936
-
937
- try:
938
- # Test archive integrity
939
- is_valid = test_archive(dataset_path)
940
-
941
- if is_valid:
942
- print(f"✅ Dataset archive '{dataset_path}' is valid")
943
-
944
- # Get archive info
945
- from tzst import list_archive
946
- contents = list_archive(dataset_path, verbose=True)
947
-
948
- print(f"📁 Archive contains {len(contents)} files")
949
-
950
- # Check for expected structure
951
- class_dirs = [item['name'] for item in contents
952
- if item['name'].startswith('class_') and item['name'].endswith('/')]
953
- print(f"🏷️ Found {len(class_dirs)} class directories")
954
-
955
- return True
956
- else:
957
- print(f"❌ Dataset archive '{dataset_path}' is corrupted")
958
- return False
959
-
960
- except Exception as e:
961
- print(f"❌ Error verifying dataset: {e}")
962
- return False
963
-
964
- def create_data_splits(dataset, split_ratios={'train': 0.8, 'test': 0.2},
965
- strategy='random', seed=42):
966
- """
967
- Create train/test splits from the dataset
968
-
969
- Args:
970
- dataset: Dataset object
971
- split_ratios: Dictionary with split names and ratios
972
- strategy: 'random' or 'stratified'
973
- seed: Random seed
974
-
975
- Returns:
976
- dict: Split datasets
977
- """
978
- from sklearn.model_selection import train_test_split
979
-
980
- np.random.seed(seed)
981
-
982
- if strategy == 'random':
983
- # Simple random split
984
- indices = list(range(len(dataset)))
985
- train_size = split_ratios.get('train', 0.8)
986
-
987
- train_indices, test_indices = train_test_split(
988
- indices, train_size=train_size, random_state=seed
989
- )
990
-
991
- splits = {
992
- 'train': dataset.select(train_indices),
993
- 'test': dataset.select(test_indices)
994
- }
995
-
996
- elif strategy == 'stratified':
997
- # Stratified split maintaining class distribution
998
- labels = dataset['label']
999
- indices = list(range(len(dataset)))
1000
- train_size = split_ratios.get('train', 0.8)
1001
-
1002
- train_indices, test_indices = train_test_split(
1003
- indices, train_size=train_size, stratify=labels, random_state=seed
1004
- )
1005
-
1006
- splits = {
1007
- 'train': dataset.select(train_indices),
1008
- 'test': dataset.select(test_indices)
1009
- }
1010
-
1011
- # Print split information
1012
- for split_name, split_dataset in splits.items():
1013
- n_samples = len(split_dataset)
1014
- n_classes = len(set(split_dataset['label']))
1015
- print(f"{split_name.capitalize()} split: {n_samples} samples, {n_classes} classes")
1016
-
1017
- return splits
1018
-
1019
- # Example usage of utility functions
1020
- def analyze_dataset(dataset_path="data.tzst"):
1021
- """
1022
- Complete dataset analysis workflow
1023
- """
1024
- print("🔍 Analyzing FSL Product Classification Dataset")
1025
- print("=" * 50)
1026
-
1027
- # 1. Verify dataset integrity
1028
- print("\n1. Verifying dataset integrity...")
1029
- is_valid = verify_dataset_integrity(dataset_path)
1030
-
1031
- if not is_valid:
1032
- return
1033
-
1034
- # 2. Load dataset
1035
- print("\n2. Loading dataset...")
1036
- try:
1037
- dataset = load_dataset("xixu-me/fsl-product-classification")["train"]
1038
- print(f" Successfully loaded dataset with {len(dataset)} samples")
1039
- except Exception as e:
1040
- print(f"❌ Error loading dataset: {e}")
1041
- return
1042
-
1043
- # 3. Generate statistics
1044
- print("\n3. Generating statistics...")
1045
- stats = dataset_statistics(dataset)
1046
-
1047
- print(f"📊 Dataset Statistics:")
1048
- print(f" Total samples: {stats['total_samples']:,}")
1049
- print(f" Total classes: {stats['total_classes']:,}")
1050
- print(f" Avg samples per class: {stats['avg_samples_per_class']:.1f}")
1051
- print(f" Min samples per class: {stats['min_samples_per_class']}")
1052
- print(f" Max samples per class: {stats['max_samples_per_class']}")
1053
- print(f" Std samples per class: {stats['std_samples_per_class']:.1f}")
1054
-
1055
- # 4. Plot distributions
1056
- print("\n4. Plotting class distribution...")
1057
- plot_class_distribution(dataset, top_k=30)
1058
-
1059
- # 5. Save dataset info
1060
- print("\n5. Saving dataset information...")
1061
- save_dataset_info(dataset)
1062
-
1063
- # 6. Create splits
1064
- print("\n6. Creating data splits...")
1065
- splits = create_data_splits(dataset, strategy='stratified')
1066
-
1067
- print("\n✅ Dataset analysis complete!")
1068
- return dataset, stats, splits
1069
-
1070
- # Run analysis
1071
- # dataset, stats, splits = analyze_dataset()
1072
- ```
1073
-
1074
- ## Troubleshooting
1075
-
1076
- ### Common Issues and Solutions
1077
-
1078
- #### 1. Archive Extraction Issues
1079
-
1080
- **Problem**: Error extracting `data.tzst` file
1081
-
1082
- ```text
1083
- TzstDecompressionError: Failed to decompress archive
1084
- ```
1085
-
1086
- **Solution**:
1087
-
1088
- ```python
1089
- # Verify archive integrity first
1090
- from tzst import test_archive
1091
- if not test_archive("data.tzst"):
1092
- print("Archive is corrupted. Please re-download.")
1093
-
1094
- # Use streaming mode for large archives
1095
- from tzst import extract_archive
1096
- extract_archive("data.tzst", "output/", streaming=True)
1097
- ```
1098
-
1099
- #### 2. Memory Issues with Large Dataset
1100
-
1101
- **Problem**: Out of memory when loading the full dataset
1102
-
1103
- **Solution**:
1104
-
1105
- ```python
1106
- # Use streaming dataset
1107
- from datasets import load_dataset
1108
- dataset = load_dataset("xixu-me/fsl-product-classification", streaming=True)
1109
-
1110
- # Or load in chunks
1111
- def load_dataset_chunked(chunk_size=1000):
1112
- dataset = load_dataset("xixu-me/fsl-product-classification")["train"]
1113
- for i in range(0, len(dataset), chunk_size):
1114
- chunk = dataset.select(range(i, min(i + chunk_size, len(dataset))))
1115
- yield chunk
1116
- ```
1117
-
1118
- #### 3. Non-continuous Class Labels
1119
-
1120
- **Problem**: Class labels are not continuous (0, 1, 2, ...)
1121
-
1122
- **Solution**:
1123
-
1124
- ```python
1125
- # Create label mapping
1126
- unique_labels = sorted(set(dataset['label']))
1127
- label_to_idx = {label: idx for idx, label in enumerate(unique_labels)}
1128
-
1129
- # Apply mapping
1130
- def map_labels(example):
1131
- example['mapped_label'] = label_to_idx[example['label']]
1132
- return example
1133
-
1134
- dataset = dataset.map(map_labels)
1135
- ```
1136
-
1137
- #### 4. CUDA/GPU Issues
1138
-
1139
- **Problem**: CUDA out of memory during training
1140
-
1141
- **Solution**:
1142
-
1143
- ```python
1144
- # Reduce batch size or use CPU
1145
- device = torch.device('cpu') # Force CPU usage
1146
-
1147
- # Or use gradient accumulation
1148
- accumulation_steps = 4
1149
- for i, (support_data, query_data) in enumerate(dataloader):
1150
- loss = model(support_data, query_data) / accumulation_steps
1151
- loss.backward()
1152
-
1153
- if (i + 1) % accumulation_steps == 0:
1154
- optimizer.step()
1155
- optimizer.zero_grad()
1156
- ```
1157
-
1158
- ### Performance Tips
1159
-
1160
- 1. **Use appropriate image sizes**: For few-shot learning, 84x84 or 224x224 are common choices
1161
- 2. **Enable streaming mode**: For memory-efficient processing of large archives
1162
- 3. **Use data augmentation**: Improve few-shot performance with transforms
1163
- 4. **Cache preprocessed data**: Save processed episodes to disk for faster iteration
1164
-
1165
- ## License
1166
-
1167
- This dataset is released under the MIT License. See the [LICENSE file](LICENSE) for details.
1168
-
1169
- ## Data Ethics and Responsible Use
1170
-
1171
- This dataset is intended for academic research and educational purposes in few-shot learning and computer vision. Users should:
1172
-
1173
- - **Respect intellectual property**: Images may be subject to copyright; use only for research purposes
1174
- - **Consider bias**: Be aware that product categories may reflect certain demographic or geographic biases
1175
- - **Commercial use**: While the license permits it, consider the ethical implications of commercial applications
1176
- - **Attribution**: Please cite this dataset in any published work
1177
-
1178
- ## Limitations
1179
-
1180
- - **Image quality**: Variable image quality and backgrounds may affect model performance
1181
- - **Class imbalance**: Some classes may have significantly fewer images than others
1182
- - **Non-continuous labels**: Class numbers are not sequential, which may require label mapping
1183
- - **Temporal bias**: Product images reflect trends from the time of collection
 
1
+ ---
2
+ license: mit
3
+ viewer: false
4
+ task_categories:
5
+ - image-classification
6
+ task_ids:
7
+ - multi-class-image-classification
8
+ tags:
9
+ - computer-vision
10
+ - product-classification
11
+ - e-commerce
12
+ - retail
13
+ - few-shot-learning
14
+ - meta-learning
15
+ - benchmark
16
+ size_categories:
17
+ - 100K<n<1M
18
+ language:
19
+ - en
20
+ pretty_name: FSL Product Classification Dataset
21
+ configs:
22
+ - config_name: default
23
+ data_files: "data.tzst"
24
+ default: true
25
+ dataset_info:
26
+ features:
27
+ - name: image
28
+ dtype: image
29
+ - name: label
30
+ dtype: int64
31
+ - name: class_name
32
+ dtype: string
33
+ - name: image_id
34
+ dtype: string
35
+ splits:
36
+ - name: train
37
+ num_bytes: 9945644054
38
+ num_examples: 279747
39
+ download_size: 9945644054
40
+ dataset_size: 9945644054
41
+ ---
42
+
43
+ # Few-Shot Learning (FSL) Product Classification Dataset
44
+
45
+ ## Dataset Description
46
+
47
+ This dataset is designed for **Few-Shot Learning (FSL)** research in product classification tasks. It contains product images organized into 763 distinct classes, with an average of approximately 367 images per class (279,747 total images), making it ideal for training and evaluating few-shot learning algorithms in e-commerce and retail scenarios. Note that class numbers are not continuous.
48
+
49
+ ### Key Features
50
+
51
+ - **763 product classes** covering diverse product categories
52
+ - **279,747 total images** (average of ~367 images per class)
53
+ - **High-quality product images** suitable for computer vision research
54
+ - **Variable class distribution** with non-continuous class numbers
55
+ - **Efficient tzst compression** for reduced storage and faster transfer
56
+ - **Compatible with Hugging Face Datasets** library
57
+
58
+ ### Dataset Statistics
59
+
60
+ - **Total Classes**: 763
61
+ - **Total Images**: 279,747
62
+ - **Images per Class**: ~367 (average, variable distribution)
63
+ - **Class Numbers**: Non-continuous (some class numbers may be missing)
64
+ - **Image Format**: PNG
65
+ - **Typical Image Size**: 50-100 KB per image
66
+ - **Average Images per Class**: 366.6 (279,747 ÷ 763)
67
+ - **Compressed Archive Size**: ~9.9 GB (data.tzst)
68
+ - **Extraction Requirements**: ~10 GB additional space
69
+
70
+ ## Dataset Structure
71
+
72
+ The dataset is stored in a compressed tzst archive ([`data.tzst`](data.tzst)) with the following structure:
73
+
74
+ ```text
75
+ data.tzst
76
+ ├── class_0/
77
+ │ ├── class_0_0.png
78
+ │ ├── class_0_1.png
79
+ │ └── ...
80
+ ├─��� class_1/
81
+ │ ├── class_1_0.png
82
+ │ ├── class_1_1.png
83
+ │ └── ...
84
+ └── ... (763 total classes with non-continuous numbers)
85
+ ```
86
+
87
+ **Note**: Class numbers are not continuous. For example, you might have class_0, class_2, class_5, etc., but not class_1, class_3, class_4. The total number of classes is 763.
88
+
89
+ ## Usage
90
+
91
+ ## Installation and Setup
92
+
93
+ ### Quick Start Installation
94
+
95
+ ```bash
96
+ # Create a new virtual environment (recommended)
97
+ python -m venv fsl-env
98
+ source fsl-env/bin/activate # On Windows: fsl-env\Scripts\activate
99
+
100
+ # Install core dependencies
101
+ pip install datasets tzst pillow
102
+
103
+ # Install additional dependencies for machine learning
104
+ pip install torch torchvision numpy scikit-learn matplotlib seaborn tqdm
105
+
106
+ # For Jupyter notebook users
107
+ pip install jupyter ipywidgets
108
+ ```
109
+
110
+ ### Complete Requirements
111
+
112
+ Create a `requirements.txt` file with the following dependencies:
113
+
114
+ ```text
115
+ # Core dependencies
116
+ datasets>=2.14.0
117
+ tzst>=1.2.8
118
+ pillow>=9.0.0
119
+
120
+ # Machine learning
121
+ torch>=1.9.0
122
+ torchvision>=0.10.0
123
+ numpy>=1.21.0
124
+ scikit-learn>=1.0.0
125
+
126
+ # Data analysis and visualization
127
+ pandas>=1.3.0
128
+ matplotlib>=3.4.0
129
+ seaborn>=0.11.0
130
+
131
+ # Progress bars and utilities
132
+ tqdm>=4.62.0
133
+ pathlib>=1.0.1
134
+
135
+ # Optional: for advanced few-shot learning
136
+ learn2learn>=0.1.7
137
+ higher>=0.2.1
138
+
139
+ # Optional: for notebook usage
140
+ jupyter>=1.0.0
141
+ ipywidgets>=7.6.0
142
+ ```
143
+
144
+ Install all requirements:
145
+
146
+ ```bash
147
+ pip install -r requirements.txt
148
+ ```
149
+
150
+ ### Docker Setup (Optional)
151
+
152
+ For a containerized environment:
153
+
154
+ ```dockerfile
155
+ FROM python:3.9-slim
156
+
157
+ WORKDIR /app
158
+
159
+ # Install system dependencies
160
+ RUN apt-get update && apt-get install -y \
161
+ git \
162
+ wget \
163
+ && rm -rf /var/lib/apt/lists/*
164
+
165
+ # Copy requirements and install Python dependencies
166
+ COPY requirements.txt .
167
+ RUN pip install --no-cache-dir -r requirements.txt
168
+
169
+ # Copy application code
170
+ COPY . .
171
+
172
+ # Set environment variables
173
+ ENV PYTHONPATH=/app
174
+ ENV HF_DATASETS_CACHE=/app/cache
175
+
176
+ # Create cache directory
177
+ RUN mkdir -p /app/cache
178
+
179
+ CMD ["python", "-c", "print('FSL Product Classification environment ready!')"]
180
+ ```
181
+
182
+ Build and run:
183
+
184
+ ```bash
185
+ docker build -t fsl-product-classification .
186
+ docker run -it --rm -v $(pwd)/data:/app/data fsl-product-classification bash
187
+ ```
188
+
189
+ ### Loading the Dataset
190
+
191
+ ```python
192
+ import os
193
+ from tzst import extract_archive
194
+ from datasets import Dataset, Features, Value, Image, ClassLabel
195
+ from PIL import Image as PILImage
196
+
197
+ # Extract the dataset archive
198
+ extract_archive("data.tzst", "extracted_data/")
199
+
200
+ # Create a custom dataset loader
201
+ def load_fsl_dataset(data_dir="extracted_data"):
202
+ samples = []
203
+ class_names = []
204
+
205
+ # Scan for class directories
206
+ for class_dir in sorted(os.listdir(data_dir)):
207
+ if class_dir.startswith("class_"):
208
+ class_path = os.path.join(data_dir, class_dir)
209
+ if os.path.isdir(class_path):
210
+ class_id = int(class_dir.split("_")[1])
211
+ class_names.append(class_dir)
212
+
213
+ # Load images from this class
214
+ for img_file in os.listdir(class_path):
215
+ if img_file.endswith('.png'):
216
+ img_path = os.path.join(class_path, img_file)
217
+ image_id = img_file.replace('.png', '')
218
+
219
+ samples.append({
220
+ 'image': img_path,
221
+ 'label': class_id,
222
+ 'class_name': class_dir,
223
+ 'image_id': image_id
224
+ })
225
+
226
+ # Create features definition
227
+ features = Features({
228
+ 'image': Image(),
229
+ 'label': Value('int64'),
230
+ 'class_name': Value('string'),
231
+ 'image_id': Value('string')
232
+ })
233
+
234
+ # Create dataset
235
+ return Dataset.from_list(samples, features=features)
236
+
237
+ # Load the dataset
238
+ dataset = load_fsl_dataset()
239
+ print(f"Loaded {len(dataset)} samples from {len(set(dataset['class_name']))} classes")
240
+ ```
241
+
242
+ Streaming mode for memory-efficient processing of large archive:
243
+
244
+ ```python
245
+ from tzst import extract_archive
246
+ import tempfile
247
+ import os
248
+
249
+ # Use streaming extraction for memory efficiency
250
+ with tempfile.TemporaryDirectory() as temp_dir:
251
+ # Extract with streaming mode
252
+ extract_archive("data.tzst", temp_dir, streaming=True)
253
+
254
+ # Process extracted data
255
+ dataset = load_fsl_dataset(temp_dir)
256
+ # ... your processing code here
257
+ ```
258
+
259
+ ### Data Exploration
260
+
261
+ ```python
262
+ from collections import Counter
263
+ import matplotlib.pyplot as plt
264
+
265
+ # Load dataset
266
+ dataset = load_dataset("xixu-me/fsl-product-classification")["train"]
267
+
268
+ # Analyze class distribution
269
+ class_counts = Counter(dataset['class_name'])
270
+ print(f"Number of classes: {len(class_counts)}")
271
+ print(f"Average images per class: {len(dataset) / len(class_counts):.1f}")
272
+
273
+ # Plot class distribution (top 20 classes)
274
+ top_classes = class_counts.most_common(20)
275
+ classes, counts = zip(*top_classes)
276
+
277
+ plt.figure(figsize=(12, 6))
278
+ plt.bar(range(len(classes)), counts)
279
+ plt.xlabel('Class')
280
+ plt.ylabel('Number of Images')
281
+ plt.title('Top 20 Classes by Image Count')
282
+ plt.xticks(range(len(classes)), [c.replace('class_', '') for c in classes], rotation=45)
283
+ plt.tight_layout()
284
+ plt.show()
285
+
286
+ # Display sample images
287
+ import random
288
+
289
+ def show_samples(dataset, num_samples=8):
290
+ """Display random samples from the dataset"""
291
+ indices = random.sample(range(len(dataset)), num_samples)
292
+
293
+ fig, axes = plt.subplots(2, 4, figsize=(15, 8))
294
+ axes = axes.flatten()
295
+
296
+ for i, idx in enumerate(indices):
297
+ sample = dataset[idx]
298
+ axes[i].imshow(sample['image'])
299
+ axes[i].set_title(f"{sample['class_name']}\nID: {sample['image_id']}")
300
+ axes[i].axis('off')
301
+
302
+ plt.tight_layout()
303
+ plt.show()
304
+
305
+ # Show sample images
306
+ show_samples(dataset)
307
+ ```
308
+
309
+ ### Few-Shot Learning Setup
310
+
311
+ #### Basic Few-Shot Episode Creation
312
+
313
+ ```python
314
+ import random
315
+ from collections import defaultdict
316
+ import torch
317
+ from torch.utils.data import DataLoader
318
+ from datasets import load_dataset
319
+
320
+ def create_few_shot_split(dataset, n_way=5, k_shot=5, n_query=15, seed=None):
321
+ """
322
+ Create a few-shot learning episode
323
+
324
+ Args:
325
+ dataset: Hugging Face Dataset instance or custom dataset
326
+ n_way: Number of classes in the episode
327
+ k_shot: Number of support samples per class
328
+ n_query: Number of query samples per class
329
+ seed: Random seed for reproducibility
330
+
331
+ Returns:
332
+ support_set, query_set: Lists of (image, label) tuples
333
+ """
334
+ if seed is not None:
335
+ random.seed(seed)
336
+
337
+ # Group samples by class
338
+ class_samples = defaultdict(list)
339
+ for i, sample in enumerate(dataset):
340
+ class_samples[sample['label']].append(i)
341
+
342
+ # Filter classes with enough samples
343
+ valid_classes = [
344
+ class_id for class_id, indices in class_samples.items()
345
+ if len(indices) >= k_shot + n_query
346
+ ]
347
+
348
+ if len(valid_classes) < n_way:
349
+ raise ValueError(f"Not enough classes with {k_shot + n_query} samples. "
350
+ f"Found {len(valid_classes)}, need {n_way}")
351
+
352
+ # Sample n_way classes
353
+ episode_classes = random.sample(valid_classes, n_way)
354
+
355
+ support_set = []
356
+ query_set = []
357
+
358
+ for new_label, original_class in enumerate(episode_classes):
359
+ class_indices = random.sample(class_samples[original_class], k_shot + n_query)
360
+
361
+ # Support samples
362
+ for idx in class_indices[:k_shot]:
363
+ sample = dataset[idx]
364
+ support_set.append((sample['image'], new_label, sample['image_id']))
365
+
366
+ # Query samples
367
+ for idx in class_indices[k_shot:]:
368
+ sample = dataset[idx]
369
+ query_set.append((sample['image'], new_label, sample['image_id']))
370
+
371
+ return support_set, query_set
372
+
373
+ # Example usage
374
+ dataset = load_dataset("xixu-me/fsl-product-classification")["train"]
375
+
376
+ # Create a 5-way 5-shot episode
377
+ support_set, query_set = create_few_shot_split(dataset, n_way=5, k_shot=5, n_query=15)
378
+
379
+ print(f"Support set: {len(support_set)} samples")
380
+ print(f"Query set: {len(query_set)} samples")
381
+ ```
382
+
383
+ #### Advanced FSL Dataset Class
384
+
385
+ ```python
386
+ import torch
387
+ from torch.utils.data import Dataset
388
+ from torchvision import transforms
389
+ from PIL import Image
390
+ import numpy as np
391
+
392
+ class FSLProductDataset(Dataset):
393
+ """
394
+ Few-Shot Learning Dataset wrapper for product classification
395
+ """
396
+
397
+ def __init__(self, hf_dataset, transform=None, target_transform=None):
398
+ self.dataset = hf_dataset
399
+ self.transform = transform or self.get_default_transform()
400
+ self.target_transform = target_transform
401
+
402
+ # Create label mapping for non-continuous labels
403
+ unique_labels = sorted(set(hf_dataset['label']))
404
+ self.label_to_idx = {label: idx for idx, label in enumerate(unique_labels)}
405
+ self.idx_to_label = {idx: label for label, idx in self.label_to_idx.items()}
406
+
407
+ def get_default_transform(self):
408
+ """Default image transformations"""
409
+ return transforms.Compose([
410
+ transforms.Resize((224, 224)),
411
+ transforms.ToTensor(),
412
+ transforms.Normalize(mean=[0.485, 0.456, 0.406],
413
+ std=[0.229, 0.224, 0.225])
414
+ ])
415
+
416
+ def __len__(self):
417
+ return len(self.dataset)
418
+
419
+ def __getitem__(self, idx):
420
+ sample = self.dataset[idx]
421
+ image = sample['image']
422
+
423
+ # Convert to PIL Image if needed
424
+ if not isinstance(image, Image.Image):
425
+ image = Image.fromarray(image)
426
+
427
+ # Apply transforms
428
+ if self.transform:
429
+ image = self.transform(image)
430
+
431
+ # Map label to continuous indices
432
+ label = self.label_to_idx[sample['label']]
433
+
434
+ if self.target_transform:
435
+ label = self.target_transform(label)
436
+
437
+ return image, label, sample['image_id']
438
+
439
+ def get_class_samples(self, class_label):
440
+ """Get all samples for a specific class"""
441
+ indices = [i for i, sample in enumerate(self.dataset)
442
+ if sample['label'] == class_label]
443
+ return [self[i] for i in indices]
444
+
445
+ def create_episode_dataloader(self, n_way=5, k_shot=5, n_query=15,
446
+ batch_size=None, shuffle=True):
447
+ """Create a DataLoader for a few-shot episode"""
448
+ support_set, query_set = create_few_shot_split(
449
+ self.dataset, n_way=n_way, k_shot=k_shot, n_query=n_query
450
+ )
451
+
452
+ # Convert to tensors
453
+ support_images = []
454
+ support_labels = []
455
+ query_images = []
456
+ query_labels = []
457
+
458
+ for image, label, _ in support_set:
459
+ if isinstance(image, Image.Image):
460
+ image = self.transform(image) if self.transform else image
461
+ support_images.append(image)
462
+ support_labels.append(label)
463
+
464
+ for image, label, _ in query_set:
465
+ if isinstance(image, Image.Image):
466
+ image = self.transform(image) if self.transform else image
467
+ query_images.append(image)
468
+ query_labels.append(label)
469
+
470
+ support_data = (torch.stack(support_images), torch.tensor(support_labels))
471
+ query_data = (torch.stack(query_images), torch.tensor(query_labels))
472
+
473
+ return support_data, query_data
474
+
475
+ # Example usage with PyTorch
476
+ transform = transforms.Compose([
477
+ transforms.Resize((84, 84)), # Common size for few-shot learning
478
+ transforms.ToTensor(),
479
+ transforms.Normalize(mean=[0.485, 0.456, 0.406],
480
+ std=[0.229, 0.224, 0.225])
481
+ ])
482
+
483
+ # Load dataset
484
+ hf_dataset = load_dataset("xixu-me/fsl-product-classification")["train"]
485
+ fsl_dataset = FSLProductDataset(hf_dataset, transform=transform)
486
+
487
+ # Create episode data
488
+ support_data, query_data = fsl_dataset.create_episode_dataloader(
489
+ n_way=5, k_shot=1, n_query=15
490
+ )
491
+
492
+ print(f"Support images shape: {support_data[0].shape}")
493
+ print(f"Support labels shape: {support_data[1].shape}")
494
+ print(f"Query images shape: {query_data[0].shape}")
495
+ print(f"Query labels shape: {query_data[1].shape}")
496
+ ```
497
+
498
+ #### Meta-Learning Training Loop
499
+
500
+ ```python
501
+ import torch.nn as nn
502
+ import torch.optim as optim
503
+ from tqdm import tqdm
504
+
505
+ def train_fsl_model(model, dataset, num_episodes=1000, n_way=5, k_shot=1,
506
+ n_query=15, lr=0.001, device='cuda'):
507
+ """
508
+ Basic training loop for few-shot learning
509
+
510
+ Args:
511
+ model: Few-shot learning model (e.g., Prototypical Network)
512
+ dataset: FSLProductDataset instance
513
+ num_episodes: Number of training episodes
514
+ n_way, k_shot, n_query: Episode configuration
515
+ lr: Learning rate
516
+ device: Training device
517
+ """
518
+ model.to(device)
519
+ optimizer = optim.Adam(model.parameters(), lr=lr)
520
+ criterion = nn.CrossEntropyLoss()
521
+
522
+ model.train()
523
+ total_loss = 0
524
+ total_acc = 0
525
+
526
+ for episode in tqdm(range(num_episodes), desc="Training"):
527
+ # Create episode
528
+ support_data, query_data = dataset.create_episode_dataloader(
529
+ n_way=n_way, k_shot=k_shot, n_query=n_query
530
+ )
531
+
532
+ support_images, support_labels = support_data
533
+ query_images, query_labels = query_data
534
+
535
+ # Move to device
536
+ support_images = support_images.to(device)
537
+ support_labels = support_labels.to(device)
538
+ query_images = query_images.to(device)
539
+ query_labels = query_labels.to(device)
540
+
541
+ # Forward pass
542
+ optimizer.zero_grad()
543
+ logits = model(support_images, support_labels, query_images)
544
+ loss = criterion(logits, query_labels)
545
+
546
+ # Backward pass
547
+ loss.backward()
548
+ optimizer.step()
549
+
550
+ # Calculate accuracy
551
+ pred = logits.argmax(dim=1)
552
+ acc = (pred == query_labels).float().mean()
553
+
554
+ total_loss += loss.item()
555
+ total_acc += acc.item()
556
+
557
+ if (episode + 1) % 100 == 0:
558
+ avg_loss = total_loss / 100
559
+ avg_acc = total_acc / 100
560
+ print(f"Episode {episode + 1}: Loss = {avg_loss:.4f}, Acc = {avg_acc:.4f}")
561
+ total_loss = 0
562
+ total_acc = 0
563
+
564
+ # Example: Simple Prototypical Network
565
+ class SimplePrototypicalNetwork(nn.Module):
566
+ def __init__(self, backbone):
567
+ super().__init__()
568
+ self.backbone = backbone
569
+
570
+ def forward(self, support_images, support_labels, query_images):
571
+ # Encode images
572
+ support_features = self.backbone(support_images)
573
+ query_features = self.backbone(query_images)
574
+
575
+ # Calculate prototypes
576
+ n_way = len(torch.unique(support_labels))
577
+ prototypes = []
578
+
579
+ for class_idx in range(n_way):
580
+ class_mask = support_labels == class_idx
581
+ class_features = support_features[class_mask]
582
+ prototype = class_features.mean(dim=0)
583
+ prototypes.append(prototype)
584
+
585
+ prototypes = torch.stack(prototypes)
586
+
587
+ # Calculate distances and logits
588
+ distances = torch.cdist(query_features, prototypes)
589
+ logits = -distances # Negative distance as logits
590
+
591
+ return logits
592
+ ```
593
+
594
+ ## Research Applications
595
+
596
+ This dataset is particularly well-suited for:
597
+
598
+ ### Few-Shot Learning
599
+
600
+ - **Meta-learning algorithms** (MAML, Prototypical Networks, Relation Networks)
601
+ - **Metric learning approaches** (Siamese Networks, Triplet Networks)
602
+ - **Gradient-based meta-learning** methods
603
+
604
+ ### Transfer Learning
605
+
606
+ - **Pre-training** on large-scale product data
607
+ - **Domain adaptation** from general images to products
608
+ - **Fine-tuning** strategies for product classification
609
+
610
+ ### Computer Vision Research
611
+
612
+ - **Product recognition** and retrieval
613
+ - **E-commerce applications**
614
+ - **Retail automation**
615
+ - **Visual search** systems
616
+
617
+ ## Benchmark Tasks
618
+
619
+ ### Standard Few-Shot Learning Evaluation
620
+
621
+ The following benchmarks are recommended for evaluating few-shot learning models on this dataset:
622
+
623
+ #### Standard Evaluation Protocol
624
+
625
+ ```python
626
+ import numpy as np
627
+ from sklearn.metrics import accuracy_score, classification_report
628
+ import json
629
+
630
+ def evaluate_fsl_model(model, dataset, num_episodes=600, n_way=5, k_shot=1,
631
+ n_query=15, device='cuda'):
632
+ """
633
+ Evaluate few-shot learning model using standard protocol
634
+
635
+ Returns:
636
+ dict: Evaluation results with mean accuracy and confidence interval
637
+ """
638
+ model.eval()
639
+ accuracies = []
640
+
641
+ with torch.no_grad():
642
+ for _ in tqdm(range(num_episodes), desc="Evaluating"):
643
+ # Create episode
644
+ support_data, query_data = dataset.create_episode_dataloader(
645
+ n_way=n_way, k_shot=k_shot, n_query=n_query
646
+ )
647
+
648
+ support_images, support_labels = support_data
649
+ query_images, query_labels = query_data
650
+
651
+ # Move to device
652
+ support_images = support_images.to(device)
653
+ support_labels = support_labels.to(device)
654
+ query_images = query_images.to(device)
655
+ query_labels = query_labels.to(device)
656
+
657
+ # Predict
658
+ logits = model(support_images, support_labels, query_images)
659
+ pred = logits.argmax(dim=1)
660
+
661
+ # Calculate episode accuracy
662
+ acc = (pred == query_labels).float().mean().item()
663
+ accuracies.append(acc)
664
+
665
+ # Calculate statistics
666
+ mean_acc = np.mean(accuracies)
667
+ std_acc = np.std(accuracies)
668
+ ci_95 = 1.96 * std_acc / np.sqrt(len(accuracies))
669
+
670
+ results = {
671
+ 'mean_accuracy': mean_acc,
672
+ 'std_accuracy': std_acc,
673
+ 'confidence_interval_95': ci_95,
674
+ 'num_episodes': num_episodes,
675
+ 'config': f"{n_way}-way {k_shot}-shot"
676
+ }
677
+
678
+ return results
679
+
680
+ # Benchmark configurations
681
+ benchmark_configs = [
682
+ {'n_way': 5, 'k_shot': 1, 'n_query': 15}, # 5-way 1-shot
683
+ {'n_way': 5, 'k_shot': 5, 'n_query': 15}, # 5-way 5-shot
684
+ {'n_way': 10, 'k_shot': 1, 'n_query': 15}, # 10-way 1-shot
685
+ {'n_way': 10, 'k_shot': 5, 'n_query': 15}, # 10-way 5-shot
686
+ ]
687
+
688
+ # Run benchmarks
689
+ def run_benchmark_suite(model, dataset, num_episodes=600):
690
+ """Run complete benchmark suite"""
691
+ results = {}
692
+
693
+ for config in benchmark_configs:
694
+ config_name = f"{config['n_way']}-way_{config['k_shot']}-shot"
695
+ print(f"\nEvaluating {config_name}...")
696
+
697
+ result = evaluate_fsl_model(
698
+ model, dataset, num_episodes=num_episodes, **config
699
+ )
700
+ results[config_name] = result
701
+
702
+ print(f"Accuracy: {result['mean_accuracy']:.4f} ± {result['confidence_interval_95']:.4f}")
703
+
704
+ return results
705
+
706
+ # Example usage
707
+ # results = run_benchmark_suite(model, test_dataset)
708
+ ```
709
+
710
+ #### Cross-Domain Evaluation
711
+
712
+ ```python
713
+ def create_cross_domain_split(dataset, train_ratio=0.6, val_ratio=0.2, test_ratio=0.2, seed=42):
714
+ """
715
+ Create train/validation/test splits at the class level for cross-domain evaluation
716
+
717
+ Args:
718
+ dataset: Hugging Face Dataset
719
+ train_ratio: Proportion of classes for training
720
+ val_ratio: Proportion of classes for validation
721
+ test_ratio: Proportion of classes for testing
722
+ seed: Random seed
723
+
724
+ Returns:
725
+ dict: Splits with class indices for each set
726
+ """
727
+ np.random.seed(seed)
728
+
729
+ # Get unique classes
730
+ unique_classes = sorted(set(dataset['label']))
731
+ n_classes = len(unique_classes)
732
+
733
+ # Calculate split sizes
734
+ n_train = int(n_classes * train_ratio)
735
+ n_val = int(n_classes * val_ratio)
736
+ n_test = n_classes - n_train - n_val
737
+
738
+ # Shuffle and split classes
739
+ shuffled_classes = np.random.permutation(unique_classes)
740
+ train_classes = shuffled_classes[:n_train]
741
+ val_classes = shuffled_classes[n_train:n_train + n_val]
742
+ test_classes = shuffled_classes[n_train + n_val:]
743
+
744
+ # Create sample indices for each split
745
+ train_indices = [i for i, sample in enumerate(dataset) if sample['label'] in train_classes]
746
+ val_indices = [i for i, sample in enumerate(dataset) if sample['label'] in val_classes]
747
+ test_indices = [i for i, sample in enumerate(dataset) if sample['label'] in test_classes]
748
+
749
+ return {
750
+ 'train': {'indices': train_indices, 'classes': train_classes.tolist()},
751
+ 'validation': {'indices': val_indices, 'classes': val_classes.tolist()},
752
+ 'test': {'indices': test_indices, 'classes': test_classes.tolist()}
753
+ }
754
+
755
+ # Create cross-domain splits
756
+ dataset = load_dataset("xixu-me/fsl-product-classification")["train"]
757
+ splits = create_cross_domain_split(dataset)
758
+
759
+ print(f"Train classes: {len(splits['train']['classes'])}")
760
+ print(f"Validation classes: {len(splits['validation']['classes'])}")
761
+ print(f"Test classes: {len(splits['test']['classes'])}")
762
+ ```
763
+
764
+ ### Performance Baselines
765
+
766
+ Expected performance ranges for different few-shot learning approaches:
767
+
768
+ | Method | 5-way 1-shot | 5-way 5-shot | 10-way 1-shot | 10-way 5-shot |
769
+ |--------|--------------|--------------|----------------|----------------|
770
+ | Random Baseline | 20.0% | 20.0% | 10.0% | 10.0% |
771
+ | Nearest Neighbor | 35-45% | 55-65% | 25-35% | 45-55% |
772
+ | Prototypical Networks | 45-55% | 65-75% | 35-45% | 55-65% |
773
+ | MAML | 48-58% | 68-78% | 38-48% | 58-68% |
774
+ | Relation Networks | 50-60% | 70-80% | 40-50% | 60-70% |
775
+
776
+ ### Utility Functions
777
+
778
+ ```python
779
+ import os
780
+ import json
781
+ from pathlib import Path
782
+ import matplotlib.pyplot as plt
783
+ import seaborn as sns
784
+ from collections import Counter
785
+
786
+ def dataset_statistics(dataset):
787
+ """
788
+ Generate comprehensive statistics about the dataset
789
+
790
+ Args:
791
+ dataset: Hugging Face Dataset or list of samples
792
+
793
+ Returns:
794
+ dict: Dataset statistics
795
+ """
796
+ if hasattr(dataset, '__getitem__') and hasattr(dataset, '__len__'):
797
+ # Hugging Face Dataset
798
+ labels = dataset['label']
799
+ class_names = dataset['class_name']
800
+ image_ids = dataset['image_id']
801
+ else:
802
+ # List of samples
803
+ labels = [sample['label'] for sample in dataset]
804
+ class_names = [sample['class_name'] for sample in dataset]
805
+ image_ids = [sample['image_id'] for sample in dataset]
806
+
807
+ # Basic statistics
808
+ n_samples = len(labels)
809
+ n_classes = len(set(labels))
810
+ class_counts = Counter(labels)
811
+
812
+ # Calculate distribution statistics
813
+ counts = list(class_counts.values())
814
+ stats = {
815
+ 'total_samples': n_samples,
816
+ 'total_classes': n_classes,
817
+ 'avg_samples_per_class': n_samples / n_classes,
818
+ 'min_samples_per_class': min(counts),
819
+ 'max_samples_per_class': max(counts),
820
+ 'std_samples_per_class': np.std(counts),
821
+ 'class_distribution': dict(class_counts)
822
+ }
823
+
824
+ return stats
825
+
826
+ def plot_class_distribution(dataset, top_k=50, figsize=(15, 8)):
827
+ """
828
+ Plot class distribution
829
+
830
+ Args:
831
+ dataset: Dataset object
832
+ top_k: Number of top classes to show
833
+ figsize: Figure size
834
+ """
835
+ # Get class counts
836
+ if hasattr(dataset, '__getitem__'):
837
+ class_counts = Counter(dataset['label'])
838
+ else:
839
+ class_counts = Counter([sample['label'] for sample in dataset])
840
+
841
+ # Get top k classes
842
+ top_classes = class_counts.most_common(top_k)
843
+ labels, counts = zip(*top_classes)
844
+
845
+ # Plot
846
+ plt.figure(figsize=figsize)
847
+ bars = plt.bar(range(len(labels)), counts)
848
+ plt.xlabel('Class ID')
849
+ plt.ylabel('Number of Samples')
850
+ plt.title(f'Class Distribution (Top {top_k} Classes)')
851
+ plt.xticks(range(0, len(labels), max(1, len(labels)//10)),
852
+ [str(l) for l in labels[::max(1, len(labels)//10)]], rotation=45)
853
+
854
+ # Add statistics text
855
+ total_samples = sum(counts)
856
+ avg_samples = total_samples / len(counts)
857
+ plt.text(0.02, 0.98, f'Total Classes: {len(class_counts)}\n'
858
+ f'Shown Classes: {len(labels)}\n'
859
+ f'Avg Samples/Class: {avg_samples:.1f}',
860
+ transform=plt.gca().transAxes, verticalalignment='top',
861
+ bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5))
862
+
863
+ plt.tight_layout()
864
+ plt.show()
865
+
866
+ return top_classes
867
+
868
+ def save_dataset_info(dataset, output_path="dataset_info.json"):
869
+ """
870
+ Save dataset information to JSON file
871
+
872
+ Args:
873
+ dataset: Dataset object
874
+ output_path: Path to save the info file
875
+ """
876
+ stats = dataset_statistics(dataset)
877
+
878
+ # Add additional metadata
879
+ info = {
880
+ 'dataset_name': 'FSL Product Classification Dataset',
881
+ 'version': '1.0',
882
+ 'statistics': stats,
883
+ 'description': 'Few-shot learning dataset for product classification',
884
+ 'features': {
885
+ 'image': 'PIL Image object',
886
+ 'label': 'Class ID (int64)',
887
+ 'class_name': 'Class name string',
888
+ 'image_id': 'Unique image identifier'
889
+ }
890
+ }
891
+
892
+ # Save to file
893
+ with open(output_path, 'w') as f:
894
+ json.dump(info, f, indent=2)
895
+
896
+ print(f"Dataset info saved to: {output_path}")
897
+ return info
898
+
899
+ def verify_dataset_integrity(dataset_path="data.tzst"):
900
+ """
901
+ Verify dataset archive integrity
902
+
903
+ Args:
904
+ dataset_path: Path to the dataset archive
905
+
906
+ Returns:
907
+ bool: True if dataset is valid
908
+ """
909
+ from tzst import test_archive
910
+
911
+ try:
912
+ # Test archive integrity
913
+ is_valid = test_archive(dataset_path)
914
+
915
+ if is_valid:
916
+ print(f"✅ Dataset archive '{dataset_path}' is valid")
917
+
918
+ # Get archive info
919
+ from tzst import list_archive
920
+ contents = list_archive(dataset_path, verbose=True)
921
+
922
+ print(f"📁 Archive contains {len(contents)} files")
923
+
924
+ # Check for expected structure
925
+ class_dirs = [item['name'] for item in contents
926
+ if item['name'].startswith('class_') and item['name'].endswith('/')]
927
+ print(f"🏷️ Found {len(class_dirs)} class directories")
928
+
929
+ return True
930
+ else:
931
+ print(f"❌ Dataset archive '{dataset_path}' is corrupted")
932
+ return False
933
+
934
+ except Exception as e:
935
+ print(f"❌ Error verifying dataset: {e}")
936
+ return False
937
+
938
+ def create_data_splits(dataset, split_ratios={'train': 0.8, 'test': 0.2},
939
+ strategy='random', seed=42):
940
+ """
941
+ Create train/test splits from the dataset
942
+
943
+ Args:
944
+ dataset: Dataset object
945
+ split_ratios: Dictionary with split names and ratios
946
+ strategy: 'random' or 'stratified'
947
+ seed: Random seed
948
+
949
+ Returns:
950
+ dict: Split datasets
951
+ """
952
+ from sklearn.model_selection import train_test_split
953
+
954
+ np.random.seed(seed)
955
+
956
+ if strategy == 'random':
957
+ # Simple random split
958
+ indices = list(range(len(dataset)))
959
+ train_size = split_ratios.get('train', 0.8)
960
+
961
+ train_indices, test_indices = train_test_split(
962
+ indices, train_size=train_size, random_state=seed
963
+ )
964
+
965
+ splits = {
966
+ 'train': dataset.select(train_indices),
967
+ 'test': dataset.select(test_indices)
968
+ }
969
+
970
+ elif strategy == 'stratified':
971
+ # Stratified split maintaining class distribution
972
+ labels = dataset['label']
973
+ indices = list(range(len(dataset)))
974
+ train_size = split_ratios.get('train', 0.8)
975
+
976
+ train_indices, test_indices = train_test_split(
977
+ indices, train_size=train_size, stratify=labels, random_state=seed
978
+ )
979
+
980
+ splits = {
981
+ 'train': dataset.select(train_indices),
982
+ 'test': dataset.select(test_indices)
983
+ }
984
+
985
+ # Print split information
986
+ for split_name, split_dataset in splits.items():
987
+ n_samples = len(split_dataset)
988
+ n_classes = len(set(split_dataset['label']))
989
+ print(f"{split_name.capitalize()} split: {n_samples} samples, {n_classes} classes")
990
+
991
+ return splits
992
+
993
+ # Example usage of utility functions
994
+ def analyze_dataset(dataset_path="data.tzst"):
995
+ """
996
+ Complete dataset analysis workflow
997
+ """
998
+ print("🔍 Analyzing FSL Product Classification Dataset")
999
+ print("=" * 50)
1000
+
1001
+ # 1. Verify dataset integrity
1002
+ print("\n1. Verifying dataset integrity...")
1003
+ is_valid = verify_dataset_integrity(dataset_path)
1004
+
1005
+ if not is_valid:
1006
+ return
1007
+
1008
+ # 2. Load dataset
1009
+ print("\n2. Loading dataset...")
1010
+ try:
1011
+ dataset = load_dataset("xixu-me/fsl-product-classification")["train"]
1012
+ print(f"✅ Successfully loaded dataset with {len(dataset)} samples")
1013
+ except Exception as e:
1014
+ print(f"❌ Error loading dataset: {e}")
1015
+ return
1016
+
1017
+ # 3. Generate statistics
1018
+ print("\n3. Generating statistics...")
1019
+ stats = dataset_statistics(dataset)
1020
+
1021
+ print(f"📊 Dataset Statistics:")
1022
+ print(f" Total samples: {stats['total_samples']:,}")
1023
+ print(f" Total classes: {stats['total_classes']:,}")
1024
+ print(f" Avg samples per class: {stats['avg_samples_per_class']:.1f}")
1025
+ print(f" Min samples per class: {stats['min_samples_per_class']}")
1026
+ print(f" Max samples per class: {stats['max_samples_per_class']}")
1027
+ print(f" Std samples per class: {stats['std_samples_per_class']:.1f}")
1028
+
1029
+ # 4. Plot distributions
1030
+ print("\n4. Plotting class distribution...")
1031
+ plot_class_distribution(dataset, top_k=30)
1032
+
1033
+ # 5. Save dataset info
1034
+ print("\n5. Saving dataset information...")
1035
+ save_dataset_info(dataset)
1036
+
1037
+ # 6. Create splits
1038
+ print("\n6. Creating data splits...")
1039
+ splits = create_data_splits(dataset, strategy='stratified')
1040
+
1041
+ print("\n✅ Dataset analysis complete!")
1042
+ return dataset, stats, splits
1043
+
1044
+ # Run analysis
1045
+ # dataset, stats, splits = analyze_dataset()
1046
+ ```
1047
+
1048
+ ## Troubleshooting
1049
+
1050
+ ### Common Issues and Solutions
1051
+
1052
+ #### 1. Archive Extraction Issues
1053
+
1054
+ **Problem**: Error extracting `data.tzst` file
1055
+
1056
+ ```text
1057
+ TzstDecompressionError: Failed to decompress archive
1058
+ ```
1059
+
1060
+ **Solution**:
1061
+
1062
+ ```python
1063
+ # Verify archive integrity first
1064
+ from tzst import test_archive
1065
+ if not test_archive("data.tzst"):
1066
+ print("Archive is corrupted. Please re-download.")
1067
+
1068
+ # Use streaming mode for large archives
1069
+ from tzst import extract_archive
1070
+ extract_archive("data.tzst", "output/", streaming=True)
1071
+ ```
1072
+
1073
+ #### 2. Memory Issues with Large Dataset
1074
+
1075
+ **Problem**: Out of memory when loading the full dataset
1076
+
1077
+ **Solution**:
1078
+
1079
+ ```python
1080
+ # Use streaming dataset
1081
+ from datasets import load_dataset
1082
+ dataset = load_dataset("xixu-me/fsl-product-classification", streaming=True)
1083
+
1084
+ # Or load in chunks
1085
+ def load_dataset_chunked(chunk_size=1000):
1086
+ dataset = load_dataset("xixu-me/fsl-product-classification")["train"]
1087
+ for i in range(0, len(dataset), chunk_size):
1088
+ chunk = dataset.select(range(i, min(i + chunk_size, len(dataset))))
1089
+ yield chunk
1090
+ ```
1091
+
1092
+ #### 3. Non-continuous Class Labels
1093
+
1094
+ **Problem**: Class labels are not continuous (0, 1, 2, ...)
1095
+
1096
+ **Solution**:
1097
+
1098
+ ```python
1099
+ # Create label mapping
1100
+ unique_labels = sorted(set(dataset['label']))
1101
+ label_to_idx = {label: idx for idx, label in enumerate(unique_labels)}
1102
+
1103
+ # Apply mapping
1104
+ def map_labels(example):
1105
+ example['mapped_label'] = label_to_idx[example['label']]
1106
+ return example
1107
+
1108
+ dataset = dataset.map(map_labels)
1109
+ ```
1110
+
1111
+ #### 4. CUDA/GPU Issues
1112
+
1113
+ **Problem**: CUDA out of memory during training
1114
+
1115
+ **Solution**:
1116
+
1117
+ ```python
1118
+ # Reduce batch size or use CPU
1119
+ device = torch.device('cpu') # Force CPU usage
1120
+
1121
+ # Or use gradient accumulation
1122
+ accumulation_steps = 4
1123
+ for i, (support_data, query_data) in enumerate(dataloader):
1124
+ loss = model(support_data, query_data) / accumulation_steps
1125
+ loss.backward()
1126
+
1127
+ if (i + 1) % accumulation_steps == 0:
1128
+ optimizer.step()
1129
+ optimizer.zero_grad()
1130
+ ```
1131
+
1132
+ ### Performance Tips
1133
+
1134
+ 1. **Use appropriate image sizes**: For few-shot learning, 84x84 or 224x224 are common choices
1135
+ 2. **Enable streaming mode**: For memory-efficient processing of large archives
1136
+ 3. **Use data augmentation**: Improve few-shot performance with transforms
1137
+ 4. **Cache preprocessed data**: Save processed episodes to disk for faster iteration
1138
+
1139
+ ## License
1140
+
1141
+ This dataset is released under the MIT License. See the [LICENSE file](LICENSE) for details.
1142
+
1143
+ ## Data Ethics and Responsible Use
1144
+
1145
+ This dataset is intended for academic research and educational purposes in few-shot learning and computer vision. Users should:
1146
+
1147
+ - **Respect intellectual property**: Images may be subject to copyright; use only for research purposes
1148
+ - **Consider bias**: Be aware that product categories may reflect certain demographic or geographic biases
1149
+ - **Commercial use**: While the license permits it, consider the ethical implications of commercial applications
1150
+ - **Attribution**: Please cite this dataset in any published work
1151
+
1152
+ ## Limitations
1153
+
1154
+ - **Image quality**: Variable image quality and backgrounds may affect model performance
1155
+ - **Class imbalance**: Some classes may have significantly fewer images than others
1156
+ - **Non-continuous labels**: Class numbers are not sequential, which may require label mapping
1157
+ - **Temporal bias**: Product images reflect trends from the time of collection