Metadata-Version: 2.3
Name: efficientv2-unet
Version: 0.0.2
Summary: A U-Net implementation of the EfficientNetV2.
Project-URL: homepage, https://github.com/DBM-MCF/efficientv2-unet
Project-URL: repository, https://github.com/DBM-MCF/efficientv2-unet
Author-email: Loïc Sauteur <loic.sauteur@unibas.ch>
License: BSD-3-Clause
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: License :: OSI Approved :: BSD License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Typing :: Typed
Requires-Python: <3.11,>=3.9
Requires-Dist: albumentations<=1.4.0
Requires-Dist: keras==2.10.0; platform_system != 'Darwin'
Requires-Dist: keras==2.11.0; platform_system == 'Darwin'
Requires-Dist: notebook
Requires-Dist: numpy>1.26
Requires-Dist: opencv-python
Requires-Dist: scikit-image
Requires-Dist: tensorflow-macos==2.11.0; platform_system == 'Darwin' and platform_machine == 'arm64'
Requires-Dist: tensorflow-metal==0.7.0; platform_system == 'Darwin' and platform_machine == 'arm64'
Requires-Dist: tensorflow==2.10.1; platform_system != 'Darwin'
Requires-Dist: tensorflow==2.11.0; platform_system == 'Darwin' and platform_machine != 'arm64'
Requires-Dist: tifffile
Provides-Extra: dev
Requires-Dist: ipython; extra == 'dev'
Requires-Dist: matplotlib; extra == 'dev'
Requires-Dist: mypy; extra == 'dev'
Requires-Dist: pdbpp; extra == 'dev'
Requires-Dist: pre-commit; extra == 'dev'
Requires-Dist: rich; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Provides-Extra: test
Requires-Dist: pytest; extra == 'test'
Requires-Dist: pytest-cov; extra == 'test'
Description-Content-Type: text/markdown

[![License](https://img.shields.io/pypi/l/efficientv2-unet.svg?color=green)](https://github.com/DBM-MCF/efficientv2-unet/blob/master/LICENSE)
[![PyPI](https://img.shields.io/pypi/v/efficientv2-unet.svg?color=green)](https://pypi.org/project/efficientv2-unet)
[![Python Version](https://img.shields.io/pypi/pyversions/efficientv2-unet.svg?color=green)](https://python.org)
[![CI](https://github.com/DBM-MCF/efficientv2-unet/actions/workflows/ci.yml/badge.svg)](https://github.com/DBM-MCF/efficientv2-unet/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/DBM-MCF/efficientv2-unet/branch/main/graph/badge.svg)](https://codecov.io/gh/DBM-MCF/efficientv2-unet)

A U-Net implementation of the EfficientNetV2.

# EfficientV2-UNet
This package is a U-Net implementation of the [EfficientNetV2](https://arxiv.org/abs/2104.00298), using TensorFlow.

EfficientNetV2 improves speed and parameter efficiency. This implementation also uses the ImageNet weights for training new models.

It is intended for segmentation of histological images (RGB) that are **not** saved in pyramidal file format (WSI).

The output segmentation are foreground / background. Multi-class segmentation is not (yet) possible.

It works on TIF images (and probably also PNG).

# Installation

1. Create a python environment (e.g. with conda, python=3.9 and 3.10 work), in a CLI:

    `conda create --name myenv python=3.9`

2. Activate environment:

    `conda activate myenv`

3. GPU support
 
   *Non GPU installations not tested*

    a. GPU support for **Windows** (example with conda):

    `conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0`

    b. GPU support for **Linux** -- not tested:

    `python3 -m pip install tensorflow[and-cuda]`

    c. **Apple Silicon** support (requires Xcode command-line tools):

    `xcode-select --install`

    `conda install -c apple tensorflow-deps --force-reinstall`


4. Install this library
    - open a CLI, activate your environment (see above))
    - (TensorFlow will be installed for Windows and macOS platforms)

    `pip install efficientv2-unet`
     
5. Verify the GPU-support:

    `python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"`

    or

    `python -c "import tensorflow as tf; print(tf.test.is_gpu_available())"`


# Data preparation
Mask should have background values of 0, and foreground values of 1.

At least 3 image/mask TIF pairs are required to train a model, and should be located in separate folders.

Folder Structure:
```
├── images
   ├── image1.tif
   ├── image2.tif
   ├── image3.tif
   └── ...
└── masks
   ├── image1.tif
   ├── image2.tif
   ├── image3.tif
   └── ...
```
Training a model will split the data into train, validation and test images (by default 70%, 15%, 15%, respectively).
And the images will be moved to corresponding sub-folders.

Training is performed not on the full images but on tiles (with no overlap), which will be saved into corresponding sub-folders.

# Usage
### Command-line:
```
ev2unet --help

# train example:
ev2unet --train --images path/to/images --masks path/to/masks --basedir . --name myUNetName --basemodel b2 --epochs 50

# predict example:
ev2unet --predict --dir path/to/images --model ./models/myUnetName/myUNetName.h5 --resolution 1 --threshold 0.5
```

### Jupyter notebooks 
Examples are also available from this [repository](notebooks/).
### QuPath extension
Get the [QuPath extension](https://github.com/DBM-MCF/qupath-extension-efficientv2unet)!

<!--
## NOTES:
<span style="color:yellow">
- !!DONE: remove all "src" from import of this package... !! i.e. refactor the folder structure !!
- !!DONE: remove the temp deactivation in data_generation line 436
- !!DONE: remove temp return in efficientv2_unet line 613
- TODO: check that resolution for image scaling (e.g. in predict) is always an int and not a float
- TODO: make a notebook, where model is loaded and images are predicted one by one (so not all images need to be loaded into memory at once)
</span>

### Data preparation:
The raw images and corresponding masks, should be in separate folders,
and the file names must be the same.

Training will split the images into train, validation, 
and test sets (default is 70%, 15%, 15%, respectively). Eventually, 
the input images will be tiled (with no overlap) for training purposes (except 
the test images).

### DataGeneration for training:
There is a resolution parameter for the data generator,
usually at 1. But it will generate in addition crops for training
at resolutions +1 and +2. Hence, generally/at the moment, training
is done at multiple resolutions.

For training/validation data, crops of the images are generated. The crops
do not have any overlap, and the image is padded (reflecting at bottom and 
right boarders), to accommodate crops.

### Best model for Martin
currently it is the B3-best-checkpoint.


### Prediction
somehow prediction works better if the input image is downscaled.
-->
