-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Add results2img, format_results for ade dataset #544
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
import os.path as osp | ||
import tempfile | ||
|
||
import mmcv | ||
import numpy as np | ||
from PIL import Image | ||
|
||
from .builder import DATASETS | ||
from .custom import CustomDataset | ||
|
||
|
@@ -82,3 +89,81 @@ def __init__(self, **kwargs): | |
seg_map_suffix='.png', | ||
reduce_zero_label=True, | ||
**kwargs) | ||
|
||
def results2img(self, results, imgfile_prefix, to_label_id): | ||
"""Write the segmentation results to images. | ||
|
||
Args: | ||
results (list[list | tuple | ndarray]): Testing results of the | ||
dataset. | ||
imgfile_prefix (str): The filename prefix of the png files. | ||
If the prefix is "somepath/xxx", | ||
the png files will be named "somepath/xxx.png". | ||
to_label_id (bool): whether convert output to label_id for | ||
submission | ||
|
||
Returns: | ||
list[str: str]: result txt files which contains corresponding | ||
semantic segmentation images. | ||
""" | ||
mmcv.mkdir_or_exist(imgfile_prefix) | ||
result_files = [] | ||
prog_bar = mmcv.ProgressBar(len(self)) | ||
for idx in range(len(self)): | ||
result = results[idx] | ||
|
||
filename = self.img_infos[idx]['filename'] | ||
basename = osp.splitext(osp.basename(filename))[0] | ||
|
||
# save seg_logit | ||
if len(result.shape) == 3: | ||
# print(result.shape) | ||
npy_filename = osp.join(imgfile_prefix, f'{basename}.npy') | ||
np.save(npy_filename, result) | ||
result_files.append(npy_filename) | ||
|
||
# save seg_pred | ||
if len(result.shape) == 2: | ||
# print(result.shape) | ||
png_filename = osp.join(imgfile_prefix, f'{basename}.png') | ||
result = result + 1 | ||
|
||
output = Image.fromarray(result.astype(np.uint8)) | ||
output.save(png_filename) | ||
result_files.append(png_filename) | ||
|
||
prog_bar.update() | ||
|
||
return result_files | ||
|
||
def format_results(self, results, imgfile_prefix=None, to_label_id=True): | ||
"""Format the results into dir (standard format for Cityscapes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. incorrect docstring. |
||
evaluation). | ||
|
||
Args: | ||
results (list): Testing results of the dataset. | ||
imgfile_prefix (str | None): The prefix of images files. It | ||
includes the file path and the prefix of filename, e.g., | ||
"a/b/prefix". If not specified, a temp file will be created. | ||
Default: None. | ||
to_label_id (bool): whether convert output to label_id for | ||
submission. Default: False | ||
|
||
Returns: | ||
tuple: (result_files, tmp_dir), result_files is a list containing | ||
the image paths, tmp_dir is the temporal directory created | ||
for saving json/png files when img_prefix is not specified. | ||
""" | ||
|
||
assert isinstance(results, list), 'results must be a list' | ||
assert len(results) == len(self), ( | ||
'The length of results is not equal to the dataset len: ' | ||
f'{len(results)} != {len(self)}') | ||
|
||
if imgfile_prefix is None: | ||
tmp_dir = tempfile.TemporaryDirectory() | ||
imgfile_prefix = tmp_dir.name | ||
else: | ||
tmp_dir = None | ||
result_files = self.results2img(results, imgfile_prefix, to_label_id) | ||
return result_files, tmp_dir |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may also support output image.