我正在关注this tutorial以构建图像二进制分类器。按照说明操作,我的下面的代码是尝试加载图像" test_00000.png"为了随机生成原始图像的20个变换:
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
img = load_img('/Users/Steven/data/image_data/test_00000.png') # this is a PIL image
x = img_to_array(img) # this is a Numpy array with shape (3, 150, 150)
x = x.reshape((1,) + x.shape) # this is a Numpy array with shape (1, 3, 150, 150)
# the .flow() command below generates batches of randomly transformed images
# and saves the results to the `preview/` directory
i = 0
for batch in datagen.flow(x, batch_size=1,
save_to_dir='/Users/Steven/data/image_data/preview', save_prefix='test', save_format='png'):
i += 1
if i > 20:
break # otherwise the generator would loop indefinitely
但我收到了一个错误:
Traceback (most recent call last):
File "tf.py", line 28, in <module>
save_to_dir='/Users/Steven/data/image_data/preview', save_prefix='test', save_format='png'):
File "C:\Users\Steven\Anaconda3\lib\site-packages\keras\preprocessing\image.py", line 1069, in __next__
return self.next(*args, **kwargs)
File "C:\Users\Steven\Anaconda3\lib\site-packages\keras\preprocessing\image.py", line 1189, in next
return self._get_batches_of_transformed_samples(index_array)
File "C:\Users\Steven\Anaconda3\lib\site-packages\keras\preprocessing\image.py", line 1171, in _get_batches_of_transformed_samples
img.save(os.path.join(self.save_to_dir, fname))
File "C:\Users\Steven\Anaconda3\lib\site-packages\PIL\Image.py", line 1932, in save
fp = builtins.open(filename, "w+b")
FileNotFoundError: [Errno 2] No such file or directory: '/Users/Steven/data/image_data/preview\\test_0_2149.png'
我手动建立了一个名为&#34的文件夹;预览&#34;在/ User / Steven / data / image_data中,这就是为什么我不明白这个错误是如何发生的。感谢您的帮助!
答案 0 :(得分:1)
我遇到了同样的问题,我认为原因是API调用发生了一些变化。相反,可以使用:
tf.keras.preprocessing.image.load_img(
path, grayscale=False, color_mode='rgb', target_size=None,
interpolation='nearest' )
记住要更改路径:)
答案 1 :(得分:0)
正确的路径应该是
$ ./bin/array_rotate
before:
0 1 2 3 4 5 6 7 8 9
elimination
(remove indexes 2,0,7,3,2,3,2,1,1):
index 2 (2) to end 9
0 1 3 4 5 6 7 8 9 2
index 0 (0) to end 8
1 3 4 5 6 7 8 9 0 2
index 7 (9) is last index 7 - no swap.
1 3 4 5 6 7 8 9 0 2
index 3 (5) to end 6
1 3 4 6 7 8 5 9 0 2
index 2 (4) to end 5
1 3 6 7 8 4 5 9 0 2
index 3 (7) to end 4
1 3 6 8 7 4 5 9 0 2
index 2 (6) to end 3
1 3 8 6 7 4 5 9 0 2
index 1 (3) to end 2
1 8 3 6 7 4 5 9 0 2
index 1 (8) is last index 1 - no swap.
1 8 3 6 7 4 5 9 0 2
after:
1 8 3 6 7 4 5 9 0 2
您忘记了分区字母'C:\\Users\\Steven\\data\\image_data\\preview\\test_0_2149.png'
P.S .:我在Windows上遇到类似的问题,发现我的某些图像的完整路径具有超过C:\\
个字符,这在Windows上是一个限制。我的解决方案是将文件夹移动到最浅的路径。