Skip to content

Commit

Permalink
updated fMRI denoiser model and moved fMRI example to other (as it is…
Browse files Browse the repository at this point in the history
… not in the same state as the ephys and ophys)
  • Loading branch information
tkuenzmw committed Dec 18, 2024
1 parent 655c14a commit 326c0a5
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 33 deletions.
82 changes: 50 additions & 32 deletions +deepinterp/+internal/fmriDenoiserNetwork.m
Original file line number Diff line number Diff line change
@@ -1,65 +1,83 @@
function net = fmriDenoiserNetwork(modelPath, pretrainedNetwork)
%fmriDenoiserNetwork - generate MATLAB Version of the DeepInterpolation fMRI-Denoiser
function net = fmriDenoiserNetwork(preN)
%FMRIDENOISERNETWORK - MATLAB Version of the DeepInterpolation fMRI-Denoiser

lgraph = layerGraph();
resize = true;

tempLayers = [
image3dInputLayer([7 7 7 5],"Name","image3dinput",'Normalization','none');
convolution3dLayer([3 3 3],8,"Name","conv3d","Padding","same","Weights",mywini(1),"Bias",mybini(1));
reluLayer("Name","relu1")];
lgraph = addLayers(lgraph,tempLayers);
lgraph = layerGraph();
poolsize = [2 2 2];
poolargs={"Stride",poolsize};
convargs={"Padding","same"};
resizeLayers1 = [
resize3dLayer("Scale",2,"Name","up1");
functionLayer(@zeropad,"Name","pad1",Formattable=true,Acceleratable=true)];
resizeLayers2 = [
resize3dLayer("Scale",2,"Name","up2")
functionLayer(@zeropad,"Name","pad2",Formattable=true,Acceleratable=true)];
cc1="pad1"; cc2="pad2";

tempLayers = [
maxPooling3dLayer([3 3 3],"Name","pool1","Padding","same")
convolution3dLayer([3 3 3],16,"Name","conv3d_1","Padding","same","Weights",mywini(2),"Bias",mybini(2))
reluLayer("Name","relu2")];
lgraph = addLayers(lgraph,tempLayers);
if ~resize
poolargs={"Padding","same"};
resizeLayers1=[]; resizeLayers2=[];
cc1="relu3"; cc2="relu4";
end

tempLayers = [
maxPooling3dLayer([3 3 3],"Name","pool2","Padding","same")
convolution3dLayer([3 3 3],32,"Name","conv3d_2","Padding","same","Weights",mywini(3),"Bias",mybini(3))
reluLayer("Name","relu3")];
image3dInputLayer([7 7 7 5],"Name","image3dinput","Normalization","none")
convolution3dLayer([3 3 3],8,"Name","conv1",convargs{:},"Weights",mywini(1,preN),"Bias",mybini(1,preN))
reluLayer("Name","relu1")
maxPooling3dLayer(poolsize,"Name","pool1",poolargs{:})
convolution3dLayer([3 3 3],16,"Name","conv2",convargs{:},"Weights",mywini(2,preN),"Bias",mybini(2,preN))
reluLayer("Name","relu2")
maxPooling3dLayer(poolsize,"Name","pool2",poolargs{:})
convolution3dLayer([3 3 3],32,"Name","conv3",convargs{:},"Weights",mywini(3,preN),"Bias",mybini(3,preN))
reluLayer("Name","relu3")
resizeLayers1];
lgraph = addLayers(lgraph,tempLayers);

tempLayers = [
depthConcatenationLayer(2,"Name","conc_up_1")
convolution3dLayer([3 3 3],16,"Name","conv3d_3","Padding","same","Weights",mywini(4),"Bias",mybini(4))
reluLayer("Name","relu4")];
convolution3dLayer([3 3 3],16,"Name","conv4",convargs{:},"Weights",mywini(4,preN),"Bias",mybini(4,preN))
reluLayer("Name","relu4")
resizeLayers2];
lgraph = addLayers(lgraph,tempLayers);
lgraph = connectLayers(lgraph,cc1,"conc_up_1/in1");
lgraph = connectLayers(lgraph,"relu2","conc_up_1/in2");

tempLayers = [
depthConcatenationLayer(2,"Name","conc_up_2")
convolution3dLayer([3 3 3],8,"Name","conv3d_4","Padding","same","Weights",mywini(5),"Bias",mybini(5))
convolution3dLayer([3 3 3],8,"Name","conv5",convargs{:},"Weights",mywini(5,preN),"Bias",mybini(5,preN))
reluLayer("Name","relu5")
convolution3dLayer([1 1 1],1,"Name","conv3d_5","Padding","same","Weights",mywini(6),"Bias",mybini(6))
convolution3dLayer([1 1 1],1,"Name","out",convargs{:},"Weights",mywini(6,preN),"Bias",mybini(6,preN))
regressionLayer("Name","out_r")];
lgraph = addLayers(lgraph,tempLayers);
lgraph = connectLayers(lgraph,cc2,"conc_up_2/in1");
lgraph = connectLayers(lgraph,"relu1","conc_up_2/in2");

