|
25 | 25 | "source": [ |
26 | 26 | "# Install senselab and dependencies\n", |
27 | 27 | "!pip install -q uv\n", |
28 | | - "!uv pip install --pre --system \"senselab[nlp,text,video]\"" |
| 28 | + "!uv pip install --pre --system \"senselab\"" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "markdown", |
| 33 | + "metadata": {}, |
| 34 | + "source": [ |
| 35 | + "> **\u26a0\ufe0f Restart runtime after install**\n", |
| 36 | + ">\n", |
| 37 | + "> The install may upgrade packages (e.g., numpy) that are already loaded.\n", |
| 38 | + "> Go to **Runtime \u2192 Restart session**, then **run all cells below** (skip this install cell).\n" |
29 | 39 | ] |
30 | 40 | }, |
31 | 41 | { |
|
115 | 125 | "metadata": {}, |
116 | 126 | "outputs": [], |
117 | 127 | "source": [ |
118 | | - "from senselab.utils.tasks.cosine_similarity import compute_cosine_similarity\n\n\n# DIRECTLY PLOT THE EMBEDDINGS FOR THE TWO FILES\ndef plot_embedding_heatmap(embeddings: List[torch.Tensor], titles: List[str]) -> None:\n \"\"\"Plot a heatmap of a list of speaker embeddings.\"\"\"\n fig, axes = plt.subplots(len(embeddings), 1, figsize=(10, 5 * len(embeddings)))\n if len(embeddings) == 1:\n axes = [axes]\n\n for ax, embedding, title in zip(axes, embeddings, titles):\n im = ax.imshow(embedding.cpu().unsqueeze(0), aspect=\"auto\", cmap=\"viridis\")\n ax.set_title(f\"Speaker Embedding: {title}\")\n ax.set_xlabel(\"Embedding Dimension\")\n fig.colorbar(im, ax=ax)\n\n plt.tight_layout()\n plt.show()\n\n\nplot_embedding_heatmap(embeddings, [\"file 1\", \"file 2\"])\n\n\n# PLOT THE COSINE SIMILARITY MATRIX FOR THE TWO FILES\ndef plot_similarity_matrix(embeddings: List[torch.Tensor], labels: List[str]) -> None:\n \"\"\"Plot a similarity matrix for a list of embeddings.\"\"\"\n n = len(embeddings)\n similarity_matrix = np.zeros((n, n))\n\n for i in range(n):\n for j in range(n):\n similarity_matrix[i, j] = compute_cosine_similarity(embeddings[i], embeddings[j])\n\n fig, ax = plt.subplots(figsize=(8, 6))\n im = ax.imshow(similarity_matrix, cmap=\"coolwarm\", vmin=-1, vmax=1)\n\n ax.set_xticks(np.arange(n))\n ax.set_yticks(np.arange(n))\n ax.set_xticklabels(labels)\n ax.set_yticklabels(labels)\n\n plt.setp(ax.get_xticklabels(), rotation=45, ha=\"right\", rotation_mode=\"anchor\")\n\n for i in range(n):\n for j in range(n):\n ax.text(j, i, f\"{similarity_matrix[i, j]:.2f}\", ha=\"center\", va=\"center\", color=\"black\")\n\n ax.set_title(\"Cosine Similarity Between Speaker Embeddings\")\n fig.colorbar(im)\n plt.tight_layout()\n plt.show()\n\n\nplot_similarity_matrix(embeddings, [\"file 1\", \"file 2\"])" |
| 128 | + "from senselab.utils.tasks.cosine_similarity import compute_cosine_similarity\n", |
| 129 | + "\n", |
| 130 | + "\n", |
| 131 | + "# DIRECTLY PLOT THE EMBEDDINGS FOR THE TWO FILES\n", |
| 132 | + "def plot_embedding_heatmap(embeddings: List[torch.Tensor], titles: List[str]) -> None:\n", |
| 133 | + " \"\"\"Plot a heatmap of a list of speaker embeddings.\"\"\"\n", |
| 134 | + " fig, axes = plt.subplots(len(embeddings), 1, figsize=(10, 5 * len(embeddings)))\n", |
| 135 | + " if len(embeddings) == 1:\n", |
| 136 | + " axes = [axes]\n", |
| 137 | + "\n", |
| 138 | + " for ax, embedding, title in zip(axes, embeddings, titles):\n", |
| 139 | + " im = ax.imshow(embedding.cpu().unsqueeze(0), aspect=\"auto\", cmap=\"viridis\")\n", |
| 140 | + " ax.set_title(f\"Speaker Embedding: {title}\")\n", |
| 141 | + " ax.set_xlabel(\"Embedding Dimension\")\n", |
| 142 | + " fig.colorbar(im, ax=ax)\n", |
| 143 | + "\n", |
| 144 | + " plt.tight_layout()\n", |
| 145 | + " plt.show()\n", |
| 146 | + "\n", |
| 147 | + "\n", |
| 148 | + "plot_embedding_heatmap(embeddings, [\"file 1\", \"file 2\"])\n", |
| 149 | + "\n", |
| 150 | + "\n", |
| 151 | + "# PLOT THE COSINE SIMILARITY MATRIX FOR THE TWO FILES\n", |
| 152 | + "def plot_similarity_matrix(embeddings: List[torch.Tensor], labels: List[str]) -> None:\n", |
| 153 | + " \"\"\"Plot a similarity matrix for a list of embeddings.\"\"\"\n", |
| 154 | + " n = len(embeddings)\n", |
| 155 | + " similarity_matrix = np.zeros((n, n))\n", |
| 156 | + "\n", |
| 157 | + " for i in range(n):\n", |
| 158 | + " for j in range(n):\n", |
| 159 | + " similarity_matrix[i, j] = compute_cosine_similarity(embeddings[i], embeddings[j])\n", |
| 160 | + "\n", |
| 161 | + " fig, ax = plt.subplots(figsize=(8, 6))\n", |
| 162 | + " im = ax.imshow(similarity_matrix, cmap=\"coolwarm\", vmin=-1, vmax=1)\n", |
| 163 | + "\n", |
| 164 | + " ax.set_xticks(np.arange(n))\n", |
| 165 | + " ax.set_yticks(np.arange(n))\n", |
| 166 | + " ax.set_xticklabels(labels)\n", |
| 167 | + " ax.set_yticklabels(labels)\n", |
| 168 | + "\n", |
| 169 | + " plt.setp(ax.get_xticklabels(), rotation=45, ha=\"right\", rotation_mode=\"anchor\")\n", |
| 170 | + "\n", |
| 171 | + " for i in range(n):\n", |
| 172 | + " for j in range(n):\n", |
| 173 | + " ax.text(j, i, f\"{similarity_matrix[i, j]:.2f}\", ha=\"center\", va=\"center\", color=\"black\")\n", |
| 174 | + "\n", |
| 175 | + " ax.set_title(\"Cosine Similarity Between Speaker Embeddings\")\n", |
| 176 | + " fig.colorbar(im)\n", |
| 177 | + " plt.tight_layout()\n", |
| 178 | + " plt.show()\n", |
| 179 | + "\n", |
| 180 | + "\n", |
| 181 | + "plot_similarity_matrix(embeddings, [\"file 1\", \"file 2\"])" |
119 | 182 | ] |
120 | 183 | }, |
121 | 184 | { |
|
0 commit comments