diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000000000000000000000000000000000..0c0ffa8454f1f5d27085e1119cd6ee8c7c0a36e2 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,36 @@ +*.7z filter=lfs diff=lfs merge=lfs -text +*.arrow filter=lfs diff=lfs merge=lfs -text +*.bin filter=lfs diff=lfs merge=lfs -text +*.bz2 filter=lfs diff=lfs merge=lfs -text +*.ckpt filter=lfs diff=lfs merge=lfs -text +*.ftz filter=lfs diff=lfs merge=lfs -text +*.gz filter=lfs diff=lfs merge=lfs -text +*.h5 filter=lfs diff=lfs merge=lfs -text +*.joblib filter=lfs diff=lfs merge=lfs -text +*.lfs.* filter=lfs diff=lfs merge=lfs -text +*.mlmodel filter=lfs diff=lfs merge=lfs -text +*.model filter=lfs diff=lfs merge=lfs -text +*.msgpack filter=lfs diff=lfs merge=lfs -text +*.npy filter=lfs diff=lfs merge=lfs -text +*.npz filter=lfs diff=lfs merge=lfs -text +*.onnx filter=lfs diff=lfs merge=lfs -text +*.ot filter=lfs diff=lfs merge=lfs -text +*.parquet filter=lfs diff=lfs merge=lfs -text +*.pb filter=lfs diff=lfs merge=lfs -text +*.pickle filter=lfs diff=lfs merge=lfs -text +*.pkl filter=lfs diff=lfs merge=lfs -text +*.pt filter=lfs diff=lfs merge=lfs -text +*.pth filter=lfs diff=lfs merge=lfs -text +*.rar filter=lfs diff=lfs merge=lfs -text +*.safetensors filter=lfs diff=lfs merge=lfs -text +saved_model/**/* filter=lfs diff=lfs merge=lfs -text +*.tar.* filter=lfs diff=lfs merge=lfs -text +*.tar filter=lfs diff=lfs merge=lfs -text +*.tflite filter=lfs diff=lfs merge=lfs -text +*.tgz filter=lfs diff=lfs merge=lfs -text +*.wasm filter=lfs diff=lfs merge=lfs -text +*.xz filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.zst filter=lfs diff=lfs merge=lfs -text +*tfevents* filter=lfs diff=lfs merge=lfs -text +images/*.jpg filter=lfs diff=lfs merge=lfs -text diff --git a/.nfs000000008874146400000004 b/.nfs000000008874146400000004 new file mode 100644 index 0000000000000000000000000000000000000000..c49d1b6f68574050e993641884e86c0c249bf981 Binary files /dev/null and b/.nfs000000008874146400000004 differ diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000000000000000000000000000000000000..16c7da5232e85d024318487de757a68fedc38b53 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "ms-python.black-formatter" + ] +} \ No newline at end of file diff --git "a/1\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \354\225\214\354\225\204\353\263\264\352\270\260/\354\230\210\354\240\234 1.01 \355\214\214\354\235\264\355\206\240\354\271\230 GPU \352\260\200\354\206\215 \355\231\225\354\235\270.ipynb" "b/1\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \354\225\214\354\225\204\353\263\264\352\270\260/\354\230\210\354\240\234 1.01 \355\214\214\354\235\264\355\206\240\354\271\230 GPU \352\260\200\354\206\215 \355\231\225\354\235\270.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..808866438f8c334fbc68d4d57cf4d25d583ff044 --- /dev/null +++ "b/1\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \354\225\214\354\225\204\353\263\264\352\270\260/\354\230\210\354\240\234 1.01 \355\214\214\354\235\264\355\206\240\354\271\230 GPU \352\260\200\354\206\215 \355\231\225\354\235\270.ipynb" @@ -0,0 +1,58 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "id": "95ff9559-b7e7-401f-9b73-8c7a68eed848", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.7.1+cu118\n", + "True\n", + "파이썬 경로: /home/ufslab223/anaconda3/envs/hface/bin/python\n", + "파이토치 버전: 2.7.1+cu118\n", + "파이토치 설치 경로: /home/ufslab223/anaconda3/envs/hface/lib/python3.11/site-packages/torch/__init__.py\n" + ] + } + ], + "source": [ + "import torch\n", + "\n", + "print(torch.__version__)\n", + "print(torch.cuda.is_available())\n", + "\n", + "\n", + "import sys\n", + "import torch\n", + "\n", + "print(f\"파이썬 경로: {sys.executable}\")\n", + "print(f\"파이토치 버전: {torch.__version__}\")\n", + "print(f\"파이토치 설치 경로: {torch.__file__}\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "hface", + "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.11.15" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/1\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \354\225\214\354\225\204\353\263\264\352\270\260/\354\230\210\354\240\234 1.02 MPS \352\260\200\354\206\215 \355\231\225\354\235\270.ipynb" "b/1\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \354\225\214\354\225\204\353\263\264\352\270\260/\354\230\210\354\240\234 1.02 MPS \352\260\200\354\206\215 \355\231\225\354\235\270.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..a9f3a8d8506494f10dfd395a47d5357463c4e14f --- /dev/null +++ "b/1\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \354\225\214\354\225\204\353\263\264\352\270\260/\354\230\210\354\240\234 1.02 MPS \352\260\200\354\206\215 \355\231\225\354\235\270.ipynb" @@ -0,0 +1,49 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "id": "95ff9559-b7e7-401f-9b73-8c7a68eed848", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.5.1\n", + "False\n", + "False\n" + ] + } + ], + "source": [ + "import torch\n", + "\n", + "print(torch.__version__)\n", + "print(torch.backends.mps.is_built())\n", + "print(torch.backends.mps.is_available())" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "hface", + "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.11.15" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/1\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \354\225\214\354\225\204\353\263\264\352\270\260/\354\230\210\354\240\234 1.03 \355\214\214\354\235\264\355\206\240\354\271\230 GPU \352\260\200\354\206\215 \355\231\225\354\235\270.ipynb" "b/1\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \354\225\214\354\225\204\353\263\264\352\270\260/\354\230\210\354\240\234 1.03 \355\214\214\354\235\264\355\206\240\354\271\230 GPU \352\260\200\354\206\215 \355\231\225\354\235\270.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..7b54b77b650f10168ac7500f60a4b364ccd6f895 --- /dev/null +++ "b/1\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \354\225\214\354\225\204\353\263\264\352\270\260/\354\230\210\354\240\234 1.03 \355\214\214\354\235\264\355\206\240\354\271\230 GPU \352\260\200\354\206\215 \355\231\225\354\235\270.ipynb" @@ -0,0 +1,38 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "95ff9559-b7e7-401f-9b73-8c7a68eed848", + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "\n", + "print(torch.__version__)\n", + "print(torch.cuda.is_available())" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/1\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \354\225\214\354\225\204\353\263\264\352\270\260/\354\230\210\354\240\234 1.04 resnet-18 \354\203\230\355\224\214 \354\275\224\353\223\234.ipynb" "b/1\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \354\225\214\354\225\204\353\263\264\352\270\260/\354\230\210\354\240\234 1.04 resnet-18 \354\203\230\355\224\214 \354\275\224\353\223\234.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..44d9c1f5c52527361a0489633836a7586f4b9fbf --- /dev/null +++ "b/1\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \354\225\214\354\225\204\353\263\264\352\270\260/\354\230\210\354\240\234 1.04 resnet-18 \354\203\230\355\224\214 \354\275\224\353\223\234.ipynb" @@ -0,0 +1,52 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "95ff9559-b7e7-401f-9b73-8c7a68eed848", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import AutoImageProcessor, AutoModelForImageClassification\n", + "import torch\n", + "from datasets import load_dataset\n", + "\n", + "dataset = load_dataset(\"huggingface/cats-image\", trust_remote_code=True)\n", + "image = dataset[\"test\"][\"image\"][0]\n", + "\n", + "image_processor = AutoImageProcessor.from_pretrained(\"microsoft/resnet-18\")\n", + "model = AutoModelForImageClassification.from_pretrained(\"microsoft/resnet-18\")\n", + "\n", + "inputs = image_processor(image, return_tensors=\"pt\")\n", + "\n", + "with torch.no_grad():\n", + " logits = model(**inputs).logits\n", + "\n", + "# model predicts one of the 1000 ImageNet classes\n", + "predicted_label = logits.argmax(-1).item()\n", + "print(model.config.id2label[predicted_label])" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/1\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \354\225\214\354\225\204\353\263\264\352\270\260/\354\230\210\354\240\234 1.05 push_to_hub \353\251\224\354\204\234\353\223\234 \354\227\205\353\241\234\353\223\234.ipynb" "b/1\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \354\225\214\354\225\204\353\263\264\352\270\260/\354\230\210\354\240\234 1.05 push_to_hub \353\251\224\354\204\234\353\223\234 \354\227\205\353\241\234\353\223\234.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..9bb6addd0b2bfa42657725a5f722441e385a68ce --- /dev/null +++ "b/1\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \354\225\214\354\225\204\353\263\264\352\270\260/\354\230\210\354\240\234 1.05 push_to_hub \353\251\224\354\204\234\353\223\234 \354\227\205\353\241\234\353\223\234.ipynb" @@ -0,0 +1,80 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "95ff9559-b7e7-401f-9b73-8c7a68eed848", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import AutoImageProcessor, AutoModelForImageClassification\n", + "import torch\n", + "from datasets import load_dataset\n", + "\n", + "dataset = load_dataset(\"huggingface/cats-image\", trust_remote_code=True)\n", + "image = dataset[\"test\"][\"image\"][0]\n", + "\n", + "image_processor = AutoImageProcessor.from_pretrained(\"microsoft/resnet-18\")\n", + "model = AutoModelForImageClassification.from_pretrained(\"microsoft/resnet-18\")\n", + "\n", + "inputs = image_processor(image, return_tensors=\"pt\")\n", + "\n", + "with torch.no_grad():\n", + " logits = model(**inputs).logits\n", + "\n", + "# model predicts one of the 1000 ImageNet classes\n", + "predicted_label = logits.argmax(-1).item()\n", + "print(model.config.id2label[predicted_label])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fb8c6599", + "metadata": {}, + "outputs": [], + "source": [ + "REPO_ID = \"모델 리포지터리 이름 입력\" # resnet-18\n", + "TOKEN = \"토큰 이름 입력\" # hf_..." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4d95cf8a", + "metadata": {}, + "outputs": [], + "source": [ + "image_processor.push_to_hub(\n", + " repo_id=REPO_ID,\n", + " token=TOKEN\n", + ")\n", + "model.push_to_hub(\n", + " repo_id=REPO_ID,\n", + " token=TOKEN\n", + ")" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.01 PretrainedConfig \355\201\264\353\236\230\354\212\244.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.01 PretrainedConfig \355\201\264\353\236\230\354\212\244.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..f39888c70340a2a26d677bd6eb912ce231e1ac7f --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.01 PretrainedConfig \355\201\264\353\236\230\354\212\244.ipynb" @@ -0,0 +1,48 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import PretrainedConfig\n", + "\n", + "config = PretrainedConfig(\n", + " model_type=\"bert\",\n", + " vocab_size=30522,\n", + " hidden_size=768,\n", + " num_attention_heads=12,\n", + " num_hidden_layers=12,\n", + " intermediate_size=3072,\n", + " hidden_act=\"gelu\",\n", + " hidden_dropout_prob=0.1,\n", + " initializer_range=0.02\n", + ")\n", + "print(config)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.02 PretrainedConfig \355\201\264\353\236\230\354\212\244\354\235\230 from_pretrained \353\251\224\354\204\234\353\223\234.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.02 PretrainedConfig \355\201\264\353\236\230\354\212\244\354\235\230 from_pretrained \353\251\224\354\204\234\353\223\234.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..e58cd3eb50206bbb7a8c2434ab84db7acfc59658 --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.02 PretrainedConfig \355\201\264\353\236\230\354\212\244\354\235\230 from_pretrained \353\251\224\354\204\234\353\223\234.ipynb" @@ -0,0 +1,42 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import PretrainedConfig\n", + "\n", + "config = PretrainedConfig.from_pretrained(\n", + " pretrained_model_name_or_path=\"google-bert/bert-base-uncased\",\n", + " hidden_act=\"swish\"\n", + ")\n", + "\n", + "print(config)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.03 BertConfig \355\201\264\353\236\230\354\212\244.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.03 BertConfig \355\201\264\353\236\230\354\212\244.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..11a4a89a4fda47bb387408d83c1a5ac8e5be3312 --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.03 BertConfig \355\201\264\353\236\230\354\212\244.ipynb" @@ -0,0 +1,42 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import BertConfig\n", + "\n", + "config = BertConfig(\n", + " num_hidden_layers=12, \n", + " num_attention_heads=12\n", + ")\n", + "\n", + "print(config)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.04 BertTokenizer \355\201\264\353\236\230\354\212\244.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.04 BertTokenizer \355\201\264\353\236\230\354\212\244.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..0e99f7d180fa790e99254f4302d566a5fa207133 --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.04 BertTokenizer \355\201\264\353\236\230\354\212\244.ipynb" @@ -0,0 +1,42 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import BertTokenizer\n", + "\n", + "tokenizer = BertTokenizer.from_pretrained(\"bert-base-uncased\")\n", + "text = \"I am learning about tokenizers.\"\n", + "encoded = tokenizer.encode(text, add_special_tokens=True)\n", + "\n", + "print(encoded)\n", + "print(tokenizer.decode(encoded))" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.05 BertModel \355\201\264\353\236\230\354\212\244.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.05 BertModel \355\201\264\353\236\230\354\212\244.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..6977a50274cd48e1e5a39f3b0ecc7a3e246d36fc --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.05 BertModel \355\201\264\353\236\230\354\212\244.ipynb" @@ -0,0 +1,44 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import BertTokenizer, BertModel\n", + "\n", + "tokenizer = BertTokenizer.from_pretrained(\"bert-base-uncased\")\n", + "text = \"I am learning about tokenizers.\"\n", + "input = tokenizer(text, return_tensors=\"pt\")\n", + "print(input)\n", + "\n", + "model = BertModel.from_pretrained(\"bert-base-uncased\")\n", + "output = model(**input)\n", + "print(output.last_hidden_state.shape)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.06 BertForMaskedLM \355\201\264\353\236\230\354\212\244.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.06 BertForMaskedLM \355\201\264\353\236\230\354\212\244.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..a68322904dcdb984ccac82bab3e02da04b46c6c5 --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.06 BertForMaskedLM \355\201\264\353\236\230\354\212\244.ipynb" @@ -0,0 +1,54 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "from torch.nn import functional as F\n", + "from transformers import BertTokenizer, BertForMaskedLM\n", + "\n", + "tokenizer = BertTokenizer.from_pretrained(\"bert-base-uncased\")\n", + "model = BertForMaskedLM.from_pretrained(\"bert-base-uncased\", return_dict=True)\n", + "\n", + "text = f\"I {tokenizer.mask_token} learning about tokenizers.\"\n", + "input = tokenizer(text, return_tensors=\"pt\")\n", + "output = model(**input)\n", + "\n", + "mask_index = torch.where(input[\"input_ids\"][0] == tokenizer.mask_token_id)\n", + "softmax = F.softmax(output.logits, dim=-1)\n", + "mask_word = softmax[0, mask_index, :]\n", + "top10 = torch.topk(mask_word, 10, dim=1)[1][0]\n", + "\n", + "for token in top10:\n", + " word = tokenizer.decode([token])\n", + " sentence = text.replace(tokenizer.mask_token, word)\n", + " print(sentence)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.07 CLIPFeatureExtractor \355\201\264\353\236\230\354\212\244.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.07 CLIPFeatureExtractor \355\201\264\353\236\230\354\212\244.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..b337ddea3f7da622c839e1892c982691bc8350e2 --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.07 CLIPFeatureExtractor \355\201\264\353\236\230\354\212\244.ipynb" @@ -0,0 +1,52 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "from transformers import CLIPFeatureExtractor\n", + "\n", + "dataset = load_dataset(\"huggingface/cats-image\")\n", + "image = dataset[\"test\"][\"image\"][0]\n", + "\n", + "feature_extractor = CLIPFeatureExtractor.from_pretrained(\"openai/clip-vit-base-patch32\")\n", + "inputs = feature_extractor(\n", + " images=image,\n", + " do_resize=True,\n", + " size=512,\n", + " do_center_crop=True,\n", + " crop_size=512,\n", + " return_tensors=\"pt\"\n", + ")\n", + "\n", + "print(inputs[\"pixel_values\"].shape)\n", + "print(inputs)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.08 Wav2Vec2FeatureExtractor \355\201\264\353\236\230\354\212\244.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.08 Wav2Vec2FeatureExtractor \355\201\264\353\236\230\354\212\244.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..46d0db5da73dfd08f62490816e7138f93722333b --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.08 Wav2Vec2FeatureExtractor \355\201\264\353\236\230\354\212\244.ipynb" @@ -0,0 +1,61 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "76155e63", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install soundfile librosa" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "from transformers import Wav2Vec2FeatureExtractor\n", + "\n", + "dataset = load_dataset(\"PolyAI/minds14\", \"ko-KR\", split=\"train\", trust_remote_code=True)\n", + "audios = [audio[\"array\"] for audio in dataset[\"audio\"][:2]]\n", + "\n", + "feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(\"facebook/wav2vec2-base-960h\")\n", + "inputs = feature_extractor(\n", + " raw_speech=audios,\n", + " padding=True,\n", + " return_attention_mask=True,\n", + " return_tensors=\"pt\"\n", + ")\n", + "\n", + "print(inputs[\"input_values\"][0].shape)\n", + "print(inputs[\"input_values\"][1].shape)\n", + "print(inputs)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.09 CLIPImageProcessor \355\201\264\353\236\230\354\212\244.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.09 CLIPImageProcessor \355\201\264\353\236\230\354\212\244.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..cf08af7e0da752ea3eb1ef760946c47927a18ff1 --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.09 CLIPImageProcessor \355\201\264\353\236\230\354\212\244.ipynb" @@ -0,0 +1,51 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "from transformers import CLIPImageProcessor\n", + "\n", + "dataset = load_dataset(\"huggingface/cats-image\")\n", + "image = dataset[\"test\"][\"image\"][0]\n", + "\n", + "image_processor = CLIPImageProcessor.from_pretrained(\"openai/clip-vit-base-patch32\")\n", + "pixel_values = image_processor(\n", + " images=image,\n", + " image_mean=[0.48145466, 0.4578275, 0.40821073],\n", + " image_std=[0.26862954, 0.26130258, 0.27577711],\n", + " do_convert_rgb=True,\n", + " return_tensors=\"pt\"\n", + ")\n", + "\n", + "print(pixel_values[\"pixel_values\"].shape)\n", + "print(pixel_values)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.10 \354\230\244\355\206\240 \355\201\264\353\236\230\354\212\244\353\241\234 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254 \353\252\250\353\215\270 \353\266\210\353\237\254\354\230\244\352\270\260.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.10 \354\230\244\355\206\240 \355\201\264\353\236\230\354\212\244\353\241\234 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254 \353\252\250\353\215\270 \353\266\210\353\237\254\354\230\244\352\270\260.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..1b32a1180503a3bf93c1080dd916bac274a654fa --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.10 \354\230\244\355\206\240 \355\201\264\353\236\230\354\212\244\353\241\234 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254 \353\252\250\353\215\270 \353\266\210\353\237\254\354\230\244\352\270\260.ipynb" @@ -0,0 +1,45 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import AutoModel, AutoTokenizer\n", + "\n", + "model_name = \"bert-base-uncased\"\n", + "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", + "model = AutoModel.from_pretrained(model_name)\n", + "\n", + "text = \"I am learning about tokenizers.\"\n", + "input = tokenizer(text, return_tensors=\"pt\")\n", + "output = model(**input)\n", + "\n", + "print(output.last_hidden_state.shape)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.11 \354\230\244\355\206\240 \355\201\264\353\236\230\354\212\244\353\241\234 \354\273\264\355\223\250\355\204\260 \353\271\204\354\240\204 \353\252\250\353\215\270 \353\266\210\353\237\254\354\230\244\352\270\260.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.11 \354\230\244\355\206\240 \355\201\264\353\236\230\354\212\244\353\241\234 \354\273\264\355\223\250\355\204\260 \353\271\204\354\240\204 \353\252\250\353\215\270 \353\266\210\353\237\254\354\230\244\352\270\260.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..6f563272c9020a4d58e8d5a471a0c0dacba628d3 --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.11 \354\230\244\355\206\240 \355\201\264\353\236\230\354\212\244\353\241\234 \354\273\264\355\223\250\355\204\260 \353\271\204\354\240\204 \353\252\250\353\215\270 \353\266\210\353\237\254\354\230\244\352\270\260.ipynb" @@ -0,0 +1,49 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from PIL import Image\n", + "from datasets import load_dataset\n", + "from transformers import AutoImageProcessor, AutoModel\n", + "\n", + "model_name = \"microsoft/resnet-18\" # \"google/vit-base-patch16-224-in21k\"\n", + "image_processor = AutoImageProcessor.from_pretrained(model_name)\n", + "model = AutoModel.from_pretrained(model_name)\n", + "\n", + "dataset = load_dataset(\"huggingface/cats-image\")\n", + "image = dataset[\"test\"][\"image\"][0]\n", + "\n", + "inputs = image_processor(images=image, return_tensors=\"pt\")\n", + "outputs = model(**inputs)\n", + "\n", + "print(outputs.last_hidden_state.shape)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.12 \354\230\244\355\206\240 \355\201\264\353\236\230\354\212\244\353\241\234 \354\230\244\353\224\224\354\230\244 \353\252\250\353\215\270 \353\266\210\353\237\254\354\230\244\352\270\260.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.12 \354\230\244\355\206\240 \355\201\264\353\236\230\354\212\244\353\241\234 \354\230\244\353\224\224\354\230\244 \353\252\250\353\215\270 \353\266\210\353\237\254\354\230\244\352\270\260.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..f1de6c300c861290c7e221173d8ca3c611aeabc3 --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.12 \354\230\244\355\206\240 \355\201\264\353\236\230\354\212\244\353\241\234 \354\230\244\353\224\224\354\230\244 \353\252\250\353\215\270 \353\266\210\353\237\254\354\230\244\352\270\260.ipynb" @@ -0,0 +1,45 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "from transformers import AutoFeatureExtractor\n", + "\n", + "model_name = \"facebook/wav2vec2-base-960h\" # \"openai/whisper-tiny\"\n", + "feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)\n", + "\n", + "dataset = load_dataset(\"PolyAI/minds14\", \"ko-KR\", split=\"train\")\n", + "audios = [audio[\"array\"] for audio in dataset[\"audio\"][:2]]\n", + "\n", + "inputs = feature_extractor(raw_speech=audios, padding=True, return_tensors=\"pt\")\n", + "print(inputs.keys())" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.13 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254 \355\214\214\354\235\264\355\224\204\353\235\274\354\235\270 \354\230\210\354\213\234 (1).ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.13 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254 \355\214\214\354\235\264\355\224\204\353\235\274\354\235\270 \354\230\210\354\213\234 (1).ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..69697d3abdcdf1c0ca829d16ac7918a3952095aa --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.13 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254 \355\214\214\354\235\264\355\224\204\353\235\274\354\235\270 \354\230\210\354\213\234 (1).ipynb" @@ -0,0 +1,69 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "import torch.nn.functional as F\n", + "from transformers import pipeline\n", + "\n", + "pipe = pipeline(\n", + " task=\"text-classification\",\n", + " model=\"cardiffnlp/twitter-roberta-base-sentiment-latest\"\n", + ")\n", + "raw_text = [\n", + " \"I love you.\",\n", + " \"I hate you.\",\n", + " \"I meet with you.\"\n", + "]\n", + "\n", + "inputs = pipe.tokenizer(raw_text, padding=True, return_tensors=\"pt\")\n", + "outputs = pipe.model(inputs[\"input_ids\"])\n", + "probabilities = F.softmax(outputs.logits, dim=1)\n", + "prediction = []\n", + "for prob in probabilities:\n", + " max_idx = torch.argmax(prob).item()\n", + " class_name = pipe.model.config.id2label[max_idx]\n", + " score = prob[max_idx].item()\n", + " prediction.append({\"label\": class_name, \"score\": score})\n", + "\n", + "print(\"- raw text:\")\n", + "print(raw_text)\n", + "\n", + "print(\"- input_ids:\")\n", + "print(inputs[\"input_ids\"])\n", + "\n", + "print(\"- logits:\")\n", + "print(outputs[\"logits\"])\n", + "\n", + "print(\"- prediction:\")\n", + "print(prediction)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.14 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254 \355\214\214\354\235\264\355\224\204\353\235\274\354\235\270 \354\230\210\354\213\234 (2).ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.14 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254 \355\214\214\354\235\264\355\224\204\353\235\274\354\235\270 \354\230\210\354\213\234 (2).ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..91e57e293626385fd7d8d07dd2b3194a86c3bef7 --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.14 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254 \355\214\214\354\235\264\355\224\204\353\235\274\354\235\270 \354\230\210\354\213\234 (2).ipynb" @@ -0,0 +1,47 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import pipeline\n", + "\n", + "pipe = pipeline(\n", + " task=\"text-classification\",\n", + " model=\"cardiffnlp/twitter-roberta-base-sentiment-latest\"\n", + ")\n", + "raw_text = [\n", + " \"I love you.\",\n", + " \"I hate you.\",\n", + " \"I meet with you.\"\n", + "]\n", + "prediction = pipe(raw_text)\n", + "print(prediction)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.15 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254 \355\214\214\354\235\264\355\224\204\353\235\274\354\235\270.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.15 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254 \355\214\214\354\235\264\355\224\204\353\235\274\354\235\270.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..656c41ad3499947000888a62596aa606596407ec --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.15 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254 \355\214\214\354\235\264\355\224\204\353\235\274\354\235\270.ipynb" @@ -0,0 +1,46 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "from transformers import pipeline\n", + "\n", + "pipe = pipeline(\n", + " task=\"text-generation\",\n", + " model=\"openai-community/gpt2\", # google-bert/bert-base-uncased\n", + " device=torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\"),\n", + " torch_dtype=torch.bfloat16\n", + ")\n", + "inputs = \"I am learning about tokenizers.\"\n", + "outputs = pipe(inputs)\n", + "print(outputs)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.16 \354\273\264\355\223\250\355\204\260 \353\271\204\354\240\204 \355\214\214\354\235\264\355\224\204\353\235\274\354\235\270.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.16 \354\273\264\355\223\250\355\204\260 \353\271\204\354\240\204 \355\214\214\354\235\264\355\224\204\353\235\274\354\235\270.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..6f751fed6ffcdf175e4fe0f074b0fec1fe5c9a3b --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.16 \354\273\264\355\223\250\355\204\260 \353\271\204\354\240\204 \355\214\214\354\235\264\355\224\204\353\235\274\354\235\270.ipynb" @@ -0,0 +1,49 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "from transformers import pipeline\n", + "\n", + "pipe = pipeline(\n", + " task=\"image-classification\",\n", + " model=\"google/vit-base-patch16-224\",\n", + " model_kwargs={\n", + " \"hidden_act\": \"gelu\" # relu\n", + " }\n", + ")\n", + "\n", + "dataset = load_dataset(\"huggingface/cats-image\")\n", + "inputs = dataset[\"test\"][\"image\"][0]\n", + "outputs = pipe(inputs)\n", + "print(outputs)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.17 \353\215\260\354\235\264\355\204\260\354\204\270\355\212\270 \353\266\210\353\237\254\354\230\244\352\270\260 (1).ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.17 \353\215\260\354\235\264\355\204\260\354\204\270\355\212\270 \353\266\210\353\237\254\354\230\244\352\270\260 (1).ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..006b391694dec9d152dc1e7e514c6ea5565bca43 --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.17 \353\215\260\354\235\264\355\204\260\354\204\270\355\212\270 \353\266\210\353\237\254\354\230\244\352\270\260 (1).ipynb" @@ -0,0 +1,41 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "\n", + "dataset1 = load_dataset(path=\"squad\")\n", + "dataset2 = load_dataset(path=\"squad\", split=\"train[:10]+validation[:10]\")\n", + "\n", + "print(dataset1)\n", + "print(dataset2)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.18 \353\215\260\354\235\264\355\204\260\354\204\270\355\212\270 \353\266\210\353\237\254\354\230\244\352\270\260 (2).ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.18 \353\215\260\354\235\264\355\204\260\354\204\270\355\212\270 \353\266\210\353\237\254\354\230\244\352\270\260 (2).ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..6db472aa2918c349548587db9df18d57c951b18a --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.18 \353\215\260\354\235\264\355\204\260\354\204\270\355\212\270 \353\266\210\353\237\254\354\230\244\352\270\260 (2).ipynb" @@ -0,0 +1,43 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "\n", + "dataset1 = load_dataset(path=\"../datasets\")\n", + "dataset2 = load_dataset(path=\"json\", data_dir=\"../datasets\")\n", + "dataset3 = load_dataset(path=\"json\", data_files={\"train\": \"../datasets/train.jsonl\", \"validation\": \"../datasets/val.jsonl\"})\n", + "\n", + "print(dataset1[\"train\"][0])\n", + "print(dataset2[\"train\"][0])\n", + "print(dataset3[\"train\"][0])" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.19 \353\215\260\354\235\264\355\204\260\354\204\270\355\212\270 \354\204\240\355\203\235, \353\266\204\353\246\254, \353\263\221\355\225\251.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.19 \353\215\260\354\235\264\355\204\260\354\204\270\355\212\270 \354\204\240\355\203\235, \353\266\204\353\246\254, \353\263\221\355\225\251.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..58b9c9de890e279a7df1d8af9958830716d09de3 --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.19 \353\215\260\354\235\264\355\204\260\354\204\270\355\212\270 \354\204\240\355\203\235, \353\266\204\353\246\254, \353\263\221\355\225\251.ipynb" @@ -0,0 +1,48 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "from datasets import concatenate_datasets\n", + "\n", + "dataset = load_dataset(\"squad\", split=\"train[:10]\")\n", + "\n", + "chunk1 = dataset.select([0, 1])\n", + "chunk2 = dataset.shard(num_shards=2, index=0)\n", + "chunk3 = dataset.shard(num_shards=2, index=1)\n", + "concat_dataset = concatenate_datasets([chunk1, chunk2, chunk3])\n", + "\n", + "print(len(chunk1))\n", + "print(len(chunk2))\n", + "print(len(chunk3))\n", + "print(len(concat_dataset))" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.20 \353\215\260\354\235\264\355\204\260\354\204\270\355\212\270 filter\354\231\200 map \353\251\224\354\204\234\353\223\234.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.20 \353\215\260\354\235\264\355\204\260\354\204\270\355\212\270 filter\354\231\200 map \353\251\224\354\204\234\353\223\234.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..818ddf4c33e28fade366a6a3fe381a0323f4d5ab --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.20 \353\215\260\354\235\264\355\204\260\354\204\270\355\212\270 filter\354\231\200 map \353\251\224\354\204\234\353\223\234.ipynb" @@ -0,0 +1,69 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "from transformers import BertTokenizer\n", + "\n", + "def filter_short_context(data):\n", + " return len(data[\"context\"]) <= 1024\n", + "\n", + "def tokenize_function(tokenizer, data):\n", + " inputs = [\n", + " f\"{question} [SEP] {context}\"\n", + " for question, context in zip(data[\"question\"], data[\"context\"])\n", + " ]\n", + " model_inputs = tokenizer(\n", + " inputs,\n", + " max_length=512,\n", + " padding=\"max_length\",\n", + " truncation=True,\n", + " return_tensors=\"pt\"\n", + " )\n", + " return model_inputs\n", + "\n", + "dataset = load_dataset(\"squad\", split=\"train[:10]\")\n", + "tokenizer = BertTokenizer.from_pretrained(\"bert-base-uncased\")\n", + "\n", + "filtered_dataset = dataset.filter(filter_short_context)\n", + "tokenized_dataset = filtered_dataset.map(\n", + " lambda x: tokenize_function(tokenizer, x),\n", + " batched=True,\n", + " remove_columns=dataset.column_names\n", + ")\n", + "tokenized_dataset.set_format(\n", + " type=\"torch\",\n", + " columns=[\"input_ids\", \"token_type_ids\", \"attention_mask\"]\n", + ")\n", + "print(tokenized_dataset)\n", + "print(tokenized_dataset[0])" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.21 \353\215\260\354\235\264\355\204\260\354\204\270\355\212\270 \354\227\205\353\241\234\353\223\234.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.21 \353\215\260\354\235\264\355\204\260\354\204\270\355\212\270 \354\227\205\353\241\234\353\223\234.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..e7728eaeb8f99a202e089a508bd52dd378fcb1c7 --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.21 \353\215\260\354\235\264\355\204\260\354\204\270\355\212\270 \354\227\205\353\241\234\353\223\234.ipynb" @@ -0,0 +1,62 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "from collections import defaultdict\n", + "from datasets import Dataset, Image, DatasetDict\n", + "\n", + "data = defaultdict(list)\n", + "folder_name = \"../images\"\n", + "for file_name in os.listdir(folder_name):\n", + " name = os.path.splitext(file_name)[0]\n", + " path = os.path.join(folder_name, file_name)\n", + "\n", + " data[\"name\"].append(name)\n", + " data[\"image\"].append(path)\n", + "\n", + "dataset = Dataset.from_dict(data).cast_column(\"image\", Image())\n", + "print(data)\n", + "print(dataset[0])\n", + "\n", + "datasetdict = DatasetDict(\n", + " {\n", + " \"train\": dataset.select(range(5)),\n", + " \"valid\": dataset.select(range(5, 10)),\n", + " \"test\": dataset.select(range(10, len(dataset)))\n", + " }\n", + ")\n", + "\n", + "hub_name = \"/\" # 데이터세트 저장소 경로 입력\n", + "token = \"hf_###...\" # 토큰 입력\n", + "datasetdict.push_to_hub(hub_name, token=token)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.22 ~ 2.26 \352\260\234\354\262\264\353\252\205 \354\235\270\354\213\235 \353\252\250\353\215\270.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.22 ~ 2.26 \352\260\234\354\262\264\353\252\205 \354\235\270\354\213\235 \353\252\250\353\215\270.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..04809ad291a7c988f2e9f22fbe0982f1f1f9e9ff --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.22 ~ 2.26 \352\260\234\354\262\264\353\252\205 \354\235\270\354\213\235 \353\252\250\353\215\270.ipynb" @@ -0,0 +1,150 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "39d707db", + "metadata": {}, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "from transformers import AutoModelForTokenClassification, AutoTokenizer\n", + "\n", + "dataset = load_dataset(\"klue\", \"ner\")\n", + "labels = dataset[\"train\"].features[\"ner_tags\"].feature.names\n", + "\n", + "model_name = \"Leo97/KoELECTRA-small-v3-modu-ner\"\n", + "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", + "model = AutoModelForTokenClassification.from_pretrained(\n", + " model_name,\n", + " num_labels=len(labels),\n", + " ignore_mismatched_sizes=True\n", + ")\n", + "\n", + "print(dataset[\"train\"])\n", + "print(dataset[\"train\"][0])\n", + "print(labels)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "56149149", + "metadata": {}, + "outputs": [], + "source": [ + "def preprocess_data(example, tokenizer):\n", + " sentence = \"\".join(example[\"tokens\"]).replace(\"\\xa0\", \" \")\n", + " encoded = tokenizer(\n", + " sentence,\n", + " return_offsets_mapping=True,\n", + " add_special_tokens=False,\n", + " padding=False,\n", + " truncation=False\n", + " )\n", + "\n", + " labels = []\n", + " for offset in encoded.offset_mapping:\n", + " if offset[0] == offset[1]:\n", + " labels.append(-100)\n", + " else:\n", + " labels.append(example[\"ner_tags\"][offset[0]])\n", + " encoded[\"labels\"] = labels\n", + " return encoded\n", + "\n", + "\n", + "processed_dataset = dataset.map(\n", + " lambda example: preprocess_data(example, tokenizer),\n", + " batched=False,\n", + " remove_columns=dataset[\"train\"].column_names\n", + ")\n", + "print(processed_dataset)\n", + "print(dataset[\"train\"][0][\"ner_tags\"])\n", + "print(processed_dataset[\"train\"][0][\"offset_mapping\"])\n", + "print(processed_dataset[\"train\"][0][\"labels\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f0aac748", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import Trainer, TrainingArguments\n", + "from transformers.data.data_collator import DataCollatorForTokenClassification\n", + "\n", + "training_args = TrainingArguments(\n", + " output_dir=\"token-classification\",\n", + " evaluation_strategy=\"epoch\",\n", + " per_device_train_batch_size=32,\n", + " per_device_eval_batch_size=32,\n", + " learning_rate=1e-4,\n", + " weight_decay=0.01,\n", + " num_train_epochs=5,\n", + " seed=42\n", + ")\n", + "\n", + "trainer = Trainer(\n", + " model=model,\n", + " args=training_args,\n", + " train_dataset=processed_dataset[\"train\"],\n", + " eval_dataset=processed_dataset[\"validation\"],\n", + " data_collator=DataCollatorForTokenClassification(tokenizer=tokenizer, padding=True)\n", + ")\n", + "\n", + "trainer.train()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ab38d1e5", + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "\n", + "model.eval()\n", + "text = \"위키북스의 윤대희, 김동화, 송종민 그리고 진현두는 2025년 서울에서 2시간 동안 신간 1권에 관한 논의를 진행했다.\"\n", + "\n", + "with torch.no_grad():\n", + " tokenized = tokenizer(\n", + " text,\n", + " return_tensors=\"pt\",\n", + " padding=True,\n", + " truncation=True,\n", + " max_length=512,\n", + " add_special_tokens=False\n", + " )\n", + " logits = model(**tokenized.to(\"cuda\")).logits.cpu()\n", + "\n", + "predictions = logits.argmax(dim=-1)[0].tolist()\n", + "tokens = tokenizer.tokenize(text)\n", + "\n", + "print(list(zip(tokens, [labels[i] for i in predictions])))" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.27 \354\235\264\353\257\270\354\247\200 \353\252\250\353\215\270 \355\217\211\352\260\200 \354\210\230\355\226\211.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.27 \354\235\264\353\257\270\354\247\200 \353\252\250\353\215\270 \355\217\211\352\260\200 \354\210\230\355\226\211.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..7aad732f5e731bdcbb1cd0d98d74c736f2cb010b --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.27 \354\235\264\353\257\270\354\247\200 \353\252\250\353\215\270 \355\217\211\352\260\200 \354\210\230\355\226\211.ipynb" @@ -0,0 +1,80 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "e41fc33d", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import torch\n", + "import evaluate\n", + "import numpy as np\n", + "from datasets import load_dataset\n", + "from transformers import AutoImageProcessor, AutoModelForImageClassification\n", + "\n", + "dataset = load_dataset(\"cifar10\", split=\"test[:1000]\")\n", + "model_name = \"nateraw/vit-base-patch16-224-cifar10\"\n", + "model = AutoModelForImageClassification.from_pretrained(model_name)\n", + "image_processor = AutoImageProcessor.from_pretrained(model_name)\n", + "\n", + "model.eval()\n", + "batch_size = 32\n", + "logits, labels = [], []\n", + "\n", + "for i in range(0, len(dataset), batch_size):\n", + " batch = dataset[i : i + batch_size]\n", + "\n", + " with torch.no_grad():\n", + " inputs = image_processor(batch[\"img\"], return_tensors=\"pt\")\n", + " outputs = model(inputs[\"pixel_values\"])\n", + "\n", + " logits.extend(outputs.logits.cpu().numpy())\n", + " labels.extend(batch[\"label\"])\n", + "\n", + "preds = np.argmax(logits, axis=-1)\n", + "\n", + "metric = evaluate.load(\"accuracy\")\n", + "accuracy = metric.compute(predictions=preds, references=labels)\n", + "print(accuracy)\n", + "\n", + "metric = evaluate.load(\"f1\")\n", + "f1 = metric.compute(predictions=preds, references=labels, average=\"macro\")\n", + "print(f1)\n", + "\n", + "comparison = evaluate.load(\"exact_match\", module_type=\"comparison\")\n", + "exact_match = comparison.compute(predictions1=preds, predictions2=labels)\n", + "print(exact_match)\n", + "\n", + "measurement = evaluate.load(\"label_distribution\", module_type=\"measurement\")\n", + "distribution = measurement.compute(data=labels)\n", + "print(distribution)\n" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.28 \353\240\210\354\235\264\353\215\224 \354\260\250\355\212\270 \354\213\234\352\260\201\355\231\224.ipynb" "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.28 \353\240\210\354\235\264\353\215\224 \354\260\250\355\212\270 \354\213\234\352\260\201\355\231\224.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..69a0686d87204e6814d60df37bf6bd521cad0936 --- /dev/null +++ "b/2\354\236\245 \355\227\210\352\271\205\355\216\230\354\235\264\354\212\244 \355\212\270\353\236\234\354\212\244\355\217\254\353\250\270/\354\230\210\354\240\234 2.28 \353\240\210\354\235\264\353\215\224 \354\260\250\355\212\270 \354\213\234\352\260\201\355\231\224.ipynb" @@ -0,0 +1,50 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "e41fc33d", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from evaluate.visualization import radar_plot\n", + "\n", + "data = [\n", + " {\"accuracy\": 0.49, \"precision\": 0.50, \"f1\": 0.49, \"latency_in_seconds\": 0.15},\n", + " {\"accuracy\": 0.92, \"precision\": 0.93, \"f1\": 0.92, \"latency_in_seconds\": 0.10},\n", + " {\"accuracy\": 0.68, \"precision\": 0.70, \"f1\": 0.68, \"latency_in_seconds\": 0.12}, \n", + " {\"accuracy\": 0.78, \"precision\": 0.80, \"f1\": 0.78, \"latency_in_seconds\": 0.11}\n", + "]\n", + "\n", + "model_names = [\"Model 1\", \"Model 2\", \"Model 3\", \"Model 4\"]\n", + "plot = radar_plot(data=data, model_names=model_names)\n", + "plot.show()" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.01 BERT \355\206\240\355\201\254\353\202\230\354\235\264\354\240\200\353\245\274 \354\235\264\354\232\251\355\225\234 \355\206\240\355\201\260\355\231\224.ipynb" "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.01 BERT \355\206\240\355\201\254\353\202\230\354\235\264\354\240\200\353\245\274 \354\235\264\354\232\251\355\225\234 \355\206\240\355\201\260\355\231\224.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..0396e48cc4e9785d9144e957dfcf76c14b23e668 --- /dev/null +++ "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.01 BERT \355\206\240\355\201\254\353\202\230\354\235\264\354\240\200\353\245\274 \354\235\264\354\232\251\355\225\234 \355\206\240\355\201\260\355\231\224.ipynb" @@ -0,0 +1,49 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "e41fc33d", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from transformers import BertTokenizer\n", + "\n", + "tokenizer = BertTokenizer.from_pretrained(\"google-bert/bert-base-multilingual-uncased\")\n", + "\n", + "text = \"Transformers Is so COOL\"\n", + "encoded = tokenizer(text)\n", + "print(encoded)\n", + "\n", + "input_ids = encoded[\"input_ids\"]\n", + "decoded = tokenizer.decode(input_ids)\n", + "print(decoded)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.02 BERT \353\252\250\353\215\270 \352\265\254\354\241\260.ipynb" "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.02 BERT \353\252\250\353\215\270 \352\265\254\354\241\260.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..a1efc274e011400c00e3d6bc6238fcd1a738d5b2 --- /dev/null +++ "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.02 BERT \353\252\250\353\215\270 \352\265\254\354\241\260.ipynb" @@ -0,0 +1,50 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "e41fc33d", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from transformers import BertModel\n", + "\n", + "model = BertModel.from_pretrained(\"google-bert/bert-base-multilingual-uncased\")\n", + "\n", + "for main_name, main_module in model.named_children():\n", + " print(main_name)\n", + " for sub_name, sub_module in main_module.named_children():\n", + " print(\"└\", sub_name)\n", + " for ssub_name, ssub_module in sub_module.named_children():\n", + " print(\"│ └\", ssub_name)\n", + " for sssub_name, sssub_module in ssub_module.named_children():\n", + " print(\"│ │ └\", sssub_name)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.03 ~ 3.07 \355\205\215\354\212\244\355\212\270 \353\266\204\353\245\230 \353\252\250\353\215\270.ipynb" "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.03 ~ 3.07 \355\205\215\354\212\244\355\212\270 \353\266\204\353\245\230 \353\252\250\353\215\270.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..fe4bc034d1af6c9b4578255775aa1233ac7f4d1d --- /dev/null +++ "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.03 ~ 3.07 \355\205\215\354\212\244\355\212\270 \353\266\204\353\245\230 \353\252\250\353\215\270.ipynb" @@ -0,0 +1,169 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "e41fc33d", + "metadata": {}, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "from transformers import BertTokenizer, BertForSequenceClassification\n", + "\n", + "def preprocess_data(example, tokenizer):\n", + " return tokenizer(example[\"document\"], truncation=True)\n", + "\n", + "model_name = \"google-bert/bert-base-multilingual-uncased\"\n", + "tokenizer = BertTokenizer.from_pretrained(model_name)\n", + "model = BertForSequenceClassification.from_pretrained(model_name, num_labels=2)\n", + "\n", + "dataset = load_dataset(\"nsmc\", trust_remote_code=True)\n", + "processed_dataset = dataset.map(\n", + " lambda example: preprocess_data(example, tokenizer),\n", + " batched=True,\n", + " remove_columns=[\"id\", \"document\"]\n", + ").rename_column(\"label\", \"labels\")\n", + "\n", + "print(dataset)\n", + "print(processed_dataset)\n", + "print(dataset[\"train\"][0])\n", + "print(processed_dataset[\"train\"][0])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ca996961", + "metadata": {}, + "outputs": [], + "source": [ + "from torch.utils.data import DataLoader\n", + "from transformers import DataCollatorWithPadding\n", + "\n", + "max_length_collator = DataCollatorWithPadding(\n", + " tokenizer=tokenizer,\n", + " padding=\"max_length\"\n", + ")\n", + "max_length_dataloader = DataLoader(\n", + " processed_dataset[\"train\"],\n", + " collate_fn=max_length_collator,\n", + " batch_size=4,\n", + " shuffle=False\n", + ")\n", + "max_length_iterator = iter(max_length_dataloader)\n", + "max_lnegth_batch = next(max_length_iterator)\n", + "print(\"max_length 패딩 입력 id shape :\", max_lnegth_batch[\"input_ids\"].shape)\n", + "\n", + "longest_collator = DataCollatorWithPadding(\n", + " tokenizer=tokenizer,\n", + " padding=\"longest\"\n", + ")\n", + "longest_dataloader = DataLoader(\n", + " processed_dataset[\"train\"],\n", + " collate_fn=longest_collator,\n", + " batch_size=4,\n", + " shuffle=False\n", + ")\n", + "longest_iterator = iter(longest_dataloader)\n", + "longest_batch = next(longest_iterator)\n", + "print(\"longest 패딩 입력 id shape :\", longest_batch[\"input_ids\"].shape)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f3fe00b8", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import TrainingArguments, Trainer\n", + "\n", + "training_args = TrainingArguments(\n", + " output_dir=\"text-classification\",\n", + " per_device_train_batch_size=8,\n", + " per_device_eval_batch_size=16,\n", + " learning_rate=5e-5,\n", + " num_train_epochs=1,\n", + " eval_steps=200,\n", + " logging_steps=200,\n", + " seed=42\n", + ")\n", + "\n", + "trainer = Trainer(\n", + " model=model,\n", + " args=training_args,\n", + " data_collator=longest_collator,\n", + " train_dataset=processed_dataset[\"train\"].select(range(10000)),\n", + " eval_dataset=processed_dataset[\"test\"].select(range(100))\n", + ")\n", + "\n", + "trainer.train()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4f2642b5", + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "\n", + "model.eval()\n", + "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", + "model.to(device)\n", + "\n", + "text = \"진짜 재밌었어요. 또 보러 갈거에요\"\n", + "inputs = tokenizer(text, return_tensors=\"pt\")\n", + "\n", + "with torch.no_grad():\n", + " outputs = model(**inputs.to(device))\n", + " print(outputs.logits)\n", + " print(outputs.logits.argmax())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2d2703ab", + "metadata": {}, + "outputs": [], + "source": [ + "import evaluate\n", + "\n", + "yhat = trainer.predict(processed_dataset[\"test\"])\n", + "predictions = yhat.predictions.argmax(axis=1)\n", + "references = yhat.label_ids\n", + "\n", + "metric = evaluate.load(\"accuracy\")\n", + "accuracy = metric.compute(predictions=predictions, references=references)\n", + "print(accuracy)\n", + "\n", + "metric = evaluate.load(\"f1\")\n", + "f1 = metric.compute(predictions=predictions, references=references)\n", + "print(f1)" + ] + } + ], + "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.10.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.08 ~ 3.09 BART \352\265\254\354\241\260.ipynb" "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.08 ~ 3.09 BART \352\265\254\354\241\260.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..a7c5578d868bc1ec635914ff2366698899a02038 --- /dev/null +++ "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.08 ~ 3.09 BART \352\265\254\354\241\260.ipynb" @@ -0,0 +1,69 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "e41fc33d", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from transformers import BartTokenizerFast\n", + "\n", + "tokenizer = BartTokenizerFast.from_pretrained(\"gogamza/kobart-base-v2\")\n", + "\n", + "text = \"BART는 요약 모델을 학습하기에 적합하다.\"\n", + "encoded = tokenizer(text)\n", + "print(encoded)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a5ec6a70", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from transformers import BartForConditionalGeneration\n", + "\n", + "model = BartForConditionalGeneration.from_pretrained(\"gogamza/kobart-base-v2\")\n", + "for main_name, main_module in model.named_children():\n", + " print(main_name)\n", + " for sub_name, sub_module in main_module.named_children():\n", + " print(\"└\", sub_name)\n", + " for ssub_name, ssub_module in sub_module.named_children():\n", + " print(\"│ └\", ssub_name)\n", + " for sssub_name, sssub_module in ssub_module.named_children():\n", + " print(\"│ │ └\", sssub_name)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.10 ~ 3.14 \354\232\224\354\225\275\353\254\270 \354\203\235\354\204\261 \353\252\250\353\215\270.ipynb" "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.10 ~ 3.14 \354\232\224\354\225\275\353\254\270 \354\203\235\354\204\261 \353\252\250\353\215\270.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..d08136cea5bc1bf88292e4974949ef9bbb2fe980 --- /dev/null +++ "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.10 ~ 3.14 \354\232\224\354\225\275\353\254\270 \354\203\235\354\204\261 \353\252\250\353\215\270.ipynb" @@ -0,0 +1,222 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "e41fc33d", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "from transformers import BartTokenizerFast, BartForConditionalGeneration\n", + "\n", + "def preprocess_data(example, tokenizer):\n", + " return tokenizer(\n", + " example[\"document\"],\n", + " text_target=example[\"summary\"],\n", + " truncation=True\n", + " )\n", + "\n", + "model_name = \"gogamza/kobart-base-v2\"\n", + "tokenizer = BartTokenizerFast.from_pretrained(model_name)\n", + "model = BartForConditionalGeneration.from_pretrained(model_name)\n", + "\n", + "dataset = load_dataset(\"daekeun-ml/naver-news-summarization-ko\")\n", + "print(dataset)\n", + "\n", + "tokenizer.model_max_length = model.config.max_position_embeddings\n", + "processed_dataset = dataset.map(\n", + " lambda example: preprocess_data(example, tokenizer),\n", + " batched=True,\n", + " remove_columns=dataset[\"train\"].column_names\n", + ")\n", + "\n", + "sample = processed_dataset[\"train\"][\"labels\"][0]\n", + "print(sample)\n", + "print(tokenizer.decode(sample))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1ab68856", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from torch.utils.data import DataLoader\n", + "from transformers import DataCollatorForSeq2Seq\n", + "\n", + "seq2seq_collator = DataCollatorForSeq2Seq(\n", + " tokenizer=tokenizer,\n", + " padding=\"longest\",\n", + " return_tensors=\"pt\"\n", + ")\n", + "\n", + "seq2seq_dataloader = DataLoader(\n", + " processed_dataset[\"train\"],\n", + " collate_fn=seq2seq_collator,\n", + " batch_size=4,\n", + " shuffle=False\n", + ")\n", + "\n", + "seq2seq_iterator = iter(seq2seq_dataloader)\n", + "seq2seq_batch = next(seq2seq_iterator)\n", + "for key, value in seq2seq_batch.items():\n", + " print(f\"{key} : {value.shape}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0536d7d0", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from transformers import Seq2SeqTrainingArguments, Seq2SeqTrainer\n", + "\n", + "training_args = Seq2SeqTrainingArguments(\n", + " output_dir=\"text-summarization\",\n", + " per_device_train_batch_size=8,\n", + " per_device_eval_batch_size=16,\n", + " learning_rate=5e-5,\n", + " num_train_epochs=1,\n", + " eval_steps=200,\n", + " logging_steps=200,\n", + " seed=42\n", + ")\n", + "\n", + "trainer = Seq2SeqTrainer(\n", + " model=model,\n", + " args=training_args,\n", + " data_collator=seq2seq_collator,\n", + " train_dataset=processed_dataset[\"train\"].select(range(10000)),\n", + " eval_dataset=processed_dataset[\"validation\"].select(range(100))\n", + ")\n", + "\n", + "trainer.train()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "45bd400b", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import torch\n", + "\n", + "model.eval()\n", + "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", + "model.to(device)\n", + "\n", + "sample = dataset[\"test\"][0]\n", + "document = sample[\"document\"]\n", + "inputs = tokenizer(document, return_tensors=\"pt\").to(device)\n", + "\n", + "with torch.no_grad():\n", + " outputs = model.generate(\n", + " **inputs,\n", + " max_length=256,\n", + " num_beams=4,\n", + " no_repeat_ngram_size=2,\n", + " early_stopping=True\n", + " )\n", + "print(\"원문 :\", document)\n", + "print(\"정답 요약문 :\", sample[\"summary\"])\n", + "print(\"생성 요약문 :\", tokenizer.decode(outputs[0], skip_special_tokens=True))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d4e44df8", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "!pip install rouge_score" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "727fabd2", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import evaluate\n", + "\n", + "test_loader = DataLoader(\n", + " processed_dataset[\"test\"].select(range(100)),\n", + " collate_fn=seq2seq_collator,\n", + " batch_size=4,\n", + " shuffle=False\n", + ")\n", + "\n", + "generated_summaries = []\n", + "true_summaries = dataset[\"test\"].select(range(100))[\"summary\"]\n", + "\n", + "with torch.no_grad():\n", + " for batch in test_loader:\n", + " batch = batch.to(device)\n", + " output = model.generate(\n", + " **batch,\n", + " max_length=1026,\n", + " num_beams=4,\n", + " no_repeat_ngram_size=2,\n", + " early_stopping=True\n", + " )\n", + " batch_summaries = tokenizer.batch_decode(output, skip_special_tokens=True)\n", + " generated_summaries.extend(batch_summaries)\n", + "\n", + "metric = evaluate.load(\"rouge\")\n", + "rouge_scores = metric.compute(predictions=generated_summaries, references=true_summaries)\n", + "print(rouge_scores)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.15 ~ 3.18 \354\266\224\354\266\234 \354\247\210\354\235\230 \354\235\221\353\213\265 \353\252\250\353\215\270.ipynb" "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.15 ~ 3.18 \354\266\224\354\266\234 \354\247\210\354\235\230 \354\235\221\353\213\265 \353\252\250\353\215\270.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..731b64cdfa39978e511afc029412f9e2600ce13e --- /dev/null +++ "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.15 ~ 3.18 \354\266\224\354\266\234 \354\247\210\354\235\230 \354\235\221\353\213\265 \353\252\250\353\215\270.ipynb" @@ -0,0 +1,177 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "e4ce77ca", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "from transformers import RobertaTokenizerFast, RobertaForQuestionAnswering\n", + "\n", + "def preprocess_data(example, tokenizer):\n", + " tokenized = tokenizer(\n", + " example[\"question\"],\n", + " example[\"context\"],\n", + " truncation=\"only_second\",\n", + " return_offsets_mapping=True\n", + " )\n", + " start_index = example[\"answers\"][\"answer_start\"][0]\n", + " answer_text = example[\"answers\"][\"text\"][0]\n", + " answer_tokens = tokenizer.encode(answer_text, add_special_tokens=False)\n", + " answer_tokens_length = len(answer_tokens)\n", + "\n", + " start_context_tokens_index = tokenized[\"input_ids\"].index(tokenizer.sep_token_id)\n", + " context_offset_mapping = tokenized[\"offset_mapping\"][start_context_tokens_index:]\n", + " tokenized[\"start_positions\"] = len(tokenized[\"input_ids\"])\n", + " tokenized[\"end_positions\"] = len(tokenized[\"input_ids\"])\n", + " \n", + " for i, (start_offset, end_offset) in enumerate(context_offset_mapping):\n", + " if start_offset >= start_index:\n", + " tokenized[\"start_positions\"] = start_context_tokens_index + i\n", + " tokenized[\"end_positions\"] = tokenized[\"start_positions\"] + answer_tokens_length\n", + " break\n", + "\n", + " return tokenized\n", + "\n", + "model_name = \"klue/roberta-base\"\n", + "tokenizer = RobertaTokenizerFast.from_pretrained(model_name)\n", + "model = RobertaForQuestionAnswering.from_pretrained(model_name)\n", + "\n", + "dataset = load_dataset(\"klue\", \"mrc\")\n", + "processed_dataset = dataset.filter(lambda x: not x[\"is_impossible\"])\n", + "processed_dataset = processed_dataset.map(\n", + " lambda example: preprocess_data(example, tokenizer), batched=False\n", + ")\n", + "processed_dataset = processed_dataset.filter(\n", + " lambda x: x[\"start_positions\"] < tokenizer.model_max_length\n", + ")\n", + "processed_dataset = processed_dataset.filter(\n", + " lambda x: x[\"end_positions\"] < tokenizer.model_max_length\n", + ")\n", + "print(dataset)\n", + "print(processed_dataset)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ce30aba3", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from transformers import DataCollatorWithPadding, TrainingArguments, Trainer\n", + "\n", + "collator = DataCollatorWithPadding(tokenizer, padding=\"longest\")\n", + "\n", + "training_arguments = TrainingArguments(\n", + " output_dir=\"question-answering\",\n", + " per_device_train_batch_size=8,\n", + " per_device_eval_batch_size=16,\n", + " learning_rate=5e-5,\n", + " num_train_epochs=1,\n", + " eval_steps=250,\n", + " logging_steps=250,\n", + " seed=42\n", + ")\n", + "\n", + "trainer = Trainer(\n", + " model=model,\n", + " args=training_arguments,\n", + " data_collator=collator,\n", + " train_dataset=processed_dataset[\"train\"].select(range(10000)),\n", + " eval_dataset=processed_dataset[\"validation\"].select(range(100))\n", + ")\n", + "\n", + "trainer.train()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "51a813f0", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import torch\n", + "\n", + "model.eval()\n", + "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", + "model.to(device)\n", + "\n", + "question = \"대한민국의 수도는 어디인가요?\"\n", + "context = \"서울은 대한민국의 수도다.\"\n", + "inputs = tokenizer(question, context, return_tensors=\"pt\").to(device)\n", + "\n", + "with torch.no_grad():\n", + " outputs = model(**inputs)\n", + "\n", + "start_index = outputs[\"start_logits\"].argmax(dim=-1).item()\n", + "end_index = outputs[\"end_logits\"].argmax(dim=-1).item()\n", + "predicted_ids = inputs[\"input_ids\"][0][start_index : end_index]\n", + "predicted_text = tokenizer.decode(predicted_ids)\n", + "print(predicted_text)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c5898add", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from evaluate import evaluator\n", + "\n", + "metric = evaluator(\"question-answering\")\n", + "results = metric.compute(\n", + " model,\n", + " tokenizer=tokenizer,\n", + " data=processed_dataset[\"validation\"].select(range(100)),\n", + " id_column=\"guid\",\n", + " question_column=\"question\",\n", + " context_column=\"context\",\n", + " label_column=\"answers\"\n", + ")\n", + "print(results)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.19 ~ 3.22 \352\270\260\352\263\204 \353\262\210\354\227\255 \353\252\250\353\215\270 copy.ipynb" "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.19 ~ 3.22 \352\270\260\352\263\204 \353\262\210\354\227\255 \353\252\250\353\215\270 copy.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..d1ae54066f6a2efefecc4ac500506065ee503aa9 --- /dev/null +++ "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.19 ~ 3.22 \352\270\260\352\263\204 \353\262\210\354\227\255 \353\252\250\353\215\270 copy.ipynb" @@ -0,0 +1,169 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "from transformers import T5TokenizerFast, T5ForConditionalGeneration\n", + "\n", + "def preprocess_data(example, tokenizer):\n", + " translation = example[\"translation\"]\n", + " translation_source = [\"en: \" + instance[\"en\"] for instance in translation]\n", + " translation_target = [\"ko: \" + instance[\"ko\"] for instance in translation]\n", + " tokenized = tokenizer(\n", + " translation_source,\n", + " text_target=translation_target,\n", + " truncation=True\n", + " )\n", + " return tokenized\n", + "\n", + "model_name = \"KETI-AIR/long-ke-t5-small\"\n", + "tokenizer = T5TokenizerFast.from_pretrained(model_name)\n", + "model = T5ForConditionalGeneration.from_pretrained(\"KETI-AIR/long-ke-t5-small\")\n", + "\n", + "dataset = load_dataset(\"Helsinki-NLP/opus-100\", \"en-ko\")\n", + "processed_dataset = dataset.map(\n", + " lambda example: preprocess_data(example, tokenizer),\n", + " batched=True,\n", + " remove_columns=dataset[\"train\"].column_names\n", + ")\n", + "\n", + "sample = processed_dataset[\"test\"][0]\n", + "print(sample)\n", + "print(\"변환된 출발 언어 :\", tokenizer.decode(sample[\"input_ids\"]))\n", + "print(\"변환된 도착 언어 :\", tokenizer.decode(sample[\"labels\"]))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c901d8ae", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import DataCollatorForSeq2Seq\n", + "from transformers import Seq2SeqTrainingArguments, Seq2SeqTrainer\n", + "\n", + "seq2seq_collator = DataCollatorForSeq2Seq(\n", + " tokenizer=tokenizer,\n", + " padding=\"longest\",\n", + " return_tensors=\"pt\"\n", + ")\n", + "\n", + "training_arguments = Seq2SeqTrainingArguments(\n", + " output_dir=\"t5-translation\",\n", + " per_device_train_batch_size=8,\n", + " per_device_eval_batch_size=16,\n", + " learning_rate=5e-5,\n", + " num_train_epochs=1,\n", + " eval_steps=2500,\n", + " logging_steps=2500,\n", + " seed=42\n", + ")\n", + "\n", + "trainer = Seq2SeqTrainer(\n", + " model=model,\n", + " args=training_arguments,\n", + " data_collator=seq2seq_collator,\n", + " train_dataset=processed_dataset[\"train\"].select(range(100000)),\n", + " eval_dataset=processed_dataset[\"validation\"].select(range(1000))\n", + ")\n", + "\n", + "trainer.train()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5e4c93c5", + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "\n", + "model.eval()\n", + "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", + "model.to(device)\n", + "\n", + "data = \"en: It's always great to acquire new knowledge.\"\n", + "inputs = tokenizer(data, return_tensors=\"pt\").to(device)\n", + "\n", + "with torch.no_grad():\n", + " outputs = model.generate(\n", + " **inputs,\n", + " max_length=512,\n", + " num_beams=4,\n", + " no_repeat_ngram_size=2,\n", + " early_stopping=True\n", + " )\n", + "print(tokenizer.decode(outputs[0], skip_special_tokens=True))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a28e8515", + "metadata": {}, + "outputs": [], + "source": [ + "import evaluate\n", + "from torch.utils.data import DataLoader\n", + "\n", + "dataloader = DataLoader(\n", + " processed_dataset[\"test\"].select(range(100)),\n", + " collate_fn=seq2seq_collator,\n", + " batch_size=4,\n", + " shuffle=False\n", + ")\n", + "\n", + "generated_translated = []\n", + "true_translated_ids = processed_dataset[\"test\"].select(range(100))[\"labels\"]\n", + "true_translated = tokenizer.batch_decode(true_translated_ids, skip_special_tokens=True)\n", + "\n", + "with torch.no_grad():\n", + " for batch in dataloader:\n", + " batch = batch.to(device)\n", + " output = model.generate(\n", + " **batch,\n", + " max_length=1026,\n", + " num_beams=4,\n", + " no_repeat_ngram_size=2,\n", + " early_stopping=True\n", + " )\n", + " batch_translated = tokenizer.batch_decode(output, skip_special_tokens=True)\n", + " generated_translated.extend(batch_translated)\n", + "\n", + "metric = evaluate.load(\"bleu\")\n", + "bleu_scores = metric.compute(\n", + " predictions=generated_translated,\n", + " references=true_translated\n", + ")\n", + "print(bleu_scores)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.23 ~ 3.26 LLaMA-3.1 \354\213\244\354\212\265.ipynb" "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.23 ~ 3.26 LLaMA-3.1 \354\213\244\354\212\265.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..c87a6fc2885eddf52d6102354474879b5f956169 --- /dev/null +++ "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.23 ~ 3.26 LLaMA-3.1 \354\213\244\354\212\265.ipynb" @@ -0,0 +1,175 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "7a8847de", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import torch\n", + "from transformers import BitsAndBytesConfig\n", + "from transformers import AutoTokenizer, AutoModelForCausalLM\n", + "\n", + "quantization_config = BitsAndBytesConfig(\n", + " load_in_4bit=True,\n", + " bnb_4bit_quant_type=\"nf4\",\n", + " bnb_4bit_compute_dtype=torch.float16,\n", + " bnb_4bit_use_double_quant=False\n", + ")\n", + "\n", + "token = \"hf_###...\" # 토큰 입력\n", + "model_name = \"meta-llama/Meta-Llama-3.1-8B-Instruct\"\n", + "\n", + "tokenizer = AutoTokenizer.from_pretrained(\n", + " model_name,\n", + " trust_remote_code=True,\n", + " token=token\n", + ")\n", + "model = AutoModelForCausalLM.from_pretrained(\n", + " model_name,\n", + " quantization_config=quantization_config,\n", + " device_map={\"\": 0},\n", + " token=token\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "949ec23a", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "model.eval()\n", + "\n", + "messages = [\n", + " {\"role\": \"user\", \"content\": \"안녕하세요.\"}\n", + "]\n", + "\n", + "input_ids = tokenizer.apply_chat_template(\n", + " messages,\n", + " add_generation_prompt=True,\n", + " return_tensors=\"pt\"\n", + ").to(model.device)\n", + "\n", + "with torch.no_grad():\n", + " outputs = model.generate(\n", + " input_ids,\n", + " max_new_tokens=256,\n", + " do_sample=True,\n", + " temperature=0.7,\n", + " top_p=0.9\n", + " )\n", + "\n", + "response = outputs[0][input_ids.shape[-1]:]\n", + "print(tokenizer.decode(response, skip_special_tokens=True))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "55a86b07", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "model.eval()\n", + "\n", + "messages = [\n", + " {\"role\": \"system\", \"content\": \"저는 위키북스의 챗봇 위키봇입니다.\"},\n", + " {\"role\": \"user\", \"content\": \"안녕하세요, 당신은 누구인가요?\"}\n", + "]\n", + "\n", + "input_ids = tokenizer.apply_chat_template(\n", + " messages,\n", + " add_generation_prompt=True,\n", + " return_tensors=\"pt\"\n", + ").to(model.device)\n", + "\n", + "with torch.no_grad():\n", + " outputs = model.generate(\n", + " input_ids,\n", + " max_new_tokens=256,\n", + " do_sample=True,\n", + " temperature=0.7,\n", + " top_p=0.9\n", + " )\n", + "\n", + "response = outputs[0][input_ids.shape[-1]:]\n", + "print(tokenizer.decode(response, skip_special_tokens=True))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9cc7c318", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "model.eval()\n", + "\n", + "messages = [\n", + " {\"role\": \"system\", \"content\": \"저는 위키북스의 챗봇 위키봇입니다.\"},\n", + " {\"role\": \"user\", \"content\": \"안녕하세요, 제 이름은 홍길동입니다.\"},\n", + " {\"role\": \"assistant\", \"content\": \"안녕하세요! 나는 위키북스의 챗봇, 위키봇입니다. 위키북스에서 다양한 지식을 공유하고 도울 목적으로 개발된 챗봇입니다. 제가 도와드릴 수 있는 다양한 주제와 질문에 대해 질문해 주세요!\"},\n", + " {\"role\": \"user\", \"content\": \"제 이름을 알고 있나요?\"}\n", + "]\n", + "\n", + "input_ids = tokenizer.apply_chat_template(\n", + " messages,\n", + " add_generation_prompt=True,\n", + " return_tensors=\"pt\"\n", + ").to(model.device)\n", + "\n", + "with torch.no_grad():\n", + " outputs = model.generate(\n", + " input_ids,\n", + " max_new_tokens=256,\n", + " do_sample=True,\n", + " temperature=0.7,\n", + " top_p=0.9\n", + " )\n", + "\n", + "response = outputs[0][input_ids.shape[-1]:]\n", + "print(tokenizer.decode(response, skip_special_tokens=True))" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.27 ~ 3.30 LLaMA-3.1 \353\257\270\354\204\270 \354\241\260\354\240\225.ipynb" "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.27 ~ 3.30 LLaMA-3.1 \353\257\270\354\204\270 \354\241\260\354\240\225.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..1ab0a7d63f15f310d93fc1ca3ef4cc5c0e885d72 --- /dev/null +++ "b/3\354\236\245 \354\236\220\354\227\260\354\226\264 \354\262\230\353\246\254/\354\230\210\354\240\234 3.27 ~ 3.30 LLaMA-3.1 \353\257\270\354\204\270 \354\241\260\354\240\225.ipynb" @@ -0,0 +1,170 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "e132c346", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import torch\n", + "from datasets import load_dataset\n", + "from transformers import BitsAndBytesConfig\n", + "from transformers import AutoTokenizer, AutoModelForCausalLM\n", + "\n", + "dataset = load_dataset(\"s076923/llama3-wikibook-ko\")\n", + "\n", + "quantization_config = BitsAndBytesConfig(\n", + " load_in_4bit=True,\n", + " bnb_4bit_quant_type=\"nf4\",\n", + " bnb_4bit_compute_dtype=torch.float16,\n", + " bnb_4bit_use_double_quant=False\n", + ")\n", + "\n", + "token = \"hf_###...\" # 토큰 입력\n", + "model_name = \"meta-llama/Meta-Llama-3.1-8B-Instruct\"\n", + "\n", + "tokenizer = AutoTokenizer.from_pretrained(\n", + " model_name,\n", + " trust_remote_code=True,\n", + " token=token\n", + ")\n", + "model = AutoModelForCausalLM.from_pretrained(\n", + " model_name,\n", + " quantization_config=quantization_config,\n", + " device_map={\"\": 0},\n", + " token=token\n", + ")\n", + "\n", + "tokenizer.pad_token = tokenizer.eos_token\n", + "model.config.use_cache = False\n", + "\n", + "print(dataset)\n", + "print(dataset[\"train\"][\"text\"][7])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "92732a20", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from peft import LoraConfig\n", + "\n", + "peft_config = LoraConfig(\n", + " r=128,\n", + " lora_alpha=4,\n", + " lora_dropout=0.1,\n", + " task_type=\"CAUSAL_LM\"\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8bd9a04f", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from transformers import TrainingArguments\n", + "from trl import SFTTrainer\n", + "\n", + "training_args = TrainingArguments(\n", + " output_dir=\"LLaMa-3.1\",\n", + " per_device_train_batch_size=1,\n", + " gradient_accumulation_steps=5,\n", + " learning_rate=2e-4,\n", + " max_steps=500,\n", + " warmup_steps=100,\n", + " logging_steps=100,\n", + " fp16=True,\n", + " optim=\"paged_adamw_8bit\",\n", + " seed=42\n", + ")\n", + "\n", + "trainer = SFTTrainer(\n", + " model=model,\n", + " tokenizer=tokenizer,\n", + " args=training_args,\n", + " peft_config=peft_config,\n", + " train_dataset=dataset[\"train\"],\n", + " dataset_text_field=\"text\",\n", + " max_seq_length=64\n", + ")\n", + "\n", + "trainer.train()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1a2d070b", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "model.eval()\n", + "\n", + "messages = [\n", + " {\"role\": \"user\", \"content\": \"위키북스 대표 저자는 누구예요?\"},\n", + "]\n", + "\n", + "input_ids = tokenizer.apply_chat_template(\n", + " messages,\n", + " add_generation_prompt=True,\n", + " return_tensors=\"pt\"\n", + ").to(model.device)\n", + "\n", + "with torch.no_grad():\n", + " outputs = model.generate(\n", + " input_ids,\n", + " max_new_tokens=64,\n", + " do_sample=True,\n", + " temperature=0.2,\n", + " top_p=0.95,\n", + " no_repeat_ngram_size=2\n", + " )\n", + "\n", + "response = outputs[0][input_ids.shape[-1]:]\n", + "print(tokenizer.decode(response, skip_special_tokens=True))" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/4\354\236\245 \354\273\264\355\223\250\355\204\260 \353\271\204\354\240\204/\354\230\210\354\240\234 4.01 ~ 4.03 \354\240\234\353\241\234\354\203\267 \354\235\264\353\257\270\354\247\200 \353\266\204\353\245\230.ipynb" "b/4\354\236\245 \354\273\264\355\223\250\355\204\260 \353\271\204\354\240\204/\354\230\210\354\240\234 4.01 ~ 4.03 \354\240\234\353\241\234\354\203\267 \354\235\264\353\257\270\354\247\200 \353\266\204\353\245\230.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..c7774874231d52ef16960763918b9ecb6f351c80 --- /dev/null +++ "b/4\354\236\245 \354\273\264\355\223\250\355\204\260 \353\271\204\354\240\204/\354\230\210\354\240\234 4.01 ~ 4.03 \354\240\234\353\241\234\354\203\267 \354\235\264\353\257\270\354\247\200 \353\266\204\353\245\230.ipynb" @@ -0,0 +1,124 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "e41fc33d", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "from transformers import CLIPProcessor, CLIPModel\n", + "\n", + "model_name = \"openai/clip-vit-base-patch32\"\n", + "processor = CLIPProcessor.from_pretrained(model_name)\n", + "model = CLIPModel.from_pretrained(model_name)\n", + "\n", + "dataset = load_dataset(\"sasha/dog-food\")\n", + "images = dataset[\"test\"][\"image\"][:2]\n", + "labels = [\"dog\", \"food\"]\n", + "inputs = processor(images=images, text=labels, return_tensors=\"pt\") # padding=True\n", + "\n", + "print(\"input_ids :\", inputs[\"input_ids\"])\n", + "print(\"attention_mask :\", inputs[\"attention_mask\"])\n", + "print(\"pixel_values :\", inputs[\"pixel_values\"])\n", + "print(\"image_shape :\", inputs[\"pixel_values\"].shape)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ac651f7b", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import torch\n", + "\n", + "model.eval()\n", + "with torch.no_grad():\n", + " outputs = model(**inputs)\n", + " logits_per_image = outputs.logits_per_image\n", + " probs = logits_per_image.softmax(dim=1)\n", + " print(\"outputs :\", outputs.keys())\n", + " print(\"logits_per_image :\", logits_per_image)\n", + " print(\"probs :\", probs)\n", + "\n", + "for idx, prob in enumerate(probs):\n", + " print(f\"- Image #{idx}\")\n", + " for label, p in zip(labels, prob):\n", + " print(f\"{label} : {p:.4f}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0d98c946", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import evaluate\n", + "from torch.utils.data import DataLoader\n", + "\n", + "test_dataloader = DataLoader(\n", + " dataset[\"test\"],\n", + " batch_size=8,\n", + " collate_fn=lambda batch: (\n", + " [item[\"image\"] for item in batch],\n", + " [item[\"label\"] for item in batch],\n", + " ),\n", + ")\n", + "\n", + "metric = evaluate.load(\"accuracy\")\n", + "predictions, references = [], []\n", + "labels_names = dataset[\"test\"].features[\"label\"].names\n", + "\n", + "model.eval()\n", + "with torch.no_grad():\n", + " for images, labels in test_dataloader:\n", + " inputs = processor(images=images, text=labels_names, return_tensors=\"pt\")\n", + " outputs = model(**inputs)\n", + " probs = outputs.logits_per_image.softmax(dim=1)\n", + "\n", + " predictions += probs.argmax(dim=1).cpu().tolist()\n", + " references += labels\n", + "\n", + "results = metric.compute(predictions=predictions, references=references)\n", + "print(f\"클래스 목록 : {labels_names}\")\n", + "print(f\"정확도 : {results['accuracy']*100 :.2f}%\")" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/4\354\236\245 \354\273\264\355\223\250\355\204\260 \353\271\204\354\240\204/\354\230\210\354\240\234 4.04 ~ 4.11 \354\240\234\353\241\234\354\203\267 \352\260\235\354\262\264 \352\262\200\354\266\234.ipynb" "b/4\354\236\245 \354\273\264\355\223\250\355\204\260 \353\271\204\354\240\204/\354\230\210\354\240\234 4.04 ~ 4.11 \354\240\234\353\241\234\354\203\267 \352\260\235\354\262\264 \352\262\200\354\266\234.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..1f709ed2ec45e0ff0c2772166c0c8396d0c7bf5f --- /dev/null +++ "b/4\354\236\245 \354\273\264\355\223\250\355\204\260 \353\271\204\354\240\204/\354\230\210\354\240\234 4.04 ~ 4.11 \354\240\234\353\241\234\354\203\267 \352\260\235\354\262\264 \352\262\200\354\266\234.ipynb" @@ -0,0 +1,290 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "d3eaffd0", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import io\n", + "from PIL import Image\n", + "from datasets import load_dataset\n", + "from transformers import Owlv2Processor, Owlv2ForObjectDetection\n", + "\n", + "model_name = \"google/owlv2-base-patch16\"\n", + "processor = Owlv2Processor.from_pretrained(model_name)\n", + "model = Owlv2ForObjectDetection.from_pretrained(model_name)\n", + "\n", + "dataset = load_dataset(\"Francesco/animals-ij5d2\")\n", + "print(dataset)\n", + "print(dataset[\"test\"][0])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1f322644", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "images = dataset[\"test\"][\"image\"][:2]\n", + "categories = dataset[\"test\"].features[\"objects\"].feature[\"category\"].names\n", + "labels = [categories] * len(images)\n", + "inputs = processor(text=labels, images=images, return_tensors=\"pt\", padding=True)\n", + "\n", + "print(images)\n", + "print(labels)\n", + "print(\"input_ids :\", inputs[\"input_ids\"])\n", + "print(\"attention_mask :\", inputs[\"attention_mask\"])\n", + "print(\"pixel_values :\", inputs[\"pixel_values\"])\n", + "print(\"image_shape :\", inputs[\"pixel_values\"].shape)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fdc753ca", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import torch\n", + "\n", + "model.eval()\n", + "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", + "model.to(device)\n", + "\n", + "with torch.no_grad():\n", + " outputs = model(**inputs.to(device))\n", + " \n", + "print(outputs.keys())\n", + "print(\"logits :\", outputs.logits.shape)\n", + "print(\"objectness_logits :\", outputs.objectness_logits.shape)\n", + "print(\"pred_boxes :\", outputs.pred_boxes.shape)\n", + "print(\"class_embeds :\", outputs.class_embeds.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "24a01f43", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "shape = [dataset[\"test\"][:2][\"width\"], dataset[\"test\"][:2][\"height\"]]\n", + "target_sizes = list(map(list, zip(*shape)))\n", + "detections = processor.post_process_object_detection(\n", + " outputs=outputs, threshold=0.5, target_sizes=target_sizes\n", + ")\n", + "\n", + "print(target_sizes)\n", + "print(detections)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ab07c0bd", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt\n", + "from PIL import ImageDraw, ImageFont\n", + "\n", + "for idx, (image, detect) in enumerate(zip(images, detections)):\n", + " im = image.copy()\n", + " draw = ImageDraw.Draw(im)\n", + " font = ImageFont.truetype(\"arial.ttf\", 36)\n", + "\n", + " for box, score, label in zip(detect[\"boxes\"], detect[\"scores\"], detect[\"labels\"]):\n", + " box = [round(i, 2) for i in box.tolist()]\n", + " draw.rectangle(box, outline=\"red\", width=3)\n", + " \n", + " label_text = f\"{labels[idx][label]}: {round(score.item(), 3)}\"\n", + " draw.text((box[0], box[1]), label_text, fill=\"red\", font=font)\n", + "\n", + " plt.imshow(im)\n", + " plt.axis(\"off\")\n", + " plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9d9c46ef", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "!pip install pycocotools" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "968261fa", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "def get_coco_annotations(dataset):\n", + " annotations = []\n", + " for data in dataset:\n", + " image_id = int(data[\"image_id\"])\n", + " objects = data[\"objects\"]\n", + " for idx in range(len(objects[\"id\"])):\n", + " annotations.append(\n", + " {\n", + " \"image_id\": image_id,\n", + " \"category_id\": int(objects[\"category\"][idx]),\n", + " \"bbox\": [float(coord) for coord in objects[\"bbox\"][idx]],\n", + " \"area\": float(objects[\"area\"][idx]),\n", + " \"id\": int(objects[\"id\"][idx]),\n", + " \"iscrowd\": 0\n", + " }\n", + " )\n", + " return annotations\n", + "\n", + "coco_annotations = get_coco_annotations(dataset[\"test\"])\n", + "coco_annotation_format = {\n", + " \"annotations\": coco_annotations,\n", + " \"images\": [{\"id\": int(data[\"image_id\"])} for data in dataset[\"test\"]],\n", + " \"categories\": [{\"id\": i, \"name\": name} for i, name in enumerate(categories)]\n", + "}\n", + "print(len(coco_annotations))\n", + "print(coco_annotation_format[\"annotations\"][0])\n", + "print(coco_annotation_format[\"images\"][0])\n", + "print(coco_annotation_format[\"categories\"][0])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "84ab7984", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from torch.utils.data import DataLoader\n", + "\n", + "dataloader = DataLoader(\n", + " dataset[\"test\"],\n", + " batch_size=2,\n", + " collate_fn=lambda batch: (\n", + " [item[\"image\"] for item in batch],\n", + " [list(item[\"image\"].size) for item in batch],\n", + " [item[\"image_id\"] for item in batch],\n", + " [item[\"objects\"] for item in batch]\n", + " )\n", + ")\n", + "\n", + "predictions = []\n", + "model.eval()\n", + "with torch.no_grad():\n", + " for images, target_sizes, image_ids, objects in dataloader:\n", + " input_labels = [categories] * len(images)\n", + " inputs = processor(images=images, text=input_labels, return_tensors=\"pt\")\n", + " outputs = model(**inputs.to(device))\n", + " detections = processor.post_process_object_detection(\n", + " outputs=outputs, threshold=0.3, target_sizes=target_sizes\n", + " )\n", + "\n", + " for batch_idx, detection in enumerate(detections):\n", + " category_ids = detection[\"labels\"].cpu().numpy().tolist()\n", + " scores = detection[\"scores\"].cpu().numpy().tolist()\n", + " boxes = detection[\"boxes\"].cpu().numpy()\n", + "\n", + " boxes[:, 2:4] -= boxes[:, :2]\n", + " boxes = boxes.tolist()\n", + "\n", + " for obj_idx, box in enumerate(boxes):\n", + " prediction = {\n", + " \"image_id\": image_ids[batch_idx],\n", + " \"category_id\": category_ids[obj_idx],\n", + " \"bbox\": box,\n", + " \"score\": scores[obj_idx]\n", + " }\n", + " predictions.append(prediction)\n", + "\n", + "print(len(predictions))\n", + "print(predictions[0])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dcda78ac", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from pycocotools.coco import COCO\n", + "from pycocotools.cocoeval import COCOeval\n", + "\n", + "coco_gt = COCO()\n", + "coco_gt.dataset = coco_annotation_format\n", + "coco_gt.createIndex()\n", + "\n", + "coco_dt = coco_gt.loadRes(predictions)\n", + "\n", + "coco_eval = COCOeval(coco_gt, coco_dt, \"bbox\")\n", + "coco_eval.evaluate()\n", + "coco_eval.accumulate()\n", + "coco_eval.summarize()" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/4\354\236\245 \354\273\264\355\223\250\355\204\260 \353\271\204\354\240\204/\354\230\210\354\240\234 4.12 ~ 4.17 \354\235\264\353\257\270\354\247\200 \354\204\270\352\267\270\353\250\274\355\205\214\354\235\264\354\205\230.ipynb" "b/4\354\236\245 \354\273\264\355\223\250\355\204\260 \353\271\204\354\240\204/\354\230\210\354\240\234 4.12 ~ 4.17 \354\235\264\353\257\270\354\247\200 \354\204\270\352\267\270\353\250\274\355\205\214\354\235\264\354\205\230.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..ee424e77365f424411cb724dff4a809469425542 --- /dev/null +++ "b/4\354\236\245 \354\273\264\355\223\250\355\204\260 \353\271\204\354\240\204/\354\230\210\354\240\234 4.12 ~ 4.17 \354\235\264\353\257\270\354\247\200 \354\204\270\352\267\270\353\250\274\355\205\214\354\235\264\354\205\230.ipynb" @@ -0,0 +1,314 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "9d9c46ef", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import io\n", + "from PIL import Image\n", + "from datasets import load_dataset\n", + "from transformers import SamProcessor, SamModel\n", + "\n", + "def filter_category(data):\n", + " # 16 = dog\n", + " # 23 = giraffe\n", + " return 16 in data[\"objects\"][\"category\"] or 23 in data[\"objects\"][\"category\"]\n", + "\n", + "def convert_image(data):\n", + " byte = io.BytesIO(data[\"image\"][\"bytes\"])\n", + " img = Image.open(byte)\n", + " return {\"img\": img}\n", + "\n", + "model_name = \"facebook/sam-vit-base\"\n", + "processor = SamProcessor.from_pretrained(model_name) \n", + "model = SamModel.from_pretrained(model_name)\n", + "\n", + "dataset = load_dataset(\"s076923/coco-val\")\n", + "filtered_dataset = dataset[\"validation\"].filter(filter_category)\n", + "converted_dataset = filtered_dataset.map(convert_image, remove_columns=[\"image\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4eece5f7", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "from matplotlib import pyplot as plt\n", + "\n", + "\n", + "def show_point_box(image, input_points, input_labels, input_boxes=None, marker_size=375):\n", + " plt.figure(figsize=(10, 10))\n", + " plt.imshow(image)\n", + " ax = plt.gca()\n", + " \n", + " input_points = np.array(input_points)\n", + " input_labels = np.array(input_labels)\n", + "\n", + " pos_points = input_points[input_labels[0] == 1]\n", + " neg_points = input_points[input_labels[0] == 0]\n", + " \n", + " ax.scatter(\n", + " pos_points[:, 0],\n", + " pos_points[:, 1],\n", + " color=\"green\",\n", + " marker=\"*\",\n", + " s=marker_size,\n", + " edgecolor=\"white\",\n", + " linewidth=1.25\n", + " )\n", + " ax.scatter(\n", + " neg_points[:, 0],\n", + " neg_points[:, 1],\n", + " color=\"red\",\n", + " marker=\"*\",\n", + " s=marker_size,\n", + " edgecolor=\"white\",\n", + " linewidth=1.25\n", + " )\n", + "\n", + " if input_boxes is not None:\n", + " for box in input_boxes:\n", + " x0, y0 = box[0], box[1]\n", + " w, h = box[2] - box[0], box[3] - box[1]\n", + " ax.add_patch(\n", + " plt.Rectangle(\n", + " (x0, y0), w, h, edgecolor=\"green\", facecolor=(0, 0, 0, 0), lw=2\n", + " )\n", + " )\n", + "\n", + " plt.axis(\"on\")\n", + " plt.show()\n", + "\n", + "\n", + "image = converted_dataset[0][\"img\"]\n", + "input_points = [[[250, 200]]]\n", + "input_labels = [[[1]]]\n", + "\n", + "show_point_box(image, input_points[0], input_labels[0])\n", + "inputs = processor(\n", + " image, input_points=input_points, input_labels=input_labels, return_tensors=\"pt\"\n", + ")\n", + "\n", + "print(\"input_points shape :\", inputs[\"input_points\"].shape)\n", + "print(\"input_points :\", inputs[\"input_points\"])\n", + "print(\"input_labels shape :\", inputs[\"input_labels\"].shape)\n", + "print(\"input_labels :\", inputs[\"input_labels\"])\n", + "print(\"pixel_values shape :\", inputs[\"pixel_values\"].shape)\n", + "print(\"pixel_values :\", inputs[\"pixel_values\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5116671c", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import torch\n", + "\n", + "\n", + "def show_mask(mask, ax, random_color=False):\n", + " if random_color:\n", + " color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)\n", + " else:\n", + " color = np.array([30 / 255, 144 / 255, 255 / 255, 0.6])\n", + " h, w = mask.shape[-2:]\n", + " mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)\n", + " ax.imshow(mask_image)\n", + "\n", + "\n", + "def show_masks_on_image(raw_image, masks, scores):\n", + " if len(masks.shape) == 4:\n", + " masks = masks.squeeze()\n", + " if scores.shape[0] == 1:\n", + " scores = scores.squeeze()\n", + "\n", + " nb_predictions = scores.shape[-1]\n", + " fig, axes = plt.subplots(1, nb_predictions, figsize=(30, 15))\n", + "\n", + " for i, (mask, score) in enumerate(zip(masks, scores)):\n", + " mask = mask.cpu().detach()\n", + " axes[i].imshow(np.array(raw_image))\n", + " show_mask(mask, axes[i])\n", + " axes[i].title.set_text(f\"Mask {i+1}, Score: {score.item():.3f}\")\n", + " axes[i].axis(\"off\")\n", + " plt.show()\n", + "\n", + "\n", + "model.eval()\n", + "with torch.no_grad():\n", + " outputs = model(**inputs)\n", + "\n", + "masks = processor.image_processor.post_process_masks(\n", + " outputs.pred_masks.cpu(),\n", + " inputs[\"original_sizes\"].cpu(),\n", + " inputs[\"reshaped_input_sizes\"].cpu(),\n", + ")\n", + "\n", + "show_masks_on_image(image, masks[0], outputs.iou_scores)\n", + "print(\"iou_scores shape :\", outputs.iou_scores.shape)\n", + "print(\"iou_scores :\", outputs.iou_scores)\n", + "print(\"pred_masks shape :\", outputs.pred_masks.shape)\n", + "print(\"pred_masks :\", outputs.pred_masks)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f64bc619", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "input_points = [[[250, 200], [15, 50]]]\n", + "input_labels = [[[0, 1]]]\n", + "input_boxes = [[[100, 100, 400, 600]]]\n", + "\n", + "show_point_box(image, input_points[0], input_labels[0], input_boxes[0])\n", + "inputs = processor(\n", + " image,\n", + " input_points=input_points,\n", + " input_labels=input_labels,\n", + " input_boxes=input_boxes,\n", + " return_tensors=\"pt\"\n", + ")\n", + "\n", + "model.eval()\n", + "with torch.no_grad():\n", + " outputs = model(**inputs)\n", + "\n", + "masks = processor.image_processor.post_process_masks(\n", + " outputs.pred_masks.cpu(),\n", + " inputs[\"original_sizes\"].cpu(),\n", + " inputs[\"reshaped_input_sizes\"].cpu(),\n", + ")\n", + "\n", + "show_masks_on_image(image, masks[0], outputs.iou_scores)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dfe37f93", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from transformers import pipeline\n", + "\n", + "generator = pipeline(\"mask-generation\", model=model_name)\n", + "outputs = generator(image, points_per_batch=32)\n", + "\n", + "plt.imshow(np.array(image))\n", + "ax = plt.gca()\n", + "for mask in outputs[\"masks\"]:\n", + " show_mask(mask, ax=ax, random_color=True)\n", + "plt.axis(\"off\")\n", + "plt.show()\n", + "\n", + "print(\"outputs mask의 개수 :\", len(outputs[\"masks\"]))\n", + "print(\"outputs scores의 개수 :\", len(outputs[\"scores\"]))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5277bdad", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "detector = pipeline(\n", + " model=\"google/owlv2-base-patch16\", task=\"zero-shot-object-detection\"\n", + ")\n", + "\n", + "image = converted_dataset[24][\"img\"]\n", + "labels = [\"dog\", \"giraffe\"]\n", + "results = detector(image, candidate_labels=labels, threshold=0.5)\n", + "\n", + "input_boxes = []\n", + "for result in results:\n", + " input_boxes.append(\n", + " [\n", + " result[\"box\"][\"xmin\"],\n", + " result[\"box\"][\"ymin\"],\n", + " result[\"box\"][\"xmax\"],\n", + " result[\"box\"][\"ymax\"]\n", + " ]\n", + " )\n", + " print(result)\n", + "\n", + "inputs = processor(image, input_boxes=[input_boxes], return_tensors=\"pt\")\n", + "\n", + "model.eval()\n", + "with torch.no_grad():\n", + " outputs = model(**inputs)\n", + "\n", + "masks = processor.image_processor.post_process_masks(\n", + " outputs.pred_masks.cpu(),\n", + " inputs[\"original_sizes\"].cpu(),\n", + " inputs[\"reshaped_input_sizes\"].cpu()\n", + ")\n", + "\n", + "plt.imshow(np.array(image))\n", + "ax = plt.gca()\n", + "\n", + "for mask, iou in zip(masks[0], outputs.iou_scores[0]):\n", + " max_iou_idx = torch.argmax(iou)\n", + " best_mask = mask[max_iou_idx]\n", + " show_mask(best_mask, ax=ax, random_color=True)\n", + "\n", + "plt.axis(\"off\")\n", + "plt.show()" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.01 ~ 5.02 BLIP-2 \353\252\250\353\215\270 \352\265\254\354\241\260.ipynb" "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.01 ~ 5.02 BLIP-2 \353\252\250\353\215\270 \352\265\254\354\241\260.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..625be59ad2d371b41c6f857625d1bbde62adcf45 --- /dev/null +++ "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.01 ~ 5.02 BLIP-2 \353\252\250\353\215\270 \352\265\254\354\241\260.ipynb" @@ -0,0 +1,60 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "da11820c", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import Blip2Config\n", + "\n", + "model_name = \"Salesforce/blip2-opt-2.7b\"\n", + "config = Blip2Config.from_pretrained(model_name)\n", + "print(config)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6d3977dd", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import Blip2ForConditionalGeneration\n", + "\n", + "model = Blip2ForConditionalGeneration(config)\n", + "\n", + "for main_name, main_module in model.named_children():\n", + " print(main_name)\n", + " for sub_name, sub_module in main_module.named_children():\n", + " print(\"└\", sub_name)\n", + " for ssub_name, ssub_module in sub_module.named_children():\n", + " print(\"│ └\", ssub_name)\n", + " for sssub_name, sssub_module in ssub_module.named_children():\n", + " print(\"│ └\", sssub_name)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.03 ~ 5.05 BLIP-2 \353\252\250\353\215\270 \354\236\221\353\217\231 \353\260\251\354\213\235.ipynb" "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.03 ~ 5.05 BLIP-2 \353\252\250\353\215\270 \354\236\221\353\217\231 \353\260\251\354\213\235.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..95934c1440d7463aabe96fce928ffd7cc7423479 --- /dev/null +++ "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.03 ~ 5.05 BLIP-2 \353\252\250\353\215\270 \354\236\221\353\217\231 \353\260\251\354\213\235.ipynb" @@ -0,0 +1,120 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "6d3977dd", + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "from datasets import load_dataset\n", + "from transformers import Blip2Processor, Blip2ForConditionalGeneration\n", + "\n", + "model_name = \"Salesforce/blip2-opt-2.7b\"\n", + "processor = Blip2Processor.from_pretrained(model_name)\n", + "model = Blip2ForConditionalGeneration.from_pretrained(\n", + " model_name, torch_dtype=torch.float16, device_map=\"auto\"\n", + ")\n", + "\n", + "dataset = load_dataset(\"huggingface/cats-image\")\n", + "image = dataset[\"test\"][\"image\"][0]\n", + "\n", + "inputs = processor(images=image, return_tensors=\"pt\").to(\n", + " model.device, dtype=torch.float16\n", + ")\n", + "image_embeds = model.vision_model(\n", + " inputs[\"pixel_values\"], return_dict=True\n", + ").last_hidden_state\n", + "\n", + "print(model.vision_model)\n", + "print(image_embeds)\n", + "print(image_embeds.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b9e4454f", + "metadata": {}, + "outputs": [], + "source": [ + "image_attention_mask = torch.ones(\n", + " image_embeds.size()[:-1], dtype=torch.long, device=model.device\n", + ")\n", + "query_tokens = model.query_tokens.expand(image_embeds.shape[0], -1, -1)\n", + "\n", + "query_outputs = model.qformer(\n", + " query_embeds=query_tokens,\n", + " encoder_hidden_states=image_embeds,\n", + " encoder_attention_mask=image_attention_mask,\n", + " return_dict=True\n", + ")\n", + "query_output = query_outputs.last_hidden_state\n", + "\n", + "print(image_attention_mask.shape)\n", + "print(query_tokens.shape)\n", + "print(query_output.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "53afdc60", + "metadata": {}, + "outputs": [], + "source": [ + "language_model_inputs = model.language_projection(query_output)\n", + "language_attention_mask = torch.ones(\n", + " language_model_inputs.size()[:-1],\n", + " dtype=torch.long,\n", + " device=model.device\n", + ")\n", + "input_ids = (\n", + " torch.LongTensor([[model.config.text_config.bos_token_id]])\n", + " .repeat(inputs[\"pixel_values\"].shape[0], 1)\n", + " .to(model.device)\n", + ")\n", + "attention_mask = torch.ones_like(input_ids)\n", + "attention_mask = torch.cat(\n", + " [language_attention_mask, attention_mask.to(model.device)], dim=1\n", + ")\n", + "\n", + "print(language_model_inputs.shape)\n", + "print(input_ids)\n", + "\n", + "inputs_embeds = model.get_input_embeddings()(input_ids)\n", + "inputs_embeds = torch.cat(\n", + " [language_model_inputs, inputs_embeds.to(model.device)], dim=1\n", + ")\n", + "\n", + "outputs = model.language_model.generate(\n", + " inputs_embeds=inputs_embeds, attention_mask=attention_mask, max_length=50\n", + ")\n", + "print(outputs)\n", + "print(outputs.shape)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.06 ~ 5.07 \354\235\264\353\257\270\354\247\200 \354\272\241\354\205\224\353\213\235.ipynb" "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.06 ~ 5.07 \354\235\264\353\257\270\354\247\200 \354\272\241\354\205\224\353\213\235.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..65c39d01fc4c14ee27a83de5799687a2725a684e --- /dev/null +++ "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.06 ~ 5.07 \354\235\264\353\257\270\354\247\200 \354\272\241\354\205\224\353\213\235.ipynb" @@ -0,0 +1,76 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "f3c6f204", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import torch\n", + "from datasets import load_dataset\n", + "from transformers import Blip2Processor, Blip2ForConditionalGeneration\n", + "\n", + "model_name = \"Salesforce/blip2-opt-2.7b\"\n", + "processor = Blip2Processor.from_pretrained(model_name)\n", + "model = Blip2ForConditionalGeneration.from_pretrained(\n", + " model_name, torch_dtype=torch.float16, device_map=\"auto\"\n", + ")\n", + "\n", + "dataset = load_dataset(\"huggingface/cats-image\")\n", + "image = dataset[\"test\"][\"image\"][0]\n", + "\n", + "inputs = processor(images=image, return_tensors=\"pt\").to(model.device, torch.float16)\n", + "generated_ids = model.generate(**inputs, max_length=50)\n", + "print(generated_ids)\n", + "\n", + "generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()\n", + "print(generated_text)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4b26083c", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "prompt = \"Question: Describe the location of the image. Answer:\"\n", + "inputs = processor(images=image, text=prompt, return_tensors=\"pt\").to(model.device, dtype=torch.float16)\n", + "\n", + "generated_ids = model.generate(**inputs, max_length=50)\n", + "generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()\n", + "print(generated_text)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.08 ~ 5.09 LayoutLMv3 \353\252\250\353\215\270 \352\265\254\354\241\260.ipynb" "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.08 ~ 5.09 LayoutLMv3 \353\252\250\353\215\270 \352\265\254\354\241\260.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..318055e75f056a74057dbacdabf6bae1e6e38322 --- /dev/null +++ "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.08 ~ 5.09 LayoutLMv3 \353\252\250\353\215\270 \352\265\254\354\241\260.ipynb" @@ -0,0 +1,68 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "f3c6f204", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from transformers import LayoutLMv3Config\n", + "\n", + "model_name = \"microsoft/layoutlmv3-base\"\n", + "config = LayoutLMv3Config(model_name)\n", + "print(config)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4b26083c", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from transformers import LayoutLMv3Model\n", + "\n", + "model = LayoutLMv3Model.from_pretrained(model_name)\n", + "\n", + "for main_name, main_module in model.named_children():\n", + " print(main_name)\n", + " for sub_name, sub_module in main_module.named_children():\n", + " print(\"└\", sub_name)\n", + " for ssub_name, ssub_module in sub_module.named_children():\n", + " print(\"│ └\", ssub_name)\n", + " for sssub_name, sssub_module in ssub_module.named_children():\n", + " print(\"│ └\", sssub_name)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.10 ~ 5.15 \353\254\270\354\204\234 \354\213\234\352\260\201 \354\247\210\354\235\230 \354\235\221\353\213\265.ipynb" "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.10 ~ 5.15 \353\254\270\354\204\234 \354\213\234\352\260\201 \354\247\210\354\235\230 \354\235\221\353\213\265.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..aeb3a95894992c6ed8200b2c8f310bb2517a86b6 --- /dev/null +++ "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.10 ~ 5.15 \353\254\270\354\204\234 \354\213\234\352\260\201 \354\247\210\354\235\230 \354\235\221\353\213\265.ipynb" @@ -0,0 +1,343 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "22628136", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "!pip3 install pytesseract" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ddf62285", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "# # Windows\n", + "# import pytesseract\n", + "# pytesseract.pytesseract.tesseract_cmd = r\"<설치 경로>\\tesseract.exe\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e4bf6720", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "# # macOS\n", + "# brew install tesseract" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b0d808bd", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "# # Linux/Google Colab\n", + "# sudo apt install tesseract-ocr" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f3c6f204", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import io\n", + "from PIL import Image\n", + "from datasets import load_dataset\n", + "from transformers import LayoutLMv3FeatureExtractor\n", + "\n", + "def get_ocr_words_and_boxes(sample):\n", + " image_bytes = io.BytesIO(sample[\"image\"])\n", + " image = Image.open(image_bytes)\n", + "\n", + " encoded_inputs = feature_extractor(image)\n", + " sample[\"words\"] = encoded_inputs.words[0]\n", + " sample[\"boxes\"] = encoded_inputs.boxes[0]\n", + " sample[\"pixel_values\"] = encoded_inputs.pixel_values[0]\n", + " return sample\n", + "\n", + "dataset = load_dataset(\"s076923/docvqa-train\")\n", + "model_name = \"microsoft/layoutlmv3-base\"\n", + "feature_extractor = LayoutLMv3FeatureExtractor(model_name)\n", + "dataset_with_ocr = dataset[\"train\"].map(get_ocr_words_and_boxes)\n", + "\n", + "print(dataset_with_ocr[1].keys())\n", + "print(\"question :\", dataset_with_ocr[1][\"question\"])\n", + "print(\"answers :\", dataset_with_ocr[1][\"answers\"])\n", + "print(\"words :\", dataset_with_ocr[1][\"words\"])\n", + "print(\"boxes :\", dataset_with_ocr[1][\"boxes\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4b26083c", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "def find_sublist(word_list, target_list):\n", + " word_list = [word.lower() for word in word_list]\n", + " target_list = target_list.lower().split()\n", + "\n", + " for i in range(len(word_list) - len(target_list) + 1):\n", + " if word_list[i : i + len(target_list)] == target_list:\n", + " return target_list, i, i + len(target_list) - 1\n", + " return None, 0, 0\n", + "\n", + "question = dataset_with_ocr[10][\"question\"]\n", + "words = dataset_with_ocr[10][\"words\"]\n", + "answers = dataset_with_ocr[10][\"answers\"]\n", + "print(question)\n", + "print(words)\n", + "print(answers)\n", + "print()\n", + "\n", + "for answer in answers:\n", + " match, word_idx_start, word_idx_end = find_sublist(words, answer)\n", + " print(\"Match :\", match)\n", + " print(\"Word idx start :\", word_idx_start)\n", + " print(\"Word idx end :\", word_idx_end)\n", + " print()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "842dce7b", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from transformers import LayoutLMv3TokenizerFast\n", + "from datasets import Features, Sequence, Value, Array2D, Array3D\n", + "\n", + "def find_answer_match(words, answers):\n", + " for answer in answers:\n", + " match, word_idx_start, word_idx_end = find_sublist(words, answer)\n", + " if match:\n", + " return match, word_idx_start, word_idx_end\n", + "\n", + " for answer in answers:\n", + " for i in range(len(answer)):\n", + " answer_modified = answer[:i] + answer[i + 1 :]\n", + " match, word_idx_start, word_idx_end = find_sublist(words, answer_modified)\n", + " if match:\n", + " return match, word_idx_start, word_idx_end\n", + "\n", + " return False, None, None\n", + "\n", + "def encode_dataset(examples, processor, max_length=512):\n", + " encoding = processor(\n", + " examples[\"question\"],\n", + " examples[\"words\"],\n", + " examples[\"boxes\"],\n", + " max_length=max_length,\n", + " padding=\"max_length\",\n", + " truncation=True\n", + " )\n", + "\n", + " cls_index = encoding.input_ids.index(processor.cls_token_id)\n", + " start_position = end_position = cls_index\n", + "\n", + " match, word_idx_start, word_idx_end = find_answer_match(\n", + " examples[\"words\"], examples[\"answers\"]\n", + " )\n", + "\n", + " if match:\n", + " sequence_ids = encoding.sequence_ids(0)\n", + " token_start_index = next(i for i, seq_id in enumerate(sequence_ids) if seq_id == 1)\n", + "\n", + " token_end_index = len(encoding.input_ids) - 1 - sequence_ids[::-1].index(1)\n", + " word_ids = encoding.word_ids()[token_start_index : token_end_index + 1]\n", + "\n", + " start_position = token_start_index + word_ids.index(word_idx_start)\n", + " end_position = token_end_index - word_ids[::-1].index(word_idx_end)\n", + "\n", + " encoding[\"image\"] = examples[\"pixel_values\"]\n", + " encoding[\"start_positions\"] = start_position\n", + " encoding[\"end_positions\"] = end_position\n", + " return encoding\n", + "\n", + "processor = LayoutLMv3TokenizerFast.from_pretrained(model_name)\n", + "encoded_dataset = dataset_with_ocr.map(\n", + " lambda x: encode_dataset(x, processor),\n", + " remove_columns=dataset_with_ocr.column_names,\n", + " features=Features(\n", + " {\n", + " \"input_ids\": Sequence(feature=Value(dtype=\"int64\")),\n", + " \"bbox\": Array2D(dtype=\"int64\", shape=(512, 4)),\n", + " \"attention_mask\": Sequence(Value(dtype=\"int64\")),\n", + " \"image\": Array3D(dtype=\"float32\", shape=(3, 224, 224)),\n", + " \"start_positions\": Value(dtype=\"int64\"),\n", + " \"end_positions\": Value(dtype=\"int64\")\n", + " }\n", + " )\n", + ")\n", + "print(encoded_dataset)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1744922d", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from transformers import TrainingArguments, Trainer\n", + "from transformers import LayoutLMv3ForQuestionAnswering\n", + "\n", + "model = LayoutLMv3ForQuestionAnswering.from_pretrained(model_name)\n", + "\n", + "training_args = TrainingArguments(\n", + " output_dir=\"DocVQA\",\n", + " num_train_epochs=20,\n", + " per_device_train_batch_size=4,\n", + " learning_rate=5e-5,\n", + " warmup_steps=100,\n", + " weight_decay=0.01,\n", + " logging_strategy=\"steps\",\n", + " logging_steps=20,\n", + " seed=42\n", + ")\n", + "\n", + "trainer = Trainer(\n", + " model=model,\n", + " args=training_args,\n", + " train_dataset=encoded_dataset\n", + ")\n", + "\n", + "trainer.train()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6453ba0f", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import torch\n", + "from transformers import LayoutLMv3Processor\n", + "\n", + "index = 5\n", + "processor = LayoutLMv3Processor.from_pretrained(model_name)\n", + "\n", + "image_bytes = io.BytesIO(dataset_with_ocr[index][\"image\"])\n", + "image = Image.open(image_bytes)\n", + "\n", + "full_text = processor.decode(encoded_dataset[\"input_ids\"][index])\n", + "print(\"Full text:\", full_text)\n", + "\n", + "question = dataset_with_ocr[index][\"question\"]\n", + "print(\"Question:\", question)\n", + "\n", + "start_position = encoded_dataset[\"start_positions\"][index]\n", + "end_position = encoded_dataset[\"end_positions\"][index]\n", + "answer = processor.decode(\n", + " encoded_dataset[\"input_ids\"][index][start_position : end_position + 1]\n", + ")\n", + "print(\"Answer:\", answer)\n", + "\n", + "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", + "encoded_inputs = processor(image, question, return_tensors=\"pt\")\n", + "encoded_inputs = {k: v.to(device) for k, v in encoded_inputs.items()}\n", + "print(\"Encoded input keys:\", encoded_inputs.keys())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "038dbe71", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "model.to(device)\n", + "model.eval()\n", + "\n", + "with torch.no_grad():\n", + " outputs = model(**encoded_inputs)\n", + "\n", + "start_logits, end_logits = outputs.start_logits, outputs.end_logits\n", + "start_index = start_logits.argmax(-1).item()\n", + "end_index = end_logits.argmax(-1).item()\n", + "predicted_answer = processor.decode(\n", + " encoded_inputs[\"input_ids\"].squeeze()[start_index : end_index + 1]\n", + ")\n", + "\n", + "print(\"Predicted start_index:\", start_index)\n", + "print(\"Predicted end_index:\", end_index)\n", + "print(\"predicted_answer:\", predicted_answer)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.16 ~ 5.17 ViLT \353\252\250\353\215\270 \352\265\254\354\241\260 copy.ipynb" "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.16 ~ 5.17 ViLT \353\252\250\353\215\270 \352\265\254\354\241\260 copy.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..ef523fa4b78fb168810fc91d4859407800cc4dc0 --- /dev/null +++ "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.16 ~ 5.17 ViLT \353\252\250\353\215\270 \352\265\254\354\241\260 copy.ipynb" @@ -0,0 +1,60 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "da11820c", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import ViltConfig\n", + "\n", + "model_name = \"dandelin/vilt-b32-mlm\"\n", + "config = ViltConfig.from_pretrained(model_name)\n", + "print(config)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6d3977dd", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import ViltModel\n", + "\n", + "model = ViltModel.from_pretrained(model_name)\n", + "\n", + "for main_name, main_module in model.named_children():\n", + " print(main_name)\n", + " for sub_name, sub_module in main_module.named_children():\n", + " print(\"└\", sub_name)\n", + " for ssub_name, ssub_module in sub_module.named_children():\n", + " print(\"│ └\", ssub_name)\n", + " for sssub_name, sssub_module in ssub_module.named_children():\n", + " print(\"│ └\", sssub_name)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.18 ~ 5.24 \354\213\234\352\260\201\354\240\201 \354\247\210\354\235\230 \354\235\221\353\213\265.ipynb" "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.18 ~ 5.24 \354\213\234\352\260\201\354\240\201 \354\247\210\354\235\230 \354\235\221\353\213\265.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..6d4727ea7b4278a092dfbf4308aff1e78a6b5c45 --- /dev/null +++ "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.18 ~ 5.24 \354\213\234\352\260\201\354\240\201 \354\247\210\354\235\230 \354\235\221\353\213\265.ipynb" @@ -0,0 +1,301 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "6629aab6", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "from transformers import ViltConfig\n", + "\n", + "def preprocess_data(example, config):\n", + " answers = example[\"answers\"]\n", + " answer_counts = {}\n", + " for answer_dict in answers:\n", + " answer_text = answer_dict[\"answer\"]\n", + " answer_counts[answer_text] = answer_counts.get(answer_text, 0) + 1\n", + "\n", + " labels = []\n", + " scores = []\n", + " for answer_text, count in answer_counts.items():\n", + " if answer_text not in config.label2id:\n", + " continue\n", + " labels.append(config.label2id[answer_text])\n", + " score = min(1.0, count / 3)\n", + " scores.append(score)\n", + "\n", + " example[\"labels\"] = labels\n", + " example[\"scores\"] = scores\n", + " return example\n", + "\n", + "config_name = \"dandelin/vilt-b32-finetuned-vqa\"\n", + "config = ViltConfig.from_pretrained(config_name)\n", + "\n", + "dataset = load_dataset(\"s076923/vqa-v2-test\")\n", + "processed_dataset = dataset[\"test\"].map(\n", + " lambda example: preprocess_data(example, config),\n", + " batched=False\n", + ")\n", + "print(processed_dataset[0])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "25001093", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import torch\n", + "from torch.utils.data import Dataset\n", + "from transformers import ViltProcessor\n", + "\n", + "class VQADataset(Dataset):\n", + " def __init__(self, dataset, config, processor):\n", + " self.dataset = dataset\n", + " self.config = config\n", + " self.processor = processor\n", + "\n", + " def __len__(self):\n", + " return len(self.dataset)\n", + "\n", + " def __getitem__(self, idx):\n", + " data = self.dataset[idx]\n", + "\n", + " encoding = self.processor(\n", + " images=data[\"image\"],\n", + " text=data[\"question\"],\n", + " padding=\"max_length\",\n", + " truncation=True,\n", + " return_tensors=\"pt\"\n", + " )\n", + " encoding = {k: v[0] for k, v in encoding.items()}\n", + "\n", + " targets = torch.zeros(len(self.config.id2label))\n", + " targets[data[\"labels\"]] = torch.tensor(data[\"scores\"])\n", + " encoding[\"labels\"] = targets\n", + "\n", + " return encoding\n", + "\n", + "model_name = \"dandelin/vilt-b32-mlm\"\n", + "processor = ViltProcessor.from_pretrained(model_name)\n", + "vqa_dataset = VQADataset(dataset=processed_dataset, config=config, processor=processor)\n", + "\n", + "print(vqa_dataset[0].keys())\n", + "print(processor.decode(vqa_dataset[0][\"input_ids\"]))\n", + "labels = torch.nonzero(vqa_dataset[0][\"labels\"]).squeeze().tolist()\n", + "print([config.id2label[label] for label in labels])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "96740a5b", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from transformers import ViltForQuestionAnswering\n", + "\n", + "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", + "model = ViltForQuestionAnswering.from_pretrained(\n", + " model_name, id2label=config.id2label, label2id=config.label2id\n", + ").to(device)\n", + "print(model.classifier)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3e966b16", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from torch.utils.data import DataLoader\n", + "\n", + "def collate_fn(batch):\n", + " input_ids = [item[\"input_ids\"] for item in batch]\n", + " pixel_values = [item[\"pixel_values\"] for item in batch]\n", + " attention_mask = [item[\"attention_mask\"] for item in batch]\n", + " token_type_ids = [item[\"token_type_ids\"] for item in batch]\n", + " labels = [item[\"labels\"] for item in batch]\n", + " \n", + " encoding = processor.image_processor.pad(pixel_values, return_tensors=\"pt\")\n", + "\n", + " batch = {\n", + " \"input_ids\": torch.stack(input_ids),\n", + " \"attention_mask\": torch.stack(attention_mask),\n", + " \"token_type_ids\": torch.stack(token_type_ids),\n", + " \"pixel_values\": encoding[\"pixel_values\"],\n", + " \"pixel_mask\": encoding[\"pixel_mask\"],\n", + " \"labels\": torch.stack(labels)\n", + " }\n", + " return batch\n", + "\n", + "dataloader = DataLoader(\n", + " vqa_dataset,\n", + " collate_fn=collate_fn,\n", + " batch_size=4,\n", + " shuffle=False\n", + ")\n", + "\n", + "batch = next(iter(dataloader))\n", + "for key, value in batch.items():\n", + " print(f\"{key}: {value.shape}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2ccdbacd", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "from PIL import Image\n", + "\n", + "def unnormalize_image(pixel_values, image_mean, image_std):\n", + " scaled = pixel_values * np.array(image_std)[:, None, None]\n", + " shifted = scaled + np.array(image_mean)[:, None, None]\n", + " uint8_image = (shifted * 255).astype(np.uint8)\n", + " return uint8_image.transpose(1, 2, 0)\n", + "\n", + "batch_idx = 1\n", + "\n", + "image = unnormalize_image(\n", + " pixel_values=batch[\"pixel_values\"][batch_idx].numpy(),\n", + " image_mean=processor.image_processor.image_mean,\n", + " image_std=processor.image_processor.image_std\n", + ")\n", + "print(\"Question:\", processor.decode(batch[\"input_ids\"][batch_idx]))\n", + "\n", + "labels = torch.nonzero(batch[\"labels\"][batch_idx]).flatten().tolist()\n", + "label_names = [config.id2label[label] for label in labels]\n", + "print(\"Possible answers:\", label_names)\n", + "\n", + "plt.imshow(Image.fromarray(image))\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f319e687", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "from transformers import Trainer, TrainingArguments\n", + "\n", + "training_args = TrainingArguments(\n", + " output_dir=\"VQA\",\n", + " num_train_epochs=20,\n", + " per_device_train_batch_size=8,\n", + " learning_rate=1e-4,\n", + " weight_decay=0.01,\n", + " logging_strategy=\"steps\",\n", + " logging_steps=20,\n", + " seed=42\n", + ")\n", + "\n", + "trainer = Trainer(\n", + " model=model,\n", + " args=training_args,\n", + " train_dataset=vqa_dataset,\n", + " data_collator=collate_fn\n", + ")\n", + "\n", + "trainer.train()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3b167bed", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "sample_index = 4\n", + "sample = vqa_dataset[sample_index]\n", + "\n", + "print(\"Sample keys:\", sample.keys())\n", + "print(\"Question:\", processor.decode(sample[\"input_ids\"]))\n", + "\n", + "sample = {k: v.unsqueeze(0).to(device) for k, v in sample.items()}\n", + "\n", + "model.eval()\n", + "with torch.no_grad():\n", + " outputs = model(**sample)\n", + "\n", + "logits = outputs.logits\n", + "predicted_probs = torch.sigmoid(logits)\n", + "top_probs, top_classes = torch.topk(predicted_probs, 5)\n", + "\n", + "top_probs = top_probs.squeeze().tolist()\n", + "top_classes = top_classes.squeeze().tolist()\n", + "for prob, class_idx in zip(top_probs, top_classes):\n", + " answer = model.config.id2label[class_idx]\n", + " print(f\"Answer: {answer:<7} Probability: {prob:.4f}\")\n", + "\n", + "unnormalized_image = unnormalize_image(\n", + " pixel_values=vqa_dataset[sample_index][\"pixel_values\"].numpy(),\n", + " image_mean=processor.image_processor.image_mean,\n", + " image_std=processor.image_processor.image_std,\n", + ")\n", + "\n", + "plt.imshow(Image.fromarray(unnormalized_image))\n", + "plt.show()" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.25 ~ 5.27 Stable-Diffusion 3 \353\252\250\353\215\270 \352\265\254\354\241\260.ipynb" "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.25 ~ 5.27 Stable-Diffusion 3 \353\252\250\353\215\270 \352\265\254\354\241\260.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..4bd9cb61a35251d9bf0bf0ef4b0be2b3a819fe6e --- /dev/null +++ "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.25 ~ 5.27 Stable-Diffusion 3 \353\252\250\353\215\270 \352\265\254\354\241\260.ipynb" @@ -0,0 +1,125 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "16c52da2", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import torch\n", + "from diffusers import StableDiffusion3Pipeline\n", + "\n", + "token = \"hf_###...\" # 토큰 입력\n", + "model_name = \"stabilityai/stable-diffusion-3-medium-diffusers\"\n", + "\n", + "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", + "pipe = StableDiffusion3Pipeline.from_pretrained(\n", + " model_name, token=token, torch_dtype=torch.float16\n", + ").to(device)\n", + "print(pipe.config)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ef58f70c", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "prompt = [\"gray cat holding a yellow ball\"]\n", + "prompt_2 = prompt\n", + "prompt_3 = prompt\n", + "num_images_per_prompt = 1\n", + "clip_skip = None\n", + "max_sequence_length = 256\n", + "\n", + "prompt_embed, pooled_prompt_embed = pipe._get_clip_prompt_embeds(\n", + " prompt=prompt,\n", + " device=\"cuda\",\n", + " num_images_per_prompt=num_images_per_prompt,\n", + " clip_skip=clip_skip,\n", + " clip_model_index=0\n", + ")\n", + "prompt_2_embed, pooled_prompt_2_embed = pipe._get_clip_prompt_embeds(\n", + " prompt=prompt_2,\n", + " device=\"cuda\",\n", + " num_images_per_prompt=num_images_per_prompt,\n", + " clip_skip=clip_skip,\n", + " clip_model_index=1\n", + ")\n", + "clip_prompt_embeds = torch.cat([prompt_embed, prompt_2_embed], dim=-1)\n", + "\n", + "t5_prompt_embed = pipe._get_t5_prompt_embeds(\n", + " prompt=prompt_3,\n", + " num_images_per_prompt=num_images_per_prompt,\n", + " max_sequence_length=max_sequence_length,\n", + " device=\"cuda\"\n", + ")\n", + "\n", + "clip_prompt_embeds = torch.nn.functional.pad(\n", + " clip_prompt_embeds, (0, t5_prompt_embed.shape[-1] - clip_prompt_embeds.shape[-1])\n", + ")\n", + "\n", + "prompt_embeds = torch.cat([clip_prompt_embeds, t5_prompt_embed], dim=-2)\n", + "pooled_prompt_embeds = torch.cat([pooled_prompt_embed, pooled_prompt_2_embed], dim=-1)\n", + "\n", + "print(\"prompt_embed.shape:\", prompt_embed.shape)\n", + "print(\"prompt2_embed.shape:\", prompt_2_embed.shape)\n", + "print(\"t5_prompt_embed.shape:\", t5_prompt_embed.shape)\n", + "print(\"prompt_embeds.shape:\", prompt_embeds.shape)\n", + "print(\"pooled_prompt_embeds.shape:\", pooled_prompt_embeds.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0eeb6074", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "for main_name, main_module in pipe.transformer.named_children():\n", + " print(main_name)\n", + " for sub_name, sub_module in main_module.named_children():\n", + " print(\"└\", sub_name)\n", + " for ssub_name, ssub_module in sub_module.named_children():\n", + " print(\"│ └\", ssub_name)\n", + " for sssub_name, sssub_module in ssub_module.named_children():\n", + " print(\"│ └\", sssub_name)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.28 ~ 5.31 \354\235\264\353\257\270\354\247\200 \354\203\235\354\204\261.ipynb" "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.28 ~ 5.31 \354\235\264\353\257\270\354\247\200 \354\203\235\354\204\261.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..7118e1ae260da701a82d966c170fa25992fc8d00 --- /dev/null +++ "b/5\354\236\245 \353\251\200\355\213\260\353\252\250\353\213\254/\354\230\210\354\240\234 5.28 ~ 5.31 \354\235\264\353\257\270\354\247\200 \354\203\235\354\204\261.ipynb" @@ -0,0 +1,133 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "51f84768", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import torch\n", + "import matplotlib.pyplot as plt\n", + "from diffusers import StableDiffusion3Pipeline\n", + "\n", + "token = \"hf_###...\" # 토큰 입력\n", + "model_name = \"stabilityai/stable-diffusion-3-medium-diffusers\"\n", + "\n", + "gpu_total_memory_gb = torch.cuda.get_device_properties(\"cuda\").total_memory / 1024**3\n", + "torch_dtype = torch.float16 if gpu_total_memory_gb < 24 else torch.float32\n", + "pipe = StableDiffusion3Pipeline.from_pretrained(\n", + " model_name, token=token, torch_dtype=torch_dtype\n", + ")\n", + "pipe.to(\"cuda\")\n", + "\n", + "generator = torch.Generator(device=\"cuda\").manual_seed(3)\n", + "image = pipe(\n", + " prompt=\"gray cat holding a yellow ball\",\n", + " negative_prompt=\"\",\n", + " num_inference_steps=28,\n", + " guidance_scale=7.0,\n", + " generator=generator\n", + ").images[0]\n", + "plt.imshow(image)\n", + "plt.show()\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9f3d3d10", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "image = pipe(\n", + " prompt=\"gray cat holding star shaped blue ball\",\n", + " negative_prompt=\"\",\n", + " num_inference_steps=28,\n", + " guidance_scale=7.0,\n", + " generator=generator\n", + ").images[0]\n", + "plt.imshow(image)\n", + "plt.show()\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7a698413", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "prompt = \"There is a sleek gray cat with gleaming silver fur and vibrant emerald eyes. This cat's most treasured possession is a bright yellow ball that it carries everywhere. One can imagine the cat happily batting the ball around the garden.\"\n", + "image = pipe(\n", + " prompt=prompt,\n", + " negative_prompt=\"\",\n", + " num_inference_steps=28,\n", + " guidance_scale=7.0,\n", + " max_sequence_length=512,\n", + " generator=generator\n", + ").images[0]\n", + "plt.imshow(image)\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1d0ca18e", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "prompt = \"There is a sleek gray cat with gleaming silver fur and vibrant emerald eyes. This cat's most treasured possession is a bright yellow ball that it carries everywhere. One can imagine the cat happily batting the ball around the garden.\"\n", + "negative_prompt = \"Blurry, low quality, distorted features, multiple cats, dogs, humans, cartoon style, anime, sketchy, black and white, indoor setting, rainy weather, dark or gloomy atmosphere.\"\n", + "image = pipe(\n", + " prompt=prompt,\n", + " negative_prompt=negative_prompt,\n", + " num_inference_steps=28,\n", + " guidance_scale=7.0,\n", + " max_sequence_length=512,\n", + " generator=generator\n", + ").images[0]\n", + "plt.imshow(image)\n", + "plt.show()" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..8f1e040f230a637e460bcc62c42428a1c043728d --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +--- +license: apache-2.0 +language: +- ko +pipeline_tag: image-classification +tags: +- resnet +--- + +Hello, Hugging Face! diff --git a/config.json b/config.json new file mode 100644 index 0000000000000000000000000000000000000000..7d91e6a3d09511e6d9985e0431cf6652eeef0f11 --- /dev/null +++ b/config.json @@ -0,0 +1,2043 @@ +{ + "_name_or_path": "microsoft/resnet-18", + "architectures": [ + "ResNetForImageClassification" + ], + "depths": [ + 2, + 2, + 2, + 2 + ], + "downsample_in_bottleneck": false, + "downsample_in_first_stage": false, + "embedding_size": 64, + "hidden_act": "relu", + "hidden_sizes": [ + 64, + 128, + 256, + 512 + ], + "id2label": { + "0": "tench, Tinca tinca", + "1": "goldfish, Carassius auratus", + "2": "great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias", + "3": "tiger shark, Galeocerdo cuvieri", + "4": "hammerhead, hammerhead shark", + "5": "electric ray, crampfish, numbfish, torpedo", + "6": "stingray", + "7": "cock", + "8": "hen", + "9": "ostrich, Struthio camelus", + "10": "brambling, Fringilla montifringilla", + "11": "goldfinch, Carduelis carduelis", + "12": "house finch, linnet, Carpodacus mexicanus", + "13": "junco, snowbird", + "14": "indigo bunting, indigo finch, indigo bird, Passerina cyanea", + "15": "robin, American robin, Turdus migratorius", + "16": "bulbul", + "17": "jay", + "18": "magpie", + "19": "chickadee", + "20": "water ouzel, dipper", + "21": "kite", + "22": "bald eagle, American eagle, Haliaeetus leucocephalus", + "23": "vulture", + "24": "great grey owl, great gray owl, Strix nebulosa", + "25": "European fire salamander, Salamandra salamandra", + "26": "common newt, Triturus vulgaris", + "27": "eft", + "28": "spotted salamander, Ambystoma maculatum", + "29": "axolotl, mud puppy, Ambystoma mexicanum", + "30": "bullfrog, Rana catesbeiana", + "31": "tree frog, tree-frog", + "32": "tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui", + "33": "loggerhead, loggerhead turtle, Caretta caretta", + "34": "leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea", + "35": "mud turtle", + "36": "terrapin", + "37": "box turtle, box tortoise", + "38": "banded gecko", + "39": "common iguana, iguana, Iguana iguana", + "40": "American chameleon, anole, Anolis carolinensis", + "41": "whiptail, whiptail lizard", + "42": "agama", + "43": "frilled lizard, Chlamydosaurus kingi", + "44": "alligator lizard", + "45": "Gila monster, Heloderma suspectum", + "46": "green lizard, Lacerta viridis", + "47": "African chameleon, Chamaeleo chamaeleon", + "48": "Komodo dragon, Komodo lizard, dragon lizard, giant lizard, Varanus komodoensis", + "49": "African crocodile, Nile crocodile, Crocodylus niloticus", + "50": "American alligator, Alligator mississipiensis", + "51": "triceratops", + "52": "thunder snake, worm snake, Carphophis amoenus", + "53": "ringneck snake, ring-necked snake, ring snake", + "54": "hognose snake, puff adder, sand viper", + "55": "green snake, grass snake", + "56": "king snake, kingsnake", + "57": "garter snake, grass snake", + "58": "water snake", + "59": "vine snake", + "60": "night snake, Hypsiglena torquata", + "61": "boa constrictor, Constrictor constrictor", + "62": "rock python, rock snake, Python sebae", + "63": "Indian cobra, Naja naja", + "64": "green mamba", + "65": "sea snake", + "66": "horned viper, cerastes, sand viper, horned asp, Cerastes cornutus", + "67": "diamondback, diamondback rattlesnake, Crotalus adamanteus", + "68": "sidewinder, horned rattlesnake, Crotalus cerastes", + "69": "trilobite", + "70": "harvestman, daddy longlegs, Phalangium opilio", + "71": "scorpion", + "72": "black and gold garden spider, Argiope aurantia", + "73": "barn spider, Araneus cavaticus", + "74": "garden spider, Aranea diademata", + "75": "black widow, Latrodectus mactans", + "76": "tarantula", + "77": "wolf spider, hunting spider", + "78": "tick", + "79": "centipede", + "80": "black grouse", + "81": "ptarmigan", + "82": "ruffed grouse, partridge, Bonasa umbellus", + "83": "prairie chicken, prairie grouse, prairie fowl", + "84": "peacock", + "85": "quail", + "86": "partridge", + "87": "African grey, African gray, Psittacus erithacus", + "88": "macaw", + "89": "sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita", + "90": "lorikeet", + "91": "coucal", + "92": "bee eater", + "93": "hornbill", + "94": "hummingbird", + "95": "jacamar", + "96": "toucan", + "97": "drake", + "98": "red-breasted merganser, Mergus serrator", + "99": "goose", + "100": "black swan, Cygnus atratus", + "101": "tusker", + "102": "echidna, spiny anteater, anteater", + "103": "platypus, duckbill, duckbilled platypus, duck-billed platypus, Ornithorhynchus anatinus", + "104": "wallaby, brush kangaroo", + "105": "koala, koala bear, kangaroo bear, native bear, Phascolarctos cinereus", + "106": "wombat", + "107": "jellyfish", + "108": "sea anemone, anemone", + "109": "brain coral", + "110": "flatworm, platyhelminth", + "111": "nematode, nematode worm, roundworm", + "112": "conch", + "113": "snail", + "114": "slug", + "115": "sea slug, nudibranch", + "116": "chiton, coat-of-mail shell, sea cradle, polyplacophore", + "117": "chambered nautilus, pearly nautilus, nautilus", + "118": "Dungeness crab, Cancer magister", + "119": "rock crab, Cancer irroratus", + "120": "fiddler crab", + "121": "king crab, Alaska crab, Alaskan king crab, Alaska king crab, Paralithodes camtschatica", + "122": "American lobster, Northern lobster, Maine lobster, Homarus americanus", + "123": "spiny lobster, langouste, rock lobster, crawfish, crayfish, sea crawfish", + "124": "crayfish, crawfish, crawdad, crawdaddy", + "125": "hermit crab", + "126": "isopod", + "127": "white stork, Ciconia ciconia", + "128": "black stork, Ciconia nigra", + "129": "spoonbill", + "130": "flamingo", + "131": "little blue heron, Egretta caerulea", + "132": "American egret, great white heron, Egretta albus", + "133": "bittern", + "134": "crane", + "135": "limpkin, Aramus pictus", + "136": "European gallinule, Porphyrio porphyrio", + "137": "American coot, marsh hen, mud hen, water hen, Fulica americana", + "138": "bustard", + "139": "ruddy turnstone, Arenaria interpres", + "140": "red-backed sandpiper, dunlin, Erolia alpina", + "141": "redshank, Tringa totanus", + "142": "dowitcher", + "143": "oystercatcher, oyster catcher", + "144": "pelican", + "145": "king penguin, Aptenodytes patagonica", + "146": "albatross, mollymawk", + "147": "grey whale, gray whale, devilfish, Eschrichtius gibbosus, Eschrichtius robustus", + "148": "killer whale, killer, orca, grampus, sea wolf, Orcinus orca", + "149": "dugong, Dugong dugon", + "150": "sea lion", + "151": "Chihuahua", + "152": "Japanese spaniel", + "153": "Maltese dog, Maltese terrier, Maltese", + "154": "Pekinese, Pekingese, Peke", + "155": "Shih-Tzu", + "156": "Blenheim spaniel", + "157": "papillon", + "158": "toy terrier", + "159": "Rhodesian ridgeback", + "160": "Afghan hound, Afghan", + "161": "basset, basset hound", + "162": "beagle", + "163": "bloodhound, sleuthhound", + "164": "bluetick", + "165": "black-and-tan coonhound", + "166": "Walker hound, Walker foxhound", + "167": "English foxhound", + "168": "redbone", + "169": "borzoi, Russian wolfhound", + "170": "Irish wolfhound", + "171": "Italian greyhound", + "172": "whippet", + "173": "Ibizan hound, Ibizan Podenco", + "174": "Norwegian elkhound, elkhound", + "175": "otterhound, otter hound", + "176": "Saluki, gazelle hound", + "177": "Scottish deerhound, deerhound", + "178": "Weimaraner", + "179": "Staffordshire bullterrier, Staffordshire bull terrier", + "180": "American Staffordshire terrier, Staffordshire terrier, American pit bull terrier, pit bull terrier", + "181": "Bedlington terrier", + "182": "Border terrier", + "183": "Kerry blue terrier", + "184": "Irish terrier", + "185": "Norfolk terrier", + "186": "Norwich terrier", + "187": "Yorkshire terrier", + "188": "wire-haired fox terrier", + "189": "Lakeland terrier", + "190": "Sealyham terrier, Sealyham", + "191": "Airedale, Airedale terrier", + "192": "cairn, cairn terrier", + "193": "Australian terrier", + "194": "Dandie Dinmont, Dandie Dinmont terrier", + "195": "Boston bull, Boston terrier", + "196": "miniature schnauzer", + "197": "giant schnauzer", + "198": "standard schnauzer", + "199": "Scotch terrier, Scottish terrier, Scottie", + "200": "Tibetan terrier, chrysanthemum dog", + "201": "silky terrier, Sydney silky", + "202": "soft-coated wheaten terrier", + "203": "West Highland white terrier", + "204": "Lhasa, Lhasa apso", + "205": "flat-coated retriever", + "206": "curly-coated retriever", + "207": "golden retriever", + "208": "Labrador retriever", + "209": "Chesapeake Bay retriever", + "210": "German short-haired pointer", + "211": "vizsla, Hungarian pointer", + "212": "English setter", + "213": "Irish setter, red setter", + "214": "Gordon setter", + "215": "Brittany spaniel", + "216": "clumber, clumber spaniel", + "217": "English springer, English springer spaniel", + "218": "Welsh springer spaniel", + "219": "cocker spaniel, English cocker spaniel, cocker", + "220": "Sussex spaniel", + "221": "Irish water spaniel", + "222": "kuvasz", + "223": "schipperke", + "224": "groenendael", + "225": "malinois", + "226": "briard", + "227": "kelpie", + "228": "komondor", + "229": "Old English sheepdog, bobtail", + "230": "Shetland sheepdog, Shetland sheep dog, Shetland", + "231": "collie", + "232": "Border collie", + "233": "Bouvier des Flandres, Bouviers des Flandres", + "234": "Rottweiler", + "235": "German shepherd, German shepherd dog, German police dog, alsatian", + "236": "Doberman, Doberman pinscher", + "237": "miniature pinscher", + "238": "Greater Swiss Mountain dog", + "239": "Bernese mountain dog", + "240": "Appenzeller", + "241": "EntleBucher", + "242": "boxer", + "243": "bull mastiff", + "244": "Tibetan mastiff", + "245": "French bulldog", + "246": "Great Dane", + "247": "Saint Bernard, St Bernard", + "248": "Eskimo dog, husky", + "249": "malamute, malemute, Alaskan malamute", + "250": "Siberian husky", + "251": "dalmatian, coach dog, carriage dog", + "252": "affenpinscher, monkey pinscher, monkey dog", + "253": "basenji", + "254": "pug, pug-dog", + "255": "Leonberg", + "256": "Newfoundland, Newfoundland dog", + "257": "Great Pyrenees", + "258": "Samoyed, Samoyede", + "259": "Pomeranian", + "260": "chow, chow chow", + "261": "keeshond", + "262": "Brabancon griffon", + "263": "Pembroke, Pembroke Welsh corgi", + "264": "Cardigan, Cardigan Welsh corgi", + "265": "toy poodle", + "266": "miniature poodle", + "267": "standard poodle", + "268": "Mexican hairless", + "269": "timber wolf, grey wolf, gray wolf, Canis lupus", + "270": "white wolf, Arctic wolf, Canis lupus tundrarum", + "271": "red wolf, maned wolf, Canis rufus, Canis niger", + "272": "coyote, prairie wolf, brush wolf, Canis latrans", + "273": "dingo, warrigal, warragal, Canis dingo", + "274": "dhole, Cuon alpinus", + "275": "African hunting dog, hyena dog, Cape hunting dog, Lycaon pictus", + "276": "hyena, hyaena", + "277": "red fox, Vulpes vulpes", + "278": "kit fox, Vulpes macrotis", + "279": "Arctic fox, white fox, Alopex lagopus", + "280": "grey fox, gray fox, Urocyon cinereoargenteus", + "281": "tabby, tabby cat", + "282": "tiger cat", + "283": "Persian cat", + "284": "Siamese cat, Siamese", + "285": "Egyptian cat", + "286": "cougar, puma, catamount, mountain lion, painter, panther, Felis concolor", + "287": "lynx, catamount", + "288": "leopard, Panthera pardus", + "289": "snow leopard, ounce, Panthera uncia", + "290": "jaguar, panther, Panthera onca, Felis onca", + "291": "lion, king of beasts, Panthera leo", + "292": "tiger, Panthera tigris", + "293": "cheetah, chetah, Acinonyx jubatus", + "294": "brown bear, bruin, Ursus arctos", + "295": "American black bear, black bear, Ursus americanus, Euarctos americanus", + "296": "ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus", + "297": "sloth bear, Melursus ursinus, Ursus ursinus", + "298": "mongoose", + "299": "meerkat, mierkat", + "300": "tiger beetle", + "301": "ladybug, ladybeetle, lady beetle, ladybird, ladybird beetle", + "302": "ground beetle, carabid beetle", + "303": "long-horned beetle, longicorn, longicorn beetle", + "304": "leaf beetle, chrysomelid", + "305": "dung beetle", + "306": "rhinoceros beetle", + "307": "weevil", + "308": "fly", + "309": "bee", + "310": "ant, emmet, pismire", + "311": "grasshopper, hopper", + "312": "cricket", + "313": "walking stick, walkingstick, stick insect", + "314": "cockroach, roach", + "315": "mantis, mantid", + "316": "cicada, cicala", + "317": "leafhopper", + "318": "lacewing, lacewing fly", + "319": "dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk", + "320": "damselfly", + "321": "admiral", + "322": "ringlet, ringlet butterfly", + "323": "monarch, monarch butterfly, milkweed butterfly, Danaus plexippus", + "324": "cabbage butterfly", + "325": "sulphur butterfly, sulfur butterfly", + "326": "lycaenid, lycaenid butterfly", + "327": "starfish, sea star", + "328": "sea urchin", + "329": "sea cucumber, holothurian", + "330": "wood rabbit, cottontail, cottontail rabbit", + "331": "hare", + "332": "Angora, Angora rabbit", + "333": "hamster", + "334": "porcupine, hedgehog", + "335": "fox squirrel, eastern fox squirrel, Sciurus niger", + "336": "marmot", + "337": "beaver", + "338": "guinea pig, Cavia cobaya", + "339": "sorrel", + "340": "zebra", + "341": "hog, pig, grunter, squealer, Sus scrofa", + "342": "wild boar, boar, Sus scrofa", + "343": "warthog", + "344": "hippopotamus, hippo, river horse, Hippopotamus amphibius", + "345": "ox", + "346": "water buffalo, water ox, Asiatic buffalo, Bubalus bubalis", + "347": "bison", + "348": "ram, tup", + "349": "bighorn, bighorn sheep, cimarron, Rocky Mountain bighorn, Rocky Mountain sheep, Ovis canadensis", + "350": "ibex, Capra ibex", + "351": "hartebeest", + "352": "impala, Aepyceros melampus", + "353": "gazelle", + "354": "Arabian camel, dromedary, Camelus dromedarius", + "355": "llama", + "356": "weasel", + "357": "mink", + "358": "polecat, fitch, foulmart, foumart, Mustela putorius", + "359": "black-footed ferret, ferret, Mustela nigripes", + "360": "otter", + "361": "skunk, polecat, wood pussy", + "362": "badger", + "363": "armadillo", + "364": "three-toed sloth, ai, Bradypus tridactylus", + "365": "orangutan, orang, orangutang, Pongo pygmaeus", + "366": "gorilla, Gorilla gorilla", + "367": "chimpanzee, chimp, Pan troglodytes", + "368": "gibbon, Hylobates lar", + "369": "siamang, Hylobates syndactylus, Symphalangus syndactylus", + "370": "guenon, guenon monkey", + "371": "patas, hussar monkey, Erythrocebus patas", + "372": "baboon", + "373": "macaque", + "374": "langur", + "375": "colobus, colobus monkey", + "376": "proboscis monkey, Nasalis larvatus", + "377": "marmoset", + "378": "capuchin, ringtail, Cebus capucinus", + "379": "howler monkey, howler", + "380": "titi, titi monkey", + "381": "spider monkey, Ateles geoffroyi", + "382": "squirrel monkey, Saimiri sciureus", + "383": "Madagascar cat, ring-tailed lemur, Lemur catta", + "384": "indri, indris, Indri indri, Indri brevicaudatus", + "385": "Indian elephant, Elephas maximus", + "386": "African elephant, Loxodonta africana", + "387": "lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens", + "388": "giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca", + "389": "barracouta, snoek", + "390": "eel", + "391": "coho, cohoe, coho salmon, blue jack, silver salmon, Oncorhynchus kisutch", + "392": "rock beauty, Holocanthus tricolor", + "393": "anemone fish", + "394": "sturgeon", + "395": "gar, garfish, garpike, billfish, Lepisosteus osseus", + "396": "lionfish", + "397": "puffer, pufferfish, blowfish, globefish", + "398": "abacus", + "399": "abaya", + "400": "academic gown, academic robe, judge's robe", + "401": "accordion, piano accordion, squeeze box", + "402": "acoustic guitar", + "403": "aircraft carrier, carrier, flattop, attack aircraft carrier", + "404": "airliner", + "405": "airship, dirigible", + "406": "altar", + "407": "ambulance", + "408": "amphibian, amphibious vehicle", + "409": "analog clock", + "410": "apiary, bee house", + "411": "apron", + "412": "ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, dustbin, trash barrel, trash bin", + "413": "assault rifle, assault gun", + "414": "backpack, back pack, knapsack, packsack, rucksack, haversack", + "415": "bakery, bakeshop, bakehouse", + "416": "balance beam, beam", + "417": "balloon", + "418": "ballpoint, ballpoint pen, ballpen, Biro", + "419": "Band Aid", + "420": "banjo", + "421": "bannister, banister, balustrade, balusters, handrail", + "422": "barbell", + "423": "barber chair", + "424": "barbershop", + "425": "barn", + "426": "barometer", + "427": "barrel, cask", + "428": "barrow, garden cart, lawn cart, wheelbarrow", + "429": "baseball", + "430": "basketball", + "431": "bassinet", + "432": "bassoon", + "433": "bathing cap, swimming cap", + "434": "bath towel", + "435": "bathtub, bathing tub, bath, tub", + "436": "beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon", + "437": "beacon, lighthouse, beacon light, pharos", + "438": "beaker", + "439": "bearskin, busby, shako", + "440": "beer bottle", + "441": "beer glass", + "442": "bell cote, bell cot", + "443": "bib", + "444": "bicycle-built-for-two, tandem bicycle, tandem", + "445": "bikini, two-piece", + "446": "binder, ring-binder", + "447": "binoculars, field glasses, opera glasses", + "448": "birdhouse", + "449": "boathouse", + "450": "bobsled, bobsleigh, bob", + "451": "bolo tie, bolo, bola tie, bola", + "452": "bonnet, poke bonnet", + "453": "bookcase", + "454": "bookshop, bookstore, bookstall", + "455": "bottlecap", + "456": "bow", + "457": "bow tie, bow-tie, bowtie", + "458": "brass, memorial tablet, plaque", + "459": "brassiere, bra, bandeau", + "460": "breakwater, groin, groyne, mole, bulwark, seawall, jetty", + "461": "breastplate, aegis, egis", + "462": "broom", + "463": "bucket, pail", + "464": "buckle", + "465": "bulletproof vest", + "466": "bullet train, bullet", + "467": "butcher shop, meat market", + "468": "cab, hack, taxi, taxicab", + "469": "caldron, cauldron", + "470": "candle, taper, wax light", + "471": "cannon", + "472": "canoe", + "473": "can opener, tin opener", + "474": "cardigan", + "475": "car mirror", + "476": "carousel, carrousel, merry-go-round, roundabout, whirligig", + "477": "carpenter's kit, tool kit", + "478": "carton", + "479": "car wheel", + "480": "cash machine, cash dispenser, automated teller machine, automatic teller machine, automated teller, automatic teller, ATM", + "481": "cassette", + "482": "cassette player", + "483": "castle", + "484": "catamaran", + "485": "CD player", + "486": "cello, violoncello", + "487": "cellular telephone, cellular phone, cellphone, cell, mobile phone", + "488": "chain", + "489": "chainlink fence", + "490": "chain mail, ring mail, mail, chain armor, chain armour, ring armor, ring armour", + "491": "chain saw, chainsaw", + "492": "chest", + "493": "chiffonier, commode", + "494": "chime, bell, gong", + "495": "china cabinet, china closet", + "496": "Christmas stocking", + "497": "church, church building", + "498": "cinema, movie theater, movie theatre, movie house, picture palace", + "499": "cleaver, meat cleaver, chopper", + "500": "cliff dwelling", + "501": "cloak", + "502": "clog, geta, patten, sabot", + "503": "cocktail shaker", + "504": "coffee mug", + "505": "coffeepot", + "506": "coil, spiral, volute, whorl, helix", + "507": "combination lock", + "508": "computer keyboard, keypad", + "509": "confectionery, confectionary, candy store", + "510": "container ship, containership, container vessel", + "511": "convertible", + "512": "corkscrew, bottle screw", + "513": "cornet, horn, trumpet, trump", + "514": "cowboy boot", + "515": "cowboy hat, ten-gallon hat", + "516": "cradle", + "517": "crane", + "518": "crash helmet", + "519": "crate", + "520": "crib, cot", + "521": "Crock Pot", + "522": "croquet ball", + "523": "crutch", + "524": "cuirass", + "525": "dam, dike, dyke", + "526": "desk", + "527": "desktop computer", + "528": "dial telephone, dial phone", + "529": "diaper, nappy, napkin", + "530": "digital clock", + "531": "digital watch", + "532": "dining table, board", + "533": "dishrag, dishcloth", + "534": "dishwasher, dish washer, dishwashing machine", + "535": "disk brake, disc brake", + "536": "dock, dockage, docking facility", + "537": "dogsled, dog sled, dog sleigh", + "538": "dome", + "539": "doormat, welcome mat", + "540": "drilling platform, offshore rig", + "541": "drum, membranophone, tympan", + "542": "drumstick", + "543": "dumbbell", + "544": "Dutch oven", + "545": "electric fan, blower", + "546": "electric guitar", + "547": "electric locomotive", + "548": "entertainment center", + "549": "envelope", + "550": "espresso maker", + "551": "face powder", + "552": "feather boa, boa", + "553": "file, file cabinet, filing cabinet", + "554": "fireboat", + "555": "fire engine, fire truck", + "556": "fire screen, fireguard", + "557": "flagpole, flagstaff", + "558": "flute, transverse flute", + "559": "folding chair", + "560": "football helmet", + "561": "forklift", + "562": "fountain", + "563": "fountain pen", + "564": "four-poster", + "565": "freight car", + "566": "French horn, horn", + "567": "frying pan, frypan, skillet", + "568": "fur coat", + "569": "garbage truck, dustcart", + "570": "gasmask, respirator, gas helmet", + "571": "gas pump, gasoline pump, petrol pump, island dispenser", + "572": "goblet", + "573": "go-kart", + "574": "golf ball", + "575": "golfcart, golf cart", + "576": "gondola", + "577": "gong, tam-tam", + "578": "gown", + "579": "grand piano, grand", + "580": "greenhouse, nursery, glasshouse", + "581": "grille, radiator grille", + "582": "grocery store, grocery, food market, market", + "583": "guillotine", + "584": "hair slide", + "585": "hair spray", + "586": "half track", + "587": "hammer", + "588": "hamper", + "589": "hand blower, blow dryer, blow drier, hair dryer, hair drier", + "590": "hand-held computer, hand-held microcomputer", + "591": "handkerchief, hankie, hanky, hankey", + "592": "hard disc, hard disk, fixed disk", + "593": "harmonica, mouth organ, harp, mouth harp", + "594": "harp", + "595": "harvester, reaper", + "596": "hatchet", + "597": "holster", + "598": "home theater, home theatre", + "599": "honeycomb", + "600": "hook, claw", + "601": "hoopskirt, crinoline", + "602": "horizontal bar, high bar", + "603": "horse cart, horse-cart", + "604": "hourglass", + "605": "iPod", + "606": "iron, smoothing iron", + "607": "jack-o'-lantern", + "608": "jean, blue jean, denim", + "609": "jeep, landrover", + "610": "jersey, T-shirt, tee shirt", + "611": "jigsaw puzzle", + "612": "jinrikisha, ricksha, rickshaw", + "613": "joystick", + "614": "kimono", + "615": "knee pad", + "616": "knot", + "617": "lab coat, laboratory coat", + "618": "ladle", + "619": "lampshade, lamp shade", + "620": "laptop, laptop computer", + "621": "lawn mower, mower", + "622": "lens cap, lens cover", + "623": "letter opener, paper knife, paperknife", + "624": "library", + "625": "lifeboat", + "626": "lighter, light, igniter, ignitor", + "627": "limousine, limo", + "628": "liner, ocean liner", + "629": "lipstick, lip rouge", + "630": "Loafer", + "631": "lotion", + "632": "loudspeaker, speaker, speaker unit, loudspeaker system, speaker system", + "633": "loupe, jeweler's loupe", + "634": "lumbermill, sawmill", + "635": "magnetic compass", + "636": "mailbag, postbag", + "637": "mailbox, letter box", + "638": "maillot", + "639": "maillot, tank suit", + "640": "manhole cover", + "641": "maraca", + "642": "marimba, xylophone", + "643": "mask", + "644": "matchstick", + "645": "maypole", + "646": "maze, labyrinth", + "647": "measuring cup", + "648": "medicine chest, medicine cabinet", + "649": "megalith, megalithic structure", + "650": "microphone, mike", + "651": "microwave, microwave oven", + "652": "military uniform", + "653": "milk can", + "654": "minibus", + "655": "miniskirt, mini", + "656": "minivan", + "657": "missile", + "658": "mitten", + "659": "mixing bowl", + "660": "mobile home, manufactured home", + "661": "Model T", + "662": "modem", + "663": "monastery", + "664": "monitor", + "665": "moped", + "666": "mortar", + "667": "mortarboard", + "668": "mosque", + "669": "mosquito net", + "670": "motor scooter, scooter", + "671": "mountain bike, all-terrain bike, off-roader", + "672": "mountain tent", + "673": "mouse, computer mouse", + "674": "mousetrap", + "675": "moving van", + "676": "muzzle", + "677": "nail", + "678": "neck brace", + "679": "necklace", + "680": "nipple", + "681": "notebook, notebook computer", + "682": "obelisk", + "683": "oboe, hautboy, hautbois", + "684": "ocarina, sweet potato", + "685": "odometer, hodometer, mileometer, milometer", + "686": "oil filter", + "687": "organ, pipe organ", + "688": "oscilloscope, scope, cathode-ray oscilloscope, CRO", + "689": "overskirt", + "690": "oxcart", + "691": "oxygen mask", + "692": "packet", + "693": "paddle, boat paddle", + "694": "paddlewheel, paddle wheel", + "695": "padlock", + "696": "paintbrush", + "697": "pajama, pyjama, pj's, jammies", + "698": "palace", + "699": "panpipe, pandean pipe, syrinx", + "700": "paper towel", + "701": "parachute, chute", + "702": "parallel bars, bars", + "703": "park bench", + "704": "parking meter", + "705": "passenger car, coach, carriage", + "706": "patio, terrace", + "707": "pay-phone, pay-station", + "708": "pedestal, plinth, footstall", + "709": "pencil box, pencil case", + "710": "pencil sharpener", + "711": "perfume, essence", + "712": "Petri dish", + "713": "photocopier", + "714": "pick, plectrum, plectron", + "715": "pickelhaube", + "716": "picket fence, paling", + "717": "pickup, pickup truck", + "718": "pier", + "719": "piggy bank, penny bank", + "720": "pill bottle", + "721": "pillow", + "722": "ping-pong ball", + "723": "pinwheel", + "724": "pirate, pirate ship", + "725": "pitcher, ewer", + "726": "plane, carpenter's plane, woodworking plane", + "727": "planetarium", + "728": "plastic bag", + "729": "plate rack", + "730": "plow, plough", + "731": "plunger, plumber's helper", + "732": "Polaroid camera, Polaroid Land camera", + "733": "pole", + "734": "police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria", + "735": "poncho", + "736": "pool table, billiard table, snooker table", + "737": "pop bottle, soda bottle", + "738": "pot, flowerpot", + "739": "potter's wheel", + "740": "power drill", + "741": "prayer rug, prayer mat", + "742": "printer", + "743": "prison, prison house", + "744": "projectile, missile", + "745": "projector", + "746": "puck, hockey puck", + "747": "punching bag, punch bag, punching ball, punchball", + "748": "purse", + "749": "quill, quill pen", + "750": "quilt, comforter, comfort, puff", + "751": "racer, race car, racing car", + "752": "racket, racquet", + "753": "radiator", + "754": "radio, wireless", + "755": "radio telescope, radio reflector", + "756": "rain barrel", + "757": "recreational vehicle, RV, R.V.", + "758": "reel", + "759": "reflex camera", + "760": "refrigerator, icebox", + "761": "remote control, remote", + "762": "restaurant, eating house, eating place, eatery", + "763": "revolver, six-gun, six-shooter", + "764": "rifle", + "765": "rocking chair, rocker", + "766": "rotisserie", + "767": "rubber eraser, rubber, pencil eraser", + "768": "rugby ball", + "769": "rule, ruler", + "770": "running shoe", + "771": "safe", + "772": "safety pin", + "773": "saltshaker, salt shaker", + "774": "sandal", + "775": "sarong", + "776": "sax, saxophone", + "777": "scabbard", + "778": "scale, weighing machine", + "779": "school bus", + "780": "schooner", + "781": "scoreboard", + "782": "screen, CRT screen", + "783": "screw", + "784": "screwdriver", + "785": "seat belt, seatbelt", + "786": "sewing machine", + "787": "shield, buckler", + "788": "shoe shop, shoe-shop, shoe store", + "789": "shoji", + "790": "shopping basket", + "791": "shopping cart", + "792": "shovel", + "793": "shower cap", + "794": "shower curtain", + "795": "ski", + "796": "ski mask", + "797": "sleeping bag", + "798": "slide rule, slipstick", + "799": "sliding door", + "800": "slot, one-armed bandit", + "801": "snorkel", + "802": "snowmobile", + "803": "snowplow, snowplough", + "804": "soap dispenser", + "805": "soccer ball", + "806": "sock", + "807": "solar dish, solar collector, solar furnace", + "808": "sombrero", + "809": "soup bowl", + "810": "space bar", + "811": "space heater", + "812": "space shuttle", + "813": "spatula", + "814": "speedboat", + "815": "spider web, spider's web", + "816": "spindle", + "817": "sports car, sport car", + "818": "spotlight, spot", + "819": "stage", + "820": "steam locomotive", + "821": "steel arch bridge", + "822": "steel drum", + "823": "stethoscope", + "824": "stole", + "825": "stone wall", + "826": "stopwatch, stop watch", + "827": "stove", + "828": "strainer", + "829": "streetcar, tram, tramcar, trolley, trolley car", + "830": "stretcher", + "831": "studio couch, day bed", + "832": "stupa, tope", + "833": "submarine, pigboat, sub, U-boat", + "834": "suit, suit of clothes", + "835": "sundial", + "836": "sunglass", + "837": "sunglasses, dark glasses, shades", + "838": "sunscreen, sunblock, sun blocker", + "839": "suspension bridge", + "840": "swab, swob, mop", + "841": "sweatshirt", + "842": "swimming trunks, bathing trunks", + "843": "swing", + "844": "switch, electric switch, electrical switch", + "845": "syringe", + "846": "table lamp", + "847": "tank, army tank, armored combat vehicle, armoured combat vehicle", + "848": "tape player", + "849": "teapot", + "850": "teddy, teddy bear", + "851": "television, television system", + "852": "tennis ball", + "853": "thatch, thatched roof", + "854": "theater curtain, theatre curtain", + "855": "thimble", + "856": "thresher, thrasher, threshing machine", + "857": "throne", + "858": "tile roof", + "859": "toaster", + "860": "tobacco shop, tobacconist shop, tobacconist", + "861": "toilet seat", + "862": "torch", + "863": "totem pole", + "864": "tow truck, tow car, wrecker", + "865": "toyshop", + "866": "tractor", + "867": "trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi", + "868": "tray", + "869": "trench coat", + "870": "tricycle, trike, velocipede", + "871": "trimaran", + "872": "tripod", + "873": "triumphal arch", + "874": "trolleybus, trolley coach, trackless trolley", + "875": "trombone", + "876": "tub, vat", + "877": "turnstile", + "878": "typewriter keyboard", + "879": "umbrella", + "880": "unicycle, monocycle", + "881": "upright, upright piano", + "882": "vacuum, vacuum cleaner", + "883": "vase", + "884": "vault", + "885": "velvet", + "886": "vending machine", + "887": "vestment", + "888": "viaduct", + "889": "violin, fiddle", + "890": "volleyball", + "891": "waffle iron", + "892": "wall clock", + "893": "wallet, billfold, notecase, pocketbook", + "894": "wardrobe, closet, press", + "895": "warplane, military plane", + "896": "washbasin, handbasin, washbowl, lavabo, wash-hand basin", + "897": "washer, automatic washer, washing machine", + "898": "water bottle", + "899": "water jug", + "900": "water tower", + "901": "whiskey jug", + "902": "whistle", + "903": "wig", + "904": "window screen", + "905": "window shade", + "906": "Windsor tie", + "907": "wine bottle", + "908": "wing", + "909": "wok", + "910": "wooden spoon", + "911": "wool, woolen, woollen", + "912": "worm fence, snake fence, snake-rail fence, Virginia fence", + "913": "wreck", + "914": "yawl", + "915": "yurt", + "916": "web site, website, internet site, site", + "917": "comic book", + "918": "crossword puzzle, crossword", + "919": "street sign", + "920": "traffic light, traffic signal, stoplight", + "921": "book jacket, dust cover, dust jacket, dust wrapper", + "922": "menu", + "923": "plate", + "924": "guacamole", + "925": "consomme", + "926": "hot pot, hotpot", + "927": "trifle", + "928": "ice cream, icecream", + "929": "ice lolly, lolly, lollipop, popsicle", + "930": "French loaf", + "931": "bagel, beigel", + "932": "pretzel", + "933": "cheeseburger", + "934": "hotdog, hot dog, red hot", + "935": "mashed potato", + "936": "head cabbage", + "937": "broccoli", + "938": "cauliflower", + "939": "zucchini, courgette", + "940": "spaghetti squash", + "941": "acorn squash", + "942": "butternut squash", + "943": "cucumber, cuke", + "944": "artichoke, globe artichoke", + "945": "bell pepper", + "946": "cardoon", + "947": "mushroom", + "948": "Granny Smith", + "949": "strawberry", + "950": "orange", + "951": "lemon", + "952": "fig", + "953": "pineapple, ananas", + "954": "banana", + "955": "jackfruit, jak, jack", + "956": "custard apple", + "957": "pomegranate", + "958": "hay", + "959": "carbonara", + "960": "chocolate sauce, chocolate syrup", + "961": "dough", + "962": "meat loaf, meatloaf", + "963": "pizza, pizza pie", + "964": "potpie", + "965": "burrito", + "966": "red wine", + "967": "espresso", + "968": "cup", + "969": "eggnog", + "970": "alp", + "971": "bubble", + "972": "cliff, drop, drop-off", + "973": "coral reef", + "974": "geyser", + "975": "lakeside, lakeshore", + "976": "promontory, headland, head, foreland", + "977": "sandbar, sand bar", + "978": "seashore, coast, seacoast, sea-coast", + "979": "valley, vale", + "980": "volcano", + "981": "ballplayer, baseball player", + "982": "groom, bridegroom", + "983": "scuba diver", + "984": "rapeseed", + "985": "daisy", + "986": "yellow lady's slipper, yellow lady-slipper, Cypripedium calceolus, Cypripedium parviflorum", + "987": "corn", + "988": "acorn", + "989": "hip, rose hip, rosehip", + "990": "buckeye, horse chestnut, conker", + "991": "coral fungus", + "992": "agaric", + "993": "gyromitra", + "994": "stinkhorn, carrion fungus", + "995": "earthstar", + "996": "hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa", + "997": "bolete", + "998": "ear, spike, capitulum", + "999": "toilet tissue, toilet paper, bathroom tissue" + }, + "label2id": { + "Afghan hound, Afghan": 160, + "African chameleon, Chamaeleo chamaeleon": 47, + "African crocodile, Nile crocodile, Crocodylus niloticus": 49, + "African elephant, Loxodonta africana": 386, + "African grey, African gray, Psittacus erithacus": 87, + "African hunting dog, hyena dog, Cape hunting dog, Lycaon pictus": 275, + "Airedale, Airedale terrier": 191, + "American Staffordshire terrier, Staffordshire terrier, American pit bull terrier, pit bull terrier": 180, + "American alligator, Alligator mississipiensis": 50, + "American black bear, black bear, Ursus americanus, Euarctos americanus": 295, + "American chameleon, anole, Anolis carolinensis": 40, + "American coot, marsh hen, mud hen, water hen, Fulica americana": 137, + "American egret, great white heron, Egretta albus": 132, + "American lobster, Northern lobster, Maine lobster, Homarus americanus": 122, + "Angora, Angora rabbit": 332, + "Appenzeller": 240, + "Arabian camel, dromedary, Camelus dromedarius": 354, + "Arctic fox, white fox, Alopex lagopus": 279, + "Australian terrier": 193, + "Band Aid": 419, + "Bedlington terrier": 181, + "Bernese mountain dog": 239, + "Blenheim spaniel": 156, + "Border collie": 232, + "Border terrier": 182, + "Boston bull, Boston terrier": 195, + "Bouvier des Flandres, Bouviers des Flandres": 233, + "Brabancon griffon": 262, + "Brittany spaniel": 215, + "CD player": 485, + "Cardigan, Cardigan Welsh corgi": 264, + "Chesapeake Bay retriever": 209, + "Chihuahua": 151, + "Christmas stocking": 496, + "Crock Pot": 521, + "Dandie Dinmont, Dandie Dinmont terrier": 194, + "Doberman, Doberman pinscher": 236, + "Dungeness crab, Cancer magister": 118, + "Dutch oven": 544, + "Egyptian cat": 285, + "English foxhound": 167, + "English setter": 212, + "English springer, English springer spaniel": 217, + "EntleBucher": 241, + "Eskimo dog, husky": 248, + "European fire salamander, Salamandra salamandra": 25, + "European gallinule, Porphyrio porphyrio": 136, + "French bulldog": 245, + "French horn, horn": 566, + "French loaf": 930, + "German shepherd, German shepherd dog, German police dog, alsatian": 235, + "German short-haired pointer": 210, + "Gila monster, Heloderma suspectum": 45, + "Gordon setter": 214, + "Granny Smith": 948, + "Great Dane": 246, + "Great Pyrenees": 257, + "Greater Swiss Mountain dog": 238, + "Ibizan hound, Ibizan Podenco": 173, + "Indian cobra, Naja naja": 63, + "Indian elephant, Elephas maximus": 385, + "Irish setter, red setter": 213, + "Irish terrier": 184, + "Irish water spaniel": 221, + "Irish wolfhound": 170, + "Italian greyhound": 171, + "Japanese spaniel": 152, + "Kerry blue terrier": 183, + "Komodo dragon, Komodo lizard, dragon lizard, giant lizard, Varanus komodoensis": 48, + "Labrador retriever": 208, + "Lakeland terrier": 189, + "Leonberg": 255, + "Lhasa, Lhasa apso": 204, + "Loafer": 630, + "Madagascar cat, ring-tailed lemur, Lemur catta": 383, + "Maltese dog, Maltese terrier, Maltese": 153, + "Mexican hairless": 268, + "Model T": 661, + "Newfoundland, Newfoundland dog": 256, + "Norfolk terrier": 185, + "Norwegian elkhound, elkhound": 174, + "Norwich terrier": 186, + "Old English sheepdog, bobtail": 229, + "Pekinese, Pekingese, Peke": 154, + "Pembroke, Pembroke Welsh corgi": 263, + "Persian cat": 283, + "Petri dish": 712, + "Polaroid camera, Polaroid Land camera": 732, + "Pomeranian": 259, + "Rhodesian ridgeback": 159, + "Rottweiler": 234, + "Saint Bernard, St Bernard": 247, + "Saluki, gazelle hound": 176, + "Samoyed, Samoyede": 258, + "Scotch terrier, Scottish terrier, Scottie": 199, + "Scottish deerhound, deerhound": 177, + "Sealyham terrier, Sealyham": 190, + "Shetland sheepdog, Shetland sheep dog, Shetland": 230, + "Shih-Tzu": 155, + "Siamese cat, Siamese": 284, + "Siberian husky": 250, + "Staffordshire bullterrier, Staffordshire bull terrier": 179, + "Sussex spaniel": 220, + "Tibetan mastiff": 244, + "Tibetan terrier, chrysanthemum dog": 200, + "Walker hound, Walker foxhound": 166, + "Weimaraner": 178, + "Welsh springer spaniel": 218, + "West Highland white terrier": 203, + "Windsor tie": 906, + "Yorkshire terrier": 187, + "abacus": 398, + "abaya": 399, + "academic gown, academic robe, judge's robe": 400, + "accordion, piano accordion, squeeze box": 401, + "acorn": 988, + "acorn squash": 941, + "acoustic guitar": 402, + "admiral": 321, + "affenpinscher, monkey pinscher, monkey dog": 252, + "agama": 42, + "agaric": 992, + "aircraft carrier, carrier, flattop, attack aircraft carrier": 403, + "airliner": 404, + "airship, dirigible": 405, + "albatross, mollymawk": 146, + "alligator lizard": 44, + "alp": 970, + "altar": 406, + "ambulance": 407, + "amphibian, amphibious vehicle": 408, + "analog clock": 409, + "anemone fish": 393, + "ant, emmet, pismire": 310, + "apiary, bee house": 410, + "apron": 411, + "armadillo": 363, + "artichoke, globe artichoke": 944, + "ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, dustbin, trash barrel, trash bin": 412, + "assault rifle, assault gun": 413, + "axolotl, mud puppy, Ambystoma mexicanum": 29, + "baboon": 372, + "backpack, back pack, knapsack, packsack, rucksack, haversack": 414, + "badger": 362, + "bagel, beigel": 931, + "bakery, bakeshop, bakehouse": 415, + "balance beam, beam": 416, + "bald eagle, American eagle, Haliaeetus leucocephalus": 22, + "balloon": 417, + "ballplayer, baseball player": 981, + "ballpoint, ballpoint pen, ballpen, Biro": 418, + "banana": 954, + "banded gecko": 38, + "banjo": 420, + "bannister, banister, balustrade, balusters, handrail": 421, + "barbell": 422, + "barber chair": 423, + "barbershop": 424, + "barn": 425, + "barn spider, Araneus cavaticus": 73, + "barometer": 426, + "barracouta, snoek": 389, + "barrel, cask": 427, + "barrow, garden cart, lawn cart, wheelbarrow": 428, + "baseball": 429, + "basenji": 253, + "basketball": 430, + "basset, basset hound": 161, + "bassinet": 431, + "bassoon": 432, + "bath towel": 434, + "bathing cap, swimming cap": 433, + "bathtub, bathing tub, bath, tub": 435, + "beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon": 436, + "beacon, lighthouse, beacon light, pharos": 437, + "beagle": 162, + "beaker": 438, + "bearskin, busby, shako": 439, + "beaver": 337, + "bee": 309, + "bee eater": 92, + "beer bottle": 440, + "beer glass": 441, + "bell cote, bell cot": 442, + "bell pepper": 945, + "bib": 443, + "bicycle-built-for-two, tandem bicycle, tandem": 444, + "bighorn, bighorn sheep, cimarron, Rocky Mountain bighorn, Rocky Mountain sheep, Ovis canadensis": 349, + "bikini, two-piece": 445, + "binder, ring-binder": 446, + "binoculars, field glasses, opera glasses": 447, + "birdhouse": 448, + "bison": 347, + "bittern": 133, + "black and gold garden spider, Argiope aurantia": 72, + "black grouse": 80, + "black stork, Ciconia nigra": 128, + "black swan, Cygnus atratus": 100, + "black widow, Latrodectus mactans": 75, + "black-and-tan coonhound": 165, + "black-footed ferret, ferret, Mustela nigripes": 359, + "bloodhound, sleuthhound": 163, + "bluetick": 164, + "boa constrictor, Constrictor constrictor": 61, + "boathouse": 449, + "bobsled, bobsleigh, bob": 450, + "bolete": 997, + "bolo tie, bolo, bola tie, bola": 451, + "bonnet, poke bonnet": 452, + "book jacket, dust cover, dust jacket, dust wrapper": 921, + "bookcase": 453, + "bookshop, bookstore, bookstall": 454, + "borzoi, Russian wolfhound": 169, + "bottlecap": 455, + "bow": 456, + "bow tie, bow-tie, bowtie": 457, + "box turtle, box tortoise": 37, + "boxer": 242, + "brain coral": 109, + "brambling, Fringilla montifringilla": 10, + "brass, memorial tablet, plaque": 458, + "brassiere, bra, bandeau": 459, + "breakwater, groin, groyne, mole, bulwark, seawall, jetty": 460, + "breastplate, aegis, egis": 461, + "briard": 226, + "broccoli": 937, + "broom": 462, + "brown bear, bruin, Ursus arctos": 294, + "bubble": 971, + "bucket, pail": 463, + "buckeye, horse chestnut, conker": 990, + "buckle": 464, + "bulbul": 16, + "bull mastiff": 243, + "bullet train, bullet": 466, + "bulletproof vest": 465, + "bullfrog, Rana catesbeiana": 30, + "burrito": 965, + "bustard": 138, + "butcher shop, meat market": 467, + "butternut squash": 942, + "cab, hack, taxi, taxicab": 468, + "cabbage butterfly": 324, + "cairn, cairn terrier": 192, + "caldron, cauldron": 469, + "can opener, tin opener": 473, + "candle, taper, wax light": 470, + "cannon": 471, + "canoe": 472, + "capuchin, ringtail, Cebus capucinus": 378, + "car mirror": 475, + "car wheel": 479, + "carbonara": 959, + "cardigan": 474, + "cardoon": 946, + "carousel, carrousel, merry-go-round, roundabout, whirligig": 476, + "carpenter's kit, tool kit": 477, + "carton": 478, + "cash machine, cash dispenser, automated teller machine, automatic teller machine, automated teller, automatic teller, ATM": 480, + "cassette": 481, + "cassette player": 482, + "castle": 483, + "catamaran": 484, + "cauliflower": 938, + "cello, violoncello": 486, + "cellular telephone, cellular phone, cellphone, cell, mobile phone": 487, + "centipede": 79, + "chain": 488, + "chain mail, ring mail, mail, chain armor, chain armour, ring armor, ring armour": 490, + "chain saw, chainsaw": 491, + "chainlink fence": 489, + "chambered nautilus, pearly nautilus, nautilus": 117, + "cheeseburger": 933, + "cheetah, chetah, Acinonyx jubatus": 293, + "chest": 492, + "chickadee": 19, + "chiffonier, commode": 493, + "chime, bell, gong": 494, + "chimpanzee, chimp, Pan troglodytes": 367, + "china cabinet, china closet": 495, + "chiton, coat-of-mail shell, sea cradle, polyplacophore": 116, + "chocolate sauce, chocolate syrup": 960, + "chow, chow chow": 260, + "church, church building": 497, + "cicada, cicala": 316, + "cinema, movie theater, movie theatre, movie house, picture palace": 498, + "cleaver, meat cleaver, chopper": 499, + "cliff dwelling": 500, + "cliff, drop, drop-off": 972, + "cloak": 501, + "clog, geta, patten, sabot": 502, + "clumber, clumber spaniel": 216, + "cock": 7, + "cocker spaniel, English cocker spaniel, cocker": 219, + "cockroach, roach": 314, + "cocktail shaker": 503, + "coffee mug": 504, + "coffeepot": 505, + "coho, cohoe, coho salmon, blue jack, silver salmon, Oncorhynchus kisutch": 391, + "coil, spiral, volute, whorl, helix": 506, + "collie": 231, + "colobus, colobus monkey": 375, + "combination lock": 507, + "comic book": 917, + "common iguana, iguana, Iguana iguana": 39, + "common newt, Triturus vulgaris": 26, + "computer keyboard, keypad": 508, + "conch": 112, + "confectionery, confectionary, candy store": 509, + "consomme": 925, + "container ship, containership, container vessel": 510, + "convertible": 511, + "coral fungus": 991, + "coral reef": 973, + "corkscrew, bottle screw": 512, + "corn": 987, + "cornet, horn, trumpet, trump": 513, + "coucal": 91, + "cougar, puma, catamount, mountain lion, painter, panther, Felis concolor": 286, + "cowboy boot": 514, + "cowboy hat, ten-gallon hat": 515, + "coyote, prairie wolf, brush wolf, Canis latrans": 272, + "cradle": 516, + "crane": 517, + "crash helmet": 518, + "crate": 519, + "crayfish, crawfish, crawdad, crawdaddy": 124, + "crib, cot": 520, + "cricket": 312, + "croquet ball": 522, + "crossword puzzle, crossword": 918, + "crutch": 523, + "cucumber, cuke": 943, + "cuirass": 524, + "cup": 968, + "curly-coated retriever": 206, + "custard apple": 956, + "daisy": 985, + "dalmatian, coach dog, carriage dog": 251, + "dam, dike, dyke": 525, + "damselfly": 320, + "desk": 526, + "desktop computer": 527, + "dhole, Cuon alpinus": 274, + "dial telephone, dial phone": 528, + "diamondback, diamondback rattlesnake, Crotalus adamanteus": 67, + "diaper, nappy, napkin": 529, + "digital clock": 530, + "digital watch": 531, + "dingo, warrigal, warragal, Canis dingo": 273, + "dining table, board": 532, + "dishrag, dishcloth": 533, + "dishwasher, dish washer, dishwashing machine": 534, + "disk brake, disc brake": 535, + "dock, dockage, docking facility": 536, + "dogsled, dog sled, dog sleigh": 537, + "dome": 538, + "doormat, welcome mat": 539, + "dough": 961, + "dowitcher": 142, + "dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk": 319, + "drake": 97, + "drilling platform, offshore rig": 540, + "drum, membranophone, tympan": 541, + "drumstick": 542, + "dugong, Dugong dugon": 149, + "dumbbell": 543, + "dung beetle": 305, + "ear, spike, capitulum": 998, + "earthstar": 995, + "echidna, spiny anteater, anteater": 102, + "eel": 390, + "eft": 27, + "eggnog": 969, + "electric fan, blower": 545, + "electric guitar": 546, + "electric locomotive": 547, + "electric ray, crampfish, numbfish, torpedo": 5, + "entertainment center": 548, + "envelope": 549, + "espresso": 967, + "espresso maker": 550, + "face powder": 551, + "feather boa, boa": 552, + "fiddler crab": 120, + "fig": 952, + "file, file cabinet, filing cabinet": 553, + "fire engine, fire truck": 555, + "fire screen, fireguard": 556, + "fireboat": 554, + "flagpole, flagstaff": 557, + "flamingo": 130, + "flat-coated retriever": 205, + "flatworm, platyhelminth": 110, + "flute, transverse flute": 558, + "fly": 308, + "folding chair": 559, + "football helmet": 560, + "forklift": 561, + "fountain": 562, + "fountain pen": 563, + "four-poster": 564, + "fox squirrel, eastern fox squirrel, Sciurus niger": 335, + "freight car": 565, + "frilled lizard, Chlamydosaurus kingi": 43, + "frying pan, frypan, skillet": 567, + "fur coat": 568, + "gar, garfish, garpike, billfish, Lepisosteus osseus": 395, + "garbage truck, dustcart": 569, + "garden spider, Aranea diademata": 74, + "garter snake, grass snake": 57, + "gas pump, gasoline pump, petrol pump, island dispenser": 571, + "gasmask, respirator, gas helmet": 570, + "gazelle": 353, + "geyser": 974, + "giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca": 388, + "giant schnauzer": 197, + "gibbon, Hylobates lar": 368, + "go-kart": 573, + "goblet": 572, + "golden retriever": 207, + "goldfinch, Carduelis carduelis": 11, + "goldfish, Carassius auratus": 1, + "golf ball": 574, + "golfcart, golf cart": 575, + "gondola": 576, + "gong, tam-tam": 577, + "goose": 99, + "gorilla, Gorilla gorilla": 366, + "gown": 578, + "grand piano, grand": 579, + "grasshopper, hopper": 311, + "great grey owl, great gray owl, Strix nebulosa": 24, + "great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias": 2, + "green lizard, Lacerta viridis": 46, + "green mamba": 64, + "green snake, grass snake": 55, + "greenhouse, nursery, glasshouse": 580, + "grey fox, gray fox, Urocyon cinereoargenteus": 280, + "grey whale, gray whale, devilfish, Eschrichtius gibbosus, Eschrichtius robustus": 147, + "grille, radiator grille": 581, + "grocery store, grocery, food market, market": 582, + "groenendael": 224, + "groom, bridegroom": 982, + "ground beetle, carabid beetle": 302, + "guacamole": 924, + "guenon, guenon monkey": 370, + "guillotine": 583, + "guinea pig, Cavia cobaya": 338, + "gyromitra": 993, + "hair slide": 584, + "hair spray": 585, + "half track": 586, + "hammer": 587, + "hammerhead, hammerhead shark": 4, + "hamper": 588, + "hamster": 333, + "hand blower, blow dryer, blow drier, hair dryer, hair drier": 589, + "hand-held computer, hand-held microcomputer": 590, + "handkerchief, hankie, hanky, hankey": 591, + "hard disc, hard disk, fixed disk": 592, + "hare": 331, + "harmonica, mouth organ, harp, mouth harp": 593, + "harp": 594, + "hartebeest": 351, + "harvester, reaper": 595, + "harvestman, daddy longlegs, Phalangium opilio": 70, + "hatchet": 596, + "hay": 958, + "head cabbage": 936, + "hen": 8, + "hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa": 996, + "hermit crab": 125, + "hip, rose hip, rosehip": 989, + "hippopotamus, hippo, river horse, Hippopotamus amphibius": 344, + "hog, pig, grunter, squealer, Sus scrofa": 341, + "hognose snake, puff adder, sand viper": 54, + "holster": 597, + "home theater, home theatre": 598, + "honeycomb": 599, + "hook, claw": 600, + "hoopskirt, crinoline": 601, + "horizontal bar, high bar": 602, + "hornbill": 93, + "horned viper, cerastes, sand viper, horned asp, Cerastes cornutus": 66, + "horse cart, horse-cart": 603, + "hot pot, hotpot": 926, + "hotdog, hot dog, red hot": 934, + "hourglass": 604, + "house finch, linnet, Carpodacus mexicanus": 12, + "howler monkey, howler": 379, + "hummingbird": 94, + "hyena, hyaena": 276, + "iPod": 605, + "ibex, Capra ibex": 350, + "ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus": 296, + "ice cream, icecream": 928, + "ice lolly, lolly, lollipop, popsicle": 929, + "impala, Aepyceros melampus": 352, + "indigo bunting, indigo finch, indigo bird, Passerina cyanea": 14, + "indri, indris, Indri indri, Indri brevicaudatus": 384, + "iron, smoothing iron": 606, + "isopod": 126, + "jacamar": 95, + "jack-o'-lantern": 607, + "jackfruit, jak, jack": 955, + "jaguar, panther, Panthera onca, Felis onca": 290, + "jay": 17, + "jean, blue jean, denim": 608, + "jeep, landrover": 609, + "jellyfish": 107, + "jersey, T-shirt, tee shirt": 610, + "jigsaw puzzle": 611, + "jinrikisha, ricksha, rickshaw": 612, + "joystick": 613, + "junco, snowbird": 13, + "keeshond": 261, + "kelpie": 227, + "killer whale, killer, orca, grampus, sea wolf, Orcinus orca": 148, + "kimono": 614, + "king crab, Alaska crab, Alaskan king crab, Alaska king crab, Paralithodes camtschatica": 121, + "king penguin, Aptenodytes patagonica": 145, + "king snake, kingsnake": 56, + "kit fox, Vulpes macrotis": 278, + "kite": 21, + "knee pad": 615, + "knot": 616, + "koala, koala bear, kangaroo bear, native bear, Phascolarctos cinereus": 105, + "komondor": 228, + "kuvasz": 222, + "lab coat, laboratory coat": 617, + "lacewing, lacewing fly": 318, + "ladle": 618, + "ladybug, ladybeetle, lady beetle, ladybird, ladybird beetle": 301, + "lakeside, lakeshore": 975, + "lampshade, lamp shade": 619, + "langur": 374, + "laptop, laptop computer": 620, + "lawn mower, mower": 621, + "leaf beetle, chrysomelid": 304, + "leafhopper": 317, + "leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea": 34, + "lemon": 951, + "lens cap, lens cover": 622, + "leopard, Panthera pardus": 288, + "lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens": 387, + "letter opener, paper knife, paperknife": 623, + "library": 624, + "lifeboat": 625, + "lighter, light, igniter, ignitor": 626, + "limousine, limo": 627, + "limpkin, Aramus pictus": 135, + "liner, ocean liner": 628, + "lion, king of beasts, Panthera leo": 291, + "lionfish": 396, + "lipstick, lip rouge": 629, + "little blue heron, Egretta caerulea": 131, + "llama": 355, + "loggerhead, loggerhead turtle, Caretta caretta": 33, + "long-horned beetle, longicorn, longicorn beetle": 303, + "lorikeet": 90, + "lotion": 631, + "loudspeaker, speaker, speaker unit, loudspeaker system, speaker system": 632, + "loupe, jeweler's loupe": 633, + "lumbermill, sawmill": 634, + "lycaenid, lycaenid butterfly": 326, + "lynx, catamount": 287, + "macaque": 373, + "macaw": 88, + "magnetic compass": 635, + "magpie": 18, + "mailbag, postbag": 636, + "mailbox, letter box": 637, + "maillot": 638, + "maillot, tank suit": 639, + "malamute, malemute, Alaskan malamute": 249, + "malinois": 225, + "manhole cover": 640, + "mantis, mantid": 315, + "maraca": 641, + "marimba, xylophone": 642, + "marmoset": 377, + "marmot": 336, + "mashed potato": 935, + "mask": 643, + "matchstick": 644, + "maypole": 645, + "maze, labyrinth": 646, + "measuring cup": 647, + "meat loaf, meatloaf": 962, + "medicine chest, medicine cabinet": 648, + "meerkat, mierkat": 299, + "megalith, megalithic structure": 649, + "menu": 922, + "microphone, mike": 650, + "microwave, microwave oven": 651, + "military uniform": 652, + "milk can": 653, + "miniature pinscher": 237, + "miniature poodle": 266, + "miniature schnauzer": 196, + "minibus": 654, + "miniskirt, mini": 655, + "minivan": 656, + "mink": 357, + "missile": 657, + "mitten": 658, + "mixing bowl": 659, + "mobile home, manufactured home": 660, + "modem": 662, + "monarch, monarch butterfly, milkweed butterfly, Danaus plexippus": 323, + "monastery": 663, + "mongoose": 298, + "monitor": 664, + "moped": 665, + "mortar": 666, + "mortarboard": 667, + "mosque": 668, + "mosquito net": 669, + "motor scooter, scooter": 670, + "mountain bike, all-terrain bike, off-roader": 671, + "mountain tent": 672, + "mouse, computer mouse": 673, + "mousetrap": 674, + "moving van": 675, + "mud turtle": 35, + "mushroom": 947, + "muzzle": 676, + "nail": 677, + "neck brace": 678, + "necklace": 679, + "nematode, nematode worm, roundworm": 111, + "night snake, Hypsiglena torquata": 60, + "nipple": 680, + "notebook, notebook computer": 681, + "obelisk": 682, + "oboe, hautboy, hautbois": 683, + "ocarina, sweet potato": 684, + "odometer, hodometer, mileometer, milometer": 685, + "oil filter": 686, + "orange": 950, + "orangutan, orang, orangutang, Pongo pygmaeus": 365, + "organ, pipe organ": 687, + "oscilloscope, scope, cathode-ray oscilloscope, CRO": 688, + "ostrich, Struthio camelus": 9, + "otter": 360, + "otterhound, otter hound": 175, + "overskirt": 689, + "ox": 345, + "oxcart": 690, + "oxygen mask": 691, + "oystercatcher, oyster catcher": 143, + "packet": 692, + "paddle, boat paddle": 693, + "paddlewheel, paddle wheel": 694, + "padlock": 695, + "paintbrush": 696, + "pajama, pyjama, pj's, jammies": 697, + "palace": 698, + "panpipe, pandean pipe, syrinx": 699, + "paper towel": 700, + "papillon": 157, + "parachute, chute": 701, + "parallel bars, bars": 702, + "park bench": 703, + "parking meter": 704, + "partridge": 86, + "passenger car, coach, carriage": 705, + "patas, hussar monkey, Erythrocebus patas": 371, + "patio, terrace": 706, + "pay-phone, pay-station": 707, + "peacock": 84, + "pedestal, plinth, footstall": 708, + "pelican": 144, + "pencil box, pencil case": 709, + "pencil sharpener": 710, + "perfume, essence": 711, + "photocopier": 713, + "pick, plectrum, plectron": 714, + "pickelhaube": 715, + "picket fence, paling": 716, + "pickup, pickup truck": 717, + "pier": 718, + "piggy bank, penny bank": 719, + "pill bottle": 720, + "pillow": 721, + "pineapple, ananas": 953, + "ping-pong ball": 722, + "pinwheel": 723, + "pirate, pirate ship": 724, + "pitcher, ewer": 725, + "pizza, pizza pie": 963, + "plane, carpenter's plane, woodworking plane": 726, + "planetarium": 727, + "plastic bag": 728, + "plate": 923, + "plate rack": 729, + "platypus, duckbill, duckbilled platypus, duck-billed platypus, Ornithorhynchus anatinus": 103, + "plow, plough": 730, + "plunger, plumber's helper": 731, + "pole": 733, + "polecat, fitch, foulmart, foumart, Mustela putorius": 358, + "police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria": 734, + "pomegranate": 957, + "poncho": 735, + "pool table, billiard table, snooker table": 736, + "pop bottle, soda bottle": 737, + "porcupine, hedgehog": 334, + "pot, flowerpot": 738, + "potpie": 964, + "potter's wheel": 739, + "power drill": 740, + "prairie chicken, prairie grouse, prairie fowl": 83, + "prayer rug, prayer mat": 741, + "pretzel": 932, + "printer": 742, + "prison, prison house": 743, + "proboscis monkey, Nasalis larvatus": 376, + "projectile, missile": 744, + "projector": 745, + "promontory, headland, head, foreland": 976, + "ptarmigan": 81, + "puck, hockey puck": 746, + "puffer, pufferfish, blowfish, globefish": 397, + "pug, pug-dog": 254, + "punching bag, punch bag, punching ball, punchball": 747, + "purse": 748, + "quail": 85, + "quill, quill pen": 749, + "quilt, comforter, comfort, puff": 750, + "racer, race car, racing car": 751, + "racket, racquet": 752, + "radiator": 753, + "radio telescope, radio reflector": 755, + "radio, wireless": 754, + "rain barrel": 756, + "ram, tup": 348, + "rapeseed": 984, + "recreational vehicle, RV, R.V.": 757, + "red fox, Vulpes vulpes": 277, + "red wine": 966, + "red wolf, maned wolf, Canis rufus, Canis niger": 271, + "red-backed sandpiper, dunlin, Erolia alpina": 140, + "red-breasted merganser, Mergus serrator": 98, + "redbone": 168, + "redshank, Tringa totanus": 141, + "reel": 758, + "reflex camera": 759, + "refrigerator, icebox": 760, + "remote control, remote": 761, + "restaurant, eating house, eating place, eatery": 762, + "revolver, six-gun, six-shooter": 763, + "rhinoceros beetle": 306, + "rifle": 764, + "ringlet, ringlet butterfly": 322, + "ringneck snake, ring-necked snake, ring snake": 53, + "robin, American robin, Turdus migratorius": 15, + "rock beauty, Holocanthus tricolor": 392, + "rock crab, Cancer irroratus": 119, + "rock python, rock snake, Python sebae": 62, + "rocking chair, rocker": 765, + "rotisserie": 766, + "rubber eraser, rubber, pencil eraser": 767, + "ruddy turnstone, Arenaria interpres": 139, + "ruffed grouse, partridge, Bonasa umbellus": 82, + "rugby ball": 768, + "rule, ruler": 769, + "running shoe": 770, + "safe": 771, + "safety pin": 772, + "saltshaker, salt shaker": 773, + "sandal": 774, + "sandbar, sand bar": 977, + "sarong": 775, + "sax, saxophone": 776, + "scabbard": 777, + "scale, weighing machine": 778, + "schipperke": 223, + "school bus": 779, + "schooner": 780, + "scoreboard": 781, + "scorpion": 71, + "screen, CRT screen": 782, + "screw": 783, + "screwdriver": 784, + "scuba diver": 983, + "sea anemone, anemone": 108, + "sea cucumber, holothurian": 329, + "sea lion": 150, + "sea slug, nudibranch": 115, + "sea snake": 65, + "sea urchin": 328, + "seashore, coast, seacoast, sea-coast": 978, + "seat belt, seatbelt": 785, + "sewing machine": 786, + "shield, buckler": 787, + "shoe shop, shoe-shop, shoe store": 788, + "shoji": 789, + "shopping basket": 790, + "shopping cart": 791, + "shovel": 792, + "shower cap": 793, + "shower curtain": 794, + "siamang, Hylobates syndactylus, Symphalangus syndactylus": 369, + "sidewinder, horned rattlesnake, Crotalus cerastes": 68, + "silky terrier, Sydney silky": 201, + "ski": 795, + "ski mask": 796, + "skunk, polecat, wood pussy": 361, + "sleeping bag": 797, + "slide rule, slipstick": 798, + "sliding door": 799, + "slot, one-armed bandit": 800, + "sloth bear, Melursus ursinus, Ursus ursinus": 297, + "slug": 114, + "snail": 113, + "snorkel": 801, + "snow leopard, ounce, Panthera uncia": 289, + "snowmobile": 802, + "snowplow, snowplough": 803, + "soap dispenser": 804, + "soccer ball": 805, + "sock": 806, + "soft-coated wheaten terrier": 202, + "solar dish, solar collector, solar furnace": 807, + "sombrero": 808, + "sorrel": 339, + "soup bowl": 809, + "space bar": 810, + "space heater": 811, + "space shuttle": 812, + "spaghetti squash": 940, + "spatula": 813, + "speedboat": 814, + "spider monkey, Ateles geoffroyi": 381, + "spider web, spider's web": 815, + "spindle": 816, + "spiny lobster, langouste, rock lobster, crawfish, crayfish, sea crawfish": 123, + "spoonbill": 129, + "sports car, sport car": 817, + "spotlight, spot": 818, + "spotted salamander, Ambystoma maculatum": 28, + "squirrel monkey, Saimiri sciureus": 382, + "stage": 819, + "standard poodle": 267, + "standard schnauzer": 198, + "starfish, sea star": 327, + "steam locomotive": 820, + "steel arch bridge": 821, + "steel drum": 822, + "stethoscope": 823, + "stingray": 6, + "stinkhorn, carrion fungus": 994, + "stole": 824, + "stone wall": 825, + "stopwatch, stop watch": 826, + "stove": 827, + "strainer": 828, + "strawberry": 949, + "street sign": 919, + "streetcar, tram, tramcar, trolley, trolley car": 829, + "stretcher": 830, + "studio couch, day bed": 831, + "stupa, tope": 832, + "sturgeon": 394, + "submarine, pigboat, sub, U-boat": 833, + "suit, suit of clothes": 834, + "sulphur butterfly, sulfur butterfly": 325, + "sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita": 89, + "sundial": 835, + "sunglass": 836, + "sunglasses, dark glasses, shades": 837, + "sunscreen, sunblock, sun blocker": 838, + "suspension bridge": 839, + "swab, swob, mop": 840, + "sweatshirt": 841, + "swimming trunks, bathing trunks": 842, + "swing": 843, + "switch, electric switch, electrical switch": 844, + "syringe": 845, + "tabby, tabby cat": 281, + "table lamp": 846, + "tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui": 32, + "tank, army tank, armored combat vehicle, armoured combat vehicle": 847, + "tape player": 848, + "tarantula": 76, + "teapot": 849, + "teddy, teddy bear": 850, + "television, television system": 851, + "tench, Tinca tinca": 0, + "tennis ball": 852, + "terrapin": 36, + "thatch, thatched roof": 853, + "theater curtain, theatre curtain": 854, + "thimble": 855, + "three-toed sloth, ai, Bradypus tridactylus": 364, + "thresher, thrasher, threshing machine": 856, + "throne": 857, + "thunder snake, worm snake, Carphophis amoenus": 52, + "tick": 78, + "tiger beetle": 300, + "tiger cat": 282, + "tiger shark, Galeocerdo cuvieri": 3, + "tiger, Panthera tigris": 292, + "tile roof": 858, + "timber wolf, grey wolf, gray wolf, Canis lupus": 269, + "titi, titi monkey": 380, + "toaster": 859, + "tobacco shop, tobacconist shop, tobacconist": 860, + "toilet seat": 861, + "toilet tissue, toilet paper, bathroom tissue": 999, + "torch": 862, + "totem pole": 863, + "toucan": 96, + "tow truck, tow car, wrecker": 864, + "toy poodle": 265, + "toy terrier": 158, + "toyshop": 865, + "tractor": 866, + "traffic light, traffic signal, stoplight": 920, + "trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi": 867, + "tray": 868, + "tree frog, tree-frog": 31, + "trench coat": 869, + "triceratops": 51, + "tricycle, trike, velocipede": 870, + "trifle": 927, + "trilobite": 69, + "trimaran": 871, + "tripod": 872, + "triumphal arch": 873, + "trolleybus, trolley coach, trackless trolley": 874, + "trombone": 875, + "tub, vat": 876, + "turnstile": 877, + "tusker": 101, + "typewriter keyboard": 878, + "umbrella": 879, + "unicycle, monocycle": 880, + "upright, upright piano": 881, + "vacuum, vacuum cleaner": 882, + "valley, vale": 979, + "vase": 883, + "vault": 884, + "velvet": 885, + "vending machine": 886, + "vestment": 887, + "viaduct": 888, + "vine snake": 59, + "violin, fiddle": 889, + "vizsla, Hungarian pointer": 211, + "volcano": 980, + "volleyball": 890, + "vulture": 23, + "waffle iron": 891, + "walking stick, walkingstick, stick insect": 313, + "wall clock": 892, + "wallaby, brush kangaroo": 104, + "wallet, billfold, notecase, pocketbook": 893, + "wardrobe, closet, press": 894, + "warplane, military plane": 895, + "warthog": 343, + "washbasin, handbasin, washbowl, lavabo, wash-hand basin": 896, + "washer, automatic washer, washing machine": 897, + "water bottle": 898, + "water buffalo, water ox, Asiatic buffalo, Bubalus bubalis": 346, + "water jug": 899, + "water ouzel, dipper": 20, + "water snake": 58, + "water tower": 900, + "weasel": 356, + "web site, website, internet site, site": 916, + "weevil": 307, + "whippet": 172, + "whiptail, whiptail lizard": 41, + "whiskey jug": 901, + "whistle": 902, + "white stork, Ciconia ciconia": 127, + "white wolf, Arctic wolf, Canis lupus tundrarum": 270, + "wig": 903, + "wild boar, boar, Sus scrofa": 342, + "window screen": 904, + "window shade": 905, + "wine bottle": 907, + "wing": 908, + "wire-haired fox terrier": 188, + "wok": 909, + "wolf spider, hunting spider": 77, + "wombat": 106, + "wood rabbit, cottontail, cottontail rabbit": 330, + "wooden spoon": 910, + "wool, woolen, woollen": 911, + "worm fence, snake fence, snake-rail fence, Virginia fence": 912, + "wreck": 913, + "yawl": 914, + "yellow lady's slipper, yellow lady-slipper, Cypripedium calceolus, Cypripedium parviflorum": 986, + "yurt": 915, + "zebra": 340, + "zucchini, courgette": 939 + }, + "layer_type": "basic", + "model_type": "resnet", + "num_channels": 3, + "out_features": [ + "stage4" + ], + "out_indices": [ + 4 + ], + "stage_names": [ + "stem", + "stage1", + "stage2", + "stage3", + "stage4" + ], + "torch_dtype": "float32", + "transformers_version": "4.41.2" +} diff --git a/datasets/train.jsonl b/datasets/train.jsonl new file mode 100644 index 0000000000000000000000000000000000000000..aca106ed965d2db4c7fb2daffe047df532b54c4b --- /dev/null +++ b/datasets/train.jsonl @@ -0,0 +1,2 @@ +{"id": 1, "text": "A", "label": 0} +{"id": 2, "text": "B", "label": 1} diff --git a/datasets/val.jsonl b/datasets/val.jsonl new file mode 100644 index 0000000000000000000000000000000000000000..84f46f45f2b698b6b3ee249944a050b29c734c1b --- /dev/null +++ b/datasets/val.jsonl @@ -0,0 +1,2 @@ +{"id": 3, "text": "C", "label": 1} +{"id": 4, "text": "D", "label": 1} diff --git a/images/airplane.jpg b/images/airplane.jpg new file mode 100644 index 0000000000000000000000000000000000000000..4c2e2161827b46f1dd478c3b9ce954117f49e4f2 --- /dev/null +++ b/images/airplane.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7f40854ecf8edcf2638d2cc5aa8c422d07875478c65514506c81d0200985f6be +size 126139 diff --git a/images/baseball.jpg b/images/baseball.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5f234fe048f18366f8f7dd44b8062208cefaeb80 --- /dev/null +++ b/images/baseball.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fb055f9572e8e059559a950b626a7a731683844f875b66523cbd8d29605d903f +size 276515 diff --git a/images/bear.jpg b/images/bear.jpg new file mode 100644 index 0000000000000000000000000000000000000000..e610bb726a87f3330aadb273dd8a914c32573ed1 --- /dev/null +++ b/images/bear.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:92a0ab00f3a0b9f6de5162449a8b79167363897fb489c172eebc244bccef23cb +size 158661 diff --git a/images/car.jpg b/images/car.jpg new file mode 100644 index 0000000000000000000000000000000000000000..2acb2656b61ced7bf52d1655ba18c508b357b2d6 --- /dev/null +++ b/images/car.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7fc5a10a8b25788b7e9d3de28f7355a1f2456fbb3a4e4db3d8e966b193b05a81 +size 273819 diff --git a/images/dog.jpg b/images/dog.jpg new file mode 100644 index 0000000000000000000000000000000000000000..45c7a9837bdec8b63c0d3e7902d2733296f2cde1 --- /dev/null +++ b/images/dog.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:378763dec71a25c7cb44ff9e345c84ca0fe2d8bf993b88775c360549edf3b733 +size 167853 diff --git a/images/giraffe.jpg b/images/giraffe.jpg new file mode 100644 index 0000000000000000000000000000000000000000..e96ab8592d4acc8aaa188cc97d605c1d50a16af0 --- /dev/null +++ b/images/giraffe.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cbe18fd21bd5691cb27085cda1f10c3c5556d5918fcd04363a5e5b145ee3177b +size 113005 diff --git a/images/horse.jpg b/images/horse.jpg new file mode 100644 index 0000000000000000000000000000000000000000..a6acf9d12284db898696b61a6d03d2e6a8f4621a --- /dev/null +++ b/images/horse.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3bb95ef4d109bf61336656d795b1ba66785c46d23bfdda66a26679cb17da1cf9 +size 187480 diff --git a/images/kitchen.jpg b/images/kitchen.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5c2d001ea92a6a45163c019346d71822bf4c0709 --- /dev/null +++ b/images/kitchen.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:376dbffac6db0b092cc07b43dfc34886725682d8e641caeff54710ac8f61ffb6 +size 94292 diff --git a/images/laptop.jpg b/images/laptop.jpg new file mode 100644 index 0000000000000000000000000000000000000000..aa9a51ef1c69c2d66b12957328b2f1d048bb1fbe --- /dev/null +++ b/images/laptop.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f941bf1156d9fc2831c487a93caf78ae3367ddaa5ba4f6cda4384af13ea91ff0 +size 78595 diff --git a/images/motorcycle.jpg b/images/motorcycle.jpg new file mode 100644 index 0000000000000000000000000000000000000000..d09b21c91f03ddc2c86b2473abbb45b8b4f6bbbc --- /dev/null +++ b/images/motorcycle.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:61b0ee16f913053290e0ee24971a6833b21290ee6a74039a0e0f3faa72dcf61c +size 172938 diff --git a/images/people.jpg b/images/people.jpg new file mode 100644 index 0000000000000000000000000000000000000000..81bff74fc246349d9434d458cb904c0701f18390 --- /dev/null +++ b/images/people.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:aacdd27c1d31c7007fafd83676e6bb66953e8c0de02080671c811093e6058b40 +size 223507 diff --git a/images/sheep.jpg b/images/sheep.jpg new file mode 100644 index 0000000000000000000000000000000000000000..8c91c41510acfdb7e70d0f216cb766df61a46a37 --- /dev/null +++ b/images/sheep.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:319d58f0fa008d076b766671a5b607f94614f6005f773adced54a57567aa28fb +size 227134 diff --git a/images/skateboard.jpg b/images/skateboard.jpg new file mode 100644 index 0000000000000000000000000000000000000000..1273f9cd0ab5e9ca8bef5e88124fcfdb98b0e7b1 --- /dev/null +++ b/images/skateboard.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:769ceca7a3baf1319cffe4ad462561859d1e3f1029034fb87a12ef69acdf91bd +size 147454 diff --git a/images/ski.jpg b/images/ski.jpg new file mode 100644 index 0000000000000000000000000000000000000000..4435bf94edb072212af97fb05eca7531b147f6cf --- /dev/null +++ b/images/ski.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:309105454f28c30038cce8c44d48300b626548477957b3d056bfacf9da3127e4 +size 111229 diff --git a/images/tennis.jpg b/images/tennis.jpg new file mode 100644 index 0000000000000000000000000000000000000000..628cf4f13bd1fb545a0c1a7ea412e71bdae4abf3 --- /dev/null +++ b/images/tennis.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5078da2ea4554eb91703e5fa0339c4d0c875ded1783a8f4ee32ece2b558178c4 +size 137855 diff --git a/images/train.jpg b/images/train.jpg new file mode 100644 index 0000000000000000000000000000000000000000..17401186ed86bd749fa3a945b689d0f62243839b --- /dev/null +++ b/images/train.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:66c6259b826b869e0d2dba078a0de6d9c82617843c2d5448e9b34566bab6f979 +size 213529 diff --git a/images/zebra.jpg b/images/zebra.jpg new file mode 100644 index 0000000000000000000000000000000000000000..6c07a802fe4d8f4e5e37e8a7d06d9e3ab38d220e --- /dev/null +++ b/images/zebra.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c97b8b6f195541932de28e6a79bacd569d65d29e0a32f04274885acc0cab505c +size 245688 diff --git a/model.safetensors b/model.safetensors new file mode 100644 index 0000000000000000000000000000000000000000..51ef6c257a0e47f18154519ac445f4dbda61e2aa --- /dev/null +++ b/model.safetensors @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:197a1c78db3d3041e2a8c0260d96305484d292f00290deedfb490162e7d55ea1 +size 46812328 diff --git a/models/.gitkeep b/models/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/preprocessor_config.json b/preprocessor_config.json new file mode 100644 index 0000000000000000000000000000000000000000..bc30051cf050c01ecfc7a5ffbe20f7228ca24ca8 --- /dev/null +++ b/preprocessor_config.json @@ -0,0 +1,37 @@ +{ + "_valid_processor_keys": [ + "images", + "do_resize", + "size", + "crop_pct", + "resample", + "do_rescale", + "rescale_factor", + "do_normalize", + "image_mean", + "image_std", + "return_tensors", + "data_format", + "input_data_format" + ], + "crop_pct": 0.875, + "do_normalize": true, + "do_rescale": true, + "do_resize": true, + "image_mean": [ + 0.485, + 0.456, + 0.406 + ], + "image_processor_type": "ConvNextImageProcessor", + "image_std": [ + 0.229, + 0.224, + 0.225 + ], + "resample": 3, + "rescale_factor": 0.00392156862745098, + "size": { + "shortest_edge": 224 + } +} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..18fc2b12ee85cf25688ef96cf66840961979b2f7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,24 @@ +torch==2.2.2+cu118 +torchaudio==2.2.2+cu118 +torchdata==0.7.1 +torchtext==0.18.0 +torchvision==0.17.2+cu118 +huggingface_hub==0.23.4 +transformers==4.41.2 +datasets==2.20.0 +evaluate==0.4.2 +tokenizers==0.19.1 +rouge_score==0.1.2 +bitsandbytes==0.43.1 +trl==0.9.4 +peft==0.11.1 +accelerate==0.32.1 +pycocotools==2.0.8 +pytesseract==0.3.10 +diffusers==0.29.2 +sentencepiece==0.2.0 +faiss==1.7.4 +ray==2.31.0 +optuna==3.6.1 +auto_gptq==0.7.1 +optimum==1.20.0 \ No newline at end of file diff --git "a/\353\266\200\353\241\235/\354\230\210\354\240\234 A.01 ~ A.04 \354\235\264\353\257\270\354\247\200 \353\247\244\354\271\255.ipynb" "b/\353\266\200\353\241\235/\354\230\210\354\240\234 A.01 ~ A.04 \354\235\264\353\257\270\354\247\200 \353\247\244\354\271\255.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..beca63b7eba5d012281b08d375eb50d2c30d6740 --- /dev/null +++ "b/\353\266\200\353\241\235/\354\230\210\354\240\234 A.01 ~ A.04 \354\235\264\353\257\270\354\247\200 \353\247\244\354\271\255.ipynb" @@ -0,0 +1,140 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "f7e3e32b", + "metadata": {}, + "outputs": [], + "source": [ + "# !pip3 install faiss-gpu\n", + "# !conda install -c pytorch faiss-gpu\n", + "!pip3 install faiss-cpu" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "da11820c", + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "from datasets import load_dataset\n", + "from transformers import ViTImageProcessor, ViTModel\n", + "\n", + "dataset = load_dataset(\"huggingface/cats-image\")\n", + "image = dataset[\"test\"][\"image\"][0]\n", + "\n", + "model_name = \"google/vit-base-patch16-224\"\n", + "processor = ViTImageProcessor.from_pretrained(model_name)\n", + "model = ViTModel.from_pretrained(model_name)\n", + "\n", + "inputs = processor(image, return_tensors=\"pt\")\n", + "with torch.no_grad():\n", + " outputs = model(inputs[\"pixel_values\"])\n", + "\n", + "print(\"마지막 특징 맵의 형태 :\", outputs[\"last_hidden_state\"].shape)\n", + "print(\"특징 벡터의 차원 수 :\", outputs[\"last_hidden_state\"][:, 0, :].shape)\n", + "print(\"특징 벡터 :\", outputs[\"last_hidden_state\"][:, 0, :])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6d3977dd", + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "import numpy as np\n", + "from datasets import load_dataset\n", + "from transformers import CLIPProcessor, CLIPModel\n", + "\n", + "dataset = load_dataset(\"sasha/dog-food\")\n", + "images = dataset[\"test\"][\"image\"][:100]\n", + "\n", + "model_name = \"openai/clip-vit-base-patch32\"\n", + "processor = CLIPProcessor.from_pretrained(model_name)\n", + "model = CLIPModel.from_pretrained(model_name)\n", + "\n", + "vectors = []\n", + "with torch.no_grad():\n", + " for image in images:\n", + " inputs = processor(images=image, padding=True, return_tensors=\"pt\")\n", + " outputs = model.get_image_features(**inputs)\n", + " vectors.append(outputs.cpu().numpy())\n", + "\n", + "vectors = np.vstack(vectors)\n", + "print(\"이미지 벡터의 shape :\", vectors.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "77af7293", + "metadata": {}, + "outputs": [], + "source": [ + "import faiss\n", + "\n", + "dimension = vectors.shape[-1]\n", + "index = faiss.IndexFlatL2(dimension)\n", + "if torch.cuda.is_available():\n", + " res = faiss.StandardGpuResources()\n", + " index = faiss.index_cpu_to_gpu(res, 0, index)\n", + "\n", + "index.add(vectors)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2b954c1a", + "metadata": {}, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt\n", + "\n", + "search_vector = vectors[0].reshape(1, -1)\n", + "num_neighbors = 5\n", + "distances, indices = index.search(x=search_vector, k=num_neighbors)\n", + "\n", + "fig, axes = plt.subplots(1, num_neighbors + 1, figsize=(15, 5))\n", + "\n", + "axes[0].imshow(images[0])\n", + "axes[0].set_title(\"Input Image\")\n", + "axes[0].axis(\"off\")\n", + "\n", + "for i, idx in enumerate(indices[0]):\n", + " axes[i + 1].imshow(images[idx])\n", + " axes[i + 1].set_title(f\"Match {i + 1}\\nIndex: {idx}\\nDist: {distances[0][i]:.2f}\")\n", + " axes[i + 1].axis(\"off\")\n", + "\n", + "print(\"유사한 벡터의 인덱스 번호:\", indices)\n", + "print(\"유사도 계산 결과:\", distances)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/\353\266\200\353\241\235/\354\230\210\354\240\234 B.03 ~ B.03 \353\240\210\354\235\264 \355\212\240.ipynb" "b/\353\266\200\353\241\235/\354\230\210\354\240\234 B.03 ~ B.03 \353\240\210\354\235\264 \355\212\240.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..40db8a65e9c179427761af53c08bd519c71ccac8 --- /dev/null +++ "b/\353\266\200\353\241\235/\354\230\210\354\240\234 B.03 ~ B.03 \353\240\210\354\235\264 \355\212\240.ipynb" @@ -0,0 +1,171 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "f7e3e32b", + "metadata": {}, + "outputs": [], + "source": [ + "!pip3 install ray[tune] optuna" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "da11820c", + "metadata": {}, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "from transformers import AutoModelForTokenClassification, AutoTokenizer\n", + "\n", + "def preprocess_data(example, tokenizer):\n", + " sentence = \"\".join(example[\"tokens\"]).replace(\"\\xa0\", \" \")\n", + " encoded = tokenizer(\n", + " sentence,\n", + " return_offsets_mapping=True,\n", + " add_special_tokens=False,\n", + " padding=False,\n", + " truncation=False\n", + " )\n", + "\n", + " labels = []\n", + " for offset in encoded.offset_mapping:\n", + " if offset[0] == offset[1]:\n", + " labels.append(-100)\n", + " else:\n", + " labels.append(example[\"ner_tags\"][offset[0]])\n", + " encoded[\"labels\"] = labels\n", + " return encoded\n", + "\n", + "dataset = load_dataset(\"klue\", \"ner\")\n", + "labels = dataset[\"train\"].features[\"ner_tags\"].feature.names\n", + "\n", + "model_name = \"Leo97/KoELECTRA-small-v3-modu-ner\"\n", + "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", + "model = AutoModelForTokenClassification.from_pretrained(\n", + " model_name,\n", + " num_labels=len(labels),\n", + " ignore_mismatched_sizes=True\n", + ")\n", + "\n", + "processed_dataset = dataset.map(\n", + " lambda example: preprocess_data(example, tokenizer),\n", + " batched=False,\n", + " remove_columns=dataset[\"train\"].column_names\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4f0e871c", + "metadata": {}, + "outputs": [], + "source": [ + "from ray import tune\n", + "from functools import partial\n", + "from transformers import Trainer, TrainingArguments\n", + "from transformers.data.data_collator import DataCollatorForTokenClassification\n", + "\n", + "def model_init(model_name, labels):\n", + " return AutoModelForTokenClassification.from_pretrained(\n", + " model_name, num_labels=len(labels), ignore_mismatched_sizes=True\n", + " )\n", + "\n", + "def hp_space(trial):\n", + " return {\n", + " \"learning_rate\": tune.loguniform(1e-5, 1e-4),\n", + " \"weight_decay\": tune.loguniform(1e-5, 1e-1),\n", + " \"num_train_epochs\": tune.choice([1, 2, 3])\n", + " }\n", + "\n", + "def compute_objective(metrics):\n", + " return metrics[\"eval_loss\"]\n", + "\n", + "training_args = TrainingArguments(\n", + " output_dir=\"token-classification-hyperparameter-search\",\n", + " evaluation_strategy=\"epoch\",\n", + " per_device_train_batch_size=32,\n", + " per_device_eval_batch_size=32,\n", + " # learning_rate=1e-4,\n", + " # weight_decay=0.01,\n", + " # num_train_epochs=5,\n", + " seed=42\n", + ")\n", + "\n", + "trainer = Trainer(\n", + " model_init=partial(model_init, model_name=model_name, labels=labels),\n", + " args=training_args,\n", + " train_dataset=processed_dataset[\"train\"],\n", + " eval_dataset=processed_dataset[\"validation\"],\n", + " data_collator=DataCollatorForTokenClassification(tokenizer=tokenizer, padding=True)\n", + ")\n", + "\n", + "best_run = trainer.hyperparameter_search(\n", + " backend=\"ray\",\n", + " n_trials=5,\n", + " direction=\"minimize\",\n", + " hp_space=hp_space,\n", + " compute_objective=compute_objective,\n", + " resources_per_trial={\"cpu\": 2, \"gpu\": 1},\n", + " trial_dirname_creator=lambda trial: str(trial)\n", + ")\n", + "print(best_run.hyperparameters)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f4f1abfb", + "metadata": {}, + "outputs": [], + "source": [ + "from ray.tune.schedulers import ASHAScheduler\n", + "from ray.tune.search.optuna import OptunaSearch\n", + "\n", + "best_run = trainer.hyperparameter_search(\n", + " backend=\"ray\",\n", + " n_trials=5,\n", + " hp_space=hp_space,\n", + " resources_per_trial={\"cpu\": 2, \"gpu\": 1},\n", + " trial_dirname_creator=lambda trial: str(trial),\n", + " search_alg=OptunaSearch(\n", + " metric=\"eval_loss\",\n", + " mode=\"min\"\n", + " ),\n", + " scheduler=ASHAScheduler(\n", + " metric=\"eval_loss\",\n", + " mode=\"min\",\n", + " max_t=1000,\n", + " grace_period=1,\n", + " reduction_factor=2\n", + " )\n", + ")\n", + "print(best_run.hyperparameters)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/\353\266\200\353\241\235/\354\230\210\354\240\234 C.01 ~ C.03 GPTQ.ipynb" "b/\353\266\200\353\241\235/\354\230\210\354\240\234 C.01 ~ C.03 GPTQ.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..92ea2d0d0e3f9177db7642df854d1c1f92b581d0 --- /dev/null +++ "b/\353\266\200\353\241\235/\354\230\210\354\240\234 C.01 ~ C.03 GPTQ.ipynb" @@ -0,0 +1,132 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "f7e3e32b", + "metadata": {}, + "outputs": [], + "source": [ + "!pip3 install auto-gptq accelerate optimum" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "da11820c", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import GPTQConfig\n", + "from transformers import AutoModelForCausalLM, AutoTokenizer\n", + "\n", + "model_name = \"facebook/opt-125m\"\n", + "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", + "\n", + "quantization_config = GPTQConfig(\n", + " bits=4,\n", + " dataset=\"c4\",\n", + " tokenizer=tokenizer\n", + ")\n", + "\n", + "quantized_model = AutoModelForCausalLM.from_pretrained(\n", + " model_name,\n", + " device_map=\"auto\",\n", + " quantization_config=quantization_config\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4f0e871c", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import pipeline\n", + "\n", + "origin_generator = pipeline(\"text-generation\", model=\"facebook/opt-125m\")\n", + "quantized_generator = pipeline(\"text-generation\", model=quantized_model, tokenizer=tokenizer)\n", + "\n", + "input_text_list = [\n", + " \"In the future, technology wil\",\n", + " \"What are we having for dinner?\",\n", + " \"What day comes after Monday?\"\n", + "]\n", + "\n", + "print(\"원본 모델의 출력 결과:\")\n", + "for input_text in input_text_list:\n", + " print(origin_generator(input_text))\n", + "print(\"양자화 모델의 출력 결과:\")\n", + "for input_text in input_text_list:\n", + " print(quantized_generator(input_text))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f4f1abfb", + "metadata": {}, + "outputs": [], + "source": [ + "import time\n", + "import numpy as np\n", + "\n", + "def measure_inference_time(generator, input_text, iterations=10):\n", + " times = []\n", + " for _ in range(iterations):\n", + " start_time = time.time()\n", + " generator(input_text)\n", + " end_time = time.time()\n", + " times.append(end_time - start_time)\n", + " avg_time = np.mean(times)\n", + " return avg_time\n", + "\n", + "def calculate_model_size(model):\n", + " total_params = sum(p.numel() for p in model.parameters())\n", + " total_memory = sum(p.numel() * p.element_size() for p in model.parameters())\n", + " total_memory_mb = total_memory / (1024 ** 2)\n", + " return total_memory_mb, total_params\n", + "\n", + "test_input = \"Once upon a time in a land far, far away, there was a small village.\"\n", + "\n", + "size_original, total_params_original = calculate_model_size(origin_generator.model)\n", + "avg_inference_time_original = measure_inference_time(origin_generator, test_input)\n", + "\n", + "size_quantized, total_params_quantized = calculate_model_size(quantized_generator.model)\n", + "avg_inference_time_quantized = measure_inference_time(quantized_generator, test_input)\n", + "\n", + "print(\"원본 모델:\")\n", + "print(f\"- 매개변수 개수: {total_params_original:,}\")\n", + "print(f\"- 모델 크기: {size_original:.2f} MB\")\n", + "print(f\"- 평균 추론 시간: {avg_inference_time_original:.4f} sec\")\n", + "\n", + "print(\"양자화 모델:\")\n", + "print(f\"- 매개변수 개수: {total_params_quantized:,}\")\n", + "print(f\"- 모델 크기: {size_quantized:.2f} MB\")\n", + "print(f\"- 평균 추론 시간: {avg_inference_time_quantized:.4f} sec\")" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/\353\266\200\353\241\235/\354\230\210\354\240\234 D.01 GPU \353\266\204\354\202\260 \355\225\231\354\212\265.ipynb" "b/\353\266\200\353\241\235/\354\230\210\354\240\234 D.01 GPU \353\266\204\354\202\260 \355\225\231\354\212\265.ipynb" new file mode 100644 index 0000000000000000000000000000000000000000..b4adcec336c0f1dae46ad7b395b776b1fc2dfe44 --- /dev/null +++ "b/\353\266\200\353\241\235/\354\230\210\354\240\234 D.01 GPU \353\266\204\354\202\260 \355\225\231\354\212\265.ipynb" @@ -0,0 +1,156 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "f7e3e32b", + "metadata": {}, + "outputs": [], + "source": [ + "!pip3 install accelerate" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b8dcb51f", + "metadata": {}, + "outputs": [], + "source": [ + "# TPU 환경\n", + "# pip3 uninstall -y tensorflow\n", + "# pip3 install accelerate==0.26.0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4875ef3c", + "metadata": {}, + "outputs": [], + "source": [ + "# TPU 환경\n", + "# import os\n", + "# os.environ[\"TPU_NAME\"] = os.environ[\"TPU_WORKER_ID\"]\n", + "# os.environ.pop('TPU_PROCESS_ADDRESSES')\n", + "# os.environ.pop('CLOUD_TPU_TASK_ID')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "da11820c", + "metadata": {}, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "from transformers import BartTokenizerFast, BartForConditionalGeneration\n", + "\n", + "def preprocess_data(example, tokenizer):\n", + " return tokenizer(\n", + " example[\"document\"],\n", + " text_target=example[\"summary\"],\n", + " truncation=True\n", + " )\n", + "\n", + "model_name = \"gogamza/kobart-base-v2\"\n", + "tokenizer = BartTokenizerFast.from_pretrained(model_name)\n", + "model = BartForConditionalGeneration.from_pretrained(model_name)\n", + "\n", + "dataset = load_dataset(\"daekeun-ml/naver-news-summarization-ko\")\n", + "print(dataset)\n", + "\n", + "tokenizer.model_max_length = model.config.max_position_embeddings\n", + "processed_dataset = dataset.map(\n", + " lambda example: preprocess_data(example, tokenizer),\n", + " batched=True,\n", + " remove_columns=dataset[\"train\"].column_names\n", + ")\n", + "\n", + "sample = processed_dataset[\"train\"][\"labels\"][0]\n", + "print(sample)\n", + "print(tokenizer.decode(sample))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f4f1abfb", + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "from torch.utils.data import DataLoader\n", + "from transformers import DataCollatorForSeq2Seq\n", + "from accelerate.utils import set_seed\n", + "from accelerate import Accelerator, notebook_launcher\n", + "\n", + "def create_dataloaders(batch_size):\n", + " seq2seq_collator = DataCollatorForSeq2Seq(\n", + " tokenizer=tokenizer,\n", + " padding=\"longest\",\n", + " return_tensors=\"pt\"\n", + " )\n", + " train_dataloader = DataLoader(\n", + " processed_dataset[\"train\"],\n", + " shuffle=True,\n", + " batch_size=batch_size,\n", + " collate_fn=seq2seq_collator\n", + " )\n", + " eval_dataloader = DataLoader(\n", + " processed_dataset[\"validation\"],\n", + " shuffle=False,\n", + " batch_size=batch_size,\n", + " collate_fn=seq2seq_collator\n", + " )\n", + " return train_dataloader, eval_dataloader\n", + "\n", + "def training_loop(model, epochs, seed, mixed_precision, batch_size, logging_steps):\n", + " set_seed(seed)\n", + " accelerator = Accelerator(mixed_precision=mixed_precision)\n", + " train_dataloader, eval_dataloader = create_dataloaders(batch_size)\n", + "\n", + " optimizer = torch.optim.Adam(params=model.parameters(), lr=5e-5)\n", + " model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare(\n", + " model, optimizer, train_dataloader, eval_dataloader\n", + " )\n", + "\n", + " for epoch in range(epochs):\n", + " model.train()\n", + " for step, batch in enumerate(train_dataloader):\n", + " outputs = model(**batch)\n", + " loss = outputs.loss\n", + " accelerator.backward(loss)\n", + " optimizer.step()\n", + " optimizer.zero_grad()\n", + " if step % logging_steps == 0:\n", + " accelerator.print(f\"epoch {epoch}: {loss.item()}\")\n", + "\n", + "model = BartForConditionalGeneration.from_pretrained(model_name)\n", + "args = (model, 5, 2024, \"fp16\", 8, 100)\n", + "notebook_launcher(training_loop, args, num_processes=4)" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}