error when using Cropping layer (Deep learning)

Illustration
Salma Ali - 2022-07-14T14:07:49+00:00
Question: error when using Cropping layer (Deep learning)

Hi, I have medical images and I have tried to implement this architecture described in(https://arxiv.org/pdf/1505.04597) but unfortunately I got this error message below, can you fix this problem please? I appreciate any help.     Error using trainNetwork (line 140) Layer 27 is expected to have a different size.,/pre> And this is the structure that I have implemented: layers = [ imageInputLayer([572 572 3],'Name','input') convolution2dLayer(3,64,'Padding',0,'Name','conv1_1') reluLayer('Name','relu1_1') convolution2dLayer(3,64,'Padding',0,'Name','conv1_2') reluLayer('Name','relu1_2') maxPooling2dLayer(2,'Stride',2,'Name','maxpool_1') convolution2dLayer(3,128,'Padding',0,'Name','conv2_1') reluLayer('Name','relu2_1') convolution2dLayer(3,128,'Padding',0,'Name','conv2_2') reluLayer('Name','relu2_2') maxPooling2dLayer(2,'Stride',2,'Name','maxpool_2') convolution2dLayer(3,256,'Padding',0,'Name','conv3_1') reluLayer('Name','relu3_1') convolution2dLayer(3,256,'Padding',0,'Name','conv3_2') reluLayer('Name','relu3_2') maxPooling2dLayer(2,'Stride',2,'Name','maxpool_3') convolution2dLayer(3,512,'Padding',0,'Name','conv4_1') reluLayer('Name','relu4_1') convolution2dLayer(3,512,'Padding',0,'Name','conv4_2') reluLayer('Name','relu4_2') maxPooling2dLayer(2,'Stride',2,'Name','maxpool_4') convolution2dLayer(3,1024,'Padding',0,'Name','conv5_1') reluLayer('Name','relu5_1') convolution2dLayer(3,1024,'Padding',0,'Name','conv5_2') reluLayer('Name','relu5_2') transposedConv2dLayer(4,1024,'Stride',2,'Cropping',1,'Name','upscore2') additionLayer(2,'Name','add1') convolution2dLayer(3,512,'Padding',0,'Name','conv6_1') reluLayer('Name','relu6_1') convolution2dLayer(3,512,'Padding',0,'Name','conv6_2') reluLayer('Name','relu6_2') transposedConv2dLayer(4,512,'Stride',2,'Cropping',1,'Name','upscore4') additionLayer(2,'Name','add2') convolution2dLayer(3,256,'Padding',0,'Name','conv7_1') reluLayer('Name','relu7_1') convolution2dLayer(3,256,'Padding',0,'Name','conv7_2') reluLayer('Name','relu7_2') transposedConv2dLayer(4,256,'Stride',2,'Cropping',1,'Name','upscore8') additionLayer(2,'Name','add3') convolution2dLayer(3,128,'Padding',0,'Name','conv8_1') reluLayer('Name','relu8_1') convolution2dLayer(3,128,'Padding',0,'Name','conv8_2') reluLayer('Name','relu8_2') transposedConv2dLayer(4,128,'Stride',2,'Cropping',1,'Name','upscore10') additionLayer(2,'Name','add4') convolution2dLayer(3,64,'Padding',0,'Name','conv9_1') reluLayer('Name','relu9_1') convolution2dLayer(3,64,'Padding',0,'Name','conv9_2') reluLayer('Name','relu9_2') convolution2dLayer(1,5,'Name','conv10') softmaxLayer('Name','softmax') classificationLayer('Name','classOutput') ]; lgraph = layerGraph(layers); score_pool4c=crop2dLayer('centercrop','Name','score_pool4c') ; lgraph = addLayers(lgraph,score_pool4c); lgraph = connectLayers(lgraph,'relu4_2','score_pool4c/in'); lgraph = connectLayers(lgraph,'upscore2','score_pool4c/ref'); lgraph = connectLayers(lgraph,'score_pool4c','add1/in2'); score_pool3c=crop2dLayer('centercrop','Name','score_pool3c') ; lgraph = addLayers(lgraph,score_pool3c); lgraph = connectLayers(lgraph,'relu3_2','score_pool3c/in'); lgraph = connectLayers(lgraph,'upscore4','score_pool3c/ref'); lgraph = connectLayers(lgraph,'score_pool3c','add2/in2'); score_pool2c=crop2dLayer('centercrop','Name','score_pool2c') ; lgraph = addLayers(lgraph,score_pool2c); lgraph = connectLayers(lgraph,'relu2_2','score_pool2c/in'); lgraph = connectLayers(lgraph,'upscore8','score_pool2c/ref'); lgraph = connectLayers(lgraph,'score_pool2c','add3/in2'); score_pool1c=crop2dLayer('centercrop','Name','score_pool1c') ; lgraph = addLayers(lgraph,score_pool1c); lgraph = connectLayers(lgraph,'relu1_2','score_pool1c/in'); lgraph = connectLayers(lgraph,'upscore10','score_pool1c/ref'); lgraph = connectLayers(lgraph,'score_pool1c','add4/in2'); figure plot(lgraph);  

Expert Answer

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-11-20

layers = [ ... imageInputLayer([572 572 1],'Name','input') convolution2dLayer([3 3],64,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_d0a-b') reluLayer('Name','relu_d0b') convolution2dLayer([3 3],64,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_d0b-c') reluLayer('Name','relu_d0c') maxPooling2dLayer([2 2],'Stride',[2 2],'Name','pool_d0c-1a')

 

 

    convolution2dLayer([3 3],128,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_d1a-b')
    reluLayer('Name','relu_d1b')
    convolution2dLayer([3 3],128,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_d1b-c')
    reluLayer('Name','relu_d1c')
    maxPooling2dLayer([2 2],'Stride',[2 2],'Name','pool_d1c-2a')     

    convolution2dLayer([3 3],256,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_d2a-b')
    reluLayer('Name','relu_d2b')
    convolution2dLayer([3 3],256,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_d2b-c')
    reluLayer('Name','relu_d2c')
    maxPooling2dLayer([2 2],'Stride',[2 2],'Name','pool_d2c-3a') 

    convolution2dLayer([3 3],512,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_d3a-b')
    reluLayer('Name','relu_d3b')
    convolution2dLayer([3 3],512,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_d3b-c')
    reluLayer('Name','relu_d3c')
    dropoutLayer(0.5,'Name','dropout_d3c') 
    maxPooling2dLayer([2 2],'Stride',[2 2],'Name','pool_d3c-4a')    
                                               %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    convolution2dLayer([3 3],1024,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_d4a-b')
    reluLayer('Name','relu_d4b')
    convolution2dLayer([3 3],1024,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_d4b-c')
    reluLayer('Name','relu_d4c')
    dropoutLayer(0.5,'Name','dropout_d4c')     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    transposedConv2dLayer(2,512,'Stride',[2 2],'Cropping',[0 0],'Name','upconv_d4c_u3a');
    reluLayer('Name','relu_u3a')
    additionLayer(2,'Name','concat_d3cc_u3a-b') %%%--------------------------------------------------
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     convolution2dLayer([3 3],512,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_u3b-c')
    reluLayer('Name','relu_u3c')
    convolution2dLayer([3 3],512,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_u3c-d')
    reluLayer('Name','relu_u3d')

    transposedConv2dLayer(2,256,'Stride',[2 2],'Cropping',[0 0],'Name','upconv_u3d_u2a');
    reluLayer('Name','relu_u2a')   
    additionLayer(2,'Name','concat_d2cc_u2a-b') %%%--------------------------------------------------
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    convolution2dLayer([3 3],256,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_u2b-c')
    reluLayer('Name','relu_u2c')
    convolution2dLayer([3 3],256,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_u2c-d')
    reluLayer('Name','relu_u2d')

    transposedConv2dLayer(2,128,'Stride',[2 2],'Cropping',[0 0],'Name','upconv_u2d_u1a');
    reluLayer('Name','relu_u1a')  
    additionLayer(2,'Name','concat_d1cc_u1a-b') %%%--------------------------------------------------
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    convolution2dLayer([3 3],128,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_u1b-c')
    reluLayer('Name','relu_u1c')
    convolution2dLayer([3 3],128,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_u1c-d')
    reluLayer('Name','relu_u1d')

    transposedConv2dLayer(2,64,'Stride',[2 2],'Cropping',[0 0],'Name','upconv_u1d_u0a');
    reluLayer('Name','relu_u0a')        
    additionLayer(2,'Name','concat_d0cc_u0a-b') %%%--------------------------------------------------
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    convolution2dLayer([3 3],64,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_u0b-c')
    reluLayer('Name','relu_u0c')
    convolution2dLayer([3 3],64,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_u0c-d')
    reluLayer('Name','relu_u0d')
    convolution2dLayer([1 1],2,'Padding',[0 0 0 0],'Stride',[1 1],'Name','conv_u0d-score')
    softmaxLayer('Name','soft_max')
    pixelClassificationLayer('Name','class')];
lgraph = layerGraph(layers);
Crop_1 = crop2dLayer('centercrop','Name','Crop_1'); %%% 568---392 568-392 /2=88 89-480 lgraph = addLayers(lgraph,Crop_1); Crop_2 = crop2dLayer('centercrop','Name','Crop_2'); %%%%%% 280--200 80/2=40 41:240 lgraph = addLayers(lgraph,Crop_2); Crop_3 = crop2dLayer('centercrop','Name','Crop_3'); %%%136-104 32/2=16 17:120 lgraph = addLayers(lgraph,Crop_3); Crop_4 = crop2dLayer('centercrop','Name','Crop_4'); %%%64 56 8/2=4 5:60 lgraph = addLayers(lgraph,Crop_4);
lgraph = connectLayers(lgraph,'relu_d0c','Crop_1/in'); lgraph = connectLayers(lgraph,'relu_d1c','Crop_2/in'); lgraph = connectLayers(lgraph,'relu_d2c','Crop_3/in'); lgraph = connectLayers(lgraph,'dropout_d3c','Crop_4/in');
lgraph = connectLayers(lgraph,'relu_u0a','Crop_1/ref'); lgraph = connectLayers(lgraph,'relu_u1a','Crop_2/ref'); lgraph = connectLayers(lgraph,'relu_u2a','Crop_3/ref'); lgraph = connectLayers(lgraph,'relu_u3a','Crop_4/ref');
lgraph = connectLayers(lgraph,'Crop_1','concat_d0cc_u0a-b/in2'); lgraph = connectLayers(lgraph,'Crop_2','concat_d1cc_u1a-b/in2'); lgraph = connectLayers(lgraph,'Crop_3','concat_d2cc_u2a-b/in2'); lgraph = connectLayers(lgraph,'Crop_4','concat_d3cc_u3a-b/in2');


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!