Skip to content

Commit

Permalink
Add mean tanh loss as hdr2
Browse files Browse the repository at this point in the history
AstroDenoisePyGUI: update dep Kivy 2.1.0
AstroDenoisePyGUI: toolbar tweaks, add label for progress, rename Expand Low to Denoise Strength and move to Denoise section.
AstroDenoisePyGUI: processing improvement, implement cancellable processing and prevent multiple process calls
AstroDenoisePyGUI: add new denoise model as part of msi dist
AstroDenoisePyGUI: minor bug fixes
AstroDenoisePyGUI: bump version to 0.4.0
  • Loading branch information
p7ayfu77 committed Mar 27, 2022
1 parent 39f4d14 commit 3741ad8
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 43 deletions.
File renamed without changes.
1 change: 1 addition & 0 deletions astrodenoisepy/dist-models/v0.4.0-01/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"n_dim": 2, "axes": "YXC", "n_channel_in": 1, "n_channel_out": 1, "train_checkpoint": "weights_best.h5", "train_checkpoint_last": "weights_last.h5", "train_checkpoint_epoch": "weights_now.h5", "probabilistic": false, "unet_residual": true, "unet_n_depth": 4, "unet_kern_size": 3, "unet_n_first": 32, "unet_last_activation": "linear", "unet_input_shape": [null, null, 1], "train_loss": "hdr2", "train_epochs": 30, "train_steps_per_epoch": 75, "train_learning_rate": 0.0004, "train_batch_size": 128, "train_tensorboard": true, "train_reduce_lr": {"factor": 0.5, "patience": 10, "min_delta": 0}}
Binary file not shown.
5 changes: 3 additions & 2 deletions astrodenoisepygui-package-dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"packages": ["kivy"],
"include_files": [
"LICENSE.txt",
["astrodenoisepy\\dist-models\\main", "models\\default"],
["astrodenoisepy\\dist-models\\v0.4.0-01", "models\\default"],
["astrodenoisepy\\dist-models\\v0.3.0-01", "models\\v0.3.0-01"],
["astrodenoisepy\\data", "astrodenoisepy\\data"],
"astrodenoisepygui.kv",
#angle
Expand All @@ -17,7 +18,7 @@
".venv\\share\\glew\\bin\\glew32.dll",
#sdl2
".venv\\share\\sdl2\\bin\\libFLAC-8.dll",
".venv\\share\\sdl2\\bin\\libfreetype-6.dll",
#".venv\\share\\sdl2\\bin\\libfreetype-6.dll",
".venv\\share\\sdl2\\bin\\libjpeg-9.dll",
".venv\\share\\sdl2\\bin\\libmodplug-1.dll",
".venv\\share\\sdl2\\bin\\libmpg123-0.dll",
Expand Down
47 changes: 25 additions & 22 deletions astrodenoisepygui.kv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#:kivy 2.0.0
#:kivy 2.1.0
#:import utils kivy.utils
#:import np numpy
#:import Path pathlib.Path
Expand Down Expand Up @@ -146,21 +146,6 @@ Root:
text: '{:.3f}'.format(s2.value)
on_text_validate: main.stfB = '{:.3f}'.format(np.clip(float(self.text),s2.min,s2.max))
input_filter: 'float'
SliderToolLayout:
ToolLabel:
text: 'Expand Low'
BoxLayout:
MySlider:
id: s5
min: 0
max: 0.5
value: main.expand_low
step: 0.01
on_slider_release: main.expand_low = self.value
MySliderTextInput:
text: '{:.3f}'.format(s5.value)
on_text_validate: main.expand_low = '{:.3f}'.format(np.clip(float(self.text),s5.min,s5.max))
input_filter: 'float'
SliderToolLayout1:
ToolLabel:
text: 'Denoise Model'
Expand All @@ -175,6 +160,21 @@ Root:
valign: 'middle'
padding_x: '5dp'
text_size: self.size
SliderToolLayout1:
ToolLabel:
text: 'Denoise Strength'
BoxLayout:
MySlider:
id: s5
min: 0
max: 1
value: main.expand_low
step: 0.01
on_slider_release: main.expand_low = self.value
MySliderTextInput:
text: '{:.3f}'.format(s5.value)
on_text_validate: main.expand_low = '{:.3f}'.format(np.clip(float(self.text),s5.min,s5.max))
input_filter: 'float'
SliderToolLayout1:
ToolLabel:
text: 'Denoise Tiling'
Expand Down Expand Up @@ -236,12 +236,15 @@ Root:
id: my_widget_process
text: "Process"
on_release: root.start_process()
ProgressBar:
id: pb
size_hint_x: .5
size_hint_y: None
height: '5dp'
value_normalized: main.progress
SliderToolLayout1:
ToolLabel:
text: 'Denoise Progress'
ProgressBar:
id: pb
size_hint_x: .5
size_hint_y: None
height: '5dp'
value_normalized: main.progress
HSeparator:
rgba: 1, 1, 1, 0
height: dp(10)
Expand Down
50 changes: 34 additions & 16 deletions astrodenoisepygui.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import kivy
kivy.require('2.0.0')
kivy.require('2.1.0')
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
Expand All @@ -17,11 +17,13 @@
import os
import sys
import signal
import time
import traceback
import threading
from pathlib import Path

import numpy as np
import tqdm
from tifffile import imread, imsave
from astropy.io import fits
import tensorflow as tf
Expand Down Expand Up @@ -112,14 +114,16 @@ def __init__(self, **kwargs):
self.fits_headers = None
self.process_trigger = Clock.create_trigger(self.process_now)
self.bind(stfC=self.process_trigger, stfB=self.process_trigger,tilling=self.process_trigger,expand_low=self.process_trigger)
self.stop_event = threading.Event()
self.processthread = None

imageout = ObjectProperty(None, allownone=True)
filetoload = StringProperty()
stfC = NumericProperty(-2.8)
stfB = NumericProperty(0.25)
tilling = NumericProperty(2)
tilling = NumericProperty(3)
sizing = NumericProperty(1)
expand_low = NumericProperty(0)
expand_low = NumericProperty(0.9)
denoise_enabled = BooleanProperty(False)
normalize_enabled = BooleanProperty(True)
autoupdate_enabled = BooleanProperty(True)
Expand Down Expand Up @@ -175,8 +179,7 @@ def load(self, path):
if not isWindows():
self.dismiss_popup()

self.sizing = 1
self.denoise_enabled = False
self.sizing = 1
self.load_now()

def load_now(self):
Expand Down Expand Up @@ -214,7 +217,8 @@ def load_result(self, result, headers):
self.imageout = None
self.processed = False
self.preprocessed = False
self.process_now()
self.denoise_enabled = False
self.process_trigger()

def load_file_data(self, path):

Expand Down Expand Up @@ -322,19 +326,19 @@ def on_stfC(self, instance, value):
def on_expand_low(self, instance, value):
self.expand_low = value
self.processed = False
self.preprocessed = False
#self.preprocessed = False

def on_tilling(self, instance, value):
self.tilling = value
self.processed = False
self.preprocessed = False
#self.preprocessed = False

def reset_sliders(self):
self.processed = False
self.preprocessed = False
#self.preprocessed = False
self.stfC = -2.8
self.stfB = 0.25
self.expand_low = 0
#self.expand_low = 0.9

def denoise_check(self, instance, value):
self.denoise_enabled = value
Expand Down Expand Up @@ -390,12 +394,23 @@ def start_process(self):
elif self.preprocessedimagedata is not None:
self.process_result(self.preprocessedimagedata)
return

if self.processthread is not None and self.processthread.is_alive():
self.stop_event.set()
with tqdm.tqdm(desc='Waiting for thread to stop...') as waiter_loger:
wait_count = 0
while self.processthread.is_alive():
wait_count += 1
time.sleep(0.1)
waiter_loger.update(wait_count)

self.stop_event.clear()

Window.set_system_cursor('wait')

processthread = threading.Thread(target=self.process_callback)
self.processthread = threading.Thread(target=self.process_callback)
threading.excepthook = self.process_exception_callback
processthread.start()
self.processthread.start()

def process_exception_callback(self,args):
e = args[1]
Expand All @@ -407,8 +422,9 @@ def process_exception_callback(self,args):

def process_callback(self):
result = self.process(self.rawimagedata,self.stfC,self.stfB)
#(1417, 2073, 3)
self.process_result(result)

if result is not None:
self.process_result(result)

@mainthread
def process_result(self,result):
Expand Down Expand Up @@ -442,15 +458,17 @@ def update_progress(self,progress):
def process(self, data, C=-2.8,B=0.25):

self.update_progress(0)

normalizer = STFNormalizer(C=C,B=B,expand_low=self.expand_low,do_after=False) if self.normalize_enabled else NoNormalizer(expand_low=self.expand_low)
expand_low_actual = 0.5 - (self.expand_low/2)
normalizer = STFNormalizer(C=C,B=B,expand_low=expand_low_actual,do_after=False) if self.normalize_enabled else NoNormalizer(expand_low=expand_low_actual)

if self.denoise_enabled:
with tf.device(f"/{self.selected_device}:0"):
axes = 'YX'
model = CARE(config=None, name=self.selected_model, basedir=self.models_basedir)
output_denoised = []
for i, c in enumerate(data):
if self.stop_event.is_set():
return None
output_denoised.append(
model.predict(c, axes, normalizer=normalizer,resizer=PadAndCropResizer(), n_tiles = (self.tilling,self.tilling))
)
Expand Down
2 changes: 1 addition & 1 deletion astrodenoisepyguiversion.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = '0.3.3'
version = '0.4.0'
13 changes: 13 additions & 0 deletions csbdeep/internals/losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ def mse(y_true, y_pred):
return R(K.square(y_pred[:,:n,...] - y_true))
return mse

def loss_hdr2(mean=True):
R = _mean_or_not(mean)
if backend_channels_last():
def hdr2(y_true, y_pred):
n = K.shape(y_true)[-1]
return R(K.abs(K.tanh(y_pred[...,:n])-K.tanh(y_true)))
return hdr2
else:
def hdr2(y_true, y_pred):
n = K.shape(y_true)[1]
return R(K.abs(K.tanh(y_pred[:,:n,...])-K.tanh(y_true)))
return hdr2

def loss_hdr(mean=True):
R = _mean_or_not(mean)
if backend_channels_last():
Expand Down
2 changes: 1 addition & 1 deletion csbdeep/internals/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from six.moves import range, zip, map, reduce, filter

from ..utils import _raise, move_channel_for_backend, axes_dict, axes_check_and_normalize, backend_channels_last
from ..internals.losses import loss_laplace, loss_mse, loss_mae, loss_hdr, loss_thresh_weighted_decay
from ..internals.losses import loss_laplace, loss_mse, loss_mae, loss_hdr, loss_hdr2, loss_thresh_weighted_decay

import numpy as np

Expand Down
2 changes: 1 addition & 1 deletion csbdeep/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def _is_int(v,low=None,high=None):
)
ok['train_loss'] = (
( self.probabilistic and self.train_loss == 'laplace' ) or
(not self.probabilistic and self.train_loss in ('mse','mae','hdr'))
(not self.probabilistic and self.train_loss in ('mse','mae','hdr','hdr2'))
)
ok['train_epochs'] = _is_int(self.train_epochs,1)
ok['train_steps_per_epoch'] = _is_int(self.train_steps_per_epoch,1)
Expand Down

0 comments on commit 3741ad8

Please sign in to comment.