From d87931c011299da5c40bd65fbbeeb7d69835e3ee Mon Sep 17 00:00:00 2001 From: jingpengw Date: Thu, 6 May 2021 09:57:07 -0400 Subject: [PATCH 1/2] initial setup of cpp with xtensor --- cpp/include/warp3d.hpp | 0 cpp/main.cpp | 20 ++++++++++++++++ neutorch/__version__.py | 1 + neutorch/dataset/transform.py | 8 ++++++- requirements.txt | 3 ++- setup.py | 45 ++++++++++++++++++++++++++++++++++- tests/train.py | 4 ---- 7 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 cpp/include/warp3d.hpp create mode 100644 cpp/main.cpp create mode 100644 neutorch/__version__.py delete mode 100644 tests/train.py diff --git a/cpp/include/warp3d.hpp b/cpp/include/warp3d.hpp new file mode 100644 index 0000000..e69de29 diff --git a/cpp/main.cpp b/cpp/main.cpp new file mode 100644 index 0000000..02fbea7 --- /dev/null +++ b/cpp/main.cpp @@ -0,0 +1,20 @@ +#include + + +PYBIND11_MODULE(libneutorch, m) { + m.doc() = R"pbdoc( + libneutorch + ----------------------- + .. currentmodule:: libneutorch + .. autosummary:: + :toctree: _generate + warp3d + )pbdoc"; + + m.def("warp3d", &warp3d, R"pbdoc( + Warp 3d image + + used for patch augmentation. + )pbdoc"); + +} \ No newline at end of file diff --git a/neutorch/__version__.py b/neutorch/__version__.py new file mode 100644 index 0000000..3b93d0b --- /dev/null +++ b/neutorch/__version__.py @@ -0,0 +1 @@ +__version__ = "0.0.2" diff --git a/neutorch/dataset/transform.py b/neutorch/dataset/transform.py index b982f06..265cc02 100644 --- a/neutorch/dataset/transform.py +++ b/neutorch/dataset/transform.py @@ -392,4 +392,10 @@ def transform(self, patch: Patch): @property def shrink_size(self): # return (0, 0, 0, 0, 0, self.max_displacement) - return (self.max_displacement,) * 6 \ No newline at end of file + return (self.max_displacement,) * 6 + + +# class Affine(SpatialTransform): +# def __init__(self, probability: float = DEFAULT_PROBABILITY, +# scale: tuple = ()): +# super().__init__(probability=probability) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 89c0f8d..ca0102d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ torch torchvision -torchio toml h5py tensorboard +scikit-image +pybind11 \ No newline at end of file diff --git a/setup.py b/setup.py index 91f9e07..96b7c55 100755 --- a/setup.py +++ b/setup.py @@ -1,15 +1,56 @@ #!/usr/bin/env python +import os +import re from setuptools import setup, find_packages +from pybind11.setup_helpers import Pybind11Extension, build_ext + + +PACKAGE_DIR = os.path.dirname(os.path.abspath(__file__)) + +with open(os.path.join(PACKAGE_DIR, 'requirements.txt')) as f: + requirements = f.read().splitlines() + requirements = [l for l in requirements if not l.startswith('#')] + +with open("README.md", "r") as fh: + long_description = fh.read() + +VERSIONFILE = os.path.join(PACKAGE_DIR, "neutorch/__version__.py") +verstrline = open(VERSIONFILE, "rt").read() +VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]" +mo = re.search(VSRE, verstrline, re.M) +if mo: + version = mo.group(1) +else: + raise RuntimeError("Unable to find version string in %s." % + (VERSIONFILE, )) + + +ext_modules = [ + Pybind11Extension("libneutorch", + ["cpp/main.cpp"], + # Example: passing in the version to the compiled code + define_macros = [('VERSION_INFO', version)], + ), +] + setup( name='neutorch', - version='0.0.1', + version=version, description='Deep Learning for brain connectomics using PyTorch', + long_description=long_description, + long_description_content_type="text/markdown", author='Jingpeng Wu', author_email='jingpeng.wu@gmail.com', url='https://github.com/brain-connectome/neutorch', packages=find_packages(exclude=['bin']), + cmdclass={"build_ext": build_ext}, + ext_modules=ext_modules, + install_requires=requirements, + tests_require=[ + 'pytest', + ], entry_points=''' [console_scripts] neutrain=neutorch.cli.train:train @@ -24,4 +65,6 @@ "Operating System :: OS Independent", "Programming Language :: Python :: 3", ], + python_requires='>=3', + zip_safe=False, ) diff --git a/tests/train.py b/tests/train.py deleted file mode 100644 index 586a5fd..0000000 --- a/tests/train.py +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - - From 96f45947dbf3e8179b23c3ed5c8522be803d15e9 Mon Sep 17 00:00:00 2001 From: jingpengw Date: Fri, 13 Aug 2021 09:28:59 -0400 Subject: [PATCH 2/2] remove warp3d.hpp; add some segmentation and patch transform scripts --- cpp/include/segmentation.hpp | 11 +++++++++++ cpp/include/warp3d.hpp | 0 neutorch/dataset/patch.py | 16 +++++++++++++++- neutorch/dataset/transform.py | 5 ++++- 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 cpp/include/segmentation.hpp delete mode 100644 cpp/include/warp3d.hpp diff --git a/cpp/include/segmentation.hpp b/cpp/include/segmentation.hpp new file mode 100644 index 0000000..00d901c --- /dev/null +++ b/cpp/include/segmentation.hpp @@ -0,0 +1,11 @@ +#pragma once +include "xtensor/" + +namespace libneutorch{ + +template +auto label2affinity(pytensor label){ + +} + +} // namespace libneutorch \ No newline at end of file diff --git a/cpp/include/warp3d.hpp b/cpp/include/warp3d.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/neutorch/dataset/patch.py b/neutorch/dataset/patch.py index 77e6e64..3a68e05 100644 --- a/neutorch/dataset/patch.py +++ b/neutorch/dataset/patch.py @@ -19,6 +19,7 @@ def __init__(self, image: np.ndarray, label: np.ndarray, assert image.shape == label.shape self.image = image self.label = label + self.target = None self.delayed_shrink_size = delayed_shrink_size def accumulate_delayed_shrink_size(self, shrink_size: tuple): @@ -61,4 +62,17 @@ def shape(self): @property @lru_cache def center(self): - return tuple(ps // 2 for ps in self.shape) \ No newline at end of file + return tuple(ps // 2 for ps in self.shape) + + @property + def target(self): + if self.target is None: + assert np.issubdtype(self.label.dtype, np.floating) + return self.label + else: + return self.target + + @property + @lru_cache + def affinity_map(self): + \ No newline at end of file diff --git a/neutorch/dataset/transform.py b/neutorch/dataset/transform.py index 41f172e..2dad369 100644 --- a/neutorch/dataset/transform.py +++ b/neutorch/dataset/transform.py @@ -536,4 +536,7 @@ def transform(self, patch: Patch): strength=random.randint(1, self.max_strength), radius = (patch.shape[-1] + patch.shape[-2]) // 4, ) ->>>>>>> main + +class LabelAsTarget() + +class Label2Affinity \ No newline at end of file