# 2. Automatic training for Single Case

## &#x20;

This tutorial guides you through the process of training a self-supervised learning (SSL) model using a YAML configuration file. This method allows you to define various parameters and settings for the model and its training in a structured way.

### 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

The next step is to define data augmentations. These are modifications made to your data before it is fed into the model. This can include transformations such as flipping, rotating, or cropping. The example below defines a dictionary of augmentations.

```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: Use Pipe for Everything

Now we will use `PipeDataset` to load the dataset and `pipe_model` to load the model. These are part of the `autoSSL` package and make it easy to streamline your workflow.

```python
from autoSSL.data import PipeDataset
from autoSSL.models import pipe_model
from autoSSL.utils import dict2transformer

# Load Dataset
pdata = PipeDataset(config=global_config, augmentation=dict2transformer(global_SSL_augmentation, view=global_config["view"]))

# Load Model
pmodel = pipe_model(config=global_config)

# Training the Model
from autoSSL.train import Trainer

# Start a new training
trainer=Trainer(config=global_config, model_mode="start")
trainer.fit(pmodel, pdata.dataloader)  
```

To continue training from a previous checkpoint, use the following code:

```python
# Continue from a previous training
trainer_continue=Trainer(config=global_config, model_mode="continue", extra_epoch=0)
trainer_continue.fit(pmodel, pdata.dataloader,ckpt_path="latest")   
```

### Step 4: Evaluation

After training, evaluate the model's performance using the provided evaluation functions from the `autoSSL.evaluate` module:

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

eval_linear(pdata, embedding_model=pmodel, split=0.8, device="cuda") 
eval_KNN(pdata, embedding_model=pmodel, split=0.8, device="cuda") 
eval_KNNplot(pdata, embedding_model=pmodel, samples=2, device="

cuda")
```

The `eval_linear` and `eval_KNN` functions evaluate the model's performance using linear evaluation and K-nearest neighbors (KNN), respectively. The `eval_KNNplot` function plots the KNN results for visualization.


---

# 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/2.-automatic-training-for-single-case.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.
