{ "cells": [ { "cell_type": "markdown", "id": "a8bd8621-65ae-4cb2-bdac-12c3b97a0de5", "metadata": {}, "source": [ "As of 24.3.18 the way to export cells to a python script is:\n", "1) pip install nbdev\n", "2) Add \"#|default_exp [file_name]\" to the top of your notebook\n", "3) Add \"#|export\" to the top of each cell you want to export\n", "4) run:\n", " import nbdev\n", " # the 2nd arg ('.') means \"save to the root directory where your .ipynb is\"\n", " nbdev.export.nb_export('[notebook_name].ipynb','.') " ] }, { "cell_type": "code", "execution_count": 7, "id": "533b4754-10d9-44bc-9894-a50523aec091", "metadata": {}, "outputs": [], "source": [ "#|default_exp main" ] }, { "cell_type": "code", "execution_count": 2, "id": "a090bf5f-9cd5-4f70-a7c0-42ad8db68db4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mDEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621\u001b[0m\u001b[33m\n", "\u001b[0mRequirement already satisfied: nbdev in /usr/local/lib/python3.9/site-packages (2.3.13)\n", "Requirement already satisfied: ipywidgets<=8.0.4 in /usr/local/lib/python3.9/site-packages (from nbdev) (8.0.4)\n", "Requirement already satisfied: fastcore>=1.5.27 in /usr/local/lib/python3.9/site-packages (from nbdev) (1.5.29)\n", "Requirement already satisfied: execnb>=0.1.4 in /usr/local/lib/python3.9/site-packages (from nbdev) (0.1.5)\n", "Requirement already satisfied: astunparse in /usr/local/lib/python3.9/site-packages (from nbdev) (1.6.3)\n", "Requirement already satisfied: ghapi>=1.0.3 in /usr/local/lib/python3.9/site-packages (from nbdev) (1.0.4)\n", "Requirement already satisfied: watchdog in /usr/local/lib/python3.9/site-packages (from nbdev) (4.0.0)\n", "Requirement already satisfied: asttokens in /usr/local/lib/python3.9/site-packages (from nbdev) (2.4.1)\n", "Requirement already satisfied: PyYAML in /usr/local/lib/python3.9/site-packages (from nbdev) (6.0.1)\n", "Requirement already satisfied: ipython in /usr/local/lib/python3.9/site-packages (from execnb>=0.1.4->nbdev) (8.18.1)\n", "Requirement already satisfied: pip in /usr/local/lib/python3.9/site-packages (from fastcore>=1.5.27->nbdev) (24.0)\n", "Requirement already satisfied: packaging in /usr/local/lib/python3.9/site-packages (from fastcore>=1.5.27->nbdev) (23.0)\n", "Requirement already satisfied: ipykernel>=4.5.1 in /usr/local/lib/python3.9/site-packages (from ipywidgets<=8.0.4->nbdev) (6.29.3)\n", "Requirement already satisfied: traitlets>=4.3.1 in /usr/local/lib/python3.9/site-packages (from ipywidgets<=8.0.4->nbdev) (5.14.2)\n", "Requirement already satisfied: widgetsnbextension~=4.0 in /usr/local/lib/python3.9/site-packages (from ipywidgets<=8.0.4->nbdev) (4.0.10)\n", "Requirement already satisfied: jupyterlab-widgets~=3.0 in /usr/local/lib/python3.9/site-packages (from ipywidgets<=8.0.4->nbdev) (3.0.10)\n", "Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.9/site-packages (from asttokens->nbdev) (1.16.0)\n", "Requirement already satisfied: wheel<1.0,>=0.23.0 in /usr/local/lib/python3.9/site-packages (from astunparse->nbdev) (0.41.3)\n", "Requirement already satisfied: appnope in /usr/local/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets<=8.0.4->nbdev) (0.1.4)\n", "Requirement already satisfied: comm>=0.1.1 in /usr/local/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets<=8.0.4->nbdev) (0.2.2)\n", "Requirement already satisfied: debugpy>=1.6.5 in /usr/local/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets<=8.0.4->nbdev) (1.8.1)\n", "Requirement already satisfied: jupyter-client>=6.1.12 in /usr/local/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets<=8.0.4->nbdev) (8.6.1)\n", "Requirement already satisfied: jupyter-core!=5.0.*,>=4.12 in /usr/local/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets<=8.0.4->nbdev) (5.7.2)\n", "Requirement already satisfied: matplotlib-inline>=0.1 in /usr/local/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets<=8.0.4->nbdev) (0.1.6)\n", "Requirement already satisfied: nest-asyncio in /usr/local/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets<=8.0.4->nbdev) (1.6.0)\n", "Requirement already satisfied: psutil in /usr/local/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets<=8.0.4->nbdev) (5.9.8)\n", "Requirement already satisfied: pyzmq>=24 in /usr/local/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets<=8.0.4->nbdev) (25.1.2)\n", "Requirement already satisfied: tornado>=6.1 in /usr/local/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets<=8.0.4->nbdev) (6.4)\n", "Requirement already satisfied: decorator in /usr/local/lib/python3.9/site-packages (from ipython->execnb>=0.1.4->nbdev) (5.1.1)\n", "Requirement already satisfied: jedi>=0.16 in /usr/local/lib/python3.9/site-packages (from ipython->execnb>=0.1.4->nbdev) (0.19.1)\n", "Requirement already satisfied: prompt-toolkit<3.1.0,>=3.0.41 in /usr/local/lib/python3.9/site-packages (from ipython->execnb>=0.1.4->nbdev) (3.0.43)\n", "Requirement already satisfied: pygments>=2.4.0 in /usr/local/lib/python3.9/site-packages (from ipython->execnb>=0.1.4->nbdev) (2.17.2)\n", "Requirement already satisfied: stack-data in /usr/local/lib/python3.9/site-packages (from ipython->execnb>=0.1.4->nbdev) (0.6.3)\n", "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.9/site-packages (from ipython->execnb>=0.1.4->nbdev) (4.5.0)\n", "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.9/site-packages (from ipython->execnb>=0.1.4->nbdev) (1.2.0)\n", "Requirement already satisfied: pexpect>4.3 in /usr/local/lib/python3.9/site-packages (from ipython->execnb>=0.1.4->nbdev) (4.9.0)\n", "Requirement already satisfied: parso<0.9.0,>=0.8.3 in /usr/local/lib/python3.9/site-packages (from jedi>=0.16->ipython->execnb>=0.1.4->nbdev) (0.8.3)\n", "Requirement already satisfied: importlib-metadata>=4.8.3 in /usr/local/lib/python3.9/site-packages (from jupyter-client>=6.1.12->ipykernel>=4.5.1->ipywidgets<=8.0.4->nbdev) (6.8.0)\n", "Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.9/site-packages (from jupyter-client>=6.1.12->ipykernel>=4.5.1->ipywidgets<=8.0.4->nbdev) (2.8.2)\n", "Requirement already satisfied: platformdirs>=2.5 in /usr/local/lib/python3.9/site-packages (from jupyter-core!=5.0.*,>=4.12->ipykernel>=4.5.1->ipywidgets<=8.0.4->nbdev) (3.1.1)\n", "Requirement already satisfied: ptyprocess>=0.5 in /usr/local/lib/python3.9/site-packages (from pexpect>4.3->ipython->execnb>=0.1.4->nbdev) (0.7.0)\n", "Requirement already satisfied: wcwidth in /usr/local/lib/python3.9/site-packages (from prompt-toolkit<3.1.0,>=3.0.41->ipython->execnb>=0.1.4->nbdev) (0.2.13)\n", "Requirement already satisfied: executing>=1.2.0 in /usr/local/lib/python3.9/site-packages (from stack-data->ipython->execnb>=0.1.4->nbdev) (2.0.1)\n", "Requirement already satisfied: pure-eval in /usr/local/lib/python3.9/site-packages (from stack-data->ipython->execnb>=0.1.4->nbdev) (0.2.2)\n", "Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.9/site-packages (from importlib-metadata>=4.8.3->jupyter-client>=6.1.12->ipykernel>=4.5.1->ipywidgets<=8.0.4->nbdev) (3.17.0)\n", "\u001b[33mDEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621\u001b[0m\u001b[33m\n", "\u001b[0mNote: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install nbdev" ] }, { "cell_type": "code", "execution_count": 6, "id": "d5b7b2d4-9cec-4227-9e85-53612e3022f1", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'mx' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[6], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m#|export\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m y \u001b[38;5;241m=\u001b[39m \u001b[43mmx\u001b[49m\n", "\u001b[0;31mNameError\u001b[0m: name 'mx' is not defined" ] } ], "source": [ "#|export\n", "y = mx" ] }, { "cell_type": "markdown", "id": "157f4ba8", "metadata": { "jp-MarkdownHeadingCollapsed": true }, "source": [ "# section header" ] }, { "cell_type": "code", "execution_count": 9, "id": "c5ca1b5b-3360-40f7-99ff-afde86356275", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'bc' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[9], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m a \u001b[38;5;241m=\u001b[39m \u001b[43mbc\u001b[49m\n", "\u001b[0;31mNameError\u001b[0m: name 'bc' is not defined" ] } ], "source": [ "a = bc" ] }, { "cell_type": "code", "execution_count": 49, "id": "91656ffe-8347-4dcd-8c1f-92019f568dbe", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'ou3' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[49], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m#|export\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m i \u001b[38;5;241m=\u001b[39m \u001b[43mou3\u001b[49m\n", "\u001b[0;31mNameError\u001b[0m: name 'ou3' is not defined" ] } ], "source": [ "#|export\n", "i = ou" ] }, { "cell_type": "code", "execution_count": 56, "id": "d09eb6cc-b7cf-4009-a13c-5df933469b96", "metadata": {}, "outputs": [], "source": [ "import nbdev\n", "\n", "notebook_name = \"lesson_2.ipynb\"\n", "export_destination = \".\" # the root directory\n", "nbdev.export.nb_export(notebook_name, export_destination)" ] }, { "cell_type": "markdown", "id": "6f087d39-106b-46a2-8623-f4429e2885f1", "metadata": {}, "source": [ "# section 2" ] }, { "cell_type": "code", "execution_count": 3, "id": "20461ad3-4fb0-44a0-946c-28fb9e06f59e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Package Version\n", "--------------------------------- --------------\n", "absl-py 2.0.0\n", "anyio 4.3.0\n", "appnope 0.1.4\n", "argon2-cffi 23.1.0\n", "argon2-cffi-bindings 21.2.0\n", "arrow 1.3.0\n", "asttokens 2.4.1\n", "astunparse 1.6.3\n", "async-lru 2.0.4\n", "attrs 23.2.0\n", "Babel 2.14.0\n", "beautifulsoup4 4.12.3\n", "black 23.1.0\n", "bleach 6.1.0\n", "blinker 1.7.0\n", "cachetools 5.3.2\n", "certifi 2023.11.17\n", "cffi 1.16.0\n", "charset-normalizer 3.3.2\n", "click 8.1.3\n", "comm 0.2.2\n", "debugpy 1.8.1\n", "decorator 5.1.1\n", "defusedxml 0.7.1\n", "exceptiongroup 1.2.0\n", "execnb 0.1.5\n", "executing 2.0.1\n", "fastcore 1.5.29\n", "fastjsonschema 2.19.1\n", "filelock 3.13.1\n", "Flask 3.0.0\n", "flatbuffers 23.5.26\n", "fqdn 1.5.1\n", "fsspec 2024.2.0\n", "gast 0.5.4\n", "ghapi 1.0.4\n", "google-auth 2.23.4\n", "google-auth-oauthlib 1.1.0\n", "google-pasta 0.2.0\n", "grpcio 1.59.3\n", "h11 0.14.0\n", "h5py 3.10.0\n", "httpcore 1.0.4\n", "httpx 0.27.0\n", "huggingface-hub 0.20.3\n", "idna 3.6\n", "importlib-metadata 6.8.0\n", "ipykernel 6.29.3\n", "ipython 8.18.1\n", "ipython-genutils 0.2.0\n", "ipywidgets 8.0.4\n", "isoduration 20.11.0\n", "itsdangerous 2.1.2\n", "jedi 0.19.1\n", "Jinja2 3.1.2\n", "joblib 1.3.2\n", "json5 0.9.24\n", "jsonpointer 2.4\n", "jsonschema 4.21.1\n", "jsonschema-specifications 2023.12.1\n", "jupyter 1.0.0\n", "jupyter_client 8.6.1\n", "jupyter-console 6.6.3\n", "jupyter-contrib-core 0.4.2\n", "jupyter-contrib-nbextensions 0.7.0\n", "jupyter_core 5.7.2\n", "jupyter-events 0.9.1\n", "jupyter-highlight-selected-word 0.2.0\n", "jupyter-lsp 2.2.4\n", "jupyter-nbextensions-configurator 0.6.3\n", "jupyter_server 2.13.0\n", "jupyter_server_terminals 0.5.3\n", "jupyterlab 4.1.5\n", "jupyterlab_pygments 0.3.0\n", "jupyterlab_server 2.25.4\n", "jupyterlab_widgets 3.0.10\n", "kaggle 1.6.6\n", "keras 2.15.0\n", "libclang 16.0.6\n", "lxml 5.1.0\n", "Markdown 3.5.1\n", "MarkupSafe 2.1.3\n", "matplotlib-inline 0.1.6\n", "mistune 3.0.2\n", "ml-dtypes 0.2.0\n", "mypy-extensions 1.0.0\n", "nbclient 0.10.0\n", "nbconvert 7.16.2\n", "nbdev 2.3.13\n", "nbformat 5.10.3\n", "nest-asyncio 1.6.0\n", "notebook 7.1.2\n", "notebook_shim 0.2.4\n", "numpy 1.26.2\n", "oauthlib 3.2.2\n", "opencv-python 4.9.0.80\n", "opt-einsum 3.3.0\n", "overrides 7.7.0\n", "packaging 23.0\n", "pandocfilters 1.5.1\n", "parso 0.8.3\n", "pathspec 0.11.1\n", "pexpect 4.9.0\n", "pip 24.0\n", "platformdirs 3.1.1\n", "prometheus_client 0.20.0\n", "prompt-toolkit 3.0.43\n", "protobuf 4.23.4\n", "psutil 5.9.8\n", "ptyprocess 0.7.0\n", "pure-eval 0.2.2\n", "pyasn1 0.5.1\n", "pyasn1-modules 0.3.0\n", "pycparser 2.21\n", "pydot 1.4.2\n", "Pygments 2.17.2\n", "pyparsing 3.1.1\n", "python-dateutil 2.8.2\n", "python-json-logger 2.0.7\n", "python-slugify 8.0.4\n", "PyYAML 6.0.1\n", "pyzmq 25.1.2\n", "qtconsole 5.5.1\n", "QtPy 2.4.1\n", "referencing 0.34.0\n", "requests 2.31.0\n", "requests-oauthlib 1.3.1\n", "rfc3339-validator 0.1.4\n", "rfc3986-validator 0.1.1\n", "rpds-py 0.18.0\n", "rsa 4.9\n", "scikit-learn 1.3.2\n", "scipy 1.11.4\n", "Send2Trash 1.8.2\n", "setuptools 68.2.2\n", "simpleai 0.8.3\n", "six 1.16.0\n", "sniffio 1.3.1\n", "soupsieve 2.5\n", "stack-data 0.6.3\n", "tensorboard 2.15.1\n", "tensorboard-data-server 0.7.2\n", "tensorflow 2.15.0\n", "tensorflow-estimator 2.15.0\n", "tensorflow-io-gcs-filesystem 0.34.0\n", "termcolor 2.3.0\n", "terminado 0.18.1\n", "text-unidecode 1.3\n", "threadpoolctl 3.2.0\n", "tinycss2 1.2.1\n", "tomli 2.0.1\n", "tornado 6.4\n", "tqdm 4.66.2\n", "traitlets 5.14.2\n", "types-python-dateutil 2.9.0.20240316\n", "typing_extensions 4.5.0\n", "uri-template 1.3.0\n", "urllib3 2.1.0\n", "watchdog 4.0.0\n", "wcwidth 0.2.13\n", "webcolors 1.13\n", "webencodings 0.5.1\n", "websocket-client 1.7.0\n", "Werkzeug 3.0.1\n", "wheel 0.41.3\n", "widgetsnbextension 4.0.10\n", "wrapt 1.14.1\n", "zipp 3.17.0\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip list" ] }, { "cell_type": "code", "execution_count": 8, "id": "746c335b-5825-4032-9ee9-834d947ba01d", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'untar_data' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[8], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mfastcore\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mall\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;241m*\u001b[39m\n\u001b[0;32m----> 2\u001b[0m path \u001b[38;5;241m=\u001b[39m \u001b[43muntar_data\u001b[49m(URLs\u001b[38;5;241m.\u001b[39mPETS)\n\u001b[1;32m 3\u001b[0m dls \u001b[38;5;241m=\u001b[39m ImageDataLoaders\u001b[38;5;241m.\u001b[39mfrom_name_re(path, get_image_files(path\u001b[38;5;241m/\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mimages\u001b[39m\u001b[38;5;124m'\u001b[39m), pat\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m(.+)_\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124md+.jpg\u001b[39m\u001b[38;5;124m'\u001b[39m, item_tfms\u001b[38;5;241m=\u001b[39mResize(\u001b[38;5;241m460\u001b[39m), batch_tfms\u001b[38;5;241m=\u001b[39maug_transforms(size\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m224\u001b[39m, min_scale\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0.75\u001b[39m))\n\u001b[1;32m 4\u001b[0m learn \u001b[38;5;241m=\u001b[39m vision_learner(dls, models\u001b[38;5;241m.\u001b[39mresnet50, metrics\u001b[38;5;241m=\u001b[39maccuracy)\n", "\u001b[0;31mNameError\u001b[0m: name 'untar_data' is not defined" ] } ], "source": [ "from fastcore.all import *\n", "path = untar_data(URLs.PETS)\n", "dls = ImageDataLoaders.from_name_re(path, get_image_files(path/'images'), pat='(.+)_\\d+.jpg', item_tfms=Resize(460), batch_tfms=aug_transforms(size=224, min_scale=0.75))\n", "learn = vision_learner(dls, models.resnet50, metrics=accuracy)\n", "learn.fine_tune(1)\n", "learn.path = Path('.')\n", "learn.export()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.18" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 5 }