def load_object(self, fname, category="", classifier=None): """Load an object, that was serialized with dump_object. Args: fname: Name of the serialized file. category: Category name, that was used for serialization. classifier: Classifier object, that was used for serialization. """ self.logger.info("Reading object from disk") t2 = time.time() if classifier == None: classifier = self.classifier try: folder = util.folder_name(self.datamanager.PATHS["CLASSIFIER"], category, classifier) if not os.path.isdir(folder): self.logger.info("Object's path doesn't exist") return None obj = joblib.load(os.path.join(folder, fname)) self.logger.info("%f seconds\n" % (time.time() - t2)) return obj except Exception as e: self.logger.error("Joblib failed: %s" % e) return None
def test_folder_name(self): clf = AdaBoostClassifier(n_estimators=23) clf.base_estimator.max_depth = 42 base = "/hello/world/" category = "testing" params_path = util.params_to_path(clf.get_params()) self.assertEqual( util.folder_name(base, category, clf), os.path.join("/hello/world/AdaBoostClassifier/testing/", params_path))
def dump_object(self, obj, fname, category="", classifier=None): """Serialize an object to disk. This is most often used to serialize trained classifiers, so they can be reloaded for later processing (e.g. visualization). The object is serialized into a file in a subfolder of the classifier-folder of self.datamanager. The path will mirror the parameter settings of the classifier object. Objects that were serialized with this method can be re-loaded with load_object. Args: obj: A python object, that should be serialized (e.g. a classifier). fname: Name of the serialized file. category: String that specifies, for which category the classifier was trained. This introduces another directory level to separate classifiers for different categories. classifier: Classifier object, that determines the path of the serialized file. The directory structure mirrors the parameters of this classifier. By default, self.classifier is used. """ self.logger.info("Writing object to disk") t2 = time.time() if classifier == None: classifier = self.classifier try: folder = util.folder_name(self.datamanager.PATHS["CLASSIFIER"], category, classifier) if not os.path.isdir(folder): os.makedirs(folder) joblib.dump(obj, os.path.join(folder, fname), compress=3) except Exception as e: self.logger.error("Joblib failed: %s" % e) self.logger.info("%f seconds\n" % (time.time() - t2))