3

摘要: 我正在尝试根据身体部位之间的角度对一些图像进行分类。

我假设人体由 10 个部分组成(作为矩形)并找到每个部分的中心并通过参考躯干计算每个部分的角度。我有三个动作类别:手波-步行-跑步。我的目标是找出哪些测试图像属于哪个动作类别。

事实: TrainSet:1057x10 特征集,1057 代表图像数量。测试集:821x10

我希望我的输出是 3x1 矩阵,每行显示动作类别的分类百分比。第 1 行:手挥 第 2 行:步行 第 3 行:跑步

代码:

actionCat='H';
[train_data_hw train_label_hw] = tugrul_traindata(TrainData,actionCat);
[test_data_hw test_label_hw] = tugrul_testdata(TestData,actionCat);


actionCat='W';
[train_data_w train_label_w] = tugrul_traindata(TrainData,actionCat);
[test_data_w test_label_w] = tugrul_testdata(TestData,actionCat);

actionCat='R';
[train_data_r train_label_r] = tugrul_traindata(TrainData,actionCat);
[test_data_r test_label_r] = tugrul_testdata(TestData,actionCat);

Train=[train_data_hw;train_data_w;train_data_r];
Test=[test_data_hw;test_data_w;test_data_r];

Target=eye(3,1);
net=newff(minmax(Train),[10 3],{'logsig' 'logsig'},'trainscg');
net.trainParam.perf='sse';
net.trainParam.epochs=50;
net.trainParam.goal=1e-5;
net=train(net,Train);

trainSize=size(Train,1);
testSize=size(Test,1);

if(trainSize > testSize)
pend=-1*ones(trainSize-testSize,size(Test,2));
Test=[Test;pend];
end


x=sim(net,Test);

问题: 我正在使用 Matlab newff 方法。但我的输出始终是 Nx10 矩阵而不是 3x1。我的输入集应分为 3 个类,但它们分为 10 个类。

谢谢

4

1 回答 1

3
%% Load data : I generated some random data instead
Train = rand(1057,10);
Test = rand(821,10);
TrainLabels = randi([1 3], [1057 1]);
TestLabels = randi([1 3], [821 1]);

trainSize = size(Train,1);
testSize = size(Test,1);

%% prepare the input/output vectors (1-of-N output encoding)
input = Train';               %'matrix of size numFeatures-by-numImages
output = zeros(3,trainSize);  % matrix of size numCategories-by-numImages
for i=1:trainSize
    output(TrainLabels(i), i) = 1;
end

%% create net: one hidden layer with 10 nodes (output layer size is infered: 3)
net = newff(input, output, 10, {'logsig' 'logsig'}, 'trainscg');
net.trainParam.perf = 'sse';
net.trainParam.epochs = 50;
net.trainParam.goal = 1e-5;
view(net)

%% training
net = init(net);                            % initialize
[net,tr] = train(net, input, output);       % train

%% performance (on Training data)
y = sim(net, input);                        % predict
%[err cm ind per] = confusion(output, y);

[maxVals predicted] = max(y);               % predicted
cm = confusionmat(predicted, TrainLabels);
acc = sum(diag(cm))/sum(cm(:));
fprintf('Accuracy = %.2f%%\n', 100*acc);
fprintf('Confusion Matrix:\n');
disp(cm)

%% Testing (on Test data)
y = sim(net, Test');

请注意我如何从每个实例的类别标签转换为(1/2/3)1 对 N 编码向量([100]: 1, [010]: 2, [001]: 3)

另请注意,当前未使用测试集,因为默认情况下,输入数据分为训练/测试/验证。您可以通过设置divideind函数并设置相应的参数net.divideFcn来实现您的手动划分。net.divideParam

我在相同的训练数据上展示了测试,但你可以对测试数据做同样的事情。

于 2009-11-04T11:42:48.287 回答