Can anyone help me in reshaping a fully connected layer output to a image?
    7 次查看(过去 30 天)
  
       显示 更早的评论
    
I am trying to make a deep layer network in which I want to connect my pretrained convolutional layer at the intermediate step? I am unable to write a custom layer for it. I am sharing the code for the custom layer, please le me know where am I going wrong in it.
classdef fullytandem < nnet.layer.Layer
    properties
        % (Optional) Layer properties
        % Layer properties go here
    end
    properties (Learnable)
        % (Optional) Layer learnable parameters
        % Layer learnable parameters go here
        W;
        b;
        Z;
    end
    methods
        function layer = fullytandem(input_shape, output_shape, im_layer, name)
            % (Optional) Create a myLayer
            % This function must have the same name as the layer
            % Layer constructor function goes here
            layer.Name = name;
            layer.Description = 'fully self';
            layer.b = rand([im_layer,1]);
            layer.W = rand([im_layer,input_shape]);
        end
        function Z = predict(layer, X)
            load('net_28s11.mat')
            % Forward input data through the layer at prediction time and
            % output the result
            %
            % Inputs:
            %         layer    -    Layer to forward propagate through
            %         X        -    Input data
            % Output:
            %         Z        -    Output of layer forward function
            % Layer forward function for prediction goes here
            disp('W')
            size(layer.W)
            disp('X')
            size(X)
            weights = layer.W;
            bias = layer.b;
            Y = fullyconnect(X,weights,bias,'DataFormat','SSCB');
%             Z = layer.W*X + layer.b';
%             disp(size(Z))
%             outputSize = X.OutputSize;
            disp('Y')
            size(Y)
            Z = reshape(Y, [4,7,1]);
            disp('Z')
            size(Z)
%             Z = predict(net,dlarray(Z));
        end
% 
    end
end
0 个评论
采纳的回答
  Srivardhan Gadila
    
 2020-11-2
        Instead of writing the code for fullyconnected layer you can make use of the existing fullyConnectedLayer & write the custom layer code only for the reshape operation as follows:
layers = [ ...
    imageInputLayer([100 1 1],'Name','input','Normalization','none')
    fullyConnectedLayer(100, 'Name','fc')
    reshapeLayer("reshape")
    convolution2dLayer(5,20,'Name','conv')];
dlnet = dlnetwork(layerGraph(layers));
analyzeNetwork(dlnet)
Custom layer code for reshape operation:
classdef reshapeLayer < nnet.layer.Layer
    properties
    end
    properties (Learnable)
    end
    methods
        function layer = reshapeLayer(name)
            layer.Name = name;
        end
        function [Z] = predict(layer, X)
            Z = reshape(X,10,10,1,[]);
        end
    end
end
5 个评论
  Atallah Baydoun
 2022-3-2
				I have implemented this reshape function and I am trying to reshape from 512 to 8 x 8 x 8. 
I have changed the reshape settings to Z = reshape(X,8,8,8);
For some reason, the output in the network is 8.
Any idea why this is happening?
Attached the analyzeNetwork

  Atallah Baydoun
 2022-3-2
				Just found the answer.
You have to add a line to the function as follows:
function [Z] = predict(layer, X)
            Z = reshape(X, 8, 8, 8, []);
            Z = dlarray(Z,'SSSCB');
更多回答(0 个)
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Deep Learning Toolbox 的更多信息
			
	产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




