pansharpening


Namepansharpening JSON
Version 0.1.0 PyPI version JSON
download
home_pagehttps://XavierJiezou.github.io/Pansharpening
SummaryDeep learning for pansharpening in remote sensing
upload_time2023-10-28 16:01:39
maintainer
docs_urlNone
authorXavierJiezou
requires_python>=3.8.1,<4.0.0
licenseMIT
keywords deep learning remote sensing pansharpening
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <div align="center">

# Lightning-Hydra-Template

[![python](https://img.shields.io/badge/-Python_3.8_%7C_3.9_%7C_3.10-blue?logo=python&logoColor=white)](https://github.com/pre-commit/pre-commit)
[![pytorch](https://img.shields.io/badge/PyTorch_2.0+-ee4c2c?logo=pytorch&logoColor=white)](https://pytorch.org/get-started/locally/)
[![lightning](https://img.shields.io/badge/-Lightning_2.0+-792ee5?logo=pytorchlightning&logoColor=white)](https://pytorchlightning.ai/)
[![hydra](https://img.shields.io/badge/Config-Hydra_1.3-89b8cd)](https://hydra.cc/)
[![black](https://img.shields.io/badge/Code%20Style-Black-black.svg?labelColor=gray)](https://black.readthedocs.io/en/stable/)
[![isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/) <br>
[![tests](https://github.com/ashleve/lightning-hydra-template/actions/workflows/test.yml/badge.svg)](https://github.com/ashleve/lightning-hydra-template/actions/workflows/test.yml)
[![code-quality](https://github.com/ashleve/lightning-hydra-template/actions/workflows/code-quality-main.yaml/badge.svg)](https://github.com/ashleve/lightning-hydra-template/actions/workflows/code-quality-main.yaml)
[![codecov](https://codecov.io/gh/ashleve/lightning-hydra-template/branch/main/graph/badge.svg)](https://codecov.io/gh/ashleve/lightning-hydra-template) <br>
[![license](https://img.shields.io/badge/License-MIT-green.svg?labelColor=gray)](https://github.com/ashleve/lightning-hydra-template#license)
[![PRs](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/ashleve/lightning-hydra-template/pulls)
[![contributors](https://img.shields.io/github/contributors/ashleve/lightning-hydra-template.svg)](https://github.com/ashleve/lightning-hydra-template/graphs/contributors)

A clean template to kickstart your deep learning project πŸš€βš‘πŸ”₯<br>
Click on [<kbd>Use this template</kbd>](https://github.com/ashleve/lightning-hydra-template/generate) to initialize new repository.

_Suggestions are always welcome!_

</div>

<br>

## πŸ“ŒΒ Β Introduction

**Why you might want to use it:**

βœ… Save on boilerplate <br>
Easily add new models, datasets, tasks, experiments, and train on different accelerators, like multi-GPU, TPU or SLURM clusters.

βœ… Education <br>
Thoroughly commented. You can use this repo as a learning resource.

βœ… Reusability <br>
Collection of useful MLOps tools, configs, and code snippets. You can use this repo as a reference for various utilities.

**Why you might not want to use it:**

❌ Things break from time to time <br>
Lightning and Hydra are still evolving and integrate many libraries, which means sometimes things break. For the list of currently known problems visit [this page](https://github.com/ashleve/lightning-hydra-template/labels/bug).

❌ Not adjusted for data engineering <br>
Template is not really adjusted for building data pipelines that depend on each other. It's more efficient to use it for model prototyping on ready-to-use data.

❌ Overfitted to simple use case <br>
The configuration setup is built with simple lightning training in mind. You might need to put some effort to adjust it for different use cases, e.g. lightning fabric.

❌ Might not support your workflow <br>
For example, you can't resume hydra-based multirun or hyperparameter search.

> **Note**: _Keep in mind this is unofficial community project._

<br>

## Main Technologies

[PyTorch Lightning](https://github.com/PyTorchLightning/pytorch-lightning) - a lightweight PyTorch wrapper for high-performance AI research. Think of it as a framework for organizing your PyTorch code.

[Hydra](https://github.com/facebookresearch/hydra) - a framework for elegantly configuring complex applications. The key feature is the ability to dynamically create a hierarchical configuration by composition and override it through config files and the command line.

<br>

## Main Ideas

- [**Rapid Experimentation**](#your-superpowers): thanks to hydra command line superpowers
- [**Minimal Boilerplate**](#how-it-works): thanks to automating pipelines with config instantiation
- [**Main Configs**](#main-config): allow you to specify default training configuration
- [**Experiment Configs**](#experiment-config): allow you to override chosen hyperparameters and version control experiments
- [**Workflow**](#workflow): comes down to 4 simple steps
- [**Experiment Tracking**](#experiment-tracking): Tensorboard, W&B, Neptune, Comet, MLFlow and CSVLogger
- [**Logs**](#logs): all logs (checkpoints, configs, etc.) are stored in a dynamically generated folder structure
- [**Hyperparameter Search**](#hyperparameter-search): simple search is effortless with Hydra plugins like Optuna Sweeper
- [**Tests**](#tests): generic, easy-to-adapt smoke tests for speeding up the development
- [**Continuous Integration**](#continuous-integration): automatically test and lint your repo with Github Actions
- [**Best Practices**](#best-practices): a couple of recommended tools, practices and standards

<br>

## Project Structure

The directory structure of new project looks like this:

```
β”œβ”€β”€ .github                   <- Github Actions workflows
β”‚
β”œβ”€β”€ configs                   <- Hydra configs
β”‚   β”œβ”€β”€ callbacks                <- Callbacks configs
β”‚   β”œβ”€β”€ data                     <- Data configs
β”‚   β”œβ”€β”€ debug                    <- Debugging configs
β”‚   β”œβ”€β”€ experiment               <- Experiment configs
β”‚   β”œβ”€β”€ extras                   <- Extra utilities configs
β”‚   β”œβ”€β”€ hparams_search           <- Hyperparameter search configs
β”‚   β”œβ”€β”€ hydra                    <- Hydra configs
β”‚   β”œβ”€β”€ local                    <- Local configs
β”‚   β”œβ”€β”€ logger                   <- Logger configs
β”‚   β”œβ”€β”€ model                    <- Model configs
β”‚   β”œβ”€β”€ paths                    <- Project paths configs
β”‚   β”œβ”€β”€ trainer                  <- Trainer configs
β”‚   β”‚
β”‚   β”œβ”€β”€ eval.yaml             <- Main config for evaluation
β”‚   └── train.yaml            <- Main config for training
β”‚
β”œβ”€β”€ data                   <- Project data
β”‚
β”œβ”€β”€ logs                   <- Logs generated by hydra and lightning loggers
β”‚
β”œβ”€β”€ notebooks              <- Jupyter notebooks. Naming convention is a number (for ordering),
β”‚                             the creator's initials, and a short `-` delimited description,
β”‚                             e.g. `1.0-jqp-initial-data-exploration.ipynb`.
β”‚
β”œβ”€β”€ scripts                <- Shell scripts
β”‚
β”œβ”€β”€ src                    <- Source code
β”‚   β”œβ”€β”€ data                     <- Data scripts
β”‚   β”œβ”€β”€ models                   <- Model scripts
β”‚   β”œβ”€β”€ utils                    <- Utility scripts
β”‚   β”‚
β”‚   β”œβ”€β”€ eval.py                  <- Run evaluation
β”‚   └── train.py                 <- Run training
β”‚
β”œβ”€β”€ tests                  <- Tests of any kind
β”‚
β”œβ”€β”€ .env.example              <- Example of file for storing private environment variables
β”œβ”€β”€ .gitignore                <- List of files ignored by git
β”œβ”€β”€ .pre-commit-config.yaml   <- Configuration of pre-commit hooks for code formatting
β”œβ”€β”€ .project-root             <- File for inferring the position of project root directory
β”œβ”€β”€ environment.yaml          <- File for installing conda environment
β”œβ”€β”€ Makefile                  <- Makefile with commands like `make train` or `make test`
β”œβ”€β”€ pyproject.toml            <- Configuration options for testing and linting
β”œβ”€β”€ requirements.txt          <- File for installing python dependencies
β”œβ”€β”€ setup.py                  <- File for installing project as a package
└── README.md
```

<br>

## πŸš€Β Β Quickstart

```bash
# clone project
git clone https://github.com/ashleve/lightning-hydra-template
cd lightning-hydra-template

# [OPTIONAL] create conda environment
conda create -n myenv python=3.9
conda activate myenv

# install pytorch according to instructions
# https://pytorch.org/get-started/

# install requirements
pip install -r requirements.txt
```

Template contains example with MNIST classification.<br>
When running `python src/train.py` you should see something like this:

<div align="center">

![](https://github.com/ashleve/lightning-hydra-template/blob/resources/terminal.png)

</div>

## ⚑  Your Superpowers

<details>
<summary><b>Override any config parameter from command line</b></summary>

```bash
python train.py trainer.max_epochs=20 model.optimizer.lr=1e-4
```

> **Note**: You can also add new parameters with `+` sign.

```bash
python train.py +model.new_param="owo"
```

</details>

<details>
<summary><b>Train on CPU, GPU, multi-GPU and TPU</b></summary>

```bash
# train on CPU
python train.py trainer=cpu

# train on 1 GPU
python train.py trainer=gpu

# train on TPU
python train.py +trainer.tpu_cores=8

# train with DDP (Distributed Data Parallel) (4 GPUs)
python train.py trainer=ddp trainer.devices=4

# train with DDP (Distributed Data Parallel) (8 GPUs, 2 nodes)
python train.py trainer=ddp trainer.devices=4 trainer.num_nodes=2

# simulate DDP on CPU processes
python train.py trainer=ddp_sim trainer.devices=2

# accelerate training on mac
python train.py trainer=mps
```

> **Warning**: Currently there are problems with DDP mode, read [this issue](https://github.com/ashleve/lightning-hydra-template/issues/393) to learn more.

</details>

<details>
<summary><b>Train with mixed precision</b></summary>

```bash
# train with pytorch native automatic mixed precision (AMP)
python train.py trainer=gpu +trainer.precision=16
```

</details>

<!-- deepspeed support still in beta
<details>
<summary><b>Optimize large scale models on multiple GPUs with Deepspeed</b></summary>

```bash
python train.py +trainer.
```

</details>
 -->

<details>
<summary><b>Train model with any logger available in PyTorch Lightning, like W&B or Tensorboard</b></summary>

```yaml
# set project and entity names in `configs/logger/wandb`
wandb:
  project: "your_project_name"
  entity: "your_wandb_team_name"
```

```bash
# train model with Weights&Biases (link to wandb dashboard should appear in the terminal)
python train.py logger=wandb
```

> **Note**: Lightning provides convenient integrations with most popular logging frameworks. Learn more [here](#experiment-tracking).

> **Note**: Using wandb requires you to [setup account](https://www.wandb.com/) first. After that just complete the config as below.

> **Note**: Click [here](https://wandb.ai/hobglob/template-dashboard/) to see example wandb dashboard generated with this template.

</details>

<details>
<summary><b>Train model with chosen experiment config</b></summary>

```bash
python train.py experiment=example
```

> **Note**: Experiment configs are placed in [configs/experiment/](configs/experiment/).

</details>

<details>
<summary><b>Attach some callbacks to run</b></summary>

```bash
python train.py callbacks=default
```

> **Note**: Callbacks can be used for things such as as model checkpointing, early stopping and [many more](https://pytorch-lightning.readthedocs.io/en/latest/extensions/callbacks.html#built-in-callbacks).

> **Note**: Callbacks configs are placed in [configs/callbacks/](configs/callbacks/).

</details>

<details>
<summary><b>Use different tricks available in Pytorch Lightning</b></summary>

```yaml
# gradient clipping may be enabled to avoid exploding gradients
python train.py +trainer.gradient_clip_val=0.5

# run validation loop 4 times during a training epoch
python train.py +trainer.val_check_interval=0.25

# accumulate gradients
python train.py +trainer.accumulate_grad_batches=10

# terminate training after 12 hours
python train.py +trainer.max_time="00:12:00:00"
```

> **Note**: PyTorch Lightning provides about [40+ useful trainer flags](https://pytorch-lightning.readthedocs.io/en/latest/common/trainer.html#trainer-flags).

</details>

<details>
<summary><b>Easily debug</b></summary>

```bash
# runs 1 epoch in default debugging mode
# changes logging directory to `logs/debugs/...`
# sets level of all command line loggers to 'DEBUG'
# enforces debug-friendly configuration
python train.py debug=default

# run 1 train, val and test loop, using only 1 batch
python train.py debug=fdr

# print execution time profiling
python train.py debug=profiler

# try overfitting to 1 batch
python train.py debug=overfit

# raise exception if there are any numerical anomalies in tensors, like NaN or +/-inf
python train.py +trainer.detect_anomaly=true

# use only 20% of the data
python train.py +trainer.limit_train_batches=0.2 \
+trainer.limit_val_batches=0.2 +trainer.limit_test_batches=0.2
```

> **Note**: Visit [configs/debug/](configs/debug/) for different debugging configs.

</details>

<details>
<summary><b>Resume training from checkpoint</b></summary>

```yaml
python train.py ckpt_path="/path/to/ckpt/name.ckpt"
```

> **Note**: Checkpoint can be either path or URL.

> **Note**: Currently loading ckpt doesn't resume logger experiment, but it will be supported in future Lightning release.

</details>

<details>
<summary><b>Evaluate checkpoint on test dataset</b></summary>

```yaml
python eval.py ckpt_path="/path/to/ckpt/name.ckpt"
```

> **Note**: Checkpoint can be either path or URL.

</details>

<details>
<summary><b>Create a sweep over hyperparameters</b></summary>

```bash
# this will run 6 experiments one after the other,
# each with different combination of batch_size and learning rate
python train.py -m data.batch_size=32,64,128 model.lr=0.001,0.0005
```

> **Note**: Hydra composes configs lazily at job launch time. If you change code or configs after launching a job/sweep, the final composed configs might be impacted.

</details>

<details>
<summary><b>Create a sweep over hyperparameters with Optuna</b></summary>

```bash
# this will run hyperparameter search defined in `configs/hparams_search/mnist_optuna.yaml`
# over chosen experiment config
python train.py -m hparams_search=mnist_optuna experiment=example
```

> **Note**: Using [Optuna Sweeper](https://hydra.cc/docs/next/plugins/optuna_sweeper) doesn't require you to add any boilerplate to your code, everything is defined in a [single config file](configs/hparams_search/mnist_optuna.yaml).

> **Warning**: Optuna sweeps are not failure-resistant (if one job crashes then the whole sweep crashes).

</details>

<details>
<summary><b>Execute all experiments from folder</b></summary>

```bash
python train.py -m 'experiment=glob(*)'
```

> **Note**: Hydra provides special syntax for controlling behavior of multiruns. Learn more [here](https://hydra.cc/docs/next/tutorials/basic/running_your_app/multi-run). The command above executes all experiments from [configs/experiment/](configs/experiment/).

</details>

<details>
<summary><b>Execute run for multiple different seeds</b></summary>

```bash
python train.py -m seed=1,2,3,4,5 trainer.deterministic=True logger=csv tags=["benchmark"]
```

> **Note**: `trainer.deterministic=True` makes pytorch more deterministic but impacts the performance.

</details>

<details>
<summary><b>Execute sweep on a remote AWS cluster</b></summary>

> **Note**: This should be achievable with simple config using [Ray AWS launcher for Hydra](https://hydra.cc/docs/next/plugins/ray_launcher). Example is not implemented in this template.

</details>

<!-- <details>
<summary><b>Execute sweep on a SLURM cluster</b></summary>

> This should be achievable with either [the right lightning trainer flags](https://pytorch-lightning.readthedocs.io/en/latest/clouds/cluster.html?highlight=SLURM#slurm-managed-cluster) or simple config using [Submitit launcher for Hydra](https://hydra.cc/docs/plugins/submitit_launcher). Example is not yet implemented in this template.

</details> -->

<details>
<summary><b>Use Hydra tab completion</b></summary>

> **Note**: Hydra allows you to autocomplete config argument overrides in shell as you write them, by pressing `tab` key. Read the [docs](https://hydra.cc/docs/tutorials/basic/running_your_app/tab_completion).

</details>

<details>
<summary><b>Apply pre-commit hooks</b></summary>

```bash
pre-commit run -a
```

> **Note**: Apply pre-commit hooks to do things like auto-formatting code and configs, performing code analysis or removing output from jupyter notebooks. See [# Best Practices](#best-practices) for more.

Update pre-commit hook versions in `.pre-commit-config.yaml` with:

```bash
pre-commit autoupdate
```

</details>

<details>
<summary><b>Run tests</b></summary>

```bash
# run all tests
pytest

# run tests from specific file
pytest tests/test_train.py

# run all tests except the ones marked as slow
pytest -k "not slow"
```

</details>

<details>
<summary><b>Use tags</b></summary>

Each experiment should be tagged in order to easily filter them across files or in logger UI:

```bash
python train.py tags=["mnist","experiment_X"]
```

> **Note**: You might need to escape the bracket characters in your shell with `python train.py tags=\["mnist","experiment_X"\]`.

If no tags are provided, you will be asked to input them from command line:

```bash
>>> python train.py tags=[]
[2022-07-11 15:40:09,358][src.utils.utils][INFO] - Enforcing tags! <cfg.extras.enforce_tags=True>
[2022-07-11 15:40:09,359][src.utils.rich_utils][WARNING] - No tags provided in config. Prompting user to input tags...
Enter a list of comma separated tags (dev):
```

If no tags are provided for multirun, an error will be raised:

```bash
>>> python train.py -m +x=1,2,3 tags=[]
ValueError: Specify tags before launching a multirun!
```

> **Note**: Appending lists from command line is currently not supported in hydra :(

</details>

<br>

## ❀️  Contributions

This project exists thanks to all the people who contribute.

![Contributors](https://readme-contributors.now.sh/ashleve/lightning-hydra-template?extension=jpg&width=400&aspectRatio=1)

Have a question? Found a bug? Missing a specific feature? Feel free to file a new issue, discussion or PR with respective title and description.

Before making an issue, please verify that:

- The problem still exists on the current `main` branch.
- Your python dependencies are updated to recent versions.

Suggestions for improvements are always welcome!

<br>

## How It Works

All PyTorch Lightning modules are dynamically instantiated from module paths specified in config. Example model config:

```yaml
_target_: src.models.mnist_model.MNISTLitModule
lr: 0.001
net:
  _target_: src.models.components.simple_dense_net.SimpleDenseNet
  input_size: 784
  lin1_size: 256
  lin2_size: 256
  lin3_size: 256
  output_size: 10
```

Using this config we can instantiate the object with the following line:

```python
model = hydra.utils.instantiate(config.model)
```

This allows you to easily iterate over new models! Every time you create a new one, just specify its module path and parameters in appropriate config file. <br>

Switch between models and datamodules with command line arguments:

```bash
python train.py model=mnist
```

Example pipeline managing the instantiation logic: [src/train.py](src/train.py).

<br>

## Main Config

Location: [configs/train.yaml](configs/train.yaml) <br>
Main project config contains default training configuration.<br>
It determines how config is composed when simply executing command `python train.py`.<br>

<details>
<summary><b>Show main project config</b></summary>

```yaml
# order of defaults determines the order in which configs override each other
defaults:
  - _self_
  - data: mnist.yaml
  - model: mnist.yaml
  - callbacks: default.yaml
  - logger: null # set logger here or use command line (e.g. `python train.py logger=csv`)
  - trainer: default.yaml
  - paths: default.yaml
  - extras: default.yaml
  - hydra: default.yaml

  # experiment configs allow for version control of specific hyperparameters
  # e.g. best hyperparameters for given model and datamodule
  - experiment: null

  # config for hyperparameter optimization
  - hparams_search: null

  # optional local config for machine/user specific settings
  # it's optional since it doesn't need to exist and is excluded from version control
  - optional local: default.yaml

  # debugging config (enable through command line, e.g. `python train.py debug=default)
  - debug: null

# task name, determines output directory path
task_name: "train"

# tags to help you identify your experiments
# you can overwrite this in experiment configs
# overwrite from command line with `python train.py tags="[first_tag, second_tag]"`
# appending lists from command line is currently not supported :(
# https://github.com/facebookresearch/hydra/issues/1547
tags: ["dev"]

# set False to skip model training
train: True

# evaluate on test set, using best model weights achieved during training
# lightning chooses best weights based on the metric specified in checkpoint callback
test: True

# simply provide checkpoint path to resume training
ckpt_path: null

# seed for random number generators in pytorch, numpy and python.random
seed: null
```

</details>

<br>

## Experiment Config

Location: [configs/experiment](configs/experiment)<br>
Experiment configs allow you to overwrite parameters from main config.<br>
For example, you can use them to version control best hyperparameters for each combination of model and dataset.

<details>
<summary><b>Show example experiment config</b></summary>

```yaml
# @package _global_

# to execute this experiment run:
# python train.py experiment=example

defaults:
  - override /data: mnist.yaml
  - override /model: mnist.yaml
  - override /callbacks: default.yaml
  - override /trainer: default.yaml

# all parameters below will be merged with parameters from default configurations set above
# this allows you to overwrite only specified parameters

tags: ["mnist", "simple_dense_net"]

seed: 12345

trainer:
  min_epochs: 10
  max_epochs: 10
  gradient_clip_val: 0.5

model:
  optimizer:
    lr: 0.002
  net:
    lin1_size: 128
    lin2_size: 256
    lin3_size: 64

data:
  batch_size: 64

logger:
  wandb:
    tags: ${tags}
    group: "mnist"
```

</details>

<br>

## Workflow

**Basic workflow**

1. Write your PyTorch Lightning module (see [models/mnist_module.py](src/models/mnist_module.py) for example)
2. Write your PyTorch Lightning datamodule (see [data/mnist_datamodule.py](src/data/mnist_datamodule.py) for example)
3. Write your experiment config, containing paths to model and datamodule
4. Run training with chosen experiment config:
   ```bash
   python src/train.py experiment=experiment_name.yaml
   ```

**Experiment design**

_Say you want to execute many runs to plot how accuracy changes in respect to batch size._

1. Execute the runs with some config parameter that allows you to identify them easily, like tags:

   ```bash
   python train.py -m logger=csv data.batch_size=16,32,64,128 tags=["batch_size_exp"]
   ```

2. Write a script or notebook that searches over the `logs/` folder and retrieves csv logs from runs containing given tags in config. Plot the results.

<br>

## Logs

Hydra creates new output directory for every executed run.

Default logging structure:

```
β”œβ”€β”€ logs
β”‚   β”œβ”€β”€ task_name
β”‚   β”‚   β”œβ”€β”€ runs                        # Logs generated by single runs
β”‚   β”‚   β”‚   β”œβ”€β”€ YYYY-MM-DD_HH-MM-SS       # Datetime of the run
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ .hydra                  # Hydra logs
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ csv                     # Csv logs
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ wandb                   # Weights&Biases logs
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ checkpoints             # Training checkpoints
β”‚   β”‚   β”‚   β”‚   └── ...                     # Any other thing saved during training
β”‚   β”‚   β”‚   └── ...
β”‚   β”‚   β”‚
β”‚   β”‚   └── multiruns                   # Logs generated by multiruns
β”‚   β”‚       β”œβ”€β”€ YYYY-MM-DD_HH-MM-SS       # Datetime of the multirun
β”‚   β”‚       β”‚   β”œβ”€β”€1                        # Multirun job number
β”‚   β”‚       β”‚   β”œβ”€β”€2
β”‚   β”‚       β”‚   └── ...
β”‚   β”‚       └── ...
β”‚   β”‚
β”‚   └── debugs                          # Logs generated when debugging config is attached
β”‚       └── ...
```

</details>

You can change this structure by modifying paths in [hydra configuration](configs/hydra).

<br>

## Experiment Tracking

PyTorch Lightning supports many popular logging frameworks: [Weights&Biases](https://www.wandb.com/), [Neptune](https://neptune.ai/), [Comet](https://www.comet.ml/), [MLFlow](https://mlflow.org), [Tensorboard](https://www.tensorflow.org/tensorboard/).

These tools help you keep track of hyperparameters and output metrics and allow you to compare and visualize results. To use one of them simply complete its configuration in [configs/logger](configs/logger) and run:

```bash
python train.py logger=logger_name
```

You can use many of them at once (see [configs/logger/many_loggers.yaml](configs/logger/many_loggers.yaml) for example).

You can also write your own logger.

Lightning provides convenient method for logging custom metrics from inside LightningModule. Read the [docs](https://pytorch-lightning.readthedocs.io/en/latest/extensions/logging.html#automatic-logging) or take a look at [MNIST example](src/models/mnist_module.py).

<br>

## Tests

Template comes with generic tests implemented with `pytest`.

```bash
# run all tests
pytest

# run tests from specific file
pytest tests/test_train.py

# run all tests except the ones marked as slow
pytest -k "not slow"
```

Most of the implemented tests don't check for any specific output - they exist to simply verify that executing some commands doesn't end up in throwing exceptions. You can execute them once in a while to speed up the development.

Currently, the tests cover cases like:

- running 1 train, val and test step
- running 1 epoch on 1% of data, saving ckpt and resuming for the second epoch
- running 2 epochs on 1% of data, with DDP simulated on CPU

And many others. You should be able to modify them easily for your use case.

There is also `@RunIf` decorator implemented, that allows you to run tests only if certain conditions are met, e.g. GPU is available or system is not windows. See the [examples](tests/test_train.py).

<br>

## Hyperparameter Search

You can define hyperparameter search by adding new config file to [configs/hparams_search](configs/hparams_search).

<details>
<summary><b>Show example hyperparameter search config</b></summary>

```yaml
# @package _global_

defaults:
  - override /hydra/sweeper: optuna

# choose metric which will be optimized by Optuna
# make sure this is the correct name of some metric logged in lightning module!
optimized_metric: "val/acc_best"

# here we define Optuna hyperparameter search
# it optimizes for value returned from function with @hydra.main decorator
hydra:
  sweeper:
    _target_: hydra_plugins.hydra_optuna_sweeper.optuna_sweeper.OptunaSweeper

    # 'minimize' or 'maximize' the objective
    direction: maximize

    # total number of runs that will be executed
    n_trials: 20

    # choose Optuna hyperparameter sampler
    # docs: https://optuna.readthedocs.io/en/stable/reference/samplers.html
    sampler:
      _target_: optuna.samplers.TPESampler
      seed: 1234
      n_startup_trials: 10 # number of random sampling runs before optimization starts

    # define hyperparameter search space
    params:
      model.optimizer.lr: interval(0.0001, 0.1)
      data.batch_size: choice(32, 64, 128, 256)
      model.net.lin1_size: choice(64, 128, 256)
      model.net.lin2_size: choice(64, 128, 256)
      model.net.lin3_size: choice(32, 64, 128, 256)
```

</details>

Next, execute it with: `python train.py -m hparams_search=mnist_optuna`

Using this approach doesn't require adding any boilerplate to code, everything is defined in a single config file. The only necessary thing is to return the optimized metric value from the launch file.

You can use different optimization frameworks integrated with Hydra, like [Optuna, Ax or Nevergrad](https://hydra.cc/docs/plugins/optuna_sweeper/).

The `optimization_results.yaml` will be available under `logs/task_name/multirun` folder.

This approach doesn't support resuming interrupted search and advanced techniques like prunning - for more sophisticated search and workflows, you should probably write a dedicated optimization task (without multirun feature).

<br>

## Continuous Integration

Template comes with CI workflows implemented in Github Actions:

- `.github/workflows/test.yaml`: running all tests with pytest
- `.github/workflows/code-quality-main.yaml`: running pre-commits on main branch for all files
- `.github/workflows/code-quality-pr.yaml`: running pre-commits on pull requests for modified files only

<br>

## Distributed Training

Lightning supports multiple ways of doing distributed training. The most common one is DDP, which spawns separate process for each GPU and averages gradients between them. To learn about other approaches read the [lightning docs](https://lightning.ai/docs/pytorch/latest/advanced/speed.html).

You can run DDP on mnist example with 4 GPUs like this:

```bash
python train.py trainer=ddp
```

> **Note**: When using DDP you have to be careful how you write your models - read the [docs](https://lightning.ai/docs/pytorch/latest/advanced/speed.html).

<br>

## Accessing Datamodule Attributes In Model

The simplest way is to pass datamodule attribute directly to model on initialization:

```python
# ./src/train.py
datamodule = hydra.utils.instantiate(config.data)
model = hydra.utils.instantiate(config.model, some_param=datamodule.some_param)
```

> **Note**: Not a very robust solution, since it assumes all your datamodules have `some_param` attribute available.

Similarly, you can pass a whole datamodule config as an init parameter:

```python
# ./src/train.py
model = hydra.utils.instantiate(config.model, dm_conf=config.data, _recursive_=False)
```

You can also pass a datamodule config parameter to your model through variable interpolation:

```yaml
# ./configs/model/my_model.yaml
_target_: src.models.my_module.MyLitModule
lr: 0.01
some_param: ${data.some_param}
```

Another approach is to access datamodule in LightningModule directly through Trainer:

```python
# ./src/models/mnist_module.py
def on_train_start(self):
  self.some_param = self.trainer.datamodule.some_param
```

> **Note**: This only works after the training starts since otherwise trainer won't be yet available in LightningModule.

<br>

## Best Practices

<details>
<summary><b>Use Miniconda</b></summary>

It's usually unnecessary to install full anaconda environment, miniconda should be enough (weights around 80MB).

Big advantage of conda is that it allows for installing packages without requiring certain compilers or libraries to be available in the system (since it installs precompiled binaries), so it often makes it easier to install some dependencies e.g. cudatoolkit for GPU support.

It also allows you to access your environments globally which might be more convenient than creating new local environment for every project.

Example installation:

```bash
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
```

Update conda:

```bash
conda update -n base -c defaults conda
```

Create new conda environment:

```bash
conda create -n myenv python=3.10
conda activate myenv
```

</details>

<details>
<summary><b>Use automatic code formatting</b></summary>

Use pre-commit hooks to standardize code formatting of your project and save mental energy.<br>
Simply install pre-commit package with:

```bash
pip install pre-commit
```

Next, install hooks from [.pre-commit-config.yaml](.pre-commit-config.yaml):

```bash
pre-commit install
```

After that your code will be automatically reformatted on every new commit.

To reformat all files in the project use command:

```bash
pre-commit run -a
```

To update hook versions in [.pre-commit-config.yaml](.pre-commit-config.yaml) use:

```bash
pre-commit autoupdate
```

</details>

<details>
<summary><b>Set private environment variables in .env file</b></summary>

System specific variables (e.g. absolute paths to datasets) should not be under version control or it will result in conflict between different users. Your private keys also shouldn't be versioned since you don't want them to be leaked.<br>

Template contains `.env.example` file, which serves as an example. Create a new file called `.env` (this name is excluded from version control in .gitignore).
You should use it for storing environment variables like this:

```
MY_VAR=/home/user/my_system_path
```

All variables from `.env` are loaded in `train.py` automatically.

Hydra allows you to reference any env variable in `.yaml` configs like this:

```yaml
path_to_data: ${oc.env:MY_VAR}
```

</details>

<details>
<summary><b>Name metrics using '/' character</b></summary>

Depending on which logger you're using, it's often useful to define metric name with `/` character:

```python
self.log("train/loss", loss)
```

This way loggers will treat your metrics as belonging to different sections, which helps to get them organised in UI.

</details>

<details>
<summary><b>Use torchmetrics</b></summary>

Use official [torchmetrics](https://github.com/PytorchLightning/metrics) library to ensure proper calculation of metrics. This is especially important for multi-GPU training!

For example, instead of calculating accuracy by yourself, you should use the provided `Accuracy` class like this:

```python
from torchmetrics.classification.accuracy import Accuracy


class LitModel(LightningModule):
    def __init__(self)
        self.train_acc = Accuracy()
        self.val_acc = Accuracy()

    def training_step(self, batch, batch_idx):
        ...
        acc = self.train_acc(predictions, targets)
        self.log("train/acc", acc)
        ...

    def validation_step(self, batch, batch_idx):
        ...
        acc = self.val_acc(predictions, targets)
        self.log("val/acc", acc)
        ...
```

Make sure to use different metric instance for each step to ensure proper value reduction over all GPU processes.

Torchmetrics provides metrics for most use cases, like F1 score or confusion matrix. Read [documentation](https://torchmetrics.readthedocs.io/en/latest/#more-reading) for more.

</details>

<details>
<summary><b>Follow PyTorch Lightning style guide</b></summary>

The style guide is available [here](https://pytorch-lightning.readthedocs.io/en/latest/starter/style_guide.html).<br>

1. Be explicit in your init. Try to define all the relevant defaults so that the user doesn’t have to guess. Provide type hints. This way your module is reusable across projects!

   ```python
   class LitModel(LightningModule):
       def __init__(self, layer_size: int = 256, lr: float = 0.001):
   ```

2. Preserve the recommended method order.

   ```python
   class LitModel(LightningModule):

       def __init__():
           ...

       def forward():
           ...

       def training_step():
           ...

       def training_step_end():
           ...

       def on_train_epoch_end():
           ...

       def validation_step():
           ...

       def validation_step_end():
           ...

       def on_validation_epoch_end():
           ...

       def test_step():
           ...

       def test_step_end():
           ...

       def on_test_epoch_end():
           ...

       def configure_optimizers():
           ...

       def any_extra_hook():
           ...
   ```

</details>

<details>
<summary><b>Version control your data and models with DVC</b></summary>

Use [DVC](https://dvc.org) to version control big files, like your data or trained ML models.<br>
To initialize the dvc repository:

```bash
dvc init
```

To start tracking a file or directory, use `dvc add`:

```bash
dvc add data/MNIST
```

DVC stores information about the added file (or a directory) in a special .dvc file named data/MNIST.dvc, a small text file with a human-readable format. This file can be easily versioned like source code with Git, as a placeholder for the original data:

```bash
git add data/MNIST.dvc data/.gitignore
git commit -m "Add raw data"
```

</details>

<details>
<summary><b>Support installing project as a package</b></summary>

It allows other people to easily use your modules in their own projects.
Change name of the `src` folder to your project name and complete the `setup.py` file.

Now your project can be installed from local files:

```bash
pip install -e .
```

Or directly from git repository:

```bash
pip install git+git://github.com/YourGithubName/your-repo-name.git --upgrade
```

So any file can be easily imported into any other file like so:

```python
from project_name.models.mnist_module import MNISTLitModule
from project_name.data.mnist_datamodule import MNISTDataModule
```

</details>

<details>
<summary><b>Keep local configs out of code versioning</b></summary>

Some configurations are user/machine/installation specific (e.g. configuration of local cluster, or harddrive paths on a specific machine). For such scenarios, a file [configs/local/default.yaml](configs/local/) can be created which is automatically loaded but not tracked by Git.

For example, you can use it for a SLURM cluster config:

```yaml
# @package _global_

defaults:
  - override /hydra/launcher@_here_: submitit_slurm

data_dir: /mnt/scratch/data/

hydra:
  launcher:
    timeout_min: 1440
    gpus_per_task: 1
    gres: gpu:1
  job:
    env_set:
      MY_VAR: /home/user/my/system/path
      MY_KEY: asdgjhawi8y23ihsghsueity23ihwd
```

</details>

<br>

## Resources

This template was inspired by:

- [PyTorchLightning/deep-learning-project-template](https://github.com/PyTorchLightning/deep-learning-project-template)
- [drivendata/cookiecutter-data-science](https://github.com/drivendata/cookiecutter-data-science)
- [lucmos/nn-template](https://github.com/lucmos/nn-template)

Other useful repositories:

- [jxpress/lightning-hydra-template-vertex-ai](https://github.com/jxpress/lightning-hydra-template-vertex-ai) - lightning-hydra-template integration with Vertex AI hyperparameter tuning and custom training job

</details>

<br>

## License

Lightning-Hydra-Template is licensed under the MIT License.

```
MIT License

Copyright (c) 2021 ashleve

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```

<br>
<br>
<br>
<br>

**DELETE EVERYTHING ABOVE FOR YOUR PROJECT**

______________________________________________________________________

<div align="center">

# Your Project Name

<a href="https://pytorch.org/get-started/locally/"><img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-ee4c2c?logo=pytorch&logoColor=white"></a>
<a href="https://pytorchlightning.ai/"><img alt="Lightning" src="https://img.shields.io/badge/-Lightning-792ee5?logo=pytorchlightning&logoColor=white"></a>
<a href="https://hydra.cc/"><img alt="Config: Hydra" src="https://img.shields.io/badge/Config-Hydra-89b8cd"></a>
<a href="https://github.com/ashleve/lightning-hydra-template"><img alt="Template" src="https://img.shields.io/badge/-Lightning--Hydra--Template-017F2F?style=flat&logo=github&labelColor=gray"></a><br>
[![Paper](http://img.shields.io/badge/paper-arxiv.1001.2234-B31B1B.svg)](https://www.nature.com/articles/nature14539)
[![Conference](http://img.shields.io/badge/AnyConference-year-4b44ce.svg)](https://papers.nips.cc/paper/2020)

</div>

## Description

What it does

## Installation

#### Pip

```bash
# clone project
git clone https://github.com/YourGithubName/your-repo-name
cd your-repo-name

# [OPTIONAL] create conda environment
conda create -n myenv python=3.9
conda activate myenv

# install pytorch according to instructions
# https://pytorch.org/get-started/

# install requirements
pip install -r requirements.txt
```

#### Conda

```bash
# clone project
git clone https://github.com/YourGithubName/your-repo-name
cd your-repo-name

# create conda environment and install dependencies
conda env create -f environment.yaml -n myenv

# activate conda environment
conda activate myenv
```

## How to run

Train model with default configuration

```bash
# train on CPU
python src/train.py trainer=cpu

# train on GPU
python src/train.py trainer=gpu
```

Train model with chosen experiment configuration from [configs/experiment/](configs/experiment/)

```bash
python src/train.py experiment=experiment_name.yaml
```

You can override any parameter from command line like this

```bash
python src/train.py trainer.max_epochs=20 data.batch_size=64
```


            

Raw data

            {
    "_id": null,
    "home_page": "https://XavierJiezou.github.io/Pansharpening",
    "name": "pansharpening",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8.1,<4.0.0",
    "maintainer_email": "",
    "keywords": "deep learning,remote sensing,pansharpening",
    "author": "XavierJiezou",
    "author_email": "878972272@qq.com",
    "download_url": "https://files.pythonhosted.org/packages/35/1b/4cad9d7c025710a644fd6252e09e4575bfba2e2dccafa14d6a1704b3b432/pansharpening-0.1.0.tar.gz",
    "platform": null,
    "description": "<div align=\"center\">\n\n# Lightning-Hydra-Template\n\n[![python](https://img.shields.io/badge/-Python_3.8_%7C_3.9_%7C_3.10-blue?logo=python&logoColor=white)](https://github.com/pre-commit/pre-commit)\n[![pytorch](https://img.shields.io/badge/PyTorch_2.0+-ee4c2c?logo=pytorch&logoColor=white)](https://pytorch.org/get-started/locally/)\n[![lightning](https://img.shields.io/badge/-Lightning_2.0+-792ee5?logo=pytorchlightning&logoColor=white)](https://pytorchlightning.ai/)\n[![hydra](https://img.shields.io/badge/Config-Hydra_1.3-89b8cd)](https://hydra.cc/)\n[![black](https://img.shields.io/badge/Code%20Style-Black-black.svg?labelColor=gray)](https://black.readthedocs.io/en/stable/)\n[![isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/) <br>\n[![tests](https://github.com/ashleve/lightning-hydra-template/actions/workflows/test.yml/badge.svg)](https://github.com/ashleve/lightning-hydra-template/actions/workflows/test.yml)\n[![code-quality](https://github.com/ashleve/lightning-hydra-template/actions/workflows/code-quality-main.yaml/badge.svg)](https://github.com/ashleve/lightning-hydra-template/actions/workflows/code-quality-main.yaml)\n[![codecov](https://codecov.io/gh/ashleve/lightning-hydra-template/branch/main/graph/badge.svg)](https://codecov.io/gh/ashleve/lightning-hydra-template) <br>\n[![license](https://img.shields.io/badge/License-MIT-green.svg?labelColor=gray)](https://github.com/ashleve/lightning-hydra-template#license)\n[![PRs](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/ashleve/lightning-hydra-template/pulls)\n[![contributors](https://img.shields.io/github/contributors/ashleve/lightning-hydra-template.svg)](https://github.com/ashleve/lightning-hydra-template/graphs/contributors)\n\nA clean template to kickstart your deep learning project \ud83d\ude80\u26a1\ud83d\udd25<br>\nClick on [<kbd>Use this template</kbd>](https://github.com/ashleve/lightning-hydra-template/generate) to initialize new repository.\n\n_Suggestions are always welcome!_\n\n</div>\n\n<br>\n\n## \ud83d\udccc\u00a0\u00a0Introduction\n\n**Why you might want to use it:**\n\n\u2705 Save on boilerplate <br>\nEasily add new models, datasets, tasks, experiments, and train on different accelerators, like multi-GPU, TPU or SLURM clusters.\n\n\u2705 Education <br>\nThoroughly commented. You can use this repo as a learning resource.\n\n\u2705 Reusability <br>\nCollection of useful MLOps tools, configs, and code snippets. You can use this repo as a reference for various utilities.\n\n**Why you might not want to use it:**\n\n\u274c Things break from time to time <br>\nLightning and Hydra are still evolving and integrate many libraries, which means sometimes things break. For the list of currently known problems visit [this page](https://github.com/ashleve/lightning-hydra-template/labels/bug).\n\n\u274c Not adjusted for data engineering <br>\nTemplate is not really adjusted for building data pipelines that depend on each other. It's more efficient to use it for model prototyping on ready-to-use data.\n\n\u274c Overfitted to simple use case <br>\nThe configuration setup is built with simple lightning training in mind. You might need to put some effort to adjust it for different use cases, e.g. lightning fabric.\n\n\u274c Might not support your workflow <br>\nFor example, you can't resume hydra-based multirun or hyperparameter search.\n\n> **Note**: _Keep in mind this is unofficial community project._\n\n<br>\n\n## Main Technologies\n\n[PyTorch Lightning](https://github.com/PyTorchLightning/pytorch-lightning) - a lightweight PyTorch wrapper for high-performance AI research. Think of it as a framework for organizing your PyTorch code.\n\n[Hydra](https://github.com/facebookresearch/hydra) - a framework for elegantly configuring complex applications. The key feature is the ability to dynamically create a hierarchical configuration by composition and override it through config files and the command line.\n\n<br>\n\n## Main Ideas\n\n- [**Rapid Experimentation**](#your-superpowers): thanks to hydra command line superpowers\n- [**Minimal Boilerplate**](#how-it-works): thanks to automating pipelines with config instantiation\n- [**Main Configs**](#main-config): allow you to specify default training configuration\n- [**Experiment Configs**](#experiment-config): allow you to override chosen hyperparameters and version control experiments\n- [**Workflow**](#workflow): comes down to 4 simple steps\n- [**Experiment Tracking**](#experiment-tracking): Tensorboard, W&B, Neptune, Comet, MLFlow and CSVLogger\n- [**Logs**](#logs): all logs (checkpoints, configs, etc.) are stored in a dynamically generated folder structure\n- [**Hyperparameter Search**](#hyperparameter-search): simple search is effortless with Hydra plugins like Optuna Sweeper\n- [**Tests**](#tests): generic, easy-to-adapt smoke tests for speeding up the development\n- [**Continuous Integration**](#continuous-integration): automatically test and lint your repo with Github Actions\n- [**Best Practices**](#best-practices): a couple of recommended tools, practices and standards\n\n<br>\n\n## Project Structure\n\nThe directory structure of new project looks like this:\n\n```\n\u251c\u2500\u2500 .github                   <- Github Actions workflows\n\u2502\n\u251c\u2500\u2500 configs                   <- Hydra configs\n\u2502   \u251c\u2500\u2500 callbacks                <- Callbacks configs\n\u2502   \u251c\u2500\u2500 data                     <- Data configs\n\u2502   \u251c\u2500\u2500 debug                    <- Debugging configs\n\u2502   \u251c\u2500\u2500 experiment               <- Experiment configs\n\u2502   \u251c\u2500\u2500 extras                   <- Extra utilities configs\n\u2502   \u251c\u2500\u2500 hparams_search           <- Hyperparameter search configs\n\u2502   \u251c\u2500\u2500 hydra                    <- Hydra configs\n\u2502   \u251c\u2500\u2500 local                    <- Local configs\n\u2502   \u251c\u2500\u2500 logger                   <- Logger configs\n\u2502   \u251c\u2500\u2500 model                    <- Model configs\n\u2502   \u251c\u2500\u2500 paths                    <- Project paths configs\n\u2502   \u251c\u2500\u2500 trainer                  <- Trainer configs\n\u2502   \u2502\n\u2502   \u251c\u2500\u2500 eval.yaml             <- Main config for evaluation\n\u2502   \u2514\u2500\u2500 train.yaml            <- Main config for training\n\u2502\n\u251c\u2500\u2500 data                   <- Project data\n\u2502\n\u251c\u2500\u2500 logs                   <- Logs generated by hydra and lightning loggers\n\u2502\n\u251c\u2500\u2500 notebooks              <- Jupyter notebooks. Naming convention is a number (for ordering),\n\u2502                             the creator's initials, and a short `-` delimited description,\n\u2502                             e.g. `1.0-jqp-initial-data-exploration.ipynb`.\n\u2502\n\u251c\u2500\u2500 scripts                <- Shell scripts\n\u2502\n\u251c\u2500\u2500 src                    <- Source code\n\u2502   \u251c\u2500\u2500 data                     <- Data scripts\n\u2502   \u251c\u2500\u2500 models                   <- Model scripts\n\u2502   \u251c\u2500\u2500 utils                    <- Utility scripts\n\u2502   \u2502\n\u2502   \u251c\u2500\u2500 eval.py                  <- Run evaluation\n\u2502   \u2514\u2500\u2500 train.py                 <- Run training\n\u2502\n\u251c\u2500\u2500 tests                  <- Tests of any kind\n\u2502\n\u251c\u2500\u2500 .env.example              <- Example of file for storing private environment variables\n\u251c\u2500\u2500 .gitignore                <- List of files ignored by git\n\u251c\u2500\u2500 .pre-commit-config.yaml   <- Configuration of pre-commit hooks for code formatting\n\u251c\u2500\u2500 .project-root             <- File for inferring the position of project root directory\n\u251c\u2500\u2500 environment.yaml          <- File for installing conda environment\n\u251c\u2500\u2500 Makefile                  <- Makefile with commands like `make train` or `make test`\n\u251c\u2500\u2500 pyproject.toml            <- Configuration options for testing and linting\n\u251c\u2500\u2500 requirements.txt          <- File for installing python dependencies\n\u251c\u2500\u2500 setup.py                  <- File for installing project as a package\n\u2514\u2500\u2500 README.md\n```\n\n<br>\n\n## \ud83d\ude80\u00a0\u00a0Quickstart\n\n```bash\n# clone project\ngit clone https://github.com/ashleve/lightning-hydra-template\ncd lightning-hydra-template\n\n# [OPTIONAL] create conda environment\nconda create -n myenv python=3.9\nconda activate myenv\n\n# install pytorch according to instructions\n# https://pytorch.org/get-started/\n\n# install requirements\npip install -r requirements.txt\n```\n\nTemplate contains example with MNIST classification.<br>\nWhen running `python src/train.py` you should see something like this:\n\n<div align=\"center\">\n\n![](https://github.com/ashleve/lightning-hydra-template/blob/resources/terminal.png)\n\n</div>\n\n## \u26a1\u00a0\u00a0Your Superpowers\n\n<details>\n<summary><b>Override any config parameter from command line</b></summary>\n\n```bash\npython train.py trainer.max_epochs=20 model.optimizer.lr=1e-4\n```\n\n> **Note**: You can also add new parameters with `+` sign.\n\n```bash\npython train.py +model.new_param=\"owo\"\n```\n\n</details>\n\n<details>\n<summary><b>Train on CPU, GPU, multi-GPU and TPU</b></summary>\n\n```bash\n# train on CPU\npython train.py trainer=cpu\n\n# train on 1 GPU\npython train.py trainer=gpu\n\n# train on TPU\npython train.py +trainer.tpu_cores=8\n\n# train with DDP (Distributed Data Parallel) (4 GPUs)\npython train.py trainer=ddp trainer.devices=4\n\n# train with DDP (Distributed Data Parallel) (8 GPUs, 2 nodes)\npython train.py trainer=ddp trainer.devices=4 trainer.num_nodes=2\n\n# simulate DDP on CPU processes\npython train.py trainer=ddp_sim trainer.devices=2\n\n# accelerate training on mac\npython train.py trainer=mps\n```\n\n> **Warning**: Currently there are problems with DDP mode, read [this issue](https://github.com/ashleve/lightning-hydra-template/issues/393) to learn more.\n\n</details>\n\n<details>\n<summary><b>Train with mixed precision</b></summary>\n\n```bash\n# train with pytorch native automatic mixed precision (AMP)\npython train.py trainer=gpu +trainer.precision=16\n```\n\n</details>\n\n<!-- deepspeed support still in beta\n<details>\n<summary><b>Optimize large scale models on multiple GPUs with Deepspeed</b></summary>\n\n```bash\npython train.py +trainer.\n```\n\n</details>\n -->\n\n<details>\n<summary><b>Train model with any logger available in PyTorch Lightning, like W&B or Tensorboard</b></summary>\n\n```yaml\n# set project and entity names in `configs/logger/wandb`\nwandb:\n  project: \"your_project_name\"\n  entity: \"your_wandb_team_name\"\n```\n\n```bash\n# train model with Weights&Biases (link to wandb dashboard should appear in the terminal)\npython train.py logger=wandb\n```\n\n> **Note**: Lightning provides convenient integrations with most popular logging frameworks. Learn more [here](#experiment-tracking).\n\n> **Note**: Using wandb requires you to [setup account](https://www.wandb.com/) first. After that just complete the config as below.\n\n> **Note**: Click [here](https://wandb.ai/hobglob/template-dashboard/) to see example wandb dashboard generated with this template.\n\n</details>\n\n<details>\n<summary><b>Train model with chosen experiment config</b></summary>\n\n```bash\npython train.py experiment=example\n```\n\n> **Note**: Experiment configs are placed in [configs/experiment/](configs/experiment/).\n\n</details>\n\n<details>\n<summary><b>Attach some callbacks to run</b></summary>\n\n```bash\npython train.py callbacks=default\n```\n\n> **Note**: Callbacks can be used for things such as as model checkpointing, early stopping and [many more](https://pytorch-lightning.readthedocs.io/en/latest/extensions/callbacks.html#built-in-callbacks).\n\n> **Note**: Callbacks configs are placed in [configs/callbacks/](configs/callbacks/).\n\n</details>\n\n<details>\n<summary><b>Use different tricks available in Pytorch Lightning</b></summary>\n\n```yaml\n# gradient clipping may be enabled to avoid exploding gradients\npython train.py +trainer.gradient_clip_val=0.5\n\n# run validation loop 4 times during a training epoch\npython train.py +trainer.val_check_interval=0.25\n\n# accumulate gradients\npython train.py +trainer.accumulate_grad_batches=10\n\n# terminate training after 12 hours\npython train.py +trainer.max_time=\"00:12:00:00\"\n```\n\n> **Note**: PyTorch Lightning provides about [40+ useful trainer flags](https://pytorch-lightning.readthedocs.io/en/latest/common/trainer.html#trainer-flags).\n\n</details>\n\n<details>\n<summary><b>Easily debug</b></summary>\n\n```bash\n# runs 1 epoch in default debugging mode\n# changes logging directory to `logs/debugs/...`\n# sets level of all command line loggers to 'DEBUG'\n# enforces debug-friendly configuration\npython train.py debug=default\n\n# run 1 train, val and test loop, using only 1 batch\npython train.py debug=fdr\n\n# print execution time profiling\npython train.py debug=profiler\n\n# try overfitting to 1 batch\npython train.py debug=overfit\n\n# raise exception if there are any numerical anomalies in tensors, like NaN or +/-inf\npython train.py +trainer.detect_anomaly=true\n\n# use only 20% of the data\npython train.py +trainer.limit_train_batches=0.2 \\\n+trainer.limit_val_batches=0.2 +trainer.limit_test_batches=0.2\n```\n\n> **Note**: Visit [configs/debug/](configs/debug/) for different debugging configs.\n\n</details>\n\n<details>\n<summary><b>Resume training from checkpoint</b></summary>\n\n```yaml\npython train.py ckpt_path=\"/path/to/ckpt/name.ckpt\"\n```\n\n> **Note**: Checkpoint can be either path or URL.\n\n> **Note**: Currently loading ckpt doesn't resume logger experiment, but it will be supported in future Lightning release.\n\n</details>\n\n<details>\n<summary><b>Evaluate checkpoint on test dataset</b></summary>\n\n```yaml\npython eval.py ckpt_path=\"/path/to/ckpt/name.ckpt\"\n```\n\n> **Note**: Checkpoint can be either path or URL.\n\n</details>\n\n<details>\n<summary><b>Create a sweep over hyperparameters</b></summary>\n\n```bash\n# this will run 6 experiments one after the other,\n# each with different combination of batch_size and learning rate\npython train.py -m data.batch_size=32,64,128 model.lr=0.001,0.0005\n```\n\n> **Note**: Hydra composes configs lazily at job launch time. If you change code or configs after launching a job/sweep, the final composed configs might be impacted.\n\n</details>\n\n<details>\n<summary><b>Create a sweep over hyperparameters with Optuna</b></summary>\n\n```bash\n# this will run hyperparameter search defined in `configs/hparams_search/mnist_optuna.yaml`\n# over chosen experiment config\npython train.py -m hparams_search=mnist_optuna experiment=example\n```\n\n> **Note**: Using [Optuna Sweeper](https://hydra.cc/docs/next/plugins/optuna_sweeper) doesn't require you to add any boilerplate to your code, everything is defined in a [single config file](configs/hparams_search/mnist_optuna.yaml).\n\n> **Warning**: Optuna sweeps are not failure-resistant (if one job crashes then the whole sweep crashes).\n\n</details>\n\n<details>\n<summary><b>Execute all experiments from folder</b></summary>\n\n```bash\npython train.py -m 'experiment=glob(*)'\n```\n\n> **Note**: Hydra provides special syntax for controlling behavior of multiruns. Learn more [here](https://hydra.cc/docs/next/tutorials/basic/running_your_app/multi-run). The command above executes all experiments from [configs/experiment/](configs/experiment/).\n\n</details>\n\n<details>\n<summary><b>Execute run for multiple different seeds</b></summary>\n\n```bash\npython train.py -m seed=1,2,3,4,5 trainer.deterministic=True logger=csv tags=[\"benchmark\"]\n```\n\n> **Note**: `trainer.deterministic=True` makes pytorch more deterministic but impacts the performance.\n\n</details>\n\n<details>\n<summary><b>Execute sweep on a remote AWS cluster</b></summary>\n\n> **Note**: This should be achievable with simple config using [Ray AWS launcher for Hydra](https://hydra.cc/docs/next/plugins/ray_launcher). Example is not implemented in this template.\n\n</details>\n\n<!-- <details>\n<summary><b>Execute sweep on a SLURM cluster</b></summary>\n\n> This should be achievable with either [the right lightning trainer flags](https://pytorch-lightning.readthedocs.io/en/latest/clouds/cluster.html?highlight=SLURM#slurm-managed-cluster) or simple config using [Submitit launcher for Hydra](https://hydra.cc/docs/plugins/submitit_launcher). Example is not yet implemented in this template.\n\n</details> -->\n\n<details>\n<summary><b>Use Hydra tab completion</b></summary>\n\n> **Note**: Hydra allows you to autocomplete config argument overrides in shell as you write them, by pressing `tab` key. Read the [docs](https://hydra.cc/docs/tutorials/basic/running_your_app/tab_completion).\n\n</details>\n\n<details>\n<summary><b>Apply pre-commit hooks</b></summary>\n\n```bash\npre-commit run -a\n```\n\n> **Note**: Apply pre-commit hooks to do things like auto-formatting code and configs, performing code analysis or removing output from jupyter notebooks. See [# Best Practices](#best-practices) for more.\n\nUpdate pre-commit hook versions in `.pre-commit-config.yaml` with:\n\n```bash\npre-commit autoupdate\n```\n\n</details>\n\n<details>\n<summary><b>Run tests</b></summary>\n\n```bash\n# run all tests\npytest\n\n# run tests from specific file\npytest tests/test_train.py\n\n# run all tests except the ones marked as slow\npytest -k \"not slow\"\n```\n\n</details>\n\n<details>\n<summary><b>Use tags</b></summary>\n\nEach experiment should be tagged in order to easily filter them across files or in logger UI:\n\n```bash\npython train.py tags=[\"mnist\",\"experiment_X\"]\n```\n\n> **Note**: You might need to escape the bracket characters in your shell with `python train.py tags=\\[\"mnist\",\"experiment_X\"\\]`.\n\nIf no tags are provided, you will be asked to input them from command line:\n\n```bash\n>>> python train.py tags=[]\n[2022-07-11 15:40:09,358][src.utils.utils][INFO] - Enforcing tags! <cfg.extras.enforce_tags=True>\n[2022-07-11 15:40:09,359][src.utils.rich_utils][WARNING] - No tags provided in config. Prompting user to input tags...\nEnter a list of comma separated tags (dev):\n```\n\nIf no tags are provided for multirun, an error will be raised:\n\n```bash\n>>> python train.py -m +x=1,2,3 tags=[]\nValueError: Specify tags before launching a multirun!\n```\n\n> **Note**: Appending lists from command line is currently not supported in hydra :(\n\n</details>\n\n<br>\n\n## \u2764\ufe0f\u00a0\u00a0Contributions\n\nThis project exists thanks to all the people who contribute.\n\n![Contributors](https://readme-contributors.now.sh/ashleve/lightning-hydra-template?extension=jpg&width=400&aspectRatio=1)\n\nHave a question? Found a bug? Missing a specific feature? Feel free to file a new issue, discussion or PR with respective title and description.\n\nBefore making an issue, please verify that:\n\n- The problem still exists on the current `main` branch.\n- Your python dependencies are updated to recent versions.\n\nSuggestions for improvements are always welcome!\n\n<br>\n\n## How It Works\n\nAll PyTorch Lightning modules are dynamically instantiated from module paths specified in config. Example model config:\n\n```yaml\n_target_: src.models.mnist_model.MNISTLitModule\nlr: 0.001\nnet:\n  _target_: src.models.components.simple_dense_net.SimpleDenseNet\n  input_size: 784\n  lin1_size: 256\n  lin2_size: 256\n  lin3_size: 256\n  output_size: 10\n```\n\nUsing this config we can instantiate the object with the following line:\n\n```python\nmodel = hydra.utils.instantiate(config.model)\n```\n\nThis allows you to easily iterate over new models! Every time you create a new one, just specify its module path and parameters in appropriate config file. <br>\n\nSwitch between models and datamodules with command line arguments:\n\n```bash\npython train.py model=mnist\n```\n\nExample pipeline managing the instantiation logic: [src/train.py](src/train.py).\n\n<br>\n\n## Main Config\n\nLocation: [configs/train.yaml](configs/train.yaml) <br>\nMain project config contains default training configuration.<br>\nIt determines how config is composed when simply executing command `python train.py`.<br>\n\n<details>\n<summary><b>Show main project config</b></summary>\n\n```yaml\n# order of defaults determines the order in which configs override each other\ndefaults:\n  - _self_\n  - data: mnist.yaml\n  - model: mnist.yaml\n  - callbacks: default.yaml\n  - logger: null # set logger here or use command line (e.g. `python train.py logger=csv`)\n  - trainer: default.yaml\n  - paths: default.yaml\n  - extras: default.yaml\n  - hydra: default.yaml\n\n  # experiment configs allow for version control of specific hyperparameters\n  # e.g. best hyperparameters for given model and datamodule\n  - experiment: null\n\n  # config for hyperparameter optimization\n  - hparams_search: null\n\n  # optional local config for machine/user specific settings\n  # it's optional since it doesn't need to exist and is excluded from version control\n  - optional local: default.yaml\n\n  # debugging config (enable through command line, e.g. `python train.py debug=default)\n  - debug: null\n\n# task name, determines output directory path\ntask_name: \"train\"\n\n# tags to help you identify your experiments\n# you can overwrite this in experiment configs\n# overwrite from command line with `python train.py tags=\"[first_tag, second_tag]\"`\n# appending lists from command line is currently not supported :(\n# https://github.com/facebookresearch/hydra/issues/1547\ntags: [\"dev\"]\n\n# set False to skip model training\ntrain: True\n\n# evaluate on test set, using best model weights achieved during training\n# lightning chooses best weights based on the metric specified in checkpoint callback\ntest: True\n\n# simply provide checkpoint path to resume training\nckpt_path: null\n\n# seed for random number generators in pytorch, numpy and python.random\nseed: null\n```\n\n</details>\n\n<br>\n\n## Experiment Config\n\nLocation: [configs/experiment](configs/experiment)<br>\nExperiment configs allow you to overwrite parameters from main config.<br>\nFor example, you can use them to version control best hyperparameters for each combination of model and dataset.\n\n<details>\n<summary><b>Show example experiment config</b></summary>\n\n```yaml\n# @package _global_\n\n# to execute this experiment run:\n# python train.py experiment=example\n\ndefaults:\n  - override /data: mnist.yaml\n  - override /model: mnist.yaml\n  - override /callbacks: default.yaml\n  - override /trainer: default.yaml\n\n# all parameters below will be merged with parameters from default configurations set above\n# this allows you to overwrite only specified parameters\n\ntags: [\"mnist\", \"simple_dense_net\"]\n\nseed: 12345\n\ntrainer:\n  min_epochs: 10\n  max_epochs: 10\n  gradient_clip_val: 0.5\n\nmodel:\n  optimizer:\n    lr: 0.002\n  net:\n    lin1_size: 128\n    lin2_size: 256\n    lin3_size: 64\n\ndata:\n  batch_size: 64\n\nlogger:\n  wandb:\n    tags: ${tags}\n    group: \"mnist\"\n```\n\n</details>\n\n<br>\n\n## Workflow\n\n**Basic workflow**\n\n1. Write your PyTorch Lightning module (see [models/mnist_module.py](src/models/mnist_module.py) for example)\n2. Write your PyTorch Lightning datamodule (see [data/mnist_datamodule.py](src/data/mnist_datamodule.py) for example)\n3. Write your experiment config, containing paths to model and datamodule\n4. Run training with chosen experiment config:\n   ```bash\n   python src/train.py experiment=experiment_name.yaml\n   ```\n\n**Experiment design**\n\n_Say you want to execute many runs to plot how accuracy changes in respect to batch size._\n\n1. Execute the runs with some config parameter that allows you to identify them easily, like tags:\n\n   ```bash\n   python train.py -m logger=csv data.batch_size=16,32,64,128 tags=[\"batch_size_exp\"]\n   ```\n\n2. Write a script or notebook that searches over the `logs/` folder and retrieves csv logs from runs containing given tags in config. Plot the results.\n\n<br>\n\n## Logs\n\nHydra creates new output directory for every executed run.\n\nDefault logging structure:\n\n```\n\u251c\u2500\u2500 logs\n\u2502   \u251c\u2500\u2500 task_name\n\u2502   \u2502   \u251c\u2500\u2500 runs                        # Logs generated by single runs\n\u2502   \u2502   \u2502   \u251c\u2500\u2500 YYYY-MM-DD_HH-MM-SS       # Datetime of the run\n\u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 .hydra                  # Hydra logs\n\u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 csv                     # Csv logs\n\u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 wandb                   # Weights&Biases logs\n\u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 checkpoints             # Training checkpoints\n\u2502   \u2502   \u2502   \u2502   \u2514\u2500\u2500 ...                     # Any other thing saved during training\n\u2502   \u2502   \u2502   \u2514\u2500\u2500 ...\n\u2502   \u2502   \u2502\n\u2502   \u2502   \u2514\u2500\u2500 multiruns                   # Logs generated by multiruns\n\u2502   \u2502       \u251c\u2500\u2500 YYYY-MM-DD_HH-MM-SS       # Datetime of the multirun\n\u2502   \u2502       \u2502   \u251c\u2500\u25001                        # Multirun job number\n\u2502   \u2502       \u2502   \u251c\u2500\u25002\n\u2502   \u2502       \u2502   \u2514\u2500\u2500 ...\n\u2502   \u2502       \u2514\u2500\u2500 ...\n\u2502   \u2502\n\u2502   \u2514\u2500\u2500 debugs                          # Logs generated when debugging config is attached\n\u2502       \u2514\u2500\u2500 ...\n```\n\n</details>\n\nYou can change this structure by modifying paths in [hydra configuration](configs/hydra).\n\n<br>\n\n## Experiment Tracking\n\nPyTorch Lightning supports many popular logging frameworks: [Weights&Biases](https://www.wandb.com/), [Neptune](https://neptune.ai/), [Comet](https://www.comet.ml/), [MLFlow](https://mlflow.org), [Tensorboard](https://www.tensorflow.org/tensorboard/).\n\nThese tools help you keep track of hyperparameters and output metrics and allow you to compare and visualize results. To use one of them simply complete its configuration in [configs/logger](configs/logger) and run:\n\n```bash\npython train.py logger=logger_name\n```\n\nYou can use many of them at once (see [configs/logger/many_loggers.yaml](configs/logger/many_loggers.yaml) for example).\n\nYou can also write your own logger.\n\nLightning provides convenient method for logging custom metrics from inside LightningModule. Read the [docs](https://pytorch-lightning.readthedocs.io/en/latest/extensions/logging.html#automatic-logging) or take a look at [MNIST example](src/models/mnist_module.py).\n\n<br>\n\n## Tests\n\nTemplate comes with generic tests implemented with `pytest`.\n\n```bash\n# run all tests\npytest\n\n# run tests from specific file\npytest tests/test_train.py\n\n# run all tests except the ones marked as slow\npytest -k \"not slow\"\n```\n\nMost of the implemented tests don't check for any specific output - they exist to simply verify that executing some commands doesn't end up in throwing exceptions. You can execute them once in a while to speed up the development.\n\nCurrently, the tests cover cases like:\n\n- running 1 train, val and test step\n- running 1 epoch on 1% of data, saving ckpt and resuming for the second epoch\n- running 2 epochs on 1% of data, with DDP simulated on CPU\n\nAnd many others. You should be able to modify them easily for your use case.\n\nThere is also `@RunIf` decorator implemented, that allows you to run tests only if certain conditions are met, e.g. GPU is available or system is not windows. See the [examples](tests/test_train.py).\n\n<br>\n\n## Hyperparameter Search\n\nYou can define hyperparameter search by adding new config file to [configs/hparams_search](configs/hparams_search).\n\n<details>\n<summary><b>Show example hyperparameter search config</b></summary>\n\n```yaml\n# @package _global_\n\ndefaults:\n  - override /hydra/sweeper: optuna\n\n# choose metric which will be optimized by Optuna\n# make sure this is the correct name of some metric logged in lightning module!\noptimized_metric: \"val/acc_best\"\n\n# here we define Optuna hyperparameter search\n# it optimizes for value returned from function with @hydra.main decorator\nhydra:\n  sweeper:\n    _target_: hydra_plugins.hydra_optuna_sweeper.optuna_sweeper.OptunaSweeper\n\n    # 'minimize' or 'maximize' the objective\n    direction: maximize\n\n    # total number of runs that will be executed\n    n_trials: 20\n\n    # choose Optuna hyperparameter sampler\n    # docs: https://optuna.readthedocs.io/en/stable/reference/samplers.html\n    sampler:\n      _target_: optuna.samplers.TPESampler\n      seed: 1234\n      n_startup_trials: 10 # number of random sampling runs before optimization starts\n\n    # define hyperparameter search space\n    params:\n      model.optimizer.lr: interval(0.0001, 0.1)\n      data.batch_size: choice(32, 64, 128, 256)\n      model.net.lin1_size: choice(64, 128, 256)\n      model.net.lin2_size: choice(64, 128, 256)\n      model.net.lin3_size: choice(32, 64, 128, 256)\n```\n\n</details>\n\nNext, execute it with: `python train.py -m hparams_search=mnist_optuna`\n\nUsing this approach doesn't require adding any boilerplate to code, everything is defined in a single config file. The only necessary thing is to return the optimized metric value from the launch file.\n\nYou can use different optimization frameworks integrated with Hydra, like [Optuna, Ax or Nevergrad](https://hydra.cc/docs/plugins/optuna_sweeper/).\n\nThe `optimization_results.yaml` will be available under `logs/task_name/multirun` folder.\n\nThis approach doesn't support resuming interrupted search and advanced techniques like prunning - for more sophisticated search and workflows, you should probably write a dedicated optimization task (without multirun feature).\n\n<br>\n\n## Continuous Integration\n\nTemplate comes with CI workflows implemented in Github Actions:\n\n- `.github/workflows/test.yaml`: running all tests with pytest\n- `.github/workflows/code-quality-main.yaml`: running pre-commits on main branch for all files\n- `.github/workflows/code-quality-pr.yaml`: running pre-commits on pull requests for modified files only\n\n<br>\n\n## Distributed Training\n\nLightning supports multiple ways of doing distributed training. The most common one is DDP, which spawns separate process for each GPU and averages gradients between them. To learn about other approaches read the [lightning docs](https://lightning.ai/docs/pytorch/latest/advanced/speed.html).\n\nYou can run DDP on mnist example with 4 GPUs like this:\n\n```bash\npython train.py trainer=ddp\n```\n\n> **Note**: When using DDP you have to be careful how you write your models - read the [docs](https://lightning.ai/docs/pytorch/latest/advanced/speed.html).\n\n<br>\n\n## Accessing Datamodule Attributes In Model\n\nThe simplest way is to pass datamodule attribute directly to model on initialization:\n\n```python\n# ./src/train.py\ndatamodule = hydra.utils.instantiate(config.data)\nmodel = hydra.utils.instantiate(config.model, some_param=datamodule.some_param)\n```\n\n> **Note**: Not a very robust solution, since it assumes all your datamodules have `some_param` attribute available.\n\nSimilarly, you can pass a whole datamodule config as an init parameter:\n\n```python\n# ./src/train.py\nmodel = hydra.utils.instantiate(config.model, dm_conf=config.data, _recursive_=False)\n```\n\nYou can also pass a datamodule config parameter to your model through variable interpolation:\n\n```yaml\n# ./configs/model/my_model.yaml\n_target_: src.models.my_module.MyLitModule\nlr: 0.01\nsome_param: ${data.some_param}\n```\n\nAnother approach is to access datamodule in LightningModule directly through Trainer:\n\n```python\n# ./src/models/mnist_module.py\ndef on_train_start(self):\n  self.some_param = self.trainer.datamodule.some_param\n```\n\n> **Note**: This only works after the training starts since otherwise trainer won't be yet available in LightningModule.\n\n<br>\n\n## Best Practices\n\n<details>\n<summary><b>Use Miniconda</b></summary>\n\nIt's usually unnecessary to install full anaconda environment, miniconda should be enough (weights around 80MB).\n\nBig advantage of conda is that it allows for installing packages without requiring certain compilers or libraries to be available in the system (since it installs precompiled binaries), so it often makes it easier to install some dependencies e.g. cudatoolkit for GPU support.\n\nIt also allows you to access your environments globally which might be more convenient than creating new local environment for every project.\n\nExample installation:\n\n```bash\nwget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh\nbash Miniconda3-latest-Linux-x86_64.sh\n```\n\nUpdate conda:\n\n```bash\nconda update -n base -c defaults conda\n```\n\nCreate new conda environment:\n\n```bash\nconda create -n myenv python=3.10\nconda activate myenv\n```\n\n</details>\n\n<details>\n<summary><b>Use automatic code formatting</b></summary>\n\nUse pre-commit hooks to standardize code formatting of your project and save mental energy.<br>\nSimply install pre-commit package with:\n\n```bash\npip install pre-commit\n```\n\nNext, install hooks from [.pre-commit-config.yaml](.pre-commit-config.yaml):\n\n```bash\npre-commit install\n```\n\nAfter that your code will be automatically reformatted on every new commit.\n\nTo reformat all files in the project use command:\n\n```bash\npre-commit run -a\n```\n\nTo update hook versions in [.pre-commit-config.yaml](.pre-commit-config.yaml) use:\n\n```bash\npre-commit autoupdate\n```\n\n</details>\n\n<details>\n<summary><b>Set private environment variables in .env file</b></summary>\n\nSystem specific variables (e.g. absolute paths to datasets) should not be under version control or it will result in conflict between different users. Your private keys also shouldn't be versioned since you don't want them to be leaked.<br>\n\nTemplate contains `.env.example` file, which serves as an example. Create a new file called `.env` (this name is excluded from version control in .gitignore).\nYou should use it for storing environment variables like this:\n\n```\nMY_VAR=/home/user/my_system_path\n```\n\nAll variables from `.env` are loaded in `train.py` automatically.\n\nHydra allows you to reference any env variable in `.yaml` configs like this:\n\n```yaml\npath_to_data: ${oc.env:MY_VAR}\n```\n\n</details>\n\n<details>\n<summary><b>Name metrics using '/' character</b></summary>\n\nDepending on which logger you're using, it's often useful to define metric name with `/` character:\n\n```python\nself.log(\"train/loss\", loss)\n```\n\nThis way loggers will treat your metrics as belonging to different sections, which helps to get them organised in UI.\n\n</details>\n\n<details>\n<summary><b>Use torchmetrics</b></summary>\n\nUse official [torchmetrics](https://github.com/PytorchLightning/metrics) library to ensure proper calculation of metrics. This is especially important for multi-GPU training!\n\nFor example, instead of calculating accuracy by yourself, you should use the provided `Accuracy` class like this:\n\n```python\nfrom torchmetrics.classification.accuracy import Accuracy\n\n\nclass LitModel(LightningModule):\n    def __init__(self)\n        self.train_acc = Accuracy()\n        self.val_acc = Accuracy()\n\n    def training_step(self, batch, batch_idx):\n        ...\n        acc = self.train_acc(predictions, targets)\n        self.log(\"train/acc\", acc)\n        ...\n\n    def validation_step(self, batch, batch_idx):\n        ...\n        acc = self.val_acc(predictions, targets)\n        self.log(\"val/acc\", acc)\n        ...\n```\n\nMake sure to use different metric instance for each step to ensure proper value reduction over all GPU processes.\n\nTorchmetrics provides metrics for most use cases, like F1 score or confusion matrix. Read [documentation](https://torchmetrics.readthedocs.io/en/latest/#more-reading) for more.\n\n</details>\n\n<details>\n<summary><b>Follow PyTorch Lightning style guide</b></summary>\n\nThe style guide is available [here](https://pytorch-lightning.readthedocs.io/en/latest/starter/style_guide.html).<br>\n\n1. Be explicit in your init. Try to define all the relevant defaults so that the user doesn\u2019t have to guess. Provide type hints. This way your module is reusable across projects!\n\n   ```python\n   class LitModel(LightningModule):\n       def __init__(self, layer_size: int = 256, lr: float = 0.001):\n   ```\n\n2. Preserve the recommended method order.\n\n   ```python\n   class LitModel(LightningModule):\n\n       def __init__():\n           ...\n\n       def forward():\n           ...\n\n       def training_step():\n           ...\n\n       def training_step_end():\n           ...\n\n       def on_train_epoch_end():\n           ...\n\n       def validation_step():\n           ...\n\n       def validation_step_end():\n           ...\n\n       def on_validation_epoch_end():\n           ...\n\n       def test_step():\n           ...\n\n       def test_step_end():\n           ...\n\n       def on_test_epoch_end():\n           ...\n\n       def configure_optimizers():\n           ...\n\n       def any_extra_hook():\n           ...\n   ```\n\n</details>\n\n<details>\n<summary><b>Version control your data and models with DVC</b></summary>\n\nUse [DVC](https://dvc.org) to version control big files, like your data or trained ML models.<br>\nTo initialize the dvc repository:\n\n```bash\ndvc init\n```\n\nTo start tracking a file or directory, use `dvc add`:\n\n```bash\ndvc add data/MNIST\n```\n\nDVC stores information about the added file (or a directory) in a special .dvc file named data/MNIST.dvc, a small text file with a human-readable format. This file can be easily versioned like source code with Git, as a placeholder for the original data:\n\n```bash\ngit add data/MNIST.dvc data/.gitignore\ngit commit -m \"Add raw data\"\n```\n\n</details>\n\n<details>\n<summary><b>Support installing project as a package</b></summary>\n\nIt allows other people to easily use your modules in their own projects.\nChange name of the `src` folder to your project name and complete the `setup.py` file.\n\nNow your project can be installed from local files:\n\n```bash\npip install -e .\n```\n\nOr directly from git repository:\n\n```bash\npip install git+git://github.com/YourGithubName/your-repo-name.git --upgrade\n```\n\nSo any file can be easily imported into any other file like so:\n\n```python\nfrom project_name.models.mnist_module import MNISTLitModule\nfrom project_name.data.mnist_datamodule import MNISTDataModule\n```\n\n</details>\n\n<details>\n<summary><b>Keep local configs out of code versioning</b></summary>\n\nSome configurations are user/machine/installation specific (e.g. configuration of local cluster, or harddrive paths on a specific machine). For such scenarios, a file [configs/local/default.yaml](configs/local/) can be created which is automatically loaded but not tracked by Git.\n\nFor example, you can use it for a SLURM cluster config:\n\n```yaml\n# @package _global_\n\ndefaults:\n  - override /hydra/launcher@_here_: submitit_slurm\n\ndata_dir: /mnt/scratch/data/\n\nhydra:\n  launcher:\n    timeout_min: 1440\n    gpus_per_task: 1\n    gres: gpu:1\n  job:\n    env_set:\n      MY_VAR: /home/user/my/system/path\n      MY_KEY: asdgjhawi8y23ihsghsueity23ihwd\n```\n\n</details>\n\n<br>\n\n## Resources\n\nThis template was inspired by:\n\n- [PyTorchLightning/deep-learning-project-template](https://github.com/PyTorchLightning/deep-learning-project-template)\n- [drivendata/cookiecutter-data-science](https://github.com/drivendata/cookiecutter-data-science)\n- [lucmos/nn-template](https://github.com/lucmos/nn-template)\n\nOther useful repositories:\n\n- [jxpress/lightning-hydra-template-vertex-ai](https://github.com/jxpress/lightning-hydra-template-vertex-ai) - lightning-hydra-template integration with Vertex AI hyperparameter tuning and custom training job\n\n</details>\n\n<br>\n\n## License\n\nLightning-Hydra-Template is licensed under the MIT License.\n\n```\nMIT License\n\nCopyright (c) 2021 ashleve\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n\n<br>\n<br>\n<br>\n<br>\n\n**DELETE EVERYTHING ABOVE FOR YOUR PROJECT**\n\n______________________________________________________________________\n\n<div align=\"center\">\n\n# Your Project Name\n\n<a href=\"https://pytorch.org/get-started/locally/\"><img alt=\"PyTorch\" src=\"https://img.shields.io/badge/PyTorch-ee4c2c?logo=pytorch&logoColor=white\"></a>\n<a href=\"https://pytorchlightning.ai/\"><img alt=\"Lightning\" src=\"https://img.shields.io/badge/-Lightning-792ee5?logo=pytorchlightning&logoColor=white\"></a>\n<a href=\"https://hydra.cc/\"><img alt=\"Config: Hydra\" src=\"https://img.shields.io/badge/Config-Hydra-89b8cd\"></a>\n<a href=\"https://github.com/ashleve/lightning-hydra-template\"><img alt=\"Template\" src=\"https://img.shields.io/badge/-Lightning--Hydra--Template-017F2F?style=flat&logo=github&labelColor=gray\"></a><br>\n[![Paper](http://img.shields.io/badge/paper-arxiv.1001.2234-B31B1B.svg)](https://www.nature.com/articles/nature14539)\n[![Conference](http://img.shields.io/badge/AnyConference-year-4b44ce.svg)](https://papers.nips.cc/paper/2020)\n\n</div>\n\n## Description\n\nWhat it does\n\n## Installation\n\n#### Pip\n\n```bash\n# clone project\ngit clone https://github.com/YourGithubName/your-repo-name\ncd your-repo-name\n\n# [OPTIONAL] create conda environment\nconda create -n myenv python=3.9\nconda activate myenv\n\n# install pytorch according to instructions\n# https://pytorch.org/get-started/\n\n# install requirements\npip install -r requirements.txt\n```\n\n#### Conda\n\n```bash\n# clone project\ngit clone https://github.com/YourGithubName/your-repo-name\ncd your-repo-name\n\n# create conda environment and install dependencies\nconda env create -f environment.yaml -n myenv\n\n# activate conda environment\nconda activate myenv\n```\n\n## How to run\n\nTrain model with default configuration\n\n```bash\n# train on CPU\npython src/train.py trainer=cpu\n\n# train on GPU\npython src/train.py trainer=gpu\n```\n\nTrain model with chosen experiment configuration from [configs/experiment/](configs/experiment/)\n\n```bash\npython src/train.py experiment=experiment_name.yaml\n```\n\nYou can override any parameter from command line like this\n\n```bash\npython src/train.py trainer.max_epochs=20 data.batch_size=64\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Deep learning for pansharpening in remote sensing",
    "version": "0.1.0",
    "project_urls": {
        "Documentation": "https://pansharpening.readthedocs.io",
        "Homepage": "https://XavierJiezou.github.io/Pansharpening",
        "Repository": "https://github.com/XavierJiezou/Pansharpening"
    },
    "split_keywords": [
        "deep learning",
        "remote sensing",
        "pansharpening"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "47b98192db0d364615755de40f6a858f732443969778684464ce8bc7cb4ed9ed",
                "md5": "df0d6ddb622dfcd7f6722dc0b00aada8",
                "sha256": "2ff7fdcd3d4bbe9debecf581b88143d136898958932d1b9eeec2712f63772999"
            },
            "downloads": -1,
            "filename": "pansharpening-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "df0d6ddb622dfcd7f6722dc0b00aada8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8.1,<4.0.0",
            "size": 31550,
            "upload_time": "2023-10-28T16:01:37",
            "upload_time_iso_8601": "2023-10-28T16:01:37.663127Z",
            "url": "https://files.pythonhosted.org/packages/47/b9/8192db0d364615755de40f6a858f732443969778684464ce8bc7cb4ed9ed/pansharpening-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "351b4cad9d7c025710a644fd6252e09e4575bfba2e2dccafa14d6a1704b3b432",
                "md5": "3723d76a4046d6d64bc3bd0c367dfd1f",
                "sha256": "74adc0c3fd4f4d2b017fea6d8d63c0a3016b41d818f8911b2e6d1753e2bd3d64"
            },
            "downloads": -1,
            "filename": "pansharpening-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "3723d76a4046d6d64bc3bd0c367dfd1f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8.1,<4.0.0",
            "size": 38469,
            "upload_time": "2023-10-28T16:01:39",
            "upload_time_iso_8601": "2023-10-28T16:01:39.729951Z",
            "url": "https://files.pythonhosted.org/packages/35/1b/4cad9d7c025710a644fd6252e09e4575bfba2e2dccafa14d6a1704b3b432/pansharpening-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-28 16:01:39",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "XavierJiezou",
    "github_project": "Pansharpening",
    "github_not_found": true,
    "lcname": "pansharpening"
}
        
Elapsed time: 0.12692s