Python 如何为图像集安装两个keras ImageDataGenerator
我正在为以下任务寻找解决方案或示例: 我有一组从不同角度拍摄的相同物体的图像。 我想用keras构建一个深度CNN,它可以获取两幅图像的集,分别对每幅图像执行数据增强,并将它们集成到一个连接的模型中 更详细的解释: 图像存储在HDF5文件中,具有以下形状:Python 如何为图像集安装两个keras ImageDataGenerator,python,tensorflow,deep-learning,keras,Python,Tensorflow,Deep Learning,Keras,我正在为以下任务寻找解决方案或示例: 我有一组从不同角度拍摄的相同物体的图像。 我想用keras构建一个深度CNN,它可以获取两幅图像的集,分别对每幅图像执行数据增强,并将它们集成到一个连接的模型中 更详细的解释: 图像存储在HDF5文件中,具有以下形状: data['Xp'] # shape=(3000, 224, 224, 3) #RGB images data['Xs'] # shape=(3000, 224, 224, 3) #RGB images data['Y'] # shape=
data['Xp'] # shape=(3000, 224, 224, 3) #RGB images
data['Xs'] # shape=(3000, 224, 224, 3) #RGB images
data['Y'] # shape=(3000, 9) #categorical data.
现在,我想要一台发电机,它可以:
X1\u列
,X2\u列
from keras.layers import Flatten, Dense, Input, Dropout, Convolution2D, MaxPooling2D, Merge
img_input = Input(shape=input_shape)
x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='block1_conv1')(img_input)
x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='block1_conv2')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
# ... more network definition here ....
model1 = Model(img_input, x)
model2 = Model(img_input, x)
merged = Merge([model1, model2], mode='concat')
final_model = Sequential()
final_model.add(merged)
final_model.add(Dense(9, activation='softmax'))
我创建了以下生成器,该生成器生成用于向
安装该型号的发电机
def aug_train_iterator(Xp, Xs, Y, database_file=database_file, is_binary=True):
from itertools import izip
from keras.preprocessing.image import ImageDataGenerator
seed = 7 #make sure that two iterators give same tomato each time...
ig = ImageDataGenerator(dim_ordering='tf', rotation_range=90,
width_shift_range=0.05,
height_shift_range=0.05,
zoom_range=0.05,
fill_mode='constant',
cval=0.0,
horizontal_flip=True,
rescale=1./255)
for batch in izip(ig.flow(Xp,Y, seed=seed), ig.flow(Xs, seed=seed)):
for i in range(len(batch[0][0])):
x1 = batch[0][0][i].reshape(1,224, 224, 3)
x2 = batch[1][i].reshape(1, 224, 224, 3)
y = batch[0][1][i].reshape(1,2)
yield ([x1, x2], y)
gen = aug_train_iterator(Xp, Xs, Y)
final_model.fit_generator(gen, 1000, 20)
现在,当我试着适应模型时
def aug_train_iterator(Xp, Xs, Y, database_file=database_file, is_binary=True):
from itertools import izip
from keras.preprocessing.image import ImageDataGenerator
seed = 7 #make sure that two iterators give same tomato each time...
ig = ImageDataGenerator(dim_ordering='tf', rotation_range=90,
width_shift_range=0.05,
height_shift_range=0.05,
zoom_range=0.05,
fill_mode='constant',
cval=0.0,
horizontal_flip=True,
rescale=1./255)
for batch in izip(ig.flow(Xp,Y, seed=seed), ig.flow(Xs, seed=seed)):
for i in range(len(batch[0][0])):
x1 = batch[0][0][i].reshape(1,224, 224, 3)
x2 = batch[1][i].reshape(1, 224, 224, 3)
y = batch[0][1][i].reshape(1,2)
yield ([x1, x2], y)
gen = aug_train_iterator(Xp, Xs, Y)
final_model.fit_generator(gen, 1000, 20)
它实际上运行了一些图像。。。然后提出约15幅图像的错误:
Epoch 1/20
15/1000 [..............................] - ETA: 606s - loss: 0.7001 - acc: 0.4000
Exception in thread Thread-44:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 404, in data_generator_task
generator_output = next(generator)
File "<ipython-input-134-f128a127c7ce>", line 35, in aug_train_iterator
for batch in izip(ig.flow(Xp,Y, seed=seed), ig.flow(Xs, seed=seed)):
File "/usr/local/lib/python2.7/dist-packages/keras/preprocessing/image.py", line 495, in next
x = self.X[j]
File "/usr/lib/python2.7/dist-packages/h5py/_hl/dataset.py", line 367, in __getitem__
if self._local.astype is not None:
AttributeError: 'thread._local' object has no attribute 'astype'
纪元1/20
15/1000[……]-预计到达时间:606s-损失:0.7001-附件:0.4000
线程-44中的异常:
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python2.7/threading.py”,第810行,在引导程序内部
self.run()
文件“/usr/lib/python2.7/threading.py”,第763行,运行中
自我目标(*自我参数,**自我参数)
文件“/usr/local/lib/python2.7/dist packages/keras/engine/training.py”,第404行,在数据生成器任务中
发电机输出=下一个(发电机)
文件“”,第35行,在aug_train_迭代器中
对于izip中的批次(ig.flow(Xp,Y,种子=种子),ig.flow(Xs,种子=种子)):
文件“/usr/local/lib/python2.7/dist packages/keras/preprocessing/image.py”,下一页第495行
x=自我。x[j]
文件“/usr/lib/python2.7/dist packages/h5py/_hl/dataset.py”,第367行,在__
如果self.\u local.astype不是None:
AttributeError:“thread.\u local”对象没有属性“astype”
问题是什么?使用pickle\u safe=True解决了问题好吧,我错过了keras文档中可能的解决方案。。。如果我将使用相同的seed和关键字参数来适应两个不同的生成器,它应该可以工作。我会测试它。似乎是h5py的问题,尝试通过
pip安装更新它——升级h5py
好吧,使用pickle\u safe=True似乎可以解决这个问题。不知道为什么