.txt ファイルにデータを書き込もうとしています。各ファイルは約 170MB です (データを書き込んだ後)。
浮動小数点値をファイルに書き込むために「%.8f」を使用して、オクターブの fprintf 関数を使用しています。ただし、一部のファイルのエントリのサブセットが破損しているという非常に奇妙なエラーに気付きました。たとえば、ファイル内の行の 1 つを次に示します。
0.43529412,0.}4313725,0.43137255,0.33233533,...
その「}」は「4」である必要がありました。そもそも、オクターブの fprintf はどのようにして「}」を '%.8f' オプションで書き込んだのでしょうか? 何がうまくいかないのですか?
別の例は、
0.73289\8B987、...
その「\8B」はどうやってそこにたどり着いたのですか?
合計 3 億 6000 万点の非常に大きなデータセットを処理する必要があります。一部のファイルの行のサブセットにおけるこのエラーは、大きな問題になりつつあります。この問題の原因は何ですか?
また、この破損はランダムに発生するわけではありません。たとえば、ファイルに 110 万行あり、各行がデータ インスタンスを表すベクトルに対応している場合、問題は最大 100 行で発生し、これらの 100 行は一緒にクラスター化されます。たとえば、これらは行 8000 から 8150 に分散されているとしますが、100 行の破損した行のうち、最初の 50 行がたとえば 10000 行目の近くにあり、残りがたとえば 20000 行目にあるというわけではありません。それらは常にクラスターを形成します。
注: 以下のコードは、データの抽出とファイルへの書き込みを担当するコード ブロックです。K_Cell などのコード内の一部の変数は、以前に計算されており、データ書き込みプロセスではほとんど役割を果たしません。
mf = fspecial('gaussian',[5 5], 2);
fidM = fopen('14_01_2016_Go_AeossRight_ClustersM_wLAMRD.txt','w');
fidC = fopen('14_01_2016_Go_AeossRight_ClustersC_wLAMRD.txt','w');
fidW = fopen('14_01_2016_Go_AeossRight_ClustersW_wLAMRD.txt','w');
kIdx = 1;
featMat = [];
% - Generate file names to print the data to
featNo = 0;
fileNo = 1;
filePath = 'wLRD10_Data_Road/featMat_';
fileName = [filePath num2str(fileNo) '.txt'];
fidFeat = fopen(fileName, 'w');
% - Compute the global means and standard deviations
gMean = zeros(1,13); % - Global mean
gStds = zeros(1,13); % - Global variance
gNpts = 0; % - Total number of data points
fidStat = fopen('wLRD10_Data_Road/featStat.txt','w');
for i=1600:10:10000
if (featNo > 1000000)
% - If more than 1m points, close the file and open new one
fclose(fidFeat);
% - Get the new file name
fileNo = fileNo + 1;
fileName = [filePath num2str(fileNo) '.txt'];
fidFeat = fopen(fileName, 'w');
featNo = 0;
end
imgName = [fAddr num2str(i-1) '.jpg'];
img = imread(imgName);
Ir = im2double(img(:,:,1));
Ig = im2double(img(:,:,2));
Ib = im2double(img(:,:,3));
imgR = filter2(mf, Ir);
imgG = filter2(mf, Ig);
imgB = filter2(mf, Ib);
I = im2double(img);
I(:,:,1) = imgR;
I(:,:,2) = imgG;
I(:,:,3) = imgB;
I = im2uint8(I);
[Feat1, Feat2] = funcFeatures1(I);
[Feat3, Feat4] = funcFeatures2(I);
[Feat5, Feat6, Feat7] = funcFeatures3(I);
[Feat8, Feat9, Feat10] = funcFeatures4(I);
ids = K_Cell{kIdx};
pixVec = zeros(length(ids),13); % - Get the local image features
for s = 1:length(ids) % - Extract features
pixVec(s,:) = [Ir(ids(s,1),ids(s,2)) Ig(ids(s,1),ids(s,2)) Ib(ids(s,1),ids(s,2)) Feat1(ids(s,1),ids(s,2)) Feat2(ids(s,1),ids(s,2)) Feat3(ids(s,1),ids(s,2)) Feat4(ids(s,1),ids(s,2)) ...
Feat5(ids(s,1),ids(s,2)) Feat6(ids(s,1),ids(s,2)) Feat7(ids(s,1),ids(s,2)) Feat8(ids(s,1),ids(s,2))/100 Feat9(ids(s,1),ids(s,2))/500 Feat10(ids(s,1),ids(s,2))/200];
end
kIdx = kIdx + 1;
for s=1:length(ids)
featNo = featNo + 1;
fprintf(fidFeat,'%d,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f\n', featNo, pixVec(s,:));
end
% - Compute the mean and variances
for s = 1:length(ids)
gNpts = gNpts + 1;
delta = pixVec(s,:) - gMean;
gMean = gMean + delta./gNpts;
gStds = gStds*(gNpts-1)/gNpts + delta.*(pixVec(s,:) - gMean)/gNpts;
end
end
コード ブロックに注意してください。
for s=1:length(ids)
featNo = featNo + 1;
fprintf(fidFeat,'%d,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f\n', featNo, pixVec(s,:));
end
データポイントをファイルに書き込むコードの唯一の部分です。
以前のコードブロック、
if (featNo > 1000000)
% - If more than 1m points, close the file and open new one
fclose(fidFeat);
% - Get the new file name
fileNo = fileNo + 1;
fileName = [filePath num2str(fileNo) '.txt'];
fidFeat = fopen(fileName, 'w');
featNo = 0;
end
現在開いているファイルが 100 万データポイントの制限を超えた場合、データを書き込むために新しいファイルを開きます。
さらに、
pixVec
変数には、float/double 値以外のものを含めることはできません。そうしないと、オクターブがエラーをスローします。