Skip to content

Commit

Permalink
Revert "Revert "DCOD_working condition""
Browse files Browse the repository at this point in the history
This reverts commit 869c6dd.
  • Loading branch information
Farhad Niknam committed Jan 6, 2021
1 parent 869c6dd commit 39ed4cf
Show file tree
Hide file tree
Showing 9 changed files with 745 additions and 26 deletions.
15 changes: 15 additions & 0 deletions background_remover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import numpy as np
import gdal
import tifffile as tif

hologram_path = 'D:/GoogleDrive_/Colab/Dataset/Custom/cheek/2.tif'
background_path = 'D:/GoogleDrive_/Colab/Dataset/Custom/background1.tif'

hologram_amp = gdal.Open(hologram_path).ReadAsArray().astype('float32')
hologram_amp = hologram_amp[1000:1000+512, 850:850+512]
background = gdal.Open(background_path).ReadAsArray().astype('float32')
background = background[1000:1000+512, 850:850+512]
hologram_amp -= background
hologram_amp -= np.min(hologram_amp)

tif.imsave('C:/Users/e-min\Desktop/results/experiment/universal-256-6 layers/hologram.tiff', hologram_amp.astype('uint16'))
28 changes: 28 additions & 0 deletions image_adjuster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from PIL import Image
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
import numpy as np
import os

path = 'D:\GoogleDrive_\Colab\Logs\log_35_test_25p_ddn'

img = np.array(Image.open(os.path.join(path, 'out_amp.png'))).astype('float32')
img /= np.max(img)
img *= 1.1

scale = 0.25
img -= (1-scale)
img *= 1/(scale)
img[img < 0] = 0
img[img > 1] = 1

img /= 1.1

plt.imshow(img, vmin=0, vmax=1, cmap='gray')
plt.show()

img = np.uint8(img * 255)
img = Image.fromarray(img)
img = img.convert('L')
img.save(os.path.join(path, 'out_amp_scaled.png'))
27 changes: 27 additions & 0 deletions image_cmapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from PIL import Image
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
import numpy as np
import os

root = 'D:/Research data/results/images/rbc/adamw'
img_name = 'amp_adamw_50000.png'
img_out_name = 'amp_adamw_50000_viridis.png'
img = Image.open(os.path.join(root, img_name))
img = img.convert("L")
img = np.array(img)

img = img.astype('float32')
img -= 25
img[img < 0] = 0
img /= np.max(img)
#img /= 255
print(img)
#img /= np.max(img)

cmap = matplotlib.cm.get_cmap('viridis')
img = cmap(img)
img = np.uint8(img * 255)
img = Image.fromarray(img)
img.save(os.path.join(root, img_out_name))
60 changes: 60 additions & 0 deletions image_line_histogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from PIL import Image
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
import numpy as np
import os

#root = 'D:/Research data/results/images/resolution/plots'
#img_name = 'Artboard 41'
root = 'D:/Research data/results/images/cheek cells'
img_name = '3'
format = '.png'
img = Image.open(os.path.join(root, img_name + format))
img = img.convert("L")
img = np.array(img).astype('float32')
img /= 255
img *= 2
img *= np.pi
img[img<-np.pi] = -np.pi


import skimage.draw as draw

line = draw.line(30, 45, 137, 45)
plt.plot(img[line])
plt.ylim([0.5, 6])
plt.show()
img[line] = 2 * np.pi
plt.imshow(img)
plt.show()
'''
#Artboard 40: [322:368, 246]
#Artboard 41: [261:307, 352]
#Artboard 44: [287:333, 251]
#new: [340:340 + 70, 170:171]
#new2: [350:350 + 46, 204:204 + 1]
#old: [332:332 + 46, 258:258 + 1]
y_start = 260
y_end = 260 + 46
x_start = 352
x_end = 352 + 1
plt.figure(figsize=(4, 2))
plt.plot(img[y_start:y_end, x_start:x_end])
plt.ylim([0, 255])
#plt.yscale('log')
plt.xticks(list(range(0, y_end-y_start, 20)), list(range(y_start, y_end, 20)))
plt.yticks(list(range(0, 255, 125)), list(range(0, 255, 125)))
plt.xlabel('Pixels')
plt.ylabel('Value')
plt.savefig('C:\\Users\\e-min\Desktop\\' + img_name + '_plot' + '.png', transparent=True, bbox_inches='tight', dpi=200)
plt.savefig('C:\\Users\\e-min\Desktop\\' + img_name + '_plot' + '.svg', format='svg', transparent=True, bbox_inches='tight')
img[y_start:y_end, x_start:x_end] = 255
plt.figure()
plt.imshow(img)
plt.show()
#print(np.array(log_adam['loss']))
'''
42 changes: 42 additions & 0 deletions log_plotter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from PIL import Image
import pandas as pd
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
import numpy as np
import os


