Git Product home page Git Product logo

yingkaisha / keras-unet-collection Goto Github PK

View Code? Open in Web Editor NEW
636.0 13.0 181.0 982 KB

The Tensorflow, Keras implementation of U-net, V-net, U-net++, UNET 3+, Attention U-net, R2U-net, ResUnet-a, U^2-Net, TransUNET, and Swin-UNET with optional ImageNet-trained backbones.

License: MIT License

Python 100.00%
unet attention-unet unet-plusplus r2u-net resunet u2net unet-threeplus pypi tensorflow vnet

keras-unet-collection's Introduction

keras-unet-collection

PyPI version PyPI license Maintenance

DOI

The tensorflow.keras implementation of U-net, V-net, U-net++, UNET 3+, Attention U-net, R2U-net, ResUnet-a, U^2-Net, TransUNET, and Swin-UNET with optional ImageNet-trained backbones.


keras_unet_collection.models contains functions that configure keras models with hyper-parameter options.

  • Pre-trained ImageNet backbones are supported for U-net, U-net++, UNET 3+, Attention U-net, and TransUNET.
  • Deep supervision is supported for U-net++, UNET 3+, and U^2-Net.
  • See the User guide for other options and use cases.
keras_unet_collection.models Name Reference
unet_2d U-net Ronneberger et al. (2015)
vnet_2d V-net (modified for 2-d inputs) Milletari et al. (2016)
unet_plus_2d U-net++ Zhou et al. (2018)
r2_unet_2d R2U-Net Alom et al. (2018)
att_unet_2d Attention U-net Oktay et al. (2018)
resunet_a_2d ResUnet-a Diakogiannis et al. (2020)
u2net_2d U^2-Net Qin et al. (2020)
unet_3plus_2d UNET 3+ Huang et al. (2020)
transunet_2d TransUNET Chen et al. (2021)
swin_unet_2d Swin-UNET Hu et al. (2021)

Note: the two Transformer models are incompatible with Numpy 1.20; NumPy 1.19.5 is recommended.


keras_unet_collection.base contains functions that build the base architecture (i.e., without model heads) of Unet variants for model customization and debugging.

keras_unet_collection.base Notes
unet_2d_base, vnet_2d_base, unet_plus_2d_base, unet_3plus_2d_base, att_unet_2d_base, r2_unet_2d_base, resunet_a_2d_base, u2net_2d_base, transunet_2d_base, swin_unet_2d_base Functions that accept an input tensor and hyper-parameters of the corresponded model, and produce output tensors of the base architecture.

keras_unet_collection.activations and keras_unet_collection.losses provide additional activation layers and loss functions.

keras_unet_collection.activations Name Reference
GELU Gaussian Error Linear Units (GELU) Hendrycks et al. (2016)
Snake Snake activation Liu et al. (2020)
keras_unet_collection.losses Name Reference
dice Dice loss Sudre et al. (2017)
tversky Tversky loss Hashemi et al. (2018)
focal_tversky Focal Tversky loss Abraham et al. (2019)
ms_ssim Multi-scale Structural Similarity Index loss Wang et al. (2003)
iou_seg Intersection over Union (IoU) loss for segmentation Rahman and Wang (2016)
iou_box (Generalized) IoU loss for object detection Rezatofighi et al. (2019)
triplet_1d Semi-hard triplet loss (experimental)
crps2d_tf CRPS loss (experimental)

Installation and usage

pip install keras-unet-collection

from keras_unet_collection import models
# e.g. models.unet_2d(...)
  • Note: Currently supported backbone models are: VGG[16,19], ResNet[50,101,152], ResNet[50,101,152]V2, DenseNet[121,169,201], and EfficientNetB[0-7]. See Keras Applications for details.

  • Note: Neural networks produced by this package may contain customized layers that are not part of the Tensorflow. It is reommended to save and load model weights.

  • Changelog

Examples

  • Jupyter notebooks are provided as examples:

    • Attention U-net with VGG16 backbone [link].

    • UNET 3+ with deep supervision, classification-guided module, and hybrid loss [link].

    • Vision-Transformer-based examples are in progress, and available at keras-vision-transformer

Dependencies

  • TensorFlow 2.5.0, Keras 2.5.0, Numpy 1.19.5.

  • (Optional for examples) Pillow, matplotlib, etc.

Overview

U-net is a convolutional neural network with encoder-decoder architecture and skip-connections, loosely defined under the concept of "fully convolutional networks." U-net was originally proposed for the semantic segmentation of medical images and is modified for solving a wider range of gridded learning problems.

U-net and many of its variants take three or four-dimensional tensors as inputs and produce outputs of the same shape. One technical highlight of these models is the skip-connections from downsampling to upsampling layers, which benefit the reconstruction of high-resolution, gridded outputs.

Contact

Yingkai (Kyle) Sha <[email protected]> <[email protected]>

License

MIT License

Citation

@misc{keras-unet-collection,
  author = {Sha, Yingkai},
  title = {Keras-unet-collection},
  year = {2021},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/yingkaisha/keras-unet-collection}},
  doi = {10.5281/zenodo.5449801}
}

keras-unet-collection's People

Contributors

sitic avatar yingkaisha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

