Created
April 22, 2022 05:55
-
-
Save moarshy/19d0a5a60797d5e86decf20adc5e7a8b to your computer and use it in GitHub Desktop.
Untitled10.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "name": "Untitled10.ipynb", | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyOXenI+xjgxKs/RK2sH5dFZ", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| }, | |
| "accelerator": "GPU" | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/moarshy/19d0a5a60797d5e86decf20adc5e7a8b/untitled10.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 26, | |
| "metadata": { | |
| "id": "75IDHaqSs-kF" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "!pip install git+https://github.com/fastai/fastai.git -Uqq\n", | |
| "!pip install timm -Uqq" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "from fastai.vision.all import *\n", | |
| "import timm" | |
| ], | |
| "metadata": { | |
| "id": "AujaDXNatQBN" | |
| }, | |
| "execution_count": 27, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "path = untar_data(URLs.PETS)" | |
| ], | |
| "metadata": { | |
| "id": "aSglXkSYtIgf" | |
| }, | |
| "execution_count": 28, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "files = get_image_files(path/\"images\")\n", | |
| "def label_func(f): return f[0].isupper()\n", | |
| "dls = ImageDataLoaders.from_name_func(path, files, label_func, item_tfms=Resize(224))" | |
| ], | |
| "metadata": { | |
| "id": "spBshfB0tO4H" | |
| }, | |
| "execution_count": 29, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "timm.list_models()" | |
| ], | |
| "metadata": { | |
| "id": "I2_A15tRuEM0" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "# Resnet" | |
| ], | |
| "metadata": { | |
| "id": "xQbpSNO8zvDm" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "learn = cnn_learner(dls, \n", | |
| " 'resnet18',\n", | |
| " metrics=accuracy).to_fp16()" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "3DzwLkAOt5L3", | |
| "outputId": "a6dca53d-1af9-45e6-c5d4-d91765501271" | |
| }, | |
| "execution_count": 42, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stderr", | |
| "text": [ | |
| "/usr/local/lib/python3.7/dist-packages/fastai/vision/learner.py:265: UserWarning: `cnn_learner` has been renamed to `vision_learner` -- please update your code\n", | |
| " warn(\"`cnn_learner` has been renamed to `vision_learner` -- please update your code\")\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "learn.model[1]" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "9nZ1MyN7zxen", | |
| "outputId": "76c4a532-b692-4ed6-aff3-e6aa166837d3" | |
| }, | |
| "execution_count": 43, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "Sequential(\n", | |
| " (0): AdaptiveConcatPool2d(\n", | |
| " (ap): AdaptiveAvgPool2d(output_size=1)\n", | |
| " (mp): AdaptiveMaxPool2d(output_size=1)\n", | |
| " )\n", | |
| " (1): Flatten(full=False)\n", | |
| " (2): BatchNorm1d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", | |
| " (3): Dropout(p=0.25, inplace=False)\n", | |
| " (4): Linear(in_features=1024, out_features=512, bias=False)\n", | |
| " (5): ReLU(inplace=True)\n", | |
| " (6): BatchNorm1d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", | |
| " (7): Dropout(p=0.5, inplace=False)\n", | |
| " (8): Linear(in_features=512, out_features=2, bias=False)\n", | |
| ")" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 43 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "learn.fit_one_cycle(1)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 81 | |
| }, | |
| "id": "dH7fa2mtuXqw", | |
| "outputId": "4d189d6c-3e60-41f3-b401-bbc38aeaa8f0" | |
| }, | |
| "execution_count": 37, | |
| "outputs": [ | |
| { | |
| "output_type": "display_data", | |
| "data": { | |
| "text/plain": [ | |
| "<IPython.core.display.HTML object>" | |
| ], | |
| "text/html": [ | |
| "\n", | |
| "<style>\n", | |
| " /* Turns off some styling */\n", | |
| " progress {\n", | |
| " /* gets rid of default border in Firefox and Opera. */\n", | |
| " border: none;\n", | |
| " /* Needs to be in here for Safari polyfill so background images work as expected. */\n", | |
| " background-size: auto;\n", | |
| " }\n", | |
| " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n", | |
| " background: #F44336;\n", | |
| " }\n", | |
| "</style>\n" | |
| ] | |
| }, | |
| "metadata": {} | |
| }, | |
| { | |
| "output_type": "display_data", | |
| "data": { | |
| "text/plain": [ | |
| "<IPython.core.display.HTML object>" | |
| ], | |
| "text/html": [ | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: left;\">\n", | |
| " <th>epoch</th>\n", | |
| " <th>train_loss</th>\n", | |
| " <th>valid_loss</th>\n", | |
| " <th>accuracy</th>\n", | |
| " <th>time</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <td>0</td>\n", | |
| " <td>0.647062</td>\n", | |
| " <td>0.246660</td>\n", | |
| " <td>0.904601</td>\n", | |
| " <td>01:08</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>" | |
| ] | |
| }, | |
| "metadata": {} | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "body = TimmBody('resnet18')" | |
| ], | |
| "metadata": { | |
| "id": "h3ENbeFO1PLN" | |
| }, | |
| "execution_count": 46, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "body.needs_pool" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "cZFpzA5h1Vbu", | |
| "outputId": "8217d109-71af-435e-f24f-a2549c77c82f" | |
| }, | |
| "execution_count": 48, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "(7, 7)" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 48 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "# Transformer Model" | |
| ], | |
| "metadata": { | |
| "id": "GuuUr8eq0j00" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "learn = cnn_learner(dls, \n", | |
| " 'vit_base_patch8_224',\n", | |
| " metrics=accuracy).to_fp16()" | |
| ], | |
| "metadata": { | |
| "id": "FI3ppdJdyLuR", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "outputId": "02cb5a75-1fbe-4b27-be7f-191f7bddfc85" | |
| }, | |
| "execution_count": 44, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stderr", | |
| "text": [ | |
| "/usr/local/lib/python3.7/dist-packages/fastai/vision/learner.py:265: UserWarning: `cnn_learner` has been renamed to `vision_learner` -- please update your code\n", | |
| " warn(\"`cnn_learner` has been renamed to `vision_learner` -- please update your code\")\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "learn.model[1]" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "LCv-qPjJ03cB", | |
| "outputId": "763744ca-9413-4044-c7c2-61a538766afc" | |
| }, | |
| "execution_count": 45, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "Sequential(\n", | |
| " (0): BatchNorm1d(1536, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", | |
| " (1): Dropout(p=0.25, inplace=False)\n", | |
| " (2): Linear(in_features=1536, out_features=512, bias=False)\n", | |
| " (3): ReLU(inplace=True)\n", | |
| " (4): BatchNorm1d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", | |
| " (5): Dropout(p=0.5, inplace=False)\n", | |
| " (6): Linear(in_features=512, out_features=2, bias=False)\n", | |
| ")" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 45 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "body = TimmBody('vit_base_patch8_224')" | |
| ], | |
| "metadata": { | |
| "id": "Nqj9Vb5b07jF" | |
| }, | |
| "execution_count": 49, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "#needs pool is none\n", | |
| "body.needs_pool" | |
| ], | |
| "metadata": { | |
| "id": "G3a4VkMx1gJL" | |
| }, | |
| "execution_count": 51, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "" | |
| ], | |
| "metadata": { | |
| "id": "-4IcebOE1hzc" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment