user_wwq
3 years ago
| 0 | { | |
| 1 | "cells": [ | |
| 2 | { | |
| 3 | "cell_type": "code", | |
| 4 | "execution_count": 3, | |
| 5 | "id": "a2c41897", | |
| 6 | "metadata": {}, | |
| 7 | "outputs": [ | |
| 8 | { | |
| 9 | "name": "stderr", | |
| 10 | "output_type": "stream", | |
| 11 | "text": [ | |
| 12 | "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", | |
| 13 | "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" | |
| 14 | ] | |
| 15 | }, | |
| 16 | { | |
| 17 | "name": "stdout", | |
| 18 | "output_type": "stream", | |
| 19 | "text": [ | |
| 20 | "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", | |
| 21 | "Instructions for updating:\n", | |
| 22 | "non-resource variables are not supported in the long term\n", | |
| 23 | "Imported model (for Places365, 128x128 images)\n" | |
| 24 | ] | |
| 25 | } | |
| 26 | ], | |
| 27 | "source": [ | |
| 28 | "import tensorflow.compat.v1 as tf\n", | |
| 29 | "tf.disable_v2_behavior()\n", | |
| 30 | "tf.reset_default_graph()\n", | |
| 31 | "import numpy as np\n", | |
| 32 | "from PIL import Image\n", | |
| 33 | "import src.model\n", | |
| 34 | "import src.util\n", | |
| 35 | "import os\n", | |
| 36 | "import sys" | |
| 37 | ] | |
| 38 | }, | |
| 39 | { | |
| 40 | "cell_type": "code", | |
| 41 | "execution_count": 4, | |
| 42 | "id": "594228ab", | |
| 43 | "metadata": {}, | |
| 44 | "outputs": [], | |
| 45 | "source": [ | |
| 46 | "model_PATH='/home/jovyan/work/src/output/models/model2000.ckpt'" | |
| 47 | ] | |
| 48 | }, | |
| 49 | { | |
| 50 | "cell_type": "code", | |
| 51 | "execution_count": 9, | |
| 52 | "id": "0f45afcf", | |
| 53 | "metadata": {}, | |
| 54 | "outputs": [], | |
| 55 | "source": [ | |
| 56 | "import time\n", | |
| 57 | "def run_time(func):\n", | |
| 58 | " def inner(model, image, question):\n", | |
| 59 | " back = func(model, image, question)\n", | |
| 60 | " print(\"Runned time: {} s\".format(round((time.time() - t)/10, 3)))\n", | |
| 61 | " return back\n", | |
| 62 | " t = time.time()\n", | |
| 63 | " return inner" | |
| 64 | ] | |
| 65 | }, | |
| 66 | { | |
| 67 | "cell_type": "code", | |
| 68 | "execution_count": 8, | |
| 69 | "id": "db3b4a43", | |
| 70 | "metadata": {}, | |
| 71 | "outputs": [], | |
| 72 | "source": [ | |
| 73 | "def load_demo_image(in_PATH):\n", | |
| 74 | " img = np.array(Image.open(in_PATH).convert('RGB'))[np.newaxis] / 255.0\n", | |
| 75 | " img_p = util.preprocess_images_outpainting(img)\n", | |
| 76 | " return img_p" | |
| 77 | ] | |
| 78 | }, | |
| 79 | { | |
| 80 | "cell_type": "code", | |
| 81 | "execution_count": 11, | |
| 82 | "id": "1252a38c", | |
| 83 | "metadata": {}, | |
| 84 | "outputs": [], | |
| 85 | "source": [ | |
| 86 | "def image_to_path(img):\n", | |
| 87 | " resize_img = img\n", | |
| 88 | " path = uuid.uuid4().hex + '.png'\n", | |
| 89 | " resize_img.save(path)\n", | |
| 90 | " return path" | |
| 91 | ] | |
| 92 | }, | |
| 93 | { | |
| 94 | "cell_type": "code", | |
| 95 | "execution_count": 6, | |
| 96 | "id": "5e7e4c55", | |
| 97 | "metadata": {}, | |
| 98 | "outputs": [], | |
| 99 | "source": [ | |
| 100 | "@run_time\n", | |
| 101 | "def inference(model_PATH, img_p):\n", | |
| 102 | " G_Z = tf.placeholder(tf.float32, shape=[None, IMAGE_SZ, IMAGE_SZ, 4], name='G_Z')\n", | |
| 103 | " G_sample = model.generator(G_Z)\n", | |
| 104 | " \n", | |
| 105 | " saver = tf.train.Saver()\n", | |
| 106 | " with tf.Session() as sess:\n", | |
| 107 | " saver.restore(sess, model_PATH)\n", | |
| 108 | " output, = sess.run([G_sample], feed_dict={G_Z: img_p})\n", | |
| 109 | " img_norm = (output[0] * 255.0).astype(np.uint8)\n", | |
| 110 | " img = Image.fromarray(img_norm, 'RGB')\n", | |
| 111 | " #util.save_image(output[0], out_PATH)\n", | |
| 112 | " return img" | |
| 113 | ] | |
| 114 | }, | |
| 115 | { | |
| 116 | "cell_type": "code", | |
| 117 | "execution_count": 12, | |
| 118 | "id": "c4224744", | |
| 119 | "metadata": {}, | |
| 120 | "outputs": [], | |
| 121 | "source": [ | |
| 122 | "def handle(conf):\n", | |
| 123 | " \"\"\"\n", | |
| 124 | " 该方法是部署之后,其他人调用你的服务时候的处理方法。\n", | |
| 125 | " 请按规范填写参数结构,这样我们就能替你自动生成配置文件,方便其他人的调用。\n", | |
| 126 | " 范例:\n", | |
| 127 | " params['key'] = value # value_type: str # description: some description\n", | |
| 128 | " value_type 可以选择:img, video, audio, str, int, float, [int], [str], [float]\n", | |
| 129 | " 参数请放到params字典中,我们会自动解析该变量。\n", | |
| 130 | " \"\"\"\n", | |
| 131 | " base64_str = conf['Photo']\n", | |
| 132 | " image = load_demo_image(base64_str)\n", | |
| 133 | " res = inference(model_PATH, image)\n", | |
| 134 | " image_str = image_to_path(res)\n", | |
| 135 | " return {'Output': res}\n", | |
| 136 | " " | |
| 137 | ] | |
| 138 | } | |
| 139 | ], | |
| 140 | "metadata": { | |
| 141 | "kernelspec": { | |
| 142 | "display_name": "Python 3 (ipykernel)", | |
| 143 | "language": "python", | |
| 144 | "name": "python3" | |
| 145 | }, | |
| 146 | "language_info": { | |
| 147 | "codemirror_mode": { | |
| 148 | "name": "ipython", | |
| 149 | "version": 3 | |
| 150 | }, | |
| 151 | "file_extension": ".py", | |
| 152 | "mimetype": "text/x-python", | |
| 153 | "name": "python", | |
| 154 | "nbconvert_exporter": "python", | |
| 155 | "pygments_lexer": "ipython3", | |
| 156 | "version": "3.7.5" | |
| 157 | } | |
| 158 | }, | |
| 159 | "nbformat": 4, | |
| 160 | "nbformat_minor": 5 | |
| 161 | } |
| 2 | 2 | tf.reset_default_graph() |
| 3 | 3 | import numpy as np |
| 4 | 4 | from PIL import Image |
| 5 | import model | |
| 6 | import util | |
| 5 | import src.model | |
| 6 | import src.util | |
| 7 | 7 | import os |
| 8 | 8 | import sys |
| 9 | 9 | |
| 53 | 53 | 参数请放到params字典中,我们会自动解析该变量。 |
| 54 | 54 | """ |
| 55 | 55 | base64_str = conf['Photo'] |
| 56 | image = load_demo_image(base64_str, image_size, device) | |
| 56 | image = load_demo_image(base64_str) | |
| 57 | 57 | res = inference(model_PATH, image) |
| 58 | 58 | image_str = image_to_path(res) |
| 59 | return {'Output': res} | |
| 59 | return {'Output': image_str} | |
| 60 | 60 | ⏎ |
| 0 | { | |
| 1 | "cells": [ | |
| 2 | { | |
| 3 | "cell_type": "code", | |
| 4 | "execution_count": 3, | |
| 5 | "id": "a0f197c0", | |
| 6 | "metadata": {}, | |
| 7 | "outputs": [ | |
| 8 | { | |
| 9 | "name": "stderr", | |
| 10 | "output_type": "stream", | |
| 11 | "text": [ | |
| 12 | "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", | |
| 13 | "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" | |
| 14 | ] | |
| 15 | }, | |
| 16 | { | |
| 17 | "name": "stdout", | |
| 18 | "output_type": "stream", | |
| 19 | "text": [ | |
| 20 | "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", | |
| 21 | "Instructions for updating:\n", | |
| 22 | "non-resource variables are not supported in the long term\n", | |
| 23 | "Imported model (for Places365, 128x128 images)\n" | |
| 24 | ] | |
| 25 | } | |
| 26 | ], | |
| 27 | "source": [ | |
| 28 | "import tensorflow.compat.v1 as tf\n", | |
| 29 | "tf.disable_v2_behavior()\n", | |
| 30 | "tf.reset_default_graph()\n", | |
| 31 | "import numpy as np\n", | |
| 32 | "from PIL import Image\n", | |
| 33 | "import model\n", | |
| 34 | "import util\n", | |
| 35 | "import os\n", | |
| 36 | "import sys" | |
| 37 | ] | |
| 38 | }, | |
| 39 | { | |
| 40 | "cell_type": "code", | |
| 41 | "execution_count": 4, | |
| 42 | "id": "35638e2a", | |
| 43 | "metadata": {}, | |
| 44 | "outputs": [], | |
| 45 | "source": [ | |
| 46 | "model_PATH='/home/jovyan/work/src/output/models/model2000.ckpt'" | |
| 47 | ] | |
| 48 | }, | |
| 49 | { | |
| 50 | "cell_type": "code", | |
| 51 | "execution_count": 9, | |
| 52 | "id": "bfa0229a", | |
| 53 | "metadata": {}, | |
| 54 | "outputs": [], | |
| 55 | "source": [ | |
| 56 | "import time\n", | |
| 57 | "def run_time(func):\n", | |
| 58 | " def inner(model, image, question):\n", | |
| 59 | " back = func(model, image, question)\n", | |
| 60 | " print(\"Runned time: {} s\".format(round((time.time() - t)/10, 3)))\n", | |
| 61 | " return back\n", | |
| 62 | " t = time.time()\n", | |
| 63 | " return inner" | |
| 64 | ] | |
| 65 | }, | |
| 66 | { | |
| 67 | "cell_type": "code", | |
| 68 | "execution_count": 8, | |
| 69 | "id": "57494ceb", | |
| 70 | "metadata": {}, | |
| 71 | "outputs": [], | |
| 72 | "source": [ | |
| 73 | "def load_demo_image(in_PATH):\n", | |
| 74 | " img = np.array(Image.open(in_PATH).convert('RGB'))[np.newaxis] / 255.0\n", | |
| 75 | " img_p = util.preprocess_images_outpainting(img)\n", | |
| 76 | " return img_p" | |
| 77 | ] | |
| 78 | }, | |
| 79 | { | |
| 80 | "cell_type": "code", | |
| 81 | "execution_count": 11, | |
| 82 | "id": "d1ab22e6", | |
| 83 | "metadata": {}, | |
| 84 | "outputs": [], | |
| 85 | "source": [ | |
| 86 | "def image_to_path(img):\n", | |
| 87 | " resize_img = img\n", | |
| 88 | " path = uuid.uuid4().hex + '.png'\n", | |
| 89 | " resize_img.save(path)\n", | |
| 90 | " return path" | |
| 91 | ] | |
| 92 | }, | |
| 93 | { | |
| 94 | "cell_type": "code", | |
| 95 | "execution_count": 6, | |
| 96 | "id": "4a7370fa", | |
| 97 | "metadata": {}, | |
| 98 | "outputs": [], | |
| 99 | "source": [ | |
| 100 | "@run_time\n", | |
| 101 | "def inference(model_PATH, img_p):\n", | |
| 102 | " G_Z = tf.placeholder(tf.float32, shape=[None, IMAGE_SZ, IMAGE_SZ, 4], name='G_Z')\n", | |
| 103 | " G_sample = model.generator(G_Z)\n", | |
| 104 | " \n", | |
| 105 | " saver = tf.train.Saver()\n", | |
| 106 | " with tf.Session() as sess:\n", | |
| 107 | " saver.restore(sess, model_PATH)\n", | |
| 108 | " output, = sess.run([G_sample], feed_dict={G_Z: img_p})\n", | |
| 109 | " img_norm = (output[0] * 255.0).astype(np.uint8)\n", | |
| 110 | " img = Image.fromarray(img_norm, 'RGB')\n", | |
| 111 | " #util.save_image(output[0], out_PATH)\n", | |
| 112 | " return img" | |
| 113 | ] | |
| 114 | }, | |
| 115 | { | |
| 116 | "cell_type": "code", | |
| 117 | "execution_count": 12, | |
| 118 | "id": "b03bba02", | |
| 119 | "metadata": {}, | |
| 120 | "outputs": [], | |
| 121 | "source": [ | |
| 122 | "def handle(conf):\n", | |
| 123 | " \"\"\"\n", | |
| 124 | " 该方法是部署之后,其他人调用你的服务时候的处理方法。\n", | |
| 125 | " 请按规范填写参数结构,这样我们就能替你自动生成配置文件,方便其他人的调用。\n", | |
| 126 | " 范例:\n", | |
| 127 | " params['key'] = value # value_type: str # description: some description\n", | |
| 128 | " value_type 可以选择:img, video, audio, str, int, float, [int], [str], [float]\n", | |
| 129 | " 参数请放到params字典中,我们会自动解析该变量。\n", | |
| 130 | " \"\"\"\n", | |
| 131 | " base64_str = conf['Photo']\n", | |
| 132 | " image = load_demo_image(base64_str, image_size, device)\n", | |
| 133 | " res = inference(model_PATH, image)\n", | |
| 134 | " image_str = image_to_path(res)\n", | |
| 135 | " return {'Output': res}\n", | |
| 136 | " " | |
| 137 | ] | |
| 138 | } | |
| 139 | ], | |
| 140 | "metadata": { | |
| 141 | "kernelspec": { | |
| 142 | "display_name": "Python 3 (ipykernel)", | |
| 143 | "language": "python", | |
| 144 | "name": "python3" | |
| 145 | }, | |
| 146 | "language_info": { | |
| 147 | "codemirror_mode": { | |
| 148 | "name": "ipython", | |
| 149 | "version": 3 | |
| 150 | }, | |
| 151 | "file_extension": ".py", | |
| 152 | "mimetype": "text/x-python", | |
| 153 | "name": "python", | |
| 154 | "nbconvert_exporter": "python", | |
| 155 | "pygments_lexer": "ipython3", | |
| 156 | "version": "3.7.5" | |
| 157 | } | |
| 158 | }, | |
| 159 | "nbformat": 4, | |
| 160 | "nbformat_minor": 5 | |
| 161 | } |
| 0 | { | |
| 1 | "cells": [ | |
| 2 | { | |
| 3 | "cell_type": "code", | |
| 4 | "execution_count": 3, | |
| 5 | "id": "29291686", | |
| 6 | "metadata": {}, | |
| 7 | "outputs": [ | |
| 8 | { | |
| 9 | "name": "stderr", | |
| 10 | "output_type": "stream", | |
| 11 | "text": [ | |
| 12 | "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", | |
| 13 | "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" | |
| 14 | ] | |
| 15 | }, | |
| 16 | { | |
| 17 | "name": "stdout", | |
| 18 | "output_type": "stream", | |
| 19 | "text": [ | |
| 20 | "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", | |
| 21 | "Instructions for updating:\n", | |
| 22 | "non-resource variables are not supported in the long term\n", | |
| 23 | "Imported model (for Places365, 128x128 images)\n" | |
| 24 | ] | |
| 25 | } | |
| 26 | ], | |
| 27 | "source": [ | |
| 28 | "import tensorflow.compat.v1 as tf\n", | |
| 29 | "tf.disable_v2_behavior()\n", | |
| 30 | "tf.reset_default_graph()\n", | |
| 31 | "import numpy as np\n", | |
| 32 | "from PIL import Image\n", | |
| 33 | "import model\n", | |
| 34 | "import util\n", | |
| 35 | "import os\n", | |
| 36 | "import sys" | |
| 37 | ] | |
| 38 | }, | |
| 39 | { | |
| 40 | "cell_type": "code", | |
| 41 | "execution_count": 4, | |
| 42 | "id": "67b9aae4", | |
| 43 | "metadata": {}, | |
| 44 | "outputs": [], | |
| 45 | "source": [ | |
| 46 | "model_PATH='/home/jovyan/work/src/output/models/model2000.ckpt'" | |
| 47 | ] | |
| 48 | }, | |
| 49 | { | |
| 50 | "cell_type": "code", | |
| 51 | "execution_count": 9, | |
| 52 | "id": "058a509a", | |
| 53 | "metadata": {}, | |
| 54 | "outputs": [], | |
| 55 | "source": [ | |
| 56 | "import time\n", | |
| 57 | "def run_time(func):\n", | |
| 58 | " def inner(model, image, question):\n", | |
| 59 | " back = func(model, image, question)\n", | |
| 60 | " print(\"Runned time: {} s\".format(round((time.time() - t)/10, 3)))\n", | |
| 61 | " return back\n", | |
| 62 | " t = time.time()\n", | |
| 63 | " return inner" | |
| 64 | ] | |
| 65 | }, | |
| 66 | { | |
| 67 | "cell_type": "code", | |
| 68 | "execution_count": 8, | |
| 69 | "id": "7dd82ead", | |
| 70 | "metadata": {}, | |
| 71 | "outputs": [], | |
| 72 | "source": [ | |
| 73 | "def load_demo_image(in_PATH):\n", | |
| 74 | " img = np.array(Image.open(in_PATH).convert('RGB'))[np.newaxis] / 255.0\n", | |
| 75 | " img_p = util.preprocess_images_outpainting(img)\n", | |
| 76 | " return img_p" | |
| 77 | ] | |
| 78 | }, | |
| 79 | { | |
| 80 | "cell_type": "code", | |
| 81 | "execution_count": 11, | |
| 82 | "id": "379d2efd", | |
| 83 | "metadata": {}, | |
| 84 | "outputs": [], | |
| 85 | "source": [ | |
| 86 | "def image_to_path(img):\n", | |
| 87 | " resize_img = img\n", | |
| 88 | " path = uuid.uuid4().hex + '.png'\n", | |
| 89 | " resize_img.save(path)\n", | |
| 90 | " return path" | |
| 91 | ] | |
| 92 | }, | |
| 93 | { | |
| 94 | "cell_type": "code", | |
| 95 | "execution_count": 6, | |
| 96 | "id": "70d5031c", | |
| 97 | "metadata": {}, | |
| 98 | "outputs": [], | |
| 99 | "source": [ | |
| 100 | "@run_time\n", | |
| 101 | "def inference(model_PATH, img_p):\n", | |
| 102 | " G_Z = tf.placeholder(tf.float32, shape=[None, IMAGE_SZ, IMAGE_SZ, 4], name='G_Z')\n", | |
| 103 | " G_sample = model.generator(G_Z)\n", | |
| 104 | " \n", | |
| 105 | " saver = tf.train.Saver()\n", | |
| 106 | " with tf.Session() as sess:\n", | |
| 107 | " saver.restore(sess, model_PATH)\n", | |
| 108 | " output, = sess.run([G_sample], feed_dict={G_Z: img_p})\n", | |
| 109 | " img_norm = (output[0] * 255.0).astype(np.uint8)\n", | |
| 110 | " img = Image.fromarray(img_norm, 'RGB')\n", | |
| 111 | " #util.save_image(output[0], out_PATH)\n", | |
| 112 | " return img" | |
| 113 | ] | |
| 114 | }, | |
| 115 | { | |
| 116 | "cell_type": "code", | |
| 117 | "execution_count": 12, | |
| 118 | "id": "d7439afa", | |
| 119 | "metadata": {}, | |
| 120 | "outputs": [], | |
| 121 | "source": [ | |
| 122 | "def handle(conf):\n", | |
| 123 | " \"\"\"\n", | |
| 124 | " 该方法是部署之后,其他人调用你的服务时候的处理方法。\n", | |
| 125 | " 请按规范填写参数结构,这样我们就能替你自动生成配置文件,方便其他人的调用。\n", | |
| 126 | " 范例:\n", | |
| 127 | " params['key'] = value # value_type: str # description: some description\n", | |
| 128 | " value_type 可以选择:img, video, audio, str, int, float, [int], [str], [float]\n", | |
| 129 | " 参数请放到params字典中,我们会自动解析该变量。\n", | |
| 130 | " \"\"\"\n", | |
| 131 | " base64_str = conf['Photo']\n", | |
| 132 | " image = load_demo_image(base64_str, image_size, device)\n", | |
| 133 | " res = inference(model_PATH, image)\n", | |
| 134 | " image_str = image_to_path(res)\n", | |
| 135 | " return {'Output': res}\n", | |
| 136 | " " | |
| 137 | ] | |
| 138 | } | |
| 139 | ], | |
| 140 | "metadata": { | |
| 141 | "kernelspec": { | |
| 142 | "display_name": "Python 3 (ipykernel)", | |
| 143 | "language": "python", | |
| 144 | "name": "python3" | |
| 145 | }, | |
| 146 | "language_info": { | |
| 147 | "codemirror_mode": { | |
| 148 | "name": "ipython", | |
| 149 | "version": 3 | |
| 150 | }, | |
| 151 | "file_extension": ".py", | |
| 152 | "mimetype": "text/x-python", | |
| 153 | "name": "python", | |
| 154 | "nbconvert_exporter": "python", | |
| 155 | "pygments_lexer": "ipython3", | |
| 156 | "version": "3.7.5" | |
| 157 | } | |
| 158 | }, | |
| 159 | "nbformat": 4, | |
| 160 | "nbformat_minor": 5 | |
| 161 | } |