keras-unet-collection's Issues

how to use u_net model on grayscale images

hello dear,
I have a problem when I want to use models on the grayscale images.
I think it is not possible to use
model = models.unet_2d((None, None, 1), [64, 128, 256, 512, 1024], n_labels=2, stack_num_down=2, stack_num_up=1, activation='GELU', output_activation='Softmax', batch_norm=True, pool='max', unpool='nearest', name='unet')
as input image size.
how to solve this problem?
Thanks a lot

hybrid loss implementation in UNET3+ example

Thank you for updating the demo for hybrid loss implementation.

Are the losses offered in this package designed for multi-class segmentation? I guess they should be different from those binary cases (like overall iou = averaged sum of class iou).
Also the comments for iou_seg indicate that the input should be box coordinates, which is confusing from the demo since the model output is tensor of probabilities with length of n_labels.

how to visualize layers

Thanks for your awesome library, it is absolutely a time saver. I have a question I want to visualize layers in the networks (feature maps)? Do you have any plan to develop this feature?

size matter

hello
A question
I have trained swin net model for segmentation and my image size was 64643 ;
Can I use (prediction) this model (model and it's weights) when I have 2562563 or 256256 without resizing it to 6464*3 or not?
If answer is yet, how?

I tried but couldn't find a way except size decreasing and this will harm the resolution ?
I will be appreciated if you help me
Thanks ,

tensorflow:Gradients do not exist for variables

I try to train my dataset using oxford-iiit.ipynb; but I get this warning and it doesnt calculate validation performance

    Initial loss = 0.0
    WARNING:tensorflow:Gradients do not exist for variables ['unet3plus_down_from4_to0_0/kernel:0', 'unet3plus_down_from4_to1_0/kernel:0', 'unet3plus_down_from4_to2_0/kernel:0', 'unet3plus_down_from4_to3_0/kernel:0', 'unet3plus_down_from4_to4_0/kernel:0', 'unet3plus_down_from4_to0_0_bn/gamma:0', 'unet3plus_down_from4_to0_0_bn/beta:0', 'unet3plus_down_from4_to1_0_bn/gamma:0', 'unet3plus_down_from4_to1_0_bn/beta:0', 'unet3plus_down_from4_to2_0_bn/gamma:0', 'unet3plus_down_from4_to2_0_bn/beta:0', 'unet3plus_down_from4_to3_0_bn/gamma:0', 'unet3plus_down_from4_to3_0_bn/beta:0', 'unet3plus_down_from4_to4_0_bn/gamma:0', 'unet3plus_down_from4_to4_0_bn/beta:0', 'unet3plus_fusion_conv_4_0/kernel:0', 'unet3plus_fusion_conv_4_0_bn/gamma:0', 'unet3plus_fusion_conv_4_0_bn/beta:0', 'unet3plus_output_final/kernel:0', 'unet3plus_output_final/bias:0'] when minimizing the loss.
    WARNING:tensorflow:Gradients do not exist for variables ['unet3plus_down_from4_to0_0/kernel:0', 'unet3plus_down_from4_to1_0/kernel:0', 'unet3plus_down_from4_to2_0/kernel:0', 'unet3plus_down_from4_to3_0/kernel:0', 'unet3plus_down_from4_to4_0/kernel:0', 'unet3plus_down_from4_to0_0_bn/gamma:0', 'unet3plus_down_from4_to0_0_bn/beta:0', 'unet3plus_down_from4_to1_0_bn/gamma:0', 'unet3plus_down_from4_to1_0_bn/beta:0', 'unet3plus_down_from4_to2_0_bn/gamma:0', 'unet3plus_down_from4_to2_0_bn/beta:0', 'unet3plus_down_from4_to3_0_bn/gamma:0', 'unet3plus_down_from4_to3_0_bn/beta:0', 'unet3plus_down_from4_to4_0_bn/gamma:0', 'unet3plus_down_from4_to4_0_bn/beta:0', 'unet3plus_fusion_conv_4_0/kernel:0', 'unet3plus_fusion_conv_4_0_bn/gamma:0', 'unet3plus_fusion_conv_4_0_bn/beta:0', 'unet3plus_output_final/kernel:0', 'unet3plus_output_final/bias:0'] when minimizing the loss.
    Validation performance 0.0 is NOT improved
    Validation performance 0.0 is NOT improved
    Validation performance 0.0 is NOT improved
    Validation performance 0.0 is NOT improved
    Validation performance 0.0 is NOT improved
    Early stopping

hybridloss

is there plans to implement hybridloss?

Patch Embedding Dimension in TransUNet

I think the patch embedding dimensions in your implementation might be larger than the original TransUNet paper.

In the original TransUNet, it is mentioned that "For the “base” model, the hidden size D, number of layers, MLP size, and number of heads are set to be 12, 768, 3072, and 12, respectively".
The hidden size D is the embedding dimension of the transformer's output which is set to 12 (unless the authors made a mistake in the order of the numbers). However, in your implementation, it is set to 768. Am I missing something?

error on my dataset for unet3+

I am a beginner. Help me solve the error I got for Unet 3+.

InvalidArgumentError Traceback (most recent call last)
in ()
14 temp_out = unet3plus.predict([valid_input])
15 y_pred = temp_out[-1]
---> 16 record = np.mean(hybrid_loss(valid_target, y_pred))
17 print('\tInitial loss = {}'.format(record))
18

9 frames
/usr/local/lib/python3.7/dist-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: Incompatible shapes: [87736320] vs. [1032192] [Op:Mul]

UNET with transformer

您好,请问是否有计划添加基于transformer的UNET架构?如TransUNET或Swin-UNET(这个貌似还没开源)等。谢谢。

It seems to be running on the CPU, not the GPU

when i use
net=models.swin_unet_2d(input_size=(224,224,1,),filter_num_begin=96,n_labels=2,depth=4,stack_num_down=2,stack_num_up=2,
patch_size=(4,4),num_heads=[3, 6, 12, 24],window_size=[7, 7, 7, 7],num_mlp=1024)

it can not run on gpu
how can i set it on gpu

GCM

What does the axis = -1 does ?

# Classification-guided Module (CGM)
# ---------------------------------------- #
# dropout --> 1-by-1 conv2d --> global-maxpooling --> sigmoid
X_CGM = X_decoder[-1]
X_CGM = Dropout(rate=0.1)(X_CGM)
X_CGM = Conv2D(filter_num_skip[-1], 1, padding='same')(X_CGM)
X_CGM = GlobalMaxPooling2D()(X_CGM)
X_CGM = Activation('sigmoid')(X_CGM)

CGM_mask = max(X_CGM, axis=-1) # <----- This value could be trained with "none-organ image"

for i in range(len(OUT_stack)):
    if i < len(OUT_stack)-1:
        OUT_stack[i] = multiply([OUT_stack[i], CGM_mask], name='{}_output_sup{}_CGM'.format(name, i))
    else:
        OUT_stack[i] = multiply([OUT_stack[i], CGM_mask], name='{}_output_final_CGM'.format(name))

Is this line of code predicting mask or image? Is there way to exclude images that just have background?

CGM_mask = max(X_CGM, axis=-1) # <----- This value could be trained with "none-organ image"

If I'm understanding correctly, the GCM is used to predict if a image blank or not?

saving model error

For models.swin_unet_2d:
ValueError: '_linear_trans1/' is not a valid scope name.
(It seems the name cannot contain '_')

detailed information:
WARNING:tensorflow:Skipping full serialization of Keras layer <tensorflow.python.keras.layers.convolutional.Conv2D object at0x7f5480505eb8>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <tensorflow.python.keras.layers.convolutional.Conv2D object at0x7f5480417128>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <tensorflow.python.keras.layers.convolutional.Conv2D object at0x7f54803d8b38>, because it is not built.
WARNING:tensorflow:Skipping full serialization of Keras layer <tensorflow.python.keras.layers.convolutional.Conv2D object at0x7f548037ebe0>, because it is not built.
Traceback (most recent call last):
File "unet_plus_binary.py", line 368, in
callbacks=snapshot.get_callbacks())
File "lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1957, in fit_generator
initial_epoch=initial_epoch)
File "lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1229, in fit
callbacks.on_epoch_end(epoch, epoch_logs)
...
File "lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 4275, in name_scope
raise ValueError("'%s' is not a valid scope name" % name)

