{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import torch" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "getattr(torch.optim.lr_scheduler, \"StepLR\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = getattr(torch.nn, \"ReLU\")()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "loss = getattr(torch.nn, \"L1Loss\")()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "input = torch.randn(3, 5, requires_grad=True)\n", "target = torch.randn(3, 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b = torch.randn(2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = loss(input, target)\n", "output.backward()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = 1.\n", "if s is not None:\n", " assert 0.0 < s < 1.0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class A:\n", " @property\n", " def __name__(self):\n", " return \"adafa\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = A()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.__name__" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from training.gpu_manager import GPUManager" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "gpu_manager = GPUManager(True)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2020-07-21 14:10:13.170 | DEBUG | training.gpu_manager:_get_free_gpu:57 - pid 11721 picking gpu 0\n" ] }, { "data": { "text/plain": [ "0" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gpu_manager.get_free_gpu()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pathlib import Path" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p = Path(\"/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "str(p).split(\"/\")[0] + \"/\" + str(p).split(\"/\")[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p.parents[0].resolve()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p.exists()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = 'Experiment JSON, e.g. \\'{\"dataset\": \"EmnistDataset\", \"model\": \"CharacterModel\", \"network\": \"mlp\"}\\''" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(d)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "import yaml" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "path = \"/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/sample_experiment.yml\"" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "with open(path) as f:\n", " d = yaml.safe_load(f)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "experiment_config = d[\"experiments\"][0]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'dataloader': 'EmnistDataLoader',\n", " 'data_loader_args': {'splits': ['train', 'val'],\n", " 'sample_to_balance': True,\n", " 'subsample_fraction': None,\n", " 'transform': None,\n", " 'target_transform': None,\n", " 'batch_size': 256,\n", " 'shuffle': True,\n", " 'num_workers': 0,\n", " 'cuda': True,\n", " 'seed': 4711},\n", " 'model': 'CharacterModel',\n", " 'metrics': ['accuracy'],\n", " 'network': 'MLP',\n", " 'network_args': {'input_size': 784, 'num_layers': 2},\n", " 'train_args': {'batch_size': 256, 'epochs': 16},\n", " 'criterion': 'CrossEntropyLoss',\n", " 'criterion_args': {'weight': None, 'ignore_index': -100, 'reduction': 'mean'},\n", " 'optimizer': 'AdamW',\n", " 'optimizer_args': {'lr': 0.0003,\n", " 'betas': [0.9, 0.999],\n", " 'eps': 1e-08,\n", " 'weight_decay': 0,\n", " 'amsgrad': False},\n", " 'lr_scheduler': 'OneCycleLR',\n", " 'lr_scheduler_args': {'max_lr': 3e-05, 'epochs': 16}}" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "experiment_config" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "import importlib" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "network_module = importlib.import_module(\"text_recognizer.networks\")\n", "network_fn_ = getattr(network_module, experiment_config[\"network\"])\n", "network_args = experiment_config.get(\"network_args\", {})" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 784)" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1,) + (network_args[\"input_size\"],)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "optimizer_ = getattr(torch.optim, experiment_config[\"optimizer\"])\n", "optimizer_args = experiment_config.get(\"optimizer_args\", {})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "optimizer_" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "optimizer_args" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "network_args" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "network_fn_" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "net = network_fn_(**network_args)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "optimizer_(net.parameters() , **optimizer_args)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "criterion_ = getattr(torch.nn, experiment_config[\"criterion\"])\n", "criterion_args = experiment_config.get(\"criterion_args\", {})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "criterion_(**criterion_args)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "models_module = importlib.import_module(\"text_recognizer.models\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "metrics = {metric: getattr(models_module, metric) for metric in experiment_config[\"metrics\"]}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "torch.randn(3, 10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "torch.randn(3, 1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "metrics['accuracy'](torch.randn(3, 10), torch.randn(3, 1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "metric_fn_ = getattr(models_module, experiment_config[\"metric\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "metric_fn_" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2.e-3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lr_scheduler_ = getattr(\n", " torch.optim.lr_scheduler, experiment_config[\"lr_scheduler\"]\n", ")\n", "lr_scheduler_args = experiment_config.get(\"lr_scheduler_args\", {})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"OneCycleLR\" in str(lr_scheduler_)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "datasets_module = importlib.import_module(\"text_recognizer.datasets\")\n", "data_loader_ = getattr(datasets_module, experiment_config[\"dataloader\"])\n", "data_loader_args = experiment_config.get(\"data_loader_args\", {})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data_loader_(**data_loader_args)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "cuda = \"cuda:0\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import re\n", "cleanString = re.sub('[^A-Za-z]+','', cuda )" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "cleanString = re.sub('[^0-9]+','', cuda )" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cleanString" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([28, 28], 1)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "([28, 28], ) + (1,)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(3-1))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1,)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tuple([1])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from glob import glob" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/text_recognizer/weights/CharacterModel_Emnist_MLP_weights.pt']" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "glob(\"/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/text_recognizer/weights/CharacterModel_*MLP_weights.pt\")" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def test(a, b, c, d):\n", " print(a,b,c,d)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "f = {\"a\": 2, \"b\": 3, \"c\": 4}" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_items([('a', 2), ('b', 3), ('c', 4)])\n" ] } ], "source": [ "print(f.items())" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 3 4 1\n" ] } ], "source": [ "test(**f, d=1)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "path = \"/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/*\"" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "l = glob(path)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "l.sort()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_124928' in l" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_124928',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_141139',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_141213',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_141433',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_141702',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_145028',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_150212',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_150301',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_150317',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_151135',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_151408',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_153144',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_153207',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_153310',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_175150',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_180741',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_181933',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_183347',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_190044',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_190633',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_190738',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_191111',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_191310',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_191412',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_191504',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0721_191826',\n", " '/home/akternurra/Documents/projects/quest-for-general-artifical-intelligence/projects/text-recognizer/src/training/experiments/CharacterModel_Emnist_MLP/0722_191559']" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "from loguru import logger" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'Logger' object has no attribute 'DEBUG'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlogger\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDEBUG\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: 'Logger' object has no attribute 'DEBUG'" ] } ], "source": [ "logger.DEBUG" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.2" } }, "nbformat": 4, "nbformat_minor": 4 }