An error occurred while importing a .pt file which include PReLU

4 Ansichten (letzte 30 Tage)
Xianda
Xianda am 10 Sep. 2024
Beantwortet: Zuber Khan am 18 Sep. 2024
I train a model in Pytorch, it's structure is as fellow:
self.encoder_forward = nn.Sequential(
nn.Linear(in_features, self.latent_size),
nn.PReLU(),
nn.Linear(self.latent_size, 2*self.latent_size),
nn.PReLU(),
nn.Linear(2*self.latent_size, 3*self.latent_size),
)
I traced it and turn to .pt file, then I use the fellow Matlab code to import it.
net = importNetworkFromPyTorch("model.pt");
inputLayer = featureInputLayer(N,'Name','input');
net = addInputLayer(net,inputLayer,Initialize=true);
netInput=zeros(N,1);
netInput(:,1)=[real(y);imag(y)];
dlNetInput = dlarray(netInput, 'CB'); % 转换为 dlarray 格式,并标记维度
out = predict(net,dlNetInput);
but it occur warning and error. How can I slove it?
  1 Kommentar
Jayanti
Jayanti am 16 Sep. 2024
Can you please provide the complete pytorch, Matlab code and the dataset which you are using as value of y is not present in the available code. Also please tell me the Matlab version you are using.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Zuber Khan
Zuber Khan am 18 Sep. 2024
Hi,
I am assuming that you have installed "Deep Learning Toolbox Converter for PyTorch Models" support package. If not, firstly you need to download it from the following link and then retry the workflow.
Based on the warning and error message, it appears that the network contains placeholders. The importer may not support Parametrized Rectified Linear Unit (PReLU) layers yet, but there is a 'preluLayer' in Deep Learning Toolbox. Replacing these layers in the imported network is going to take a bit of layer surgery, but it should resolve the issue.
You can use the following workflow to replace these layers:
  • Access the layers of imported dlnetwork
  • Identify the indices of 'prelu' layers that need to be replaced
  • Initialize the inbuilt 'preluLayer'
  • Replace these layers with inbuilt 'preluLayer' by using "replaceLayer" function.
I am attaching a code snippet for your reference.
net = importNetworkFromPyTorch("model.pt");
% Obtain Layer Names
info = analyzeNetwork(net,Plots="none");
layerNames = info.LayerInfo.Name;
% Find indices of imported prelu layers that should be replaced
% with inbuilt preluLayer
preluLayerIdx = find(contains(layerNames,'prelu')); %assuming the imported layer name is 'prelu'
for i = 1:numel(preluLayerIdx)
% Create inbuilt preluLayer
layer = preluLayer(Name=['preluLayer',num2str(i)]);
% Replace imported prelu Layer
net = replaceLayer(net,net.Layers(preluLayerIdx(i)).Name,layer,ReconnectBy='order');
end
For more information on the 'preluLayer' present in Deep Learning Toolbox, you can refer to the following MathWorks documentation:
Further, for any clarification on "replaceLayer" function, kindly refer to the below mentioned MathWorks documentation:
I hope it will resolve your issue.
Regards,
Zuber

Kategorien

Mehr zu Image Data Workflows finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by