# MelGAN

This project is about MelGAN, proposed in this [paper](https://arxiv.org/abs/1910.06711).

### Quick start

If you want run script and don't care you can run this commands in your terminal:

```bash
git clone https://github.com/PUSSYMIPT/MelGAN.git && cd MelGAN
pip install -r requirements/requirements.txt
sudo apt-get install libsndfile1 -y  # not necessary but sometimes required
bash bin/download_lj_speech.sh
export PYTHONPATH=$PYTHONPATH:.  # not necessary but sometimes required
bash bin/download_lj_speach.sh
python scripts/preprocess.py -d data/LJSpeech-1.1/wavs
catalyst-dl run -C configs/LJ_config.yml --verbose
```

## Run experiment

First of all we need to install all required dependencies

```
$ pip install -r requirements/requirements.txt
```

Sometimes if `librosa` wasn't install we also need to install some other additional library

```bash
$ sudo apt-get install libsndfile1 -y
```

Also we need to download and preprocess dataset. For example LJ1.1.

```bash
bash bin/download_lj_speech.sh
export PYTHONPATH=$PYTHONPATH:.  # not necessary but sometimes required
bash bin/download_lj_speach.sh
python scripts/preprocess.py -d data/LJSpeech-1.1/wavs
```

### Config API

The most product-ready and kinda professional way to run script in catalyst is to run it via config API. You need to write your `config.yml` file and download your dataset. Then just run&#x20;

```bash
$ catalyst-dl run -C PATH_TO_CONFIG
```

Also we can run in distributed or/and apex mode:

```bash
$ catalyst-dl run -C PATH_TO_CONFIG --distributed --apex
```

{% hint style="info" %}
&#x20;Reproducibility guaranteed only in non-distributed mode.&#x20;
{% endhint %}

### Notebook API

If you want to create something new and run small experiment to check your hypothesis you can use notebook API. Here is a minimal example.

```python
from collections import OrderedDict

from catalyst import dl
from src.callbacks.discriminator_loss_callback import DiscriminatorLossCallback
from src.callbacks.generator_loss_callback import GeneratorLossCallback
from src.data.dataset import MelFromDisk
from src.models import Discriminator, Generator
from src.runner import MelGANRunner
import torch


def main():
    """Test Notebook API"""
    dataset = MelFromDisk(path="data/test")
    dataloader = torch.utils.data.DataLoader(dataset)
    loaders = OrderedDict({"train": dataloader})
    generator = Generator(80)
    discriminator = Discriminator()

    model = torch.nn.ModuleDict(
        {"generator": generator, "discriminator": discriminator}
    )
    optimizer = {
        "opt_g": torch.optim.Adam(generator.parameters()),
        "opt_d": torch.optim.Adam(discriminator.parameters()),
    }
    callbacks = {
        "loss_g": GeneratorLossCallback(),
        "loss_d": DiscriminatorLossCallback(),
        "o_g": dl.OptimizerCallback(
            metric_key="generator_loss", optimizer_key="opt_g"
        ),
        "o_d": dl.OptimizerCallback(
            metric_key="discriminator_loss", optimizer_key="opt_d"
        ),
    }
    runner = MelGANRunner()

    runner.train(
        model=model,
        loaders=loaders,
        optimizer=optimizer,
        callbacks=callbacks,
        check=True,
        main_metric="discriminator_loss",
    )
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://elephantmipt.gitbook.io/melgan/master.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
