Skip to content
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

Fix the bug of overflow in AutoContrast #643

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

SCZwangxiao
Copy link

Bug description

Fix the bug of data overflow in AutoContrast function when applying Randaugment. See this issue.

Commit explanation

The root cause is the overflow caused by np.uint8. Specifically, in L40 of the following code:

def autocontrast_func(img, cutoff=0):
"""
same output as PIL.ImageOps.autocontrast
"""
n_bins = 256
def tune_channel(ch):
n = ch.size
cut = cutoff * n // 100
if cut == 0:
high, low = ch.max(), ch.min()
else:
hist = cv2.calcHist([ch], [0], None, [n_bins], [0, n_bins])
low = np.argwhere(np.cumsum(hist) > cut)
low = 0 if low.shape[0] == 0 else low[0]
high = np.argwhere(np.cumsum(hist[::-1]) > cut)
high = n_bins - 1 if high.shape[0] == 0 else n_bins - 1 - high[0]
if high <= low:
table = np.arange(n_bins)
else:
scale = (n_bins - 1) / (high - low)
offset = -low * scale
table = np.arange(n_bins) * scale + offset
table[table < 0] = 0
table[table > n_bins - 1] = n_bins - 1
table = table.clip(0, 255).astype(np.uint8)
return table[ch]
channels = [tune_channel(ch) for ch in cv2.split(img)]
out = cv2.merge(channels)
return out

where ch is one channel of an image (thus type np.uint8), and low is of type np.uint8. Therefore offset = -low * scale will cause an overflow since it cannot represent a negative number.

The reason why this bug did not affect much on the performance is: low is 0 most of the time.

Copy link

Thanks for the contribution! Before we can merge this, we need @SCZwangxiao to sign the Salesforce Inc. Contributor License Agreement.

@SCZwangxiao
Copy link
Author

salesforce-cla

Signed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant