r/chess•
aladdinator

Created /u/ChessFenBot, a machine learning bot that predicts FENs from screenshots posted to /r/chess

Hi all,

What

u/ChessFenBot will reply to r/chess image posts with online chessboard screenshots with a predicted FEN and lichess analysis link.

I'm making this post as a place for people to direct comments/complaints/suggestions about the bot.


Why

I noticed whenever a puzzle or question was posted with an image of the board, I usually wanted to pull it up on an online chess analysis engine (I use lichess).

After maybe the 10th time doing that I was frustrated and took this as an opportunity to learn TensorFlow and apply some basic computer vision and machine learning techniques to predict the FEN from an online chessboard screenshot.

It's definitely not right all the time, so sorry about that, it'll post how certain it is about the result as well. Anything under 90% has probably got issues. If you reply to one of those with the correct FEN or lichess/editor link I'll have a script that pulls them into the training dataset.


How

Here's the Github with code and documentation. I tried to make it more accessible by documenting the learning process in 4 IPython notebooks with fun images.

Here is u/ChessFenBot 's first post! Cheers, hope it'll be helpful to some of you in the community!

P.S. Happy Valentines Day


^(Update: Bot is live Account is just new, mods say comments should show up in 24h)

^(Some of you may not be able to see the posts in r/chess by u/ChessFenBot because it )^(may need minimimum karma) ^(to post on r/chess )

^(Trying to get this resolved, if it is karma and you want to help, ChessFenBot posted about itself on )^(r/MachineLearning)

Posted Feb 15, 2016, 1:21 AM UTC · edited

9728
mattbrah1700s | shitty chess engine developer•

You're the real MVP.

11
Phyisis•

Excellent job, I can see this being immensely useful to the sub. However, it seems that for now its comments are hidden and I can only see them on the bot's user page which is unfortunate. Not sure if this is for everyone or if its RES or something on my end.

edit: its possible r/chess requires a minimum karma threshold to post, and if it only ever posts here it will never reach that comment threshold.

7
aladdinatorOP•

final edit: 24h passed, bot is visible to all now, cheers.

Thanks for bringing it to my attention, where did you see that requirement?

edit: Messaged the mods, if it is karma I decided to let u/ChessFenBot make a post describing itself on /r/MachineLearning

edit: Mod replied, account is just new, should be fine after a day, was nice enough to approve current batch of comments

5
Hennns•

I can't see it either, try shooting the mods a message. (also, does the bot currently handle the board from only white's side, or both sides?)

http://www.reddit.com/message/compose?to=%2Fr%2Fchess

2
aladdinatorOP•

Thanks, messaged the mods.

The bot will scan the board as if it is white side first. It then checks if the title has 'black to play' in it, and if so it flips the order.

I'm in the process of adding this so some may be showing the wrong side still edit : Done

2
Hennns•

hm, maybe just check for 'black' or 'white'. That way you'd cover a lot of different ways, like 'black to move' 'black mates in X' 'white to draw' 'white to win' etc. A title will practically never have a color in it if it's not that side's turn.

Anyway, you're already doing god's work :)

1
Xiver1972•

Why not just have two links? White to play and black to play?

1
Phyisis•

Oh sorry I couldn't confirm that, it's just a suspicion. I'm sure a mod will poke by at some point.

1
ornicar2lichess.org dictator•

Fascinating. Note that a link to lichess analysis might be more useful than to the editor. Just replace the FEN spaces with underscores to build the URL. Example:

http://lichess.org/analysis/rnbqk2r/ppp1ppbp/3p1np1/8/2PP4/2N2NP1/PP2PP1P/R1BQKB1R_b_KQkq_-

5
aladdinatorOP•

Hey, yep it links back to lichess analysis, it also links to editor, I may not have been clear on that, updated title

3
TheRagingNerd•

Nifty, thanks for sharing.

2
statsjunkie•

I wonder if you could use a camera connected to a chess clock (to capture a picture after every move) and auto-annotate a game. Then use the difference between pictures to increase accuracy.

Obviously there would be problems in low time situations where people would get in the way, but it still might be useful.

2
aladdinatorOP•

I'd love to be able to track OTB games with an overhead webcam. The vision is much more challenging. Starting from known starting position and track movement of pieces would probably work better than predicting pieces.

