keras的ImageDataGenerator和flow()的用法说明
ImageDataGenerator的参数自己看文档
fromkeras.preprocessingimportimage importnumpyasnp X_train=np.ones((3,123,123,1)) Y_train=np.array([[1],[2],[2]]) generator=image.ImageDataGenerator(featurewise_center=False, samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, zca_whitening=False, zca_epsilon=1e-6, rotation_range=180, width_shift_range=0.2, height_shift_range=0.2, shear_range=0, zoom_range=0.001, channel_shift_range=0, fill_mode='nearest', cval=0., horizontal_flip=True, vertical_flip=True, rescale=None, preprocessing_function=None, data_format='channels_last') a=generator.flow(X_train,Y_train,batch_size=20)#生成的是一个迭代器,可直接用于for循环 ''' batch_size如果小于X的第一维m,next生成的多维矩阵的第一维是为batch_size,输出是从输入中随机选取batch_size个数据 batch_size如果大于X的第一维m,next生成的多维矩阵的第一维是m,输出是m个数据,不过顺序随机 ,输出的X,Y是一一对对应的 如果要直接用于tf.placeholder(),要求生成的矩阵和要与tf.placeholder相匹配 ''' X,Y=next(a) print(Y) X,Y=next(a) print(Y) X,Y=next(a) print(Y) X,Y=next(a)
输出
[[2] [1] [2]] [[2] [2] [1]] [[2] [2] [1]] [[2] [2] [1]]
补充知识:tensorflow与keras混用之坑
在使用tensorflow与keras混用是model.save是正常的但是在load_model的时候报错了在这里mark一下
其中错误为:TypeError:tupleindicesmustbeintegers,notlist
再一一番百度后无结果,上谷歌后找到了类似的问题。但是是一对鸟文不知道什么东西(翻译后发现是俄文)。后来谷歌翻译了一下找到了解决方法。故将原始问题文章贴上来警示一下
原训练代码
fromtensorflow.python.keras.preprocessing.imageimportImageDataGenerator fromtensorflow.python.keras.modelsimportSequential fromtensorflow.python.keras.layersimportConv2D,MaxPooling2D,BatchNormalization fromtensorflow.python.keras.layersimportActivation,Dropout,Flatten,Dense #Каталогсданнымидляобучения train_dir='train' #Каталогсданнымидляпроверки val_dir='val' #Каталогсданнымидлятестирования test_dir='val' #Размерыизображения img_width,img_height=800,800 #Размерностьтензоранаосновеизображениядлявходныхданныхвнейроннуюсеть #backendTensorflow,channels_last input_shape=(img_width,img_height,3) #Количествоэпох epochs=1 #Размермини-выборки batch_size=4 #Количествоизображенийдляобучения nb_train_samples=300 #Количествоизображенийдляпроверки nb_validation_samples=25 #Количествоизображенийдлятестирования nb_test_samples=25 model=Sequential() model.add(Conv2D(32,(7,7),padding="same",input_shape=input_shape)) model.add(BatchNormalization()) model.add(Activation('tanh')) model.add(MaxPooling2D(pool_size=(10,10))) model.add(Conv2D(64,(5,5),padding="same")) model.add(BatchNormalization()) model.add(Activation('tanh')) model.add(MaxPooling2D(pool_size=(10,10))) model.add(Flatten()) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(10,activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer="Nadam", metrics=['accuracy']) print(model.summary()) datagen=ImageDataGenerator(rescale=1./255) train_generator=datagen.flow_from_directory( train_dir, target_size=(img_width,img_height), batch_size=batch_size, class_mode='categorical') val_generator=datagen.flow_from_directory( val_dir, target_size=(img_width,img_height), batch_size=batch_size, class_mode='categorical') test_generator=datagen.flow_from_directory( test_dir, target_size=(img_width,img_height), batch_size=batch_size, class_mode='categorical') model.fit_generator( train_generator, steps_per_epoch=nb_train_samples//batch_size, epochs=epochs, validation_data=val_generator, validation_steps=nb_validation_samples//batch_size) print('Сохраняемсеть') model.save("grib.h5") print("Сохранениезавершено!")
模型载入
fromtensorflow.python.keras.preprocessing.imageimportImageDataGenerator fromtensorflow.python.keras.modelsimportSequential fromtensorflow.python.keras.layersimportConv2D,MaxPooling2D,BatchNormalization fromtensorflow.python.keras.layersimportActivation,Dropout,Flatten,Dense fromkeras.modelsimportload_model print("Загрузкасети") model=load_model("grib.h5") print("Загрузказавершена!")
报错
/usr/bin/python3.5/home/disk2/py/neroset/do.py /home/mama/.local/lib/python3.5/site-packages/h5py/__init__.py:36:FutureWarning:Conversionofthesecondargumentofissubdtypefrom`float`to`np.floating`isdeprecated.Infuture,itwillbetreatedas`np.float64==np.dtype(float).type`. from._convimportregister_convertersas_register_converters UsingTensorFlowbackend. Загрузкасети Traceback(mostrecentcalllast): File"/home/disk2/py/neroset/do.py",line13,inmodel=load_model("grib.h5") File"/usr/local/lib/python3.5/dist-packages/keras/models.py",line243,inload_model model=model_from_config(model_config,custom_objects=custom_objects) File"/usr/local/lib/python3.5/dist-packages/keras/models.py",line317,inmodel_from_config returnlayer_module.deserialize(config,custom_objects=custom_objects) File"/usr/local/lib/python3.5/dist-packages/keras/layers/__init__.py",line55,indeserialize printable_module_name='layer') File"/usr/local/lib/python3.5/dist-packages/keras/utils/generic_utils.py",line144,indeserialize_keras_object list(custom_objects.items()))) File"/usr/local/lib/python3.5/dist-packages/keras/models.py",line1350,infrom_config model.add(layer) File"/usr/local/lib/python3.5/dist-packages/keras/models.py",line492,inadd output_tensor=layer(self.outputs[0]) File"/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py",line590,in__call__ self.build(input_shapes[0]) File"/usr/local/lib/python3.5/dist-packages/keras/layers/normalization.py",line92,inbuild dim=input_shape[self.axis] TypeError:tupleindicesmustbeintegersorslices,notlist Processfinishedwithexitcode1
战斗种族解释
убераюBatchNormalizationвсёработаетхорошо.Неподскажитевчёмошибка?Выяснилчтосохранениеkerasинормализацияtensorflowнеработаютвместенужнопростоизменитьстрокуимпорта.(译文:整理BatchNormalization一切正常。不要告诉我错误是什么?我发现保存keras和规范化tensorflow不能一起工作;只需更改导入字符串即可。)
强调文本强调文本
keras.preprocessing.imageimportImageDataGenerator keras.modelsimportSequential keras.layersimportConv2D,MaxPooling2D,BatchNormalization keras.layersimportActivation,Dropout,Flatten,Dense
##完美解决
##附上原文链接
https://qa-help.ru/questions/keras-batchnormalization
以上这篇keras的ImageDataGenerator和flow()的用法说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。