-
Notifications
You must be signed in to change notification settings - Fork 14
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
CNN & NN regressor #16
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
427b715
Testing CNN regressor
li-li-github d00e83d
CNN regressor 1st workable version.
li-li-github f7758e0
Merge pull request #1 from li-li-github/cnn_reg
li-li-github 2ce3500
Merge pull request #2 from li-li-github/master
li-li-github 7bf1afb
Demo of using deterministic feature extraction.
li-li-github 02a5735
Regressor using neural networks.
li-li-github cecc1b2
Output figure title corrected.
li-li-github 281f719
number of neurons has reduced.
li-li-github e52eb4e
cnnreg_test.py
li-li-github 87f8fa3
src/pitl/regression/nn_old.py
li-li-github 7082c19
Removed unnecessary comments and spaces.
li-li-github 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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import os | ||
import time | ||
|
||
import numpy as np | ||
from matplotlib import pyplot as plt | ||
from skimage.data import camera | ||
from skimage.exposure import rescale_intensity | ||
from skimage.measure import compare_psnr as psnr | ||
from skimage.measure import compare_ssim as ssim | ||
from skimage.util import random_noise | ||
|
||
from src.pitl.features.multiscale_convolutions import MultiscaleConvolutionalFeatures | ||
from src.pitl.pitl_classic import ImageTranslator | ||
from src.pitl.regression.nn import CNNRegressor, Modeltype | ||
|
||
""" | ||
Demo for self-supervised denoising using camera image with synthetic noise | ||
""" | ||
|
||
|
||
def demo_pitl_2D(noisy): | ||
|
||
|
||
scales = [1, 3, 7, 15, 31, 63, 127] | ||
widths = [3, 3, 3, 3, 3, 3, 3] | ||
|
||
start_time = time.time() | ||
|
||
regressor = CNNRegressor(mode=Modeltype.Convolutional, | ||
learning_rate=0.001, | ||
early_stopping_rounds=5) | ||
|
||
|
||
generator = MultiscaleConvolutionalFeatures(kernel_widths=widths, | ||
kernel_scales=scales, | ||
kernel_shapes=['l1'] * len(scales), | ||
exclude_center=True, | ||
) | ||
|
||
it = ImageTranslator(feature_generator=generator, regressor=regressor) | ||
|
||
denoised = it.train(noisy, noisy) | ||
|
||
results = [[psnr(noisy, image), ssim(noisy, image)]] | ||
results.append([psnr(denoised, image), ssim(denoised, image)]) | ||
results.append(time.time() - start_time) | ||
|
||
print("noisy ", results[0]) | ||
print("denoised ", results[1]) | ||
print("time elapsed: ", results[2]) | ||
|
||
# from napari import ViewerApp | ||
# with app_context(): | ||
# viewer = ViewerApp() | ||
# viewer.add_image(rescale_intensity(image, in_range='image', out_range=(0, 1)), name='image') | ||
# viewer.add_image(rescale_intensity(noisy, in_range='image', out_range=(0, 1)), name='noisy') | ||
|
||
# viewer.add_image(rescale_intensity(denoised, in_range='image', out_range=(0, 1)), name='denoised%d' % param) | ||
# viewer.add_image(rescale_intensity(denoised_predict, in_range='image', out_range=(0, 1)), name='denoised_predict%d' % param) | ||
|
||
return denoised, results | ||
|
||
|
||
image = camera().astype(np.float32) # [:,50:450] | ||
image = rescale_intensity(image, in_range='image', out_range=(0, 1)) | ||
|
||
intensity = 5 | ||
np.random.seed(0) | ||
noisy = np.random.poisson(image * intensity) / intensity | ||
noisy = random_noise(noisy, mode='gaussian', var=0.01, seed=0) | ||
noisy = noisy.astype(np.float32) | ||
|
||
denoised_cnn, results_cnn = demo_pitl_2D(noisy) | ||
|
||
savepath = '/Users/hirofumi.kobayashi/Github_repositories/pitl/output_data' | ||
plt.figure() | ||
plt.subplot(221) | ||
plt.imshow(image, cmap='gray') | ||
plt.axis('off') | ||
plt.title('Original') | ||
plt.subplot(222) | ||
plt.imshow(noisy, cmap='gray') | ||
plt.axis('off') | ||
plt.title('Noisy \nPSNR={:.2f}, SSMI={:.2f}'.format(results_cnn[0][0], results_cnn[0][1])) | ||
# plt.subplot(223) | ||
# plt.imshow(denoised_lgbm, cmap='gray') | ||
# plt.axis('off') | ||
# plt.title('LGBM {:.2f}sec \nPSNR={:.2f}, SSMI={:.2f}'.format(results_lgbm[2], results_lgbm[1][0], results_lgbm[1][1])) | ||
plt.subplot(224) | ||
plt.imshow(denoised_cnn, cmap='gray') | ||
plt.axis('off') | ||
plt.title('CNN {:.2f}sec \nPSNR={:.2f}, SSMI={:.2f}'.format(results_cnn[2], results_cnn[1][0], results_cnn[1][1])) | ||
plt.subplots_adjust(left=0.11, right=0.9, top=0.91, bottom=0.02, hspace=0.25, wspace=0.2) | ||
plt.savefig(os.path.join(savepath, 'CNN_2D.png'), dpi=300) | ||
plt.show() |
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import os | ||
import time | ||
|
||
import numpy as np | ||
from matplotlib import pyplot as plt | ||
from skimage.data import camera | ||
from skimage.exposure import rescale_intensity | ||
from skimage.measure import compare_psnr as psnr | ||
from skimage.measure import compare_ssim as ssim | ||
from skimage.util import random_noise | ||
|
||
from src.pitl.features.multiscale_convolutions import MultiscaleConvolutionalFeatures | ||
from src.pitl.pitl_classic import ImageTranslator | ||
from src.pitl.regression.gbm import GBMRegressor | ||
|
||
""" | ||
Demo for self-supervised denoising using camera image with synthetic noise | ||
""" | ||
|
||
|
||
def demo_pitl_2D(noisy): | ||
|
||
|
||
scales = [1, 3, 7, 15, 31, 63, 127] | ||
widths = [3, 3, 3, 3, 3, 3, 3] | ||
|
||
start_time = time.time() | ||
|
||
regressor = GBMRegressor(learning_rate=0.001, | ||
early_stopping_rounds=5) | ||
|
||
|
||
generator = MultiscaleConvolutionalFeatures(kernel_widths=widths, | ||
kernel_scales=scales, | ||
kernel_shapes=['l1'] * len(scales), | ||
exclude_center=True, | ||
) | ||
|
||
it = ImageTranslator(feature_generator=generator, regressor=regressor) | ||
|
||
denoised = it.train(noisy, noisy) | ||
|
||
results = [[psnr(noisy, image), ssim(noisy, image)]] | ||
results.append([psnr(denoised, image), ssim(denoised, image)]) | ||
results.append(time.time() - start_time) | ||
|
||
print("noisy ", results[0]) | ||
print("denoised ", results[1]) | ||
print("time elapsed: ", results[2]) | ||
|
||
# from napari import ViewerApp | ||
# with app_context(): | ||
# viewer = ViewerApp() | ||
# viewer.add_image(rescale_intensity(image, in_range='image', out_range=(0, 1)), name='image') | ||
# viewer.add_image(rescale_intensity(noisy, in_range='image', out_range=(0, 1)), name='noisy') | ||
|
||
# viewer.add_image(rescale_intensity(denoised, in_range='image', out_range=(0, 1)), name='denoised%d' % param) | ||
# viewer.add_image(rescale_intensity(denoised_predict, in_range='image', out_range=(0, 1)), name='denoised_predict%d' % param) | ||
|
||
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. here again commented out lines |
||
return denoised, results | ||
|
||
|
||
image = camera().astype(np.float32) # [:,50:450] | ||
image = rescale_intensity(image, in_range='image', out_range=(0, 1)) | ||
|
||
intensity = 5 | ||
np.random.seed(0) | ||
noisy = np.random.poisson(image * intensity) / intensity | ||
noisy = random_noise(noisy, mode='gaussian', var=0.01, seed=0) | ||
noisy = noisy.astype(np.float32) | ||
|
||
denoised_lgbm, results_lgbm = demo_pitl_2D(noisy) | ||
|
||
savepath = '/Users/hirofumi.kobayashi/Github_repositories/pitl/output_data' | ||
plt.figure() | ||
plt.subplot(221) | ||
plt.imshow(image, cmap='gray') | ||
plt.axis('off') | ||
plt.title('Original') | ||
plt.subplot(222) | ||
plt.imshow(noisy, cmap='gray') | ||
plt.axis('off') | ||
plt.title('Noisy \nPSNR={:.2f}, SSMI={:.2f}'.format(results_lgbm[0][0], results_lgbm[0][1])) | ||
plt.subplot(223) | ||
plt.imshow(denoised_lgbm, cmap='gray') | ||
plt.axis('off') | ||
plt.title('LGBM {:.2f}sec \nPSNR={:.2f}, SSMI={:.2f}'.format(results_lgbm[2], results_lgbm[1][0], results_lgbm[1][1])) | ||
# plt.subplot(224) | ||
# plt.imshow(denoised_cnn, cmap='gray') | ||
# plt.axis('off') | ||
# plt.title('CNN {:.2f}sec \nPSNR={:.2f}, SSMI={:.2f}'.format(results_cnn[2], results_cnn[1][0], results_cnn[1][1])) | ||
plt.subplots_adjust(left=0.11, right=0.9, top=0.91, bottom=0.02, hspace=0.25, wspace=0.2) | ||
plt.savefig(os.path.join(savepath, 'NN_2D.png'), dpi=300) | ||
plt.show() |
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import os | ||
import time | ||
|
||
import numpy as np | ||
from matplotlib import pyplot as plt | ||
from skimage.data import camera | ||
from skimage.exposure import rescale_intensity | ||
from skimage.measure import compare_psnr as psnr | ||
from skimage.measure import compare_ssim as ssim | ||
from skimage.util import random_noise | ||
|
||
from src.pitl.features.multiscale_convolutions import MultiscaleConvolutionalFeatures | ||
from src.pitl.pitl_classic import ImageTranslator | ||
from src.pitl.regression.nn import CNNRegressor, Modeltype | ||
|
||
""" | ||
Demo for self-supervised denoising using camera image with synthetic noise | ||
""" | ||
|
||
|
||
def demo_pitl_2D(noisy): | ||
|
||
|
||
scales = [1, 3, 7, 15, 31, 63, 127] | ||
widths = [3, 3, 3, 3, 3, 3, 3] | ||
|
||
start_time = time.time() | ||
|
||
regressor = CNNRegressor(mode=Modeltype.Perceptron, | ||
learning_rate=0.001, | ||
early_stopping_rounds=5) | ||
|
||
|
||
generator = MultiscaleConvolutionalFeatures(kernel_widths=widths, | ||
kernel_scales=scales, | ||
kernel_shapes=['l1'] * len(scales), | ||
exclude_center=True, | ||
) | ||
|
||
it = ImageTranslator(feature_generator=generator, regressor=regressor) | ||
|
||
denoised = it.train(noisy, noisy) | ||
|
||
results = [[psnr(noisy, image), ssim(noisy, image)]] | ||
results.append([psnr(denoised, image), ssim(denoised, image)]) | ||
results.append(time.time() - start_time) | ||
|
||
print("noisy ", results[0]) | ||
print("denoised ", results[1]) | ||
print("time elapsed: ", results[2]) | ||
|
||
# from napari import ViewerApp | ||
# with app_context(): | ||
# viewer = ViewerApp() | ||
# viewer.add_image(rescale_intensity(image, in_range='image', out_range=(0, 1)), name='image') | ||
# viewer.add_image(rescale_intensity(noisy, in_range='image', out_range=(0, 1)), name='noisy') | ||
|
||
# viewer.add_image(rescale_intensity(denoised, in_range='image', out_range=(0, 1)), name='denoised%d' % param) | ||
# viewer.add_image(rescale_intensity(denoised_predict, in_range='image', out_range=(0, 1)), name='denoised_predict%d' % param) | ||
|
||
return denoised, results | ||
|
||
|
||
image = camera().astype(np.float32) # [:,50:450] | ||
image = rescale_intensity(image, in_range='image', out_range=(0, 1)) | ||
|
||
intensity = 5 | ||
np.random.seed(0) | ||
noisy = np.random.poisson(image * intensity) / intensity | ||
noisy = random_noise(noisy, mode='gaussian', var=0.01, seed=0) | ||
noisy = noisy.astype(np.float32) | ||
|
||
denoised_cnn, results_cnn = demo_pitl_2D(noisy) | ||
|
||
savepath = '/Users/hirofumi.kobayashi/Github_repositories/pitl/output_data' | ||
plt.figure() | ||
plt.subplot(221) | ||
plt.imshow(image, cmap='gray') | ||
plt.axis('off') | ||
plt.title('Original') | ||
plt.subplot(222) | ||
plt.imshow(noisy, cmap='gray') | ||
plt.axis('off') | ||
plt.title('Noisy \nPSNR={:.2f}, SSMI={:.2f}'.format(results_cnn[0][0], results_cnn[0][1])) | ||
# plt.subplot(223) | ||
# plt.imshow(denoised_lgbm, cmap='gray') | ||
# plt.axis('off') | ||
# plt.title('LGBM {:.2f}sec \nPSNR={:.2f}, SSMI={:.2f}'.format(results_lgbm[2], results_lgbm[1][0], results_lgbm[1][1])) | ||
plt.subplot(224) | ||
plt.imshow(denoised_cnn, cmap='gray') | ||
plt.axis('off') | ||
plt.title('NN {:.2f}sec \nPSNR={:.2f}, SSMI={:.2f}'.format(results_cnn[2], results_cnn[1][0], results_cnn[1][1])) | ||
plt.subplots_adjust(left=0.11, right=0.9, top=0.91, bottom=0.02, hspace=0.25, wspace=0.2) | ||
plt.savefig(os.path.join(savepath, 'NN_2D.png'), dpi=300) | ||
plt.show() |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
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.
all looks good except these commented out lines