% Complete evaluation of Neural network - takes a long time to run!
tic;

disp('Alphabet data loading from files...');
loadalphs

[c alph_ct] = size(alphs);
[c let_ct] = size(alphs{1});

% Parameters that may be varied
x_net_sz = 15;
y_net_sz = 12;
hidden_node_ct = ceil(8*sqrt(x_net_sz*y_net_sz));
sweeps = 500;

disp('Cleaning and normalizing alphabets...');
thrnormalall
alphs_scaled = change_size_all(alphs, x_net_sz, y_net_sz);

avgerrscore = 0;
fail_hist = zeros(1, let_ct);

for i=1:alph_ct
    fprintf('Training Neural Net with %d-by-%d scaled pictures...\n', x_net_sz, y_net_sz);
    net = MakeNNet({alphs_scaled{1:i-1} alphs_scaled{i+1:alph_ct}}, hidden_node_ct, sweeps);

    disp('Net training complete.');

    fprintf('Performance Evaluation on test alphabet %d (%s):\n', i, names{i});
    [fail err] = showNetPerformance(net, alphs_scaled, i);
    avgerrscore = avgerrscore + err;
    for j=1:let_ct
        k = strfind(fail,char('A'+j-1));
        fail_hist(j) = fail_hist(j) + length(k);
    end
end

avgerrscore = avgerrscore/alph_ct;
fprintf('SUMMARY PERFORMANCE:\nAvg. Letters Missed: %.2f\nAvg. Error Score: %.2f\n', ...
    mean(fail_hist), avgerrscore);

showall(alphs_scaled);

figure;
bar(fail_hist/alph_ct);
title('Neural Network Failure Analysis');
ylabel('Average Number of Failures on Letter');
xlabel('Letter');
set(gca,'XTick',1:let_ct);
set(gca,'XTickLabel',reshape('A':'Z', let_ct, 1));

time = toc;
fprintf('Evaluation took %d mins and %d seconds.\n', floor(time/60), floor(rem(time,60)));