Step 2 – Loading the training/test images to generate train/test set

We set the number of color channels as 3 for the images. In the previous section, we have seen that it should be 1 for grayscale images:

num_channels = 3 

For the simplicity, we assume the image dimensions should be squares only. Let's set the size to be 128:

img_size = 128 

Now that we have the image size (that is, 128) and the number of the channel (that is, 3), the size of the image when flattened to a single dimension would be the multiplication of the image dimension and the number of channels, as follows:

img_size_flat = img_size * img_size * num_channels 

Note that, at a later stage, we might need to reshape the image for the max pooling and convolutional layers, so we need to reshape the image. For our case, it would be the tuple with height and width of images used to reshape arrays:

img_shape = (img_size, img_size)  

We should have explicitly defined the labels (that is, classes) since we only have the raw color image, and so the images do not have the labels like other numeric machine learning dataset, have. Let's explicitly define the class info as follows:

classes = ['dogs', 'cats']  
num_classes = len(classes) 

We need to define the batch size that needs to be trained on our CNN model later on:

batch_size = 14  

Note that we also can define what portion of the training set will be used as the validation split. Let's assume that 16% will be used, for simplicity:

validation_size = 0.16  

One important thing to set is how long to wait after the validation loss stops improving before terminating the training. We should use none if we do not want to implement early stopping:

early_stopping = None   

Now, download the dataset and you have to do one thing manually: separate the images of dogs and cats and place them in two separate folders. To be more specific, suppose you put your training set under the path /home/DoG_CaT/data/train/. In the train folder, create two separate folders dogs and cats but only show the path to DoG_CaT/data/train/. We also assume that our test set is in the /home/DoG_CaT/data/test/ directory. In addition, you can define the checkpoint directory where the logs and model checkpoint files will be written:

train_path = '/home/DoG_CaT/data/train/' 
test_path = '/home/DoG_CaT/data/test/' 
checkpoint_dir = "models/" 

Then we start reading the training set and prepare it for the CNN model. For processing the test and train set, we have another script Preprocessor.py. Nonetheless, it would be better to prepare the test set as well:

data = Preprocessor.read_train_sets(train_path, img_size, classes, validation_size=validation_size) 

The preceding line of code reads the raw images of cats and dogs and creates the training set. The read_train_sets() function goes as follows:

def read_train_sets(train_path, image_size, classes, validation_size=0): 
  class DataSets(object): 
      pass 
      data_sets = DataSets() 
      images, labels, ids, cls = load_train(train_path, image_size, classes) 
      images, labels, ids, cls = shuffle(images, labels, ids, cls) 
if isinstance(validation_size, float): validation_size = int(validation_size * images.shape[0]) validation_images = images[:validation_size] validation_labels = labels[:validation_size] validation_ids = ids[:validation_size] validation_cls = cls[:validation_size] train_images = images[validation_size:] train_labels = labels[validation_size:] train_ids = ids[validation_size:] train_cls = cls[validation_size:] data_sets.train = DataSet(train_images, train_labels, train_ids, train_cls) data_sets.valid = DataSet(validation_images, validation_labels, validation_ids, validation_cls) return data_sets

In the previous code segment, we have used the method load_train() to load the images which is an instance of a class called DataSet:

def load_train(train_path, image_size, classes): 
    images = [] 
    labels = [] 
    ids = [] 
    cls = [] 
 
    print('Reading training images') 
    for fld in classes:    
        index = classes.index(fld) 
        print('Loading {} files (Index: {})'.format(fld, index)) 
        path = os.path.join(train_path, fld, '*g') 
        files = glob.glob(path) 
for fl in files: image = cv2.imread(fl) image = cv2.resize(image, (image_size, image_size), cv2.INTER_LINEAR) images.append(image) label = np.zeros(len(classes)) label[index] = 1.0 labels.append(label) flbase = os.path.basename(fl) ids.append(flbase) cls.append(fld)
images = np.array(images) labels = np.array(labels) ids = np.array(ids) cls = np.array(cls) return images, labels, ids, cls

The DataSet class, which is used to generate the batches of the training set, is as follows:

class DataSet(object): 
   
  def next_batch(self, batch_size): 
    """Return the next `batch_size` examples from this data set.""" 
    start = self._index_in_epoch 
    self._index_in_epoch += batch_size 
    if self._index_in_epoch > self._num_examples: 
      # Finished epoch 
      self._epochs_completed += 1 
      start = 0 
      self._index_in_epoch = batch_size 
      assert batch_size <= self._num_examples 
    end = self._index_in_epoch 
    return self._images[start:end], self._labels[start:end], self._ids[start:end], self._cls[start:end] 

Then, similarly, we prepare the test set from the test images that are mixed (dogs and cats):

test_images, test_ids = Preprocessor.read_test_set(test_path, img_size) 

We have the read_test_set() function for ease, as follows:

def read_test_set(test_path, image_size): 
  images, ids  = load_test(test_path, image_size) 
  return images, ids 

Now, similar to the training set, we have a dedicated function called load_test () for loading the test set, which goes as follows:

def load_test(test_path, image_size): 
  path = os.path.join(test_path, '*g') 
  files = sorted(glob.glob(path)) 
 
  X_test = [] 
  X_test_id = [] 
  print("Reading test images") 
  for fl in files: 
      flbase = os.path.basename(fl) 
      img = cv2.imread(fl) 
      img = cv2.resize(img, (image_size, image_size), cv2.INTER_LINEAR) 
      X_test.append(img) 
      X_test_id.append(flbase) 
X_test = np.array(X_test, dtype=np.uint8) X_test = X_test.astype('float32') X_test = X_test / 255 return X_test, X_test_id

Well done! We can now see some randomly selected images. For this, we have the helper function called plot_images(); it creates a figure with 3 x 3 sub-plots. So, all together, nine images will be plotted, along with their true label. It goes as follows:

def plot_images(images, cls_true, cls_pred=None): 
    if len(images) == 0: 
        print("no images to show") 
        return  
    else: 
        random_indices = random.sample(range(len(images)), min(len(images), 9))         
        images, cls_true  = zip(*[(images[i], cls_true[i]) for i in random_indices])     
    fig, axes = plt.subplots(3, 3) 
    fig.subplots_adjust(hspace=0.3, wspace=0.3) 
    for i, ax in enumerate(axes.flat): 
        # Plot image. 
        ax.imshow(images[i].reshape(img_size, img_size, num_channels)) 
        if cls_pred is None: 
            xlabel = "True: {0}".format(cls_true[i]) 
        else: 
            xlabel = "True: {0}, Pred: {1}".format(cls_true[i], cls_pred[i]) 
        ax.set_xlabel(xlabel)         
        ax.set_xticks([]) 
        ax.set_yticks([])     
    plt.show() 

Let's get some random images and their labels from the train set:

images, cls_true  = data.train.images, data.train.cls 

Finally, we plot the images and labels using our helper-function in the preceding code:

  
plot_images(images=images, cls_true=cls_true) 

The preceding line of code generates the true labels of the images that are randomly selected:

Figure 6: The true labels of the images that are randomly selected

Finally, we can print the dataset statistics:

print("Size of:") 
print("  - Training-set:tt{}".format(len(data.train.labels)))  
print("  - Test-set:tt{}".format(len(test_images))) 
print("  - Validation-set:t{}".format(len(data.valid.labels))) 
>>>
Reading training images
Loading dogs files (Index: 0)
Loading cats files (Index: 1)
Reading test images
Size of:
- Training-set: 21000
- Test-set: 12500
- Validation-set: 4000
..................Content has been hidden....................

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