Repository: ViCCo-Group/thingsvision (language: python)

Issue:
rename vgg ecoset model 
rename the vgg ecoset model to VGG16bn_ecoset


Discussion / hints:

Fix patch:
diff --git a/doc/.ipynb_checkpoints/pytorch-checkpoint.ipynb b/doc/.ipynb_checkpoints/pytorch-checkpoint.ipynb
new file mode 100644
index 00000000..9488484f
--- /dev/null
+++ b/doc/.ipynb_checkpoints/pytorch-checkpoint.ipynb
@@ -0,0 +1,214 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# Thingsvision "
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "## Settings"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "id": "fvJFUBiJ_j0W"
+   },
+   "outputs": [],
+   "source": [
+    "images_dir = 'thingsvision/images'  # path to images directory in GDrive\n",
+    "output_dir = 'thingsvision/features' # path to images directory in GDrive\n",
+    "backend = 'pt' # backend 'pt' for PyTorch or 'tf' for Tensorflow \n",
+    "pretrained = True # use pretrained model weights, if False set path to model weights"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {
+    "id": "PHSuNkaIAZw2"
+   },
+   "source": [
+    "Specify your google drive directory where the images are placed as well as the directory where you want the results saved. In this example the images are placed in the directory /thingsvision/images and the features will be put into /thingsvision/features. The images directory is expected to have a content like /dog/img1.png\n",
+    "\n",
+    "Specify which model and which layer you want to extract. If you want to use a torchvision model use the model name from https://pytorch.org/vision/stable/models.html. For example `vgg16` if you want to use `VGG16`. You can see the names of the layers by using `model.show()`.\n",
+    "\n",
+    "Specify whether you want to use pretrained model weights."
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {
+    "id": "3ICWd-3iA671"
+   },
+   "source": [
+    "Install thingsvision and dependencies"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "id": "h0nVMt-M_KX_"
+   },
+   "outputs": [],
+   "source": [
+    "!pip install --upgrade thingsvision"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "id": "yq-bNySyBGO-"
+   },
+   "outputs": [],
+   "source": [
+    "import torch\n",
+    "import thingsvision.vision as vision\n",
+    "from thingsvision.model_class import Model\n",
+    "from google.colab import drive\n",
+    "import os"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {
+    "id": "xF0R7sFu-7gI"
+   },
+   "source": [
+    "Mount Google Drive "
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "colab": {
+     "base_uri": "https://localhost:8080/"
+    },
+    "id": "I8nY_u1p-1F6",
+    "outputId": "fd0e4a58-872c-4bfd-fb79-17586d6e48ce"
+   },
+   "outputs": [],
+   "source": [
+    "mounted_dir = '/thingsvision'\n",
+    "drive.mount(mounted_dir, force_remount=True)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "colab": {
+     "base_uri": "https://localhost:8080/",
+     "height": 383
+    },
+    "id": "MkYIhI_P_Z6t",
+    "outputId": "d1d1f8d3-16bc-43bf-f4ff-7b4a292e274f"
+   },
+   "outputs": [],
+   "source": [
+    "full_images_dir = os.path.join(mounted_dir, 'MyDrive', images_dir)\n",
+    "full_output_dir = os.path.join(mounted_dir, 'MyDrive', output_dir)\n",
+    "\n",
+    "def extract_features(model_name, module_name, full_images_dir, clip=False):\n",
+    "    device = 'cuda' if torch.cuda.is_available() else 'cpu'\n",
+    "    model = Model(model_name, pretrained=pretrained, model_path=None, device=device, backend=backend)\n",
+    "    dl = vision.load_dl(root=full_images_dir, out_path=full_output_dir, batch_size=64, transforms=model.get_transformations(), backend=backend)\n",
+    "    features, targets = model.extract_features(data_loader=dl, module_name=module_name, batch_size=64, flatten_acts=True, clip=clip, return_probabilities=False)\n",
+    "    return features \n",
+    "\n"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "## VGG16 - Pretrained"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "model_name = 'vgg16' \n",
+    "module_name = 'features.23' \n",
+    "features = extract_features(model_name, module_name, full_images_dir)\n",
+    "vision.save_features(features, f'{full_output_dir}/features_{model_name}_{module_name}', 'npy')"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "## VGG16 - trained on Ecoset dataset"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "model_name = 'VGG16_ecoset' \n",
+    "module_name = 'features.23' \n",
+    "features = extract_features(model_name, module_name, full_images_dir)\n",
+    "vision.save_features(features, f'{full_output_dir}/features_{model_name}_{module_name}', 'npy')"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "## CLIP"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "model_name = 'clip-ViT'\n",
+    "module_name = 'visual'\n",
+    "features = extract_features(model_name, module_name, full_images_dir, clip=True)\n",
+    "features = vision.center_features(features)\n",
+    "vision.save_features(features, f'{full_output_dir}/features_{model_name}_{module_name}', 'npy')"
+   ]
+  }
+ ],
+ "metadata": {
+  "colab": {
+   "collapsed_sections": [],
+   "name": "THINGSVISION.ipynb",
+   "provenance": []
+  },
+  "kernelspec": {
+   "display_name": "Python 3",
+   "language": "python",
+   "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.8.5"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 1
+}
diff --git a/doc/.ipynb_checkpoints/tensorflow-checkpoint.ipynb b/doc/.ipynb_checkpoints/tensorflow-checkpoint.ipynb
new file mode 100644
index 00000000..0eb5e5fe
--- /dev/null
+++ b/doc/.ipynb_checkpoints/tensorflow-checkpoint.ipynb
@@ -0,0 +1,175 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# Thingsvision "
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "## Settings"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {
+    "id": "PHSuNkaIAZw2"
+   },
+   "source": [
+    "Specify your google drive directory where the images are placed as well as the directory where you want the results saved. In this example the images are placed in the directory /thingsvision/images and the features will be put into /thingsvision/features. The images directory is expected to have a content like /dog/img1.png\n",
+    "\n",
+    "Specify which model and which layer you want to extract. If you want to use a torchvision model use the model name from https://pytorch.org/vision/stable/models.html. For example `vgg16` if you want to use `VGG16`. You can see the names of the layers by using `model.show()`.\n",
+    "\n",
+    "Specify whether you want to use pretrained model weights."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "id": "fvJFUBiJ_j0W"
+   },
+   "outputs": [],
+   "source": [
+    "images_dir = 'thingsvision/images'  # path to images directory in GDrive\n",
+    "output_dir = 'thingsvision/features' # path to images directory in GDrive\n",
+    "backend = 'tf' # backend 'pt' for PyTorch or 'tf' for Tensorflow \n",
+    "pretrained = True # use pretrained model weights, if False set path to model weights"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {
+    "id": "3ICWd-3iA671"
+   },
+   "source": [
+    "Install thingsvision and dependencies"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "id": "h0nVMt-M_KX_"
+   },
+   "outputs": [],
+   "source": [
+    "!pip install --upgrade thingsvision"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "id": "yq-bNySyBGO-"
+   },
+   "outputs": [],
+   "source": [
+    "import torch\n",
+    "import thingsvision.vision as vision\n",
+    "from thingsvision.model_class import Model\n",
+    "from google.colab import drive\n",
+    "import os"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {
+    "id": "xF0R7sFu-7gI"
+   },
+   "source": [
+    "Mount Google Drive "
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "colab": {
+     "base_uri": "https://localhost:8080/"
+    },
+    "id": "I8nY_u1p-1F6",
+    "outputId": "fd0e4a58-872c-4bfd-fb79-17586d6e48ce"
+   },
+   "outputs": [],
+   "source": [
+    "mounted_dir = '/thingsvision'\n",
+    "drive.mount(mounted_dir, force_remount=True)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "colab": {
+     "base_uri": "https://localhost:8080/",
+     "height": 383
+    },
+    "id": "MkYIhI_P_Z6t",
+    "outputId": "d1d1f8d3-16bc-43bf-f4ff-7b4a292e274f"
+   },
+   "outputs": [],
+   "source": [
+    "full_images_dir = os.path.join(mounted_dir, 'MyDrive', images_dir)\n",
+    "full_output_dir = os.path.join(mounted_dir, 'MyDrive', output_dir)\n",
+    "\n",
+    "def extract_features(model_name, module_name, full_images_dir, clip=False):\n",
+    "    device = 'cuda' if torch.cuda.is_available() else 'cpu'\n",
+    "    model = Model(model_name, pretrained=pretrained, model_path=None, device=device, backend=backend)\n",
+    "    dl = vision.load_dl(root=full_images_dir, out_path=full_output_dir, batch_size=64, transforms=model.get_transformations(), backend=backend)\n",
+    "    features, targets = model.extract_features(data_loader=dl, module_name=module_name, batch_size=64, flatten_acts=True, clip=clip, return_probabilities=False)\n",
+    "    return features \n",
+    "\n"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "## VGG16 - Pretrained"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "model_name = 'vgg16' \n",
+    "module_name = 'features.23' \n",
+    "features = extract_features(model_name, module_name, full_images_dir)\n",
+    "vision.save_features(features, f'{full_output_dir}/features_{model_name}_{module_name}', 'npy')"
+   ]
+  }
+ ],
+ "metadata": {
+  "colab": {
+   "collapsed_sections": [],
+   "name": "THINGSVISION.ipynb",
+   "provenance": []
+  },
+  "kernelspec": {
+   "display_name": "Python 3",
+   "language": "python",
+   "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.8.5"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 1
+}
diff --git a/doc/pytorch.ipynb b/doc/pytorch.ipynb
index 18a53580..5c880ac1 100644
--- a/doc/pytorch.ipynb
+++ b/doc/pytorch.ipynb
@@ -342,7 +342,7 @@
    "outputs": [],
    "source": [
     "## load model\n",
-    "model_name = 'VGG16_ecoset'\n",
+    "model_name = 'VGG16bn_ecoset'\n",
     "model = Model(\n",
     "            model_name,\n",
     "            pretrained=pretrained,\n",
diff --git a/thingsvision/custom_models/__init__.py b/thingsvision/custom_models/__init__.py
index 541b7e58..5e75bf54 100644
--- a/thingsvision/custom_models/__init__.py
+++ b/thingsvision/custom_models/__init__.py
@@ -1,2 +1,2 @@
 from thingsvision.custom_models.resnet50_ecoset import Resnet50_ecoset
-from thingsvision.custom_models.vgg16_ecoset import VGG16_ecoset
\ No newline at end of file
+from thingsvision.custom_models.vgg16bn_ecoset import VGG16bn_ecoset
\ No newline at end of file
diff --git a/thingsvision/custom_models/vgg16_ecoset.py b/thingsvision/custom_models/vgg16bn_ecoset.py
similarity index 77%
rename from thingsvision/custom_models/vgg16_ecoset.py
rename to thingsvision/custom_models/vgg16bn_ecoset.py
index b5d9b826..2bb98466 100644
--- a/thingsvision/custom_models/vgg16_ecoset.py
+++ b/thingsvision/custom_models/vgg16bn_ecoset.py
@@ -2,7 +2,7 @@
 import torchvision.models as torchvision_models
 import torch
 
-class VGG16_ecoset(Custom):
+class VGG16bn_ecoset(Custom):
     def __init__(self, device, backend) -> None:
         super().__init__(device, backend)
 
@@ -10,6 +10,6 @@ def create_model(self):
         if self.backend == 'pt':
             model = torchvision_models.vgg16_bn(pretrained=False, num_classes=565)
             path_to_weights = 'https://osf.io/fe7s5/download'
-            state_dict = torch.hub.load_state_dict_from_url(path_to_weights, map_location=self.device, file_name='VGG16_ecoset')
-            model.load_state_dict(state_dict)
+            state_dict = torch.hub.load_state_dict_from_url(path_to_weights, map_location=self.device, file_name='VGG16bn_ecoset')
+            model.load_state_dict(state_dict)  
             return model
\ No newline at end of file


Test patch:
diff --git a/tests/helper.py b/tests/helper.py
index 351f96e4..d1d22698 100644
--- a/tests/helper.py
+++ b/tests/helper.py
@@ -32,7 +32,7 @@
     'cornet_z': ['decoder.flatten'],
     'clip-ViT': ['visual'],
     'clip-RN': ['visual'],
-    'VGG16_ecoset': ['classifier.3']
+    'VGG16bn_ecoset': ['classifier.3']
 }
 
 TF_MODEL_AND_MODULES_NAMES = {
diff --git a/tests/model/test_model.py b/tests/model/test_model.py
index f1b66db9..0d51b9c8 100644
--- a/tests/model/test_model.py
+++ b/tests/model/test_model.py
@@ -41,7 +41,7 @@ def test_transformations_cnn(self):
         self.assertTrue(isinstance(transforms, tf.keras.Sequential))
 
     def test_load_custom_user_model(self):
-        model_name = 'VGG16_ecoset'
+        model_name = 'VGG16bn_ecoset'
         model = Model(model_name, True, 'cpu')
         self.assertTrue(model.__class__.__name__, 'vgg')