def _test_real_serialization(self): wannabe = TinyConvNet3D() tik_torch = TikTorch(model=wannabe) with tempdir() as d: the_path = '{}/testfile.nn'.format(d) tik_torch.serialize(to_path=the_path) new_torch = TikTorch.unserialize(from_path=the_path) self.assertIsInstance(new_torch, TikTorch)
def __init__(self, tiktorch_net, filename=None, HALO_SIZE=32, BATCH_SIZE=3): """ Args: tiktorch_net (tiktorch): tiktorch object to be loaded into this classifier object filename (None, optional): Save file name for future reference """ self._filename = filename if self._filename is None: self._filename = "" self.HALO_SIZE = HALO_SIZE self.BATCH_SIZE = BATCH_SIZE if tiktorch_net is None: print(self._filename) tiktorch_net = TikTorch.unserialize(self._filename) # print (self._filename) # assert tiktorch_net.return_hypercolumns == False # print('blah') self._tiktorch_net = tiktorch_net self._opReorderAxes = OpReorderAxes(graph=Graph()) self._opReorderAxes.AxisOrder.setValue("zcyx")
def setUp(self): # Build model and object model = TinyConvNet3D() tiktorch = TikTorch(model=model).configure(window_size=[3, 512, 512], num_input_channels=1, num_output_channels=1) self.tiktorch = tiktorch self.input_array = np.zeros(shape=(1, 3, 512, 512))
def _test_gpu_serialization(self): wannabe = TinyConvNet3D() tik_torch = TikTorch(model=wannabe) tik_torch.cuda() with tempdir() as d: the_path = "{}/testfile.nn".format(d) tik_torch.serialize(to_path=the_path) new_torch = TikTorch.unserialize(from_path=the_path) self.assertIsInstance(new_torch, TikTorch) self.assertTrue(new_torch.is_cuda)
def create_and_train_pixelwise(self, feature_images, label_images, axistags=None, feature_names=None): self._filename = PYTORCH_MODEL_FILE_PATH logger.debug("Loading pytorch network from {}".format(self._filename)) # Save for future reference # known_labels = numpy.sort(vigra.analysis.unique(y)) # TODO: check whether loaded network has the same number of classes as specified in ilastik! self._loaded_pytorch_net = TikTorch.unserialize(self._filename) logger.info(self.description) # logger.info("OOB during training: {}".format( oob )) return TikTorchLazyflowClassifier(self._loaded_pytorch_net, self._filename)
def deserialize_hdf5(cls, h5py_group): # TODO: load from HDF5 instead of hard coded path! logger.debug("Deserializing") # HACK: # filename = PYTORCH_MODEL_FILE_PATH filename = h5py_group[cls.HDF5_GROUP_FILENAME] logger.debug("Deserializing from {}".format(filename)) with tempfile.TemporaryFile() as f: f.write(h5py_group["classifier"].value) f.seek(0) loaded_pytorch_net = TikTorch.unserialize(f) return TikTorchLazyflowClassifier(loaded_pytorch_net, filename)
def test_less_output_channels(self): model = TinyConvNet3D(num_input_channels=3, num_output_channels=2) tiktorch = TikTorch(model=model).configure(window_size=[3, 512, 512], num_input_channels=3, num_output_channels=2) self.tiktorch = tiktorch self.input_array = np.zeros(shape=(3, 3, 512, 512)) # Forward and check output = self.tiktorch.forward([self.input_array]) # noinspection PyTypeChecker self.assertEqual(len(output), 1) # noinspection PyUnresolvedReferences self.assertIsInstance(output[0], np.ndarray) # noinspection PyUnresolvedReferences self.assertEqual(output[0].shape, (2, 3, 512, 512))
def setup_class(cls): cls.graph = Graph() cls.model = TinyConvNet2D(num_input_channels=1, num_output_channels=2) cls.tiktorch_net = TikTorch(model=cls.model) # HACK: Window size catered to settings in tiktorchLazyflowClassifier :/ cls.tiktorch_net.configure(window_size=[192, 192], num_input_channels=1, num_output_channels=2) # HACK: this is also catered to the hardcoded settings in tiktorchLazyflowClassifier cls.data = numpy.arange(3 * 192 * 192).astype(numpy.uint8).reshape( (3, 1, 192, 192)) cls.tmp_dir = tempfile.mkdtemp() cls.h5_file = h5py.File(os.path.join(cls.tmp_dir, "h5_file.h5"), mode="a") cls.classifier_group = cls.h5_file.create_group("classifier")
def setUp(self): TikTorch.read_config = lambda self: self tiktorch = TikTorch(build_directory=".") if self.MODEL == "HEAVY": tiktorch._model = nn.Sequential( nn.Conv2d(1, 512, 9, padding=4), nn.ELU(), nn.Conv2d(512, 512, 9, padding=4), nn.ELU(), nn.Conv2d(512, 512, 9, padding=4), nn.ELU(), nn.Conv2d(512, 512, 9, padding=4), nn.ELU(), nn.Conv2d(512, 1, 9, padding=4), ) elif self.MODEL == "LITE": tiktorch._model = nn.Conv2d(1, 1, 1) tiktorch._config = {"input_shape": [1, 512, 512], "dynamic_input_shape": "(32 * (nH + 1), 32 * (nW + 1))"} tiktorch._set_handler(tiktorch._model) self.tiktorch = tiktorch
def setUp(self): TikTorch.read_config = lambda self: self tiktorch = TikTorch(build_directory='.') if self.MODEL == 'HEAVY': tiktorch._model = nn.Sequential( nn.Conv2d(1, 512, 9, padding=4), nn.ELU(), nn.Conv2d(512, 512, 9, padding=4), nn.ELU(), nn.Conv2d(512, 512, 9, padding=4), nn.ELU(), nn.Conv2d(512, 512, 9, padding=4), nn.ELU(), nn.Conv2d(512, 1, 9, padding=4), ) elif self.MODEL == 'LITE': tiktorch._model = nn.Conv2d(1, 1, 1) tiktorch._config = { 'input_shape': [1, 512, 512], 'dynamic_input_shape': '(32 * (nH + 1), 32 * (nW + 1))' } tiktorch._set_handler(tiktorch._model) self.tiktorch = tiktorch