7

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

Braidy916
  • 186
  • 2
  • 5
  • Does setting `verbose=1` (progress bar logging) do what you want it to? – user3381590 May 15 '17 at 14:26
  • Thanks for your reply. What I want is just one progress bar logging. For example, the second batch (64/60000) progress bar replace (32/60000) progress bar in the same place and so on. I don't want to print the second progress bar. So for 1 epoch, there is only one progress bar – Braidy916 May 15 '17 at 15:14
  • Actually it is supposed to look like the way you want it. So there must be some bug that prevents it to work on your system the way it is supposed to. Which operating system and terminal are you using? – sietschie May 16 '17 at 10:05
  • Yes, this seems like a terminal/OS problem rather than a general Keras problem. Could your terminal be misinterpreting \r or \b characters? – user3381590 May 16 '17 at 20:22
  • Has an arrow appeared after ~2000 batches? – Marcin Możejko May 16 '17 at 21:53
  • I have the same problem on Ubuntu 16.04 but not on macOS 10.13. Haven`t found a solution yet. @MarcinMożejko An arrow appears. So the bar just works fine. There is only a problem with the `\r` or `\b` as @user3381590 pointed out. – McLawrence May 24 '17 at 13:44
  • As a quick fix for this on windows operating system, you can just maximize the command prompt window. as an example: below are two screenshots, the first one shows exactly the same problem like yours when I ran similar code on a normal size command prompt window. and the other one when running the same code on a maximized command prompt window. normal size: [Normal Size Window](https://i.stack.imgur.com/5mpQX.png) maximized: [Maximized Window](https://i.stack.imgur.com/s6atG.png) –  Jul 27 '17 at 23:29

4 Answers4

1

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.

Antonin G.
  • 374
  • 2
  • 8
0

As a quick fix for this on windows operating system, you can just maximize the command prompt window. this will exactly display the progress bar for each epoch as you want.

0

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.

0

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).

casper.dcl
  • 13,035
  • 4
  • 31
  • 32