# 1. Train a customized SSL Model for single case

## &#x20;

This tutorial will walk you through how to create a dataset, initialize a self-supervised learning (SSL) model, train the model, and evaluate its performance. The tutorial uses the `autoSSL` library, a package created for self-supervised learning research. Please note that this tutorial assumes you have a basic understanding of Python, machine learning, and PyTorch.

### Step 1: Create the Dataset

First, we need to define how to transform the input data, specifically the type and parameters of the augmentation techniques to be applied. We're going to use the global view function `global_SSL_augmentation` that was defined in the script you provided.

Next, let's create the dataset using `PipeDataset`:

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

SSL_augmentation = global_SSL_augmentation
config = global_config

pipe_data_test = PipeDataset(
    input_dir=config["input_dir"], 
    augmentation=dict2transformer(SSL_augmentation, view=config["view"]),
    samples=config["samples"],
    batch_size=config["batch_size"],
    shuffle=config["shuffle"],
    drop_last=config["drop_last"],
    num_workers=config["num_workers"]
)
```

We can now explore the data:

```python
pipe_data_test.plot([2,3,4])
```

You can print the length of the dataset:

```python
print(f".pipe_data :{len(pipe_data_test)}")     # get dataset class
```

To access a specific data item, you can use:

```python
pipe_data_test[2]
```

To get the dataloader, use:

```python
pipe_data_test.dataloader  # get dataloader  
```

To print the length of the dataset class:

```python
print(f".dataset :{len(pipe_data_test.dataset)}")     # get dataset class
```

To get the array of data:

```python
temp=pipe_data_test.array
print(f".array :{len(temp)}")       #get array.
print(f".array[0] :{len(temp[0])}")       #get array.
```

### Step 2: Define the Model

We're going to use `pipe_model` to define our model:

```python
from autoSSL.models import pipe_model

pmodel = pipe_model(
    backbone=config["backbone"], 
    stop_gradient=config["stop_gradient"], 
    prjhead_dim=config["prjhead_dim"]
)
```

### Step 3: Training

Before training, we need to setup a trainer:

```python
from pytorch_lightning import Trainer
from autoSSL.utils import ck_callback, join_dir, ContinuousCSVLogger

trainer = Trainer(
    max_epochs=config["max_epochs"]+3,
    accelerator=config["device"], 
    callbacks=[ck_callback(config["log_dir"])],
    logger=ContinuousCSVLogger(save_dir=config["log_dir"], name=config["name"])
)
```

To train the model, use the `fit` function of the trainer:

```python
trainer.fit(pmodel, pipe_data_test.dataloader, ckpt_path=(join_dir(config["log_dir"])))
```

To continue training from a previous state, you can load previous weights:

```python
trainer.fit(pmodel, pipe_data_test.dataloader, ckpt_path=(join_dir(config["log_dir"],"checkpoints-epoch=02-train_loss=20.89.ckpt")))  
```

### Step 4: Evaluation

After the model is trained, it is time to evaluate its performance. You can use the provided `eval_linear`, `eval_KNN` and `eval_KNNplot` functions from the `autoSSL.evaluate` module:

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

eval_linear(pipe_data_train, embedding_model=model, split=0.8, device="cuda") 
eval_KNN(pipe_data_train, embedding_model=model, split=0.8, device="cuda") 
eval_KNNplot(pipe_data_test, embedding_model=model, samples=2, device="cuda")
```

You can also evaluate the model using a test set:

```python
eval_linear(pipe_data_train, embedding_model=model, test=pipe_data_test, device="cuda") 
eval_KNN(pipe_data_train, embedding_model=model, test=pipe_data_test, device="cuda") 
```

This tutorial provided a walkthrough for preparing your data, creating an SSL model, training the model, and evaluating it. Please note that the `autoSSL` library is a research library, and the functions provided may not include every feature or setting you need for your own experiments. Always adapt and expand the code to meet your own needs when performing machine learning experiments.


---

# 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/tutorial/1.-train-a-customized-ssl-model-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.
