Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Add turbo color map #1055

Merged
merged 19 commits into from
Jul 14, 2020
Merged

Add turbo color map #1055

merged 19 commits into from
Jul 14, 2020

Conversation

kalpitthakkar-lm
Copy link
Contributor

@kalpitthakkar-lm kalpitthakkar-lm commented May 26, 2020

What this patch does to fix the issue.

This PR contains replacement of the current Jet colormap of matplotlib by adding the Google proposed Turbo Colormap as highlighted in #485 by @tk26eng in blueoil/common.py.

This colormap is for replacing the Jet colormap in matplotlib that is used for coloring heatmaps. So I added a new function that takes the heatmap as input and then returns the colored heatmap. As the function uses numpy, it should be called using tf.py_func interface.

The table for reference colors is a (256x3) matrix of sRGB colors and I saved it to a separate file blueoil/turbo_color_map.py.

Link to any relevant issues or pull requests.

Relevant issue: #485

@blueoil-butler blueoil-butler bot added the CI: auto-run Run CI automatically label May 26, 2020
@bo-code-review-bot
Copy link

This PR needs Approvals as follows.

  • Ownership Approval for / from iizukak, tkng, ruimashita
  • Readability Approval for Python from tkng, tsawada, tfujiwar

Please choose reviewers and requet reviews!

Click to see how to approve each reviews

You can approve this PR by triggered comments as follows.

  • Approve all reviews requested to you (readability and ownership) and LGTM review
    Approval, LGTM

  • Approve all ownership reviews
    Ownership Approval or OA

  • Approve all readability reviews
    Readability Approval or RA

  • Approve specified review targets

    • Example of Ownership Reviewer of /: Ownership Approval for / or OA for /
    • Example of Readability Reviewer of Python: Readability Approval for Python or RA for Python
  • Approve LGTM review
    LGTM

See all trigger comments

Please replace [Target] to review target

  • Ownership Approval
    • Ownership Approval for [Target]
    • OA for [Target]
    • Ownership Approval
    • OA
    • Approval
  • Readability Approval
    • Readability Approval for [Target]
    • RA for [Target]
    • [Target] Readability Approval
    • [Target] RA
    • Readability Approval
    • RA
    • Approval
  • LGTM
    • LGTM
    • lgtm

Copy link
Member

@iizukak iizukak left a comment

Choose a reason for hiding this comment

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

@kalpitthakkar-lm Thanks. turbo colormap looks nice.

# For replacing the Matplotlib Jet colormap, we use the Turbo color map
# https://ai.googleblog.com/2019/08/turbo-improved-rainbow-colormap-for.html
# The colormap allows for a large number of quantization levels:
# https://github.com/blue-oil/blueoil/tree/master/docs/_static/turbo_cmap.png
Copy link
Member

Choose a reason for hiding this comment

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

Missing link. Do you have plan to add image to the documentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iizukak Yes, I will add that image and upload to the commit.