Attention U-Net on multiple classes

I have 6 classes semantic segmentation problem. How attention u-net in this package considers a particular class? How to give more attention to a particular class?

Examples

Can you provide an example how to use backbones? Is there a list of backbones supported?

After update, error in import module

After update to 0.1.0, a Google Colab show the following error:

ModuleNotFoundError: No module named 'keras_unet_collection'.

The version of TensorFlow and Keras installed is 2.5.0.

Imagenet backbones

Does this example supports imagenet backbones?

name = 'unet3plus'
activation = 'ReLU'
filter_num_down = [32, 64, 128, 256, 512]
filter_num_skip = [32, 32, 32, 32]
filter_num_aggregate = 160

stack_num_down = 2
stack_num_up = 1
n_labels = 3

# `unet_3plus_2d_base` accepts an input tensor 
# and produces output tensors from different upsampling levels
# ---------------------------------------- #
input_tensor = keras.layers.Input((128, 128, 3))
# base architecture
X_decoder = base.unet_3plus_2d_base(
    input_tensor, filter_num_down, filter_num_skip, filter_num_aggregate, 
    stack_num_down=stack_num_down, stack_num_up=stack_num_up, activation=activation, 
    batch_norm=True, pool=True, unpool=True, backbone=None, name=name)

vnet_2d: Inputs have incompatible shapes. Received shapes (32, 32, 512) and (32, 32, 256)

I tested all of the models and they all worked fine, except the vnet_2d:

model.vnet_2d((128, 128, 3), filter_num=[64, 128, 256, 512], n_labels=7,
res_num_ini=1, res_num_max=3, activation='ReLU',
output_activation="Softmax",
batch_norm=True, pool=True, unpool=True,
name='vnet')

