GeoMOER / geoAI

Module on geospatial data prediction and remote sensing of the Honour Degree Programm „AI und Entrepreneurship"
MIT License
0 stars 0 forks source link

GeoAI_2021_unit_04_EX_unet #12

Open Baldl opened 2 years ago

FloFranz commented 2 years ago

In the process of model training I want to implement an if-else statement so that it doesn't have to train again when I compile it to PDF. Is there a way to load the trained model for plotting the loss and accuracy? I tried it as follows:

unet_model <- get_unet_128()
# compile the model 
unet_model %>% compile(
   optimizer = optimizer_adam(learning_rate = 0.0001),
   loss = "binary_crossentropy",
   metrics = "accuracy"
)

# train the model
if (!file.exists(file.path(envrmt$path_models, "/unet_buildings.hdf5"))) {

  hist <- unet_model %>% fit(training_dataset,
                             validation_data = validation_dataset,
                             epochs = 10,
                             verbose = 1)

  unet_model %>% save_model_hdf5(file.path(envrmt$path_models, "unet_buildings.hdf5"))

} else {

  hist <- load_model_hdf5(file.path(envrmt$path_models, "unet_buildings.hdf5"), compile = TRUE)

}

plot(hist)

But the else statement where loading the model and saving it in the variable hist results in <keras.engine.functional.Functional object at 0x000001D69A5F4D88> and this object can not be plotted.

Would be really nice to find a solution for this because this step takes some time...

jp-hecht commented 2 years ago

@FloFranz You are not able to plot your variable hist because you basically loaded the whole model as you can see when you just print this variable in your console and unfortunately I could not find/know a solution for restoring your training model history out of the loaded model. So I guess it is easier to save the variable hist or the output in the viewer window as a html-file after fitting your model.