DISCOVR / upload_to_hf.py
dkillough's picture
Add files using upload-large-folder tool
ef32553 verified
#!/usr/bin/env python3
"""
Upload VR Scene Evaluation Dataset to Hugging Face Hub
This script uploads a YOLO format dataset to Hugging Face Hub as a dataset repository.
For very large datasets, you can also use the command line approach:
1. Install huggingface-hub: pip install huggingface-hub
2. Login: huggingface-cli login
3. Upload: hf upload-large-folder <username>/<repo-name> /path/to/dataset --repo-type=dataset
This Python script provides more control and better error handling.
"""
import os
import yaml
from pathlib import Path
from huggingface_hub import HfApi, login, create_repo
import shutil
import tempfile
# Configuration
DATASET_NAME = "DISCOVR" # Change this to your desired dataset name
HF_USERNAME = None # Will be set after login
DATASET_PATH = "/home/daniel/_datasets/post-2/aggregate"
REPO_TYPE = "dataset"
def load_dataset_config():
"""Load the dataset configuration from data.yaml"""
with open(os.path.join(DATASET_PATH, "data.yaml"), 'r') as f:
config = yaml.safe_load(f)
return config
def create_dataset_card(config):
"""Create a README.md file for the dataset"""
class_names = config['names']
num_classes = config['nc']
readme_content = f"""---
license: cc-by-4.0
task_categories:
- object-detection
language:
- en
tags:
- computer-vision
- object-detection
- yolo
- virtual-reality
- vr
- scene-evaluation
size_categories:
- 1K<n<10K
---
# VR Scene Evaluation Dataset
## Dataset Description
This dataset contains {num_classes} object classes for VR scene evaluation, formatted for YOLO object detection models.
### Classes ({num_classes} total):
{chr(10).join([f"- {i}: {name}" for i, name in enumerate(class_names)])}
## Dataset Structure
```
β”œβ”€β”€ train/
β”‚ β”œβ”€β”€ images/
β”‚ └── labels/
β”œβ”€β”€ valid/
β”‚ β”œβ”€β”€ images/
β”‚ └── labels/
β”œβ”€β”€ test/
β”‚ β”œβ”€β”€ images/
β”‚ └── labels/
└── data.yaml
```
## Usage
### With YOLOv8
```python
from ultralytics import YOLO
# Load a model
model = YOLO('yolov8n.pt')
# Train the model
results = model.train(data='path/to/data.yaml', epochs=100, imgsz=640)
```
### With Hugging Face Datasets
```python
from datasets import load_dataset
dataset = load_dataset("{HF_USERNAME}/{DATASET_NAME}")
```
## License
This dataset is licensed under CC BY 4.0.
## Citation
```
@dataset{{vr_scene_evaluation,
title={{VR Scene Evaluation Dataset}},
year={{2025}},
publisher={{Hugging Face}},
version={{1.0}},
}}
```
## Original Source
This dataset was originally sourced from Roboflow:
- Workspace: my-workspace-zhz1m
- Project: vr-scene-evaluation-o1hbg
- Version: 6
- URL: https://universe.roboflow.com/my-workspace-zhz1m/vr-scene-evaluation-o1hbg/dataset/6
"""
return readme_content
def prepare_upload_directory():
"""Prepare a clean directory for upload"""
upload_dir = tempfile.mkdtemp()
# Copy essential files
files_to_copy = [
"data.yaml",
"README.dataset.txt",
"README.roboflow.txt"
]
for file in files_to_copy:
src = os.path.join(DATASET_PATH, file)
if os.path.exists(src):
shutil.copy2(src, upload_dir)
# Copy train, valid, test directories
for split in ["train", "valid", "test"]:
src_dir = os.path.join(DATASET_PATH, split)
if os.path.exists(src_dir):
dst_dir = os.path.join(upload_dir, split)
shutil.copytree(src_dir, dst_dir)
return upload_dir
def main():
global HF_USERNAME
print("=== Hugging Face Dataset Upload Script ===")
print(f"Dataset path: {DATASET_PATH}")
print(f"Dataset name: {DATASET_NAME}")
# Load dataset config
try:
config = load_dataset_config()
print(f"βœ“ Loaded dataset config: {config['nc']} classes")
except Exception as e:
print(f"βœ— Error loading dataset config: {e}")
return
# Login to Hugging Face
print("\n1. Logging into Hugging Face...")
print("You need a Hugging Face account and access token.")
print("Get your token from: https://huggingface.co/settings/tokens")
try:
login()
api = HfApi()
user_info = api.whoami()
HF_USERNAME = user_info['name']
print(f"βœ“ Logged in as: {HF_USERNAME}")
except Exception as e:
print(f"βœ— Login failed: {e}")
print("Make sure you have a valid token and internet connection.")
return
# Create repository
repo_id = f"{HF_USERNAME}/{DATASET_NAME}"
print(f"\n2. Creating repository: {repo_id}")
try:
create_repo(
repo_id=repo_id,
repo_type=REPO_TYPE,
private=False, # Set to True if you want a private repo
exist_ok=True
)
print("βœ“ Repository created/verified")
except Exception as e:
print(f"βœ— Error creating repository: {e}")
return
# Prepare upload directory
print("\n3. Preparing files for upload...")
try:
upload_dir = prepare_upload_directory()
print(f"βœ“ Files prepared in: {upload_dir}")
# Create README.md
readme_content = create_dataset_card(config)
with open(os.path.join(upload_dir, "README.md"), 'w') as f:
f.write(readme_content)
print("βœ“ Dataset card created")
except Exception as e:
print(f"βœ— Error preparing files: {e}")
return
# Upload to Hugging Face
print(f"\n4. Uploading to {repo_id}...")
print("This may take a while depending on dataset size...")
print("Using upload_large_folder for better handling of large datasets...")
try:
# Use upload_large_folder for better handling of large datasets
api.upload_large_folder(
folder_path=upload_dir,
repo_id=repo_id,
repo_type=REPO_TYPE,
num_workers=4, # Use multiple workers for faster upload
create_pr=False, # Upload directly to main branch
allow_patterns=["**/*"], # Upload all files
ignore_patterns=[".git/**", "**/.DS_Store", "**/__pycache__/**"] # Ignore system files
)
print("βœ“ Upload completed successfully!")
print(f"\nπŸŽ‰ Your dataset is now available at:")
print(f"https://huggingface.co/datasets/{repo_id}")
except Exception as e:
print(f"βœ— Upload failed: {e}")
print("If the upload failed due to size, you can try:")
print("1. Reducing the number of workers (num_workers parameter)")
print("2. Using the command line: hf upload-large-folder")
print("3. Splitting the dataset into smaller chunks")
return
finally:
# Clean up
shutil.rmtree(upload_dir)
print("βœ“ Temporary files cleaned up")
if __name__ == "__main__":
main()