Google I/O에 참여해 주셔서 감사합니다. 주문형 모든 세션 보기 주문형 시청

임의 스타일의 빠른 스타일 전달

TensorFlow.org에서 보기 Google Colab에서 실행 GitHub에서소스 보기 노트북 다운로드 TF Hub 모델보기

본 내용은 magenta의 모델 코드와 다음 발행물에 기초합니다.

Exploring the structure of a real-time, arbitrary neural artistic stylization network(실시간 임의 신경 예술적 스타일화 네트워크의 구조 탐색). Golnaz Ghiasi, Honglak Lee, Manjunath Kudlur, Vincent Dumoulin, Jonathon Shlens, Proceedings of the British Machine Vision Conference (BMVC), 2017.


TF2 및 관련된 모든 종속성을 가져오는 것으로 시작합니다.

import functools
import os

from matplotlib import gridspec
import matplotlib.pylab as plt
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub

print("TF Version: ", tf.__version__)
print("TF Hub version: ", hub.__version__)
print("Eager mode enabled: ", tf.executing_eagerly())
print("GPU available: ", tf.config.list_physical_devices('GPU'))
2022-12-14 22:17:09.394415: W tensorflow/compiler/xla/stream_executor/platform/default/] Could not load dynamic library ''; dlerror: cannot open shared object file: No such file or directory
2022-12-14 22:17:09.394519: W tensorflow/compiler/xla/stream_executor/platform/default/] Could not load dynamic library ''; dlerror: cannot open shared object file: No such file or directory
2022-12-14 22:17:09.394529: W tensorflow/compiler/tf2tensorrt/utils/] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
TF Version:  2.11.0
TF Hub version:  0.12.0
Eager mode enabled:  True
GPU available:  [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:1', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:2', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:3', device_type='GPU')]
# @title Define image loading and visualization functions  { display-mode: "form" }

def crop_center(image):
  """Returns a cropped square image."""
  shape = image.shape
  new_shape = min(shape[1], shape[2])
  offset_y = max(shape[1] - shape[2], 0) // 2
  offset_x = max(shape[2] - shape[1], 0) // 2
  image = tf.image.crop_to_bounding_box(
      image, offset_y, offset_x, new_shape, new_shape)
  return image

def load_image(image_url, image_size=(256, 256), preserve_aspect_ratio=True):
  """Loads and preprocesses images."""
  # Cache image file locally.
  image_path = tf.keras.utils.get_file(os.path.basename(image_url)[-128:], image_url)
  # Load and convert to float32 numpy array, add batch dimension, and normalize to range [0, 1].
  img =,
      channels=3, dtype=tf.float32)[tf.newaxis, ...]
  img = crop_center(img)
  img = tf.image.resize(img, image_size, preserve_aspect_ratio=True)
  return img

def show_n(images, titles=('',)):
  n = len(images)
  image_sizes = [image.shape[1] for image in images]
  w = (image_sizes[0] * 6) // 320
  plt.figure(figsize=(w * n, w))
  gs = gridspec.GridSpec(1, n, width_ratios=image_sizes)
  for i in range(n):
    plt.imshow(images[i][0], aspect='equal')
    plt.title(titles[i] if len(titles) > i else '')

작업에 이용할 수 있는 몇 가지 이미지도 함께 가져옵니다.

# @title Load example images  { display-mode: "form" }

content_image_url = ''  # @param {type:"string"}
style_image_url = ''  # @param {type:"string"}
output_image_size = 384  # @param {type:"integer"}

# The content image size can be arbitrary.
content_img_size = (output_image_size, output_image_size)
# The style prediction model was trained with image size 256 and it's the 
# recommended image size for the style image (though, other sizes work as 
# well but will lead to different results).
style_img_size = (256, 256)  # Recommended to keep it at 256.

content_image = load_image(content_image_url, content_img_size)
style_image = load_image(style_image_url, style_img_size)
style_image = tf.nn.avg_pool(style_image, ksize=[3,3], strides=[1,1], padding='SAME')
show_n([content_image, style_image], ['Content image', 'Style image'])
Downloading data from
71406/71406 [==============================] - 0s 1us/step
Downloading data from
2684586/2684586 [==============================] - 0s 0us/step


TF Hub 모듈 가져오기

# Load TF Hub module.

hub_handle = ''
hub_module = hub.load(hub_handle)

이미지 스타일화를 위한 이 허브 모듈의 서명은 다음과 같습니다.

outputs = hub_module(content_image, style_image) stylized_image = outputs[0]

content_image, style_image, stylized_image는 형상이 [batch_size, image_height, image_width, 3]인 4-D 텐서로 예상됩니다.

현재 예제에서는 단일 이미지만 제공하므로 배치 차원은 1이지만 같은 모듈을 사용하여 더 많은 이미지를 동시에 처리할 수 있습니다.

이미지의 입력 및 출력 값은 [0, 1] 범위에 있어야 합니다.

콘텐츠 형상과 스타일 이미지가 일치할 필요는 없습니다. 출력 이미지 형상은 콘텐츠 이미지 형상과 같습니다.

이미지 스타일화 시연하기

# Stylize content image with given style image.
# This is pretty fast within a few milliseconds on a GPU.

outputs = hub_module(tf.constant(content_image), tf.constant(style_image))
stylized_image = outputs[0]
# Visualize input images and the generated stylized image.

show_n([content_image, style_image, stylized_image], titles=['Original content image', 'Style image', 'Stylized image'])


더 많은 이미지에 시도하기

# @title To Run: Load more images { display-mode: "form" }

content_urls = dict(
style_urls = dict(

content_image_size = 384
style_image_size = 256
content_images = {k: load_image(v, (content_image_size, content_image_size)) for k, v in content_urls.items()}
style_images = {k: load_image(v, (style_image_size, style_image_size)) for k, v in style_urls.items()}
style_images = {k: tf.nn.avg_pool(style_image, ksize=[3,3], strides=[1,1], padding='SAME') for k, style_image in style_images.items()}
Downloading data from
3170828/3170828 [==============================] - 0s 0us/step
Downloading data from
406531/406531 [==============================] - 0s 0us/step
Downloading data from
61306/61306 [==============================] - 0s 0us/step
Downloading data from
195196/195196 [==============================] - 0s 0us/step
Downloading data from
46930988/46930988 [==============================] - 2s 0us/step
Downloading data from
396423/396423 [==============================] - 0s 0us/step
Downloading data from
144340/144340 [==============================] - 0s 1us/step
Downloading data from
11403121/11403121 [==============================] - 0s 0us/step
Downloading data from
2905099/2905099 [==============================] - 0s 0us/step
Downloading data from
1234199/1234199 [==============================] - 0s 0us/step
Downloading data from
120288/120288 [==============================] - 0s 1us/step
Downloading data from
131604/131604 [==============================] - 0s 1us/step
Downloading data from
32390/32390 [==============================] - 0s 1us/step
Downloading data from
1914618/1914618 [==============================] - 0s 0us/step
Downloading data from
40620/40620 [==============================] - 0s 1us/step
Downloading data from
66306/66306 [==============================] - 0s 1us/step

Specify the main content image and the style you want to use.