deephealthproject / eddl

European Distributed Deep Learning (EDDL) library. A general-purpose library initially developed to cover deep learning needs in healthcare use cases within the DeepHealth project.
https://deephealthproject.github.io/eddl/
MIT License
34 stars 10 forks source link

Importing model weights with onnx #279

Closed PerloDaniele closed 3 years ago

PerloDaniele commented 3 years ago

Hi everyone, I'm trying to import a Image-Net pretrained onnx model, but if I inspect the eddl layer weights, they are re-initializated after the build call.

torch.onnx.export( model , dummy_in , f"onnx_models/{name}.onnx" , export_params=True, training = torch.onnx.TrainingMode.EVAL ,  opset_version=12, do_constant_folding=False)
...
...
net = eddl.import_net_from_onnx_file(f"onnx_models/{name}.onnx")
eddl.build(
            net, eddl.sgd(0.001, 0.9),["soft_cross_entropy"], ["categorical_accuracy"],
            eddl.CS_GPU(args.gpu) if args.gpu else eddl.CS_CPU()
)
eddl.set_mode(net, 0) #I want to evaluate the pytorch pretrained model in eddl
print(net.layers[1].params[0].getdata()[:3,:3,:3])

gives me always different values.

I think it happens because the build method call .initialize(), but how can i avoid that? Do i need to build the net just for inference?

chavicoski commented 3 years ago

In the call to the build function you have an aditional argument after the computing service for doing what you want. The name of the parameter is "init_weights" and if you set it to False the weights will not be initialized. Link to the pyeddl doc: https://deephealthproject.github.io/pyeddl/api/eddl.html#pyeddl.eddl.build