Keras CNN, verbose training progress bar display

Viewed 3129

Running CNN in Keras. When it starts to run model.fit, it prints progress bar for each batch, like this

progress bar for each batch

Is it possible to display the progress bar for each epoch? Like this

enter image description here

Here is how I am using model.fit(x_train, y_train, nb_epoch = 1, batch_size = 32, verbose=1) I have tried to set verbose to 0 and 2, but there is no progress bar.

Please let me know if you have any thoughts. Many thanks

4 Answers

I gave a solution (not sure if can work for every case, but worked well for me) in https://stackoverflow.com/a/57475559/9531617. To quote myself:


The problem can be fixed without modifying the sources by simply installing ipykernel and importing it in your code:

pip install ipykernel Then import ipykernel

In fact, in the Keras generic_utils.py file, the probematic line was (in my case):

            if self._dynamic_display:
                sys.stdout.write('\b' * prev_total_width)
                sys.stdout.write('\r')
            else:
                sys.stdout.write('\n')

Where the self._dynamic_display was False, whereas it needed to be True to work properly. But the value self._dynamic_display was initiated such as:

        self._dynamic_display = ((hasattr(sys.stdout, 'isatty') and
                                  sys.stdout.isatty()) or
                                 'ipykernel' in sys.modules)

So, loading ipykerneladded it to sys.modules and fixed the problem for me.

As the question stated, this is for a package that we are importing with progress bars already defined.

In my case, the problem was compounded by printing all the updates on one looooong line. (PyCHARM is not treating \r as EOL?)

If you want a quick and dirty fix to progress bar issues, this helped mitigate the issue:

tensorflow_core.python.keras.utils.generic_utils.Progbar.__init__.__defaults__ = (10, 0, 5.0, None, 'step')

Works if you want to spend more time learning Keras and less time with weird UI issues.

I think this is a duplicate of Keras verbose training progress bar writing a new line on each batch issue but you could use tqdm (>=4.41.0) instead:

from tqdm.keras import TqdmCallback
...
model.fit(..., verbose=0, callbacks=[TqdmCallback(verbose=2)])

This turns off keras' progress (verbose=0), and uses tqdm instead. For the callback, verbose=2 means separate progressbars for epochs and batches. 1 means clear batch bars when done. 0 means only show epochs (never show batch bars).

Related