image_output_1.png
import matplotlib.image as mpimg

import matplotlib.pyplot as plt

import numpy as np

import os

import pathlib

import random

def plot_loss_curves(history):
    """
      Returns separate loss curves for training and validation metrics.
      Args:
        history: TensorFlow model History object (see: https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/History)    
    """
    loss = history.history['loss']
    val_loss = history.history['val_loss']
    acc = history.history['accuracy']
    val_acc = history.history['val_accuracy']
    epochs = range(len(history.history['loss']))

def compare_historys(original_history, new_history, initial_epochs=5):
    """
    Compares two TensorFlow model History objects.
    Args:
      original_history: History object from original model (before new_history)
      new_history: History object from continued model training (after original_history)
      initial_epochs: Number of epochs in original_history (new_history plot starts from here) 
    """
    acc = original_history.history['accuracy']
    loss = original_history.history['loss']
    val_acc = original_history.history['val_accuracy']
    val_loss = original_history.history['val_loss']
    total_acc = acc + new_history.history['accuracy']
    total_loss = loss + new_history.history['loss']
    total_val_acc = val_acc + new_history.history['val_accuracy']
    total_val_loss = val_loss + new_history.history['val_loss']

def walk_through_dir(dir_path):
    """
    Walks through dir_path returning its contents.
    Args:
        dir_path (str): target directory
    Returns:
        A print out of:
            number of images (files) in each subdirectory
            name of each subdirectory
    """

# Plot Some Random Images 
data_dir = pathlib.Path('101_food_classes_10_percent/train')
class_names = np.array(sorted([item.name for item in data_dir.glob('*')]))

def view_rand_img(target_dir):
    plt.figure(figsize = (12,8)) # create the figure size
    
    for i in range(12): # loop to show 12 images at a time
        
        ax = plt.subplot(3, 4, i+1) # show the chosen 12 images in a 3 * 4 grid
        
        rand_class = random.choice(class_names) # choose a random class
        target_folder = '101_food_classes_10_percent/' + target_dir + "/" + rand_class # create the directory to the images
        
        rand_img = random.sample(os.listdir(target_folder), 12) # choose the 12 images randomly
        img = mpimg.imread(target_folder + "/" + rand_img[i]) # read the images
        plt.imshow(img) # show the images
        plt.title(rand_class) # set title
        plt.axis(False) # hide the axis

view_rand_img('train')