# 3. Design Auto pipeline for ablation Experiments

## &#x20;

This tutorial guides you through the process of writing an experiment notebook for different configurations in a self-supervised learning (SSL) model. This is particularly useful when you want to test different configurations and compare their performance.

### Step 1: Set Configuration

The first step is to load the YAML configuration file, which contains the settings for your experiment.

```python
import yaml

with open('global.yaml', 'r') as file:
    global_config = yaml.safe_load(file)

global_config["experiment"] = "batch VS model"
```

### Step 2: Define Data Augmentations

Next, define the data augmentations. These are transformations applied to the data before feeding it into the model, such as cropping, flipping, or rotating.

```python
global_SSL_augmentation = {
    'RandomResizedCrop': {'size': (global_config["input_size"], global_config["input_size"])},
    'RandomApply':{'transforms':[RandomRotation(degrees=90)], 'p':0.8},
    'RandomHorizontalFlip': {'p': 0.5},
    'RandomVerticalFlip':  {'p':0.5},
    'RandomApply':{'transforms': [ColorJitter(brightness=0.04,contrast=0.04,saturation=0.02,hue=0.01)], 'p':0.8},
    'RandomGrayscale' :{'p':0.2},
    'RandomSolarize':{'threshold':128, 'p':0.1},
    'RandomApply':{'transforms':[GaussianBlur(kernel_size=3,sigma=(0.2, 2))],'p':0.8},
    'ToTensor': {},
    'Normalize': {"mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225]}
}
```

### Step 3: Create a Config for Each Experiment

In this step, you create a new config for each experiment by copying the global config and modifying the necessary parameters.

```python

for batch_num in [32, 64,128,256,512,1024,2048]:
    # MAKE YOUR OWN CONFIG
    config=global_config.copy()
    # Fill the config
    SSL_augmentation=global_SSL_augmentation.copy()
    config["name"]=f"barlow_batch_{batch_num}"
    config["batch"]=batch_num
    config["model"]="BarlowTwins"

    # THIS IS THE CODE TO LOAD DATASET
    pdata= PipeDataset(config=config,augmentation=dict2transformer(SSL_augmentation,view=config["view"]))
    # THIS IS THE CODE TO LOAD MODEL
    pmodel=pipe_model(config=config)

    # Use this if you want to START a train
    trainer=Trainer(config, model_mode="start")
    trainer.fit(pmodel, pdata.dataloader)  

    # Use this if you want to CONTINUE to train
    #trainer1=Trainer(config, model_mode="continue", extra_epoch=0)
    #trainer1.fit(pmodel, pdata.dataloader,ckpt_path="latest")  
```

If you wish to continue training from a previous checkpoint, you can do so by setting `model_mode` to "continue":

```python
trainer_continue = Trainer(config, model_mode="continue", extra_epoch=0)
trainer_continue.fit(pmodel, pdata.dataloader,ckpt_path="latest")   
```

This will save the model and the training state to a local file, preserving the experiment setup.

### Step 4: Evaluation

After training, you can evaluate the model's performance using the provided evaluation functions.

```python
from autoSSL.evaluate import eval_linear, eval_KNN, eval_KNNplot
from autoSSL.utils import pipe_collate

test_augmentation = {
    'RandomResizedCrop': {'size': (global_config["input_size"], global_config["input_size"])},
    'ToTensor': {},
    'Normalize': {"mean": [0.485, 0.456, 0.406], "std": [0.229,

 0.224, 0.225]}
}

# Load test data
pdata = PipeDataset(input_dir=global_config["path_to_test_cifar10"],augmentation=dict2transformer(test_augmentation,view=1))

# Load models from checkpoint directory
collate =pipe_collate(address="experiment_checkpoints/batch VS model/", reg="batch_[0-9]+")

# Evaluate models
aaa=eval_linear(pdata, models=collate, device=global_config["device"], split=0.8)
```

This setup will help you keep each experiment's configuration, model weights, and results organized. You can easily track the performance of each configuration and compare them to select the best one for your task.


---

# 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://autossl.gitbook.io/autossl/advanced/3.-design-auto-pipeline-for-ablation-experiments.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.
