Skip to main content

advermorel logo

A multi-objective optimization framework for improving DNN robustness against adversarial attacks.

Installation

conda create -n advermorel python=3.13
conda activate advermorel
pip install advermorel
# To install CUDA‐enabled PyTorch, run (or visit: https://pytorch.org/get-started/locally/):
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

Or, to install the latest code from GitHub:

conda create -n advermorel python=3.13
conda activate advermorel
git clone https://github.com/salomonhotegni/MOREL.git
cd MOREL
pip install -e .
# To install CUDA‐enabled PyTorch, run (or visit: https://pytorch.org/get-started/locally/):
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

Basic usage

Assume you want to train a ResNet-18 model with MOREL on the CIFAR-10 dataset. The advermorel package provides three objective functions for robust prediction—TRADES, MART, and LOAT—but you can also supply your own. Below is an end-to-end example training ResNet-18 for 10 epochs. By default, PGD-10 with epsilon = 0.031 is considered for training.

import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
from torchvision.models import resnet18
from advermorel import MOREL

EPOCHS = 10
BATCH_SIZE = 128

my_model = resnet18()
classifier_layer = "fc" # the name of the classifier in resnet18()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Initialize the MOREL class
morel = MOREL(original_model=my_model, 
              name_last_layer=classifier_layer,
              num_class=10, device=device, accu_obj="mart")

# Prepare the train dataloader:
transform_train = torchvision.transforms.Compose(
            [
                torchvision.transforms.RandomCrop(32, padding=4),
                torchvision.transforms.RandomHorizontalFlip(),
                torchvision.transforms.ToTensor(),
            ]
        )
trainset = torchvision.datasets.CIFAR10(
            root="data/cifar10", train=True, download=True, transform=transform_train
        )
train_loader = torch.utils.data.DataLoader(
        trainset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2
    )

# Choose an optimizer:
optimizer = optim.SGD(
                morel.model.parameters(),
                lr=0.001,
                momentum=0.9,
                weight_decay=2e-4,
            )

# Train the model:
morel.train(optimizer=optimizer,
            num_epochs=EPOCHS, 
            train_loader=train_loader, 
            seed=0)

Let’s evaluate the model’s robustness on the test dataset using a new adversarial attack. The advermorel package accepts attack methods from the adversarial-robustness-toolbox. In this example, we apply the PGD-20 attack:

from art.attacks.evasion import ProjectedGradientDescent
from art.estimators.classification import PyTorchClassifier

# Prepare the test dataloader:
transform_test = torchvision.transforms.Compose(
            [
                torchvision.transforms.ToTensor(),
            ]
        )
testset = torchvision.datasets.CIFAR10(
            root="data/cifar10", train=False, download=True, transform=transform_test
        )
test_loader = torch.utils.data.DataLoader(
        testset, batch_size=BATCH_SIZE, shuffle=False, num_workers=2
    )

# Create the PGD-20 attack
classifier_att = PyTorchClassifier(
                    model=morel.model,
                    clip_values=(0.0, 1.0),
                    loss=nn.CrossEntropyLoss(),
                    optimizer=optimizer,
                    input_shape=(3, 32, 32),
                    nb_classes=morel.num_class,
                )
attack = ProjectedGradientDescent(
                    estimator=classifier_att,
                    norm=np.inf,
                    eps=morel.epsilon,
                    eps_step=morel.eval_step_size,
                    max_iter=20,
                    targeted=False,
                    num_random_init=0,
                    batch_size=BATCH_SIZE,
                )

# Test the robustness of the trained model against this attack:
clean_accuracy, robust_accuracy = morel.test(test_loader, attack=attack)

Citation

If you find advermorel useful in your research, please consider citing:

@inproceedings{hotegni2025morel,
  title     = {Enhancing Adversarial Robustness through Multi-Objective Representation Learning},
  author    = {Hotegni, Sedjro Salomon and Peitz, Sebastian},
  booktitle = {International Conference on Artificial Neural Networks},
  year      = {2025},
  publisher = {Springer}
}

Metadata

Release files for advermorel 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for advermorel 0.1.6
File Size Uploaded Provenance Actions
advermorel-0.1.6.tar.gz 26.6 kB Without provenance Details for advermorel-0.1.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for advermorel 0.1.6
File Interpreter ABI Platform Actions
advermorel-0.1.6-py3-none-any.whl Python 3 none any Details for advermorel-0.1.6-py3-none-any.whl

Total release size: 50.5 kB

Release files / advermorel-0.1.6.tar.gz

Download URL advermorel-0.1.6.tar.gz
Size 26.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d584efbfbaf190d7b417b7ba04037bc2e38c95bc37199017def46abda70387f5
BLAKE2b-256 checksum
How to use checksums
180c121f2a89f920f53e4d271752e8c734918b037d1a09a7cbe373dea34b56df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / advermorel-0.1.6-py3-none-any.whl

Download URL advermorel-0.1.6-py3-none-any.whl
Size 23.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c6cb08c46ab6e033c01e76fe7a718537c206489a0c1b06be71d7b387d04a008
BLAKE2b-256 checksum
How to use checksums
6e11f25549612ad7e2cea72a8bce6f41b953e07bbb9717cbedf730ebb7470cb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Security for advermorel 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page