Caffe accuracy bigger than 100% - neural-network

I'm building one but, and when I use the custom train function provided on lenet example with a batch size bigger than 110 my accuracy gets bigger than 1 (100%).
If I use batch size 32, I get 30 percent of accuracy. Batch size equal 64 my net accuracy is 64. And batch size equal to 128, the accuracy is 1.2.
My images are 32x32.
Train dataset: 56 images of Neutral faces. 60 images of Surprise faces. Test dataset: 15 images of Neutral faces. 15 images of Surprise faces.
This is my code:
def train(solver):
niter = 200
test_interval = 25
train_loss = zeros(niter)
test_acc = zeros(int(np.ceil(niter / test_interval)))
output = zeros((niter, 32, 2))
for it in range(niter):
solver.step(1)
train_loss[it] = solver.net.blobs['loss'].data
solver.test_nets[0].forward(start='conv1')
output[it] = solver.test_nets[0].blobs['ip2'].data[:32]
if it % test_interval == 0:
print 'Iteration', it, 'testing...'
correct = 0
for test_it in range(100):
solver.test_nets[0].forward()
correct += sum(solver.test_nets[0].blobs['ip2'].data.argmax(1) == solver.test_nets[0].blobs['label'].data)
test_acc[it // test_interval] = correct / 1e4
So, what is wrong with my code?

In your testing code you run 100 iterations (for test_it in range(100)), on each iteration you compute correct as number of examples in a batch that are correct. You then divide that number by 1e4.
Let's assume your model is very good and has almost 100% prediction rate. Then with batch size of 32 on each of 100 iterations you will add 32 to correct, yielding 3200. You then divide it by 1e4, ending up with 0.32, which is almost consistent with what you see (your number is slightly less because sometimes your model does mispredict the target).
To fix it, you can replace
test_acc[it // test_interval] = correct / 1e4
with
test_acc[it // test_interval] = correct / (100.0 * batch_size)

Related

fastest way to find the rgb pixel color count of image

I have a use case where i have to find the consecutive rgb pixel color count of each frame of live video after searching i found a piece of code which does the same thing but performance wise it take around ~ 3 sec to give me output but in my case i have to do this calculation as fast as possible may be 25 frames in 1 seconds. Can someone help me to figure out how to do this by refactoring the below code
from PIL import Image
import timeit
starttime = timeit.default_timer()
with Image.open("netflix.png") as image:
color_count = {}
width, height = image.size
print(width,height)
rgb_image = image.convert('RGB')
for x in range(width):
for y in range(height):
rgb = rgb_image.getpixel((x, y))
if rgb in color_count:
color_count[rgb] += 1
else:
color_count[rgb] = 1
print('Pixel Count per Unique Color:')
print('-' * 30)
print(len(color_count.items()))
print("The time difference is :", timeit.default_timer() - starttime)
output:
Pixel Count per Unique Color:
130869
The time difference is : 3.9660612
You need to use Numpy, or OpenCV, for fast image processing in Python. I made a 9-colour version of Paddington:
from PIL import Image
import numpy as np
# Open Paddington and make sure he is RGB - not palette
im = Image.open('paddington.png').convert('RGB')
# Make into Numpy array
na = np.array(im)
# Arrange all pixels into a tall column of 3 RGB values and find unique rows (colours)
colours, counts = np.unique(na.reshape(-1,3), axis=0, return_counts=1)
print(colours)
print(counts)
Results
[[ 14 48 84]
[ 19 21 30]
[ 33 108 163]
[ 33 152 190]
[ 72 58 58]
[ 96 154 210]
[180 89 64]
[205 210 200]
[208 151 99]]
[20389 40269 12820 1488 17185 25371 17050 16396 9032]
That means there are 20,389 pixels of RGB(14,48,84), and so on.
That takes 125ms on my Mac for a 400x400 image, which will give you 8 fps, so you better have at least 4 CPU cores and use all of them to get 25+ fps.
Update
I think you can actually go significantly faster than this. If you take the dot-product of each of the pixels with [1,256,65536], you will get a single 24-bit number for each pixel, rather than 3 8-bit numbers. It is then a lot faster to find the unique values. That looks like this:
# Open Paddington and make sure he is RGB - not palette
im = Image.open('paddington.png').convert('RGB')
# Make into Numpy array
na = np.array(im)
# Make a single 24-bit number for each pixel
f = np.dot(na.astype(np.uint32),[1,256,65536])
nColours = len(np.unique(f)) # prints 9
That takes 4ms rather than 125ms on my Mac :-)
Keywords: Python, Numpy, PIL/Pillow, image processing, count unique colours, count colors.

Calculating false positive rate for ANN in MATLAB (logic error)

My university lecturer has provided us with MATLAB code that encorporates the use of a ANN to predict and classify a number of images based on whether they have a logo present in their image or not.
I'm trying to make my life slightly easier by writing some extra code that quickly determines that false positive and false negative rate of the ANN's classification output.
I've written the code, however when I train and run the neural network the correct and incorrect classifications both come out at around ~30 each, out of 60. The first 30 are logo images, and the second 30 are non-logo images. This means the network has a roughly 50% success rate, which is pretty bad. I've also been through and tested each picture manually, and although there are some errors, there definitely isn't a 50:50 split on correct and incorrect classifications.
Considering the code for the ANN was written by my lecturer, I don't think its the ANN's output that's the problem, but rather my logic in determining the FNR and FPR values. However I've been staring at it for the last half an hour and I can't seem to figure out what the issue is. If someone could look over it and see if I have any logic errors and point them out I'd appreciate it. Sometimes a fresh pair of eyes helps.
for i = 1 : number_test_images
test_image=imread(['Test_logo\' test_folder(i+2).name]);
feature_vector=get_featureVector(test_image); % function get_featureVector returns a column vector.
Y_testing = net(feature_vector'); % compute the output of the trained network
if round(Y_testing)>=.5, %logo classification
disp('Logo');
if current_image <= 30 %first 30 images are logos = correct classification
correct = correct + 1;
else current_image > 30 %last 30 images are non-logos = incorrect classification
incorrect = incorrect + 1;
end
else
disp('Non logo'); %non-logo classification
if current_image <= 30 %first 30 images are logos = incorrect classification
incorrect = incorrect + 1;
else current_image > 30 %last 30 images are non-logos = correct classification
correct = correct + 1;
end
end
end
FPR = incorrect/30;
FNR = correct/30;
error = ((FPR+FNR)/2);
I think you have a problem with the if-else statement, with the current_image > 30 statement being executed not as part of the if-else evaluation.
It should be
if current_image <= 30 %first 30 images are logos = correct classification
correct = correct + 1;
elseif current_image > 30 %last 30 images are non-logos = incorrect classification
incorrect = incorrect + 1;
end

MATLAB is too slow calculation of Spearman's rank correlation for 9-element vectors

I need to calculate the Spearman's rank correlation (using corr function) for pairs of vectors with different lengths (for example 5-element vectors to 20-element vectors). The number of pairs is usually above 300 pairs for each length. I track the progress with waitbar. I have noticed that it takes unusually very long time for 9-element pair of vectors, where for other lengths (greater and smaller) it takes very short times. Since the formula is exactly the same, the problem must have originated in MATLAB function corr.
I wrote the following code to verify that the problem is with corr function and not other calculations that I have besides 'corr', where all of that calculations (including 'corr') take place inside some 2 or 3 'for' loops. The code repeats the timing 50 times to avoid accidental results.
The result is a bar graph, confirming that it takes a long time for MATLAB to calculate Spearman's rank correlation for 9-element vectors. Since my calculations are not that heavy, this problem does not cause endless wait, it just increases the total time consumed for the whole process. Can someone tell me that what causes the problem and how to avoid it?
Times1 = zeros(20,50);
for i = 5:20
for j = 1:50
tic
A = rand(i,2);
[r,p] = corr(A(:,1),A(:,2),'type','Spearman');
Times1(i,j) = toc;
end
end
Times2 = mean(Times1,2);
bar(Times2);
xticks(1:25);
xlabel('number of elements in vectors');
ylabel('average time');
After some investigation, I think I found the root of this very interesting problem. My tests have been conducted profiling every outer iteration using the built-in Matlab profiler, as follows:
res = cell(20,1);
for i = 5:20
profile clear;
profile on -history;
for j = 1:50
uni = rand(i,2);
corr(uni(:,1),uni(:,2),'type','Spearman');
end
profile off;
p = profile('info');
res{i} = p.FunctionTable;
end
The produced output looks like this:
The first thing I noticed is that the Spearman correlation for matrices with a number of rows less than or equal to 9 is computed in a different way than for matrices with 10 or more rows. For the former, the functions being internally called by the corr function are:
Function Number of Calls
----------------------- -----------------
'factorial' 100
'tiedrank>tr' 100
'tiedrank' 100
'corr>pvalSpearman' 50
'corr>rcumsum' 50
'perms>permsr' 50
'perms' 50
'corr>spearmanExactSub' 50
'corr>corrPearson' 50
'corr>corrSpearman' 50
'corr' 50
'parseArgs' 50
'parseArgs' 50
For the latter, the functions being internally called by the corr function are:
Function Number of Calls
----------------------- -----------------
'tiedrank>tr' 100
'tiedrank' 100
'corr>AS89' 50
'corr>pvalSpearman' 50
'corr>corrPearson' 50
'corr>corrSpearman' 50
'corr' 50
'parseArgs' 50
'parseArgs' 50
Since the computation of the Spearman correlation for matrices with 10 or more rows seems to run smoothly and quickly and doesn't show any evidence of performance bottlenecks, I decided to avoid losing time investigating on this fact and I focused on the main concern: the small matrices.
I tried to understand the difference between the execution time of the whole process for a matrix with 5 rows and for one with 9 rows (the one notably showing the worst performance). This is the code I used:
res5 = res{5,1};
res5_tt = [res5.TotalTime];
res5_tt_perc = ((res5_tt ./ sum(res5_tt)) .* 100).';
res9_tt = [res{9,1}.TotalTime];
res9_tt_perc = ((res9_tt ./ sum(res9_tt)) .* 100).';
res_diff = res9_tt_perc - res5_tt_perc;
[~,res_diff_sort] = sort(res_diff,'desc');
tab = [cellstr(char(res5.FunctionName)) num2cell([res5_tt_perc res9_tt_perc res_diff])];
tab = tab(res_diff_sort,:);
tab = cell2table(tab,'VariableNames',{'Function' 'TT_M5' 'TT_M9' 'DIFF'});
And here is the result:
Function TT_M5 TT_M9 DIFF
_______________________ _________________ __________________ __________________
'corr>spearmanExactSub' 7.14799963478685 16.2879721171023 9.1399724823154
'corr>pvalSpearman' 7.98185309750143 16.3043118970503 8.32245879954885
'perms>permsr' 3.47311716905926 8.73599255035966 5.26287538130039
'perms' 4.58132952553723 8.77488502392486 4.19355549838763
'corr>corrSpearman' 15.629476293326 16.440893059217 0.811416765890929
'corr>rcumsum' 0.510550019981949 0.0152486312660671 -0.495301388715882
'factorial' 0.669357868472376 0.0163923929871943 -0.652965475485182
'parseArgs' 1.54242684137027 0.0309456171268161 -1.51148122424345
'tiedrank>tr' 2.37642998160463 0.041010720272735 -2.3354192613319
'parseArgs' 2.4288171135289 0.0486075856244615 -2.38020952790444
'corr>corrPearson' 2.49766877262937 0.0484657591710417 -2.44920301345833
'tiedrank' 3.16762535118088 0.0543584195582888 -3.11326693162259
'corr' 21.8214856092549 16.5664346332513 -5.25505097600355
Once the bottleneck was detected, I started analyzing the internal code (open corr) and I finally found the cause of the problem. Within the spearmanExactSub, this part of code is being executed (where n is the number of rows of the matrix):
n = arg1;
nfact = factorial(n);
Dperm = sum((repmat(1:n,nfact,1) - perms(1:n)).^2, 2);
A permutation is being computed on a vector whose values range from 1 to n. This is what comes into play increasing the computational complexity (and, obviously, the computational time) of the function. Other operations, like the subsequent repmat on factorial(n) of 1:n and the ones below that point, contribute to worsen the situation. Now, long story short...
factorial(5) = 120
factorial(6) = 720
factorial(7) = 5040
factorial(8) = 40320
factorial(9) = 362880
can you see the reason why, between 5 and 9, your bar graph shows an "exponentially" increasing computational time?
On a side note, there is nothing you can do to solve this problem, unless you find another implementation of the Spearman correlation that doesn't present the same bottleneck or you implement your own.

How to setup fitensemble for binary imbalanced datasets?

I've been trying to test matlab's ensemble methods with randomly generated imbalance dataset and no matter what I set the prior/cost/weight parameters the method never predicts close to the label ratio.
Below is an example of the tests I did.
prob = 0.9; %set label ratio to 90% 1 and 10% 0
y = (rand(100,1) < prob);
X = rand(100,3); %generate random training data with three features
X_test = rand(100,3); %generate random test data
%A few parameter sets I've tested
B = TreeBagger(100,X,y);
B2 = TreeBagger(100,X,y,'Prior','Empirical');
B3 = TreeBagger(100,X,y,'Cost',[0,9;1,0]);
B4 = TreeBagger(100,X,y,'Cost',[0,1;9,0]);
B5 = fitensemble(X,y,'RUSBoost', 20, 'Tree', 'Prior', 'Empirical');
Here I tried to predict the trained classifiers on random test data. My assumption is that since the classifier is trained on random data, it should on average predict close to the dataset ratio (1/9) if it takes the prior into account. But each of the classifiers predicted 98-100% in favor of '1' instead of ~90% that I am looking for.
l1 = predict(B,X_test);
l2 = predict(B2,X_test);
l3 = predict(B3,X_test);
l4 = predict(B4,X_test);
l5 = predict(B5,X_test);
How do I get the ensemble method to take the prior into account? Or is there a fundamental misunderstanding on my part?
I don't think it can work like you think.
Thats because as i understood your training and test data is random. So how should your classifier find any relation between features and your label?
lets take the accuracy as a mesurement and make an example.
class A: 900 datarows.
class B: 100 datarows.
Classify 100% as A:
0.9*/(0.1+0.9) = 0.9
gets you 90% Accuracy.
if your classifier does something different, means trying to classify some datarows to B he will by chance get 9 times more wrongly classified A datarows
Lets say 20 B datarows are correctly classified you will get around 180 wrong a classified A datarows
B: 20 correct, 80 incorrect
A: 720 correct, 180 wrong
740/(740+260) = 0.74
Accuracy goes down to 74 %. And thats not something your classifying algorithms want.
Long story short: Your classifier will allways tend to classify allmost 100% class A if you dont get any information into your Data

What does a function n-mod(a,m) such as 8-mod(330,8) do on MATLAB?

I really can't figure out what it does.
More specifically, I am working on image compression on Matlab and I am provided a code that looks like this:
X=imread('image1.jpg');
s=size(X); % image1.jpg has size 330 x 220
offset1 = mod(8-mod(s(1),8),8);
offset2 = mod(8-mod(s(2),8),8);
if offset1 ~= 0 || offset2 ~= 0
X(s(1)+offset1, s(2)+offset2, 3) = 0;
end
figure(1)
image(X);
axis image
axis off
Trying to figure out what that if statement does, but I have no clue what that offset1 and offset2 is referring to.
They're trying to determine whether the image size is a multiple of 8. JPEG images are always multiples of 8 in size internally because they are made from 8x8 DCT blocks. The header can specify a smaller size, in which case only the specified upper-leftmost portion is visible, and the right and bottom are trimmed.
The part 8 - mod(s(1), 8) is computing how many more bytes it would take to get to the next multiple of 8 in the X size. The outer mod(..., 8) just folds the case of "8 more bytes" back into "0 more bytes".