Spaces:
Sleeping
Sleeping
| import os | |
| import tensorflow as tf | |
| # # Load compressed models from tensorflow_hub | |
| # os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED' | |
| import IPython.display as display | |
| import matplotlib.pyplot as plt | |
| import matplotlib as mpl | |
| mpl.rcParams['figure.figsize'] = (12, 12) | |
| mpl.rcParams['axes.grid'] = False | |
| import numpy as np | |
| import PIL.Image | |
| import time | |
| import functools | |
| def load_img(path_to_img): | |
| max_dim = 512 | |
| img = tf.io.read_file(path_to_img) | |
| img = tf.image.decode_image(img, channels=3) | |
| img = tf.image.convert_image_dtype(img, tf.float32) | |
| shape = tf.cast(tf.shape(img)[:-1], tf.float32) | |
| long_dim = max(shape) | |
| scale = max_dim / long_dim | |
| new_shape = tf.cast(shape * scale, tf.int32) | |
| img = tf.image.resize(img, new_shape) | |
| img = img[tf.newaxis, :] | |
| return img | |
| def clip_0_1(image): | |
| """keep pixel values of an image between 0 and 1""" | |
| return tf.clip_by_value(image, clip_value_min=0.0, clip_value_max=1.0) | |
| def tensor_to_image(tensor): | |
| tensor = tensor*255 | |
| tensor = np.array(tensor, dtype=np.uint8) | |
| if np.ndim(tensor)>3: | |
| assert tensor.shape[0] == 1 | |
| tensor = tensor[0] | |
| return PIL.Image.fromarray(tensor) |