yu4u / noise2noise

An unofficial and partial Keras implementation of "Noise2Noise: Learning Image Restoration without Clean Data"
MIT License
1.07k stars 237 forks source link

16bit grayscale image #20

Closed Barbetto80 closed 5 years ago

Barbetto80 commented 5 years ago

Hi, I try to modify the code in order to read 16bit gray scale tiff images.

First I directly read the image but I found out that cv2.imread(str(image_path)) correctly read the images but cast the image to 8 bit depth. So I modified cv2.imread(str(image_path)) by adding cv2.imread(str(image_path), cv2.IMREAD_ANYDEPTH)

For some reason with this option image.shape became bidimensional, so I modified h, w = image.shape _ = 1 # i know my images are 1 channel

but i get lost with the next problem: "StopIteration: could not broadcast input array from shape (128,128) into shape (128,128,3)"

yu4u commented 5 years ago

I did not check the code but please try:

x = np.zeros((batch_size, image_size, image_size), dtype=np.uint8)
y = np.zeros((batch_size, image_size, image_size), dtype=np.uint8)

instead of

x = np.zeros((batch_size, image_size, image_size, 3), dtype=np.uint8)
y = np.zeros((batch_size, image_size, image_size, 3), dtype=np.uint8)

Some noise models might not work due to the number of channels.

Barbetto80 commented 5 years ago

Hi,

Thank for your suggestion we made one step ahead, I have made also some other modification to reach my goal but still missing something:

ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (8, 128, 128)

I include my modified python files: noise2noise.zip

Thanks in advance Carlo

yu4u commented 5 years ago

Okey, please try:

image = np.expand_dims(cv2.imread(str(image_path), cv2.IMREAD_ANYDEPTH), -1)
...
x = np.zeros((batch_size, image_size, image_size, 1), dtype=np.uint8)
y = np.zeros((batch_size, image_size, image_size, 1), dtype=np.uint8)
Barbetto80 commented 5 years ago

Yes !!!! It seems to work now, thank you very much. A small recap for the people that can be interested:

----- File generator.py:

  1. Read tif images
#self.image_paths = list(Path(image_dir).glob("*.jpg"))
self.image_paths = list(Path(image_dir).glob("*.tif"))
  1. Fill empty data
#x = np.zeros((batch_size, image_size, image_size, 3), dtype=np.uint8)
#y = np.zeros((batch_size, image_size, image_size, 3), dtype=np.uint8)
x = np.zeros((batch_size, image_size, image_size, 1), dtype=np.uint16)
y = np.zeros((batch_size, image_size, image_size, 1), dtype=np.uint16)
  1. open the image through opencv
#image = cv2.imread(str(image_path))
image = np.expand_dims(cv2.imread(str(image_path), cv2.IMREAD_ANYDEPTH), -1)

and also

#y = cv2.imread(str(image_path))
y = np.expand_dims(cv2.imread(str(image_path), cv2.IMREAD_ANYDEPTH), -1)

----- File model.py

Just change the default SRResNet default channel

#def get_srresnet_model(input_channel_num=3, feature_dim=64, resunit_num=16):
def get_srresnet_model(input_channel_num=1, feature_dim=64, resunit_num=16):

Well thanks again 👍

IsabellLehmann commented 5 years ago

Thanks a lot, I had the same problem and your solution solved it. :)

IsabellLehmann commented 5 years ago

Hi! I was happy too early. After the first 1000 steps, the training broke up and I got the following messages:

File "/net/home/Dokumente/PA/virtualenv/lib/python3.6/site-packages/keras/engine/training.py", line 1418, in fit_generator initial_epoch=initial_epoch) File "/net/home/Dokumente/PA/virtualenv/lib/python3.6/site-packages/keras/engine/training_generator.py", line 234, in fit_generator workers=0) File "/net/home/Dokumente/PA/virtualenv/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper return func(*args, **kwargs) File "/net/home/Dokumente/PA/virtualenv/lib/python3.6/site-packages/keras/engine/training.py", line 1472, in evaluate_generator verbose=verbose) File "/net/home/Dokumente/PA/virtualenv/lib/python3.6/site-packages/keras/engine/training_generator.py", line 346, in evaluate_generator outs = model.test_on_batch(x, y, sample_weight=sample_weight) File "/net/home/Dokumente/PA/virtualenv/lib/python3.6/site-packages/keras/engine/training.py", line 1250, in test_on_batch sample_weight=sample_weight) File "/net/home/Dokumente/PA/virtualenv/lib/python3.6/site-packages/keras/engine/training.py", line 751, in _standardize_user_data exception_prefix='input') File "/net/home/Dokumente/PA/virtualenv/lib/python3.6/site-packages/keras/engine/training_utils.py", line 128, in standardize_input_data 'with shape ' + str(data_shape)) ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (1, 176, 512)

I have no idea, where this shape comes from. I included the changes you said, Barbaretto80, but I use STFTs of speech signals, therefore my dtype is np.float64. Can the type be the reason for the error?

yu4u commented 5 years ago

You might have to do the same thing to ValGenerator:

https://github.com/yu4u/noise2noise/blob/master/generator.py#L63-L65

The shape seems to come from y = y[:(h // 16) * 16, :(w // 16) * 16].

shenmena commented 3 years ago

Hello,

I've implemented Barbetto80's changes as I'm also interested in working with 16bit TIFF images. After running the following line:

python3 train.py --image_dir train --test_dir test --image_size 256 --batch_size 4 --lr 0.001 --output_path gaussian

The script goes through the first 1000 steps of the first epoch but then I get this error:

F tensorflow/stream_executor/cuda/cuda_dnn.cc:91] Check failed: narrow == wide (-1673887744 vs. 2621079552)checked narrowing failed; values not equal post-conversion /usr/local/bin/assign-gpu: line 23: 388324 Aborted (core dumped) /usr/bin/numactl --cpunodebind="${CUDA_VISIBLE_DEVICES}" --membind="${CUDA_VISIBLE_DEVICES}" $@

The gaussian folder is created but is empty.

The training and testing TIFF images are 4700x8742 pixels and 78MB in size.

I was wondering if you could help with this. Thanks!