0

My project is OCR. I used image_retraining(v0.10.0) to recognize letters.

I train it with pictures size 128x128

After that I use my code to input several letter pictures (1306 pictures) which I segmented from a page of document

The code run so slow.

  • It took 3 seconds to recognize 1 letter and near 30 minutes to finish 1306 pictures on my laptop.

  • It took 38 seconds to recognize 1 letter and near 6 hours to finish 1306 pictures on pi 2

I don't know why it run so slow. My C++ code use SVM on QT just took 5 seconds to do that ( It uses picture size 32x24).

So Is it because I use picture too large ? or python run slower than C++

Would you mind giving me advices to make it run faster

Update #1: The picture size is not the big problem. Follow the time_chart. It seems the code slow because of this command

predictions = sess.run(softmax_tensor, \
        {'DecodeJpeg/contents:0': image_data})

Does anyone have advices to make the code run faster.

Nguyen Khoi
  • 75
  • 1
  • 10
  • Do some [profiling](http://docs.python.org/2/library/profile.html#module-cProfile) to see where the main bottle-necks are. – ekhumoro Nov 20 '16 at 18:45
  • Do you know how to output the command "print s.getvalue()" to file text in python. It too long so the terminal cannot show all of it – Nguyen Khoi Nov 21 '16 at 17:31
  • Don't use `StringIO` - use [dump_stats](http://docs.python.org/2/library/profile.html#profile.Profile.dump_stats). – ekhumoro Nov 21 '16 at 17:45
  • Should I follow this http://stackoverflow.com/questions/29630667/how-can-i-analyze-a-file-created-with-pstats-dump-statsfilename-off-line ? or https://julien.danjou.info/blog/2015/guide-to-python-profiling-cprofile-concrete-case-carbonara – Nguyen Khoi Nov 21 '16 at 18:20
  • In that case, it's probably simpler to do `open('stats.txt', 'w').write(s.getvalue())`. – ekhumoro Nov 21 '16 at 18:25
  • https://github.com/shaolinkhoa/tensorflow/blob/master/time_chart.txt It seems the code slow because of this command `predictions = sess.run(softmax_tensor, \ {'DecodeJpeg/contents:0': image_data})` – Nguyen Khoi Nov 21 '16 at 21:31
  • According to [this](http://deeplearning4j.org/compare-dl4j-torch7-pylearn#tensorflow), tensorflow can be slow sometimes, because parts of it are implemented in pure python. – ekhumoro Nov 21 '16 at 22:11

1 Answers1

0

This sounds like your session graph grows and grows with each iteration. Try to put all your code until the result into a 'with' statement context:

# example function using 'with' context:

some_Function(param1, param2):
    with tf.Graph().as_default():
        ...
        graph = ...
        ...
        sess = tf.Session()
        result = sess.run(graph)
        return result
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Steve H.
  • 1
  • 2