def load(cls, path): """Loads object from file. This function loads an object from file (with pickle). The object can be correctly loaded in the following cases: - loaded and calling class have the same type. - calling class is the superclass of the loaded class's package. - calling class is `.CCreator`. Parameters ---------- path : str Path of the target object file. """ loaded_obj = pck.load(path) if loaded_obj.__class__ == cls or cls == CCreator or \ (has_super(loaded_obj) and cls.__name__ == loaded_obj.__super__): return loaded_obj else: err_str = "'{0}' can be loaded from: '{0}'".format( loaded_obj.__class__.__name__) if has_super(loaded_obj): err_str += ", '{:}'".format(loaded_obj.__super__) raise TypeError(err_str + " or 'CCreator'.")
def load(cls, path): """Load Security evaluation data from file. Save a python dict containing all the results. """ data = cls() data.set_params(pk.load(path)) return data
def _load_mnist(): """Load MNIST 4971 dataset.""" digits = [4, 9, 7, 1] digits_str = "".join(['{:}-'.format(i) for i in digits[:-1]]) digits_str += '{:}'.format(digits[-1]) # FIXME: REMOVE THIS AFTER CDATALOADERS AUTOMATICALLY STORE DS tr_file = fm.join(fm.abspath(__file__), 'mnist_tr_{:}.gz'.format(digits_str)) if not fm.file_exist(tr_file): loader = CDataLoaderMNIST() tr = loader.load('training', digits=digits) pickle_utils.save(tr_file, tr) else: tr = pickle_utils.load(tr_file, encoding='latin1') ts_file = fm.join(fm.abspath(__file__), 'mnist_ts_{:}.gz'.format(digits_str)) if not fm.file_exist(ts_file): loader = CDataLoaderMNIST() ts = loader.load('testing', digits=digits) pickle_utils.save(ts_file, ts) else: ts = pickle_utils.load(ts_file, encoding='latin1') idx = CArray.arange(tr.num_samples) val_dts_idx = CArray.randsample(idx, 200, random_state=0) val_dts = tr[val_dts_idx, :] tr_dts_idx = CArray.randsample(idx, 200, random_state=0) tr = tr[tr_dts_idx, :] idx = CArray.arange(0, ts.num_samples) ts_dts_idx = CArray.randsample(idx, 200, random_state=0) ts = ts[ts_dts_idx, :] tr.X /= 255.0 ts.X /= 255.0 return tr, val_dts, ts, digits, tr.header.img_w, tr.header.img_h
def test_save_load(self): a = CArray([1, 2, 3]) # Dummy test array # Generate a temp file to test import tempfile tempdir = tempfile.gettempdir() tempfile = fm.join(tempdir, 'secml_testpickle') tempfile = pickle_utils.save(tempfile, a) a_loaded = pickle_utils.load(tempfile) self.assert_array_equal(a_loaded, a)
def load_state(self, path): """Sets the object state from file. Parameters ---------- path : str The full path of the file from which to load the object state. See Also -------- set_state : Sets the object state using input dictionary. """ # Copy not needed for objects loaded from disk self.set_state(pck.load(path), copy=False)