From ebf9633723074ed838c017072f02ea657a783b33 Mon Sep 17 00:00:00 2001 From: frankknoll Date: Thu, 16 Mar 2023 15:17:57 +0100 Subject: [PATCH] updating --- src/captcha.ipynb | 99 ++++++++++++++++++++++++++++------------- src/captcha/ModelDAO.py | 6 +-- 2 files changed, 72 insertions(+), 33 deletions(-) diff --git a/src/captcha.ipynb b/src/captcha.ipynb index 196fbbd6b31..5c7adb68f4c 100644 --- a/src/captcha.ipynb +++ b/src/captcha.ipynb @@ -25,8 +25,10 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, + "execution_count": null, + "metadata": { + "id": "ioGwCR3Xl31V" + }, "outputs": [], "source": [ "import sys\n", @@ -35,8 +37,10 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": {}, + "execution_count": null, + "metadata": { + "id": "l-coMy_2l31X" + }, "outputs": [], "source": [ "def isInColab():\n", @@ -49,8 +53,10 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": {}, + "execution_count": null, + "metadata": { + "id": "goO0feQwl31Y" + }, "outputs": [], "source": [ "inColab = isInColab()" @@ -59,7 +65,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "nsE9VWCel31Z" + }, "outputs": [], "source": [ "if inColab:\n", @@ -71,13 +79,27 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "l9qhlDVNl31b" + }, "outputs": [], "source": [ - "if inColab:\n", - " sys.path.insert(0, '/content/HowBadIsMyBatch/src')" + "import os\n", + "srcPath = '/content/HowBadIsMyBatch/src' if inColab else os.getcwd()" ] }, + { + "cell_type": "code", + "source": [ + "if inColab:\n", + " sys.path.insert(0, srcPath)" + ], + "metadata": { + "id": "c-2fE6vZsD7a" + }, + "execution_count": null, + "outputs": [] + }, { "cell_type": "code", "execution_count": null, @@ -86,7 +108,6 @@ }, "outputs": [], "source": [ - "import os\n", "import numpy as np\n", "from pathlib import Path\n", "import tensorflow as tf\n", @@ -104,7 +125,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "BWqAvnVOl31d" + }, "outputs": [], "source": [ "from pathlib import Path\n", @@ -223,7 +246,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "HEKh6eval31k" + }, "outputs": [], "source": [ "def printLayers(model):\n", @@ -249,6 +274,21 @@ " return accuracy.result().numpy()" ] }, + { + "cell_type": "code", + "source": [ + "def saveModel(srcPath, modelDAO, model):\n", + " modelFilepath = f'{srcPath}/captcha/{model.name}'\n", + " modelDAO.saveModel(model, modelFilepath)\n", + " if inColab:\n", + " GoogleDriveManager.uploadFolderToGoogleDrive(model.name)" + ], + "metadata": { + "id": "WG2rSl9nxZQ0" + }, + "execution_count": null, + "outputs": [] + }, { "cell_type": "markdown", "metadata": { @@ -303,6 +343,7 @@ "outputs": [], "source": [ "if inColab:\n", + " !apt-get update\n", " !sudo apt install ttf-mscorefonts-installer\n", " !sudo fc-cache -f\n", " !fc-match Arial" @@ -318,8 +359,8 @@ "source": [ "# \"We generate 200,000 images for base model pre-training\"\n", "captchaGenerator = CaptchaGenerator(\n", - " numCaptchas = 50, # 50, # 200000,\n", - " dataDir = Path(\"captchas/generated/VAERS/\"))" + " numCaptchas = 200000, # 50, # 200000,\n", + " dataDir = Path(srcPath + '/captchas/generated/VAERS/'))" ] }, { @@ -395,9 +436,7 @@ }, "outputs": [], "source": [ - "modelDAO.saveModel(model)\n", - "if inColab:\n", - " GoogleDriveManager.uploadFolderToGoogleDrive(model.name)" + "saveModel(srcPath, modelDAO, model)" ] }, { @@ -460,7 +499,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "qZvn1k2Ul31v" + }, "outputs": [], "source": [ "modelName, numTrainableLayers = 'MobileNetV3Small', 104\n", @@ -476,7 +517,7 @@ "outputs": [], "source": [ "# FK-TODO: DRY with VAERSFileDownloader\n", - "modelFilepath = f'{os.getcwd()}/captcha/{modelName}'\n", + "modelFilepath = f'{srcPath}/captcha/{modelName}'\n", "model = modelDAO.loadModel(modelFilepath)\n", "model.summary(show_trainable=True)" ] @@ -584,21 +625,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], "source": [ - "modelDAO.saveModel(model)\n", - "if inColab:\n", - " GoogleDriveManager.uploadFolderToGoogleDrive(model.name)" - ] + "saveModel(srcPath, modelDAO, model)" + ], + "metadata": { + "id": "FpJTHU6dxOVy" + }, + "execution_count": null, + "outputs": [] } ], "metadata": { "accelerator": "GPU", "colab": { - "collapsed_sections": [], - "name": "captcha.ipynb", "private_outputs": true, "provenance": [] }, @@ -623,4 +662,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/src/captcha/ModelDAO.py b/src/captcha/ModelDAO.py index 6b095a249ef..fe57bfd71cc 100644 --- a/src/captcha/ModelDAO.py +++ b/src/captcha/ModelDAO.py @@ -4,9 +4,9 @@ import shutil class ModelDAO: - def saveModel(self, model): - shutil.rmtree(model.name, ignore_errors = True) - model.save(model.name) + def saveModel(self, model, modelFilepath): + shutil.rmtree(modelFilepath, ignore_errors = True) + model.save(modelFilepath) def loadModel(self, modelFilepath): return keras.models.load_model(modelFilepath)