How to solve the error generated while defining custom FFT layer?

Hello
I was trying to create a custom FFT layer but I'm getting errors, either 'cell' data type related as in 'brace indexing isn't supported' or 'data type isn't supported'. The custom layer is followed by a 2d CNN and defined after the imageinputlayer. I have used imagedatastore to keep my training data. The class definition code is attached. I'm getting error for line 55 but I have tried to resolve that and then I get the data type related error.
Thanks in advance.

1 件のコメント

Matt J
Matt J 2025 年 2 月 25 日
編集済み: Matt J 2025 年 2 月 25 日
Please provide code and input data (as compact as possible) to reproduce the error.

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2025 年 2 月 25 日
編集済み: Matt J 2025 年 2 月 25 日

0 投票

Your predict() method is written to expect that the input X will be a cell array. It's not clear why you expect this, but you shouldn't. The input given to the layers will always be either regular numeric arrays or dlarrays. The outputs need to be likewise.
It is also not entirely clear why you don't use a functionLayer to implement this, rather than a custom layer. You don't appear to be providing a custom backward or forward method, so a custom layer doesn't seem necessary.

4 件のコメント

Nasrin
Nasrin 2025 年 3 月 1 日
Hi
I tried to use the functionLayer but still getting error. Below is how I defined the functionLayers:
layer1 = functionLayer(@(X) ((20*log10(X)+30)/30));
layer2 = functionLayer(@(X) abs(fftshift(fft2((X-mean(X,"all")),296,296))));
layer3 = functionLayer(@(X) (X-mean(X,"all"))/std(X,[],"all"));
Layers2 = [
imageInputLayer(inputSize)
functionLayer(@(X) abs(fftshift(fft2((X-mean(X,"all")),296,296))))
functionLayer(@(X) (X-mean(X,"all"))/std(X,[],"all"))
convolution2dLayer(7, 32, 'Padding', 'same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2, 'Stride', 2)
dropoutLayer(.3)
I'm also attaching the network diagram and scrnshot of the error I'm getting. I noticed it's saying 'fftn' instead of 'fft2' so can it be because of the 'imagedatastore' cell format that's causing the problem? How do I fix that?
Thanks for the help.
Matt J
Matt J 2025 年 3 月 2 日
fft2 is not defined when X is a dlarray. You can either define one,
function out=dlfft2(X,a,b)
out=fft(fft(X,a,1),b,2)
end
aor you can use trainNetwork, which doesn't use dlarrays, instead of trainnet.
Nasrin
Nasrin 2025 年 3 月 2 日
Yes, it worked. Thanks. But I'm still getting an error using 'trainnet' which I think is due to dimension mismatch during minibatch processing. Should I use a custom training loop? I'm attaching the error snippet here.
Matt J
Matt J 2025 年 3 月 2 日
編集済み: Matt J 2025 年 3 月 2 日
I doubt that error is coming from the fft code.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeImage Data Workflows についてさらに検索

質問済み:

2025 年 2 月 25 日

編集済み:

2025 年 3 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by