Awesome
Deep Sudoku Solver
A Sudoku Solver that leverages TensorFlow and BNNS of iOS 10 SDK for deep learning.
<img src="https://cloud.githubusercontent.com/assets/2617118/24401396/d3673e58-13e6-11e7-953e-99be889f08c9.png" width="300">Usage
- Add a picture of sudoku puzzle from the photo library.
- Import the picture into a puzzle.
- Edit any digits not correctly predicted.
- Solve the puzzle.
- Save the solution to the photo library or share it to SNS or other apps.
Development
The steps below illustrate how to prepare your own training data for TensorFlow and use the training results for prediction in BNNS of iOS 10 SDK.
Exporting Models from TensorFlow
I use the Chars74K image dataset for training. My trained models are in the folder Assets.xcassets with the filename model-h#[b|w]-*.dataset
. To train your own models, follow the steps below.
- Resize your images to dimension of 28x28.
- Label your images and split them into training and testing data.
- Convert your data into the MNIST format using JPG-PNG-to-MNIST-NN-Format
- Install TensorFlow
- Put your data
[train|test]-[images|labels]-idx[1|3]-ubyte.gz
in the scripts folder. - Run
mnist-predict-from-model.py
to get the models. - Import the models to the Asset Catalog in XCode.
Predicting Labels from Images Using BNNS
- Add the following files to your project.
- Use the following script for prediction.
let magic = ImageMagic()
let ai = MnistNet()
guard let data = magic.mnistData(image: UIImage)
else {
return
}
let predicted: Int = ai.predict(input: data)
// Process your predicted label
Refer to the function importSudoku()
in ViewController.swift for a full example.
Credit
I reused soruce code and configurations from:
- Using TensorFlow models in iOS BNNS
- Tutorial on UICollectionViewCell
- Creating a grid of UITextField in UICollectionView
- Using CIAreaAverage to compute the average color of a CIImage
- Converting a color image to gray scale
- Tutorial on UIActivitiyViewController
- Tutorial on moving a view up when covered by the keyboard
- Swift implementation of Peter Norvig's constraint based sudoku solver