AlexNet (filter groups) cannot be well resolved inside dlfeval (dimension mismatch error)
3 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Joss Knight
on 16 Apr 2024
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 Comments
More Answers (0)
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!