Detailed review of TensorFlow CIFAR-10 tutorial, Part 2 [Click here for Part 1]
Execution process of ‘python cifar10_train.py’
- Initialising flags and constants from cifar10.py and cifar10_input.py:
- data_dir
- batch_size
- use_fp16
- IMAGE_SIZE
- NUM_CLASSES
- NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN
- NUM_EXAMPLES_PER_EPOCH_FOR_EVAL
- MOVING_AVERAGE_DECAY
- NUM_EPOCHS_PER_DECAY
- LEARNING_RATE_DECAY_FACTOR
- INITIAL_LEARNING_RATE
- Detailed description of all flags and constants: CIFAR-10 TensorFlow tutorial review, part 1
- Downloading and unpacking a ‘cifar-10-binary.tar.gz‘ dataset.
- Calling the ‘cifar10_train.py‘ with default parameters.
- Setting up the required variables.
- Calling ‘distorted_inputs()‘ from ‘cifar10_input.py‘
- Preparing images and labels from the downloaded dataset .
- Filling the queue with 20’000 CIFAR images.
- Generating images and labels batch (shape: 128, 24, 24, 3; dtype:float32)
- Calling ‘distorted_inputs()‘ from ‘cifar10.py‘ to construct distorted input for CIFAR training.
- Returns images to ‘cifar10_train.py/train()’
- Building CIFAR-10 model ‘inference(images)‘ in ‘cifar10.py‘ with following layers:
- conv1
- pool1
- norm1
- conv2
- norm2
- pool2
- local3
- local4
- softmax
- Defines ‘weight_decay‘ for each layer (tf.Variable).
- Assigns the ‘loss_summaries‘ for each layer.
- Builds and applies the gradients.
- Builds histograms of weights and biases for each layer.
- Creating a saver function, to be able to recover from checkpoints later.
- Builds the summary operation.
- Creates and starts the ‘tf.Session’.
- Starting the queue runners, begins the training loop.
Prediction (CIFAR-10 TensorFlow evaluation example outputs labels and probabilities)
Labels and probabilities output for single image of CIFAR-10 TensorFlow tutorial:
from PIL import Image
We will use the Python Imaging Library (PIL) to process images into JPEG files supported by out mode without additional losses.
width = 24 height = 24 categories = [ "airplane","automobile","bird","cat","deer","dog","frog","horse","ship","truck" ] filename = "/tmp/images/dog1.png" # absolute path to input image
im = Image.open(filename) im.save(filename, format='JPEG', subsampling=0, quality=100)
Converting the file from input format to JPEG with 100% quality and without subsampling.
input_img = tf.image.decode_jpeg(tf.read_file(filename), channels=3) tf_cast = tf.cast(input_img, tf.float32)
Decoding image into 3 channels (red, green, blue) float32 array.
float_image = tf.image.resize_image_with_crop_or_pad(tf_cast, height, width)
Reshaping image.
images = tf.expand_dims(float_image, 0)
Creating a batch of images with just one image.
logits = cifar10.inference(images)
Get logits from main model (cifar10.py).
_, top_k_pred = tf.nn.top_k(logits, k=5)
Get top 5 predicions (‘top_k_pred’) and their values (‘_’).
init_op = tf.initialize_all_variables()
Initialising variables.
with tf.Session() as sess: sess.run(init_op) _, top_indices = sess.run([_, top_k_pred]) for key, value in enumerate(top_indices[0]): print (categories[value] + ", " + str(_[0][key]))
Runing tf.Session() and processing the output.
Example of input:
Example of output: