diff --git a/Untitled.ipynb b/Untitled.ipynb new file mode 100644 index 0000000..7bbdbfe --- /dev/null +++ b/Untitled.ipynb @@ -0,0 +1,162 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "id": "a2c41897", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2022-06-27 17:55:39.372674: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory\n", + "2022-06-27 17:55:39.372709: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/python/compat/v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.\n", + "Instructions for updating:\n", + "non-resource variables are not supported in the long term\n", + "Imported model (for Places365, 128x128 images)\n" + ] + } + ], + "source": [ + "import tensorflow.compat.v1 as tf\n", + "tf.disable_v2_behavior()\n", + "tf.reset_default_graph()\n", + "import numpy as np\n", + "from PIL import Image\n", + "import src.model\n", + "import src.util\n", + "import os\n", + "import sys" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "594228ab", + "metadata": {}, + "outputs": [], + "source": [ + "model_PATH='/home/jovyan/work/src/output/models/model2000.ckpt'" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "0f45afcf", + "metadata": {}, + "outputs": [], + "source": [ + "import time\n", + "def run_time(func):\n", + " def inner(model, image, question):\n", + " back = func(model, image, question)\n", + " print(\"Runned time: {} s\".format(round((time.time() - t)/10, 3)))\n", + " return back\n", + " t = time.time()\n", + " return inner" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "db3b4a43", + "metadata": {}, + "outputs": [], + "source": [ + "def load_demo_image(in_PATH):\n", + " img = np.array(Image.open(in_PATH).convert('RGB'))[np.newaxis] / 255.0\n", + " img_p = util.preprocess_images_outpainting(img)\n", + " return img_p" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "1252a38c", + "metadata": {}, + "outputs": [], + "source": [ + "def image_to_path(img):\n", + " resize_img = img\n", + " path = uuid.uuid4().hex + '.png'\n", + " resize_img.save(path)\n", + " return path" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "5e7e4c55", + "metadata": {}, + "outputs": [], + "source": [ + "@run_time\n", + "def inference(model_PATH, img_p):\n", + " G_Z = tf.placeholder(tf.float32, shape=[None, IMAGE_SZ, IMAGE_SZ, 4], name='G_Z')\n", + " G_sample = model.generator(G_Z)\n", + " \n", + " saver = tf.train.Saver()\n", + " with tf.Session() as sess:\n", + " saver.restore(sess, model_PATH)\n", + " output, = sess.run([G_sample], feed_dict={G_Z: img_p})\n", + " img_norm = (output[0] * 255.0).astype(np.uint8)\n", + " img = Image.fromarray(img_norm, 'RGB')\n", + " #util.save_image(output[0], out_PATH)\n", + " return img" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "c4224744", + "metadata": {}, + "outputs": [], + "source": [ + "def handle(conf):\n", + " \"\"\"\n", + " 该方法是部署之后,其他人调用你的服务时候的处理方法。\n", + " 请按规范填写参数结构,这样我们就能替你自动生成配置文件,方便其他人的调用。\n", + " 范例:\n", + " params['key'] = value # value_type: str # description: some description\n", + " value_type 可以选择:img, video, audio, str, int, float, [int], [str], [float]\n", + " 参数请放到params字典中,我们会自动解析该变量。\n", + " \"\"\"\n", + " base64_str = conf['Photo']\n", + " image = load_demo_image(base64_str)\n", + " res = inference(model_PATH, image)\n", + " image_str = image_to_path(res)\n", + " return {'Output': res}\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.7.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/handler.py b/handler.py index e361aa2..229249c 100644 --- a/handler.py +++ b/handler.py @@ -3,8 +3,8 @@ tf.reset_default_graph() import numpy as np from PIL import Image -import model -import util +import src.model +import src.util import os import sys @@ -54,8 +54,8 @@ 参数请放到params字典中,我们会自动解析该变量。 """ base64_str = conf['Photo'] - image = load_demo_image(base64_str, image_size, device) + image = load_demo_image(base64_str) res = inference(model_PATH, image) image_str = image_to_path(res) - return {'Output': res} + return {'Output': image_str} diff --git a/src/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/src/.ipynb_checkpoints/Untitled-checkpoint.ipynb deleted file mode 100644 index 21a6d1e..0000000 --- a/src/.ipynb_checkpoints/Untitled-checkpoint.ipynb +++ /dev/null @@ -1,162 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 3, - "id": "a0f197c0", - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2022-06-27 17:55:39.372674: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory\n", - "2022-06-27 17:55:39.372709: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/python/compat/v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.\n", - "Instructions for updating:\n", - "non-resource variables are not supported in the long term\n", - "Imported model (for Places365, 128x128 images)\n" - ] - } - ], - "source": [ - "import tensorflow.compat.v1 as tf\n", - "tf.disable_v2_behavior()\n", - "tf.reset_default_graph()\n", - "import numpy as np\n", - "from PIL import Image\n", - "import model\n", - "import util\n", - "import os\n", - "import sys" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "35638e2a", - "metadata": {}, - "outputs": [], - "source": [ - "model_PATH='/home/jovyan/work/src/output/models/model2000.ckpt'" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "bfa0229a", - "metadata": {}, - "outputs": [], - "source": [ - "import time\n", - "def run_time(func):\n", - " def inner(model, image, question):\n", - " back = func(model, image, question)\n", - " print(\"Runned time: {} s\".format(round((time.time() - t)/10, 3)))\n", - " return back\n", - " t = time.time()\n", - " return inner" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "57494ceb", - "metadata": {}, - "outputs": [], - "source": [ - "def load_demo_image(in_PATH):\n", - " img = np.array(Image.open(in_PATH).convert('RGB'))[np.newaxis] / 255.0\n", - " img_p = util.preprocess_images_outpainting(img)\n", - " return img_p" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "d1ab22e6", - "metadata": {}, - "outputs": [], - "source": [ - "def image_to_path(img):\n", - " resize_img = img\n", - " path = uuid.uuid4().hex + '.png'\n", - " resize_img.save(path)\n", - " return path" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "4a7370fa", - "metadata": {}, - "outputs": [], - "source": [ - "@run_time\n", - "def inference(model_PATH, img_p):\n", - " G_Z = tf.placeholder(tf.float32, shape=[None, IMAGE_SZ, IMAGE_SZ, 4], name='G_Z')\n", - " G_sample = model.generator(G_Z)\n", - " \n", - " saver = tf.train.Saver()\n", - " with tf.Session() as sess:\n", - " saver.restore(sess, model_PATH)\n", - " output, = sess.run([G_sample], feed_dict={G_Z: img_p})\n", - " img_norm = (output[0] * 255.0).astype(np.uint8)\n", - " img = Image.fromarray(img_norm, 'RGB')\n", - " #util.save_image(output[0], out_PATH)\n", - " return img" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "b03bba02", - "metadata": {}, - "outputs": [], - "source": [ - "def handle(conf):\n", - " \"\"\"\n", - " 该方法是部署之后,其他人调用你的服务时候的处理方法。\n", - " 请按规范填写参数结构,这样我们就能替你自动生成配置文件,方便其他人的调用。\n", - " 范例:\n", - " params['key'] = value # value_type: str # description: some description\n", - " value_type 可以选择:img, video, audio, str, int, float, [int], [str], [float]\n", - " 参数请放到params字典中,我们会自动解析该变量。\n", - " \"\"\"\n", - " base64_str = conf['Photo']\n", - " image = load_demo_image(base64_str, image_size, device)\n", - " res = inference(model_PATH, image)\n", - " image_str = image_to_path(res)\n", - " return {'Output': res}\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.7.5" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/src/Untitled.ipynb b/src/Untitled.ipynb deleted file mode 100644 index cdb0b6b..0000000 --- a/src/Untitled.ipynb +++ /dev/null @@ -1,162 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 3, - "id": "29291686", - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2022-06-27 17:55:39.372674: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory\n", - "2022-06-27 17:55:39.372709: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/python/compat/v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.\n", - "Instructions for updating:\n", - "non-resource variables are not supported in the long term\n", - "Imported model (for Places365, 128x128 images)\n" - ] - } - ], - "source": [ - "import tensorflow.compat.v1 as tf\n", - "tf.disable_v2_behavior()\n", - "tf.reset_default_graph()\n", - "import numpy as np\n", - "from PIL import Image\n", - "import model\n", - "import util\n", - "import os\n", - "import sys" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "67b9aae4", - "metadata": {}, - "outputs": [], - "source": [ - "model_PATH='/home/jovyan/work/src/output/models/model2000.ckpt'" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "058a509a", - "metadata": {}, - "outputs": [], - "source": [ - "import time\n", - "def run_time(func):\n", - " def inner(model, image, question):\n", - " back = func(model, image, question)\n", - " print(\"Runned time: {} s\".format(round((time.time() - t)/10, 3)))\n", - " return back\n", - " t = time.time()\n", - " return inner" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "7dd82ead", - "metadata": {}, - "outputs": [], - "source": [ - "def load_demo_image(in_PATH):\n", - " img = np.array(Image.open(in_PATH).convert('RGB'))[np.newaxis] / 255.0\n", - " img_p = util.preprocess_images_outpainting(img)\n", - " return img_p" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "379d2efd", - "metadata": {}, - "outputs": [], - "source": [ - "def image_to_path(img):\n", - " resize_img = img\n", - " path = uuid.uuid4().hex + '.png'\n", - " resize_img.save(path)\n", - " return path" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "70d5031c", - "metadata": {}, - "outputs": [], - "source": [ - "@run_time\n", - "def inference(model_PATH, img_p):\n", - " G_Z = tf.placeholder(tf.float32, shape=[None, IMAGE_SZ, IMAGE_SZ, 4], name='G_Z')\n", - " G_sample = model.generator(G_Z)\n", - " \n", - " saver = tf.train.Saver()\n", - " with tf.Session() as sess:\n", - " saver.restore(sess, model_PATH)\n", - " output, = sess.run([G_sample], feed_dict={G_Z: img_p})\n", - " img_norm = (output[0] * 255.0).astype(np.uint8)\n", - " img = Image.fromarray(img_norm, 'RGB')\n", - " #util.save_image(output[0], out_PATH)\n", - " return img" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "d7439afa", - "metadata": {}, - "outputs": [], - "source": [ - "def handle(conf):\n", - " \"\"\"\n", - " 该方法是部署之后,其他人调用你的服务时候的处理方法。\n", - " 请按规范填写参数结构,这样我们就能替你自动生成配置文件,方便其他人的调用。\n", - " 范例:\n", - " params['key'] = value # value_type: str # description: some description\n", - " value_type 可以选择:img, video, audio, str, int, float, [int], [str], [float]\n", - " 参数请放到params字典中,我们会自动解析该变量。\n", - " \"\"\"\n", - " base64_str = conf['Photo']\n", - " image = load_demo_image(base64_str, image_size, device)\n", - " res = inference(model_PATH, image)\n", - " image_str = image_to_path(res)\n", - " return {'Output': res}\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.7.5" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -}