对不起,我的英语不好=。=
我创建了一个keras模型,并使用tf.keras.estimator.model_to_estimator
转换为估计量,但是该模型是多输入的,我该如何创建数据集来提供数据?
这是我的型号代码:
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?
答案 0 :(得分:0)
首先,输入输入占位符:
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)]
,请执行以下操作:
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
。