{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "2683899d", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from scipy.misc import face\n", "from scipy.ndimage import zoom\n", "from scipy.special import logsumexp\n", "import torch\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": null, "id": "9ab97d0d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "a9c8a9e7", "metadata": {}, "outputs": [], "source": [ "import scipy.io" ] }, { "cell_type": "code", "execution_count": null, "id": "32bd8589", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import cv2\n", "import os\n", "\n", "def load_images_from_folder(folder):\n", " images = []\n", " img_name = []\n", " for filename in os.listdir(folder):\n", " img = cv2.imread(os.path.join(folder,filename))\n", " if img is not None:\n", " images.append(img)\n", " img_name.append(filename)\n", " return images, img_name" ] }, { "cell_type": "code", "execution_count": null, "id": "c5ebf6a1", "metadata": {}, "outputs": [], "source": [ "imgs, img_name = load_images_from_folder('fix_stimuli')" ] }, { "cell_type": "code", "execution_count": null, "id": "571c8db2", "metadata": {}, "outputs": [], "source": [ "img_name" ] }, { "cell_type": "code", "execution_count": null, "id": "cd911d2d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "e99e7121", "metadata": {}, "outputs": [], "source": [ "len(img_name)" ] }, { "cell_type": "code", "execution_count": null, "id": "6e65c80b", "metadata": { "scrolled": false }, "outputs": [], "source": [ "int(img_name[0].split('.')[0])" ] }, { "cell_type": "code", "execution_count": null, "id": "303e82af", "metadata": { "scrolled": false }, "outputs": [], "source": [ "scipy.io.loadmat('S02_fix/S02_face_1.mat')['currImData'][:,4]" ] }, { "cell_type": "code", "execution_count": null, "id": "f5b41345", "metadata": {}, "outputs": [], "source": [ "scipy.io.loadmat('S02_fix/S02_face_1.mat')['currImData'][:,5]" ] }, { "cell_type": "code", "execution_count": null, "id": "e60e197d", "metadata": { "scrolled": true }, "outputs": [], "source": [ "scipy.io.loadmat('S02_fix/S02_pareidolia_64.mat')" ] }, { "cell_type": "code", "execution_count": null, "id": "a071e2e4", "metadata": {}, "outputs": [], "source": [ "for filename in os.listdir('S02_fix'):\n", " print(filename)" ] }, { "cell_type": "code", "execution_count": null, "id": "0dc9ab34", "metadata": {}, "outputs": [], "source": [ "def load_fix_from_folder(folder):\n", " fix_X = []\n", " fix_Y = []\n", " img_name = []\n", " for filename in os.listdir(folder):\n", " fix_X.append(scipy.io.loadmat(os.path.join(folder,filename))['currImData'][:,4])\n", " fix_Y.append(scipy.io.loadmat(os.path.join(folder,filename))['currImData'][:,5])\n", " img_name.append(str(scipy.io.loadmat(os.path.join(folder,filename))['currImName'][0][0]) + '.jpg')\n", " #print(filename)\n", " #print(img_name)\n", " return fix_X, fix_Y, img_name" ] }, { "cell_type": "code", "execution_count": null, "id": "5ad3e153", "metadata": {}, "outputs": [], "source": [ "fix_X, fix_Y, img_name = load_fix_from_folder('S_fix/S13_fix')" ] }, { "cell_type": "code", "execution_count": null, "id": "43fc95a0", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import glob\n", "import os\n", "\n", "# Specify the directory containing the nested folder structure\n", "root_dir = '/home/pranjul/DeepGaze/fix_stimuli/'\n", "\n", "# Specify the image file extensions you want to load\n", "extensions = ['*.jpg', '*.jpeg', '*.png']\n", "\n", "# Create a list to store the image file paths\n", "image_paths = []\n", "\n", "# Traverse through all subdirectories and search for image files\n", "for extension in extensions:\n", " search_pattern = os.path.join(root_dir, '**', extension)\n", " image_paths.extend(glob.glob(search_pattern, recursive=True))\n", "\n", "# Print the paths of the loaded image files\n", "for image_path in image_paths:\n", " print(image_path)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "864cb318", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "def create_folder(folder_path):\n", " try:\n", " os.mkdir(folder_path)\n", " print(f\"Folder '{folder_path}' created successfully.\")\n", " except FileExistsError:\n", " print(f\"Folder '{folder_path}' already exists.\")\n", " except Exception as e:\n", " print(f\"An error occurred: {e}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "47b06581", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "def folder_exists(folder_path):\n", " return os.path.exists(folder_path) and os.path.isdir(folder_path)\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1d22fce3", "metadata": {}, "outputs": [], "source": [ "# Replace 'path/to/your/folder' with the folder path you want to check\n", "folder_path = 'S_fix/S18_fix'\n", "if folder_exists(folder_path):\n", " print(f\"Folder '{folder_path}' exists.\")\n", "else:\n", " print(f\"Folder '{folder_path}' does not exist.\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "031c09c0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "30f2ed42", "metadata": { "scrolled": true }, "outputs": [], "source": [ "for f in range(13, 56):\n", " print(f)\n", " print('S_fix/S'+ str(f) +'_fix')" ] }, { "cell_type": "code", "execution_count": null, "id": "d5f1efd1", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from scipy.misc import face\n", "from scipy.ndimage import zoom\n", "from scipy.special import logsumexp\n", "import torch\n", "\n", "import deepgaze_pytorch\n", "\n", "DEVICE = 'cuda'\n", "\n", "# you can use DeepGazeI or DeepGazeIIE\n", "model = deepgaze_pytorch.DeepGazeIII(pretrained=True).to(DEVICE)\n", "\n", "#image = face()\n", "\n", "x = {}\n", "for q in range(1, 10):\n", " \n", " # Replace 'path/to/your/folder' with the folder path you want to check\n", " folder_path = 'S_fix/S0'+ str(q) +'_fix'\n", " if folder_exists(folder_path):\n", " \n", " fix_X, fix_Y, img_name = load_fix_from_folder('S_fix/S0'+ str(q) +'_fix')\n", "\n", " # Replace 'path/to/your/folder' with the desired folder path\n", " folder_path = 'DG3_heatmaps/S0'+ str(q) +'_fix'\n", " create_folder(folder_path)\n", "\n", "\n", " for i in range(len(img_name)):\n", "\n", " image = cv2.imread('/home/pranjul/DeepGaze/fix_stimuli/' + img_name[i])\n", "\n", " if image is not None and len(fix_X[i]) > 3 and len(fix_Y[i] > 3):\n", "\n", " # location of previous scanpath fixations in x and y (pixel coordinates), starting with the initial fixation on the image.\n", " #fixation_history_x = np.array([1024//2, 300, 500, 200, 200, 700])\n", " #fixation_history_y = np.array([768//2, 300, 100, 300, 100, 500])\n", "\n", " #print(img_name[i])\n", "\n", " fixation_history_x = fix_X[i]/3\n", " #print(fixation_history_x)\n", " fixation_history_y = fix_Y[i]/3\n", "\n", " # load precomputed centerbias log density (from MIT1003) over a 1024x1024 image\n", " # you can download the centerbias from https://github.com/matthias-k/DeepGaze/releases/download/v1.0.0/centerbias_mit1003.npy\n", " # alternatively, you can use a uniform centerbias via `centerbias_template = np.zeros((1024, 1024))`.\n", " centerbias_template = np.load('centerbias_mit1003.npy')\n", " # rescale to match image size\n", " centerbias = zoom(centerbias_template, (image.shape[0]/centerbias_template.shape[0], image.shape[1]/centerbias_template.shape[1]), order=0, mode='nearest')\n", " # renormalize log density\n", " centerbias -= logsumexp(centerbias)\n", "\n", " image_tensor = torch.tensor([image.transpose(2, 0, 1)]).to(DEVICE)\n", " centerbias_tensor = torch.tensor([centerbias]).to(DEVICE)\n", " x_hist_tensor = torch.tensor([fixation_history_x[model.included_fixations]]).to(DEVICE)\n", " y_hist_tensor = torch.tensor([fixation_history_y[model.included_fixations]]).to(DEVICE)\n", "\n", " log_density_prediction = model(image_tensor, centerbias_tensor, x_hist_tensor, y_hist_tensor)\n", "\n", " # Scale factor\n", " #scale_factor = 3\n", "\n", " # Calculate the new width and height\n", " #new_width = image.shape[1] * scale_factor\n", " #new_height = image.shape[0] * scale_factor\n", "\n", " # Resize the image using cv2.resize()\n", " #image = cv2.resize(image, (new_width, new_height))\n", "\n", " image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n", "\n", "\n", " f, axs = plt.subplots(nrows=1, ncols=2, figsize=(16, 9))\n", " axs[0].imshow(image)\n", " #axs[0].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " #axs[0].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='yellow', zorder=100)\n", " axs[0].set_axis_off()\n", " axs[1].matshow(log_density_prediction.detach().cpu().numpy()[0, 0]) # first image in batch, first (and only) channel\n", " #axs[1].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " #axs[1].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='yellow', zorder=100)\n", " axs[1].set_axis_off()\n", " plt.savefig(os.path.join('DG3_heatmaps/S0'+ str(q) +'_fix', img_name[i]))\n", " plt.close()\n", " #break\n", " #break\n", " #break" ] }, { "cell_type": "code", "execution_count": null, "id": "bb2809f0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "4a041477", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "a0baeecb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "70e54f03", "metadata": {}, "outputs": [], "source": [ "len(fixation_history_y)" ] }, { "cell_type": "code", "execution_count": null, "id": "16ec0624", "metadata": {}, "outputs": [], "source": [ "i" ] }, { "cell_type": "code", "execution_count": null, "id": "964e517a", "metadata": {}, "outputs": [], "source": [ "S02_img_name[244]" ] }, { "cell_type": "code", "execution_count": null, "id": "67397109", "metadata": {}, "outputs": [], "source": [ "np.where(np.array(S02_img_name) == '44.jpg')" ] }, { "cell_type": "code", "execution_count": null, "id": "02cd8a6b", "metadata": {}, "outputs": [], "source": [ "indices = np.where(arr == 2)[0]" ] }, { "cell_type": "code", "execution_count": null, "id": "a64314eb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "09b449d7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "f2e3afae", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "f8433595", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "11bb0a30", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "852d1d54", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "45392af9", "metadata": {}, "outputs": [], "source": [ "img.shape" ] }, { "cell_type": "code", "execution_count": null, "id": "93a09086", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import numpy as np\n", "from scipy.misc import face\n", "from scipy.ndimage import zoom\n", "from scipy.special import logsumexp\n", "import torch\n", "import matplotlib.pyplot as plt\n", "\n", "import deepgaze_pytorch\n", "\n", "DEVICE = 'cuda'\n", "\n", "# you can use DeepGazeI or DeepGazeIIE\n", "model = deepgaze_pytorch.DeepGazeIIE(pretrained=True).to(DEVICE)\n", "\n", "# image = face()\n", "\n", "x = {}\n", "\n", "for i in range(len(image_paths)):\n", " print(i)\n", " \n", " image = cv2.imread(image_paths[i])\n", " \n", " # load precomputed centerbias log density (from MIT1003) over a 1024x1024 image\n", " # you can download the centerbias from https://github.com/matthias-k/DeepGaze/releases/download/v1.0.0/centerbias_mit1003.npy\n", " # alternatively, you can use a uniform centerbias via `centerbias_template = np.zeros((1024, 1024))`.\n", " centerbias_template = np.load('centerbias_mit1003.npy')\n", " # rescale to match image size\n", " centerbias = zoom(centerbias_template, (image.shape[0]/centerbias_template.shape[0], image.shape[1]/centerbias_template.shape[1]), order=0, mode='nearest')\n", " # renormalize log density\n", " centerbias -= logsumexp(centerbias)\n", "\n", " image_tensor = torch.tensor([image.transpose(2, 0, 1)]).to(DEVICE)\n", " centerbias_tensor = torch.tensor([centerbias]).to(DEVICE)\n", "\n", " log_density_prediction = model(image_tensor, centerbias_tensor)\n", " \n", " #a = log_density_prediction.detach().cpu().numpy()[0, 0]\n", " \n", " #x[img_name[i].split('.')[0]] = a\n", " \n", " \n", " f, axs = plt.subplots(nrows=1, ncols=2, figsize=(16, 9))\n", " axs[0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n", " # axs[0].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " # axs[0].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='yellow', zorder=100)\n", " axs[0].set_axis_off()\n", " axs[1].matshow(log_density_prediction.detach().cpu().numpy()[0, 0]) # first image in batch, first (and only) channel\n", " # axs[1].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " # axs[1].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='yellow', zorder=100)\n", " axs[1].set_axis_off()\n", " plt.savefig(os.path.join('DG2_modified_imgs_heatmaps', '{0}.jpg'.format(i)))\n", " \n", " \n", " #break" ] }, { "cell_type": "code", "execution_count": null, "id": "3e4e709a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "2bd1220a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "60141a51", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "d2f42e76", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import numpy as np\n", "from scipy.misc import face\n", "from scipy.ndimage import zoom\n", "from scipy.special import logsumexp\n", "import torch\n", "import matplotlib.pyplot as plt\n", "\n", "import deepgaze_pytorch\n", "\n", "DEVICE = 'cuda'\n", "\n", "# you can use DeepGazeI or DeepGazeIIE\n", "model = deepgaze_pytorch.DeepGazeIIE(pretrained=True).to(DEVICE)\n", "\n", "# image = face()\n", "\n", "x = {}\n", "\n", "for i in range(len(imgs)):\n", " \n", " image = imgs[i]\n", " \n", " # load precomputed centerbias log density (from MIT1003) over a 1024x1024 image\n", " # you can download the centerbias from https://github.com/matthias-k/DeepGaze/releases/download/v1.0.0/centerbias_mit1003.npy\n", " # alternatively, you can use a uniform centerbias via `centerbias_template = np.zeros((1024, 1024))`.\n", " centerbias_template = np.load('centerbias_mit1003.npy')\n", " # rescale to match image size\n", " centerbias = zoom(centerbias_template, (image.shape[0]/centerbias_template.shape[0], image.shape[1]/centerbias_template.shape[1]), order=0, mode='nearest')\n", " # renormalize log density\n", " centerbias -= logsumexp(centerbias)\n", "\n", " image_tensor = torch.tensor([image.transpose(2, 0, 1)]).to(DEVICE)\n", " centerbias_tensor = torch.tensor([centerbias]).to(DEVICE)\n", "\n", " log_density_prediction = model(image_tensor, centerbias_tensor)\n", " \n", " a = log_density_prediction.detach().cpu().numpy()[0, 0]\n", " \n", " x[img_name[i].split('.')[0]] = a\n", " \n", " '''\n", " f, axs = plt.subplots(nrows=1, ncols=2, figsize=(16, 9))\n", " axs[0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n", " # axs[0].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " # axs[0].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='yellow', zorder=100)\n", " axs[0].set_axis_off()\n", " axs[1].matshow(log_density_prediction.detach().cpu().numpy()[0, 0]) # first image in batch, first (and only) channel\n", " # axs[1].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " # axs[1].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='yellow', zorder=100)\n", " axs[1].set_axis_off()\n", " # plt.savefig(os.path.join('DG2_heatmaps', '{0}.jpg'.format(i)))\n", " '''\n", " \n", " #break" ] }, { "cell_type": "code", "execution_count": null, "id": "ca4ab04b", "metadata": {}, "outputs": [], "source": [ "for k in x:\n", " print(k)" ] }, { "cell_type": "code", "execution_count": null, "id": "26527272", "metadata": {}, "outputs": [], "source": [ "import glob\n", "from scipy.io import loadmat\n", "from scipy.stats import pearsonr, spearmanr\n", "from sklearn.preprocessing import MinMaxScaler\n", "\n", "scaler = MinMaxScaler()" ] }, { "cell_type": "code", "execution_count": null, "id": "3938f5cb", "metadata": {}, "outputs": [], "source": [ "\n", "y_faces = {}\n", "\n", "for filename in glob.glob('/home/pranjul/DeepGaze/heatmaps/faces/*.mat'): #assuming gif\n", " \n", " fn=loadmat(filename)\n", " y_faces[filename.split('/')[-1].split('.')[0]] = fn\n", " #break" ] }, { "cell_type": "code", "execution_count": null, "id": "c5902106", "metadata": {}, "outputs": [], "source": [ "\n", "y_objects = {}\n", "\n", "for filename in glob.glob('/home/pranjul/DeepGaze/heatmaps/objects/*.mat'): #assuming gif\n", " \n", " fn=loadmat(filename)\n", " y_objects[filename.split('/')[-1].split('.')[0]] = fn\n", " #break" ] }, { "cell_type": "code", "execution_count": null, "id": "e6fa7c47", "metadata": {}, "outputs": [], "source": [ "\n", "y_pareidolia = {}\n", "\n", "for filename in glob.glob('/home/pranjul/DeepGaze/heatmaps/pareidolia/*.mat'): #assuming gif\n", " \n", " fn=loadmat(filename)\n", " y_pareidolia[filename.split('/')[-1].split('.')[0]] = fn\n", " #break" ] }, { "cell_type": "code", "execution_count": null, "id": "a2166932", "metadata": { "scrolled": true }, "outputs": [], "source": [ "dg_faces = []\n", "eg_faces = []\n", "ke = []\n", "for k in x:\n", " if k in y_faces:\n", " print(k)\n", " ke.append(k)\n", " #print(np.shape(x[k]))\n", " #print(y_faces[k])\n", " dg_faces.append(scaler.fit_transform(np.array(x[k])).flatten())\n", " eg_faces.append(scaler.fit_transform(np.array(y_faces[k]['a'])).flatten())\n", " #break\n", "\n", "correlation_coef, p_value = spearmanr(np.array(dg_faces).flatten(), np.array(eg_faces).flatten())\n", "# correlation_coef = np.corrcoef(a, b)\n", "print(\"Correlation coefficient:\", correlation_coef)\n", "print(\"p-value:\", p_value)" ] }, { "cell_type": "code", "execution_count": null, "id": "77266844", "metadata": { "scrolled": true }, "outputs": [], "source": [ "dg_objects = []\n", "eg_objects = []\n", "ke = []\n", "for k in x:\n", " if k in y_objects:\n", " print(k)\n", " ke.append(k)\n", " #print(np.shape(x[k]))\n", " #print(y_faces[k])\n", " dg_objects.append(scaler.fit_transform(np.array(x[k])).flatten())\n", " eg_objects.append(scaler.fit_transform(np.array(y_objects[k]['a'])).flatten())\n", " #break\n", "\n", "correlation_coef, p_value = spearmanr(np.array(dg_objects).flatten(), np.array(eg_objects).flatten())\n", "# correlation_coef = np.corrcoef(a, b)\n", "print(\"Correlation coefficient:\", correlation_coef)\n", "print(\"p-value:\", p_value)" ] }, { "cell_type": "code", "execution_count": null, "id": "0403de67", "metadata": { "scrolled": true }, "outputs": [], "source": [ "dg_pareidolia = []\n", "eg_pareidolia = []\n", "ke = []\n", "for k in x:\n", " if k in y_pareidolia:\n", " print(k)\n", " ke.append(k)\n", " #print(np.shape(x[k]))\n", " #print(y_faces[k])\n", " dg_pareidolia.append(scaler.fit_transform(np.array(x[k])).flatten())\n", " eg_pareidolia.append(scaler.fit_transform(np.array(y_pareidolia[k]['a'])).flatten())\n", " #break\n", "\n", "correlation_coef, p_value = spearmanr(np.array(dg_pareidolia).flatten(), np.array(eg_pareidolia).flatten())\n", "# correlation_coef = np.corrcoef(a, b)\n", "print(\"Correlation coefficient:\", correlation_coef)\n", "print(\"p-value:\", p_value)" ] }, { "cell_type": "code", "execution_count": null, "id": "c297d11a", "metadata": {}, "outputs": [], "source": [ "correlation_coef, p_value = spearmanr(np.array(dg_faces).flatten(), np.array(eg_faces).flatten())\n", "# correlation_coef = np.corrcoef(a, b)\n", "print(\"Correlation coefficient:\", correlation_coef)\n", "print(\"p-value:\", p_value)" ] }, { "cell_type": "code", "execution_count": null, "id": "85e5edb2", "metadata": {}, "outputs": [], "source": [ "correlation_coef, p_value = spearmanr(np.array(dg_objects).flatten(), np.array(eg_objects).flatten())\n", "# correlation_coef = np.corrcoef(a, b)\n", "print(\"Correlation coefficient:\", correlation_coef)\n", "print(\"p-value:\", p_value)" ] }, { "cell_type": "code", "execution_count": null, "id": "d99c7309", "metadata": {}, "outputs": [], "source": [ "correlation_coef, p_value = spearmanr(np.array(dg_pareidolia).flatten(), np.array(eg_pareidolia).flatten())\n", "# correlation_coef = np.corrcoef(a, b)\n", "print(\"Correlation coefficient:\", correlation_coef)\n", "print(\"p-value:\", p_value)" ] }, { "cell_type": "code", "execution_count": null, "id": "e9702314", "metadata": {}, "outputs": [], "source": [ "len(dg_pareidolia)" ] }, { "cell_type": "code", "execution_count": null, "id": "c319f00b", "metadata": {}, "outputs": [], "source": [ "len(dg_faces[:83])" ] }, { "cell_type": "code", "execution_count": null, "id": "1fa45e93", "metadata": {}, "outputs": [], "source": [ "len(dg_objects[:83])" ] }, { "cell_type": "code", "execution_count": null, "id": "5e9fd2b6", "metadata": {}, "outputs": [], "source": [ "correlation_coef, p_value = spearmanr(np.array(dg_faces[:83]).flatten(), np.array(dg_objects[:83]).flatten())\n", "# correlation_coef = np.corrcoef(a, b)\n", "print(\"Correlation coefficient:\", correlation_coef)\n", "print(\"p-value:\", p_value)" ] }, { "cell_type": "code", "execution_count": null, "id": "a9357f4f", "metadata": {}, "outputs": [], "source": [ "correlation_coef, p_value = spearmanr(np.array(dg_faces[:83]).flatten(), np.array(dg_pareidolia[:83]).flatten())\n", "# correlation_coef = np.corrcoef(a, b)\n", "print(\"Correlation coefficient:\", correlation_coef)\n", "print(\"p-value:\", p_value)" ] }, { "cell_type": "code", "execution_count": null, "id": "f70021f3", "metadata": {}, "outputs": [], "source": [ "correlation_coef, p_value = spearmanr(np.array(dg_pareidolia[:83]).flatten(), np.array(dg_objects[:83]).flatten())\n", "# correlation_coef = np.corrcoef(a, b)\n", "print(\"Correlation coefficient:\", correlation_coef)\n", "print(\"p-value:\", p_value)" ] }, { "cell_type": "code", "execution_count": null, "id": "0df004a4", "metadata": {}, "outputs": [], "source": [ "correlation_coef, p_value = spearmanr(np.array(eg_pareidolia[:83]).flatten(), np.array(eg_objects[:83]).flatten())\n", "# correlation_coef = np.corrcoef(a, b)\n", "print(\"Correlation coefficient:\", correlation_coef)\n", "print(\"p-value:\", p_value)" ] }, { "cell_type": "code", "execution_count": null, "id": "1742ecb1", "metadata": {}, "outputs": [], "source": [ "correlation_coef, p_value = spearmanr(np.array(eg_pareidolia[:83]).flatten(), np.array(eg_faces[:83]).flatten())\n", "# correlation_coef = np.corrcoef(a, b)\n", "print(\"Correlation coefficient:\", correlation_coef)\n", "print(\"p-value:\", p_value)" ] }, { "cell_type": "code", "execution_count": null, "id": "0a02bf88", "metadata": {}, "outputs": [], "source": [ "correlation_coef, p_value = spearmanr(np.array(eg_faces[:83]).flatten(), np.array(eg_objects[:83]).flatten())\n", "# correlation_coef = np.corrcoef(a, b)\n", "print(\"Correlation coefficient:\", correlation_coef)\n", "print(\"p-value:\", p_value)" ] }, { "cell_type": "code", "execution_count": null, "id": "b7ccd13e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "fdeb2fc5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "7faa17c1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "f9af3d41", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from scipy.stats import spearmanr\n", "\n", "# Generate two arrays with random data\n", "array1 = np.random.rand(100)\n", "array2 = np.random.rand(100)\n", "\n", "# Calculate Spearman's correlation coefficient and p-value\n", "correlation, p_value = spearmanr(array1, array2)\n", "\n", "print(\"Spearman's correlation coefficient:\", correlation)\n", "print(\"p-value:\", p_value)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "f7cd0d61", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "3570f454", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "3a0a92be", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from scipy.stats import pearsonr\n", "\n", "# define two eye gaze heatmaps\n", "heatmap1 = np.array([[0.2, 0.3, 0.1],\n", " [0.1, 0.4, 0.3],\n", " [0.3, 0.2, 0.1]])\n", "\n", "heatmap2 = np.array([[0.1, 0.2, 0.3],\n", " [0.2, 0.3, 0.2],\n", " [0.3, 0.1, 0.1]])\n", "\n", "# flatten the heatmaps into 1D arrays\n", "flat_heatmap1 = heatmap1.flatten()\n", "flat_heatmap2 = heatmap2.flatten()\n", "\n", "# calculate the Pearson correlation coefficient and p-value\n", "corr, p_value = pearsonr(flat_heatmap1, flat_heatmap2)\n", "\n", "print(\"Correlation coefficient:\", corr)\n", "print(\"p-value:\", p_value)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "98a8e3c1", "metadata": {}, "outputs": [], "source": [ "np.shape(b)" ] }, { "cell_type": "code", "execution_count": null, "id": "55b352bd", "metadata": {}, "outputs": [], "source": [ "np.shape(a)" ] }, { "cell_type": "code", "execution_count": null, "id": "3fe648aa", "metadata": {}, "outputs": [], "source": [ "np.shape(correlation_coef)" ] }, { "cell_type": "code", "execution_count": null, "id": "cd8e091b", "metadata": {}, "outputs": [], "source": [ "plt.imshow(correlation_coef)" ] }, { "cell_type": "code", "execution_count": null, "id": "884bf73a", "metadata": {}, "outputs": [], "source": [ "correlation_coef[83:, :83]" ] }, { "cell_type": "code", "execution_count": null, "id": "4a540fa9", "metadata": { "scrolled": true }, "outputs": [], "source": [ "plt.imshow(correlation_coef[83:, :83])" ] }, { "cell_type": "code", "execution_count": null, "id": "62cadea8", "metadata": {}, "outputs": [], "source": [ "plt.plot(np.diagonal(correlation_coef[100:, :100]), 'o') #faces" ] }, { "cell_type": "code", "execution_count": null, "id": "f05bd895", "metadata": {}, "outputs": [], "source": [ "np.mean(np.diagonal(correlation_coef[100:, :100]))" ] }, { "cell_type": "code", "execution_count": null, "id": "6e227007", "metadata": {}, "outputs": [], "source": [ "plt.plot(np.diagonal(correlation_coef[100:, :100]), 'o') #obj" ] }, { "cell_type": "code", "execution_count": null, "id": "12ed25e7", "metadata": {}, "outputs": [], "source": [ "np.mean(np.diagonal(correlation_coef[86:, :86]))" ] }, { "cell_type": "code", "execution_count": null, "id": "ccf0a569", "metadata": {}, "outputs": [], "source": [ "plt.plot(np.diagonal(correlation_coef[83:, :83]), 'o') #pare" ] }, { "cell_type": "code", "execution_count": null, "id": "923ed911", "metadata": {}, "outputs": [], "source": [ "np.mean(np.diagonal(correlation_coef[83:, :83]))" ] }, { "cell_type": "code", "execution_count": null, "id": "27bac165", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "f751bc29", "metadata": {}, "outputs": [], "source": [ "plt.imshow(y_objects['1153']['a'])" ] }, { "cell_type": "code", "execution_count": null, "id": "0c6a4bb4", "metadata": {}, "outputs": [], "source": [ "y_faces" ] }, { "cell_type": "code", "execution_count": null, "id": "40a93053", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "4a9b5849", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "76eee42b", "metadata": {}, "outputs": [], "source": [ "np.shape(imgs)" ] }, { "cell_type": "code", "execution_count": null, "id": "1cc44e0e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "1d14b8ad", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "feddeb52", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from scipy.misc import face\n", "from scipy.ndimage import zoom\n", "from scipy.special import logsumexp\n", "import torch\n", "\n", "import deepgaze_pytorch\n", "\n", "DEVICE = 'cuda'\n", "\n", "# you can use DeepGazeI or DeepGazeIIE\n", "model = deepgaze_pytorch.DeepGazeIII(pretrained=True).to(DEVICE)\n", "\n", "image = face()\n", "\n", "# location of previous scanpath fixations in x and y (pixel coordinates), starting with the initial fixation on the image.\n", "fixation_history_x = np.array([1024//2, 300, 500, 200, 200, 700])\n", "fixation_history_y = np.array([768//2, 300, 100, 300, 100, 500])\n", "\n", "# load precomputed centerbias log density (from MIT1003) over a 1024x1024 image\n", "# you can download the centerbias from https://github.com/matthias-k/DeepGaze/releases/download/v1.0.0/centerbias_mit1003.npy\n", "# alternatively, you can use a uniform centerbias via `centerbias_template = np.zeros((1024, 1024))`.\n", "centerbias_template = np.load('centerbias_mit1003.npy')\n", "# rescale to match image size\n", "centerbias = zoom(centerbias_template, (image.shape[0]/centerbias_template.shape[0], image.shape[1]/centerbias_template.shape[1]), order=0, mode='nearest')\n", "# renormalize log density\n", "centerbias -= logsumexp(centerbias)\n", "\n", "image_tensor = torch.tensor([image.transpose(2, 0, 1)]).to(DEVICE)\n", "centerbias_tensor = torch.tensor([centerbias]).to(DEVICE)\n", "x_hist_tensor = torch.tensor([fixation_history_x[model.included_fixations]]).to(DEVICE)\n", "y_hist_tensor = torch.tensor([fixation_history_x[model.included_fixations]]).to(DEVICE)\n", "\n", "log_density_prediction = model(image_tensor, centerbias_tensor, x_hist_tensor, y_hist_tensor)\n", "\n", "f, axs = plt.subplots(nrows=1, ncols=2, figsize=(8, 3))\n", "axs[0].imshow(image)\n", "axs[0].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", "axs[0].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='yellow', zorder=100)\n", "axs[0].set_axis_off()\n", "axs[1].matshow(log_density_prediction.detach().cpu().numpy()[0, 0]) # first image in batch, first (and only) channel\n", "axs[1].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", "axs[1].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='yellow', zorder=100)\n", "axs[1].set_axis_off()" ] }, { "cell_type": "code", "execution_count": null, "id": "2b512963", "metadata": {}, "outputs": [], "source": [ "model.included_fixations" ] }, { "cell_type": "code", "execution_count": null, "id": "33d6872d", "metadata": {}, "outputs": [], "source": [ "fixation_history_x" ] }, { "cell_type": "code", "execution_count": null, "id": "8bce1d25", "metadata": {}, "outputs": [], "source": [ "fixation_history_x[model.included_fixations]" ] }, { "cell_type": "code", "execution_count": null, "id": "751cb04e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "b3160caa", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from scipy.misc import face\n", "from scipy.ndimage import zoom\n", "from scipy.special import logsumexp\n", "import torch\n", "\n", "import deepgaze_pytorch\n", "\n", "DEVICE = 'cuda'\n", "\n", "# you can use DeepGazeI or DeepGazeIIE\n", "model = deepgaze_pytorch.DeepGazeIII(pretrained=True).to(DEVICE)\n", "\n", "#image = face()\n", "\n", "x = {}\n", "\n", "for i in range(len(imgs)):\n", " \n", " image = imgs[i]\n", " \n", " # location of previous scanpath fixations in x and y (pixel coordinates), starting with the initial fixation on the image.\n", " fixation_history_x = np.array([1024//2, 300, 500, 200, 200, 700])\n", " fixation_history_y = np.array([768//2, 300, 100, 300, 100, 500])\n", "\n", " # load precomputed centerbias log density (from MIT1003) over a 1024x1024 image\n", " # you can download the centerbias from https://github.com/matthias-k/DeepGaze/releases/download/v1.0.0/centerbias_mit1003.npy\n", " # alternatively, you can use a uniform centerbias via `centerbias_template = np.zeros((1024, 1024))`.\n", " centerbias_template = np.load('centerbias_mit1003.npy')\n", " # rescale to match image size\n", " centerbias = zoom(centerbias_template, (image.shape[0]/centerbias_template.shape[0], image.shape[1]/centerbias_template.shape[1]), order=0, mode='nearest')\n", " # renormalize log density\n", " centerbias -= logsumexp(centerbias)\n", "\n", " image_tensor = torch.tensor([image.transpose(2, 0, 1)]).to(DEVICE)\n", " centerbias_tensor = torch.tensor([centerbias]).to(DEVICE)\n", " x_hist_tensor = torch.tensor([fixation_history_x[model.included_fixations]]).to(DEVICE)\n", " y_hist_tensor = torch.tensor([fixation_history_x[model.included_fixations]]).to(DEVICE)\n", "\n", " log_density_prediction = model(image_tensor, centerbias_tensor, x_hist_tensor, y_hist_tensor)\n", "\n", " f, axs = plt.subplots(nrows=1, ncols=2, figsize=(8, 3))\n", " axs[0].imshow(image)\n", " axs[0].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " axs[0].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='yellow', zorder=100)\n", " axs[0].set_axis_off()\n", " axs[1].matshow(log_density_prediction.detach().cpu().numpy()[0, 0]) # first image in batch, first (and only) channel\n", " axs[1].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " axs[1].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='yellow', zorder=100)\n", " axs[1].set_axis_off()" ] }, { "cell_type": "code", "execution_count": null, "id": "aa2d7d4e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "274b461a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "f71d7915", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "6c4adce6", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from scipy.misc import face\n", "from scipy.ndimage import zoom\n", "from scipy.special import logsumexp\n", "import torch\n", "import matplotlib.pyplot as plt\n", "\n", "import deepgaze_pytorch\n", "\n", "DEVICE = 'cuda'\n", "\n", "# you can use DeepGazeI or DeepGazeIIE\n", "model = deepgaze_pytorch.DeepGazeIIE(pretrained=True).to(DEVICE)\n", "\n", "# image = face()\n", "\n", "x = {}\n", "\n", "for i in range(len(imgs)):\n", " \n", " image = imgs[i]\n", " \n", " # load precomputed centerbias log density (from MIT1003) over a 1024x1024 image\n", " # you can download the centerbias from https://github.com/matthias-k/DeepGaze/releases/download/v1.0.0/centerbias_mit1003.npy\n", " # alternatively, you can use a uniform centerbias via `centerbias_template = np.zeros((1024, 1024))`.\n", " centerbias_template = np.load('centerbias_mit1003.npy')\n", " # rescale to match image size\n", " centerbias = zoom(centerbias_template, (image.shape[0]/centerbias_template.shape[0], image.shape[1]/centerbias_template.shape[1]), order=0, mode='nearest')\n", " # renormalize log density\n", " centerbias -= logsumexp(centerbias)\n", "\n", " image_tensor = torch.tensor([image.transpose(2, 0, 1)]).to(DEVICE)\n", " centerbias_tensor = torch.tensor([centerbias]).to(DEVICE)\n", "\n", " log_density_prediction = model(image_tensor, centerbias_tensor)\n", " \n", " a = log_density_prediction.detach().cpu().numpy()[0, 0]\n", " \n", " x[img_name[i].split('.')[0]] = a\n", " \n", " '''\n", " f, axs = plt.subplots(nrows=1, ncols=2, figsize=(16, 9))\n", " axs[0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n", " # axs[0].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " # axs[0].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='yellow', zorder=100)\n", " axs[0].set_axis_off()\n", " axs[1].matshow(log_density_prediction.detach().cpu().numpy()[0, 0]) # first image in batch, first (and only) channel\n", " # axs[1].plot(fixation_history_x, fixation_history_y, 'o-', color='red')\n", " # axs[1].scatter(fixation_history_x[-1], fixation_history_y[-1], 100, color='yellow', zorder=100)\n", " axs[1].set_axis_off()\n", " # plt.savefig(os.path.join('DG2_heatmaps', '{0}.jpg'.format(i)))\n", " '''\n", " \n", " #break" ] }, { "cell_type": "code", "execution_count": null, "id": "d69ce384", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "14d2b609", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "80a068d8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "ceddabde", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "c8207585", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "984c0e9c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "7196ebb9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "dc722528", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "20dd8746", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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": 5 }