1919
2020from monai .apps .auto3dseg .bundle_gen import BundleAlgo
2121from monai .apps .utils import get_logger
22- from monai .auto3dseg import Algo , AlgoGen , algo_from_pickle , algo_to_pickle
22+ from monai .auto3dseg import Algo , AlgoGen , algo_from_json , algo_to_json
2323from monai .bundle .config_parser import ConfigParser
2424from monai .config import PathLike
2525from monai .utils import optional_import
@@ -36,7 +36,7 @@ class HPOGen(AlgoGen):
3636 """
3737 The base class for hyperparameter optimization (HPO) interfaces to generate algos in the Auto3Dseg pipeline.
3838 The auto-generated algos are saved at their ``output_path`` on the disk. The files in the ``output_path``
39- may contain scripts that define the algo, configuration files, and pickle files that save the internal states
39+ may contain scripts that define the algo, configuration files, and JSON files that save the internal states
4040 of the algo before/after the training. Compared to the BundleGen class, HPOGen generates Algo on-the-fly, so
4141 training and algo generation may be executed alternatively and take a long time to finish the generation process.
4242
@@ -72,7 +72,7 @@ class NNIGen(HPOGen):
7272
7373 Args:
7474 algo: an Algo object (e.g. BundleAlgo) with defined methods: ``get_output_path`` and train
75- and supports saving to and loading from pickle files via ``algo_from_pickle `` and ``algo_to_pickle ``.
75+ and supports saving to and loading via ``algo_from_json `` and ``algo_to_json ``.
7676 params: a set of parameter to override the algo if override is supported by Algo subclass.
7777
7878 Examples::
@@ -81,16 +81,16 @@ class NNIGen(HPOGen):
8181 ├── algorithm_templates
8282 │ └── unet
8383 ├── unet_0
84- │ ├── algo_object.pkl
84+ │ ├── algo_object.json
8585 │ ├── configs
8686 │ └── scripts
8787 ├── unet_0_learning_rate_0.01
88- │ ├── algo_object.pkl
88+ │ ├── algo_object.json
8989 │ ├── configs
9090 │ ├── model_fold0
9191 │ └── scripts
9292 └── unet_0_learning_rate_0.1
93- ├── algo_object.pkl
93+ ├── algo_object.json
9494 ├── configs
9595 ├── model_fold0
9696 └── scripts
@@ -129,10 +129,10 @@ def __init__(self, algo: Algo | None = None, params: dict | None = None):
129129 else :
130130 self .algo = algo
131131
132- self .obj_filename = algo_to_pickle (self .algo , template_path = self .algo .template_path )
132+ self .obj_filename = algo_to_json (self .algo , template_path = self .algo .template_path )
133133
134134 def get_obj_filename (self ):
135- """Return the filename of the dumped pickle algo object."""
135+ """Return the filename of the dumped algo object."""
136136 return self .obj_filename
137137
138138 def print_bundle_algo_instruction (self ):
@@ -190,7 +190,7 @@ def generate(self, output_folder: str = ".") -> None:
190190 task_id = self .get_task_id ()
191191 task_prefix = os .path .basename (self .algo .get_output_path ())
192192 write_path = os .path .join (output_folder , task_prefix + task_id )
193- self .obj_filename = os .path .join (write_path , "algo_object.pkl " )
193+ self .obj_filename = os .path .join (write_path , "algo_object.json " )
194194
195195 if isinstance (self .algo , BundleAlgo ):
196196 self .algo .export_to_disk (
@@ -214,15 +214,15 @@ def run_algo(self, obj_filename: str, output_folder: str = ".", template_path: P
214214 The python interface for NNI to run.
215215
216216 Args:
217- obj_filename: the pickle-exported Algo object.
217+ obj_filename: the serialized Algo object.
218218 output_folder: the root path of the algorithms templates.
219219 template_path: the algorithm_template. It must contain algo.py in the follow path:
220220 ``{algorithm_templates_dir}/{network}/scripts/algo.py``
221221 """
222222 if not os .path .isfile (obj_filename ):
223223 raise ValueError (f"{ obj_filename } is not found" )
224224
225- self .algo , algo_meta_data = algo_from_pickle (obj_filename , template_path = template_path )
225+ self .algo , algo_meta_data = algo_from_json (obj_filename , template_path = template_path )
226226
227227 # step 1 sample hyperparams
228228 params = self .get_hyperparameters ()
@@ -235,7 +235,7 @@ def run_algo(self, obj_filename: str, output_folder: str = ".", template_path: P
235235 acc = self .algo .get_score ()
236236 algo_meta_data = {str (AlgoKeys .SCORE ): acc }
237237
238- algo_to_pickle (self .algo , template_path = self .algo .template_path , ** algo_meta_data )
238+ algo_to_json (self .algo , template_path = self .algo .template_path , ** algo_meta_data )
239239 self .set_score (acc )
240240
241241
@@ -250,7 +250,7 @@ class OptunaGen(HPOGen):
250250
251251 Args:
252252 algo: an Algo object (e.g. BundleAlgo). The object must at least define two methods: get_output_path and train
253- and supports saving to and loading from pickle files via ``algo_from_pickle `` and ``algo_to_pickle ``.
253+ and supports saving to and loading via ``algo_from_json `` and ``algo_to_json ``.
254254 params: a set of parameter to override the algo if override is supported by Algo subclass.
255255
256256 Examples::
@@ -259,16 +259,16 @@ class OptunaGen(HPOGen):
259259 ├── algorithm_templates
260260 │ └── unet
261261 ├── unet_0
262- │ ├── algo_object.pkl
262+ │ ├── algo_object.json
263263 │ ├── configs
264264 │ └── scripts
265265 ├── unet_0_learning_rate_0.01
266- │ ├── algo_object.pkl
266+ │ ├── algo_object.json
267267 │ ├── configs
268268 │ ├── model_fold0
269269 │ └── scripts
270270 └── unet_0_learning_rate_0.1
271- ├── algo_object.pkl
271+ ├── algo_object.json
272272 ├── configs
273273 ├── model_fold0
274274 └── scripts
@@ -296,10 +296,10 @@ def __init__(self, algo: Algo | None = None, params: dict | None = None) -> None
296296 else :
297297 self .algo = algo
298298
299- self .obj_filename = algo_to_pickle (self .algo , template_path = self .algo .template_path )
299+ self .obj_filename = algo_to_json (self .algo , template_path = self .algo .template_path )
300300
301301 def get_obj_filename (self ):
302- """Return the dumped pickle object of algo."""
302+ """Return the dumped object of algo."""
303303 return self .obj_filename
304304
305305 def get_hyperparameters (self ):
@@ -329,7 +329,7 @@ def __call__(
329329 Callable that Optuna will use to optimize the hyper-parameters
330330
331331 Args:
332- obj_filename: the pickle-exported Algo object.
332+ obj_filename: the serialized Algo object.
333333 output_folder: the root path of the algorithms templates.
334334 template_path: the algorithm_template. It must contain algo.py in the follow path:
335335 ``{algorithm_templates_dir}/{network}/scripts/algo.py``
@@ -364,7 +364,7 @@ def generate(self, output_folder: str = ".") -> None:
364364 task_id = self .get_task_id ()
365365 task_prefix = os .path .basename (self .algo .get_output_path ())
366366 write_path = os .path .join (output_folder , task_prefix + task_id )
367- self .obj_filename = os .path .join (write_path , "algo_object.pkl " )
367+ self .obj_filename = os .path .join (write_path , "algo_object.json " )
368368
369369 if isinstance (self .algo , BundleAlgo ):
370370 self .algo .export_to_disk (output_folder , task_prefix + task_id , fill_with_datastats = False )
@@ -377,15 +377,15 @@ def run_algo(self, obj_filename: str, output_folder: str = ".", template_path: P
377377 The python interface for NNI to run.
378378
379379 Args:
380- obj_filename: the pickle-exported Algo object.
380+ obj_filename: the serialized Algo object.
381381 output_folder: the root path of the algorithms templates.
382382 template_path: the algorithm_template. It must contain algo.py in the follow path:
383383 ``{algorithm_templates_dir}/{network}/scripts/algo.py``
384384 """
385385 if not os .path .isfile (obj_filename ):
386386 raise ValueError (f"{ obj_filename } is not found" )
387387
388- self .algo , algo_meta_data = algo_from_pickle (obj_filename , template_path = template_path )
388+ self .algo , algo_meta_data = algo_from_json (obj_filename , template_path = template_path )
389389
390390 # step 1 sample hyperparams
391391 params = self .get_hyperparameters ()
@@ -397,5 +397,5 @@ def run_algo(self, obj_filename: str, output_folder: str = ".", template_path: P
397397 # step 4 report validation acc to controller
398398 acc = self .algo .get_score ()
399399 algo_meta_data = {str (AlgoKeys .SCORE ): acc }
400- algo_to_pickle (self .algo , template_path = self .algo .template_path , ** algo_meta_data )
400+ algo_to_json (self .algo , template_path = self .algo .template_path , ** algo_meta_data )
401401 self .set_score (acc )
0 commit comments