Skip to content

Commit

Permalink
Merge pull request #110 from juglab/tf2
Browse files Browse the repository at this point in the history
Upgrade to TensorFlow 2
  • Loading branch information
tibuch authored Jun 24, 2021
2 parents ce99376 + ffbd612 commit 1c50f5b
Show file tree
Hide file tree
Showing 23 changed files with 1,081 additions and 656 deletions.
10 changes: 1 addition & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,10 @@ env:

matrix:
include:
- os: linux
dist: bionic
python: 3.6
env: TENSORFLOW='tensorflow==1.14.0' KERAS='keras==2.2.5'
- os: linux
dist: bionic
python: 3.6
env: TENSORFLOW='tensorflow==1.12.0' KERAS='keras==2.2.5'
- os: linux
dist: bionic
python: 3.7
env: TENSORFLOW='tensorflow==1.14.0' KERAS='keras==2.2.5'
env: TENSORFLOW='tensorflow==2.4.1' KERAS='keras==2.3.1'

install:
- pip install $TENSORFLOW $KERAS
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Our implementation is based on [CSBDEEP](http://csbdeep.bioimagecomputing.com) (

## Installation
This implementation requires [Tensorflow](https://www.tensorflow.org/install/).
We have tested Noise2Void on LinuxMint 19 and Ubuntu 18.0 using python 3.6 and 3.7 and tensorflow-gpu 1.12.0 and 1.14.0.
We have tested Noise2Void using Python 3.7 and tensorflow-gpu 2.4.1.

Note: If you want to use TensorFlow 1.15 you have to install N2V v0.2.1. N2V v0.3.0 supports TensorFlow 2 only.

#### If you start from scratch...
We recommend using [miniconda](https://docs.conda.io/en/latest/miniconda.html).
Expand All @@ -22,13 +24,12 @@ If you do not yet have a strong opinion, just use it too!
After installing Miniconda, the following lines might are likely the easiest way to get Tensorflow and CuDNN installed on your machine (_Note:_ Macs are not supported, and if you sit on a Windows machine all this might also require some modifications.):

```
$ conda create -n 'n2v' python=3.6
$ conda create -n 'n2v' python=3.7
$ source activate n2v
$ conda install tensorflow-gpu=1.14 keras=2.2.4
$ conda install tensorflow-gpu=2.4.1 keras=2.3.1
$ pip install jupyter
```

Note: it is very important that the version of keras be 2.2.4 or 2.2.5, hence the explicit installation above.
Once this is done (or you had tensorflow et al. installed already), you can install N2V with one of the following two options:

#### Option 1: PIP (current stable release)
Expand Down
578 changes: 529 additions & 49 deletions examples/2D/denoising2D_BSD68/BSD68_reproducibility.ipynb

Large diffs are not rendered by default.

107 changes: 58 additions & 49 deletions examples/2D/denoising2D_RGB/01_training.ipynb

Large diffs are not rendered by default.

18 changes: 5 additions & 13 deletions examples/2D/denoising2D_RGB/02_prediction.ipynb

Large diffs are not rendered by default.

114 changes: 61 additions & 53 deletions examples/2D/denoising2D_SEM/01_training.ipynb

Large diffs are not rendered by default.

26 changes: 10 additions & 16 deletions examples/2D/denoising2D_SEM/02_prediction.ipynb

Large diffs are not rendered by default.

65 changes: 34 additions & 31 deletions examples/2D/structN2V_2D_convallaria/01_training.ipynb

Large diffs are not rendered by default.

20 changes: 6 additions & 14 deletions examples/2D/structN2V_2D_convallaria/02_prediction.ipynb

Large diffs are not rendered by default.

121 changes: 60 additions & 61 deletions examples/2D/structN2V_2D_synth_mem/train_and_predict.ipynb

Large diffs are not rendered by default.

82 changes: 39 additions & 43 deletions examples/3D/01_training.ipynb

Large diffs are not rendered by default.

20 changes: 6 additions & 14 deletions examples/3D/02_prediction.ipynb

Large diffs are not rendered by default.

37 changes: 19 additions & 18 deletions n2v/internals/N2V_DataWrapper.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from keras.utils import Sequence
from csbdeep.internals.train import RollingSequence
from tensorflow.keras.utils import Sequence

import numpy as np

class N2V_DataWrapper(Sequence):
class N2V_DataWrapper(RollingSequence):
"""
The N2V_DataWrapper extracts random sub-patches from the given data and manipulates 'num_pix' pixels in the
input.
Expand All @@ -23,8 +24,9 @@ class N2V_DataWrapper(Sequence):
The manipulator used for the pixel replacement.
"""

def __init__(self, X, Y, batch_size, perc_pix=0.198, shape=(64, 64),
def __init__(self, X, Y, batch_size, length, perc_pix=0.198, shape=(64, 64),
value_manipulation=None, structN2Vmask=None):
super(N2V_DataWrapper, self).__init__(data_size=len(X), batch_size=batch_size, length=length)
self.X, self.Y = X, Y
self.batch_size = batch_size
self.perm = np.random.permutation(len(self.X))
Expand Down Expand Up @@ -54,24 +56,23 @@ def __init__(self, X, Y, batch_size, perc_pix=0.198, shape=(64, 64),
else:
raise Exception('Dimensionality not supported.')

self.X_Batches = np.zeros((self.X.shape[0], *self.shape, self.n_chan), dtype=np.float32)
self.Y_Batches = np.zeros((self.Y.shape[0], *self.shape, 2*self.n_chan), dtype=np.float32)

def __len__(self):
return int(np.ceil(len(self.X) / float(self.batch_size)))
self.X_Batches = np.zeros((self.batch_size, *self.shape, self.n_chan), dtype=np.float32)
self.Y_Batches = np.zeros((self.batch_size, *self.shape, 2*self.n_chan), dtype=np.float32)

def on_epoch_end(self):
self.perm = np.random.permutation(len(self.X))
self.X_Batches *= 0
self.Y_Batches *= 0


def __getitem__(self, i):
idx = slice(i * self.batch_size, (i + 1) * self.batch_size)
idx = self.perm[idx]
idx = self.batch(i)
# idx = slice(i * self.batch_size, (i + 1) * self.batch_size)
# idx = self.perm[idx]
self.X_Batches *= 0
self.Y_Batches *= 0
self.patch_sampler(self.X, self.X_Batches, indices=idx, range=self.range, shape=self.shape)

for c in range(self.n_chan):
for j in idx:
for j in range(self.batch_size):
coords = self.get_stratified_coords(self.rand_float, box_size=self.box_size,
shape=self.shape)

Expand All @@ -87,7 +88,7 @@ def __getitem__(self, i):
if self.structN2Vmask is not None:
self.apply_structN2Vmask(self.X_Batches[j, ..., c], coords, self.dims, self.structN2Vmask)

return self.X_Batches[idx], self.Y_Batches[idx]
return self.X_Batches, self.Y_Batches

def apply_structN2Vmask(self, patch, coords, dims, mask):
"""
Expand All @@ -112,18 +113,18 @@ def apply_structN2Vmask(self, patch, coords, dims, mask):
# return x_val_structN2V, indexing_structN2V
@staticmethod
def __subpatch_sampling2D__(X, X_Batches, indices, range, shape):
for j in indices:
for i, j in enumerate(indices):
y_start = np.random.randint(0, range[0] + 1)
x_start = np.random.randint(0, range[1] + 1)
X_Batches[j] = np.copy(X[j, y_start:y_start + shape[0], x_start:x_start + shape[1]])
X_Batches[i] = np.copy(X[j, y_start:y_start + shape[0], x_start:x_start + shape[1]])

@staticmethod
def __subpatch_sampling3D__(X, X_Batches, indices, range, shape):
for j in indices:
for i, j in enumerate(indices):
z_start = np.random.randint(0, range[0] + 1)
y_start = np.random.randint(0, range[1] + 1)
x_start = np.random.randint(0, range[2] + 1)
X_Batches[j] = np.copy(X[j, z_start:z_start + shape[0], y_start:y_start + shape[1], x_start:x_start + shape[2]])
X_Batches[i] = np.copy(X[j, z_start:z_start + shape[0], y_start:y_start + shape[1], x_start:x_start + shape[2]])

@staticmethod
def __get_stratified_coords2D__(coord_gen, box_size, shape):
Expand Down
2 changes: 1 addition & 1 deletion n2v/internals/n2v_losses.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import keras.backend as K
import tensorflow.keras.backend as K

import tensorflow as tf

Expand Down
4 changes: 1 addition & 3 deletions n2v/models/n2v_config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import argparse

import keras.backend as K
import tensorflow.keras.backend as K

from csbdeep.models import BaseConfig
from csbdeep.utils import _raise, axes_check_and_normalize, axes_dict, backend_channels_last

from six import string_types

import numpy as np
from logging.config import BaseConfigurator

# This class is a adapted version of csbdeep.models.config.py.
class N2VConfig(argparse.Namespace):
Expand Down
Loading

0 comments on commit 1c50f5b

Please sign in to comment.