dan@9
|
1 function [trainlists, testlists, testgtlists] = sceneClassificationMetrics_createFolds(wavsourcedir, targetdir)
|
dan@9
|
2
|
dan@9
|
3 % [trainlists, testlists, testgtlists] = sceneClassificationMetrics_createFolds('~/data/aasp_chall_FROMRDR2/scenes_stereo/scenes_stereo', 'tmp')
|
dan@9
|
4 % [trainlists, testlists, testgtlists] = sceneClassificationMetrics_createFolds('scenes_stereo', 'tmp');
|
dan@9
|
5
|
dan@9
|
6 numfolds=5;
|
dan@9
|
7
|
dan@9
|
8 wavpaths = dir(fullfile(wavsourcedir, '*.wav'));
|
dan@9
|
9
|
dan@9
|
10 % Slightly strange way to index class labels; it's because containers.Map not present in Octave
|
dan@9
|
11 classIDs.bus = 1;
|
dan@9
|
12 classIDs.busystreet = 2;
|
dan@9
|
13 classIDs.office = 3;
|
dan@9
|
14 classIDs.openairmarket = 4;
|
dan@9
|
15 classIDs.park = 5;
|
dan@9
|
16 classIDs.quietstreet = 6;
|
dan@9
|
17 classIDs.restaurant = 7;
|
dan@9
|
18 classIDs.supermarket = 8;
|
dan@9
|
19 classIDs.tube = 9;
|
dan@9
|
20 classIDs.tubestation = 10;
|
dan@9
|
21 classcollections = cell(10,1);
|
dan@9
|
22
|
dan@9
|
23 % For every file, we create an entry in classcollections[id][], also allocating its fold number
|
dan@9
|
24 for index = 1:length(wavpaths)
|
dan@9
|
25 wavpathobj = wavpaths(index);
|
dan@9
|
26 wavitem.origpath = fullfile(wavsourcedir, wavpathobj.name);
|
dan@9
|
27 wavitem.classstr = wavpathobj.name(:,1:length(wavpathobj.name)-6); % rm 6 chars eg '09.wav'
|
dan@9
|
28 wavitem.classID = getfield(classIDs, wavitem.classstr);
|
dan@9
|
29 numsofar = length(classcollections{wavitem.classID});
|
dan@9
|
30 wavitem.whichfold = mod(numsofar, numfolds) + 1;
|
dan@9
|
31 classcollections{wavitem.classID}{numsofar+1} = wavitem;
|
dan@9
|
32 end
|
dan@9
|
33
|
dan@9
|
34
|
dan@9
|
35 % for each fold, we'll create a folder with a scrambley name, and we'll initialise output files
|
dan@9
|
36 for whichfold=1:numfolds
|
dan@9
|
37 foldfolders{whichfold} = fullfile(targetdir, sprintf('%d_%s', whichfold, RandomString1(6)));
|
dan@9
|
38 mkdir(foldfolders{whichfold});
|
dan@9
|
39 trainlists{whichfold} = fullfile(foldfolders{whichfold}, sprintf('fold%d_train.txt', whichfold));
|
dan@9
|
40 testlists{whichfold} = fullfile(foldfolders{whichfold}, sprintf('fold%d_test.txt', whichfold));
|
dan@9
|
41 testgtlists{whichfold} = fullfile(targetdir, sprintf('fold%d_testgt_%s.txt', whichfold, RandomString1(6)));
|
dan@9
|
42 f_trainlists{whichfold} = fopen(trainlists{ whichfold}, 'w');
|
dan@9
|
43 f_testlists{whichfold} = fopen(testlists{ whichfold}, 'w');
|
dan@9
|
44 f_testgtlists{whichfold} = fopen(testgtlists{whichfold}, 'w');
|
dan@9
|
45 end
|
dan@9
|
46
|
dan@9
|
47 for whichclass = 1:length(classcollections)
|
dan@9
|
48 thisclass = classcollections{whichclass};
|
dan@9
|
49
|
dan@9
|
50 for whichitem = 1:length(thisclass)
|
dan@9
|
51 wavitem = thisclass{whichitem};
|
dan@9
|
52 for whichfold=1:numfolds
|
dan@9
|
53 newfilename = fullfile(foldfolders{whichfold}, sprintf('%d_%s.wav', whichitem, RandomString1(6)));
|
dan@9
|
54 copyfile(wavitem.origpath, newfilename); % BE CAREFUL WITH THIS!!!
|
dan@9
|
55 if whichfold==wavitem.whichfold
|
dan@9
|
56 % this fold is TESTING for this item
|
dan@9
|
57 fprintf(f_testgtlists{whichfold}, '%s\t%s\n', newfilename, wavitem.classstr);
|
dan@9
|
58 fprintf(f_testlists{ whichfold}, '%s\n', newfilename);
|
dan@9
|
59 else
|
dan@9
|
60 % this fold is TRAINING for this item
|
dan@9
|
61 fprintf(f_trainlists{whichfold}, '%s\t%s\n', newfilename, wavitem.classstr);
|
dan@9
|
62 end
|
dan@9
|
63 end
|
dan@9
|
64 end
|
dan@9
|
65 end
|
dan@9
|
66
|
dan@9
|
67 % close the files
|
dan@9
|
68 for whichfold=1:numfolds
|
dan@9
|
69 fclose(f_trainlists{ whichfold});
|
dan@9
|
70 fclose(f_testlists{ whichfold});
|
dan@9
|
71 fclose(f_testgtlists{whichfold});
|
dan@9
|
72 end
|
dan@9
|
73
|
dan@9
|
74
|