在Keras中训练我的顺序模型时遇到了一些问题。我是这个主题的新手,因此涉及很多以下教程和代码片段...
我正在构建一个基本的CNN,以基于正射影像来区分线性基础结构和地貌特征。由于图像非常大,因此我必须创建一个遵循Keras Docs的数据生成器。 该模型的编译工作正常。但是每次我运行model.fit_generator()命令时,都会出现错误,我的训练图像之一丢失了(不是)。我设置了五个纪元开始,该错误已在第一个纪元中发生。
我感谢任何想法可能出了问题。
我正在使用带有iypthon笔记本theano后端的Ubuntu 16.04.5 LTS进行工作。
train_path = '/path/to/train/folder'
tree_top = os.listdir(train_path)
training_filenames = os.listdir('%s/%s/' %(train_path, tree_top[0])) + os.listdir('%s/%s/' %(train_path, tree_top[1]))
valid_path = '/path/to/valid/folder'
tree_top = os.listdir(valid_path)
valid_filenames = os.listdir('%s/%s/' %(valid_path, tree_top[0])) + os.listdir('%s/%s/' %(valid_path, tree_top[1]))
数据生成器
from skimage.io import imread
from skimage.transform import resize
import numpy as np
class MY_Generator(Sequence): # inherits from Sequence class
def __init__(self, image_filenames, labels, batch_size):
self.image_filenames, self.labels = image_filenames, labels
self.batch_size = batch_size
def __len__(self): # computes number of batches by dividing sample size by the batch_size
return np.ceil(len(self.image_filenames) / float(self.batch_size))
num_training_samples = len(self.image_filenames)
return num_training_samples
def __getitem__(self, idx):
batch_x = self.image_filenames[idx * self.batch_size:(idx + 1) * self.batch_size]
batch_y = self.labels[idx * self.batch_size:(idx + 1) * self.batch_size]
return np.array([
resize(imread(file_name), (200, 200))
for file_name in batch_x]), np.array(batch_y)
my_training_batch_generator = MY_Generator(training_filenames, tree_top, batch_size)
my_validation_batch_generator = MY_Generator(valid_filenames, tree_top, batch_size)
convnet
model = Sequential([
Conv2D(3, (3, 3), activation='relu', input_shape=(300,400,3)),
Flatten(),
Dense(2, activation='softmax'),
])
model.compile(Adam(lr=.0001), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit_generator(generator=my_training_batch_generator,
steps_per_epoch=(len(my_training_batch_generator.image_filenames) // batch_size),
epochs=5,
verbose=1,
validation_data=my_validation_batch_generator,
validation_steps=(len(my_validation_batch_generator.image_filenames) // batch_size)
)
输出如下:
第1/5版
-------------------------------------------------- ---------------------------- IOError Traceback(最近的呼叫 最后)在() 4 verbose = 1, 5validation_data = my_validation_batch_generator, ----> 6validation_steps = [len(my_validation_batch_generator.image_filenames) // batch_size) 7)
/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.pyc在 包装器(* args,** kwargs) 89 warnings.warn('将您的
' + object_name + '
调用更新为'+ 90'Keras 2 API:'+签名,stacklevel = 2) ---> 91 return func(* args,** kwargs) 92 wrapper._original_function = func 93返回包装器/usr/local/lib/python2.7/dist-packages/keras/engine/training.pyc在 fit_generator(self,generator,steps_per_epoch,epochs,verbose, 回调,validation_data,validation_steps,class_weight, max_queue_size,工作者,use_multiprocessing,随机播放,initial_epoch) 1416 use_multiprocessing = use_multiprocessing,1417
shuffle = shuffle, -> 1418 initial_epoch = initial_epoch)1419 1420 @ interfaces.legacy_generator_methods_support/usr/local/lib/python2.7/dist-packages/keras/engine/training_generator.pyc 在fit_generator(model,generator,steps_per_epoch,epochs,verbose, 回调,validation_data,validation_steps,class_weight, max_queue_size,工作者,use_multiprocessing,随机播放,initial_epoch) 179 batch_index = 0 180,而steps_done
181 generator_output =下一步(output_generator) 182 183 if not hasattr(generator_output,' len '): /usr/local/lib/python2.7/dist-packages/keras/utils/data_utils.pyc在 得到(个体) 599除了e为例外: 600 self.stop() -> 601 six.reraise(* sys.exc_info()) 602 603
/usr/local/lib/python2.7/dist-packages/keras/utils/data_utils.pyc在 得到(个体) 593尝试: 594而self.is_running(): -> 595个输入= self.queue.get(block = True).get() 第596章 597如果输入不是None:
/usr/lib/python2.7/multiprocessing/pool.pyc在get(self,timeout)中 565返回self._value 第566章 -> 567提高self._value 568 569 def set(self,i,obj):
IOError:[Errno 2]没有这样的文件或目录:'DJI_0168.JPG'