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

[SPARK-23205][ML] Update ImageSchema.readImages to correctly set alpha values for four-channel images #20389

Closed
wants to merge 3 commits into from

Conversation

smurching
Copy link
Contributor

What changes were proposed in this pull request?

When parsing raw image data in ImageSchema.decode(), we use a java.awt.Color constructor that sets alpha = 255, even for four-channel images (which may have different alpha values). This PR fixes this issue & adds a unit test to verify correctness of reading four-channel images.

How was this patch tested?

Updates an existing unit test ("readImages pixel values test" in ImageSchemaSuite) to also verify correctness when reading a four-channel image.

Copy link
Member

@dongjoon-hyun dongjoon-hyun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, LGTM.

@SparkQA
Copy link

SparkQA commented Jan 24, 2018

Test build #86607 has finished for PR 20389 at commit 054c1dd.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Jan 25, 2018

Test build #86609 has finished for PR 20389 at commit 4ec463e.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@smurching
Copy link
Contributor Author

Thanks for the reviews @srowen, @dongjoon-hyun! Would it make sense to merge this before Spark 2.3 is released & if so would one of you be able to do so?

@srowen
Copy link
Member

srowen commented Jan 25, 2018

It's minor, though that cuts two ways - low risk to merge, but not critical. I think that's a moderately important issue for this module and a clean fix so I'd put it in branch 2.3

@@ -169,8 +169,7 @@ object ImageSchema {
var offset = 0
for (h <- 0 until height) {
for (w <- 0 until width) {
val color = new Color(img.getRGB(w, h))

val color = new Color(img.getRGB(w, h), nChannels == 4)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't directly use hasAlpha?

@@ -169,8 +169,7 @@ object ImageSchema {
var offset = 0
for (h <- 0 until height) {
for (w <- 0 until width) {
val color = new Color(img.getRGB(w, h))

val color = new Color(img.getRGB(w, h), nChannels == 4)
decoded(offset) = color.getBlue.toByte
decoded(offset + 1) = color.getGreen.toByte
decoded(offset + 2) = color.getRed.toByte
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At line 177,

if (nChannels == 4) {
  decoded(offset + 3) = color.getAlpha.toByte
}

We can directly use hasAlpha too, instead of indirectly comparing nChannels.

Copy link
Member

@viirya viirya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Minor comments.

@SparkQA
Copy link

SparkQA commented Jan 25, 2018

Test build #86645 has finished for PR 20389 at commit 8f1286e.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

asfgit pushed a commit that referenced this pull request Jan 26, 2018
…a values for four-channel images

## What changes were proposed in this pull request?

When parsing raw image data in ImageSchema.decode(), we use a [java.awt.Color](https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#Color(int)) constructor that sets alpha = 255, even for four-channel images (which may have different alpha values). This PR fixes this issue & adds a unit test to verify correctness of reading four-channel images.

## How was this patch tested?

Updates an existing unit test ("readImages pixel values test" in `ImageSchemaSuite`) to also verify correctness when reading a four-channel image.

Author: Sid Murching <sid.murching@databricks.com>

Closes #20389 from smurching/image-schema-bugfix.

(cherry picked from commit 7bd46d9)
Signed-off-by: Sean Owen <sowen@cloudera.com>
@srowen
Copy link
Member

srowen commented Jan 26, 2018

Merged to master/2.3

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

Successfully merging this pull request may close these issues.

5 participants