如何修复jupyter笔记本中的错误:图像数据无法转换为浮点



我需要别人的帮助。

我还尝试了以下代码,但出现了一个错误(大约是pix2pix,结果在这一部分没有显示任何照片(:

import tensorflow as tf
import os
import time
import numpy as np
from matplotlib import pyplot as plt
from IPython import display
PATH = os.path.join('datasets/','facades/', 'train/')
BUFFER_SIZE = 400
BATCH_SIZE = 1
IMG_WIDTH = 256
IMG_HEIGHT = 256
inp, re = load(PATH+'train/100.jpg')
plt.figure()
plt.imshow(inp/255.0)
plt.figure()
plt.imshow(re/255.0)

这是错误

TypeError                                 Traceback (most recent call last)
<ipython-input-16-0ddde1d49d08> in <module>
2 
3 plt.figure()
----> 4 plt.imshow(inp/255.0)
5 plt.figure()
6 plt.imshow(re/255.0)
C:ProgramDataAnaconda3envsgenerativelibsite-packagesmatplotlibpyplot.py in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, data, **kwargs)
2699         filternorm=filternorm, filterrad=filterrad, imlim=imlim,
2700         resample=resample, url=url, **({"data": data} if data is not
-> 2701         None else {}), **kwargs)
2702     sci(__ret)
2703     return __ret
C:ProgramDataAnaconda3envsgenerativelibsite-packagesmatplotlib__init__.py in inner(ax, data, *args, **kwargs)
1808                         "the Matplotlib list!)" % (label_namer, func.__name__),
1809                         RuntimeWarning, stacklevel=2)
-> 1810             return func(ax, *args, **kwargs)
1811 
1812         inner.__doc__ = _add_data_doc(inner.__doc__,
C:ProgramDataAnaconda3envsgenerativelibsite-packagesmatplotlibaxes_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
5492                               resample=resample, **kwargs)
5493 
-> 5494         im.set_data(X)
5495         im.set_alpha(alpha)
5496         if im.get_clip_path() is None:
C:ProgramDataAnaconda3envsgenerativelibsite-packagesmatplotlibimage.py in set_data(self, A)
640         if (self._A.dtype != np.uint8 and
641                 not np.can_cast(self._A.dtype, float, "same_kind")):
--> 642             raise TypeError("Image data cannot be converted to float")
643 
644         if not (self._A.ndim == 2
TypeError: Image data cannot be converted to float

我也尝试过这个路径,但得到了相同的错误

_URL = 'https://people.eecs.berkeley.edu/~tinghuiz/projects/pix2pix/datasets/facades.tar.gz'
path_to_zip = tf.keras.utils.get_file('facades.tar.gz',
origin=_URL,
extract=True)
PATH = os.path.join(os.path.dirname(path_to_zip), 'facades/')

此错误通常表示您没有正确读取图像。

我曾尝试在Colab中执行pix2pix代码,并成功运行。如果您遇到任何问题,请尝试下面的代码并告诉我们。

import tensorflow as tf
print(tf.__version__)
import os
import time
import numpy as np
from matplotlib import pyplot as plt
from IPython import display
_URL = 'https://people.eecs.berkeley.edu/~tinghuiz/projects/pix2pix/datasets/facades.tar.gz'
path_to_zip = tf.keras.utils.get_file('facades.tar.gz',
origin=_URL,
extract=True)
PATH = os.path.join(os.path.dirname(path_to_zip), 'facades/')
BUFFER_SIZE = 400
BATCH_SIZE = 1
IMG_WIDTH = 256
IMG_HEIGHT = 256
def load(image_file):
image = tf.io.read_file(image_file)
image = tf.image.decode_jpeg(image)
w = tf.shape(image)[1]
w = w // 2
real_image = image[:, :w, :]
input_image = image[:, w:, :]
input_image = tf.cast(input_image, tf.float32)
real_image = tf.cast(real_image, tf.float32)
return input_image, real_image

inp, re = load(PATH+'train/100.jpg')
plt.figure()
plt.imshow(inp/255.0)
plt.figure()
plt.imshow(re/255.0)

仅当当前工作目录中有datasets时,才按如下方式设置路径。

PATH = os.path.join('datasets/','facades/')
inp, re = load(PATH+'train/100.jpg')

PATH = os.path.join('datasets/','facades/', 'train/')
inp, re = load(PATH+'100.jpg')

相关内容

  • 没有找到相关文章

最新更新