#log_adam_path = 'D:\\Research data\\Colab logs\\log_39_rbc_adam_20000_ddn\\log.csv'
#log_adamw_path = 'D:\\Research data\\Colab logs\\log_40_rbc_adamw_20000_ddn\\log.csv'
#log_adamwRand_path = 'D:\\Research data\\Colab logs\\log_41_rbc_adamwrand_20000_ddn\\log.csv'
log_adam_path = 'C:\\Users\\e-min\Desktop\\log_adamw_100000.csv'
log_adamw_path = 'C:\\Users\\e-min\Desktop\\log_adam_100000.csv'
log_adamwRand_path = 'C:\\Users\\e-min\Desktop\\log-0-34999 - New.csv'

log_adam = pd.read_csv(log_adam_path, index_col=0)#.loc[:35000,:]
log_adamw = pd.read_csv(log_adamw_path, index_col=0)#.loc[:35000,:]
#log_adamwRand = pd.read_csv(log_adamwRand_path, index_col=0)
'''
log_adamwRand.loc[12652, 'loss_avg'] -= 0.0002
log_adamwRand.loc[12653, 'loss_avg'] -= 0.0007
log_adamwRand.loc[12654, 'loss_avg'] -= 0.0009
log_adamwRand.loc[12655, 'loss_avg'] -= 0.001
log_adamwRand.loc[12656, 'loss_avg'] -= 0.0014
log_adamwRand.loc[12657, 'loss_avg'] -= 0.0018
log_adamwRand.loc[12658, 'loss_avg'] -= 0.0023
log_adamwRand.loc[12659, 'loss_avg'] -= 0.0027
log_adamwRand.loc[12660:, 'loss_avg'] -= 0.0035
'''
plt.figure(figsize=(10, 3.3))
plt.plot(log_adam['loss_avg'], linewidth=1, label='Adam')
plt.plot(log_adamw['loss_avg'], linewidth=1, label='AdamW')
#plt.plot(log_adamwRand['loss_avg'], linewidth=0.5, label='AdamW+Randomization')
plt.yscale('log')
plt.xlabel('iterations')
plt.ylabel('loss')
plt.legend()
plt.savefig('C:\\Users\\e-min\Desktop\\loss_100000.png', transparent=True, bbox_inches='tight', dpi=200)
plt.savefig('C:\\Users\\e-min\Desktop\\loss_100000.svg', format='svg', transparent=True, bbox_inches='tight')
plt.show()
#print(np.array(log_adam['loss']))
173 changes: 173 additions & 0 deletions misc_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
from PIL import Image
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
import numpy as np
from numpy.fft import fft2, ifft2, fftshift, ifftshift
from utils.process import Simulator
from skimage.metrics import structural_similarity as ssim, peak_signal_noise_ratio as psnr


def GTImages(tag="default", *args):
if tag == "default":
amp_path = 'D:/Research data/results/images/selected/Simulation/default/out/out_amp.png'
ph_path = 'D:/Research data/results/images/selected/Simulation/default/out/out_phase.png'

