0

Follwing the tutorial at https://www.tensorflow.org/tutorials/images/hub_with_keras resulted in a file model.h5. Converting to tensorflow-js with the command

tensorflowjs_converter --input_format keras ./model.h5 /tmp/jsmodel/

failed with

Exception: Error dumping weights, duplicate weight name Variable

Why is this and how can it be fixed?

MCVE

from __future__ import absolute_import, division, print_function
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras import layers
import numpy as np

data_root = tf.keras.utils.get_file(
'flower_photos','https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
   untar=True)

image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1/255)
IMAGE_SHAPE = (224, 224)
image_data = image_generator.flow_from_directory(str(data_root), target_size=IMAGE_SHAPE)

feature_extractor_url = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/2" #@param {type:"string"}
feature_extractor_layer = hub.KerasLayer(feature_extractor_url,
                                         input_shape=(224,224,3))

for image_batch, label_batch in image_data:
  print("Image batch shape: ", image_batch.shape)
  print("Labe batch shape: ", label_batch.shape)
  break

feature_extractor_layer.trainable = False

model = tf.keras.Sequential([
  feature_extractor_layer,
  layers.Dense(image_data.num_classes, activation='softmax')
])

model.compile(
  optimizer=tf.keras.optimizers.Adam(), 
  loss='categorical_crossentropy',
  metrics=['acc'])

steps_per_epoch = np.ceil(image_data.samples/image_data.batch_size)

history = model.fit(image_data, epochs=2, 
                    steps_per_epoch=steps_per_epoch)  # removed callback

model.save("/tmp/so_model.h5")

This fails with a

RuntimeError: Unable to create link (name already exists)

but the model is created. Calling the above tensorflowjs_converter --input_format keras /tmp/model.h5 /tmp/jsmodel fails with the above

Exception: Error dumping weights, duplicate weight name Variable

UPDATE: see also Retrain image detection with MobileNet

serv-inc
  • 35,772
  • 9
  • 166
  • 188
  • 1
    I think that the error says it all. There are some operations that have the same name. If you could share your model, it can give some inisight – edkeveked Apr 03 '19 at 10:28
  • @edkeveked: good feedback. See the MCVE. It yields the same error. – serv-inc Apr 03 '19 at 10:55
  • 1
    It seems that there is a related bug : https://github.com/tensorflow/tensorflow/issues/26811 . You can fill an issue on github for tfjs-converter – edkeveked Apr 03 '19 at 14:55
  • 1
    @edkeveked: issues at https://github.com/tensorflow/tensorflow/issues/27539 and https://github.com/tensorflow/tensorflow/issues/27538 – serv-inc Apr 07 '19 at 18:30
  • @edkeveked: previous issues are fixed and state is back at the above: saving shows error, but creates file, converting fails again – serv-inc Apr 20 '19 at 17:40

0 Answers0