首页 > 解决方案 > 重新创建着色神经网络代码

问题描述

我正在尝试从该网站重新创建一个项目:

如何用 100 行神经网络代码为黑白照片着色

这是我想询问的代码的一部分:

#Load weights
inception = InceptionResNetV2(weights=None, include_top=True)
inception.load_weights('drive/MyDrive/Colab Notebooks/inception_resnet_v2.h5')
inception.graph = tf.compat.v1.get_default_graph()
embed_input = Input(shape=(1000,))

对于这个我不得不改变原来的

inception.graph = tf.get_default_graph()

inception.graph = tf.compat.v1.get_default_graph()

让它工作。其余的部分:

def create_inception_embedding(grayscaled_rgb):
    grayscaled_rgb_resized = []
    for i in grayscaled_rgb:
        i = resize(i, (299, 299, 3), mode='constant')
        grayscaled_rgb_resized.append(i)
    grayscaled_rgb_resized = np.array(grayscaled_rgb_resized)
    grayscaled_rgb_resized = preprocess_input(grayscaled_rgb_resized)
    with inception.graph.as_default():
        embed = inception.predict(grayscaled_rgb_resized)
    return embed

def image_a_b_gen(batch_size):
    for batch in datagen.flow(Xtrain, batch_size=batch_size):
        grayscaled_rgb = gray2rgb(rgb2gray(batch))
        embed = create_inception_embedding(grayscaled_rgb)
        lab_batch = rgb2lab(batch)
        X_batch = lab_batch[:,:,:,0]
        X_batch = X_batch.reshape(X_batch.shape+(1,))
        Y_batch = lab_batch[:,:,:,1:] / 128
        yield ([X_batch, create_inception_embedding(grayscaled_rgb)], Y_batch)

#Train model      
tensorboard = TensorBoard(log_dir="/output")
model.compile(optimizer='adam', loss='mse')
model.fit(image_a_b_gen(batch_size), callbacks=[tensorboard], epochs=1000, steps_per_epoch=20)

我在最后一行得到的错误是:

ValueError: Calling `Model.predict` in graph mode is not supported when the `Model` instance was constructed with eager mode enabled. Please construct your `Model` instance in graph mode or call `Model.predict` with eager mode enabled.

我检查了许多有关此问题的网站,但找不到任何好的答案。我试过:

inception.compile()
inception.run_eagerly = True

model.compile()
model.run_eagerly = True

但它们都不起作用。有什么想法我可以做不同的吗?

张量流版本 -2.6.0

标签: pythontensorflowmachine-learningkerasneural-network

解决方案


推荐阅读