-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add dim check while calculating mIoU&pixAcc for segmentation test #991
Open
chunhanl
wants to merge
4
commits into
dmlc:master
Choose a base branch
from
chunhanl:add_dim_check_for_segmentation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 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
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 |
---|---|---|
|
@@ -7,15 +7,19 @@ | |
__all__ = ['SegmentationMetric', 'batch_pix_accuracy', 'batch_intersection_union', | ||
'pixelAccuracy', 'intersectionAndUnion'] | ||
|
||
|
||
class SegmentationMetric(EvalMetric): | ||
"""Computes pixAcc and mIoU metric scores | ||
""" | ||
|
||
|
||
def __init__(self, nclass): | ||
super(SegmentationMetric, self).__init__('pixAcc & mIoU') | ||
self.nclass = nclass | ||
self.lock = threading.Lock() | ||
self.reset() | ||
|
||
|
||
def update(self, labels, preds): | ||
"""Updates the internal evaluation result. | ||
|
||
|
@@ -27,6 +31,8 @@ def update(self, labels, preds): | |
preds : 'NDArray' or list of `NDArray` | ||
Predicted values. | ||
""" | ||
|
||
|
||
def evaluate_worker(self, label, pred): | ||
correct, labeled = batch_pix_accuracy( | ||
pred, label) | ||
|
@@ -38,18 +44,20 @@ def evaluate_worker(self, label, pred): | |
self.total_inter += inter | ||
self.total_union += union | ||
|
||
|
||
if isinstance(preds, mx.nd.NDArray): | ||
evaluate_worker(self, labels, preds) | ||
elif isinstance(preds, (list, tuple)): | ||
threads = [threading.Thread(target=evaluate_worker, | ||
args=(self, label, pred), | ||
) | ||
) | ||
for (label, pred) in zip(labels, preds)] | ||
for thread in threads: | ||
thread.start() | ||
for thread in threads: | ||
thread.join() | ||
|
||
|
||
def get(self): | ||
"""Gets the current evaluation result. | ||
|
||
|
@@ -63,23 +71,30 @@ def get(self): | |
mIoU = IoU.mean() | ||
return pixAcc, mIoU | ||
|
||
|
||
def reset(self): | ||
"""Resets the internal evaluation result to initial state.""" | ||
self.total_inter = 0 | ||
self.total_union = 0 | ||
self.total_correct = 0 | ||
self.total_label = 0 | ||
|
||
|
||
def batch_pix_accuracy(output, target): | ||
"""PixAcc""" | ||
# inputs are NDarray, output 4D, target 3D | ||
# add new axis if missing batch dimension | ||
# the category -1 is ignored class, typically for background / boundary | ||
if len(output.shape) == 3: | ||
output = output[np.newaxis, :] | ||
if len(target.shape) == 2: | ||
target = target[np.newaxis, :] | ||
predict = np.argmax(output.asnumpy(), 1).astype('int64') + 1 | ||
|
||
target = target.asnumpy().astype('int64') + 1 | ||
|
||
pixel_labeled = np.sum(target > 0) | ||
pixel_correct = np.sum((predict == target)*(target > 0)) | ||
pixel_correct = np.sum((predict == target) * (target > 0)) | ||
|
||
assert pixel_correct <= pixel_labeled, "Correct area should be smaller than Labeled" | ||
return pixel_correct, pixel_labeled | ||
|
@@ -88,7 +103,12 @@ def batch_pix_accuracy(output, target): | |
def batch_intersection_union(output, target, nclass): | ||
"""mIoU""" | ||
# inputs are NDarray, output 4D, target 3D | ||
# add new axis if missing batch dimension | ||
# the category -1 is ignored class, typically for background / boundary | ||
if len(output.shape) == 3: | ||
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.
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. OK I'll try to modify from the dataset |
||
output = output[np.newaxis, :] | ||
if len(target.shape) == 2: | ||
target = target[np.newaxis, :] | ||
mini = 1 | ||
maxi = nclass | ||
nbins = nclass | ||
|
@@ -119,7 +139,7 @@ def pixelAccuracy(imPred, imLab): | |
# Remove classes from unlabeled pixels in gt image. | ||
# We should not penalize detections in unlabeled portions of the image. | ||
pixel_labeled = np.sum(imLab > 0) | ||
pixel_correct = np.sum((imPred == imLab)*(imLab > 0)) | ||
pixel_correct = np.sum((imPred == imLab) * (imLab > 0)) | ||
pixel_accuracy = 1.0 * pixel_correct / pixel_labeled | ||
return (pixel_accuracy, pixel_correct, pixel_labeled) | ||
|
||
|
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.
why make this change?
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.
Hello,
According in test.py
gluon-cv/scripts/segmentation/test.py
Line 20 in a78965f
The args defined in parse_args() no longer has args 'model-zoo'
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.
Please add that back instead. Not sure who removed that.
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.
I'm not sure whether this may have conflicts with these settings.
gluon-cv/scripts/segmentation/test.py
Lines 204 to 230 in a78965f