Skip to content

Commit 78f2dc3

Browse files
Zihan WangZihan Wang
authored andcommitted
core implementation
1 parent ae49585 commit 78f2dc3

File tree

3,731 files changed

+214676
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

3,731 files changed

+214676
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

README.md

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
# AIM
2+
3+
This repository contains the implementations of our paper:
4+
**"AI Model Modulation with Logits Redistribution"**.
5+
6+
## Introduction
7+
8+
Large-scale models are typically adapted to meet the diverse requirements of model owners and users. However, maintaining multiple specialized versions of the model is inefficient. In response, we propose AIM, a novel model modulation paradigm that enables a single model to exhibit diverse behaviors to meet the specific end requirements. AIM enables two key modulation modes: utility and focus modulations. The former provides model owners with dynamic control over output quality to deliver varying utility levels, and the latter offers users precise control to shift model’s focused input features. AIM introduces a logits redistribution strategy that operates in a training data-agnostic and retraining-free manner. We establish a formal foundation to ensure AIM’s regulation capability, based on the statistical properties of logits ordering via joint probability distributions. Our evaluation confirms AIM’s practicality and versatility for AI model modulation, with tasks spanning image classification, semantic segmentation and text generation, and prevalent architectures including ResNet, SegFormer and Llama.
9+
10+
## CV Task1: Image Classification
11+
### 1. Project Structure
12+
13+
```
14+
img_classification
15+
├── dataset/
16+
└── model/
17+
└── val.py
18+
19+
```
20+
21+
### 2. Quick Start
22+
* Install dependencies
23+
```python
24+
pip install torch torchvision matplotlib
25+
```
26+
* Run evaluation (CIFAR-10/100 example)
27+
For CIFAR10/CIFAR100, we use pretrained models from: ***[PyTorch CIFAR Models](https://github.com/chenyaofo/pytorch-cifar-models).***
28+
```python
29+
python img_classification/val.py --dataset cifar10 --model resnet56 --batch_size 128 --num_perturb_steps 101 --perturb_step 0.2 --plot --save_plot
30+
```
31+
- Results from Our paper for reference:
32+
<br><img width="380" alt="image" src="https://github.com/user-attachments/assets/77c6881f-23b6-4679-8ff1-50f628e12329" />
33+
34+
35+
36+
## CV Task2: Semantic Segmentation
37+
### 1. Project Structure
38+
39+
```
40+
semantic-segmentation-main/
41+
├── assests/ # Test images
42+
├── configs/ # Configuration files
43+
├── data/ # Dataset root
44+
│ └── (example) ADEChallengeData2016/
45+
│ ├── annotations/
46+
│ └── images/
47+
├── tools/perturbation/
48+
│ ├── models.py # Perturbation models
49+
│ ├── evaluation.py # Metric evaluation
50+
│ └── infer_perturbation.py # Inference script
51+
└── output/ # Results directory
52+
53+
```
54+
55+
### 2. Setup Instructions
56+
- Please download base from ***[Semantic Segmentation](https://github.com/sithu31296/semantic-segmentation/tree/main)*** or Clone base repository
57+
```bash
58+
git clone https://github.com/sithu31296/semantic-segmentation.git
59+
```
60+
- Set up env
61+
```python
62+
pip install -e .
63+
```
64+
- Navigate to the base directory
65+
```bash
66+
cd semantic-segmentation
67+
```
68+
```bash
69+
git clone https://github.com/CurtisYoung18/AIM.git
70+
```
71+
- ***Copy and Paste*** our segmentaion py files to semantic-segmentation-main/tools/
72+
```bash
73+
cp -r AIM-main/semantic_segmentation/perturbation tools/
74+
```
75+
- Download datasets(eg.***[ADE20K datasets](http://sceneparsing.csail.mit.edu/)***). Place the dataset in the root directory and update the dataset path in configs accordingly
76+
- Download the backbone model(eg. ***[Backbones](https://github.com/sithu31296/semantic-segmentation/blob/main/docs/BACKBONES.md), [Pretrained Models](https://github.com/sithu31296/semantic-segmentation/blob/main/docs/MODELS.md)***). Update the configuration file accordingly
77+
- Add configs to desired yaml files:
78+
```python
79+
PERTURBATION:
80+
METHOD: 'single' # Options: 'single' or 'multi'
81+
RATIO: 0.2 # Base perturbation ratio
82+
STEP_SIZE: 0.4 # Ratio increment step
83+
STEP_LIMIT: 10 # Number of perturbation steps
84+
TARGET_IDX: 136 # Class index for single perturbation
85+
POSITIVE: true # Perturbation direction
86+
TARGETS: # For multi-class perturbation
87+
- index: 136
88+
positive: true
89+
- index: 20
90+
positive: false
91+
```
92+
- (Optional)Download inference pictures(eg. ***[Kitty](https://www.kaggle.com/datasets/klemenko/kitti-dataset)***) for testing and place them to assests(eg. /ade)
93+
94+
95+
### 3.Run
96+
<details open>
97+
<summary><strong>Inference</strong></summary>
98+
99+
To make an inference, edit the parameters of the config file from below.
100+
* Change `MODEL` >> `NAME` and `BACKBONE` to your desired pretrained model.
101+
* Change `DATASET` >> `NAME` to the dataset name depending on the pretrained model.
102+
* Set `TEST` >> `MODEL_PATH` to pretrained weights of the testing model.
103+
* Change `TEST` >> `FILE` to the file or image folder path you want to test.
104+
* Testing results will be saved in `SAVE_DIR`.
105+
106+
- Example:
107+
```python
108+
python tools/perturbation/infer_perturbation.py \
109+
--cfg configs/ade20k.yaml \
110+
--pert_ratio 0.2 \
111+
--pert_method single
112+
```
113+
</details>
114+
115+
<details open>
116+
<summary><strong>Evaluation</strong></summary>
117+
118+
```python
119+
tools/perturbation/evaluation.py --cfg configs/ade20k.yaml
120+
```
121+
</details>
122+
123+
- Results from Our paper for reference:
124+
<br> ![image](https://github.com/user-attachments/assets/115bab21-27c3-4bdc-85dd-1e12a5fd1f50)
125+
126+
127+
### 4. Core Components
128+
129+
| File | Description |
130+
|-----------------------|---------------------------------------------------------------|
131+
| `models.py` | Implements SingleClassPerturbator and MultiClassPerturbator |
132+
| `evaluation.py` | Evaluation is performed over a range of perturbation ratios on the "person" class. |
133+
| `infer_perturbation.py` | Visualization pipeline with perturbation controls |
134+
135+
136+
## NLP Task: Modulation Modultion during Text Generation
137+
We tailored the OpenCompass framework to include a new class `HuggingFaceNoiseModel` which introduces the capability to inject Gaussian noise into the logits during the generation process of a Hugging Face model, thus enable model modulation for AI Models.
138+
139+
## Key Modifications
140+
141+
Gaussian Noise Injection:
142+
143+
The `GaussianNoiseLogitsProcessor` class is responsible for adding Gaussian noise to the logits (scores) during the generation process. The noise is sampled from a normal distribution with a configurable mean and standard deviation (std).
144+
145+
Integration with Generation Configuration:
146+
147+
The HuggingFaceNoiseModel class checks for the presence of a noise_std parameter in the generation_kwargs. If found, it initializes the GaussianNoiseLogitsProcessor with the specified standard deviation and adds it to the LogitsProcessorList.
148+
149+
The noise_std parameter is then removed from generation_kwargs to avoid conflicts during the generation process.
150+
151+
OpencCompass Config:
152+
153+
THe main script is eval_noise_llama3.py, which contains the model, datasets, and std config. You can reproduce our experiments following Usage step by step.
154+
155+
156+
## Usage
157+
To use the HuggingFaceNoiseModel class, follow these steps:
158+
159+
### 🛠️ Installation
160+
161+
Below are the steps for quick installation and datasets preparation.
162+
163+
#### 💻 Environment Setup
164+
165+
We highly recommend using conda to manage your python environment.
166+
167+
- #### Create your virtual environment
168+
169+
```bash
170+
conda create --name opencompass python=3.10 -y
171+
conda activate opencompass
172+
```
173+
174+
- #### Install OpenCompass via pip
175+
176+
```bash
177+
pip install -U opencompass
178+
179+
## Full installation (with support for more datasets)
180+
# pip install "opencompass[full]"
181+
182+
## Environment with model acceleration frameworks
183+
## Manage different acceleration frameworks using virtual environments
184+
## since they usually have dependency conflicts with each other.
185+
# pip install "opencompass[lmdeploy]"
186+
# pip install "opencompass[vllm]"
187+
188+
## API evaluation (i.e. Openai, Qwen)
189+
# pip install "opencompass[api]"
190+
```
191+
192+
- #### Install OpenCompass from source
193+
194+
If you want to use opencompass's latest features, or develop new features, you can also build it from source
195+
196+
```bash
197+
git clone https://github.com/open-compass/opencompass opencompass
198+
cd opencompass
199+
pip install -e .
200+
# pip install -e ".[full]"
201+
# pip install -e ".[vllm]"
202+
```
203+
- ### Run command
204+
```bash
205+
python -u run.py configs/eval_noise_llama3.py
206+
```

custom_opencompass/.codespellrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[codespell]
2+
skip = *.ipynb
3+
count =
4+
quiet-level = 3
5+
ignore-words-list = nd, ans, ques, rouge, softwares, wit
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
name: 🐞 Bug report
2+
description: Create a report to help us improve
3+
labels: ["bug"]
4+
title: "[Bug] "
5+
body:
6+
- type: markdown
7+
attributes:
8+
value: |
9+
For general questions or idea discussions, please post it to our [**Forum**](https://github.com/open-compass/opencompass/discussions).
10+
If you have already identified the reason, we strongly appreciate you creating a new PR according to [the tutorial](https://opencompass.readthedocs.io/en/master/community/CONTRIBUTING.html)!
11+
If you need our help, please fill in the following form to help us to identify the bug.
12+
13+
- type: checkboxes
14+
attributes:
15+
label: Prerequisite
16+
description: Please check the following items before creating a new issue.
17+
options:
18+
- label: I have searched [Issues](https://github.com/open-compass/opencompass/issues/) and [Discussions](https://github.com/open-compass/opencompass/discussions) but cannot get the expected help.
19+
required: true
20+
- label: The bug has not been fixed in the [latest version](https://github.com/open-compass/opencompass).
21+
required: true
22+
23+
- type: dropdown
24+
id: task
25+
attributes:
26+
label: Type
27+
description: The problem arises when
28+
options:
29+
- I'm evaluating with the officially supported tasks/models/datasets.
30+
- I have modified the code (config is not considered code), or I'm working on my own tasks/models/datasets.
31+
validations:
32+
required: true
33+
34+
- type: textarea
35+
id: environment
36+
validations:
37+
required: true
38+
attributes:
39+
label: Environment
40+
description: |
41+
Please run `python -c "import opencompass.utils;import pprint;pprint.pprint(dict(opencompass.utils.collect_env()))"` to collect necessary environment information and paste it here.
42+
placeholder: |
43+
```python
44+
# The output the above command
45+
```
46+
47+
- type: textarea
48+
attributes:
49+
label: Reproduces the problem - code/configuration sample
50+
description: |
51+
Please provide a code or configuration sample that reproduces the problem you ran into. It can be a Colab link or just a code snippet.
52+
placeholder: |
53+
```python
54+
# Sample code to reproduce the problem
55+
```
56+
validations:
57+
required: true
58+
59+
- type: textarea
60+
attributes:
61+
label: Reproduces the problem - command or script
62+
description: |
63+
What command or script did you run?
64+
placeholder: |
65+
```shell
66+
The command or script you run.
67+
```
68+
validations:
69+
required: true
70+
71+
- type: textarea
72+
attributes:
73+
label: Reproduces the problem - error message
74+
description: |
75+
Please provide the error message or logs you got, with the full traceback.
76+
77+
Tip: You can attach images or log files by dragging them into the text area..
78+
placeholder: |
79+
```
80+
The error message or logs you got, with the full traceback.
81+
```
82+
validations:
83+
required: true
84+
85+
- type: textarea
86+
id: other
87+
attributes:
88+
label: Other information
89+
description: |
90+
Tell us anything else you think we should know.
91+
92+
1. What's your expected result?
93+
2. What dataset did you use?
94+
3. What do you think might be the reason?
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: 🚀 Feature request
2+
description: Suggest an idea for this project
3+
labels: ["enhancement"]
4+
title: "[Feature] "
5+
body:
6+
- type: markdown
7+
attributes:
8+
value: |
9+
For general questions or idea discussions, please post it to our [**Forum**](https://github.com/open-compass/opencompass/discussions).
10+
If you have already implemented the feature, we strongly appreciate you creating a new PR according to [the tutorial](https://opencompass.readthedocs.io/en/master/community/CONTRIBUTING.html)!
11+
12+
- type: textarea
13+
id: describe
14+
validations:
15+
required: true
16+
attributes:
17+
label: Describe the feature
18+
description: |
19+
What kind of feature do you want OpenCompass to add. If there is an official code release or third-party implementation, please also provide the information here, which would be very helpful.
20+
placeholder: |
21+
A clear and concise description of the motivation of the feature.
22+
Ex1. It is inconvenient when \[....\].
23+
Ex2. There is a recent paper \[....\], which is very helpful for \[....\].
24+
25+
- type: checkboxes
26+
id: pr
27+
attributes:
28+
label: Will you implement it?
29+
options:
30+
- label: I would like to implement this feature and create a PR!

0 commit comments

Comments
 (0)