STDC/mmseg/datasets/golf8_dataset.py
charlie880624 7716a0060f
Some checks failed
deploy / build-n-publish (push) Has been cancelled
lint / lint (push) Has been cancelled
build / build_cpu (3.7, 1.5.1, torch1.5, 0.6.1) (push) Has been cancelled
build / build_cpu (3.7, 1.6.0, torch1.6, 0.7.0) (push) Has been cancelled
build / build_cpu (3.7, 1.7.0, torch1.7, 0.8.1) (push) Has been cancelled
build / build_cpu (3.7, 1.8.0, torch1.8, 0.9.0) (push) Has been cancelled
build / build_cpu (3.7, 1.9.0, torch1.9, 0.10.0) (push) Has been cancelled
build / build_cuda101 (3.7, 1.5.1+cu101, torch1.5, 0.6.1+cu101) (push) Has been cancelled
build / build_cuda101 (3.7, 1.6.0+cu101, torch1.6, 0.7.0+cu101) (push) Has been cancelled
build / build_cuda101 (3.7, 1.7.0+cu101, torch1.7, 0.8.1+cu101) (push) Has been cancelled
build / build_cuda101 (3.7, 1.8.0+cu101, torch1.8, 0.9.0+cu101) (push) Has been cancelled
build / build_cuda102 (3.6, 1.9.0+cu102, torch1.9, 0.10.0+cu102) (push) Has been cancelled
build / build_cuda102 (3.7, 1.9.0+cu102, torch1.9, 0.10.0+cu102) (push) Has been cancelled
build / build_cuda102 (3.8, 1.9.0+cu102, torch1.9, 0.10.0+cu102) (push) Has been cancelled
build / build_cuda102 (3.9, 1.9.0+cu102, torch1.9, 0.10.0+cu102) (push) Has been cancelled
build / test_windows (windows-2022, cpu, 3.8) (push) Has been cancelled
build / test_windows (windows-2022, cu111, 3.8) (push) Has been cancelled
feat: add golf dataset, kneron configs, and tools
- Add golf1/2/4/7/8 dataset classes for semantic segmentation
- Add kneron-specific configs (meconfig series, kn_stdc1_golf4class)
- Organize scripts into tools/check/ and tools/kneron/
- Add kneron_preprocessing module
- Update README with quick-start guide
- Update .gitignore to exclude data dirs, onnx, nef outputs

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-18 13:14:30 +08:00

93 lines
3.3 KiB
Python

# Copyright (c) OpenMMLab. All rights reserved.
import os.path as osp
import mmcv
import numpy as np
from mmcv.utils import print_log
from PIL import Image
from .builder import DATASETS
from .custom import CustomDataset
@DATASETS.register_module()
class Golf8Dataset(CustomDataset):
"""Golf8Dataset for semantic segmentation with 8 valid classes (ignore background)."""
CLASSES = (
'bunker', 'car', 'grass',
'greenery', 'person', 'pond',
'road', 'tree'
)
PALETTE = [
[128, 0, 0], # bunker - dark red
[0, 0, 128], # car - dark blue
[0, 128, 0], # grass - green
[0, 255, 0], # greenery - light green
[255, 0, 0], # person - red
[0, 255, 255], # pond - cyan
[255, 165, 0], # road - orange
[0, 128, 128], # tree - dark cyan
]
def __init__(self,
img_suffix='_leftImg8bit.png',
seg_map_suffix='_gtFine_labelIds.png',
**kwargs):
super(Golf8Dataset, self).__init__(
img_suffix=img_suffix,
seg_map_suffix=seg_map_suffix,
**kwargs)
print("✅ [Golf8Dataset] 初始化完成")
print(f" ➤ CLASSES: {self.CLASSES}")
print(f" ➤ PALETTE: {self.PALETTE}")
print(f" ➤ img_suffix: {img_suffix}")
print(f" ➤ seg_map_suffix: {seg_map_suffix}")
print(f" ➤ img_dir: {self.img_dir}")
print(f" ➤ ann_dir: {self.ann_dir}")
print(f" ➤ dataset length: {len(self)}")
def results2img(self, results, imgfile_prefix, indices=None):
"""Write the segmentation results to images."""
if indices is None:
indices = list(range(len(self)))
mmcv.mkdir_or_exist(imgfile_prefix)
result_files = []
for result, idx in zip(results, indices):
filename = self.img_infos[idx]['filename']
basename = osp.splitext(osp.basename(filename))[0]
png_filename = osp.join(imgfile_prefix, f'{basename}.png')
output = Image.fromarray(result.astype(np.uint8)).convert('P')
palette = np.zeros((len(self.PALETTE), 3), dtype=np.uint8)
for label_id, color in enumerate(self.PALETTE):
palette[label_id] = color
output.putpalette(palette)
output.save(png_filename)
result_files.append(png_filename)
return result_files
def format_results(self, results, imgfile_prefix, indices=None):
"""Format the results into dir (for evaluation or visualization)."""
return self.results2img(results, imgfile_prefix, indices)
def evaluate(self,
results,
metric='mIoU',
logger=None,
imgfile_prefix=None):
"""Evaluate the results with the given metric."""
print("🧪 [Golf8Dataset.evaluate] 被呼叫")
print(f" ➤ 當前 CLASSES: {self.CLASSES}")
print(f" ➤ 評估 metric: {metric}")
print(f" ➤ 結果數量: {len(results)}")
metrics = metric if isinstance(metric, list) else [metric]
eval_results = super(Golf8Dataset, self).evaluate(results, metrics, logger)
print(f" ➤ 返回評估指標: {list(eval_results.keys())}")
return eval_results