Guest User

Untitled

a guest
Dec 15th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.27 KB | None | 0 0
  1. function [] = createIMDB(trainingImageLocation, dumpSpace)
  2. %createIMDB: Function used to create IMDB
  3. % Function scans through the real image database on the computer and
  4. % creates the IMDB of well labeled and classified images. Meta data such
  5. % as image categorizations are also included in the created db to guide
  6. % any user who might need to use the IMDB in the future
  7. % 70 percent of the data is used for training
  8. % 20 percent for validation
  9. % 10 percent reserved for testing
  10. % TrainingSet : to fit the parameters [i.e., weights]
  11. % ValidationSet: to tune the parameters [i.e., architecture]
  12. % Testset : to assess the performance [i.e. predictive power]
  13.  
  14.  
  15. %{
  16. Created on: 31st March, 2017
  17. Author: Oluwole Oyetoke Jnr
  18. Using MATLAB 2016
  19. %}
  20.  
  21. %Validation
  22. if nargin ~= 2
  23. error('createIMDB:Input_Argument_Error','This function works with 2 input argument -trainingImageLocation, dumpSpace- ')
  24. end
  25.  
  26. %IMDB Creation Start Time
  27. datenow = datetime('now','Format','dd-MMM-yyyy HH:mm:ss');
  28. fprintf('Start Time: %s\n\n',datenow);
  29.  
  30. %Create an empty IMDB structure
  31. imdb = struct();
  32. categories = {'speed_20', 'speed_30','speed_50','speed_60','speed_70',...
  33. 'speed_80','speed_less_80','speed_100','speed_120',...
  34. 'no_car_overtaking','no_truck_overtaking','priority_road',...
  35. 'priority_road_2','yield_right_of_way','stop','road_closed',...
  36. 'maximum_weight_allowed','entry_prohibited','danger','curve_left',...
  37. 'curve_right','double_curve_right','rough_road','slippery_road',...
  38. 'road_narrows_right','work_in_progress','traffic_light_ahead',...
  39. 'pedestrian_crosswalk','children_area','bicycle_crossing',...
  40. 'beware_of_ice','wild_animal_crossing','end_of_restriction',...
  41. 'must_turn_right','must_turn_left','must_go_straight',...
  42. 'must_go_straight_or_right','must_go_straight_or_left',...
  43. 'mandatroy_direction_bypass_obstacle',...
  44. 'mandatroy_direction_bypass_obstacle2', ...
  45. 'traffic_circle','end_of_no_car_overtaking',...
  46. 'end_of_no_truck_overtaking'};
  47.  
  48. datasets = {'train', 'validate', 'test'};
  49.  
  50. %To Create an IMDB scaled to a different size, simply change netInputSize
  51. netInputSize = [227 227];
  52.  
  53. %.ppm (portable pixmap format) is used in this project
  54. primaryTrainingDataPath = trainingImageLocation;
  55.  
  56. try
  57. %Loads all the content of the training folder
  58. trainingFolderStruct = dir([primaryTrainingDataPath]);
  59. catch
  60. error('createIMDB2:Traing_Image_Location_Error','Error Encounterd When Loading Image Data From Folder')
  61. end
  62. [noOfTrainingFolders d] = size(trainingFolderStruct);
  63. dirFlags = [trainingFolderStruct.isdir];
  64. subFolderList = trainingFolderStruct(dirFlags);
  65.  
  66.  
  67. %Loop through the training image folder to get total number of images in DB
  68. %Main folder contains subfolders of images for each training class
  69. imageCount=0;
  70. for mainLoopCount = 3:noOfTrainingFolders
  71. secondaryTrainingDataPath = fullfile(primaryTrainingDataPath,...
  72. subFolderList(mainLoopCount).name, '*.ppm');
  73. subFolderStruct = dir([secondaryTrainingDataPath]);
  74. noOfContents2= numel(subFolderStruct);
  75. imageCount =imageCount+noOfContents2;
  76. % fprintf('Number of Images in Training Class %s = %d\n', ...
  77. %subFolderList(mainLoopCount).name, noOfContents2);
  78. end
  79. fprintf('Number of Training Images in Total: %d\n', imageCount);
  80.  
  81.  
  82. %Initialize part of the imdb structure
  83. imdb.meta.sets = {'train', 'validate', 'test'};
  84.  
  85. %Possible Image Categories
  86. imdb.meta.categories = categories;
  87.  
  88. %AlexNet Uses 227 by 227 by 3 images
  89. imdb.images.data = ones(netInputSize(1), netInputSize(2), 3, imageCount, 'single');
  90. imdb.images.labels = ones(1,imageCount, 'single'); %Image label
  91. % vector indicating to which set an image belong,
  92. %i.e., % training, validation, test etc.
  93. imdb.images.set = ones(1, imageCount, 'uint8');
  94.  
  95. fprintf('Each image will be resized to %d by %d by 3 \n', netInputSize(1), netInputSize(2));
  96.  
  97. %Loop through Dataset, appropriately dimension all contents, label,
  98. %classify and place in sets
  99. imageCounter=1;
  100. for mainLoopCount = 3:noOfTrainingFolders
  101.  
  102. actualPos = mainLoopCount-2;
  103. toWorkOn = char (categories(actualPos));
  104. fprintf('%d. Loading and working on training, validation and test images for '' %s '' traffic sign\n',actualPos, toWorkOn);
  105. secondaryTrainingDataPath = fullfile(primaryTrainingDataPath,...
  106. subFolderList(mainLoopCount).name, '*.ppm');
  107.  
  108. %Get only .ppm contnets of the folder
  109. subFolderStruct = dir([secondaryTrainingDataPath]);
  110.  
  111.  
  112. %Get no. of contents in folder
  113. noOfContents2= numel(subFolderStruct);
  114.  
  115. if(noOfContents2<10)
  116. error('createIMDB2:Image_Class_Error',...
  117. 'Every Class in the dataset should contain at least 10 images')
  118. end
  119.  
  120. %Get Number of images to be used for training, validation and testing
  121. trainNo = floor(0.7* noOfContents2);
  122. valNo = floor(0.2* noOfContents2);
  123. testNo = floor(0.1* noOfContents2);
  124. total =trainNo+valNo+testNo;
  125. if(total<noOfContents2)
  126. difference = noOfContents2 - total;
  127. trainNo = trainNo+difference;
  128. elseif (total>noOfContents2)
  129. difference = total-noOfContents2;
  130. trainNo = trainNo-difference;
  131. end
  132. fprintf('Out of a total of %d images in this class %d will be used for training, %d for validation and %d for testing\n', total, trainNo, valNo, testNo);
  133. setBank = getSetPositions(trainNo, valNo, testNo);
  134.  
  135. for innerLoopCount = 1:noOfContents2
  136.  
  137. pathToImage= fullfile(primaryTrainingDataPath, subFolderList(mainLoopCount).name, subFolderStruct(innerLoopCount).name);
  138.  
  139. imageRead = imread(pathToImage);
  140.  
  141. %Check to make sure image contains 3 channels. AlexNet works with 3
  142. %channels
  143. [xDim yDim zDim] = size(imageRead);
  144. threeDImage = imageRead;
  145. if (zDim==1)
  146. threeDImage = cat(3, imageRead, imageRead, imageRead);
  147. end
  148.  
  149.  
  150. %Resize Image to acceptable AlexNet Input size [227 227 3]
  151. properSizedImageData = threeDImage;
  152. if(xDim~=netInputSize(1) || yDim~=netInputSize(2))
  153. properSizedImageData = imresize(threeDImage, [netInputSize(1) netInputSize(2)], 'bilinear');
  154. end
  155.  
  156. %Set image back into DB & apply all related meta information
  157. %AlexNet Uses 227 by 227 by 3 images
  158. %Load in Image Data. Stack of 3 channels
  159. imdb.images.data(:,:,1,imageCounter) = properSizedImageData(:,:,1);
  160. imdb.images.data(:,:,2,imageCounter) = properSizedImageData(:,:,2);
  161. imdb.images.data(:,:,3,imageCounter) = properSizedImageData(:,:,3);
  162.  
  163. %Assign Label to image
  164. %categories(mainLoopCount-2);
  165. imdb.images.labels(1,imageCounter) = mainLoopCount-2;
  166.  
  167. %datasets(1) = Training, datasets(2) = Validate datasets(3)= Test
  168. imdb.images.set(1, imageCounter) = setBank(innerLoopCount);
  169.  
  170. imageCounter = imageCounter+1;
  171.  
  172. end %Inner Loop
  173. percentageCompleted = uint8 ((imageCounter*100)/imageCount);
  174. fprintf('%d%% completed so far\n\n', percentageCompleted);
  175. end %Main Loop
  176.  
  177.  
  178. %Save IMDB
  179. fprintf('Saving IMDB file\n');
  180. filename = fullfile(dumpSpace, 'Traffic_Sign_IMDB(GSTBR)_All_32by32.mat');
  181. save(filename, 'imdb');
  182.  
  183. datenow2 = datetime('now','Format','dd-MMM-yyyy HH:mm:ss');
  184. fprintf('End Time: %s\n\n',datenow2);
  185.  
  186.  
  187. d1=datenum(datenow); % convert to number
  188. d2=datenum(datenow2); % convert to number
  189. difference=d2-d1; % difference between the two
  190. days = floor(difference);
  191. hrs = datestr(difference, 'HH');
  192. mins = datestr(difference, 'MM');
  193. seconds = datestr(difference, 'SS');
  194. % difference in days:hr:min:sec
  195.  
  196. %Escape random number generator legacy mode
  197. rng('default');
  198.  
  199. fprintf('Overall Time Taken: %d day(s), %s hour(s), %s minute(s), %s second(s) \n\n',days, hrs, mins, seconds);
  200.  
  201. end
  202.  
  203.  
  204.  
  205. %Function used to pick random images for test, validate and train
  206. function allocationSpots = getSetPositions(trainNo, valNo, testNo);
  207. total = trainNo+valNo+testNo;
  208. perms = randperm(total);
  209.  
  210. trainPositions = perms(1:trainNo);
  211. valPositions = perms(trainNo+1:trainNo+valNo);
  212. testPositions = perms(trainNo+valNo+1:trainNo+valNo+testNo);
  213.  
  214. allocationSpots = zeros(total,1);
  215.  
  216. for i=1:total
  217. if(ismember(i,trainPositions))
  218. allocationSpots(i) = 1;
  219. elseif(ismember(i,valPositions))
  220. allocationSpots(i) = 2;
  221. elseif(ismember(i,testPositions))
  222. allocationSpots(i) = 3;
  223. end
  224. end
  225.  
  226.  
  227. end
Add Comment
Please, Sign In to add comment