keras 从.pt模型转换为.h5模型

tpxzln5u  于 12个月前  发布在  其他
关注(0)|答案(4)|浏览(217)

我正在使用谷歌colab.我想转换.pt模型从我的谷歌驱动器到.h5模型.我按照链接https://github.com/gmalivenko/pytorch2kerashttps://www.programmersought.com/article/57938937172/和安装库,并编写代码如下:

%pip install pytorch2keras
%pip install onnx==1.8.1

个字符
但它给我的错误是:

WARNING:pytorch2keras:Custom shapes isn't supported now.
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-53-eef217a11c8a> in <module>()
      8 input_var = Variable(torch.FloatTensor(input_np))
      9 model='/content/gdrive/MyDrive/model.pt'
---> 10 pytorch_to_keras(model,input_np,input_shapes= [(10, 32, 32,)], verbose=True)
     11 
     12 

/usr/local/lib/python3.7/dist-packages/pytorch2keras/converter.py in pytorch_to_keras(model, args, input_shapes, change_ordering, verbose, name_policy, use_optimizer, do_constant_folding)
     51     args = tuple(args)
     52 
---> 53     dummy_output = model(*args)
     54 
     55     if isinstance(dummy_output, torch.autograd.Variable):

TypeError: 'str' object is not callable

gijlo24d

gijlo24d1#

啊,PyTorch到Tensorflow的经典问题。多年来,许多库来来去去,但我发现ONNX的工作最一致。你可以尝试这样的东西。
PyTorch特有的是Dynamic Computational Graph。动态计算图意味着PyTorch模型可以动态适应不同的输入大小。您可以指定哪些轴需要动态调整大小。下面是一些将CNN从PyTorch转换为ONNX的最小代码。

import onnx
import torch

model = get_model()
model.eval()

# Test model on sample image size 
example_input = torch.randn((1, 3, img_size, img_size), requires_grad=True)
model(example_input)

# Set input and output names, include more names in the list if your model has more than 1 input/output
input_names = ["input0"]
output_names = ["output0"]

# Set dynamic axes (in this case, make the batch a dynamic dimension)
dynamic_axes = {'input0': {0: 'batch'}, 'output0': {0: 'batch'}}

# Export model with the above parameters
torch_out = torch.onnx.export(
    model, example_input, 'model.onnx', export_params=True, input_names=input_names, output_names=output_names, 
    dynamic_axes=dynamic_axes, operator_export_type=torch.onnx.OperatorExportTypes.ONNX
)

# Use ONNX checker to verify integrity of model
onnx_model = onnx.load("model.onnx")
onnx.checker.check_model(onnx_model)

字符串
还可以将模型的高度和宽度设置为动态输入大小,

dynamic_axes['input0'][2] = 'height'
dynamic_axes['input0'][3] = 'width'


接下来,我们将ONNX模型转换为Tensorflow SavedModel。

from onnx_tf.backend import prepare
import onnx

onnx_model = onnx.load('model.onnx')
tf_model = prepare(onnx_model)
tf_model.export_graph('tf_model')


tf_model现在是一个Tensorflow SavedModel。

zlwx9yxi

zlwx9yxi2#

看起来你还没有导入pytorch_to_keras方法来开始。另外,作为第一个参数,你可能需要提供实际的模型,而不是模型路径。给予这些,但是,我不熟悉这个库。

axkjgtzd

axkjgtzd3#

“model”应该是一个(加载的)pytorch模型(而不是字符串)

model = torch.load('your_model.py')
keras_model = pytorch_to_keras(model,input_var,input_shapes= [(10, 32, 32,)], verbose=True)

字符串

ulydmbyx

ulydmbyx4#

如果你只是想将.pt模型状态转换为.h5,而不用担心Keras或ONNX的兼容性,你可以加载模型状态,然后将每个Tensor递归导出到.h5文件:

import torch
import h5py

# Load your model state .pt
model_state_file = 'path/to/file.pt'
model_state = torch.load(model_state_file, map_location=torch.device('cpu'))

# Recursively save to .h5
def save_dict_h5py(h5py_file, group_name, dic, verbose=False):    
    for key, item in dic.items():
        if isinstance(item, (dict, collections.OrderedDict)):
            save_dict_h5py(h5py_file, group_name + '/' + str(key), item)
        else:
            if verbose: print(f'| writing {group_name + "/" + str(key)}')
            h5py_file[group_name + '/' + str(key)] = item

with h5py.File('path/to/file.h5', 'w') as f:
    save_dict_h5py(f, group_name='model_state', dic=model_state, verbose=True)

字符串
这个函数应该打印类似于:

| writing model_state/fc1.0.weight
| writing model_state/fc1.0.bias
| writing model_state/unet_block.conv1.0.weight
| writing model_state/unet_block.conv1.0.bias


现在你可以加载你的.h5文件:

with h5py.File('path/to/file.h5', 'r') as f:
    print('fc1.0 bias shape:', f['model_state']['fc1.0.bias'])
    print('fc1.0 bias values:', f['model_state']['fc1.0.bias'][()])
    print('upconv8.0 weight shape:', f['model_state']['unet_block.upconv8.0.weight'])

# ------ Returns something like ------
# fc1.0 bias shape: <HDF5 dataset "fc1.0.bias": shape (16384,), type "<f4">
# fc1.0 bias values: [0.02804164 0.04839667 0.04619668 ... 0.04043121 0.03506938 0.05372864]
# upconv8.0 weight shape: <HDF5 dataset "unet_block.upconv8.0.weight": shape (512, 512, 9, 9), type "<f4">

相关问题