请求加入做种队列,或现在用 pf download zhiqing0205/u2net-mvtec-loco-segmentation 获取(CLI 指南)。
This tool uses U²-Net for generating binary foreground masks from the MVTec LOCO anomaly detection dataset.
The mvtec_loco_fg_segmentation.py script processes the entire MVTec LOCO dataset and generates binary foreground masks for all images. It uses the U²-Net model to perform salient object detection and converts the probability maps to binary masks.
# Create conda environment
conda create -n u2net python=3.8 -y
conda activate u2net
# Install dependencies
pip install torch==1.13.1+cu116 torchvision==0.14.1+cu116 --index-url https://download.pytorch.org/whl/cu116
pip install opencv-python scikit-image matplotlib numpy pillow
Option 1: Automatic Download (Recommended)
# Install HuggingFace Hub
pip install huggingface_hub
# The model will be automatically downloaded when you run the script
python mvtec_loco_fg_segmentation.py
Option 2: Manual Download
u2net.pth (176.3 MB) from GoogleDrive./saved_models/u2net/u2net.pthOption 3: Download from HuggingFace
# Download only the model
python download_from_hf.py --model-only
# Or download the complete repository
python download_from_hf.py --complete-repo
Ensure your MVTec LOCO dataset follows this structure:
mvtec_loco_anomaly_detection/
├── breakfast_box/
│ ├── test/
│ │ ├── good/
│ │ ├── logical_anomalies/
│ │ └── structural_anomalies/
│ └── train/
│ └── good/
├── screw_bag/
│ ├── test/
│ └── train/
└── ... (other categories)
# Process entire dataset with default settings
python mvtec_loco_fg_segmentation.py
# Show help
python mvtec_loco_fg_segmentation.py -h
# Specify custom dataset and model paths
python mvtec_loco_fg_segmentation.py \
--dataset_path /path/to/mvtec_loco \
--model_path /path/to/u2net.pth
# Process specific categories only
python mvtec_loco_fg_segmentation.py \
--categories breakfast_box juice_bottle
# Process only test split
python mvtec_loco_fg_segmentation.py \
--splits test
# Use different threshold for binary mask generation
python mvtec_loco_fg_segmentation.py \
--threshold 0.3
# Custom output directory name
python mvtec_loco_fg_segmentation.py \
--output_dir custom_masks
# Optimize processing with multiple workers
python mvtec_loco_fg_segmentation.py \
--num_workers 4 \
--batch_size 4
| Argument | Type | Default | Description |
|---|---|---|---|
--dataset_path |
str | /root/hy-data/datasets/mvtec_loco_anomaly_detection |
Path to MVTec LOCO dataset root |
--model_path |
str | ./saved_models/u2net/u2net.pth |
Path to U2NET model weights |
--output_dir |
str | fg_mask |
Output directory name for masks |
--threshold |
float | 0.5 |
Threshold for binary mask generation |
--categories |
list | all 5 categories |
Categories to process |
--splits |
list | ['test', 'train'] |
Dataset splits to process |
--batch_size |
int | 1 |
Batch size for processing |
--num_workers |
int | 1 |
Number of data loading workers |
The script generates masks in the following structure:
mvtec_loco_anomaly_detection/
├── fg_mask/ # Generated masks directory
│ ├── breakfast_box/
│ │ ├── test/
│ │ │ ├── good/
│ │ │ │ ├── 000.png # Binary mask (0/255 values)
│ │ │ │ ├── 001.png
│ │ │ │ └── ...
│ │ │ ├── logical_anomalies/
│ │ │ └── structural_anomalies/
│ │ └── train/
│ │ └── good/
│ └── ... (other categories)
└── ... (original dataset)
ERROR: Dataset path not found: Check dataset path and extractionERROR: Model path not found: Download and place u2net.pth correctlyERROR: Invalid categories: Use valid category namesThe script provides detailed progress information:
Configuration:
Dataset path: /root/hy-data/datasets/mvtec_loco_anomaly_detection
Model path: ./saved_models/u2net/u2net.pth
Output directory: fg_mask
Binary threshold: 0.5
Categories: ['breakfast_box', 'screw_bag', 'juice_bottle', 'splicing_connectors', 'pushpins']
Splits: ['test', 'train']
...load U2NET---
Processing category: breakfast_box
Processing breakfast_box/test/good
Found 102 images
Processing 1/102: 000.png
Processing 20/102: 019.png
...
If you use this tool in your research, please cite the original U²-Net paper:
@InProceedings{Qin_2020_PR,
title = {U2-Net: Going Deeper with Nested U-Structure for Salient Object Detection},
author = {Qin, Xuebin and Zhang, Zichen and Huang, Chenyang and Dehghan, Masood and Zaiane, Osmar and Jagersand, Martin},
journal = {Pattern Recognition},
volume = {106},
pages = {107404},
year = {2020}
}
This tool extends the original U²-Net implementation. Please refer to the original repository for license information.
有文件?提交种子。
发布种子saved_models/u2net_portrait/u2net_portrait.pth 168 MiBsaved_models/u2net/u2net.pth 168 MiB