Я новичок в углубленном изучении.Я работаю над двумя ноутбуками Jupyter.С одним из них работает код, а другой выдает сообщение об ошибке.Может быть, кто-то может мне помочь.
Я уже попробовал все решения, показанные здесь.К сожалению, ни одно решение не решило мою проблему.
from PIL import Image
import os
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
import keras
keras.__version__
import os
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
base_dir = 'path'
train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')
test_dir = os.path.join(base_dir, 'test')
datagen = ImageDataGenerator(rescale=1./255)
batch_size = 20
def extract_features(directory, sample_count):
features = np.zeros(shape=(sample_count, 7, 7, 1280))
labels = np.zeros(shape=(sample_count))
generator = datagen.flow_from_directory(
directory,
target_size=(224, 224),
batch_size=batch_size,
class_mode='binary')
i = 0
for inputs_batch, labels_batch in generator:
features_batch = conv_base.predict(inputs_batch)
features[i * batch_size : (i + 1) * batch_size] = features_batch
labels[i * batch_size : (i + 1) * batch_size] = labels_batch
i += 1
if i * batch_size >= sample_count:
# Note that since generators yield data indefinitely in a loop,
# we must `break` after every image has been seen once.
break
return features, labels
train_features, train_labels = extract_features(train_dir, 2000)
validation_features, validation_labels = extract_features(validation_dir, 1000)
test_features, test_labels = extract_features(test_dir, 1000)
Локально код выполняется нормально.На работе я получаю следующее сообщение об ошибке.
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-9-e489372cb3cc> in <module>()
----> 1 train_features, train_labels = extract_features(train_dir, 2000)
2 validation_features, validation_labels = extract_features(validation_dir, 1000)
3 test_features, test_labels = extract_features(test_dir, 1000)
<ipython-input-8-12cf5e6b2065> in extract_features(directory, sample_count)
22 class_mode='binary')
23 i = 0
---> 24 for inputs_batch, labels_batch in generator:
25 features_batch = conv_base.predict(inputs_batch)
26 features[i * batch_size : (i + 1) * batch_size] = features_batch
/usr/local/lib/python3.5/dist-packages/keras_preprocessing/image/iterator.py in __next__(self, *args, **kwargs)
98
99 def __next__(self, *args, **kwargs):
--> 100 return self.next(*args, **kwargs)
101
102 def next(self):
/usr/local/lib/python3.5/dist-packages/keras_preprocessing/image/iterator.py in next(self)
110 # The transformation of images is not under thread lock
111 # so it can be done in parallel
--> 112 return self._get_batches_of_transformed_samples(index_array)
113
114 def _get_batches_of_transformed_samples(self, index_array):
/usr/local/lib/python3.5/dist-packages/keras_preprocessing/image/iterator.py in _get_batches_of_transformed_samples(self, index_array)
224 color_mode=self.color_mode,
225 target_size=self.target_size,
--> 226 interpolation=self.interpolation)
227 x = img_to_array(img, data_format=self.data_format)
228 # Pillow images should be closed after `load_img`,
/usr/local/lib/python3.5/dist-packages/keras_preprocessing/image/utils.py in load_img(path, grayscale, color_mode, target_size, interpolation)
102 raise ImportError('Could not import PIL.Image. '
103 'The use of `array_to_img` requires PIL.')
--> 104 img = pil_image.open(path)
105 if color_mode == 'grayscale':
106 if img.mode != 'L':
~/.local/lib/python3.5/site-packages/PIL/Image.py in open(fp, mode)
2685 warnings.warn(message)
2686 raise IOError("cannot identify image file %r"
-> 2687 % (filename if filename else fp))
2688
2689 #
OSError: cannot identify image file 'somepath'