master
/ Untitled.ipynb

Untitled.ipynb @4cd1a00view markup · raw · history · blame

Notebook
In [2]:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
tf.reset_default_graph()
import numpy as np
from PIL import Image
import src.model
import src.util
import os
import sys
2022-07-21 21:07:47.961364: 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
2022-07-21 21:07:47.961398: 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.
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.
Instructions for updating:
non-resource variables are not supported in the long term
Imported model (for Places365, 128x128 images)
In [3]:
model_PATH='./src/output/models/model227000.ckpt'
out_PATH='./results/test_output.png'
IMAGE_SZ = 128
In [4]:
import time
def run_time(func):
    def inner(model_PATH, img_p):
        back = func(model_PATH, img_p)
        print("Runned time: {} s".format(round((time.time() - t)/10, 3)))
        return back
    t = time.time()
    return inner
In [5]:
def load_demo_image(in_PATH):
    img = np.array(Image.open(in_PATH).resize((128, 128), Image.ANTIALIAS).convert('RGB'))[np.newaxis] / 255.0
    img_p = src.util.preprocess_images_outpainting(img)
    return img_p
In [6]:
def image_to_path(img):
    resize_img = img
    path = out_PATH
    resize_img.save(path)
    return path
In [7]:
@run_time
def inference(model_PATH, img_p):
    G_Z = tf.placeholder(tf.float32, shape=[None, IMAGE_SZ, IMAGE_SZ, 4], name='G_Z')
    G_sample = src.model.generator(G_Z)
    
    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, model_PATH)
        output, = sess.run([G_sample], feed_dict={G_Z: img_p})
        img_norm = (output[0] * 255.0).astype(np.uint8)
        img = Image.fromarray(img_norm, 'RGB')
        #util.save_image(output[0], out_PATH)
    return img
In [8]:
def handle(conf):
    """
    该方法是部署之后,其他人调用你的服务时候的处理方法。
    请按规范填写参数结构,这样我们就能替你自动生成配置文件,方便其他人的调用。
    范例:
    params['key'] = value # value_type: str # description: some description
    value_type 可以选择:img, video, audio, str, int, float, [int], [str], [float]
    参数请放到params字典中,我们会自动解析该变量。
    """
    base64_str = conf['Photo']
    image = load_demo_image(base64_str)
    res = inference(model_PATH, image)
    image_str = image_to_path(res)
    return {'Output': image_str}
    
In [9]:
handle({'Photo': '/home/jovyan/work/images/test.png'})
WARNING:tensorflow:From /home/jovyan/work/src/model.py:20: conv2d (from tensorflow.python.keras.legacy_tf_layers.convolutional) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.keras.layers.Conv2D` instead.
WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/legacy_tf_layers/convolutional.py:424: Layer.apply (from tensorflow.python.keras.engine.base_layer_v1) is deprecated and will be removed in a future version.
Instructions for updating:
Please use `layer.__call__` method instead.
WARNING:tensorflow:From /home/jovyan/work/src/model.py:79: conv2d_transpose (from tensorflow.python.keras.legacy_tf_layers.convolutional) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.keras.layers.Conv2DTranspose` instead.
INFO:tensorflow:Restoring parameters from ./src/output/models/model227000.ckpt
2022-07-21 21:07:52.404094: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2022-07-21 21:07:52.404122: W tensorflow/stream_executor/cuda/cuda_driver.cc:312] failed call to cuInit: UNKNOWN ERROR (303)
2022-07-21 21:07:52.404144: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (notebook): /proc/driver/nvidia/version does not exist
2022-07-21 21:07:52.404443: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-07-21 21:07:52.426464: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 2500000000 Hz
2022-07-21 21:07:52.440496: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x441b180 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2022-07-21 21:07:52.440522: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
Runned time: 0.204 s
Out[9]:
{'Output': './results/test_output.png'}