Setting up input pipelines for training and testing

TensorFlow allows us to create a reliable input pipeline for quick and easy training. In this section, we will implement tf.TextLineReader to read the train and test text files. We will use tf.train.batch to read and preprocess images in parallel.

First, we need to create a new Python file named datasets.py in the project directory and add the following code:

    import tensorflow as tf 
    import os 
 
    def load_files(filenames): 
    filename_queue = tf.train.string_input_producer(filenames) 
    line_reader = tf.TextLineReader() 
    key, line = line_reader.read(filename_queue) 
    label, image_path = tf.decode_csv(records=line, 
                                         
    record_defaults=[tf.constant([], dtype=tf.int32),   
    tf.constant([], dtype=tf.string)], 
                                      field_delim=' ') 
    file_contents = tf.read_file(image_path) 
    image = tf.image.decode_jpeg(file_contents, channels=3) 
 
    return image, label 

In the load_files method, we use the tf.TextLineReader to read each line of the text file, such as trainval.txt, test.txt. tf.TextLineReader needs a queue of strings to read, so we use tf.train.string_input_producer to store the filenames. After that, we pass the line variable into tf.decode_cvs in order to get the label and filename. The image can be easily read with tf.image.decode_jpeg.

Now that we can load the image, we can move forward and create image batches and label batches for training.

In datasets.py, we need to add a new method:

 def input_pipeline(dataset_dir, batch_size, num_threads=8,   
    is_training=True, shuffle=True): 
    if is_training: 
        file_names = [os.path.join(dataset_dir, "trainval.txt")] 
    else: 
        file_names = [os.path.join(dataset_dir, "test.txt")] 
    image, label = load_files(file_names) 
 
    image = preprocessing(image, is_training) 
 
    min_after_dequeue = 1000 
    capacity = min_after_dequeue + 3 * batch_size 
    if shuffle: 
     image_batch, label_batch = tf.train.shuffle_batch( 
     [image, label], batch_size, capacity,  
     min_after_dequeue, num_threads 
      ) 
    else: 
        image_batch, label_batch = tf.train.batch( 
            [image, label], batch_size, num_threads, capacity 
            ) 
    return image_batch, label_batch

We first load the image and label with the load_files method. Then, we pass the image through a new preprocessing method, which we will implement shortly. Finally, we pass the image and label into tf.train.shuffle_batch for training and tf.train.batch for testing:

 def preprocessing(image, is_training=True, image_size=224,  
resize_side_min=256, resize_side_max=312): image = tf.cast(image, tf.float32) if is_training: resize_side = tf.random_uniform([], minval=resize_side_min,
maxval=resize_side_max+1, dtype=tf.int32) resized_image = _aspect_preserving_resize(image,
resize_side) distorted_image = tf.random_crop(resized_image, [image_size,
image_size, 3]) distorted_image =
tf.image.random_flip_left_right(distorted_image) distorted_image =
tf.image.random_brightness(distorted_image, max_delta=50) distorted_image = tf.image.random_contrast(distorted_image,
lower=0.2, upper=2.0) return distorted_image else: resized_image = _aspect_preserving_resize(image, image_size) return tf.image.resize_image_with_crop_or_pad(resized_image,
image_size, image_size)

There are two different approaches to preprocessing in training and testing. In training, we need to augment data to create more training data from the current dataset. There are a few techniques that are used in the preprocessing method:

  • The images in the dataset can have different image resolutions, but we only need 224x224 images. Therefore, we need to resize the image to a reasonable size before performing random_crop. The following diagram describes how cropping works. The _aspect_preserving_resize method will be implemented shortly:
  • After cropping the image, we pass the image through tf.image.random_flip_left_right, tf.image.random_brightness and tf.image.random_contrast to distort the image and create a new training sample.
  • In the testing routine, we only need to resize the image with _aspect_preserving_resize and tf.image.resize_image_with_crop_or_pad. tf.image.resize_image_with_crop_or_pad allows us to crop centrally or pad the image to the target width and height.

Now, we need to add the last two methods into datasets.py, as shown here:

    def _smallest_size_at_least(height, width, smallest_side): 
      smallest_side = tf.convert_to_tensor(smallest_side,   
      dtype=tf.int32) 
 
      height = tf.to_float(height) 
      width = tf.to_float(width) 
      smallest_side = tf.to_float(smallest_side) 
 
      scale = tf.cond(tf.greater(height, width), 
                    lambda: smallest_side / width, 
                    lambda: smallest_side / height) 
      new_height = tf.to_int32(height * scale) 
      new_width = tf.to_int32(width * scale) 
      return new_height, new_width 
 
 
    def _aspect_preserving_resize(image, smallest_side): 
      smallest_side = tf.convert_to_tensor(smallest_side,   
      dtype=tf.int32) 
      shape = tf.shape(image) 
      height = shape[0] 
      width = shape[1] 
      new_height, new_width = _smallest_size_at_least(height, width,   
      smallest_side) 
      image = tf.expand_dims(image, 0) 
      resized_image = tf.image.resize_bilinear(image, [new_height,   
      new_width], align_corners=False) 
      resized_image = tf.squeeze(resized_image) 
      resized_image.set_shape([None, None, 3]) 
      return resized_image 

Up to this section, we had to do a lot of work to prepare the dataset and input pipelines. In the following section, we will define the model for our dataset, loss, accuracy and training operations to perform the training routine.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.7.208