> For the complete documentation index, see [llms.txt](https://docs.raga.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.raga.ai/ragaai-prism/test-inventory/semantic-segmentation/semantic-similarity.md).

# Semantic Similarity

Evaluate similarity between segmentation outputs. Improve consistency with semantic checks.

### Execute Test:

The provided code snippet is intended to set up and execute a Semantic Similarity Test in RagaAI to assess the likeness between HR and LR images.

**Step 1: Define the Semantic Similarity Rules**

Start by setting the rules that will assess the semantic similarity between the image pairs.

```python
rules = LQRules()
rules.add(metric="similarity_score", metric_threshold=0.2)

semantic_similarity_test = semantic_similarity(test_session=test_session,
                                      dataset_name = "dataset_name",
                                      test_name = "semantic_similarity_test",
                                      type = "semantic_similarity",
                                      output_type="super_resolution",
                                      embed_col_name="lr_embedding",
                                      generated_embed_col_name="hr_embedding",
                                      rules=rules)
                                      
test_session.add(semantic_similarity_test)

test_session.run()
```

* `LQRules()`: Initialises the rules for the semantic similarity test.
* `rules.add()`: Adds a rule to evaluate the similarity:
  * `metric`: The performance metric to use, "similarity\_score" in this instance.
  * `metric_threshold`: The threshold value for the similarity score, indicating the level of likeness required.
* `semantic_similarity()`: Prepares the semantic similarity test with the following parameters:
  * `test_session`: The session object tied to your RagaAI project.
  * `dataset_name`: The name of the dataset you're working with.
  * `test_name`: A descriptive name for this test, e.g., "semantic\_similarity\_test".
  * `type`: The type of test, which is "semantic\_similarity" in this context.
  * `output_type`: The type of output, "super\_resolution" here, indicating the nature of the images being compared.
  * `embed_col_name`: The column name containing embeddings of the LR images.
  * `generated_embed_col_name`: The column name containing embeddings of the HR images.
  * `rules`: The ruleset you've defined for measuring semantic similarity.

`test_session.add()`: Registers the semantic similarity test with the session.

`test_session.run()`: Initiates the execution of all tests within the session, including the semantic similarity test.

By following these instructions, you have initiated a Semantic Similarity Test in RagaAI. This process will help you quantify the semantic likeness between your dataset's LR and HR images.

### Analysing Test Results

<figure><img src="https://1811327582-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYbIiNdp1QbG4avl7VShw%2Fuploads%2F575EpZbc45jbT0n8tXYr%2Fimage.png?alt=media&amp;token=fa7edaaf-d09a-49c9-bc86-eaa01025fe07" alt=""><figcaption></figcaption></figure>

#### Understanding Similarity Scores

* **Score Calculation**: The test computes a similarity score for each HR-LR image pair.

#### Analysing Results

* **Embedding View**: Use this interactive feature to observe the distribution of similarity scores.
* **Datagrid View**: Visualise the HR and LR images along with their scores.

#### Detailed Image Analysis

* **Image View**: Clicking on an image in the Datagrid View opens a detailed view, where you can see the HR image alongside its corresponding LR image and their calculated similarity score.

This test is instrumental in ensuring the super-resolution process maintains fidelity across image resolutions.

After the test concludes, examine the results to see how well your HR images are matching up to the LR ones.
