Initial commit for waifu notebook master
authoranon <anon@anon.anon>
Sun, 5 Jun 2022 17:23:13 +0000 (17:23 +0000)
committeranon <anon@anon.anon>
Sun, 5 Jun 2022 17:23:13 +0000 (17:23 +0000)
README.md [new file with mode: 0644]
waifu-notebook.ipynb [new file with mode: 0644]

diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..e845566
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+README
diff --git a/waifu-notebook.ipynb b/waifu-notebook.ipynb
new file mode 100644 (file)
index 0000000..8be8d99
--- /dev/null
@@ -0,0 +1,135 @@
+{
+  "nbformat": 4,
+  "nbformat_minor": 0,
+  "metadata": {
+    "colab": {
+      "name": "project_hentai_ai_waifu.ipynb",
+      "provenance": []
+    },
+    "kernelspec": {
+      "name": "python3",
+      "display_name": "Python 3"
+    },
+    "language_info": {
+      "name": "python"
+    },
+    "accelerator": "GPU"
+  },
+  "cells": [
+    {
+      "cell_type": "code",
+      "execution_count": null,
+      "metadata": {
+        "id": "QaGZdBqh5reK"
+      },
+      "outputs": [],
+      "source": [
+        "# Click Runtime -> Change runtime type -> select Hardware accelerator: GPU\n",
+        "!pip install -Uqq fastbook\n",
+        "import fastbook\n",
+        "fastbook.setup_book()"
+      ]
+    },
+    {
+      "cell_type": "code",
+      "source": [
+        "from fastai.vision.all import *\n",
+        "import pandas as pd\n",
+        "from google.colab import drive\n",
+        "base_path = '/content/drive'\n",
+        "drive.mount(base_path)\n",
+        "img_path = f\"{base_path}/MyDrive/dataset_thighs_cropped\""
+      ],
+      "metadata": {
+        "id": "F-3tVQpn59D_"
+      },
+      "execution_count": null,
+      "outputs": []
+    },
+    {
+      "cell_type": "code",
+      "source": [
+        "# https://docs.fast.ai/vision.data.html#ImageDataLoaders.from_df\n",
+        "# https://eagerai.github.io/fastai/reference/ImageDataLoaders_from_df.html\n",
+        "size = 1000\n",
+        "user = \"UserC\" # [UserA, UserB, UserC]\n",
+        "df = pd.read_csv(f'{img_path}/{user}.csv')[:size]\n",
+        "print(df['label'].value_counts())\n",
+        "train_df = df.iloc[:int(size*0.9)].copy()\n",
+        "test_df = df.iloc[int(size*0.9):].copy() # 10% testing set"
+      ],
+      "metadata": {
+        "id": "yZzZlcb1EvBT"
+      },
+      "execution_count": null,
+      "outputs": []
+    },
+    {
+      "cell_type": "code",
+      "source": [
+        "# https://medium.com/unpackai/data-augmentation-with-fastai-library-b4f8ffb6f00d\n",
+        "# https://rdrr.io/cran/fastai/man/aug_transforms.html\n",
+        "\n",
+        "dls = ImageDataLoaders.from_df(train_df, img_path, bs=10, valid_pct = 0.11111111112 # 10% validation set\n",
+        "                               ,item_tfms=RandomResizedCrop(224, min_scale=0.75)\n",
+        "                               ,batch_tfms=aug_transforms(min_zoom=1.0, max_zoom=1.8, do_flip=True, flip_vert=True, max_rotate=70, max_lighting=0.5, p_lighting=0.4)\n",
+        "                               )\n",
+        "dls.show_batch(nrows=3, ncols=3)\n",
+        "#dls.train.show_batch(max_n=10, nrows=2, unique=True) # Showing batch_tfms"
+      ],
+      "metadata": {
+        "id": "IvTcHowo6sfh"
+      },
+      "execution_count": null,
+      "outputs": []
+    },
+    {
+      "cell_type": "code",
+      "source": [
+        "learn = vision_learner(dls, resnet34, metrics=error_rate)\n",
+        "learn.fine_tune(20)\n",
+        "#learn.show_results()"
+      ],
+      "metadata": {
+        "id": "JyjHOdM5wvQw"
+      },
+      "execution_count": null,
+      "outputs": []
+    },
+    {
+      "cell_type": "code",
+      "source": [
+        "# https://forums.fast.ai/t/saving-and-using-a-model/47216/3\n",
+        "from datetime import datetime\n",
+        "now = datetime.now()\n",
+        "model_path = f'{base_path}/MyDrive/ai_models/wAiFu-{now.strftime(\"%Y%m%d-%H%M%S\")}-{user}.pkl'\n",
+        "learn.export(model_path)\n",
+        "learn = load_learner(model_path)\n",
+        "\n",
+        "#https://forums.fast.ai/t/a-brief-guide-to-test-sets-in-v2-you-can-do-labelled-now-too/57054\n",
+        "test_dl = dls.test_dl(test_df, with_labels=True)\n",
+        "#test_dl.show_batch(nrows=3, ncols=3)\n",
+        "\n",
+        "learn.validate(dl=test_dl)\n",
+        "interp = ClassificationInterpretation.from_learner(learn, dl=test_dl)\n",
+        "interp.plot_confusion_matrix(figsize=(12,12), dpi=80)"
+      ],
+      "metadata": {
+        "id": "1wCCrFSKgpTo"
+      },
+      "execution_count": null,
+      "outputs": []
+    },
+    {
+      "cell_type": "code",
+      "source": [
+        "learn.show_results(dl=test_dl)"
+      ],
+      "metadata": {
+        "id": "XgV9swnOUYnK"
+      },
+      "execution_count": null,
+      "outputs": []
+    }
+  ]
+}
\ No newline at end of file