{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "\n", "from fastai.vision.all import *\n", "import gradio as gr\n", "import timm" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['convnext_atto',\n", " 'convnext_atto_ols',\n", " 'convnext_base',\n", " 'convnext_base_384_in22ft1k',\n", " 'convnext_base_in22ft1k',\n", " 'convnext_base_in22k',\n", " 'convnext_femto',\n", " 'convnext_femto_ols',\n", " 'convnext_large',\n", " 'convnext_large_384_in22ft1k',\n", " 'convnext_large_in22ft1k',\n", " 'convnext_large_in22k',\n", " 'convnext_nano',\n", " 'convnext_nano_ols',\n", " 'convnext_pico',\n", " 'convnext_pico_ols',\n", " 'convnext_small',\n", " 'convnext_small_384_in22ft1k',\n", " 'convnext_small_in22ft1k',\n", " 'convnext_small_in22k',\n", " 'convnext_tiny',\n", " 'convnext_tiny_384_in22ft1k',\n", " 'convnext_tiny_hnf',\n", " 'convnext_tiny_in22ft1k',\n", " 'convnext_tiny_in22k',\n", " 'convnext_xlarge_384_in22ft1k',\n", " 'convnext_xlarge_in22ft1k',\n", " 'convnext_xlarge_in22k']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "timm.list_models('convnext*')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'dls' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[4], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m learn \u001b[39m=\u001b[39m vision_learner(dls , \u001b[39m'\u001b[39m\u001b[39mconvnext_tiny_in22k\u001b[39m\u001b[39m'\u001b[39m , metrics \u001b[39m=\u001b[39m error_rate)\u001b[39m.\u001b[39mto_fp16()\n\u001b[0;32m 2\u001b[0m learn\u001b[39m.\u001b[39mfine_tune(\u001b[39m3\u001b[39m)\n", "\u001b[1;31mNameError\u001b[0m: name 'dls' is not defined" ] } ], "source": [] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10.75" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def quad(a , b , c , x): return a*x**2 + b*x + c\n", "\n", "quad(3 , 2 , 1 , 1.5)\n", "\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10.75" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from functools import partial\n", "\n", "def mk_quad(a , b , c): return partial(quad ,a , b , c)\n", "\n", "f = mk_quad(3 , 2 , 1)\n", "\n", "f(1.5)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "import torch\n", "import matplotlib.pyplot as plt \n", "def plot_function(f, title=None, min=-2.1, max=2.1, color='r', ylim=None):\n", " x = torch.linspace(min,max, 100)[:,None]\n", " if ylim: plt.ylim(ylim)\n", " plt.plot(x, f(x), color)\n", " if title is not None: plt.title(title)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def mse(preds , acts): return ((preds - acts)**2).mean()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "def noise(x, scale): return np.random.normal(scale=scale, size=x.shape)\n", "def add_noise(x, mult, add): return x * (1+noise(x,mult)) + noise(x,add)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "np.random.seed(42)\n", "\n", "x = torch.linspace(-2, 2, steps=20)[:,None]\n", "y = add_noise(f(x), 0.15, 1.5)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "80a6fa4ee63e4e1c8cf49638c80adf50", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(FloatSlider(value=1.5, description='a', max=4.5, min=-1.5), FloatSlider(value=1.5, descr…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from ipywidgets import interact\n", "@interact(a=1.5 , b = 1.5 , c=1.5)\n", "def plot_quad(a , b , c):\n", " f = mk_quad(a , b , c)\n", " plt.scatter(x , y)\n", " loss = mse(f(x) , y)\n", " plot_function(f , ylim=(-3 , 12) , title = f\"MSE: {loss:.2f}\")" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "def quad_mse(params):\n", " f = mk_quad(*params)\n", " return mse(f(x) , y)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([1.5000, 1.5000, 1.5000], requires_grad=True)" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abc = torch.tensor([1.5 , 1.5 , 1.5])\n", "abc.requires_grad_()\n", "\n" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(5.4892, dtype=torch.float64, grad_fn=)" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "loss = quad_mse(abc)\n", "\n", "loss" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "loss.backward()" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([-7.0908, 1.0602, -1.8620])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abc.grad" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "LOSS IS tensor(2.6496, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(3.3536, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(4.1631, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(4.7701, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(4.9408, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(4.5979, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(3.8490, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(2.9481, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(2.2058, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(1.8796, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(2.0819, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(2.7403, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(3.6224, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(4.4176, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(4.8469, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(4.7612, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(4.1943, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(3.3515, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(2.5374, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(2.0492, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(2.0717, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(2.6126, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(3.4991, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(4.4391, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(5.1229, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(5.3318, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(5.0142, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(4.3023, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(3.4640, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(2.8078, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(2.5724, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(2.8419, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(3.5167, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(4.3478, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(5.0262, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(5.2927, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(5.0303, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(4.3074, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(3.3546, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(2.4847, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(1.9837, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(2.0101, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(2.5399, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(3.3748, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(4.2127, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(4.7532, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(4.8036, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(4.3463, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(3.5442, dtype=torch.float64, grad_fn=)\n", "LOSS IS tensor(2.6829, dtype=torch.float64, grad_fn=)\n" ] } ], "source": [ "for i in range(50):\n", " loss = quad_mse(abc)\n", " loss.backward()\n", " with torch.no_grad():\n", " abc -= abc.grad * 0.01\n", " print(\"LOSS IS\" , loss)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([2.8198, 0.7940, 0.9271], requires_grad=True)" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abc" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "def rectified_linear(m , b , x):\n", " y = m*x + b\n", " return torch.clip(y , 0.0)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "diffusers", "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.8.16" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "d26bfdf1b33eed3ee2dd554c3e2dd92c45e09514ce35d65c074828f753b40123" } } }, "nbformat": 4, "nbformat_minor": 2 }