diff --git a/notebooks/graph_features.ipynb b/notebooks/graph_features.ipynb deleted file mode 100644 index 7e45d6a..0000000 --- a/notebooks/graph_features.ipynb +++ /dev/null @@ -1,216 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "import torch\n", - "import numpy as np\n", - "import networkx as nx\n", - "\n", - "from pathlib import Path\n", - "from grace.base import GraphAttrs\n", - "from grace.io import write_graph, read_graph\n", - "from grace.io.image_dataset import ImageGraphDataset\n", - "from grace.models.feature_extractor import FeatureExtractor\n" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "PosixPath('/Users/kulicna/Desktop/dataset/playground/train')" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "path = \"/Users/kulicna/Desktop/dataset/playground/train/\"\n", - "path = Path(path)\n", - "path" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [], - "source": [ - "bbox_size = (224, 224)\n", - "extractor_filename = \"/Users/kulicna/Desktop/classifier/extractor/resnet152.pt\"\n", - "pre_trained_resnet = torch.load(extractor_filename)\n", - "feature_extractor = FeatureExtractor(model=pre_trained_resnet, bbox_size=bbox_size)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "GraceFileDataset(metadata=None, graph=None, annotation=None)" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# grace_pack = read_graph(path)\n", - "# grace_pack" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "ename": "AttributeError", - "evalue": "'NoneType' object has no attribute 'number_of_nodes'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m/Users/kulicna/Desktop/grace/notebooks/graph_features.ipynb Cell 4\u001b[0m line \u001b[0;36m2\n\u001b[1;32m 1\u001b[0m graph \u001b[39m=\u001b[39m grace_pack\u001b[39m.\u001b[39mgraph\n\u001b[0;32m----> 2\u001b[0m graph\u001b[39m.\u001b[39;49mnumber_of_nodes(), graph\u001b[39m.\u001b[39mnumber_of_edges()\n", - "\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'number_of_nodes'" - ] - } - ], - "source": [ - "# graph = grace_pack.graph\n", - "# graph.number_of_nodes(), graph.number_of_edges()" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dataset = ImageGraphDataset(path, path, transform=feature_extractor)\n", - "dataset" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "09/29/2023 01:37:45 PM MRC_Synthetic_File_000\n", - "09/29/2023 01:37:45 PM Nodes count | [58, 96, 36] x | [30.53, 50.53, 18.95] %\n", - "09/29/2023 01:37:45 PM Edges count | [228, 120, 206] x | [41.16, 21.66, 37.18] %\n", - "09/29/2023 01:37:45 PM Relabelled 'Annotation.UNKNOWN'\n", - "09/29/2023 01:37:45 PM Nodes count | [94, 96, 0] x | [49.47, 50.53, 0.0] %\n", - "09/29/2023 01:37:45 PM Edges count | [434, 120, 0] x | [78.34, 21.66, 0.0] %\n", - "/Users/kulicna/anaconda3/envs/grace-environment/lib/python3.10/site-packages/torchvision/transforms/functional.py:1603: UserWarning: The default value of the antialias parameter of all the resizing transforms (Resize(), RandomResizedCrop(), etc.) will change from None to True in v0.17, in order to be consistent across the PIL and Tensor backends. To suppress this warning, directly pass antialias=True (recommended, future default), antialias=None (current default, which means False for Tensors and True for PIL), or antialias=False (only works on Tensors - PIL will still use antialiasing). This also applies if you are using the inference transforms from the models weights: update the call to weights.transforms(antialias=True).\n", - " warnings.warn(\n", - "[W NNPACK.cpp:53] Could not initialize NNPACK! Reason: Unsupported hardware.\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "torch.Size([3724, 3724])\n" - ] - }, - { - "ename": "ArrowTypeError", - "evalue": "Could not convert tensor([0.7841, 0.4256, 0.8014, ..., 0.4391, 0.3728, 0.3906]) with type Tensor: was not a sequence or recognized null for conversion to list type", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mArrowTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m/Users/kulicna/Desktop/grace/notebooks/graph_features.ipynb Cell 7\u001b[0m line \u001b[0;36m6\n\u001b[1;32m 2\u001b[0m \u001b[39mprint\u001b[39m (image\u001b[39m.\u001b[39mshape)\n\u001b[1;32m 4\u001b[0m fn \u001b[39m=\u001b[39m target[\u001b[39m\"\u001b[39m\u001b[39mmetadata\u001b[39m\u001b[39m\"\u001b[39m][\u001b[39m\"\u001b[39m\u001b[39mimage_filename\u001b[39m\u001b[39m\"\u001b[39m]\n\u001b[0;32m----> 6\u001b[0m write_graph(\n\u001b[1;32m 7\u001b[0m filename\u001b[39m=\u001b[39;49mpath \u001b[39m/\u001b[39;49m \u001b[39mf\u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39m{\u001b[39;49;00mfn\u001b[39m}\u001b[39;49;00m\u001b[39m_supplement.grace\u001b[39;49m\u001b[39m\"\u001b[39;49m,\n\u001b[1;32m 8\u001b[0m graph\u001b[39m=\u001b[39;49mtarget[\u001b[39m\"\u001b[39;49m\u001b[39mgraph\u001b[39;49m\u001b[39m\"\u001b[39;49m],\n\u001b[1;32m 9\u001b[0m metadata\u001b[39m=\u001b[39;49mtarget[\u001b[39m\"\u001b[39;49m\u001b[39mmetadata\u001b[39;49m\u001b[39m\"\u001b[39;49m],\n\u001b[1;32m 10\u001b[0m annotation\u001b[39m=\u001b[39;49mtarget[\u001b[39m\"\u001b[39;49m\u001b[39mannotation\u001b[39;49m\u001b[39m\"\u001b[39;49m],\n\u001b[1;32m 11\u001b[0m )\n", - "File \u001b[0;32m~/Desktop/grace/grace/io/core.py:189\u001b[0m, in \u001b[0;36mwrite_graph\u001b[0;34m(filename, graph, metadata, annotation)\u001b[0m\n\u001b[1;32m 182\u001b[0m \u001b[39mwith\u001b[39;00m GraceFile(filename) \u001b[39mas\u001b[39;00m gfile:\n\u001b[1;32m 183\u001b[0m data \u001b[39m=\u001b[39m GraceFileDataset(\n\u001b[1;32m 184\u001b[0m graph\u001b[39m=\u001b[39mgraph,\n\u001b[1;32m 185\u001b[0m metadata\u001b[39m=\u001b[39mmetadata,\n\u001b[1;32m 186\u001b[0m annotation\u001b[39m=\u001b[39mannotation,\n\u001b[1;32m 187\u001b[0m )\n\u001b[0;32m--> 189\u001b[0m gfile\u001b[39m.\u001b[39;49mwrite(data)\n", - "File \u001b[0;32m~/Desktop/grace/grace/io/core.py:85\u001b[0m, in \u001b[0;36mGraceFile.write\u001b[0;34m(self, data)\u001b[0m\n\u001b[1;32m 81\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"Write a graph to a `.grace` file.\"\"\"\u001b[39;00m\n\u001b[1;32m 83\u001b[0m \u001b[39mif\u001b[39;00m data\u001b[39m.\u001b[39mgraph \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n\u001b[1;32m 84\u001b[0m \u001b[39m# write the graph nodes\u001b[39;00m\n\u001b[0;32m---> 85\u001b[0m nodes_df \u001b[39m=\u001b[39m pa\u001b[39m.\u001b[39;49mTable\u001b[39m.\u001b[39;49mfrom_pylist(\n\u001b[1;32m 86\u001b[0m [node_attr \u001b[39mfor\u001b[39;49;00m _, node_attr \u001b[39min\u001b[39;49;00m data\u001b[39m.\u001b[39;49mgraph\u001b[39m.\u001b[39;49mnodes(data\u001b[39m=\u001b[39;49m\u001b[39mTrue\u001b[39;49;00m)],\n\u001b[1;32m 87\u001b[0m schema\u001b[39m=\u001b[39;49mNODE_SCHEMA,\n\u001b[1;32m 88\u001b[0m )\n\u001b[1;32m 89\u001b[0m pq\u001b[39m.\u001b[39mwrite_table(nodes_df, \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mnodes_filename)\n\u001b[1;32m 91\u001b[0m \u001b[39m# write the graph edges\u001b[39;00m\n", - "File \u001b[0;32m~/anaconda3/envs/grace-environment/lib/python3.10/site-packages/pyarrow/table.pxi:3782\u001b[0m, in \u001b[0;36mpyarrow.lib.Table.from_pylist\u001b[0;34m()\u001b[0m\n", - "File \u001b[0;32m~/anaconda3/envs/grace-environment/lib/python3.10/site-packages/pyarrow/table.pxi:5313\u001b[0m, in \u001b[0;36mpyarrow.lib._from_pylist\u001b[0;34m()\u001b[0m\n", - "File \u001b[0;32m~/anaconda3/envs/grace-environment/lib/python3.10/site-packages/pyarrow/table.pxi:3657\u001b[0m, in \u001b[0;36mpyarrow.lib.Table.from_arrays\u001b[0;34m()\u001b[0m\n", - "File \u001b[0;32m~/anaconda3/envs/grace-environment/lib/python3.10/site-packages/pyarrow/table.pxi:1421\u001b[0m, in \u001b[0;36mpyarrow.lib._sanitize_arrays\u001b[0;34m()\u001b[0m\n", - "File \u001b[0;32m~/anaconda3/envs/grace-environment/lib/python3.10/site-packages/pyarrow/array.pxi:350\u001b[0m, in \u001b[0;36mpyarrow.lib.asarray\u001b[0;34m()\u001b[0m\n", - "File \u001b[0;32m~/anaconda3/envs/grace-environment/lib/python3.10/site-packages/pyarrow/array.pxi:320\u001b[0m, in \u001b[0;36mpyarrow.lib.array\u001b[0;34m()\u001b[0m\n", - "File \u001b[0;32m~/anaconda3/envs/grace-environment/lib/python3.10/site-packages/pyarrow/array.pxi:39\u001b[0m, in \u001b[0;36mpyarrow.lib._sequence_to_array\u001b[0;34m()\u001b[0m\n", - "File \u001b[0;32m~/anaconda3/envs/grace-environment/lib/python3.10/site-packages/pyarrow/error.pxi:144\u001b[0m, in \u001b[0;36mpyarrow.lib.pyarrow_internal_check_status\u001b[0;34m()\u001b[0m\n", - "File \u001b[0;32m~/anaconda3/envs/grace-environment/lib/python3.10/site-packages/pyarrow/error.pxi:123\u001b[0m, in \u001b[0;36mpyarrow.lib.check_status\u001b[0;34m()\u001b[0m\n", - "\u001b[0;31mArrowTypeError\u001b[0m: Could not convert tensor([0.7841, 0.4256, 0.8014, ..., 0.4391, 0.3728, 0.3906]) with type Tensor: was not a sequence or recognized null for conversion to list type" - ] - } - ], - "source": [ - "for image, target in dataset:\n", - " print (image.shape)\n", - " \n", - " fn = target[\"metadata\"][\"image_filename\"]\n", - " graph = target[\"graph\"]\n", - "\n", - " for _, node in graph.nodes(data=True):\n", - " node[GraphAttrs.NODE_FEATURES] = node[GraphAttrs.NODE_FEATURES].tolist()\n", - "\n", - " write_graph(\n", - " filename=path / f\"{fn}_supplement.grace\",\n", - " graph=graph,\n", - " metadata=target[\"metadata\"],\n", - " annotation=target[\"annotation\"],\n", - " )\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "grace-environment", - "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.10.11" - }, - "orig_nbformat": 4 - }, - "nbformat": 4, - "nbformat_minor": 2 -}