Skip to content

Commit

Permalink
Add huggingface segmentation models (#254)
Browse files Browse the repository at this point in the history
* add  huggingface-segmentation

* increment version

* update to not use allow_install

* segmentation testing notebook

* fiddling

* rename and simplify segmentation notebook WIP

* change bad notebook cells

* rename example notebook

* rename segmentation notebook
  • Loading branch information
edwardchalstrey1 authored May 25, 2022
1 parent 1276fb2 commit 92bf9ff
Show file tree
Hide file tree
Showing 4 changed files with 935 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,53 +150,18 @@
"source": [
"## Loading the model\n",
"\n",
"Some model entries in the scivision catalog contain a single loadable model. Here, let's load the \"image-classifiers\" entry, which contains each of the models in the [image-classifiers](https://pypi.org/project/image-classifiers/) package. We can do this with the `load_pretrained_model` function. We'll load 2 models (\"resnet18\" and \"densenet121\"), making sure to set the `allow_install` arg to True the first time `load_pretrained_model` is called (note the \"model\" arg is not mandatory and will have a default value for any model catalog entries that have more than one model - for model catalog entries with a single model, this need not be set):"
"Some model entries in the scivision catalog contain a single loadable model. Here, let's load the \"image-classifiers\" entry, which contains each of the models in the [image-classifiers](https://pypi.org/project/image-classifiers/) package. We can do this with the `load_pretrained_model` function. We'll load 2 models (`resnet18` and `densenet121`).\n",
"\n",
"Note: the \"model\" arg is not mandatory and will have a default value for any model catalog entries that have more than one model - for model catalog entries with a single model, this need not be set:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting scivision_classifier\n",
" Cloning https://github.com/alan-turing-institute/scivision_classifier (to revision main) to /private/var/folders/kf/72snt1z56h56smb3y7c0dxpr9rwb6q/T/pip-install-12ttdzvt/scivision-classifier_7004fcecb5334c47b6cb2eae4803b6c3\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" Running command git clone --filter=blob:none --quiet https://github.com/alan-turing-institute/scivision_classifier /private/var/folders/kf/72snt1z56h56smb3y7c0dxpr9rwb6q/T/pip-install-12ttdzvt/scivision-classifier_7004fcecb5334c47b6cb2eae4803b6c3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" Resolved https://github.com/alan-turing-institute/scivision_classifier to commit 9ae8cd11232ffd1bddcbcb0a7bbf1172028570ef\n",
" Preparing metadata (setup.py): started\n",
" Preparing metadata (setup.py): finished with status 'done'\n",
"Building wheels for collected packages: scivision_classifier\n",
" Building wheel for scivision_classifier (setup.py): started\n",
" Building wheel for scivision_classifier (setup.py): finished with status 'done'\n",
" Created wheel for scivision_classifier: filename=scivision_classifier-0.0.1-py3-none-any.whl size=3883 sha256=4fc051cf088067bf79e11d01db304d4e002e7a663284ea549a5cb039ad4f1521\n",
" Stored in directory: /private/var/folders/kf/72snt1z56h56smb3y7c0dxpr9rwb6q/T/pip-ephem-wheel-cache-7_rxr2ez/wheels/af/27/f9/5c32ecec2f2682373da54294d060008ca1d046e18d74187784\n",
"Successfully built scivision_classifier\n",
"Installing collected packages: scivision_classifier\n",
" Attempting uninstall: scivision_classifier\n",
" Found existing installation: scivision-classifier 0.0.1\n",
" Uninstalling scivision-classifier-0.0.1:\n",
" Successfully uninstalled scivision-classifier-0.0.1\n",
"Successfully installed scivision_classifier-0.0.1\n"
]
}
],
"outputs": [],
"source": [
"resnet_model = load_pretrained_model(model_repo, allow_install=True, model='resnet18')\n",
"resnet_model = load_pretrained_model(model_repo, model='resnet18')\n",
"densenet_model = load_pretrained_model(model_repo, model='densenet121')"
]
},
Expand Down Expand Up @@ -920,7 +885,7 @@
"Coordinates:\n",
" * y (y) int64 0 1 2 3 4 5 6 7 8 ... 231 232 233 234 235 236 237 238 239\n",
" * x (x) int64 0 1 2 3 4 5 6 7 8 ... 253 254 255 256 257 258 259 260 261\n",
" * channel (channel) int64 0 1 2</pre><div class='xr-wrap' style='display:none'><div class='xr-header'><div class='xr-obj-type'>xarray.DataArray</div><div class='xr-array-name'></div><ul class='xr-dim-list'><li><span class='xr-has-index'>y</span>: 240</li><li><span class='xr-has-index'>x</span>: 262</li><li><span class='xr-has-index'>channel</span>: 3</li></ul></div><ul class='xr-sections'><li class='xr-section-item'><div class='xr-array-wrap'><input id='section-1ae1e7de-8d49-4043-b4bd-6dbfd025764c' class='xr-array-in' type='checkbox' checked><label for='section-1ae1e7de-8d49-4043-b4bd-6dbfd025764c' title='Show/hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-array-preview xr-preview'><span>dask.array&lt;chunksize=(240, 262, 3), meta=np.ndarray&gt;</span></div><div class='xr-array-data'><table>\n",
" * channel (channel) int64 0 1 2</pre><div class='xr-wrap' style='display:none'><div class='xr-header'><div class='xr-obj-type'>xarray.DataArray</div><div class='xr-array-name'></div><ul class='xr-dim-list'><li><span class='xr-has-index'>y</span>: 240</li><li><span class='xr-has-index'>x</span>: 262</li><li><span class='xr-has-index'>channel</span>: 3</li></ul></div><ul class='xr-sections'><li class='xr-section-item'><div class='xr-array-wrap'><input id='section-fdb4719e-8aab-437f-891e-565e35b5e4aa' class='xr-array-in' type='checkbox' checked><label for='section-fdb4719e-8aab-437f-891e-565e35b5e4aa' title='Show/hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-array-preview xr-preview'><span>dask.array&lt;chunksize=(240, 262, 3), meta=np.ndarray&gt;</span></div><div class='xr-array-data'><table>\n",
" <tr>\n",
" <td>\n",
" <table>\n",
Expand Down Expand Up @@ -1000,7 +965,7 @@
"</svg>\n",
" </td>\n",
" </tr>\n",
"</table></div></div></li><li class='xr-section-item'><input id='section-06aeccac-5369-4c0d-bc99-ebe58a07c921' class='xr-section-summary-in' type='checkbox' checked><label for='section-06aeccac-5369-4c0d-bc99-ebe58a07c921' class='xr-section-summary' >Coordinates: <span>(3)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>y</span></div><div class='xr-var-dims'>(y)</div><div class='xr-var-dtype'>int64</div><div class='xr-var-preview xr-preview'>0 1 2 3 4 5 ... 235 236 237 238 239</div><input id='attrs-6ae8ece5-8f0f-4b02-b0b7-7179346dc4ce' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-6ae8ece5-8f0f-4b02-b0b7-7179346dc4ce' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-773e4854-4a7a-44d1-9b16-711256c2696e' class='xr-var-data-in' type='checkbox'><label for='data-773e4854-4a7a-44d1-9b16-711256c2696e' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([ 0, 1, 2, ..., 237, 238, 239])</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>x</span></div><div class='xr-var-dims'>(x)</div><div class='xr-var-dtype'>int64</div><div class='xr-var-preview xr-preview'>0 1 2 3 4 5 ... 257 258 259 260 261</div><input id='attrs-db6f1784-257f-4591-b5e8-f2fb52e6516a' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-db6f1784-257f-4591-b5e8-f2fb52e6516a' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-aff686c4-e173-4024-85f8-c97554bd5ce2' class='xr-var-data-in' type='checkbox'><label for='data-aff686c4-e173-4024-85f8-c97554bd5ce2' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([ 0, 1, 2, ..., 259, 260, 261])</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>channel</span></div><div class='xr-var-dims'>(channel)</div><div class='xr-var-dtype'>int64</div><div class='xr-var-preview xr-preview'>0 1 2</div><input id='attrs-19fda842-f299-4283-add5-8c7df15f1e01' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-19fda842-f299-4283-add5-8c7df15f1e01' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-af751093-a408-46a4-9ec7-c0cf85624483' class='xr-var-data-in' type='checkbox'><label for='data-af751093-a408-46a4-9ec7-c0cf85624483' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([0, 1, 2])</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-6fbbf13f-1245-4cea-a8d9-bd6b83a9b34c' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-6fbbf13f-1245-4cea-a8d9-bd6b83a9b34c' class='xr-section-summary' title='Expand/collapse section'>Attributes: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'></dl></div></li></ul></div></div>"
"</table></div></div></li><li class='xr-section-item'><input id='section-27da3ffc-3f0a-4107-b297-345844ca77bc' class='xr-section-summary-in' type='checkbox' checked><label for='section-27da3ffc-3f0a-4107-b297-345844ca77bc' class='xr-section-summary' >Coordinates: <span>(3)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>y</span></div><div class='xr-var-dims'>(y)</div><div class='xr-var-dtype'>int64</div><div class='xr-var-preview xr-preview'>0 1 2 3 4 5 ... 235 236 237 238 239</div><input id='attrs-1780b9b5-7e84-468a-8211-24e23f1a81a2' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-1780b9b5-7e84-468a-8211-24e23f1a81a2' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-756a69e6-e7a1-451b-849e-be6b25bd37d5' class='xr-var-data-in' type='checkbox'><label for='data-756a69e6-e7a1-451b-849e-be6b25bd37d5' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([ 0, 1, 2, ..., 237, 238, 239])</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>x</span></div><div class='xr-var-dims'>(x)</div><div class='xr-var-dtype'>int64</div><div class='xr-var-preview xr-preview'>0 1 2 3 4 5 ... 257 258 259 260 261</div><input id='attrs-bae0823f-7d6a-4240-b9f5-1edd2325d445' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-bae0823f-7d6a-4240-b9f5-1edd2325d445' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-d6ae0d9a-e6d6-478a-bf12-bf88c6e76d21' class='xr-var-data-in' type='checkbox'><label for='data-d6ae0d9a-e6d6-478a-bf12-bf88c6e76d21' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([ 0, 1, 2, ..., 259, 260, 261])</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>channel</span></div><div class='xr-var-dims'>(channel)</div><div class='xr-var-dtype'>int64</div><div class='xr-var-preview xr-preview'>0 1 2</div><input id='attrs-fb1dd320-738d-433a-9f21-354600f8be47' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-fb1dd320-738d-433a-9f21-354600f8be47' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-db9a335d-ef6f-4b36-af5e-0f3164420ec6' class='xr-var-data-in' type='checkbox'><label for='data-db9a335d-ef6f-4b36-af5e-0f3164420ec6' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([0, 1, 2])</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-f2a4c119-969c-45cc-af0e-50541ca1fddd' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-f2a4c119-969c-45cc-af0e-50541ca1fddd' class='xr-section-summary' title='Expand/collapse section'>Attributes: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'></dl></div></li></ul></div></div>"
],
"text/plain": [
"<xarray.DataArray (y: 240, x: 262, channel: 3)>\n",
Expand Down Expand Up @@ -1045,7 +1010,7 @@
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x167182a00>"
"<matplotlib.image.AxesImage at 0x173c761f0>"
]
},
"execution_count": 13,
Expand Down Expand Up @@ -1083,6 +1048,13 @@
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1/1 [==============================] - 0s 371ms/step\n"
]
},
{
"data": {
"text/plain": [
Expand All @@ -1103,6 +1075,13 @@
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1/1 [==============================] - 2s 2s/step\n"
]
},
{
"data": {
"text/plain": [
Expand Down Expand Up @@ -1346,42 +1325,6 @@
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting scivision_huggingface\n",
" Cloning https://github.com/alan-turing-institute/scivision_huggingface (to revision main) to /private/var/folders/kf/72snt1z56h56smb3y7c0dxpr9rwb6q/T/pip-install-56tpd_1p/scivision-huggingface_5812e68de5c54f73889ae7ce50486097\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" Running command git clone --filter=blob:none --quiet https://github.com/alan-turing-institute/scivision_huggingface /private/var/folders/kf/72snt1z56h56smb3y7c0dxpr9rwb6q/T/pip-install-56tpd_1p/scivision-huggingface_5812e68de5c54f73889ae7ce50486097\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" Resolved https://github.com/alan-turing-institute/scivision_huggingface to commit 16df12f6872876b1b22ea45a3a2f33dd42091087\n",
" Preparing metadata (setup.py): started\n",
" Preparing metadata (setup.py): finished with status 'done'\n",
"Building wheels for collected packages: scivision_huggingface\n",
" Building wheel for scivision_huggingface (setup.py): started\n",
" Building wheel for scivision_huggingface (setup.py): finished with status 'done'\n",
" Created wheel for scivision_huggingface: filename=scivision_huggingface-0.0.1-py3-none-any.whl size=3817 sha256=8a036189b9f92358eb27bc1709617dbc35a27fdbe70256d558960867597e0c4d\n",
" Stored in directory: /private/var/folders/kf/72snt1z56h56smb3y7c0dxpr9rwb6q/T/pip-ephem-wheel-cache-p3bnj_5g/wheels/58/43/19/be5a260399f14abe94dbcffed3df890b92e52dc026f56e3ec2\n",
"Successfully built scivision_huggingface\n",
"Installing collected packages: scivision_huggingface\n",
" Attempting uninstall: scivision_huggingface\n",
" Found existing installation: scivision-huggingface 0.0.1\n",
" Uninstalling scivision-huggingface-0.0.1:\n",
" Successfully uninstalled scivision-huggingface-0.0.1\n",
"Successfully installed scivision_huggingface-0.0.1\n"
]
},
{
"name": "stderr",
"output_type": "stream",
Expand All @@ -1393,7 +1336,7 @@
],
"source": [
"huggingface_repo = models_catalog[models_catalog.name == \"huggingface-classifiers\"].url.item()\n",
"microsoft_model = load_pretrained_model(huggingface_repo, allow_install=True, model='microsoft_swin_tiny_patch4_window7_224')"
"microsoft_model = load_pretrained_model(huggingface_repo, model='microsoft_swin_tiny_patch4_window7_224')"
]
},
{
Expand Down
Loading

0 comments on commit 92bf9ff

Please sign in to comment.