### Navigate to Installation Directory (Windows) Source: https://connectomics.readthedocs.io/en/latest/notes/installation Changes the current directory in the Anaconda Prompt to the desired location for installing the package. Users should replace the example path with their preferred installation directory. ```batch cd ``` -------------------------------- ### Install PyTorch Connectomics Directly from GitHub (Bash) Source: https://connectomics.readthedocs.io/en/latest/notes/installation Installs the PyTorch Connectomics package directly from its GitHub repository using pip. This is an alternative to cloning the repository and installing locally, useful for users who only need the library without the source code. ```bash pip install git+https://github.com/zudi-lin/pytorch_connectomics.git ``` -------------------------------- ### Install PyTorch Connectomics and Dependencies (Shell) Source: https://connectomics.readthedocs.io/en/latest/notes/installation These commands set up a Python 3.8.11 virtual environment using Conda, activate it, install Git, and then install PyTorch with CUDA support. It clones the PyTorch Connectomics repository, installs it in editable mode, and installs the imagecodecs library for Windows image processing. ```shell conda create --name py3_torch python=3.8.11 -y conda activate py3_torch conda install git -y conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch -y git clone https://github.com/zudi-lin/pytorch_connectomics.git cd pytorch_connectomics pip install --editable . cd .. conda install -c conda-forge imagecodecs -y echo Completely finished with installation. Software is ready to use ``` -------------------------------- ### Build and Install Neuroglancer from Source Source: https://connectomics.readthedocs.io/en/latest/external/neuroglancer Builds Neuroglancer from source, requiring Node.js and nvm. It clones the repository, installs Node.js, sets up the virtual environment, installs dependencies, and then installs Neuroglancer using setup.py. ```bash # build neuroglancer from source (requires nvm/node.js) mkdir project cd project source activate py3_torch git clone https://github.com/google/neuroglancer.git cd neuroglancer curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm pip install numpy Pillow requests tornado sockjs-tornado six google-apitools selenium imageio h5py cloud-volume python setup.py install ``` -------------------------------- ### Clone and Install PyTorch Connectomics (Linux) Source: https://connectomics.readthedocs.io/en/latest/notes/installation Clones the PyTorch Connectomics repository from GitHub and installs it in editable mode using pip. Editable mode allows for code changes to be reflected without re-installation. This command should be run after setting up the environment and installing PyTorch. ```bash git clone https://github.com/zudi-lin/pytorch_connectomics.git cd pytorch_connectomics pip install --editable . ``` -------------------------------- ### Run Training with Pretrained Model (PyTorch Distributed) Source: https://connectomics.readthedocs.io/en/latest/tutorials/neuron Starts training a model using PyTorch's distributed data parallel, loading weights from a specified checkpoint file. Requires CUDA devices, distributed training setup, and configuration files. ```shell CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 python -u -m torch.distributed.run \ --nproc_per_node=2 --master_port=1234 scripts/main.py --distributed \ --config-base configs/SNEMI/SNEMI-Base.yaml \ --config-file configs/SNEMI/SNEMI-Affinity-UNet.yaml \ --checkpoint /path/to/checkpoint/checkpoint_xxxxx.pth.tar ``` -------------------------------- ### Verify CUDA Compiler (Bash) Source: https://connectomics.readthedocs.io/en/latest/notes/installation Checks the version of the NVIDIA CUDA compiler (nvcc) installed on the system. This helps confirm that the CUDA toolkit is correctly installed and accessible. ```bash nvcc --version ``` -------------------------------- ### Start Local Neuroglancer Server Source: https://connectomics.readthedocs.io/en/latest/external/neuroglancer Initializes and starts a local Neuroglancer web server. It sets the bind address and port, creates a viewer instance, and prints the viewer link. This script must be run interactively (e.g., using `python -i` or in a Jupyter notebook) to keep the server running. ```python import neuroglancer ip = 'localhost' # or public IP of the machine for sharable display port = 9999 # change to an unused port number neuroglancer.set_server_bind_address(bind_address=ip, bind_port=port) viewer = neuroglancer.Viewer() print(viewer) ``` -------------------------------- ### Install and Use Waterz for Segmentation Source: https://connectomics.readthedocs.io/en/latest/tutorials/neuron Clones the waterz repository, installs it as an editable package, and then uses its API to generate segmentations from affinity maps. The API requires affinity maps, thresholds, and optionally ground truth for evaluation. ```shell git clone https://github.com/zudi-lin/waterz.git cd waterz pip install --editable . pip install waterz ``` -------------------------------- ### Install PyTorch and Dependencies (Linux) Source: https://connectomics.readthedocs.io/en/latest/notes/installation Installs PyTorch with specified versions for CPU/GPU, torchvision, torchaudio, and cudatoolkit within a Conda environment. It also installs Pillow to resolve dependency issues. This is a prerequisite for installing PyTorch Connectomics. ```bash conda create -y -n pytc python=3.9 conda activate pytc conda install -y pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit==11.3.1 -c pytorch pip install pillow==9.4.0 ``` -------------------------------- ### Install Neuroglancer with Pip Source: https://connectomics.readthedocs.io/en/latest/external/neuroglancer Installs Neuroglancer and necessary libraries (imageio, h5py, cloud-volume, jupyter) using pip within a Python virtual environment. This is the recommended method for a quick start. ```bash # Install neuroglancer using pip in virtual env, which # is the recommended way to start quickly. source activate py3_torch pip install --upgrade pip pip install neuroglancer imageio h5py cloud-volume pip install jupyter #(optional) jupyter/ipykernel installation ``` -------------------------------- ### Download EM-R50 Dataset Source: https://connectomics.readthedocs.io/en/latest/tutorials/synapse Downloads the example dataset for synaptic polarity detection from a remote server using wget. This is the first step to acquire the necessary data for the tutorial. ```shell wget http://rhoana.rc.fas.harvard.edu/dataset/jwr15_synapse.zip ``` -------------------------------- ### Download SNEMI Dataset Source: https://connectomics.readthedocs.io/en/latest/tutorials/neuron This command downloads the SNEMI dataset using wget. Ensure you have wget installed and sufficient storage space. The downloaded zip file contains 'image' and 'seg' folders. ```bash wget http://rhoana.rc.fas.harvard.edu/dataset/snemi.zip ``` -------------------------------- ### Get Dataset Function Source: https://connectomics.readthedocs.io/en/latest/modules/data The get_dataset function prepares a dataset instance for training and inference. It requires a configuration object, an augmentor, and the training mode. Optional parameters include rank, dataset class, dataset options, and initial directory/image names. ```python def connectomics.data.dataset.get_dataset( cfg, augmentor_, mode='train', rank=None, dataset_class=connectomics.data.dataset.dataset_volume.VolumeDataset, dataset_options={}, dir_name_init=None, img_name_init=None ) ``` -------------------------------- ### Build TestAugmentor from Configuration Source: https://connectomics.readthedocs.io/en/latest/modules/data A class method to construct a TestAugmentor instance directly from a configuration object. This allows for flexible setup of augmentation parameters based on predefined configurations. ```python _classmethod _`build_from_cfg`(_cfg_ , _activation =True_) ``` -------------------------------- ### Download CREMI Dataset Source: https://connectomics.readthedocs.io/en/latest/tutorials/synapse Downloads the CREMI dataset using wget. Ensure you have wget installed and sufficient disk space. The script assumes it's run from the root directory of the project. ```bash wget http://rhoana.rc.fas.harvard.edu/dataset/cremi.zip ``` -------------------------------- ### Configure Multitask Learning for Prediction Source: https://connectomics.readthedocs.io/en/latest/notes/config Demonstrates how to set up multitask learning to predict multiple targets from a single image volume. This example shows configuration for instance segmentation, predicting both foreground masks and instance boundaries, along with associated losses. ```yaml MODEL: TARGET_OPT: ['0', '4-2-1'] LOSS_OPTION: [['WeightedBCE', 'DiceLoss'], ['WeightedBCE']] LOSS_WEIGHT: [[1.0, 1.0], [1.0]] WEIGHT_OPT: [['1', '0'], ['1']] ``` -------------------------------- ### List of Absolute Paths for 2D Inference Source: https://connectomics.readthedocs.io/en/latest/notes/dataloading This is an example format for the test_path.txt file used during 2D inference. It should contain a list of absolute paths to the image files, one per line, to ensure clarity and prevent ambiguity during loading. ```text /data/test/slice_0001.png /data/test/slice_0002.png /data/test/slice_0003.png ... /data/test/slice_0004.png ``` -------------------------------- ### Visualize Training Progress with Tensorboard Source: https://connectomics.readthedocs.io/en/latest/tutorials/neuron Launches Tensorboard to visualize training progress from a specified log directory. This is useful for monitoring metrics and understanding model behavior during training. ```shell tensorboard --logdir outputs/SNEMI_UNet/ ``` -------------------------------- ### Visualize Training Progress with TensorBoard Source: https://connectomics.readthedocs.io/en/latest/tutorials/synapse Launches TensorBoard to visualize the training progress, including metrics and model checkpoints. The log directory should be set to where the training outputs are saved. ```bash tensorboard --logdir outputs/CREMI_Binary_UNet ``` -------------------------------- ### Verify PyTorch CUDA Support (Python) Source: https://connectomics.readthedocs.io/en/latest/notes/installation Checks if PyTorch has been installed with CUDA support enabled. This is a crucial verification step after installing PyTorch to ensure GPU acceleration is available. ```python import torch print(torch.cuda.is_available()) ``` -------------------------------- ### Verify PyTorch CUDA Version (Python) Source: https://connectomics.readthedocs.io/en/latest/notes/installation Prints the CUDA version that PyTorch was compiled with. This is important for ensuring compatibility between the PyTorch installation and the system's CUDA toolkit. ```python import torch print(torch.version.cuda) ``` -------------------------------- ### Get Sample Position for Testing/Validation - Python Source: https://connectomics.readthedocs.io/en/latest/_modules/connectomics/data/dataset/dataset_volume Determines the exact starting position (dataset index, z, y, x) for a given sample index, specifically for validation and testing modes. It handles potential out-of-bound positions by adjusting them. ```python def _get_pos_test(self, index): pos = [0, 0, 0, 0] did = self._index_to_dataset(index) pos[0] = did index2 = index - self.sample_num_c[did] pos[1:] = self._index_to_location(index2, self.sample_size_test[did]) # if out-of-bound, tuck in for i in range(1, 4): if pos[i] != self.sample_size[pos[0]][i-1]-1: pos[i] = int(pos[i] * self.sample_stride[i-1]) else: pos[i] = int(self.volume_size[pos[0]][i-1] - self.sample_volume_size[i-1]) return pos ``` -------------------------------- ### Visualize Training Progress with TensorBoard Source: https://connectomics.readthedocs.io/en/latest/tutorials/synapse Launches TensorBoard to visualize the training progress of the synaptic polarity detection model. It requires specifying the log directory where training outputs are saved. ```shell tensorboard --logdir outputs/Synaptic_Polarity_UNet ``` -------------------------------- ### Load CUDA Modules on FASRC Cluster (Bash) Source: https://connectomics.readthedocs.io/en/latest/notes/installation Loads the necessary CUDA and cuDNN modules on the Harvard FASRC cluster. This command is used to make CUDA-related libraries available in the environment on that specific cluster. ```bash module load cuda cudnn ``` -------------------------------- ### Add CUDA to Environment Variables (Bash) Source: https://connectomics.readthedocs.io/en/latest/notes/installation Appends the CUDA binary and include directories to the system's PATH and CPATH environment variables, respectively. This allows the system to find CUDA executables and header files, which is necessary for compilation and runtime. ```bash PATH=/usr/local/cuda/bin:$PATH echo $PATH CPATH=/usr/local/cuda/include:$CPATH echo $CPATH ``` -------------------------------- ### Run Neuron Segmentation Training (Single GPU) Source: https://connectomics.readthedocs.io/en/latest/tutorials/neuron This command initiates the neuron segmentation training process using PyTorch Connectomics. It requires activating a specific Python environment and specifies base and configuration files for training the UNet model. ```bash source activate py3_torch python -u scripts/main.py \ --config-base configs/SNEMI/SNEMI-Base.yaml \ --config-file configs/SNEMI/SNEMI-Foreground-UNet.yaml ``` -------------------------------- ### Visualize Training Progress Source: https://connectomics.readthedocs.io/en/latest/tutorials/mito Launches TensorBoard to visualize the training progress and metrics. The log directory should match the output directory specified in the configuration. ```bash tensorboard --logdir outputs/Lucchi_UNet/ ``` -------------------------------- ### Model Weight Initialization Utilities in Python Source: https://connectomics.readthedocs.io/en/latest/_modules/connectomics/model/utils/initialize Provides functions to initialize weights for PyTorch neural network models. Supports 'xavier', 'kaiming', 'selu', and 'orthogonal' initialization modes. The `model_init` function applies a chosen initialization strategy recursively to all submodules. ```python import torch import torch.nn as nn from math import sqrt def model_init(model, mode='orthogonal'): """Initialization of model weights. """ model_init_dict = { 'xavier': xavier_init, 'kaiming': kaiming_init, 'selu': selu_init, 'orthogonal': ortho_init, } # Applies fn recursively to every submodule (as returned by .children()) as well # as self. See https://pytorch.org/docs/stable/generated/torch.nn.Module.html. model.apply(model_init_dict[mode]) def xavier_init(model): # sxavier initialization for m in model.modules(): if isinstance(m, (nn.Conv2d, nn.Conv3d, nn.Linear)): nn.init.xavier_uniform_(m.weight, gain=nn.init.calculate_gain('relu')) if m.bias is not None: nn.init.zeros_(m.bias) def kaiming_init(model): # he initialization for m in model.modules(): if isinstance(m, (nn.Conv2d, nn.Conv3d, nn.Linear)): nn.init.kaiming_normal_(m.weight, mode='fan_in') def selu_init(model): # selu init for m in model.modules(): if isinstance(m, (nn.Conv2d, nn.Conv3d)): fan_in = m.kernel_size[0] * m.kernel_size[1] * m.in_channels nn.init.normal(m.weight, 0, sqrt(1. / fan_in)) elif isinstance(m, nn.Linear): fan_in = m.in_features nn.init.normal(m.weight, 0, sqrt(1. / fan_in)) def ortho_init(model): # orthogonal initialization for m in model.modules(): if isinstance(m, (nn.Conv2d, nn.Conv3d, nn.Linear)): nn.init.orthogonal_(m.weight) ``` -------------------------------- ### Download Dataset Source: https://connectomics.readthedocs.io/en/latest/tutorials/mito This command downloads the Lucchi dataset, which is used for mitochondria segmentation. Ensure you have wget installed and sufficient disk space. ```bash wget http://rhoana.rc.fas.harvard.edu/dataset/lucchi.zip ``` -------------------------------- ### Get Activation Layer Source: https://connectomics.readthedocs.io/en/latest/_modules/connectomics/model/utils/misc Retrieves a specified activation layer module from a predefined dictionary. Supports various common activation functions. ```APIDOC ## GET /activation ### Description Retrieves a specified activation layer module. Supports various common activation functions like 'relu', 'leaky_relu', 'elu', 'gelu', 'silu', 'swish', 'efficient_swish', and 'none'. ### Method GET ### Endpoint /activation ### Query Parameters - **activation** (str) - Optional - The name of the activation layer to retrieve. Defaults to 'relu'. Possible values: 'relu', 'leaky_relu', 'elu', 'gelu', 'silu', 'swish', 'efficient_swish', 'none'. ### Response #### Success Response (200) - **activation_module** (nn.Module) - The requested activation layer instance. #### Response Example ```json { "activation_module": "" } ``` ``` -------------------------------- ### Finetuning from a Saved Checkpoint in PyTorch Connectomics Source: https://connectomics.readthedocs.io/en/latest/notes/faq This explains how to finetune a PyTorch Connectomics model from a saved checkpoint. It involves adding a `--checkpoint` argument to the training command. To restart training from the beginning on a new dataset, set `SOLVER.ITERATION_RESTART` to `True`. ```bash python train.py --checkpoint checkpoint_xxxxx.pth.tar --solver.iteration_restart True ``` -------------------------------- ### PyTorch Distributed DataLoader Setup Source: https://connectomics.readthedocs.io/en/latest/_modules/connectomics/data/dataset/build Sets up a PyTorch DataLoader for distributed training using DistributedSampler. It configures batch size, shuffling, collate function, sampler, number of workers, and memory pinning. Note that each worker creates a copy of the dataset, potentially increasing memory usage if data is preloaded. ```python sampler = torch.utils.data.distributed.DistributedSampler(dataset) img_loader = torch.utils.data.DataLoader( dataset, batch_size=batch_size, shuffle=False, collate_fn=cf, sampler=sampler, num_workers=num_workers, pin_memory=True) return img_loader ``` -------------------------------- ### Perform Training connectomics.engine Trainer Source: https://connectomics.readthedocs.io/en/latest/modules/engine Starts the model training process. This method encapsulates the core training loop, including forward and backward passes, and optimizer updates. ```python trainer.train() ``` -------------------------------- ### Get Functional Activation Source: https://connectomics.readthedocs.io/en/latest/_modules/connectomics/model/utils/misc Retrieves a specified functional activation function. Supports functions like 'relu', 'tanh', 'elu', 'sigmoid', 'softmax', and 'none'. ```APIDOC ## GET /functional_activation ### Description Retrieves a specified functional activation function. Supports functions like 'relu', 'tanh', 'elu', 'sigmoid', 'softmax', and 'none'. ### Method GET ### Endpoint /functional_activation ### Query Parameters - **activation** (str) - Optional - The name of the functional activation to retrieve. Defaults to 'sigmoid'. Possible values: 'relu', 'tanh', 'elu', 'sigmoid', 'softmax', 'none'. ### Response #### Success Response (200) - **functional_activation** (function) - The requested functional activation callable. #### Response Example ```json { "functional_activation": "" } ``` ``` -------------------------------- ### Initialize Dataset Configuration - Python Source: https://connectomics.readthedocs.io/en/latest/_modules/connectomics/data/dataset/dataset_volume Initializes the dataset configuration, calculating various sizes and ratios for volumes, samples, and labels. It handles multi-volume inputs, determines sample strides, and sets the number of iterations based on the dataset mode (train, val, test). ```python def __init__(self, data_match_act, volume, sample_volume_size, sample_label_size=None, label=None, augmentor=None, sample_stride=[1, 1, 1], valid_mask=None, valid_ratio=0.0, mode='train', target_opt='mask', erosion_rates=[0.0], dilation_rates=[0.0], iter_num=1000, do_2d=False): self.data_match_act = data_match_act # dataset: channels, depths, rows, cols # volume size, could be multi-volume input self.volume_size = [np.array(x.shape) for x in self.volume] self.sample_volume_size = np.array(sample_volume_size).astype(int) # model input size if self.label is not None: self.sample_label_size = np.array(sample_label_size).astype(int) # model label size self.label_vol_ratio = self.sample_label_size / self.sample_volume_size if self.augmentor is not None: assert np.array_equal( self.augmentor.sample_size, self.sample_label_size) self._assert_valid_shape() # compute number of samples for each dataset (multi-volume input) self.sample_stride = np.array(sample_stride).astype(int) self.sample_size = [count_volume(self.volume_size[x], self.sample_volume_size, self.sample_stride) for x in range(len(self.volume_size))] # total number of possible inputs for each volume self.sample_num = np.array([np.prod(x) for x in self.sample_size]) self.sample_num_a = np.sum(self.sample_num) self.sample_num_c = np.cumsum([0] + list(self.sample_num)) # handle partially labeled volume self.valid_mask = valid_mask self.valid_ratio = valid_ratio if self.mode in ['val', 'test']: # for validation and test self.sample_size_test = [ np.array([np.prod(x[1:3]), x[2]]) for x in self.sample_size] # For relatively small volumes, the total number of samples can be generated is smaller # than the number of samples required for training (i.e., iteration * batch size). Thus # we let the __len__() of the dataset return the larger value among the two during training. self.iter_num = max( iter_num, self.sample_num_a) if self.mode == 'train' else self.sample_num_a print('Total number of samples to be generated: ', self.iter_num) ``` -------------------------------- ### Initialize Neuroglancer for Local Dataset Source: https://connectomics.readthedocs.io/en/latest/external/neuroglancer Initializes Neuroglancer and sets up the server configuration for loading local datasets. This snippet prepares the environment for displaying local TIFF or HDF5 files, such as the SNEMI neuron segmentation dataset. ```python import neuroglancer import numpy as np import imageio import h5py ip = 'localhost' #or public IP of the machine for sharable display port = 9999 #change to an unused port number neuroglancer.set_server_bind_address(bind_address=ip,bind_port=port) viewer=neuroglancer.Viewer() ``` -------------------------------- ### Get Random Sample Position for Training - Python Source: https://connectomics.readthedocs.io/en/latest/_modules/connectomics/data/dataset/dataset_volume Placeholder function for calculating random sample positions during training. This is typically used for rejection sampling or other random sampling strategies. ```python def _get_pos_train(self, vol_size): # random: multithread ```