-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbsubset.m.orig
152 lines (139 loc) · 4.42 KB
/
dbsubset.m.orig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<<<<<<< HEAD
function dbsubset( nQueries , newName)
=======
function [ queries good ok junk] = dbsubset( nQueries , newName)
>>>>>>> d4a0acff3fa2650bb9162fb54ef37aad16524382
%DBSUBSET Gernerates a rnd subset of the oxbuild DATASET
% Select random subsets from the queries and optionaly random subset of
% images per querry and create a folder with only the number of images
% that the queries will use
<<<<<<< HEAD
nGood = inf;
nOk = inf;
nJunk = inf;
nBad = 200;
=======
nGood = 10;
nOk = 10;
nJunk = 10;
>>>>>>> d4a0acff3fa2650bb9162fb54ef37aad16524382
seed = 1;
path = fullfile('vlbenchmark','vlbenchmarks','data','datasets','vggRetrievalDataset');
gtPath = fullfile(path,'oxbuild_gt');
imgPath = fullfile(path,'oxbuild');
%% load all queries
queries = dir(fullfile(gtPath, '*query.txt'));
good = dir(fullfile(gtPath, '*good.txt'));
ok = dir(fullfile(gtPath, '*ok.txt'));
junk = dir(fullfile(gtPath, '*junk.txt'));
%% fill new dtDir with random queries
newGTPath = fullfile(path,[newName,'_gt']);
if(~exist(newGTPath,'dir'))
mkdir(newGTPath );
end
rng(seed);
nq = length(queries);
l = 1;
alllines = cell(2);
%% function to select random number od lines from a file and write it to a new one
function cpSubset(in,out,nLines)
% open files
fin = fopen(in , 'r');
<<<<<<< HEAD
=======
fout = fopen(out,'w');
>>>>>>> d4a0acff3fa2650bb9162fb54ef37aad16524382
%read all lines
line = fgets(fin);
k = 1;
while ischar(line)
lines{k} = line;
alllines{l} = line;
l = l+1;
k=k+1;
line = fgets(fin);
end
<<<<<<< HEAD
fclose(fin);
% select the images to retain from the query and write to new file
if nLines ~= inf
fout = fopen(out,'w');
indices = randi(length(lines),1,nLines);
for j=1:nLines
fwrite(fout,lines{indices(j)});
end
fclose(fout);
else
copyfile(in,out);
end
% close files
=======
% select the images to retain from the query and write to new file
indices = randi(length(lines),1,nLines);
for j=1:nLines
fwrite(fout,lines{indices(j)});
end
% close files
fclose(fout);
fclose(fin);
>>>>>>> d4a0acff3fa2650bb9162fb54ef37aad16524382
end
%% select the query images to use
for i=1:nQueries
% select the query
index = randi(nq);
% copy rnd subsets of lines from the file
<<<<<<< HEAD
cpSubset(fullfile(gtPath,good(index).name),fullfile(newGTPath,good(index).name),nGood);
cpSubset(fullfile(gtPath,ok(index).name),fullfile(newGTPath,ok(index).name),nOk);
cpSubset(fullfile(gtPath,junk(index).name),fullfile(newGTPath,junk(index).name),nJunk);
% copy the query
copyfile(fullfile(gtPath,queries(index).name), fullfile(newGTPath,queries(index).name));
fid = fopen(fullfile(gtPath,queries(index).name),'r');
%parse the image name of the query and
s = fgets(fid);
x = strfind(s,' ');
alllines{l} = [s(6:x(1)-1),' '];
l = l+1;
end
%% select bad images
listing = dir(fullfile(imgPath,'*.jpg'));
names = {listing(:).name};
for i=1:nBad
name = names{randi([1,numel(names)])}(1:end-3);
alllines{l} = name;
l = l+1;
end
allimages = unique(alllines);
%% copy all images used by the query
nImages = length(allimages);
newImgPath = fullfile(path,newName);
if ~exist(fullfile(newImgPath),'dir')
mkdir(fullfile(newImgPath));
end
for i=1:nImages
name = [allimages{i}(1:end-1),'.jpg'];
if ~exist(fullfile(newImgPath,name),'file')
copyfile(fullfile(imgPath,name),newImgPath);
end
=======
goodName = good(index).name;
cpSubset(fullfile(gtPath,goodName),fullfile(newGTPath,goodName),nGood);
goodName = ok(index).name;
cpSubset(fullfile(gtPath,goodName),fullfile(newGTPath,goodName),nOk);
goodName = junk(index).name;
cpSubset(fullfile(gtPath,goodName),fullfile(newGTPath,goodName),nJunk);
% copy the query
copyfile(fullfile(gtPath,queries(index).name), fullfile(newGTPath,queries(index).name));
end
%% copy all images used by the query
allimages = unique(alllines);
nImages = length(allimages);
newImgPath = fullfile(path,newName);
mkdir(fullfile(newImgPath));
for i=1:nImages
name = [allimages{i}(1:end-1),'.jpg'];
copyfile(fullfile(imgPath,name),newImgPath);
>>>>>>> d4a0acff3fa2650bb9162fb54ef37aad16524382
end
end