0

I'm trying to download a style transfer model, and I want to get style bottlenecks from a bunch of images. Here's the code, which I sourced from this site.

style_predict_path = tf.keras.utils.get_file('style_predict.tflite', 'https://tfhub.dev/google/lite-model/magenta/arbitrary-image-stylization-v1-256/int8/prediction/1?lite-format=tflite')

# Function to run style prediction on preprocessed style image.
def run_style_predict(preprocessed_style_image):
  # Load the model.
  interpreter = tf.lite.Interpreter(model_path=style_predict_path)

  # Set model input.
  interpreter.allocate_tensors()
  input_details = interpreter.get_input_details()
  interpreter.set_tensor(input_details[0]["index"], preprocessed_style_image)

  # Calculate style bottleneck.
  interpreter.invoke()
  style_bottleneck = interpreter.tensor(
      interpreter.get_output_details()[0]["index"]
      )()

  return style_bottleneck

# Calculate style bottleneck for the preprocessed style image.
style_bottleneck = run_style_predict(preprocessed_style_image)
print('Style Bottleneck Shape:', style_bottleneck.shape)

However, when I try to run the code, I get the following error:

Traceback (most recent call last):
  File "C:\Users\(Name)\Documents\Python\image_style_transfer.py", line 119, in <module>
    style_bottleneck = run_style_predict(style_image)
  File "C:\Users\(Name)\Documents\Python\image_style_transfer.py", line 60, in run_style_predict
    interpreter = tf.lite.Interpreter(model_path=style_predict_path)
  File "C:\Users\(Name)\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\lite\python\interpreter.py", line 456, in __init__
    _interpreter_wrapper.CreateWrapperFromFile(
ValueError: Could not open 'C:\Users\(Name)\.keras\datasets\style_predict.tflite'.

EDIT: This might or might not help, but the MD5 hash of the style_predict.tflite file I get is 01fdaf469369c39e8662fae8662321b6. I used the code here to get the hash.

Xhatahx
  • 5
  • 3
  • Is the file actually downloaded to the location or is the interpreter just unable to open it? Can the interpreter open it when you download it manually to e.g. your Downloads directory? What happens if you change the default cache location by setting `cache_dir=...` when calling [tf.keras.utils.get_file](https://www.tensorflow.org/api_docs/python/tf/keras/utils/get_file)? – WGierke Feb 22 '22 at 15:12
  • @WGierke Downloading it manually results in the same error. I'm not quite sure what you meant with the cache location, but adding `cache_dir=...` resulted in `TypeError: expected str, bytes or os.PathLike object, not ellipsis` and adding `cache_dir='C:/Users/Grétar/Downloads'` just results in the same ValueError as before, just with a different file path. – Xhatahx Feb 22 '22 at 18:07

0 Answers0