AlexNet (filter groups) cannot be well resolved inside dlfeval (dimension mismatch error)
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm trying to implement a grad-CAM for a 3D imaging model (with input size 227*227*7), so I used dlfeval for automatic differentiation. However, although I can successfully get a output with predict function outside of dlfeval, dimension mismatch error occurred for the same code inside dlfeval.
My DL model if anyone willing to try (and thank you): https://drive.google.com/file/d/1QJmVX31SWOVd3X_zuzgOyJMi123E4J-D/view?usp=sharing
Update:
After looking into the model, I found it is because I'm using AlexNet backbone, and AlexNet uses filter groups for some certain conv layers. (e.g. 'conv2' layer has NumChannels = [48, 48] instead of 96. the input from previous layer has 96 channels, while the size of weights of 'conv2' layer is 5*5*48*256, and thus the input channel 96 is not equal to weights channel 48) But I still do not get why the filter groups cannot be well resolved inside dlfeval, but work properly outside dlfeval.
Code (I'm using zeros array instead of real images here, but the same thing happened to my real images):
image_data = zeros(227,227,7);
load(['case_1.mat']); % DL model
dlnet = dag2dlnetwork(net_final);
dlInputData = dlarray(image_data);
prediction1 = predict(dlnet, dlInputData)
prediction2 = dlfeval(@gradcam, dlnet, dlInputData)
function prediction = gradcam(dlnet, dlInputData)
prediction = predict(dlnet, dlInputData);
end
Outputs (and errors) :
prediction1 =
1×2 single dlarray
0.0301 0.9699
Error using dlnetwork/predict (line 658)
Execution failed during layer(s) 'conv2, relu2'.
Error in test2>gradcam (line 11)
prediction = predict(dlnet, dlInputData);
Error in deep.internal.dlfeval (line 17)
[varargout{1:nargout}] = fun(x{:});
Error in deep.internal.dlfevalWithNestingCheck (line 19)
[varargout{1:nargout}] = deep.internal.dlfeval(fun,varargin{:});
Error in dlfeval (line 31)
[varargout{1:nargout}] = deep.internal.dlfevalWithNestingCheck(fun,varargin{:});
Error in test2 (line 9)
prediction2 = dlfeval(@gradcam, dlnet, dlInputData)
Caused by:
Error using deep.internal.dlarray.validateConvolutionChannelDimension (line 26)
The size of the 'C' dimension of the input data (96) must be equal to the number of channels of weights (48,
specified by the size of weights dimension number 3).
0 个评论
采纳的回答
Joss Knight
2024-4-16
Sorry about this. This is because you have an old version of alexnet (although yes, it shouldn't error). For now, please get yourself a new alexnet using imagePretrainedNetwork('alexnet'), which will work correctly.
Alternatively you can manually convert your Convolution layers which have two filter groups into GroupedConvolution2DLayer objects using replaceLayer.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!