nmr sequences contain noise wavelet coefficient as a cnn input and the corresponding clean nmr sequences wavelet cofficient as the output to train the cnn in matlab

To train a CNN to denoise NMR sequences using wavelet coefficients in Matlab, you can follow these steps:

  1. Load the noisy and clean NMR data:
main.m
noisy_data = load('noisy_nmr_data.mat');
clean_data = load('clean_nmr_data.mat');
82 chars
3 lines
  1. Compute the wavelet coefficients for both the noisy and clean NMR data using the wavelet transform:
main.m
[noisy_coeffs,~] = wavedec(noisy_data,levels,wav_name); % levels and wav_name are additional parameters to be defined
[clean_coeffs,~] = wavedec(clean_data,levels,wav_name);
174 chars
3 lines
  1. Extract patches of the wavelet coefficients and the corresponding clean wavelet coefficients:
main.m
patch_size = [H,W,D]; % height, width and depth of a patch
stride = [h,w,d]; % stride for patch extraction
noisy_patches = im2col(noisy_coeffs, patch_size, stride); % reshape the coefficients into patches
clean_patches = im2col(clean_coeffs, patch_size, stride);
263 chars
5 lines
  1. Reshape the patches to be compatible with a CNN:
main.m
noisy_patches = reshape(noisy_patches, [patch_size(1), patch_size(2), patch_size(3), size(noisy_patches, 2)]);
clean_patches = reshape(clean_patches, [patch_size(1), patch_size(2), patch_size(3), size(clean_patches, 2)]);
222 chars
3 lines
  1. Define the CNN architecture for denoising:
main.m
layers = [
          imageInputLayer([H W D])
          convolution2dLayer(filterSize,numFilters,'Padding',pad,'Name','conv1')
          reluLayer('Name','relu1')
          convolution2dLayer(filterSize,numFilters,'Padding',pad,'Name','conv2')
          reluLayer('Name','relu2')
          convolution2dLayer(filterSize,numFilters,'Padding',pad,'Name','conv3')
          reluLayer('Name','relu3')
          convolution2dLayer(filterSize,numFilters,'Padding',pad,'Name','conv4')
          reluLayer('Name','relu4')
          convolution2dLayer(filterSize,numFilters,'Padding',pad,'Name','conv5')
          reluLayer('Name','relu5')
          convolution2dLayer(filterSize,numFilters,'Padding',pad,'Name','conv6')
          reluLayer('Name','relu6')
          convolution2dLayer(filterSize,numFilters,'Padding',pad,'Name','conv7')
          reluLayer('Name','relu7')
          convolution2dLayer(filterSize,numFilters,'Padding',pad,'Name','conv8')
          reluLayer('Name','relu8')
          convolution2dLayer(filterSize,numFilters,'Padding',pad,'Name','conv9')
          reluLayer('Name','relu9')
          convolution2dLayer(filterSize,numFilters,'Padding',pad,'Name','conv10')
          reluLayer('Name','relu10')
          convolution2dLayer(filterSize,numFilters,'Padding',pad,'Name','conv11')
          reluLayer('Name','relu11')
          convolution2dLayer(filterSize,numFilters,'Padding',pad,'Name','conv12')
          reluLayer('Name','relu12')
          convolution2dLayer(filterSize,numFilters,'Padding',pad,'Name','conv13')
          reluLayer('Name','relu13')
          convolution2dLayer(filterSize,numFilters,'Padding',pad,'Name','conv14')
          reluLayer('Name','relu14')
          convolution2dLayer(filterSize,D,'Padding',pad,'Name','conv15')
          regressionLayer('Name','output')
          ];
1823 chars
34 lines
  1. Specify the training options and train the CNN:
main.m
options = trainingOptions('adam', ...
    'MaxEpochs',10, ...
    'MiniBatchSize',64, ...
    'Shuffle','every-epoch', ...
    'InitialLearnRate',1e-3, ...
    'LearnRateSchedule','piecewise', ...
    'LearnRateDropFactor',0.1, ...
    'LearnRateDropPeriod',5, ...
    'L2Regularization',0.01, ...
    'Plots','training-progress');

net = trainNetwork(noisy_patches, clean_patches, layers, options);
400 chars
13 lines
  1. Use the trained CNN to denoise new NMR data:
main.m
% load a new noisy NMR sequence
noisy_data = load('new_noisy_nmr_data.mat');

% compute the wavelet coefficients
[noisy_coeffs,~] = wavedec(noisy_data,levels,wav_name);

% extract patches of the wavelet coefficients
noisy_patches = im2col(noisy_coeffs, patch_size, stride);

% reshape the patches to be compatible with the CNN
noisy_patches = reshape(noisy_patches, [patch_size(1), patch_size(2), patch_size(3), size(noisy_patches, 2)]);

% denoise the wavelet coefficients using the trained CNN
denoised_patches = predict(net, noisy_patches);

% reshape the denoised patches back to their original shape
denoised_coeffs = col2im(denoised_patches, patch_size, size(noisy_coeffs), stride);

% compute the inverse wavelet transform to obtain the denoised NMR sequence
denoised_data = waverec(denoised_coeffs, wav_name);
818 chars
21 lines

gistlibby LogSnag