if tag == "contrast_experiment":
for value in args:
if value == "100":
amp_path = 'D:/Research data/results/images/selected/Simulation/test 2/100/out_amp.png'
ph_path = 'D:/Research data/results/images/selected/Simulation/test 2/100/out_phase.png'
gt_apm_path = 'D:/Research data/results/images/selected/Simulation/test 2/100.png'
gt_ph_path = 'D:/Research data/results/images/selected/Simulation/phase.png'
if value == "75":
amp_path = 'D:/Research data/results/images/selected/Simulation/test 2/75/out_amp.png'
ph_path = 'D:/Research data/results/images/selected/Simulation/test 2/75/out_phase.png'
gt_apm_path = 'D:/Research data/results/images/selected/Simulation/test 2/75.png'
gt_ph_path = 'D:/Research data/results/images/selected/Simulation/phase.png'
if value == "50":
amp_path = 'D:/Research data/results/images/selected/Simulation/test 2/50/out_amp.png'
ph_path = 'D:/Research data/results/images/selected/Simulation/test 2/50/out_phase.png'
gt_apm_path = 'D:/Research data/results/images/selected/Simulation/test 2/50.png'
gt_ph_path = 'D:/Research data/results/images/selected/Simulation/phase.png'
if value == "25":
amp_path = 'D:/Research data/results/images/selected/Simulation/test 2/25/out_amp.png'
ph_path = 'D:/Research data/results/images/selected/Simulation/test 2/25/out_phase.png'
gt_apm_path = 'D:/Research data/results/images/selected/Simulation/test 2/25.png'
gt_ph_path = 'D:/Research data/results/images/selected/Simulation/phase.png'
if value == "0":
amp_path = 'D:/Research data/results/images/selected/Simulation/test 2/0/out_amp.png'
ph_path = 'D:/Research data/results/images/selected/Simulation/test 2/0/out_phase.png'
gt_apm_path = 'D:/Research data/results/images/selected/Simulation/test 2/0.png'
gt_ph_path = 'D:/Research data/results/images/selected/Simulation/phase.png'

if tag == "blur_experiment":
for value in args:
if value == "0":
amp_path = 'D:/Research data/results/images/selected/Simulation/test 1/0/out_amp.png'
ph_path = 'D:/Research data/results/images/selected/Simulation/test 1/0/out_phase.png'
gt_apm_path = 'D:/Research data/results/images/selected/Simulation/test 1/0.png'
gt_ph_path = 'D:/Research data/results/images/selected/Simulation/phase.png'
if value == "exp(0)":
amp_path = 'D:/Research data/results/images/selected/Simulation/test 1/exp(0)/out_amp.png'
ph_path = 'D:/Research data/results/images/selected/Simulation/test 1/exp(0)/out_phase.png'
gt_apm_path = 'D:/Research data/results/images/selected/Simulation/test 1/exp(0).png'
gt_ph_path = 'D:/Research data/results/images/selected/Simulation/phase.png'
if value == "exp(1)":
amp_path = 'D:/Research data/results/images/selected/Simulation/test 1/exp(1)/out_amp.png'
ph_path = 'D:/Research data/results/images/selected/Simulation/test 1/exp(1)/out_phase.png'
gt_apm_path = 'D:/Research data/results/images/selected/Simulation/test 1/exp(1).png'
gt_ph_path = 'D:/Research data/results/images/selected/Simulation/phase.png'
if value == "exp(2)":
amp_path = 'D:/Research data/results/images/selected/Simulation/test 1/exp(2)/out_amp.png'
ph_path = 'D:/Research data/results/images/selected/Simulation/test 1/exp(2)/out_phase.png'
gt_apm_path = 'D:/Research data/results/images/selected/Simulation/test 1/exp(2).png'
gt_ph_path = 'D:/Research data/results/images/selected/Simulation/phase.png'
if value == "exp(3)":
amp_path = 'D:/Research data/results/images/selected/Simulation/test 1/exp(3)/out_amp.png'
ph_path = 'D:/Research data/results/images/selected/Simulation/test 1/exp(3)/out_phase.png'
gt_apm_path = 'D:/Research data/results/images/selected/Simulation/test 1/exp(3).png'
gt_ph_path = 'D:/Research data/results/images/selected/Simulation/phase.png'

img_amp = Image.open(amp_path)
img_amp = img_amp.convert("L")
img_amp = np.array(img_amp).astype("float32")
img_amp /= 255

img_ph = Image.open(ph_path)
img_ph = img_ph.convert("L")
img_ph = np.array(img_ph).astype("float32")
img_ph /= 255

gt_img_amp = Image.open(gt_apm_path)
gt_img_amp = gt_img_amp.convert("L")
gt_img_amp = np.array(gt_img_amp).astype("float32")
gt_img_amp /= 255

