使用tensorflow估计器创建多输入 [英] what use tensorflow estimator create multi-input
问题描述
对不起,我的英语不好=.=
Sorry my English is poor =。=
我创建了一个keras模型并使用tf.keras.estimator.model_to_estimator
转换为估算器,但是该模型是多输入的,我该如何创建数据集来提供数据?
I create a keras model and use tf.keras.estimator.model_to_estimator
convert to estimator but the model is multi-input, what can I create Dataset feed the data?
这是我的型号代码:
model = VGG19(include_top=False, input_shape=(182, 182 , 3))
y = model.output
y = keras.layers.Flatten()(y)
y = keras.layers.Dense(512, activation='relu')(y)
y = keras.layers.Dense(256, activation='relu')(y)
y = keras.layers.Dense(128, activation='relu')(y)
model = keras.Model(inputs=model.input, outputs=y)
input_image = keras.layers.Input(shape=(182, 182, 3))
input_anchor = keras.layers.Input(shape=(182, 182, 3))
out_image = model(input_image)
out_anchor = model(input_anchor)
out = keras.layers.concatenate([out_image, out_anchor])
out = keras.layers.Dense(1, activation='sigmoid')(out)
img_model = keras.Model([input_image, input_anchor], out)
face_model.compile(optimizer=tf.train.AdamOptimizer(1e-4, loss='binary_crossentropy', metrics=['accuracy'])
distribution = tf.contrib.distribute.CollectiveAllReduceStrategy(num_gpus_per_worker=0)
config = tf.estimator.RunConfig(model_dir='/home/zjq/test/image_model.h5', train_distribute=distribution)
est_model = tf.keras.estimator.model_to_estimator(keras_model=image_model, config=config)
现在,我有一个输入列表,形状为[(100000,182,182,3),(100000,182,182,3),(100000,1)],如何定义输入函数return tf .data.DataSet?
Now, I have a input list, the shape is [(100000, 182, 182, 3), (100000, 182, 182, 3), (100000, 1)],how to define the input function return tf.data.DataSet?
推荐答案
首先,输入输入占位符:
First, name input placeholders:
input_image = keras.layers.Input(shape=(182, 182, 3),name='image')
input_anchor = keras.layers.Input(shape=(182, 182, 3),name='anchor')
如果输入数据为train_data
并且形状为[(100000, 182, 182, 3), (100000, 182, 182, 3), (100000, 1)]
,请执行以下操作:
If your input data are train_data
and shape is [(100000, 182, 182, 3), (100000, 182, 182, 3), (100000, 1)]
, then do it:
BATCH_SIZE = 512
EPOCHS = 4
def input_fn(data, epochs, batch_size):
# Convert the inputs to a Dataset.
dataset = tf.data.Dataset.from_tensor_slices(({'image':data[0],'anchor':data[1]}, data[2]))
# Shuffle, repeat, and batch the examples.
SHUFFLE_SIZE = 1000
dataset = dataset.shuffle(SHUFFLE_SIZE).repeat(epochs).batch(batch_size)
dataset = dataset.prefetch(2)
# Return the dataset.
return dataset
est_model.train(lambda :input_fn(train_data,EPOCHS,BATCH_SIZE))
可以根据需要调整参数BATCH_SIZE
,EPOCHS
和SHUFFLE_SIZE
.
The parameters BATCH_SIZE
,EPOCHS
and SHUFFLE_SIZE
can be adjusted according to your needs.
这篇关于使用tensorflow估计器创建多输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!