Home > Mobile >  R keras concatenate layers in U-net
R keras concatenate layers in U-net

Time:09-28

I cannot make keras::layer_concatenate work when I want to concatenate the current layer and a previous layer specified in the model. For example:

model = keras::keras_model_sequential(input_shape = inputShape, 
                                      batch_size = batchSize)

keras::layer_conv_2d(model, filters = 32, kernel_size = list(3L, 3L),
                     padding = "same", activation = "relu", name = "conv1")

keras::layer_max_pooling_2d(model, pool_size = c(2L, 2L), name = "pool1")

up1 = keras::layer_upsampling_2d(model, size = c(2L, 2L), name = "tmpUp1")

# Now I want to concatenate tmpUp1 and conv1:
keras::layer_concatenate(list(model$get_layer("conv1"), 
                              model$get_layer("tmpUp1")))

# Error in py_call_impl(callable, dots$args, dots$keywords) : 
# TypeError: object of type 'NoneType' has no len()

How to solve this?

Thanks!

CodePudding user response:

layer_concatenate() takes tensors, not layers. You can access the output tensor of a layer with layer$output.

library(keras)

model <- keras_model_sequential(
  input_shape = c(6, 6, 3), batch_size = 5) %>% 
  layer_conv_2d(32, kernel_size = list(3L, 3L),
                padding = "same", activation = "relu", name = "conv1") %>% 
  layer_max_pooling_2d(pool_size = c(2L, 2L), name = "pool1") %>% 
  layer_upsampling_2d(size = c(2L, 2L), name = "tmpUp1")

layer_concatenate(model$get_layer("conv1")$output, 
                  model$get_layer("tmpUp1")$output)
#> KerasTensor(type_spec=TensorSpec(shape=(5, 6, 6, 64), dtype=tf.float32, name=None), name='concatenate/concat:0', description="created by layer 'concatenate'")
  • Related