ModelExecutor
- class medigan.execute_model.model_executor.ModelExecutor(model_id: str, execution_config: dict, download_package: bool = True, install_dependencies: bool = False)[source]
Bases:
object
ModelExecutor class: Find config links to download models, init models as python packages, run generate methods.
- Parameters
model_id (str) – The generative model’s unique id
execution_config (dict) – The part of the config below the ‘execution’ key
download_package (bool) – Flag indicating, if True, that the model’s package should be downloaded instead of using an existing one that was downloaded previously
install_dependencies (bool) – flag indicating whether a generative model’s dependencies are automatically installed. Else error is raised if missing dependencies are detected.
- model_id
The generative model’s unique id
- Type
str
- execution_config
The part of the config below the ‘execution’ key
- Type
dict
- download_package
Flag indicating, if True, that the model’s package should be downloaded instead of using an existing one that was downloaded previously
- Type
bool
- image_size
Pixel dimension of the generated samples, where images are assumed to have the same width and height
- Type
int
- dependencies
List of the dependencies of a models python package.
- Type
list
- model_name
Name of the generative model
- Type
str
- model_extension
File extension of the generative model’s weights file.
- Type
str
- package_name
Name of the model’s python package i.e. the name of the model’s zip file and unzipped package folder
- Type
str
- package_link
The link to the zipped model package. Note: Convention is to host models on Zenodo (reason: static doi content)
- Type
str
- generate_method_name
The name of the model’s generate method inside the model package. This method is called to generate samples.
- Type
str
- generate_method_args
The args of the model’s generate method inside the model package
- Type
dict
- serialised_model_file_path
Path as string to the generative model’s weights file
- Type
str
- package_path
Path as string to the generative model’s python package containing an __init__.py file
- Type
str
- deserialized_model_as_lib
The generative model’s package imported as python library. Generate method inside this library can be called.
Methods Summary
generate
([num_samples, output_path, ...])Generate samples using the generative model or return the model's generate function.
Check if a valid path to the model files exists and, if so, set the package_path
Methods Documentation
- generate(num_samples: int = 20, output_path: Optional[str] = None, save_images: bool = True, is_gen_function_returned: bool = False, batch_size: int = 32, **kwargs)[source]
Generate samples using the generative model or return the model’s generate function.
The name amd additional parameters of the generate function of the respective generative model are retrieved from the model’s execution_config.
- Parameters
num_samples (int) – the number of samples that will be generated
output_path (str) – the path as str to the output folder where the generated samples will be stored
save_images (bool) – flag indicating whether generated samples are returned (i.e. as list of numpy arrays) or rather stored in file system (i.e in output_path)
is_gen_function_returned (bool) – flag indicating whether, instead of generating samples, the sample generation function will be returned
batch_size (int) – the batch size for the sample generation function
**kwargs – arbitrary number of keyword arguments passed to the model’s sample generation function
- Returns
Returns images as list of numpy arrays if save_images is False. However, if is_gen_function_returned is True, it returns the internal generate function of the model.
- Return type
list
- Raises
Exception – If the generate method of the model does not exist, cannot be called, or is called with missing params, or if the sample generation inside the model package returns an exception.