Skip to content

hdf5_matrix as model input #410

@Fubukimaru

Description

@Fubukimaru

Hello,

I have modified the convolutional LSTM example (https://keras.rstudio.com/articles/examples/conv_lstm.html) for processing my own data. It works with a regular multidimensional array, however if I use an HDF5 matrix it doesn't work.

The code is the following:

dataset <- hdf5_matrix(datasetPath, datasetHDF5Path)

# Extract input shape
df_input_shape <- dataset$shape
# Time is NULL to enable variable length in sequences.
df_input_shape[2] <- list(NULL) 
df_input_shape[1] <- NULL # Remove number of samples

# Model definition --------------------------------------------------------
{
#Initialize model
model <- keras_model_sequential()
    # Input shape
    # With channels_last (default) -> (samples,time, rows, cols, channels)
    # In input_shape we don't include the samples
model %>%
  # Begin with 2D convolutional LSTM layer
  layer_conv_lstm_2d(
    input_shape = df_input_shape,  # Requiered when this layer is the first.
    data_format = "channels_last",
    filters = 5, kernel_size = c(3,3),
    padding = "same", 
    return_sequences = TRUE
  ) %>%
  # Normalize the activations of the previous layer
  layer_batch_normalization() %>%
  
  # Add 3x hidden 2D convolutions LSTM layers, with
  # batch normalization layers between
  layer_conv_lstm_2d(
    filters = 5, kernel_size = c(3,3),
    data_format = "channels_last",
    padding = "same", return_sequences = TRUE
  ) %>%
  layer_batch_normalization() %>%
  layer_conv_lstm_2d(
    filters = 5, kernel_size = c(3,3),
    data_format = "channels_last",
    padding = "same", return_sequences = TRUE
  ) %>%
  layer_batch_normalization() %>% 
  layer_conv_lstm_2d(
    filters = 5, kernel_size = c(3,3),
    data_format = "channels_last",
    padding = "same", return_sequences = TRUE
  ) %>%
  layer_batch_normalization() %>%
  
  # Add final 3D convolutional output layer 
  layer_conv_3d(
    filters = df_input_shape[[4]], kernel_size = c(3,3,3),
    activation = "sigmoid", 
    padding = "same", data_format ="channels_last"
  )
}

# Prepare model for training
model %>% compile(
  loss = "binary_crossentropy", 
  optimizer = "adadelta"
)

model


# Training ----------------------------------------------------------------



model %>% fit(
  dataset,
  dataset,
  batch_size = 1,
  epochs = 30
  #validation_split = 0.05
)

The model compiles properly using the extracted shape:

Model                                                                                                     
___________________________________________________________________________________________________________
Layer (type)                                    Output Shape                              Param #          
===========================================================================================================
conv_lst_m2d_2 (ConvLSTM2D)                     (None, None, 762, 1000, 5)                1460             
___________________________________________________________________________________________________________
batch_normalization_1 (BatchNormalization)      (None, None, 762, 1000, 5)                20               
___________________________________________________________________________________________________________
conv_lst_m2d_3 (ConvLSTM2D)                     (None, None, 762, 1000, 5)                1820             
___________________________________________________________________________________________________________
batch_normalization_2 (BatchNormalization)      (None, None, 762, 1000, 5)                20               
___________________________________________________________________________________________________________
conv_lst_m2d_4 (ConvLSTM2D)                     (None, None, 762, 1000, 5)                1820             
___________________________________________________________________________________________________________
batch_normalization_3 (BatchNormalization)      (None, None, 762, 1000, 5)                20               
___________________________________________________________________________________________________________
conv_lst_m2d_5 (ConvLSTM2D)                     (None, None, 762, 1000, 5)                1820             
___________________________________________________________________________________________________________
batch_normalization_4 (BatchNormalization)      (None, None, 762, 1000, 5)                20               
___________________________________________________________________________________________________________
conv3d_1 (Conv3D)                               (None, None, 762, 1000, 3)                408              
===========================================================================================================
Total params: 7,408
Trainable params: 7,368
Non-trainable params: 40
___________________________________________________________________________________________________________

However, at the moment I try to fit the model it gives me an error:

> model %>% fit(                                                                                          
+   dataset,                                                                                              
+   dataset,                                                                                              
+   batch_size = 1,                                                                                       
+   epochs = 30                                                                                           
+   #validation_split = 0.05                                                                              
+ )                                                                                                       
Error in dim(x) <- length(x) : invalid first argument     

Am I doing anything wrong? Are these functions already working with hdf5_matrix type? I cannot find the code for the fit() function in this repository, so it's a blind guess.

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions