简体   繁体   English

以下 MATLAB 代码中单精度格式的应用是什么?

[英]What is the application of single-precision format in the following MATLAB code?

I am imitating the following MATLAB code.我正在模仿以下 MATLAB 代码。 actually, this code implement the eigenface method for face recognition.实际上,这段代码实现了人脸识别的 eigenface 方法。

%You are free to use, modify or distribute this code
loaded_Image=load_img();
random_Index=round(400*rand(1,1));          
random_Image=loaded_Image(:,random_Index);                         
rest_of_the_images=loaded_Image(:,[1:random_Index-1 random_Index+1:end]);         
image_Signature=20;                            
white_Image=uint8(ones(1,size(rest_of_the_images,2)));
mean_value=uint8(mean(rest_of_the_images,2));                
mean_Removed=rest_of_the_images-uint8(single(mean_value)*single(white_Image)); 
L=single(mean_Removed)'*single(mean_Removed);
[V,D]=eig(L);
V=single(mean_Removed)*V;
V=V(:,end:-1:end-(image_Signature-1));          
all_image_Signatire=zeros(size(rest_of_the_images,2),image_Signature);
for i=1:size(rest_of_the_images,2);
    all_image_Signatire(i,:)=single(mean_Removed(:,i))'*V;  
end
subplot(121);
imshow(reshape(random_Image,112,92));
title('Looking for this Face','FontWeight','bold','Fontsize',16,'color','red');
subplot(122);
p=random_Image-mean_value;
s=single(p)'*V;
z=[];
for i=1:size(rest_of_the_images,2)
    z=[z,norm(all_image_Signatire(i,:)-s,2)];
    if(rem(i,20)==0),imshow(reshape(rest_of_the_images(:,i),112,92)),end;
    drawnow;
end
[a,i]=min(z);
subplot(122);
imshow(reshape(rest_of_the_images(:,i),112,92));
title('Recognition Completed','FontWeight','bold','Fontsize',16,'color','red');

Do you know why it uses single-precision format?你知道为什么它使用单精度格式吗? what is the applications of this format?这种格式的应用是什么? as far as I know, the the difference between single-precision format and double-precision format is the number of bits used for exponential, mantissa and etc.据我所知,单精度格式和双精度格式的区别在于用于指数、尾数等的位数。

The loaded_Image , rest_of_the_images , random_Image , etc. are all stored as type uint8 for saving space because pixel values range from [0 0 0] (black) to [255 255 255] (white) and 8-bits are sufficient for that (0:255). loaded_Imagerest_of_the_imagesrandom_Image等都存储为uint8类型以节省空间,因为像素值的范围从[0 0 0] (黑色)到[255 255 255] (白色)并且 8 位就足够了(0 :255)。 But some mathematical operations are not supported for integer classes.但是整数类不支持某些数学运算。 For example, if I try to multiply two uint8 matrices I get this error:例如,如果我尝试将两个uint8矩阵相乘,则会出现以下错误:

>> uint8(ones(5)) * uint8(ones(5))
Error using  * 
MTIMES (*) is not fully supported for integer classes. 
At least one argument must be scalar.

Same for eig :eig相同:

>> eig(uint8(ones(5)))
Error using eig
Invalid data type. Input matrix must be double or single.

So, one has to convert to a supported datatype before the operation and re-convert back to uint8 when storing the image.因此,必须在操作之前转换为支持的数据类型,并在存储图像时重新转换回uint8 The author chose single because it saves 50% of memory compared to double , but the speed difference will be negligible on modern architectures.作者选择single是因为它比double节省了 50% 的内存,但在现代架构上速度差异可以忽略不计。

The authors makes unnecessary conversions, though.不过,作者进行了不必要的转换。 For example, the following line could be written without any conversions:例如,可以编写以下行而不进行任何转换:

mean_Removed = rest_of_the_images - uint8(single(mean_value)*single(white_Image))
% could be written
mean_Removed = rest_of_the_images - mean_value

Other places where single would be the right choice is when dealing with specific architectures like GPUs, because GPUs fully support single datatypes but not double s yet.其他可以选择single的地方是在处理特定架构(如 GPU)时,因为 GPU 完全支持single数据类型,但还不支持double

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM