Post

Tensorflow item recognition

Leveraging Google’s Tensorflow Machine Learning libraries for item recognition in images is fantastically easy to get going. The below Dockerfile will setup a container with everything required and allow the user to feed a URL to a file for classification:

Dockerfile:
Download raw from here: https://pastebin.com/raw/mdJ225vp
FROM ubuntu:16.04

RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo ‘root:tensorflow’ | chpasswd
RUN sed -i ‘s/PermitRootLogin prohibit-password/PermitRootLogin yes/’ /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed ‘s@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g’ -i /etc/pam.d/sshd

ENV NOTVISIBLE “in users profile”
RUN echo “export VISIBLE=now” » /etc/profile

EXPOSE 22
CMD [“/usr/sbin/sshd”, “-D”]

RUN apt-get install -y python-pip git
RUN pip install numpy tensorflow

RUN git clone https://github.com/jonas-werner/tf_item_recognition.git
Save the above into a file called “Dockerfile”.
Enter the directory where the Dockerfile is saved and build the Docker image:
docker build -t tf-image_recognition .
Verify the Docker image:
jonas@continuity:~/CODE/tf-image_recognition$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
tf-image_recognition latest cd28f91139a2 6 minutes ago 1.07GB
Run the image. We’ll expose SSH on port 22 on the container as 2222 on the host:
docker run -it -d -P -p 2222:22 tf-image_recognition
Verify the local Docker gateway IP using the container ID (81f13360885f in this case – use “docker ps” to find out):
jonas@continuity:~/CODE/tf-image_recognition$ docker inspect 81f13360885f | grep Gateway
“Gateway”: “172.17.0.1”,
“IPv6Gateway”: “”,
“Gateway”: “172.17.0.1”,
“IPv6Gateway”: “”,
SSH and execute the image classification script (password: “tensorflow”):
ssh root@172.17.0.1 -p 2222 “wget https://jonamiki.com/wp-content/uploads/2013/07/wpid-IMAG0438_1.jpg; python /tf_item_recognition/classify_image.py –image_file ~/*.jpg; rm ~/*.jpg”
This is the image we’ve pulled down:

And this is the classification result:
motor scooter, scooter (score = 0.89661)
moped (score = 0.04356)
disk brake, disc brake (score = 0.00290)
crash helmet (score = 0.00289)
snowmobile (score = 0.00216)
Not too bad 🙂 Tensorflow accurately detects that the image contains a scooter, a crash helmet and even sees the disk brake on the scooter! Try with any image URL to see what Tensorflow will classify your image as. Have fun!

This post is licensed under CC BY 4.0 by the author.