【发布时间】:2021-10-08 17:21:46
【问题描述】:
我正在尝试在 Keras 中加载本地图像,但它无法正常工作,尝试显示时出现错误:
/usr/local/lib/python3.7/dist-packages/IPython/core/formatters.py:364: FormatterWarning: image/png formatter returned invalid type <class 'PIL.JpegImagePlugin.JpegImageFile'> (expected (<class 'bytes'>, <class 'str'>)) for object: <IPython.core.display.Image object>
FormatterWarning
<IPython.core.display.Image object>
我正在使用 Keras 的创始人创建的这个笔记本(它自己工作,但它从互联网下载图像): https://colab.research.google.com/drive/1H-lNMxVF9NPME6oZVphFRoi7yglJMZxP
我想尝试使用自己的图像来玩转风格,所以我改变了这个:
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.applications import vgg19
# base_image_path = keras.utils.get_file('my_pic.jpg', './my_pic.png')
base_image_path = keras.utils.load_img('/content/my_pic.jpg')
style_reference_image_path = keras.utils.get_file('starry_night.jpg', 'https://i.imgur.com/9ooB60I.jpg')
result_prefix = 'paris_generated'
iterations = 5000
# Weights of the different loss components
total_variation_weight = 1e-6
style_weight = 2e-6
content_weight = 2e-8
# Dimensions of the generated picture.
width, height = keras.preprocessing.image.load_img(base_image_path).size
img_nrows = 400
img_ncols = int(width * img_nrows / height)
当我做出这个改变时:
# base_image_path = keras.utils.get_file('my_pic.jpg', './my_pic.png')
base_image_path = keras.utils.load_img('/content/my_pic.jpg')
所有代码都停止工作。我认为 get_file 方法正在做一些事情以使其作为图片可访问,因为在原始笔记本中它显示图片但对我来说却没有。
有什么我可以做的吗?我是处理图像的新手,但我确实对此进行了研究,我认为 get_file 方法用于通过网络获取文件,所以我认为如果我在本地 colab 系统上复制文件,它将无法正常工作。
【问题讨论】: