Matlab神经网络错误:输入1大小与net.inputs {1} .size不匹配 [英] Matlab neural network error: Input 1 size does not match net.inputs{1}.size

查看:2398
本文介绍了Matlab神经网络错误:输入1大小与net.inputs {1} .size不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个保存我的火车数据的90×50矩阵.每行都包含从数字输入图像中提取的特征(已读取90张图像-每位数字10张图像).前10行是从数字1的10张图像中提取的,后10行是从数字2的10张图像中提取的,依此类推,因此是size(dataset,1) = 90

I have a 90×50 matrix that holds my train data. every row holds features extracted from input images of digits (90 images have been read - 10 images for every digits). First 10 rows are extracted from 10 images of digit 1, the second 10 rows are extracted from 10 images digit 2, and so on, hence size(dataset,1) = 90

我的代码的神经网络部分如下所示:

Neural networks part of my code is shown below:

T=zeros(1,90);
for  i=1:90
    T(i)=ceil(i/10);
end
setdemorandstream(491218382);
net=fitnet(20);

[net,tr]=train(net,datasetNormalized',T);

datasetNormalized是我的数据集,以[0 1]间隔归一化. T是网络的目标. 我现在想要做的是获取一个数字的新图像,将其转换为1×50矢量(在这种情况下为m_normalized),并在我训练有素的网络的帮助下进行猜测.我使用了下面的代码,但确实会产生错误:

datasetNormalized is my dataset normalized in the [0 1] interval. T is target for network. What I want to do now is get a new image of a digit, turning it into a 1×50 vector(m_normalized in this case) and guess with the help of my trained network which digit it is. I used the code below but it does generate errors:

[a,b]=max(sim(net,m_normalized));
disp(b);
 msgbox(['digit is: ' num2str(b)],'Digit recognized','help');

错误消息如下:

Error using network/sim (line 130)
Input 1 size does not match net.inputs{1}.size.

Error in Neural (line 92)
[a,b]=max(sim(net,m_normalized));

您是否知道如何从脚本中获取输出,该脚本向我显示输入图像的数字? 顺便说一句,完整的脚本代码在这里供进一步参考:

Do you have any idea how I can get an output from the script that shows me which digit the input image was? By the way, the full script code is here for further reference:

clc
clear
close all

numOfPhotos = 90;
imgRows = 100;
imgCols = 50;
X = zeros(numOfPhotos, (imgRows * imgCols) / 100);

%% Resize Images
% myresize(imgRows,imgCols);

% read train images
datasetIndex = 0;    

for i = 1:numOfPhotos/10
    for j = 1:numOfPhotos/9           
        datasetIndex = datasetIndex+1;
        im = imread(['resized_train_numbers\' num2str(i) ' (' num2str(j) ').jpg']);
        im = im2bw(im, graythresh(im));    

        c = 1;
        for g = 1:imgRows/10
            for e = 1:imgCols/10
                s = sum(sum(im((g*10-9 : g*10),(e*10-9 : e*10))));
                X(datasetIndex, c) = s;
                c = c+1;            
            end    
        end
    end
end

datasetNormalized = zeros(numOfPhotos, imgRows*imgCols/100);
%% Normalize dataset contents
minDataset = min(min(X));
maxDataset = max(max(X));
for i = 1:numOfPhotos
    for j = 1:imgRows*imgCols/100
        datasetNormalized(i, j) = (X(i, j) - minDataset) / (maxDataset - minDataset);
    end
end

%%Neural network part

T = zeros(1, 90);
for  i = 1:90
    T(i) = ceil(i/10);
end

setdemorandstream(491218382);
net = fitnet(20);
[net, tr]=train(net, datasetNormalized', T);

% Read input image for recognition

newImg = imread('plate_1\1.jpg');
newImg = imresize(newImg, [imgRows imgCols]);
newImg = im2bw(newImg, graythresh(newImg));
scrsz = get(0, 'ScreenSize');
figure('Position', [1 1 scrsz(3)/3 scrsz(4)/2]),
imshow(newImg);

m = zeros(1, imgRows*imgCols/100);
c = 1;
for g = 1:imgRows/10
    for e = 1:imgCols/10
        s = sum(sum(newImg((g*10-9 : g*10), (e*10-9 : e*10))));
        m(c) = s;
        c = c+1;            
    end
end

%Normalize m contents
m_normalized = zeros(1, imgRows*imgCols/100);
for i = 1:imgRows*imgCols/100    
    m_normalized(i) = (m(i)-min(m)) / (max(m)-min(m));
end

[a,b] = max(sim(net, m_normalized));
disp(b);
msgbox(['digit is: ' num2str(b)], 'Digit recognized', 'help');

推荐答案

用于训练神经网络的输入的大小与用于训练后模拟网络的输入的大小必须匹配.在上述问题中,输入为50x90矩阵.每列代表一个数字.每列都有一个对应的输出.因此将模拟结果(输出)分配给一个变量(b),然后显示出来. 上面产生错误的代码是这样的:
b=sim(net,m_normalized);
由于m_normalized是输入,因此它必须匹配用于训练网络的列.假设它是其中一列.因此我们必须将其转置为50x1的向量,以匹配50x90形式的训练输入:
b=sim(net,m_normalized');
解决错误.

size of the inputs used for training neural networks and inputs used to simulate network after training have to match. In the question above, input was a 50x90 matrix. Every column representing a digit. There is a corresponding output for every column. so simulation result (output) is assigned into one variable(b) and then displayed. The code that generated error above was like this:
b=sim(net,m_normalized);
Since m_normalized is an input, it has to match the columns used to train the network. Let's say it is one of the columns. so we have to transpose it to make a 50x1 vector to match the 50x90 form of training inputs:
b=sim(net,m_normalized');
fixes the error.

这篇关于Matlab神经网络错误:输入1大小与net.inputs {1} .size不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