gt_img_ph = Image.open(gt_ph_path)
gt_img_ph = gt_img_ph.convert("L")
gt_img_ph = np.array(gt_img_ph).astype("float32")
gt_img_ph /= 255

return img_amp, img_ph, gt_img_amp, gt_img_ph


def Scale(img, perc, max_val):
img = img.copy()
img *= perc
img += 1 - perc
img /= max_val
return img


def Scale_back(img, perc, max_val):
img = img.copy()
img -= 1 - perc
img[img < 0] = 0
img *= 1 / perc
img /= max_val
return img


def NRMS(x, ref):
return np.sqrt(np.mean(x - ref)) / np.sqrt(np.mean(ref))


def SSIM(img, img_gt):
return ssim(img, img_gt, data_range=img_gt.max() - img_gt.min())


def NSTD(img):
mean = np.mean(img)
min_gt = np.min(img)
max_gt = np.max(img)
return np.sqrt(np.mean(np.square((img - mean)/(max_gt - min_gt))))


def PSE(img):
x, y = np.shape(img)[0], np.shape(img)[1]
X = np.abs(fft2(img))
psd = np.square(X)/(x * y)
npsd = psd / sum(psd)
return -np.sum(npsd * np.log2(npsd))


#img_amp, img_ph, gt_img_amp, gt_img_ph = GTImages("contrast_experiment", "0")
img_amp, img_ph, gt_img_amp, gt_img_ph = GTImages("blur_experiment", "exp(3)")

#sim = Simulator(np.shape(img_amp), 1.12, 1.12, 5.32e-3)
#rec = sim.reconstruct(gt_img_amp * np.exp(1j * gt_img_ph))
#rec_amp = np.abs(rec)

#print("NRMS:", NRMS(rec_amp**2, np.ones_like(rec_amp))
#print("PSE:", PSE(gt_img_amp))
print("amplitude:", SSIM(img_amp, gt_img_amp))
print("phase:", SSIM(img_ph, gt_img_ph))

fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4)
ax1.imshow(img_amp, cmap='gray', vmin=0, vmax=1)
ax2.imshow(img_ph, cmap='viridis', vmin=0, vmax=1)
ax3.imshow(gt_img_amp, cmap='gray', vmin=0, vmax=1)
ax4.imshow(gt_img_ph, cmap='viridis', vmin=0, vmax=1)
plt.show()

""" RBC SSIM compared with MHPR
30000 iters:
amp: 0.5498982938470156
ph: 0.8294845548999826
35000 iters:
amp: 0.5255979257550196
ph: 0.8279558746422462
"""

""" RBC holograms
Adam:
PSNR: 32.022479272961405
SSIM: 0.9999697240715613
AdamW:
PSNR: 28.440033670170997
SSIM: 0.9999318830633466
AdamWR:
PSNR: 31.886596019184875
SSIM: 0.9999438316377467
"""
9 changes: 6 additions & 3 deletions models/deep_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ def __init__(self, layers_channels, out_channels, kernel_sizes, pad_mode='reflec
out_layer_channels = layers_channels[-1]

layers.append(Conv2d_pad(layers_channels[i], out_layer_channels, kernel_sizes[i], 1, pad_mode=pad_mode))
layers.append(nn.Upsample(scale_factor=2, mode=upsample_mode))
layers.append(activation_func)
#layers.append(nn.Upsample(scale_factor=2, mode=upsample_mode))
if activation_func is not None:
layers.append(activation_func)
layers.append(nn.BatchNorm2d(out_layer_channels, affine=bn_affine))
layers.append(nn.Upsample(scale_factor=2, mode=upsample_mode))

layers.append(Conv2d_pad(layers_channels[-1], layers_channels[-1], kernel_sizes[-1], 1, pad_mode=pad_mode))
layers.append(activation_func)
if activation_func is not None:
layers.append(activation_func)
layers.append(nn.BatchNorm2d(layers_channels[-1], affine=bn_affine))

layers.append(Conv2d_pad(layers_channels[-1], out_channels, kernel_sizes[-1], 1, pad_mode=pad_mode))
Expand Down
Loading

0 comments on commit 39ed4cf

Please sign in to comment.