tensorflow / hub

A library for transfer learning by reusing parts of TensorFlow models.
https://tensorflow.org/hub
Apache License 2.0
3.49k stars 1.67k forks source link

Error while using hub.KerasLayer #782

Closed RaresBuzatu97 closed 3 years ago

RaresBuzatu97 commented 3 years ago

Hello, This is my code:

import tensorflow_hub as hub
import tensorflow.compat.v1 as tf
import cv2
import numpy as np

if __name__ == '__main__':
  img_name = 'vlcsnap-2021-07-02-12h51m27s390.png'
  content_image_path = '/home/work/Pictures/vlcsnap-2021-07-02-13h26m43s659.png'
  style_image_path = '/home/work/Downloads/The_Great_Wave_off_Kanagawa.jpg'
  content_image = cv2.imread(content_image_path)
  style_image = cv2.imread(style_image_path)
  style_image = cv2.resize(style_image, (256, 256))
  content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.
  style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.

  hub_handle = 'https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2'
  hub_layer = hub.KerasLayer(hub_handle, trainable=True)
  tf_input1 = tf.keras.layers.Input((None, None, 3), name = 'placeholder')
  tf_input2 = tf.keras.layers.Input((256, 256, 3), name = 'placeholder_1')
  tf_x = hub_layer([tf_input1, tf_input2])
  `

When I run the code I get the following error:

TypeError: Expected argument names ['placeholder', 'placeholder_1'] but got values for ['placeholder']. Missing: ['placeholder_1'].

Any idea why this is happening?

arghyaganguly commented 3 years ago

@RaresBuzatu97 , this doesn't seem to be a bug or a feature request within scope of tensorflow_ hub.Please post this kind of issues in stackoverflow.

Also, please try with the below snippet and revert with the output/error :-

 hub_handle = 'https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2'
 hub_layer = hub.KerasLayer(hub_handle, trainable=True)
 tf_input1 = tf.keras.layers.Input((None, None, 3), name = 'placeholder')
 tf_input2 = tf.keras.layers.Input((256, 256, 3), name = 'placeholder_1')
 tf_x = hub_layer(tf_input1,tf_input2)

Reproducible colab gist.

RaresBuzatu97 commented 3 years ago

Hello! Thank you for your help. However, after modifying the last line I still get the same error: image

WGierke commented 3 years ago

Hi @RaresBuzatu97, please try:

layer = hub.KerasLayer(hub_handle, arguments={"placeholder_1": tf.constant(style_image)})
outputs = layer(content_image)
WGierke commented 3 years ago

The approach above solved the issue for me. Please re-open the ticket if the problem persists.