Skip to content
Jupyter Notebook Python
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
data in case target folder is empty, also init download Oct 19, 2019
test Update to Keras2 api Sep 1, 2017
train Use SGD instead of Adam Mar 28, 2018
.gitignore First commit Jan 19, 2017
LICENSE Add License Jan 19, 2017 add requirements.txt Oct 19, 2019 Handle upper-case extensions Jun 29, 2018
requirements.txt add requirements.txt Oct 19, 2019 Fix rotation of rectangular images Mar 25, 2018


This repository contains the code necessary to train and test convolutional neural networks (CNNs) for predicting the rotation angle of an image to correct its orientation. There are scripts to train two models, one on MNIST and another one on the Google Street View dataset. Since the data for this application is generated on-the-fly, you can also train using your own images in a similar way. A detailed explanation of the code and motivation for this project can be found in my blog.


The code mainly relies on Keras to train and test the CNN models, and OpenCV for image manipulation.

You can install all the required packages using pip: pip install -r requirements.txt

The recommended way to use Keras is with the TensorFlow backend. If you want to use it with the Theano backend you will need to make some minor modifications to the code to make it work.


Run either python train/ to train on MNIST or python train/ to train on the Google Street View dataset. Note that the first time you run the scripts will take longer since the datasets will be automatically downloaded. Also, you will need a decent GPU to train the ResNet50 model that is used in, otherwise it will take quite long to finish.

If you only want to test the models, you can download pre-trained versions here.

Note also that the regression models ( and don't provide a good accuracy and are only included for illustration purposes.


You can evaluate the models and display examples using the provided Jupyter notebooks. Simply run jupyter notebook from the root directory and navigate to test/test_mnist.ipynb or test/test_street_view.ipynb.

Finally, you can use the script to correct the orientation of your own images. You can run it as follows:

python <path_to_hdf5_model> <path_to_input_image_or_directory>

You can also specify the following command line arguments:

  • -o, --output to specify the output image or directory.
  • -b, --batch_size to specify the batch size used to run the model.
  • -c, --crop to crop out the black borders after rotating the images.
You can’t perform that action at this time.