Not a simple problem, lighting, occlusion, webcam framerate, to name a few. It'd not be very robust at first and would slow down OTB play.

Potentially recording video and processing later on, could use heavier algorithms, easier to share results as well, that'd be somewhat fun actually. Generalizing would still be difficult. Not sure if there is an audience for it, I don't do OTB games often so it'd be hard to test this.

1
statsjunkie•

I found a repo on github that did it from the side. They found the exact angle that had the best angle for their algorithm. They started by finding all of the squares of the board, then using the average color of the square to determine whether a piece is on the square or not (or just covered from an adjacent piece). It would then calculate the most likely move to get to that configuration. Obviously, it wasn't right all the time, but it had a mean time to fail at like 50 moves. That's pretty good in my estimation. It also eliminated the need for learning the piece shapes, or anything like that.

I couldn't figure out where they found all the dependencies and the owner of the repo wasn't responsive. Would love to try and resurrect that though.

1
ozerioss•

Nice job

2
Hexofin1350ish•

This is fantastic!

2
blahs44English defence•

Nice!

2
isolatedqpawnSometimes weak, but dangerous when pushed!•

Very cool.

2
dendiz•

cool idea - are there any apps that do the same for printed material?

2
Sicilian-Dragon~1900 USCF•

Very cool, thank you! Although yeah, I'm also having the issue of the comments being hidden (I do have RES also).

1
fischerandchips•

i'm looking at some of its posts. Perhaps make some adjustments when it detects unlikely pieces? if it sees black has two light square bishops or white has two of each bishop, for example. http://www.fen-to-image.com/image/30/11111111/1111B111/bBK11Nr1/11111111/11b1B1B1/p11k1111/1p111p11/11111111.png

1
aladdinatorOP•

Yeah! I've been considering some fun directions to go with this one, right now the neural network only sees a single tile with no knowledge of other tiles or where on the board it is when it makes it's prediction.

Another layer on that would be to train the model based on which tile we're looking at (for example files 2 tend to have white pawns most of the time).

A top layer that takes the probabilities for each tiles, and makes decisions like no two black bishops on same color into account, it could be trained for each variant of chess.

These are fun, I hope to go along this direction next. Thanks for the suggestion

1
jlobesFilthy Casual•

Are you accepting pull requests?

1
aladdinatorOP•

Sure depending on what you are doing, I try to catch up with this on weekends

1
phr00tbr00tn00b•

Would it be possible to use this software to generate FENs from a local directory of image files?

1
aladdinatorOP•

Yes, though for someone else to do it they'll need the neural network model structure (a gig or 2 I think), which I need to upload somewhere first, and then modify a python script to point to a file instead of an online link.

If you're comfortable enough to do the modification, I can upload the model structure for the neural net on drive and share it with you. Otherwise I'll post an issue and try to put together a setup so people can do what you suggest at some point when I find time.

2
phr00tbr00tn00b•

What I want to do is take screenshots of diagrams in ebooks, save them all in a directory, then batch process all images in a directory, output the FENs to a txt file.

I have only limited programming experience, but I should be capable of modifying a python script to look at files, loop through a directory and send output to txt file.

Given that the images won't be from lichess or chess.com, will I be able to train the bot to read the common style of diagrams found in ebooks? If yes, it would be great if you can share the model structure!

1
aladdinatorOP•

I went ahead and updated the code so one can run it for a local image file like so:

./tensorflow_chessbot.py --filepath some_pic.png

which would print out:

Predicted FEN: 11111111/11111p11/11111k1P/11p1111P/1p1p1111/11111111/111K1111/11111111

I put up the instructions on the tensorflow_chessbot readme here

You'll need a linux OS for now (such as ubuntu) to install tensorflow, though I've seen some people successfully install it on windows. Once you have that running it should be enough to start modifying the python code.

2
phr00tbr00tn00b•

Thanks! I'll have time to set it up and test it out tomorrow.

1
Pimozv1700 @ lichess•

Dude couldn't that be used by cheaters? IMHO it'd be much better if people could just post FENs instead of pictures.

-6
CamelYouBastardAronian wins•

There are already existing programs used by cheaters based on this idea. It's not like they need to copy this one.

1