Tutorial of an ML pipeline for detecting metastatic cancer

 

Identify metastatic cancer in small image patches taken from larger digital pathology scans

November 29, 2018, Joni Juvonen

 

Generate 3D models with a generative adversarial network

 

Generating 3D objects is surprisingly hard for generative adversarial networks (GAN) but with the recent relativistic cost functions, I managed to get stable training and relatively good results. With this project, you can train your own model that produces 64 x 64 x 64 3D models.

The resulting 3D models can be 3D printed as nice AI decorations.

3D printing AI-generated objects

July 27, 2018, Joni Juvonen

 

TensorFlow - Installing on Windows

 

TensorFlow™ is an open source machine learning library originally developed by the Google Brain team. It was released as an open source project in the late 2015 and it has become the most popular machine learning library since then.

The main TensorFlow API is composed of a different python modules but after the release of version 1.0 in the early 2017, APIs for Java and Go have been added. It is currently possible to use TensorFlow in desktop Windows (CPU and GPU processing), Linux, macOS, Android and iOS.

TensorFlow has very detailed and up-to-date instructions on Windows installation. However, I collected the main steps here with additional comments about some issues that I encountered.

If you have relatively new NVIDIA Graphics card with a CUDA compute capability of at least 3.0, you could install the GPU accelerated version. The list of NVIDIA graphics card compute capabilities. Note that this option requires a free NVIDIA developer account.

GPU installation additional steps

You need to install CUDA® Toolkit 8.0 and the associated drivers for your graphics card.
//docs.nvidia.com/cuda/cuda-installation-guide-microsoft-windows///developer.nvidia.com/cuda-downloads

Get cuDNN v.6.1 library (requires a free NVIDIA developer account to download).
//developer.nvidia.com/cudnn

Extract the zip to your desired location and add the bin folders path to the environment %PATH% variables. You can do this in console by typing:

set PATH=%PATH%;C:\<cuDNN-bin-dir-location>

Installing with native pip

Make sure the latest 64 bit python is installed and that Python commands are added to environment variables. 
//www.python.org/downloads/release/latest/

Enter terminal as administrator and run (for default installation):

pip3 install --upgrade tensorflow

And for GPU installation:

pip3 install --upgrade tensorflow-gpu

Validate TensorFlow installation with this python script. Enter in terminal and type:

python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

You should see a greeting text if everything works but if you encounter errors, see the most common issues here.

April 15, 2017 (updated December 2, 2017), Joni Juvonen

 

TensorFlow - Retrain image classifier on Windows

 

TensorFlow API is capable of image classifications through Inception-v3 image recognition engine. It works by a convolutional neural network that can achieve reasonable performance on hard visual recognition tasks. The number of objects this engine recognizes are limited as it only knows the classes from the industry standard ImageNet training set. However, TensorFlow can be taught completely new classes by training the last layer of Inception's classification by introducing large batches of new sample images.

TensorFlow has a well explained tutorial on this subject here but it requires the use of a Bazel compile tool that is currently not officially supported in Windows (highly experimental version available). The Bazel steps can be bypassed by downloading the required python script form the official source.

 
Install TensorFlow if you have not done it already

Collect sample images

Create an empty working directory (for example 'C:\TensorFlowDemo') for the retraining and create a 'classes' subfolder. Then, create subfolder for each training class (e.g. cat, dog, ...) to the 'classes' folder. Place your training images for these folders. The more images the better results.

A useful tip from Siraj Raval's tutorial was to use "Fatkun Batch Download Image" Chrome extension to download all result images from a Google image search.

Retrain image classification

Clone or download the git project of TensorFlow.

Open terminal and navigate to the TensorFlow's image_retraing example directory: ...\tensorflow-master\tensorflow\examples\image_retraining

Run retrain.py script in python with the following flags:

python retrain.py \
--bottleneck_dir=C:\TensorFlowDemo\bottlenecks \
--how_many_training_steps 500 \
--model_dir=C:\TensorFlowDemo\inception \
--output_graph=C:\TensorFlowDemo\retrained_graph.pb \
--output_labels=C:\TensorFlowDemo\retrained_labels.txt \
--image_dir C:\TensorFlowDemo\classes

 

This may take some time depending mostly on the amount of training steps and image count. For ~200 images and 500 steps this task took about 2 minutes with a GPU accelerated TensorFlow (GeForce GTX 1080).

Get a script to classify new images

Now you would need to compile label_image TensorFlow example with Bazel but as you only need to use a compiled label_image.py python script, you can download it from a compiled source.

Open the label_image.py with a text editor and replace the label_lines path to
C:\TensorFlowDemo\retrained_labels.txt
The line should look like:

label_lines = [line.rstrip() for line in tf.gfile.GFile(r'C:\TensorFlowDemo\retrained_labels.txt')]

Replace also the graph path to
C:\TensorFlowDemo\retrained_graph.pb
This should look like:

with tf.gfile.FastGFile(r'C:\TensorFlowDemo\retrained_graph.pb', 'rb') as f:

Then save and exit.

Test your retrained image classification model

Open terminal and run label_image.py script in python with a test image as your parameter:

python label_image.py C:\TensorFlowDemo\test.jpg

This should output a confidence value between 0 and 1 for each of your categories. e.g.
cat(score = 0.985)
dog(score = 0.015)

cat (score = 0.98524)

April 17, 2017, Joni Juvonen

TensorFlow, the TensorFlow logo and any related marks are trademarks of Google Inc.