python - 重新创建着色神经网络代码
问题描述
我正在尝试从该网站重新创建一个项目:
这是我想询问的代码的一部分:
#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
解决方案
推荐阅读
- bash - 为什么交互式 kubectl bash 会话中缺少空格/换行符
- java - 在 log4j2 GelfLayout 中提供方法名称
- python-3.x - Python-Pandas:我想要 groupby 用户和日期范围(-7)天
- excel - 如何从 Excel 获取上一季度(发生在上一年)?
- database - 数据库结构(MongoDB)的最佳实践
- servicestack - 有没有办法在 servicestack 中应用行级安全性?
- css - 如何使用样式组件定位元素的第 n 个类型
- java - Java填充嵌套HashMap
- c# - 如何使用 ShortName 属性?
- javascript - 如何添加 JavaScript cookie 同意警报在 3 分钟后消失