plot_learning_curves.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. % Plots the lerning curves for the specified training runs from data in the
  2. % file "lfw_result.txt" stored in the log directory for the respective
  3. % model.
  4. % MIT License
  5. %
  6. % Copyright (c) 2016 David Sandberg
  7. %
  8. % Permission is hereby granted, free of charge, to any person obtaining a copy
  9. % of this software and associated documentation files (the "Software"), to deal
  10. % in the Software without restriction, including without limitation the rights
  11. % to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. % copies of the Software, and to permit persons to whom the Software is
  13. % furnished to do so, subject to the following conditions:
  14. %
  15. % The above copyright notice and this permission notice shall be included in all
  16. % copies or substantial portions of the Software.
  17. %
  18. % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. % IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. % FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. % AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. % LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. % OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. % SOFTWARE.
  25. %%
  26. addpath('/home/david/git/facenet/util/');
  27. log_dirs = { '/home/david/logs/facenet' };
  28. %%
  29. res = { ...
  30. { '20180402-114759', 'vggface2, wd=5e-4, center crop, fixed image standardization' }, ...
  31. };
  32. %%
  33. res = { ...
  34. { '20180408-102900', 'casia, wd=5e-4, pnlf=5e-4, fixed image standardization' }, ...
  35. };
  36. %%
  37. colors = {'b', 'g', 'r', 'c', 'm', 'y', 'k'};
  38. markers = {'.', 'o', 'x', '+', '*', 's', 'd' };
  39. lines = {'-', '-.', '--', ':' };
  40. fontSize = 6;
  41. lineWidth = 2;
  42. lineStyles = combineStyles(colors, markers);
  43. lineStyles2 = combineStyles(colors, {''}, lines);
  44. legends = cell(length(res),1);
  45. legends_accuracy = cell(length(res),1);
  46. legends_valrate = cell(length(res),1);
  47. var = cell(length(res),1);
  48. for i=1:length(res),
  49. for k=1:length(log_dirs)
  50. if exist(fullfile(log_dirs{k}, res{i}{1}), 'dir')
  51. ld = log_dirs{k};
  52. end
  53. end
  54. filename = fullfile(ld, res{i}{1}, 'stat.h5');
  55. var{i} = readlogs(filename,{'loss', 'reg_loss', 'xent_loss', 'lfw_accuracy', ...
  56. 'lfw_valrate', 'val_loss', 'val_xent_loss', 'val_accuracy', ...
  57. 'accuracy', 'prelogits_norm', 'learning_rate', 'center_loss', ...
  58. 'prelogits_hist', 'accuracy'});
  59. var{i}.steps = 1:length(var{i}.loss);
  60. epoch = find(var{i}.lfw_accuracy,1,'last');
  61. var{i}.epochs = 1:epoch;
  62. legends{i} = sprintf('%s: %s', res{i}{1}, res{i}{2});
  63. start_epoch = max(1,epoch-10);
  64. legends_accuracy{i} = sprintf('%s: %s (%.2f%%)', res{i}{1}, res{i}{2}, mean(var{i}.lfw_accuracy(start_epoch:epoch))*100 );
  65. legends_valrate{i} = sprintf('%s: %s (%.2f%%)', res{i}{1}, res{i}{2}, mean(var{i}.lfw_valrate(start_epoch:epoch))*100 );
  66. arguments_filename = fullfile(ld, res{i}{1}, 'arguments.txt');
  67. if exist(arguments_filename)
  68. str = fileread(arguments_filename);
  69. var{i}.wd = getParameter(str, 'weight_decay', '0.0');
  70. var{i}.cl = getParameter(str, 'center_loss_factor', '0.0');
  71. var{i}.fixed_std = getParameter(str, 'use_fixed_image_standardization', '0');
  72. var{i}.data_dir = getParameter(str, 'data_dir', '');
  73. var{i}.lr = getParameter(str, 'learning_rate', '0.1');
  74. var{i}.epoch_size = str2double(getParameter(str, 'epoch_size', '1000'));
  75. var{i}.batch_size = str2double(getParameter(str, 'batch_size', '90'));
  76. var{i}.examples_per_epoch = var{i}.epoch_size*var{i}.batch_size;
  77. var{i}.mnipc = getParameter(str, 'filter_min_nrof_images_per_class', '-1');
  78. var{i}.val_step = str2num(getParameter(str, 'validate_every_n_epochs', '10'));
  79. var{i}.pnlf = getParameter(str, 'prelogits_norm_loss_factor', '-1');
  80. var{i}.emb_size = getParameter(str, 'embedding_size', '-1');
  81. fprintf('%s: wd=%s lr=%s, pnlf=%s, data_dir=%s, emb_size=%s\n', ...
  82. res{i}{1}, var{i}.wd, var{i}.lr, var{i}.pnlf, var{i}.data_dir, var{i}.emb_size);
  83. end
  84. end;
  85. timestr = datestr(now,'yyyymmdd_HHMMSS');
  86. h = 1; figure(h); close(h); figure(h); hold on; setsize(1.5);
  87. title('LFW accuracy');
  88. xlabel('Steps');
  89. ylabel('Accuracy');
  90. grid on;
  91. N = 1; flt = ones(1,N)/N;
  92. for i=1:length(var),
  93. plot(var{i}.epochs*1000, filter(flt, 1, var{i}.lfw_accuracy(var{i}.epochs)), lineStyles2{i}, 'LineWidth', lineWidth);
  94. end;
  95. legend(legends_accuracy,'Location','SouthEast','FontSize',fontSize);
  96. v=axis;
  97. v(3:4) = [ 0.95 1.0 ];
  98. axis(v);
  99. accuracy_file_name = sprintf('lfw_accuracy_%s',timestr);
  100. %print(accuracy_file_name,'-dpng')
  101. if 0
  102. %%
  103. %h = 2; figure(h); close(h); figure(h); hold on; setsize(1.5);
  104. h = 1; figure(h); hold on;
  105. title('LFW validation rate');
  106. xlabel('Step');
  107. ylabel('VAL @ FAR = 10^{-3}');
  108. grid on;
  109. for i=1:length(var),
  110. plot(var{i}.epochs*1000, var{i}.lfw_valrate(var{i}.epochs), lineStyles{i}, 'LineWidth', lineWidth);
  111. end;
  112. legend(legends_valrate,'Location','SouthEast','FontSize',fontSize);
  113. v=axis;
  114. v(3:4) = [ 0.5 1.0 ];
  115. axis(v);
  116. valrate_file_name = sprintf('lfw_valrate_%s',timestr);
  117. % print(valrate_file_name,'-dpng')
  118. end
  119. if 0
  120. %% Plot cross-entropy loss
  121. h = 3; figure(h); close(h); figure(h); hold on; setsize(1.5);
  122. title('Training/validation set cross-entropy loss');
  123. xlabel('Step');
  124. title('Training/validation set cross-entropy loss');
  125. grid on;
  126. N = 500; flt = ones(1,N)/N;
  127. for i=1:length(var),
  128. var{i}.xent_loss(var{i}.xent_loss==0) = NaN;
  129. plot(var{i}.steps, filter(flt, 1, var{i}.xent_loss), lineStyles2{i}, 'LineWidth', lineWidth);
  130. end;
  131. legend(legends, 'Location', 'NorthEast','FontSize',fontSize);
  132. % Plot cross-entropy loss on validation set
  133. N = 1; flt = ones(1,N)/N;
  134. for i=1:length(var),
  135. v = var{i}.val_xent_loss;
  136. val_steps = (1:length(v))*var{i}.val_step*1000;
  137. v(v==0) = NaN;
  138. plot(val_steps, filter(flt, 1, v), [ lineStyles2{i} '.' ], 'LineWidth', lineWidth);
  139. end;
  140. legend(legends, 'Location', 'NorthEast','FontSize',fontSize);
  141. hold off
  142. xent_file_name = sprintf('xent_%s',timestr);
  143. %print(xent_file_name,'-dpng')
  144. end
  145. if 0
  146. %% Plot accuracy on training set
  147. h = 32; figure(h); clf; hold on;
  148. title('Training/validation set accuracy');
  149. xlabel('Step');
  150. ylabel('Training/validation set accuracy');
  151. grid on;
  152. N = 500; flt = ones(1,N)/N;
  153. for i=1:length(var),
  154. var{i}.accuracy(var{i}.accuracy==0) = NaN;
  155. plot(var{i}.steps*1000, filter(flt, 1, var{i}.accuracy), lineStyles2{i}, 'LineWidth', lineWidth);
  156. end;
  157. legend(legends, 'Location', 'SouthEast','FontSize',fontSize);
  158. grid on;
  159. N = 1; flt = ones(1,N)/N;
  160. for i=1:length(var),
  161. v = var{i}.val_accuracy;
  162. val_steps = (1:length(v))*var{i}.val_step*1000;
  163. v(v==0) = NaN;
  164. plot(val_steps*1000, filter(flt, 1, v), [ lineStyles2{i} '.' ], 'LineWidth', lineWidth);
  165. end;
  166. legend(legends, 'Location', 'SouthEast','FontSize',fontSize);
  167. hold off
  168. acc_file_name = sprintf('accuracy_%s',timestr);
  169. %print(acc_file_name,'-dpng')
  170. end
  171. if 0
  172. %% Plot prelogits CDF
  173. h = 35; figure(h); clf; hold on;
  174. title('Prelogits histogram');
  175. xlabel('Epoch');
  176. ylabel('Prelogits histogram');
  177. grid on;
  178. N = 1; flt = ones(1,N)/N;
  179. for i=1:length(var),
  180. epoch = var{i}.epochs(end);
  181. q = cumsum(var{i}.prelogits_hist(:,epoch));
  182. q2 = q / q(end);
  183. plot(linspace(0,10,1000), q2, lineStyles2{i}, 'LineWidth', lineWidth);
  184. end;
  185. legend(legends, 'Location', 'SouthEast','FontSize',fontSize);
  186. hold off
  187. end
  188. if 0
  189. %% Plot prelogits norm
  190. h = 32; figure(h); clf; hold on;
  191. title('Prelogits norm');
  192. xlabel('Step');
  193. ylabel('Prelogits norm');
  194. grid on;
  195. N = 1; flt = ones(1,N)/N;
  196. for i=1:length(var),
  197. plot(var{i}.steps, filter(flt, 1, var{i}.prelogits_norm), lineStyles2{i}, 'LineWidth', lineWidth);
  198. end;
  199. legend(legends, 'Location', 'NorthEast','FontSize',fontSize);
  200. hold off
  201. end
  202. if 0
  203. %% Plot learning rate
  204. h = 42; figure(h); clf; hold on;
  205. title('Learning rate');
  206. xlabel('Step');
  207. ylabel('Learning rate');
  208. grid on;
  209. N = 1; flt = ones(1,N)/N;
  210. for i=1:length(var),
  211. semilogy(var{i}.epochs, filter(flt, 1, var{i}.learning_rate(var{i}.epochs)), lineStyles2{i}, 'LineWidth', lineWidth);
  212. end;
  213. legend(legends, 'Location', 'NorthEast','FontSize',fontSize);
  214. hold off
  215. end
  216. if 0
  217. %% Plot center loss
  218. h = 9; figure(h); close(h); figure(h); hold on; setsize(1.5);
  219. title('Center loss');
  220. xlabel('Epochs');
  221. ylabel('Center loss');
  222. grid on;
  223. N = 500; flt = ones(1,N)/N;
  224. for i=1:length(var),
  225. if isempty(var{i}.center_loss)
  226. var{i}.center_loss = ones(size(var{i}.steps))*NaN;
  227. end;
  228. var{i}.center_loss(var{i}.center_loss==0) = NaN;
  229. plot(var{i}.steps/var{i}.epoch_size, filter(flt, 1, var{i}.center_loss), lineStyles2{i}, 'LineWidth', lineWidth);
  230. end;
  231. legend(legends, 'Location', 'NorthEast','FontSize',fontSize);
  232. end
  233. if 0
  234. %% Plot center loss with factor
  235. h = 9; figure(h); close(h); figure(h); hold on; setsize(1.5);
  236. title('Center loss with factor');
  237. xlabel('Epochs');
  238. ylabel('Center loss * center loss factor');
  239. grid on;
  240. N = 500; flt = ones(1,N)/N;
  241. for i=1:length(var),
  242. if isempty(var{i}.center_loss)
  243. var{i}.center_loss = ones(size(var{i}.steps))*NaN;
  244. end;
  245. var{i}.center_loss(var{i}.center_loss==0) = NaN;
  246. plot(var{i}.steps/var{i}.epoch_size, filter(flt, 1, var{i}.center_loss*str2num(var{i}.cl)), lineStyles2{i}, 'LineWidth', lineWidth);
  247. end;
  248. legend(legends, 'Location', 'NorthEast','FontSize',fontSize);
  249. end
  250. if 0
  251. %% Plot total loss
  252. h = 4; figure(h); close(h); figure(h); hold on; setsize(1.5);
  253. title('Total loss');
  254. xlabel('Epochs');
  255. ylabel('Total loss');
  256. grid on;
  257. N = 500; flt = ones(1,N)/N;
  258. for i=1:length(var),
  259. var{i}.loss(var{i}.loss==0) = NaN;
  260. plot(var{i}.steps/var{i}.epoch_size, filter(flt, 1, var{i}.loss), lineStyles2{i}, 'LineWidth', lineWidth);
  261. end;
  262. legend(legends, 'Location', 'NorthEast','FontSize',fontSize);
  263. end
  264. if 0
  265. %% Plot regularization loss
  266. h = 5; figure(h); close(h); figure(h); hold on; setsize(1.5);
  267. title('Regularization loss');
  268. xlabel('Epochs');
  269. ylabel('Regularization loss');
  270. grid on;
  271. N = 500; flt = ones(1,N)/N;
  272. for i=1:length(var),
  273. var{i}.reg_loss(var{i}.reg_loss==0) = NaN;
  274. plot(var{i}.steps/var{i}.epoch_size, filter(flt, 1, var{i}.reg_loss), lineStyles2{i}, 'LineWidth', lineWidth);
  275. end;
  276. legend(legends, 'Location', 'NorthEast','FontSize',fontSize);
  277. end