Код: Выделить всё
model = tf.saved_model.load(model_path)
ws = []
for i in range(len(model.variables)):
ws.append((i, model.variables[i].name, model.variables[i].numpy()))
with open("manually_dumped_contentnet_weights.pkl", "wb") as ofile:
pickle.dump(ws, ofile)
Я заметил, что в коде TF модель загружается не напрямую, а в сеансе tf:
Код: Выделить всё
with Session(graph=Graph(), config=ConfigProto(allow_soft_placement=True, log_device_placement=False)) as sess:
saved_model.loader.load(sess, [saved_model.tag_constants.SERVING], model_path)
patch_feature, patch_label = sess.run(output_nodes,feed_dict={input_node: patch})
При загрузке данных я выполнил транспозиции (3,2,0,1) для conv2d и (2,3,0,1 ) для conv2d по глубине:
Код: Выделить всё
def reload_conv2d(layer, weights):
### weights is a tuple, where each element has consists of a truple: (1) an index number, (2) name of the layer the weights were dumped from in TF, and (3) the weights
count = 0
if (
"/conv2d/kernel" not in weights[0][1]
and "/conv2d_1/kernel" not in weights[0][1]
and "depthwise_conv2d/depthwise_kernel" not in weights[0][1]
and "final_conv2d/final_conv2d" not in weights[0][1]
):
raise ValueError(
f"need to have conv2d/kernel on the first index but got {weights[0][1]}"
)
transpose_shape = (2,3,0,1) if "depthwise" in weights[0][1] else (3, 2, 0, 1)
transposed_weights = torch.from_numpy(weights[0][2].transpose(transpose_shape[0], transpose_shape[1], transpose_shape[2], transpose_shape[3]))
layer.weight.data = transposed_weights
count += 1
if layer.bias is not None or layer.bias:
if (
"/conv2d/bias" not in weights[1][1]
and "/conv2d_1/bias" not in weights[1][1]
):
raise ValueError(
f"need to have conv2d/bias on the second index but got {weights[1][1]}"
)
layer.bias.data = (
torch.from_numpy(weights[1][2])
if type(weights[1][2]) == np.ndarray
else torch.from_numpy(weights[1][2])
)
count += 1
return layer, count
Подробнее здесь: https://stackoverflow.com/questions/790 ... ficientnet