Traceback (most recent call last):
File "C:\Users\mehra\anaconda3\envs\gis2\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
File "C:\Users\mehra\OneDrive\Desktop\deep_learning\tf_keras\semantic_segmentation\model.py", line 116, in model_application
base_model = unet_collection(pixels, n_classes, activation, backbone_model_name, main_model_name, weights)
File "C:\Users\mehra\OneDrive\Desktop\deep_learning\tf_keras\semantic_segmentation\models_code\unet_collection.py", line 78, in unet_collection
base_model = kuc_model.vnet_2d((pixels, pixels, 3), filter_num=[64, 128, 256, 512], n_labels=n_classes,
File "C:\Users\mehra\anaconda3\envs\gis2\lib\site-packages\keras_unet_collection_model_vnet_2d.py", line 225, in vnet_2d
X = vnet_2d_base(X, filter_num, res_num_ini=res_num_ini, res_num_max= res_num_max,
File "C:\Users\mehra\anaconda3\envs\gis2\lib\site-packages\keras_unet_collection_model_vnet_2d.py", line 165, in vnet_2d_base
X = vnet_right(X, [X_skip[i],], f, res_num=res_num_list[i],
File "C:\Users\mehra\anaconda3\envs\gis2\lib\site-packages\keras_unet_collection_model_vnet_2d.py", line 82, in vnet_right
X = Res_CONV_stack(X, X_skip, channel, res_num, activation=activation,
File "C:\Users\mehra\anaconda3\envs\gis2\lib\site-packages\keras_unet_collection\layer_utils.py", line 271, in Res_CONV_stack
X = add([X_skip, X], name='{}_add'.format(name))
File "C:\Users\mehra\anaconda3\envs\gis2\lib\site-packages\keras\layers\merge.py", line 791, in add
return Add(**kwargs)(inputs)
File "C:\Users\mehra\anaconda3\envs\gis2\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\mehra\anaconda3\envs\gis2\lib\site-packages\keras\layers\merge.py", line 78, in _compute_elemwise_op_output_shape
raise ValueError(
ValueError: Inputs have incompatible shapes. Received shapes (32, 32, 512) and (32, 32, 256)

how to add recurrent iterations in different models?

Hi, Thanks again for your nice library and sorry I'm sorry about asking many questions. I have a question is it possible to add recurrent iteration to the different models? for example, I want to add recurrent iterations to the decoder part of trans U-net or to the attention u-net to improve results. how it is possible?
Thanks again.

patch size on TransUNet

Hello, I want to ask about the patch size on your code
why do you make it 1 x 1?

I think, in the original paper, the author made it 16 x 16 as the default

Thanks

Eldi

关于模型文件

你好,我想问下,您的这个仓库中的是没有已经训练好的模型文件吗?如果我想使用的话,是不是自己重新训练。

Transunet for different image size (256x256)

I tried to run the Transunet model for image segmentation on 256x256 images. This is the configuration I have used:
model = models.transunet_2d((256, 256, 3), filter_num=[64, 128, 256, 512], n_labels=7, stack_num_down=2, stack_num_up=2, proj_dim=768, num_mlp=3072, num_heads=12, num_transformer=12, activation='ReLU', mlp_activation='GELU', output_activation='Softmax', batch_norm=True, pool=True, unpool='bilinear', name='transunet')
However the following error appears:
in quick_execute inputs, attrs, num_outputs) tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [16,4096,768] vs. [1024,768] [[node gradient_tape/transunet_model/vi_t_embedding/add/BroadcastGradientArgs (defined at transunet_test.py:18) ]] [Op:__inference_train_function_18605]

I have tried to change the size of filters, but it produces the same "incompatible shapes" error but with different values and it always a factor of 4. Another solution I tried is to work with a 512x512 but again the same problem.

Shapes incompatible when testing Unet++ on COCO

I was using your Image segmentation using customized UNET 3+ and hybrid loss and user_guide_models notebook " to train with Unet_plus_2d model on COCO.
but during training,I got the following error
Shapes (2069, 128, 128, 2) and (4, 2069, 128, 128, 3) are incompatible.
Any idea why this is happening,the only changes i made in the seg attention unet backbone notebook was to change the model
Also in the notebook Image segmentation using customized UNET 3+ and hybrid loss
while displaying the images,why is the image so grainy and what can be done to resolve it

ValueError: Dimensions must be equal

I get this error when I try to run the unet3plus with 512,512,3 as the input.

ValueError: Dimensions must be equal, but are 512 and 64 for '{{node binary_crossentropy_2/mul}} = Mul[T=DT_FLOAT](replicated_input_2, binary_crossentropy_2/Log)' with input shapes: [8,512,512,1], [8,64,64,1].

Parameter 'output_activation' ignored when 'deep_supervision' is False.

Hi,
First of all - thank you for this great model collection.
Maybe this is intended by design but:
base_model = models.unet_3plus_2d(input_shape, n_labels=19, filter_num_down=[64, 128, 256, 512, 1026], filter_num_skip=[64, 64, 64, 64], filter_num_aggregate=256, stack_num_down=2, stack_num_up=1, activation='ReLU', output_activation='Sigmoid', batch_norm=True, pool='max', unpool=False, deep_supervision=False, name='unet3plus')
then the call
base_model.layers[-1].__class__
returns
tensorflow.python.keras.layers.advanced_activations.ReLU
I'd rather expected a 'sigmoid' activation.

input output issue

Intro:
in image denoising encoder-decoder networks are perfect like auto-encoders and u-net networks have same property, but you design your network just for image segmentation.
issue:
for your network: input is RGB image and out put is binary mask, so that network will train and learn how to segment special part of image.
my error: I have noisy RGB image as input and RGB clean image as output(here as mask) so that my network k will learn how to clear noise from image.
my question: how can I change network in a way that accept both input and output in RGB (None,None,3)?

ResourceExhaustedError: Swin Unet

I have a 6 class semantic segmentation problem. I am trying to use SwinUNet as follows
models.swin_unet_2d((512, 512, 3), filter_num_begin=64, n_labels=6, depth=4, stack_num_down=2, stack_num_up=2, patch_size=(2, 2), num_heads=[4, 8, 8, 8], window_size=[4, 2, 2, 2], num_mlp=512, output_activation='Softmax', shift_window=True, name='swin_unet')

But getting below error

ResourceExhaustedError: 2 root error(s) found.
(0) Resource exhausted: OOM when allocating tensor with shape[8,65536,64] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
[[node swin_unet_model/swin_transformer_block_15/name1_norm2/batchnorm/mul_2 (defined at /opt/conda/lib/python3.7/site-packages/keras_unet_collection/transformer_layers.py:623) ]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.

 [[gradient_tape/swin_unet_model/patch_embedding_2/embedding_2/embedding_lookup/Reshape/_734]]

Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.

(1) Resource exhausted: OOM when allocating tensor with shape[8,65536,64] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
[[node swin_unet_model/swin_transformer_block_15/name1_norm2/batchnorm/mul_2 (defined at /opt/conda/lib/python3.7/site-packages/keras_unet_collection/transformer_layers.py:623) ]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.

0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_152577]

Function call stack:
train_function -> train_function
`
Please help.

"NameError: name 'keras' is not defined" when loading model weights (transunet, swin-unet)

I've saved the trained model, using "model.save(filepath, save_traces=True)" like in the guide. But when I try to load the model weights using "utils.dummy_loader" I get this error and I've already imported keras. Anybody got a solution for that? Or generally to load models/model weights.

Edit: I solved the load problem using "custom_objects" to import custom layers with load_model.

Bug in _model_unet_3plus_2d.py and small suggestion

File _model_unet_3plus_2d.py line 128
current
X = UNET_left(X, filter_num[i_real], stack_num=stack_num_down, activation=activation, pool=pool, batch_norm=batch_norm, name='{}_down{}'.format(name, i_real+1))
should rather be
X = UNET_left(X, filter_num_down[i_real], stack_num=stack_num_down, activation=activation, pool=pool, batch_norm=batch_norm, name='{}_down{}'.format(name, i_real+1))

And small enhancement suggestion:
I'm writing classification model based on unet_3plus_2d.
It would be great to have X_decoder as output from model to be able to construct GCM (controlled by input parameter: return_decoder)

model = unet_3plus_2d(input_shape, .... return_decoder=True, name='unet3plus')
then
[OUT_stack, X_decoder] = model.output
Classification-guided Module (CGM)
dropout --> 1-by-1 conv2d --> global-maxpooling --> sigmoid
X_CGM = X_decoder[-1]
X_CGM = Dropout(rate=0.1)(X_CGM)
X_CGM = Conv2D(filter_num_skip[-1], 1, padding='same')(X_CGM)
...

Your opinion?

Exemple 3 in the user guide

The code for the third exemple in the user guide (attention-Unet for single target regression) does not work for me. When I train the model, I get this error : "

ValueError: Negative dimension size caused by subtracting 2 from 1 for '{{node attunet_model/attunet_down1_encode_stride_conv/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true](attunet_model/attunet_down0_1_activation/Relu, attunet_model/attunet_down1_encode_stride_conv/Conv2D/ReadVariableOp)' with input shapes: [?,1,1,64], [2,2,64,128]

"
I created a training matrix of 3 variables a vector for the labels and then added 2 other dimensions so it can be processed by the unet.Maybe that is the problem.

train_labels = default_rng(42).random((386))
train_specs = default_rng(42).random((386,3))
train_specs = tf.expand_dims(train_specs, axis=1)
train_specs = tf.expand_dims(train_specs, axis=1)

thanks for your efforts.

filter_num in TransUNet

Hi,

In TransUNet, filter_num shows the number of filters for down and upsampling levels, right? However, If I use 3 filters filter_num=[64, 128, 128] instead of the default filter_num=[64, 128, 128, 512], the number of parameters of the network increases, and I get an OOM error. Is this a bug or am I missing something?

NotImplementedError when using numpy version > 1.19.5

File "D:\Work\keras-unet-collection-main\train.py", line 49, in
model = models.transunet_2d((512, 512, 3), filter_num=[64, 128, 256, 512], n_labels=12, stack_num_down=2,
File "D:\Work\keras-unet-collection-main\keras_unet_collection_model_transunet_2d.py", line 339, in transunet_2d
X = transunet_2d_base(IN, filter_num, stack_num_down=stack_num_down, stack_num_up=stack_num_up,
File "D:\Work\keras-unet-collection-main\keras_unet_collection_model_transunet_2d.py", line 234, in transunet_2d_base
X = ViT_block(X, num_heads, key_dim, filter_num_MLP, activation=mlp_activation,
File "D:\Work\keras-unet-collection-main\keras_unet_collection_model_transunet_2d.py", line 76, in ViT_block
V_atten = LayerNormalization(name='{}_layer_norm_1'.format(name))(V_atten)
File "C:\Users\12809.conda\envs\tf_env2\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 969, in call
return self._functional_construction_call(inputs, args, kwargs,
File "C:\Users\12809.conda\envs\tf_env2\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1107, in _functional_construction_call
outputs = self._keras_tensor_symbolic_call(
File "C:\Users\12809.conda\envs\tf_env2\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 840, in _keras_tensor_symbolic_call
return self._infer_output_signature(inputs, args, kwargs, input_masks)
File "C:\Users\12809.conda\envs\tf_env2\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 880, in _infer_output_signature
outputs = call_fn(inputs, *args, **kwargs)
File "C:\Users\12809.conda\envs\tf_env2\lib\site-packages\tensorflow\python\keras\layers\normalization.py", line 1275, in call
scale = array_ops.ones([pre_dim], dtype=self.dtype)
File "C:\Users\12809.conda\envs\tf_env2\lib\site-packages\tensorflow\python\util\dispatch.py", line 206, in wrapper
return target(*args, **kwargs)
File "C:\Users\12809.conda\envs\tf_env2\lib\site-packages\tensorflow\python\ops\array_ops.py", line 3212, in ones
output = _constant_if_small(one, shape, dtype, name)
File "C:\Users\12809.conda\envs\tf_env2\lib\site-packages\tensorflow\python\ops\array_ops.py", line 2896, in _constant_if_small
if np.prod(shape) < 1000:
File "<array_function internals>", line 5, in prod
File "C:\Users\12809.conda\envs\tf_env2\lib\site-packages\numpy\core\fromnumeric.py", line 3030, in prod
return _wrapreduction(a, np.multiply, 'prod', axis, dtype, out,
File "C:\Users\12809.conda\envs\tf_env2\lib\site-packages\numpy\core\fromnumeric.py", line 87, in _wrapreduction
return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
File "C:\Users\12809.conda\envs\tf_env2\lib\site-packages\tensorflow\python\framework\ops.py", line 867, in array
raise NotImplementedError(
NotImplementedError: Cannot convert a symbolic Tensor (transunet_ViT_0_layer_norm_1/mul_1:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

I'm using numpy version of 1.19.5 but numpy-base version above 1.20 and I meet this error.

Model saving issue for TransUNET

Hello, I got following error messages when training with TransUNET (with checkpoint saving callback):

WARNING:tensorflow:Model failed to serialize as JSON. Ignoring... Layer ViT_patch_gen has arguments in __init__ and therefore must override get_config.
NotImplementedError: Layer ViT_patch_gen has arguments in __init__ and therefore must override get_config.

Looks like Keras failed to save the model since it can't load it:
https://stackoverflow.com/questions/58678836/notimplementederror-layers-with-arguments-in-init-must-override-get-conf

Also at the beginning of 2nd epoch the training loop would break with:
W tensorflow/core/kernels/data/generator_dataset_op.cc:107] Error occurred when finalizing GeneratorDataset iterator: Failed precondition: Python interpreter state is not initialized. The process may be terminated.
I wrote my own data generator to load image tiles and it works fine with your UNET3+ or attUNET.
I tried a smaller batch size but it's not working, somehow not sure if this is related to the model source code.

Please let me know if you need more details.

Many thanks.

I have a question about the shapes incompatible error.

I downloaded the COCO dataset and tested it as in the example.
I create a dataset and an error occurs in the train code.

...
record = np.mean(keras.losses.categorical_crossentropy(valid_label, y_pred))
...
loss_ = model.train_on_batch([train_input,], [train_label,])

Error : Shapes (2069, 128, 128, 2) and (2069, 128, 128, 3) are incompatible.

How do I solve it?

Multiclass input

Hello, I'm wondering how to do multiclass classification with Unet. I saw that a previous hot encode step is needed. My dataset consists in two classes + background. Thanks in advance!

ValueError: Input 0 is incompatible with layer model_1: expected shape=(None, 512, 512, 3), found shape=(None, 512, 512, 64)

I am getting this error when I added custom loss

input shape(512,512,3)
model=models.unet_2d(input_size, filter_num, n_labels=1, stack_num_down=2, stack_num_up=2,
activation='ReLU', output_activation=None, batch_norm=True, pool='max', unpool='nearest',
backbone='VGG16', weights='imagenet', freeze_backbone=True, freeze_batch_norm=True, name='unet')

////////
selected_layers = ['block1_conv1', 'block2_conv2',"block3_conv3" ]
selected_layer_weights = [0.65, 0.3 , 0.05 ]

vgg = tf.keras.applications.VGG16(weights='imagenet', include_top=False, input_shape=input_size)
vgg.trainable = False
outputs = [vgg.get_layer(l).output for l in selected_layers]
model = tf.keras.Model(vgg.input, outputs)

@tf.function
def perceptual_loss(input_image , reconstruct_image):
h1_list = model(input_image)
h2_list = model(reconstruct_image)

rc_loss = 0.0
for h1, h2, weight in zip(h1_list, h2_list, selected_layer_weights):
    h1 = K.batch_flatten(h1)
    h2 = K.batch_flatten(h2)
    rc_loss = rc_loss + weight * K.sum(K.square(h1 - h2), axis=-1)

return rc_loss

///////
model.compile(loss=perceptual_loss, optimizer=keras.optimizers.SGD(lr=1e-2),metrics=[tf.keras.metrics.MeanAbsoluteError(),tf.keras.metrics.MeanSquaredError()])

pretrained weight='imagenet'
Backbone=VGG16

How to resolve this

CGM模块训练

Hello,请问下UNET3+ example里CGM值应该如何训练?论文里好像也没见详细描述。是需要用数据集里的阴性样本去单独训练模型吗?谢谢。

Preprocessing in examples

If using the, for example, VGG19 backend, the corresponding keras guide recommends applying the preprocessing from here:

Note: each Keras Application expects a specific kind of input preprocessing. For VGG19, call tf.keras.applications.vgg19.preprocess_input on your inputs before passing them to the model. vgg19.preprocess_input will convert the input images from RGB to BGR, then will zero-center each color channel with respect to the ImageNet dataset, without scaling.

The examples show only rescaling [0, 255] to [0, 1], when, in actual fact, this is nothing like the images the backend networks were trained on. When using the correct scaling (i.e. via preprocess_input) training speed is fastly improved, at least in my experience.

UNET 3plus not learning on large datasets

I am trying to use the UNET 3plus with deep supervision and cgm for nodule segmentation from lung ct scans. When I train the model on a 1000 image subset of the dataset the model learns and outputs meaningful segmentation masks.

However, when I train on a larger dataset of 2000 or more images the model does not learn and outputs a mask of all 0s always.

I have tried increasing and reducing the learning rate as well changing optimizers and loss functions but the issue remains. I also tried the att_unet_2d model and had the same issue.

I have attached my code:
training_code.zip

how to use more than 3 channel input

is it possible to use more than 3 channels as input? I want to concentrate on different images transforming and creating more than 3 channels. I used that method and put input_size=(512,512,4) but I got the error, is there any way I could use more than 3 channels?

image

Question regarding the U2-Net architecture

Hello yingkaisha,

thanks for all the implementations, it really helps people try out different U-Nets.

I tried your U2-Net implementation and ran it with the parametes in your documentation, i.e.

u2net = models.u2net_2d((None, None, 3), n_labels=2, 
                        filter_num_down=[64, 128, 256, 512],
                        activation='ReLU', output_activation='Sigmoid', 
                        batch_norm=True, pool=True, unpool=True, deep_supervision=True, name='u2net')

and it works fine. However I don't get why I get output as follows:

...
    32	1430/1430 [==============================] - 994s 695ms/step - loss: 1.6761
- u2net_output_sup0_activation_loss: 0.2554
- u2net_output_sup1_activation_loss: 0.2537
- u2net_output_sup2_activation_loss: 0.2599
- u2net_output_sup3_activation_loss: 0.2854
- u2net_output_sup4_activation_loss: 0.3481
- u2net_output_final_activation_loss: 0.2735
- u2net_output_sup0_activation_accuracy: 0.9073
- u2net_output_sup1_activation_accuracy: 0.9073
- u2net_output_sup2_activation_accuracy: 0.9047
- u2net_output_sup3_activation_accuracy: 0.8932
- u2net_output_sup4_activation_accuracy: 0.8651
- u2net_output_final_activation_accuracy: 0.9081
- val_loss: 1.7999
- val_u2net_output_sup0_activation_loss: 0.2733
- val_u2net_output_sup1_activation_loss: 0.2743
- val_u2net_output_sup2_activation_loss: 0.2821
- val_u2net_output_sup3_activation_loss: 0.3056
- val_u2net_output_sup4_activation_loss: 0.3655
- val_u2net_output_final_activation_loss: 0.2991
- val_u2net_output_sup0_activation_accuracy: 0.8993
- val_u2net_output_sup1_activation_accuracy: 0.8998
- val_u2net_output_sup2_activation_accuracy: 0.8971
- val_u2net_output_sup3_activation_accuracy: 0.8876
- val_u2net_output_sup4_activation_accuracy: 0.8614
- val_u2net_output_final_activation_accuracy: 0.9002
...

This is just (verbose) reporting from the Keras-API about all the outputs (sup0 to sup4 and final for training and validation), but I am wondering where sup5 is gone as the original paper seems to have 7 outputs and I think you don't add sup5(?) to the output?

At the beginning of the training one gets the following report:

...
The depth of u2net_2d = len(filter_num_down) + len(filter_4f_num) = 6
----------
deep_supervision = True
names of output tensors are listed as follows (the last one is the final output):
    u2net_output_sup0_activation
    u2net_output_sup1_activation
    u2net_output_sup2_activation
    u2net_output_sup3_activation
    u2net_output_sup4_activation
    u2net_output_sup5_activation
    u2net_output_final_activation
,,,

but it seems that sup5 is not in the output stack here. The range goes to L_out-1 so the last output is missing?

So if you find the time I would be happy for an explanation.

Cheers.

Selecting the final output layer for predictionUnet3plus for

I was wondering how can I select the final output layer for prediction when using deep supervision (w/ efficientnet backbone)? I get this value error.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-61daffd1399c> in <module>
    89 
    90             pred = cv2.resize(pred, (WINDOW, WINDOW))
---> 91             preds[x1:x2,y1:y2] += (pred > THRESHOLD).astype(np.uint8)
    92 
    93 

ValueError: operands could not be broadcast together with shapes (1024,1024) (1024,1024,256) (1024,1024) 

configuration for more deep model

Hi @yingkaisha ,
I am testing the models with a more complex problem. So I tried to increase depth of swin unet model with the following code:
model = swin_unet_2d(input_shape, filter_num_begin=64, n_labels=2, depth=8, stack_num_down=4, stack_num_up=4, patch_size=(2, 2), num_heads=[4, 8, 8, 8,8, 8, 8,16], window_size=[4, 2, 2, 2,2, 2, 2,2], num_mlp=512, utput_activation='Softmax', shift_window=True, name='swin_unet')
The following error occurred.

ValueError: Dimensions must be equal, but are 4 and 1 for '{{node swin_transformer_block_28/window_attention_28/matmul_1}} = BatchMatMulV2[T=DT_FLOAT, adj_x=false, adj_y=false](swin_transformer_block_28/window_attention_28/dropout_84/Identity, swin_transformer_block_28/window_attention_28/strided_slice_2)' with input shapes: [?,16,4,4], [?,16,1,512].

What is wrong with my configuration?

Need Dropout Layer in UNet and UNet++

Here is an example code for the dropout layer:

def GiveMeUnet(inputImage, numFilters = 16, droupouts = 0.1, doBatchNorm = True):
    # defining encoder Path
    c1 = Conv2dBlock(inputImage, numFilters * 1, kernelSize = 3, doBatchNorm = doBatchNorm)
    p1 = tf.keras.layers.MaxPooling2D((2,2))(c1)
    p1 = tf.keras.layers.Dropout(droupouts)(p1)
    
    c2 = Conv2dBlock(p1, numFilters * 2, kernelSize = 3, doBatchNorm = doBatchNorm)
    p2 = tf.keras.layers.MaxPooling2D((2,2))(c2)
    p2 = tf.keras.layers.Dropout(droupouts)(p2)
    
    c3 = Conv2dBlock(p2, numFilters * 4, kernelSize = 3, doBatchNorm = doBatchNorm)
    p3 = tf.keras.layers.MaxPooling2D((2,2))(c3)
    p3 = tf.keras.layers.Dropout(droupouts)(p3)
    
    c4 = Conv2dBlock(p3, numFilters * 8, kernelSize = 3, doBatchNorm = doBatchNorm)
    p4 = tf.keras.layers.MaxPooling2D((2,2))(c4)
    p4 = tf.keras.layers.Dropout(droupouts)(p4)
    
    c5 = Conv2dBlock(p4, numFilters * 16, kernelSize = 3, doBatchNorm = doBatchNorm)
    
    # defining decoder path
    u6 = tf.keras.layers.Conv2DTranspose(numFilters*8, (3, 3), strides = (2, 2), padding = 'same')(c5)
    u6 = tf.keras.layers.concatenate([u6, c4])
    u6 = tf.keras.layers.Dropout(droupouts)(u6)
    c6 = Conv2dBlock(u6, numFilters * 8, kernelSize = 3, doBatchNorm = doBatchNorm)
    
    u7 = tf.keras.layers.Conv2DTranspose(numFilters*4, (3, 3), strides = (2, 2), padding = 'same')(c6)
    
    u7 = tf.keras.layers.concatenate([u7, c3])
    u7 = tf.keras.layers.Dropout(droupouts)(u7)
    c7 = Conv2dBlock(u7, numFilters * 4, kernelSize = 3, doBatchNorm = doBatchNorm)
    
    u8 = tf.keras.layers.Conv2DTranspose(numFilters*2, (3, 3), strides = (2, 2), padding = 'same')(c7)
    u8 = tf.keras.layers.concatenate([u8, c2])
    u8 = tf.keras.layers.Dropout(droupouts)(u8)
    c8 = Conv2dBlock(u8, numFilters * 2, kernelSize = 3, doBatchNorm = doBatchNorm)
    
    u9 = tf.keras.layers.Conv2DTranspose(numFilters*1, (3, 3), strides = (2, 2), padding = 'same')(c8)
    u9 = tf.keras.layers.concatenate([u9, c1])
    u9 = tf.keras.layers.Dropout(droupouts)(u9)
    c9 = Conv2dBlock(u9, numFilters * 1, kernelSize = 3, doBatchNorm = doBatchNorm)
    
    output = tf.keras.layers.Conv2D(1, (1, 1), activation = 'sigmoid')(c9)
    model = tf.keras.Model(inputs = [inputImage], outputs = [output])
    return model

transunet_2d

Can't save the model and I get;
NotImplementedError: Layer patch_extract has arguments in __init__ and therefore must override get_config.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.