Skip to main content

MOREL is a multi-objective optimization framework for improving DNN robustness against adversarial attacks.

Project description

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 src/advermorel
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,
            scheduler=scheduler,
            num_epochs=EPOCHS, 
            train_loader=train_loader, 
            val_loader=test_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 CW-∞ attack:

from art.attacks.evasion import CarliniLInfMethod
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 CW-inf 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 = CarliniLInfMethod(
            classifier=classifier_att,
            targeted=False,
            initial_const=15,
            learning_rate=1e-2,
            max_iter=10,
            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}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

advermorel-0.1.2.tar.gz (26.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

advermorel-0.1.2-py3-none-any.whl (23.5 kB view details)

Uploaded Python 3

File details

Details for the file advermorel-0.1.2.tar.gz.

File metadata

  • Download URL: advermorel-0.1.2.tar.gz
  • Upload date:
  • Size: 26.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for advermorel-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5d9c1522f456efa068b0e9ce36d199265be72719a536390957f82e37d1b10ba1
MD5 49b5cc3fa680984ccbb83f6d7c2e95f1
BLAKE2b-256 b674d6477aa329afd0bc2d6a71fbdd2a1f2cc7483a602ffe503d9173be9c97c4

See more details on using hashes here.

File details

Details for the file advermorel-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: advermorel-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 23.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for advermorel-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4bf118a5c9d94814b5c774e67439ee2026e381d6808c801a2be8a4ce6d9cc248
MD5 f6f6961bc8440df29f01d53382763895
BLAKE2b-256 5843a702f57fef6d8226f05ac3d6f5a2ba1fa624d5e3237199453b1798d06821

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page