Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate dreamsim #148

Merged
merged 13 commits into from
Jul 26, 2023
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ $ pip install git+https://github.com/serre-lab/Harmonization.git
$ pip install keras-cv-attention-models>=1.3.5
```

If you want to extract features for [DreamSim](https://dreamsim-nights.github.io/) from the [DreamSim repo](https://github.com/ssundaram21/dreamsim), you have to additionally run the following `pip` command in your `thingsvision` environment,

```bash
$ pip install dreamsim==0.1.2
```

See the [docs](https://thingsvision.github.io/AvailableModels.html) for which `DreamSim` models are available in `thingsvision`.

#### Google Colab.
Alternatively, you can use Google Colab to play around with `thingsvision` by uploading your image data to Google Drive (via directory mounting).
You can find the jupyter notebook using `PyTorch` [here](https://colab.research.google.com/github/ViCCo-Group/thingsvision/blob/master/notebooks/pytorch.ipynb) and the `TensorFlow` example [here](https://colab.research.google.com/github/ViCCo-Group/thingsvision/blob/master/notebooks/tensorflow.ipynb).
Expand Down
27 changes: 27 additions & 0 deletions docs/AvailableModels.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,30 @@ extractor = get_extractor(
model_parameters=model_parameters
)
```

### DreamSim
We provide the [DreamSim](https://dreamsim-nights.github.io/) model from the original [DreamSim repo](https://github.com/ssundaram21/dreamsim). To extract features, first install the `dreamsim` package with the following `pip` command
```bash
$ pip install dreamsim==0.1.2
```
The model name is:
- `DreamSim`

We provide two `DreamSim` architectures: CLIP ViT-B/32 and OpenCLIP ViT-B/32. This can be specified using the `model_parameters` argument. For instance, to get the OpenCLIP variant of DreamSim you would do the following:
```python
model_name = 'DreamSim'
source = 'custom'
device = 'cuda'
model_parameters = {
'variant': 'open_clip_vitb32'
}

extractor = get_extractor(
model_name=model_name,
source=source,
device=device,
pretrained=True,
model_parameters=model_parameters
)
```
To load the CLIP ViT-B/32 variant, pass `'clip_vitb32'` to the `variant` parameter instead.
24 changes: 22 additions & 2 deletions notebooks/pytorch.ipynb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -9,13 +10,15 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Settings"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "3ICWd-3iA671"
Expand Down Expand Up @@ -67,13 +70,15 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Image and feature directories"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "PHSuNkaIAZw2"
Expand All @@ -94,6 +99,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "xF0R7sFu-7gI"
Expand Down Expand Up @@ -129,6 +135,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -211,14 +218,15 @@
" class_names=class_names,\n",
" file_names=file_names,\n",
" )\n",
" # replace with e.g., [f'conv_{l:02d}'] or [f'fc_{l:02d}']\n",
" features_per_layer[f'layer_{l:02d}'] = features\n",
" # if you want, replace for simplicity with e.g., [f'conv_{l:02d}'], [f'fc_{l:02d}'], or [f'layer_{l:02d}']\n",
" features_per_layer[f'{module_name}'] = features\n",
" # save features to disk\n",
" save_features(features, out_path=f'{out_path}/features_{model_name}_{module_name}', file_format=file_format)\n",
" return features_per_layer"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -245,20 +253,23 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Select `model` and `layer` for which you want to extract image features. If you want to extract features from a `torchvision` model, use the model naming defined [here](https://pytorch.org/vision/stable/models.html) (e.g., `vgg16` if you want to use VGG-16). If you are uncertain about the naming and enumeration of the layers, use `extractor.show_model()` to see how specific layers are called. This command will show the architecture of the model."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example 1: VGG-16 with batch norm (pretrained on ImageNet)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -300,6 +311,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -335,6 +347,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -388,6 +401,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -430,6 +444,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -463,6 +478,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -516,6 +532,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -558,6 +575,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -591,6 +609,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -634,6 +653,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down
19 changes: 17 additions & 2 deletions notebooks/tensorflow.ipynb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -9,13 +10,15 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Settings"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "3ICWd-3iA671"
Expand Down Expand Up @@ -66,13 +69,15 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Image and feature directories"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "PHSuNkaIAZw2"
Expand All @@ -93,6 +98,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "xF0R7sFu-7gI"
Expand Down Expand Up @@ -128,6 +134,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -214,14 +221,15 @@
" class_names=class_names,\n",
" file_names=file_names,\n",
" )\n",
" # replace with e.g., [f'conv_{l:02d}'] or [f'fc_{l:02d}']\n",
" features_per_layer[f'{layer}_{l:02d}'] = features\n",
" # if you want, replace for simplicity with e.g., [f'conv_{l:02d}'], [f'fc_{l:02d}'], or [f'{layer}_{l:02d}']\n",
" features_per_layer[f'{module_name}'] = features\n",
" # save features to disk\n",
" save_features(features, out_path=f'{out_path}/features_{model_name}_{module_name}', file_format='npy')\n",
" return features_per_layer"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -247,20 +255,23 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Select `model` and `layer` for which you want to extract image features. If you want to extract features from a `torchvision` model, use the model naming defined [here](https://pytorch.org/vision/stable/models.html) (e.g., `vgg16` if you want to use VGG-16). If you are uncertain about the naming and enumeration of the layers, use `model.show()` to see how specific layers called."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### VGG-16 (pretrained on ImageNet)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -304,6 +315,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -334,6 +346,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -385,6 +398,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -428,6 +442,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ torch==1.13.*
torchvision==0.14.*
torchtyping
tqdm
dreamsim==0.1.2
git+https://github.com/openai/CLIP.git
git+https://github.com/serre-lab/Harmonization.git
12 changes: 12 additions & 0 deletions tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@
"pretrained": True,
"source": "custom",
"kwargs": {"variant": "ViT_B16"},
},
"DreamSim": {
"modules": ["model.mlp"],
"pretrained": True,
"source": "custom",
"kwargs": {"variant": "clip_vitb32"},
},
"DreamSim": {
"modules": ["model.mlp"],
"pretrained": True,
"source": "custom",
"kwargs": {"variant": "open_clip_vitb32"},
}
}

Expand Down
5 changes: 2 additions & 3 deletions thingsvision/core/extraction/base.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import abc
import re
import os
import re
import warnings
from typing import Callable, Iterator, List, Optional, Union

import numpy as np
import torch
from torchtyping import TensorType
from tqdm.auto import tqdm

import torch

Array = np.ndarray


Expand Down
Loading
Loading