clear tempLayers;

lgraph = connectLayers(lgraph,"relu1","pool1");
lgraph = connectLayers(lgraph,"relu1","conc_up_2/in2");
lgraph = connectLayers(lgraph,"relu2","pool2");
lgraph = connectLayers(lgraph,"relu2","conc_up_1/in2");
lgraph = connectLayers(lgraph,"relu3","conc_up_1/in1");
lgraph = connectLayers(lgraph,"relu4","conc_up_2/in1");

net = assembleNetwork(lgraph);
save(modelPath,"net")
end


%% Local Functions

function w = mywini(ilayer)
function w = mywini(ilayer, preN)
lwlnames = {'conv3d','conv3d_1','conv3d_2','conv3d_3','conv3d_4','conv3d_5'}; %layers_with_learnables
lname = lwlnames{ilayer};
thisweights = h5read(pretrainedNetwork,strcat('/model_weights/',lname,'/',lname,'/kernel:0'));
thisweights = h5read(preN,strcat('/model_weights/',lname,'/',lname,'/kernel:0'));
w = permute(thisweights,[5,4,3,2,1]);
end

function b = mybini(ilayer)
function b = mybini(ilayer, preN)
lwlnames = {'conv3d','conv3d_1','conv3d_2','conv3d_3','conv3d_4','conv3d_5'}; %layers_with_learnables
lwldims = [8 16 32 16 8 1];
lname = lwlnames{ilayer};
b = h5read(pretrainedNetwork,strcat('/model_weights/',lname,'/',lname,'/bias:0'));
b = h5read(preN,strcat('/model_weights/',lname,'/',lname,'/bias:0'));
b = reshape(b,[1 1 1 lwldims(ilayer)]);
end

function out = zeropad(in)
m = extractdata(in);
n = padarray(m,[1 1 1],0,"post");
out = dlarray(n);
end
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Expected use cases will involve larger data sizes, for which remote data locatio
| Data Type | Pretrained Model | Public Data Archive | View :eyes: | Run ▶️
|---|---|---|---|---|
|🔬"Ophys" (optical physiology) | [model](pretrainedModels/pretrained.json) | [Allen Brain Observatory](http://allen-brain-observatory.s3.amazonaws.com/visual-coding-2p/ophys_movies/ophys_experiment_496908818.h5) (55.6 GB) | [![View Deep-Interpolation-MATLAB on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://viewer.mathworks.com/?viewer=live_code&url=https%3A%2F%2Fwww.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2F84c22101-bffc-435a-910c-b0c7dcd5b386%2F29e7e92d-4639-4178-8e19-739580981e60%2Ffiles%2Fexamples%2Fophys_AllenBrainObservatory.mlx&embed=web) | (\*) |
|🧠 fMRI (functional magnetic resonance imaging) | [TODO](pretrainedModels/pretrained.json) (AWS)| [Open Neuro](https://openneuro.org/datasets/ds001246/versions/1.2.1) (18.3 GB)| [![View Deep-Interpolation-MATLAB on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://viewer.mathworks.com/?viewer=live_code&url=https%3A%2F%2Fwww.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2F84c22101-bffc-435a-910c-b0c7dcd5b386%2F29e7e92d-4639-4178-8e19-739580981e60%2Ffiles%2Fexamples%2Ftiny_fMRI_inference.mlx&embed=web) | [![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=INCF/DeepInterpolation-MATLAB&file=examples/fMRI_OpenNeuro.mlx)
|🧠 fMRI (functional magnetic resonance imaging) | [TODO](pretrainedModels/pretrained.json) (AWS)| [Open Neuro](https://openneuro.org/datasets/ds001246/versions/1.2.1) (18.3 GB)| [![View Deep-Interpolation-MATLAB on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://viewer.mathworks.com/?viewer=live_code&url=https%3A%2F%2Fwww.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2F84c22101-bffc-435a-910c-b0c7dcd5b386%2F29e7e92d-4639-4178-8e19-739580981e60%2Ffiles%2Fexamples%2Ftiny_fMRI_inference.mlx&embed=web) | [![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=INCF/DeepInterpolation-MATLAB&file=examples/other/fMRI_OpenNeuro.mlx)

<sub>(\*) This data-intensive example is recommended for use on a local machine, not for MATLAB online.</sub>

Expand Down
Binary file removed examples/fMRI_OpenNeuro.mlx
Binary file not shown.
Binary file added examples/other/fMRI_OpenNeuro.mlx
Binary file not shown.
Binary file modified gettingStarted.mlx
Binary file not shown.
Binary file not shown.
Binary file modified pretrainedModels/fmri_pretrained_matlab_compatible.mat
Binary file not shown.

0 comments on commit 326c0a5

Please sign in to comment.