如何解决Keras 与 Tensorflow-2 相同的模型与多个权重混淆第二个推理
所以我一直在尝试制作模型:
- 一个主干特征提取模型(resnet 50 -> resnet-50 每一列的卷积-> Upsampling)
- 使用主干模型提取的特征的多个 HEAD。它们本质上是相同的模型,但权重不同。
我能够创建一个模型并训练它们(Trained Backbone + HEAD1,然后 Froze Backbone 层和 Trained HEAD2)。使用 GradientTape 等,但现在的问题是推理。在我的推论中,我希望这两个 HEAD 都能够在来自 BackBone 的功能上运行,问题是, 我创建了 2 个头,用不同的权重加载它们 但是当我查看推理结果时,HEAD 1 的结果正是我所期望的,但 HEAD2 的结果完全一团糟。
我曾尝试使用单独的 Graph 和 Sessions,但这似乎没有帮助。
import cv2
from splitted_model import back_bone_model,predictor_model
import numpy as np
import math
def resize_image(image,image_short_side=1472):
height,width,_ = image.shape
if height < width:
new_height = image_short_side
new_width = int(math.ceil(new_height / height * width / 32) * 32)
else:
new_width = image_short_side
new_height = int(math.ceil(new_width / width * height / 32) * 32)
resized_img = cv2.resize(image,(new_width,new_height))
return resized_img
def get_hw_head():
graph1 = tf.compat.v1.Graph()
with graph1.as_default():
session1 = tf.compat.v1.Session()
with session1.as_default():
hw_training_head,predictor_head = predictor_model(filters=64,input_size=1440)
predictor_head.load_weights("./checkpoints/HW_HEAD_2.9451_adam.h5",by_name=True)
return predictor_head,graph1,session1
def get_stamps_head():
graph2 = tf.compat.v1.Graph()
with graph2.as_default():
session2 = tf.compat.v1.Session()
with session2.as_default():
hw_training_head,graph2,session2
def get_back_bone():
back_bone = back_bone_model()
back_bone.load_weights("./checkpoints/BACKBONE_2.9451_adam.h5",by_name=True)
return back_bone
back_bone_model = get_back_bone()
predictor_head,graph,session = get_hw_head()
predictor_head2,session2 = get_stamps_head()
# print(predictor_head.summary())
image = cv2.imread("./DB/raw_images/6484.png")
src_image = image.copy()
h,w = image.shape[:2]
image = resize_image(image,2208)
gray_src_image = cv2.cvtColor(src_image,cv2.COLOR_BGR2GRAY)
print(image.shape)
image = image.astype(np.float32)
mean = np.array([103.939,116.779,123.68])
image -= mean
image_input = np.expand_dims(image,axis=0)
P2,P3,P4,P5 = back_bone_model.predict(image_input)
with graph.as_default():
with session.as_default():
output = predictor_head.predict([P2,P5])
p = output[0]
print(output.shape)
bitmap = p > 0.2
bitmap = bitmap[...,0]
temp = (bitmap * 255).astype(np.uint8)
print(temp)
with graph2.as_default():
with session2.as_default():
output2 = predictor_head2.predict([P2,P5])
p2 = output2[0]
print(output2.shape)
bitmap2 = p2 > 0.1
bitmap2 = bitmap2[...,0]
temp2 = (bitmap2 * 255).astype(np.uint8)
# print(temp)
cv2.namedWindow("ImageSTAMP",cv2.WINDOW_NORMAL)
cv2.imshow("ImageSTAMP",temp2)
cv2.namedWindow("Image",cv2.WINDOW_NORMAL)
cv2.imshow("Image",temp)
cv2.waitKey(0)
任何帮助将不胜感激。如果您需要更多信息,请告诉我。
谢谢!!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。