从Pytork转换为coreml模型太大

2024-05-14 13:58:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用coremltools将我的pytorch(.pth)模型转换为coreml(.mlmodel)。我的初始pth文件的大小约为218mb,我使用以下代码进行转换

model = model.UNet_ConvLSTM(config.img_channel, config.class_num).to(device)
model.load_state_dict(torch.load(config.pretrained_path))
model.eval()
input = rand_tensor = torch.rand((1, 5, 3, 128, 256), device=device)
print(input.shape)
trace = torch.jit.trace(model, input)
mlmodel = ct.convert(
    trace,
    inputs=[ct.TensorType(name="input", shape=input.shape)],
)
mlmodel.save("unetlstm.mlmodel")

转换成功,但输出文件约900mb大。为什么会发生这种情况?我可以做些什么来减小模型的大小


Tags: 文件模型configinputmodeldevicetraceload

热门问题