|
@@ -9,16 +9,22 @@
|
|
},
|
|
},
|
|
{
|
|
{
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
- "execution_count": 7,
|
|
|
|
|
|
+ "execution_count": 1,
|
|
"metadata": {},
|
|
"metadata": {},
|
|
"outputs": [
|
|
"outputs": [
|
|
|
|
+ {
|
|
|
|
+ "name": "stderr",
|
|
|
|
+ "output_type": "stream",
|
|
|
|
+ "text": [
|
|
|
|
+ "/home/kleinsteuber/anaconda3/envs/pytorch-gpu/lib/python3.10/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
|
|
|
+ " from .autonotebook import tqdm as notebook_tqdm\n"
|
|
|
|
+ ]
|
|
|
|
+ },
|
|
{
|
|
{
|
|
"name": "stdout",
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"text": [
|
|
- "The autoreload extension is already loaded. To reload it, use:\n",
|
|
|
|
- " %reload_ext autoreload\n",
|
|
|
|
- "Found 1 sessions\n",
|
|
|
|
|
|
+ "Found 2 sessions\n",
|
|
"Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
|
|
"Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
|
|
"Loaded scans.\n"
|
|
"Loaded scans.\n"
|
|
]
|
|
]
|
|
@@ -110,7 +116,7 @@
|
|
},
|
|
},
|
|
{
|
|
{
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
- "execution_count": 25,
|
|
|
|
|
|
+ "execution_count": 3,
|
|
"metadata": {},
|
|
"metadata": {},
|
|
"outputs": [
|
|
"outputs": [
|
|
{
|
|
{
|
|
@@ -132,7 +138,7 @@
|
|
},
|
|
},
|
|
{
|
|
{
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
- "execution_count": 26,
|
|
|
|
|
|
+ "execution_count": 3,
|
|
"metadata": {},
|
|
"metadata": {},
|
|
"outputs": [
|
|
"outputs": [
|
|
{
|
|
{
|
|
@@ -172,7 +178,7 @@
|
|
")"
|
|
")"
|
|
]
|
|
]
|
|
},
|
|
},
|
|
- "execution_count": 26,
|
|
|
|
|
|
+ "execution_count": 3,
|
|
"metadata": {},
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
"output_type": "execute_result"
|
|
}
|
|
}
|
|
@@ -185,7 +191,7 @@
|
|
},
|
|
},
|
|
{
|
|
{
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
- "execution_count": 27,
|
|
|
|
|
|
+ "execution_count": 6,
|
|
"metadata": {},
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"outputs": [],
|
|
"source": [
|
|
"source": [
|
|
@@ -198,7 +204,7 @@
|
|
},
|
|
},
|
|
{
|
|
{
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
- "execution_count": 28,
|
|
|
|
|
|
+ "execution_count": 7,
|
|
"metadata": {},
|
|
"metadata": {},
|
|
"outputs": [
|
|
"outputs": [
|
|
{
|
|
{
|
|
@@ -220,7 +226,7 @@
|
|
},
|
|
},
|
|
{
|
|
{
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
- "execution_count": 29,
|
|
|
|
|
|
+ "execution_count": 9,
|
|
"metadata": {},
|
|
"metadata": {},
|
|
"outputs": [
|
|
"outputs": [
|
|
{
|
|
{
|
|
@@ -250,23 +256,42 @@
|
|
},
|
|
},
|
|
{
|
|
{
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
- "execution_count": 30,
|
|
|
|
|
|
+ "execution_count": 16,
|
|
"metadata": {},
|
|
"metadata": {},
|
|
"outputs": [
|
|
"outputs": [
|
|
{
|
|
{
|
|
"name": "stderr",
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"text": [
|
|
- "100%|██████████| 22/22 [03:12<00:00, 8.77s/it]\n"
|
|
|
|
|
|
+ "100%|██████████| 22/22 [03:42<00:00, 10.09s/it]\n"
|
|
]
|
|
]
|
|
}
|
|
}
|
|
],
|
|
],
|
|
"source": [
|
|
"source": [
|
|
|
|
+ "from sklearn.neighbors import KernelDensity\n",
|
|
|
|
+ "\n",
|
|
|
|
+ "#Losses\n",
|
|
"criterion = nn.MSELoss()\n",
|
|
"criterion = nn.MSELoss()\n",
|
|
"normal_losses = []\n",
|
|
"normal_losses = []\n",
|
|
"anomalous_losses = []\n",
|
|
"anomalous_losses = []\n",
|
|
|
|
+ "\n",
|
|
|
|
+ "# KDE\n",
|
|
|
|
+ "kde = KernelDensity(kernel=\"gaussian\", bandwidth=0.2)\n",
|
|
|
|
+ "normal_encoded_imgs = []\n",
|
|
|
|
+ "anomalous_encoded_imgs = []\n",
|
|
|
|
+ "\n",
|
|
"for features, labels in tqdm(test_dl):\n",
|
|
"for features, labels in tqdm(test_dl):\n",
|
|
- " output_batch = model(features)\n",
|
|
|
|
|
|
+ " # Calculate and save encoded representation\n",
|
|
|
|
+ " encoded = model.encoder(features)\n",
|
|
|
|
+ " encoded_flat = encoded.detach().numpy().reshape(encoded.size()[0], -1)\n",
|
|
|
|
+ " for enc, label in zip(encoded_flat, labels):\n",
|
|
|
|
+ " if label == 1:\n",
|
|
|
|
+ " anomalous_encoded_imgs.append(enc)\n",
|
|
|
|
+ " else:\n",
|
|
|
|
+ " normal_encoded_imgs.append(enc)\n",
|
|
|
|
+ " \n",
|
|
|
|
+ " # decode and determine loss\n",
|
|
|
|
+ " output_batch = model.decoder(encoded)\n",
|
|
" for input, output, label in zip(features, output_batch, labels):\n",
|
|
" for input, output, label in zip(features, output_batch, labels):\n",
|
|
" loss = criterion(input, output)\n",
|
|
" loss = criterion(input, output)\n",
|
|
" if label == 1:\n",
|
|
" if label == 1:\n",
|
|
@@ -275,6 +300,53 @@
|
|
" normal_losses.append(loss.item())"
|
|
" normal_losses.append(loss.item())"
|
|
]
|
|
]
|
|
},
|
|
},
|
|
|
|
+ {
|
|
|
|
+ "cell_type": "code",
|
|
|
|
+ "execution_count": 17,
|
|
|
|
+ "metadata": {},
|
|
|
|
+ "outputs": [
|
|
|
|
+ {
|
|
|
|
+ "name": "stderr",
|
|
|
|
+ "output_type": "stream",
|
|
|
|
+ "text": [
|
|
|
|
+ "/home/kleinsteuber/anaconda3/envs/pytorch-gpu/lib/python3.10/site-packages/numpy/lib/npyio.py:518: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.\n",
|
|
|
|
+ " arr = np.asanyarray(arr)\n"
|
|
|
|
+ ]
|
|
|
|
+ }
|
|
|
|
+ ],
|
|
|
|
+ "source": [
|
|
|
|
+ "np.save(\"Cache_NoBackup/approach4_ae2_encoded.npy\", [normal_encoded_imgs, anomalous_encoded_imgs])"
|
|
|
|
+ ]
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ "cell_type": "code",
|
|
|
|
+ "execution_count": 21,
|
|
|
|
+ "metadata": {},
|
|
|
|
+ "outputs": [
|
|
|
|
+ {
|
|
|
|
+ "data": {
|
|
|
|
+ "text/plain": [
|
|
|
|
+ "{'algorithm': 'auto',\n",
|
|
|
|
+ " 'atol': 0,\n",
|
|
|
|
+ " 'bandwidth': 0.2,\n",
|
|
|
|
+ " 'breadth_first': True,\n",
|
|
|
|
+ " 'kernel': 'gaussian',\n",
|
|
|
|
+ " 'leaf_size': 40,\n",
|
|
|
|
+ " 'metric': 'euclidean',\n",
|
|
|
|
+ " 'metric_params': None,\n",
|
|
|
|
+ " 'rtol': 0}"
|
|
|
|
+ ]
|
|
|
|
+ },
|
|
|
|
+ "execution_count": 21,
|
|
|
|
+ "metadata": {},
|
|
|
|
+ "output_type": "execute_result"
|
|
|
|
+ }
|
|
|
|
+ ],
|
|
|
|
+ "source": [
|
|
|
|
+ "kde = KernelDensity(kernel=\"gaussian\", bandwidth=0.2).fit(normal_encoded_imgs)\n",
|
|
|
|
+ "kde.get_params()"
|
|
|
|
+ ]
|
|
|
|
+ },
|
|
{
|
|
{
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
"execution_count": 31,
|
|
"execution_count": 31,
|