MATLAB Answers

How do I save the training progress images generated in "trainNetwork" when 'Plots' is set to 'training-progress' in the training options?

185 views (last 30 days)
When training my neural net with "trainNetwork", I have passed in training options with the 'Plots' field set to 'training-options'. This causes a plot to be generated regularly throughout the training process, updating the user on mini-batch loss and accuracy, validation data loss and accuracy, and a few other metrics.
Is there any way I can save these images?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 4 May 2020
Edited: MathWorks Support Team on 4 May 2020
One way to do this is to make use of the "OutputFcn" field in the training options structure. You can customize a function, and assign it as the value of this field when calling "trainingOptions". See the 'OutputFcn' section in the below MATLAB documentation.
This function will execute once before the training, once after the training, and once after every iteration. It takes as input an information structure with information about the progress of the training. This is useful since you could, for instance, check the "State" field of the info structure to see if the training is over, and then save the final result figure using "savefig".
As a caveat, the figures produced during the training are hidden by default, so the function "findall" may be needed to find the handles that are invisible.
>> findall(groot, 'Type', 'Figure')

  3 Comments

Chee Ho Ng
Chee Ho Ng on 6 Jan 2018
Dear MathWorks Support Team, I'm having this issue also. However, I'm not familiar with the "findall" function. Could you provide a simple example code on "how to save the training progress plot"?
Thank you in advance!
Erica Kiderman
Erica Kiderman on 15 May 2018
Thank you for your feedback. I have updated the answer above to include an example of how to use the "findall" function

Sign in to comment.

More Answers (7)

Peter
Peter on 24 Apr 2018
The following works for me: add the following to the trainingOptions:
'OutputFcn',@(info)savetrainingplot(info),
Add the following function at the end of the script
function stop=savetrainingplot(info)
stop=false; %prevents this function from ending trainNetwork prematurely
if info.State=='done' %check if all iterations have completed
% if true
saveas(gcf,'filename.png') % save figure as .png, you can change this
end
end

  7 Comments

Show 4 older comments
Shweta Singh
Shweta Singh on 4 May 2020
Hi Francisco,
If you need help with the specific error message, please contact MathWorks Technical Support by using the following link:
Thanks,
Shweta Singh

Sign in to comment.


Feruza Amirkulova
Feruza Amirkulova on 10 Aug 2019
When training process is over, use on command window:
>> h= findall(groot,'Type','Figure');
>> h.MenuBar = 'figure';
It will make traning window look like Matlab Figure, and save in jpg or eps format. I was able to save as Matlab .fig files but I wasn't able to open them.

  2 Comments

Siyuan Zhou
Siyuan Zhou on 31 May 2020
Hi Feruza, I tried your code and it works for saving the training loss curve, but I can not edit the saved fig, nor can I copy figure
I do not know how to cope with this situation, can you help me with that? Many thanks!
Feruza Amirkulova
Feruza Amirkulova on 31 May 2020
Hi Siyuan,
I have have not used Matlab Deep learning recently, and my students and I mostly used TensorFlow with Keras. I will be working on research over summer and will try to use Matlab Deep learning again. From past experience I found that printing figures espaciallay traning process on MATLAB 2019a is finicky. I have not used latest vesion yet but was told that Deep Learning toolbox has been improved. I will get back to you ones I start working on it. If you have licence you may also contact MathWork support team. I used their suggessions.
Thanks,
Feruza

Sign in to comment.


Aqib Mumtaz
Aqib Mumtaz on 25 Mar 2018
I am also looking for some quick solution to save progress plot instead of plotting first my own then saving?

  0 Comments

Sign in to comment.



Jifeng Chu
Jifeng Chu on 28 May 2019
I also have this problem, maybe there has another convenient way.
You can try the following script to output the data, and draw the curves of training progress by yourself.
[trainedNet,traininfo] = trainNetwork(___)

  0 Comments

Sign in to comment.


Daniel Hussey
Daniel Hussey on 6 Nov 2019
Another solution I have found is to turn on MATLAB's diary feature, which will save the output of the training to the console in text format. This actually seems to be in Markup format, so I used an online tool (https://tableconvert.com/?output=excel) to convert the data back into something more useful, like Excel, where I could play around with the formatting and such better. Just another option.

  0 Comments

Sign in to comment.


Mendi
Mendi on 5 Dec 2019
Go inside trainNetwork.m
Change Line 1:
function [trainedNet, info, fig] = trainNetwork(varargin)
Change line 168:
[trainedNet, info, fig] = doTrainNetwork(layersOrGraph, opts, X, Y);
Change line 175:
function [trainedNet, info, fig] = doTrainNetwork(layersOrGraph, opts, X, Y)
In line 271 (before function "doTrainNetwork" end) add those lines:
idxTrainingPlotReporter=find(cellfun(@(r)isa(r,'nnet.internal.cnn.util.TrainingPlotReporter'),reporters.Reporters),1);
if ~isempty(idxTrainingPlotReporter)
fig=reporters.Reporters{idxTrainingPlotReporter}.TrainingPlotPresenter.TrainingPlotView.Figure;
else
fig=[];
end
Now in you can call:
[net,info,fig] = trainNetwork(trainingData,net,options);
saveas(fig,'myfig.png')

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by