def save(self, path): """Load Security evaluation data from file. Save a python dict containing all the results. """ results = {p: getattr(self, p) for p in self.get_params()} pk.save(path, results)
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 save_state(self, path): """Store the object state to file. Parameters ---------- path : str Path of the file where to store object state. Returns ------- str The full path of the stored object. See Also -------- get_state : Returns the object state dictionary. """ return pck.save(path, self.get_state())
def save(self, path): """Save class object to file. This function stores an object to file (with pickle). `.load()` can be used to restore the object later. Parameters ---------- path : str Path of the target object file. Returns ------- obj_path : str The full path of the stored object. """ return pck.save(path, self)
def save_state(self, path, **kwargs): """Store the object state to file. Parameters ---------- path : str Path of the file where to store object state. **kwargs Arguments to be passed to `get_state` calls in the hierarchy. Returns ------- str The full path of the stored object. See Also -------- get_state : Returns the object state dictionary. """ return pck.save(path, self.get_state(**kwargs))