@@ -0,0 +1,258 @@
TURBO_CMAP_DATA = [
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I get it. That will make sure the tests will pass (the ones that have failed, because the symlinks are missing there).

Thank you!!

@kalpitthakkar-lm kalpitthakkar-lm requested a review from iizukak May 29, 2020 07:05
Copy link
Member

@iizukak iizukak left a comment

Choose a reason for hiding this comment

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

@kalpitthakkar-lm
Sorry, Can you add license header to the new file?

like

# -*- coding: utf-8 -*-
# Copyright 2018 The Blueoil Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================

@kalpitthakkar-lm kalpitthakkar-lm requested a review from iizukak June 1, 2020 03:24
Copy link
Member

@iizukak iizukak left a comment

Choose a reason for hiding this comment

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

OA

@kalpitthakkar-lm
Copy link
Contributor Author

@iizukak The tests failed after I updated the branch by pulling from master. What should we do?
Are there other reviews required for merge?

@iizukak
Copy link
Member

iizukak commented Jun 4, 2020

@kalpitthakkar-lm
CI sometimes unstable. Please wait for this trial.
If this trial also failed, We should contact to the infra team.

@kalpitthakkar-lm
Copy link
Contributor Author

@tk26eng @iizukak Can someone now push this to master?

@iizukak
Copy link
Member

iizukak commented Jun 9, 2020

@kalpitthakkar-lm This PR should get Python Readability Approval

@iizukak iizukak requested a review from a-hanamoto June 9, 2020 00:12
# The colormap allows for a large number of quantization levels:
# https://github.com/blue-oil/blueoil/tree/master/docs/_static/turbo_cmap.png
# Implementation inspired from the following gist:
# https://gist.github.com/mikhailov-work/ee72ba4191942acecc03fe6da94fc73f

Choose a reason for hiding this comment

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

Original codes are Apache 2.0 licensed.
To use Apache 2.0 licensed codes, you need to do the followings:
(Please see http://www.apache.org/licenses/LICENSE-2.0 4. Redistribution for further information)

  1. You need to put a copy of the License somewhere
  2. You need to clarify what you changed from the original
  3. You need to leave the original copyright
  4. You need to leave NOTICE files if any
    I think there are some rules (e.g. where you should put the License files) in Blueoil, so please ask BO team that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iizukak Where should I put the license file / clarify what I have changed from the original?

# limitations under the License.
# =============================================================================

TURBO_CMAP_DATA = [

Choose a reason for hiding this comment

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

Same as above.

Comment on lines 71 to 87
def interpolate(colormap, x):
x = max(0.0, min(1.0, x))
a = int(x*255.0)
b = min(255, a + 1)
f = x*255.0 - a
return [colormap[a][0] + (colormap[b][0] - colormap[a][0]) * f,
colormap[a][1] + (colormap[b][1] - colormap[a][1]) * f,
colormap[a][2] + (colormap[b][2] - colormap[a][2]) * f]


def interpolate_or_clip(colormap, x):
if x < 0.0:
return [0.0, 0.0, 0.0]
elif x > 1.0:
return [1.0, 1.0, 1.0]
else:
return interpolate(colormap, x)

Choose a reason for hiding this comment

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

Will you need these methods?
I think only color_map_apply is sufficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed them.

Comment on lines 105 to 106
image_colored[image < 0.0] = 0.0
image_colored[image > 1.0] = 1.0

Choose a reason for hiding this comment

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

I think this won't happen since we clipped values before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, I fixed it.

Comment on lines 93 to 95
# If image range is [0, 1], continue ahead. Else convert to that range.
if image.max() > 1.0:
image = (image - image.min()) / image.max()

Choose a reason for hiding this comment

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

There is inconsistency between the comment and its code.
(The code does not check the min value)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't need to change the range of values.
I've updated the code.

if image.max() > 1.0:
image = (image - image.min()) / image.max()

x = image.clip(0.0, 1.0)

Choose a reason for hiding this comment

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

If the comment is true, then you don't need this code.

return interpolate(colormap, x)


def color_map_apply(image):

Choose a reason for hiding this comment

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

I think apply_color_map is more straightforward.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made the change.

x = image.clip(0.0, 1.0)
a = (x * 255).astype(int)
b = (a + 1).clip(max=255)
f = x * 255.0 - a

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wouldn't it be easier to understand this?

Choose a reason for hiding this comment

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

Although it is not a major issue, it seems redundant to calculate x * 255.0 twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, let me update it to np.modf (I will add a comment on how it works above it though..)

Choose a reason for hiding this comment

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

Thank you!

Copy link

@a-hanamoto a-hanamoto left a comment

Choose a reason for hiding this comment

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

Commented

Copy link

@a-hanamoto a-hanamoto left a comment

Choose a reason for hiding this comment

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

Please let me review after fixing license issues. Thank you!

@iizukak
Copy link
Member

iizukak commented Jul 1, 2020

Sorry, I'm now checking the status

Copy link

@a-hanamoto a-hanamoto left a comment

Choose a reason for hiding this comment

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

  • Please put a copy of the License somewhere
  • Please clarify what you changed from the original

@kalpitthakkar-lm
Copy link
Contributor Author

  • Please put a copy of the License somewhere

They have not provided their LICENSE copy, but only the License Identifier (SPDX line) and a copyright line. I have included both in the colormap table file. Apache-2.0 LICENSE terms are available everywhere.

  • Please clarify what you changed from the original

I did not change anything in the colormap table. That's the only thing I have taken from the gist. The function apply_color_map is written by myself.

Copy link

@a-hanamoto a-hanamoto left a comment

Choose a reason for hiding this comment

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

They have not provided their LICENSE copy, but only the License Identifier (SPDX line) and a copyright line. I have included both in the colormap table file. Apache-2.0 LICENSE terms are available everywhere.

Got it.
@iizukak Could you confirm whether it is OK or not?

I did not change anything in the colormap table. That's the only thing I have taken from the gist. The function apply_color_map is written by myself.

It seems you changed its format and name.

@@ -0,0 +1,279 @@
# -*- coding: utf-8 -*-
# Copyright 2018 The Blueoil Authors. All Rights Reserved.

Choose a reason for hiding this comment

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

Suggested change
# Copyright 2018 The Blueoil Authors. All Rights Reserved.
# Copyright 2020 The Blueoil Authors. All Rights Reserved.

@a-hanamoto a-hanamoto requested a review from iizukak July 3, 2020 11:07
Copy link
Member

@iizukak iizukak left a comment

Choose a reason for hiding this comment

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

Sorry for taking long time!
Ownership Approval

Copy link

@a-hanamoto a-hanamoto left a comment

Choose a reason for hiding this comment

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

RA (reverse-shadowing)

@a-hanamoto a-hanamoto requested a review from tsawada July 9, 2020 04:16
@iizukak
Copy link
Member

iizukak commented Jul 9, 2020

/ready

@bo-mergebot
Copy link
Contributor

bo-mergebot bot commented Jul 9, 2020

⏳Merge job is queued...

@bo-mergebot
Copy link
Contributor

bo-mergebot bot commented Jul 9, 2020

😥This PR is not approved yet.

@kalpitthakkar-lm
Copy link
Contributor Author

@tsawada Can you have a look?

Copy link
Contributor

@tsawada tsawada left a comment

Choose a reason for hiding this comment

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

Readability wise, this PR is okay.

Is this going to allow us to drop matplotlib dependency? If not, simply switching to inferno colormap would be easier.

Comment on lines +17 to +20
# Colormap table taken from the following gist:
# https://gist.github.com/mikhailov-work/ee72ba4191942acecc03fe6da94fc73f
# Copyright 2019 Google LLC.
# SPDX-License-Identifier: Apache-2.0
Copy link
Contributor

Choose a reason for hiding this comment

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

so what's the license of this file? I feel it's weird having two license claims on this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I will remove the Blueoil LICENSE header, as the table in the file is from the gist so I'll put these four lines at the top.

Comment on lines 91 to 96
# >>> print(i.shape, f.shape)
# >>> (6,) (6,)
# >>> print(i)
# >>> [ 1. 2. 4. 20. 6. 8.]
# >>> print(f)
# >>> [0.2 0.3 0.5 0.45 0.75 0.88]
Copy link
Contributor

Choose a reason for hiding this comment

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

some lines wouldn't have >>> on the interpreter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. Will update that.

@kalpitthakkar-lm
Copy link
Contributor Author

@tsawada Yes this will allow us to drop matplotlib dependency (it's used for Jet colormap used in heatmaps).

@kalpitthakkar-lm kalpitthakkar-lm requested a review from tsawada July 13, 2020 04:08
Copy link
Contributor

@tsawada tsawada left a comment

Choose a reason for hiding this comment

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

Readability Approval

@kalpitthakkar-lm
Copy link
Contributor Author

@iizukak Please give the command to merge this PR once the status checks are completed. Thank you!

@iizukak
Copy link
Member

iizukak commented Jul 14, 2020

/ready

@bo-mergebot
Copy link
Contributor

bo-mergebot bot commented Jul 14, 2020

⏳Merge job is queued...

@bo-mergebot bo-mergebot bot merged commit 08bf6c1 into blue-oil:master Jul 14, 2020
@kalpitthakkar-lm kalpitthakkar-lm deleted the color_map branch July 14, 2020 04:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CI: auto-run Run CI automatically
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants