utility_functions.py
def load_data(data_directory, bts, input_size):
    
    """
    Loads image data from sprecified directory (the dataset needs to be split into 
    three parts, training, validation (respetive folders 'train', 'valid' and 'test' are 
    necesseray). 
    
    For the training, transformations such as random scaling, cropping, and flipping are
    applied. Input data is resized to 224x224 pixels as required by the pre-trained 
    networks.
    
    The validation and testing sets are resized and cropped to appropoate sizes.
    For all three sets the means and standard deviations of the images are normalized
    to what the network expects. For the means, it's [0.485, 0.456, 0.406] and for the 
    standard deviations [0.229, 0.224, 0.225], calculated from the ImageNet images. 
    These values will shift each color channel to be centered at 0 and range from -1 to 1.
    Dataloaders for training, validation and testing data are defined.
    The function returns the transformed image data sets and the dataloaders.
    
    Parameters:
     data_directory - main directory where the image data can be found
     bts            - batch size: number of images per batch
     input_size     - required image input size by the model architecture
    Returns:
     dataloader     - provides transformed image data for training, validation and testing
     datasets       - transformed image data for training, validation and testing
    """
    
    # Quick check if '/' was added at the end of directoy path
    if data_directory[-1] == '/':
        data_directory = data_directory[:-1]
    
    # Define paths to train, validation and test data
    train_dir = data_directory + '/train'
    valid_dir = data_directory + '/valid'
    test_dir  = data_directory + '/test'
    
    # Define transformations for the data sets
    train_transform = transforms.Compose([transforms.RandomRotation(30),
                                          transforms.RandomResizedCrop(input_size),
                                          transforms.RandomHorizontalFlip(),
                                          transforms.ToTensor(),
                                          transforms.Normalize([0.485, 0.456, 0.406], 
                                                               [0.229, 0.224, 0.225])])

    test_transform = transforms.Compose([transforms.Resize(255),
                                         transforms.CenterCrop(input_size),
                                         transforms.ToTensor(),
                                         transforms.Normalize([0.485, 0.456, 0.406], 
                                                              [0.229, 0.224, 0.225])])

    val_transform = test_transform

    # Load the datasets with ImageFolder
    train_dataset = datasets.ImageFolder(train_dir, transform=train_transform)
    valid_dataset = datasets.ImageFolder(valid_dir, transform=val_transform)
    test_dataset  = datasets.ImageFolder(test_dir,  transform=test_transform)

    # Create a dictionay with the loaded data
    image_datasets = {'train': train_dataset,
                      'valid': valid_dataset,
                      'test':  test_dataset}

    # Define the dataloaders
    trainloader = torch.utils.data.DataLoader(train_dataset, batch_size=bts, shuffle=True)
    validloader = torch.utils.data.DataLoader(valid_dataset, batch_size=bts, shuffle=True)
    testloader  = torch.utils.data.DataLoader(test_dataset,  batch_size=bts, shuffle=True)
    
    # Create a dictionay with the loaded data
    dataloaders = {'train': trainloader,
                   'valid': validloader,
                   'test':  testloader}
    
    return dataloaders, image_datasets

    Loads image data from sprecified directory (the dataset needs to be split into 
    three parts, training, validation (respetive folders 'train', 'valid' and 'test' are 
    necesseray). 
    
    For the training, transformations such as random scaling, cropping, and flipping are
    applied. Input data is resized to 224x224 pixels as required by the pre-trained 
    networks.
    
    The validation and testing sets are resized and cropped to appropoate sizes.
    For all three sets the means and standard deviations of the images are normalized
    to what the network expects. For the means, it's [0.485, 0.456, 0.406] and for the 
    standard deviations [0.229, 0.224, 0.225], calculated from the ImageNet images. 
    These values will shift each color channel to be centered at 0 and range from -1 to 1.
    Dataloaders for training, validation and testing data are defined.
    The function returns the transformed image data sets and the dataloaders.
    
    Parameters:
     data_directory - main directory where the image data can be found
     bts            - batch size: number of images per batch
     input_size     - required image input size by the model architecture
    Returns:
     dataloader     - provides transformed image data for training, validation and testing
     datasets       - transformed image data for training, validation and testing
    


For the training, transformations such as random scaling, cropping, and flipping are
applied. Input data is resized to 224x224 pixels as required by the pre-trained
networks.

The validation and testing sets are resized and cropped to appropoate sizes.
For all three sets the means and standard deviations of the images are normalized
to what the network expects.

main directory where the image data can be found
bts            - batch size: number of images per batch
input_size     - required image input size by the model architecture
Returns:
dataloader     - provides transformed image data for training, validation and testing
datasets       - transformed image data for training, validation and testing
For the training transformations such as random scaling cropping and flipping are applied . Input data is resized to 224x224 pixels as required by the pre - trained networks . The validation and testing sets are resized and cropped to appropoate sizes . For all three sets the means and standard deviations of the images are normalized to what the network expects . main directory where the image data can be found bts - batch size : number of images per batch input_size - required image input size by the model architecture Returns : dataloader - provides transformed image data for training validation and testing datasets - transformed image data for training validation and testing
def load_data(data_directory, bts, input_size):
    if data_directory[-1] == '/':
        data_directory = data_directory[:-1]
    train_dir = data_directory + '/train'
    valid_dir = data_directory + '/valid'
    test_dir  = data_directory + '/test'
    train_transform = transforms.Compose([transforms.RandomRotation(30),
                                          transforms.RandomResizedCrop(input_size),
                                          transforms.RandomHorizontalFlip(),
                                          transforms.ToTensor(),
                                          transforms.Normalize([0.485, 0.456, 0.406], 
                                                               [0.229, 0.224, 0.225])])
    test_transform = transforms.Compose([transforms.Resize(255),
                                         transforms.CenterCrop(input_size),
                                         transforms.ToTensor(),
                                         transforms.Normalize([0.485, 0.456, 0.406], 
                                                              [0.229, 0.224, 0.225])])
    val_transform = test_transform
    train_dataset = datasets.ImageFolder(train_dir, transform=train_transform)
    valid_dataset = datasets.ImageFolder(valid_dir, transform=val_transform)
    test_dataset  = datasets.ImageFolder(test_dir,  transform=test_transform)
    image_datasets = {'train': train_dataset,
                      'valid': valid_dataset,
                      'test':  test_dataset}
    trainloader = torch.utils.data.DataLoader(train_dataset, batch_size=bts, shuffle=True)
    validloader = torch.utils.data.DataLoader(valid_dataset, batch_size=bts, shuffle=True)
    testloader  = torch.utils.data.DataLoader(test_dataset,  batch_size=bts, shuffle=True)
    dataloaders = {'train': trainloader,
                   'valid': validloader,
                   'test':  testloader}
    return dataloaders, image_datasets
def load_data ( data_directory , bts , input_size ) : if data_directory [ - 1 ] == '/' : data_directory = data_directory [ : - 1 ] train_dir = data_directory + '/train' valid_dir = data_directory + '/valid' test_dir = data_directory + '/test' train_transform = transforms . Compose ( [ transforms . RandomRotation ( 30 ) , transforms . RandomResizedCrop ( input_size ) , transforms . RandomHorizontalFlip ( ) , transforms . ToTensor ( ) , transforms . Normalize ( [ 0.485 , 0.456 , 0.406 ] , [ 0.229 , 0.224 , 0.225 ] ) ] ) test_transform = transforms . Compose ( [ transforms . Resize ( 255 ) , transforms . CenterCrop ( input_size ) , transforms . ToTensor ( ) , transforms . Normalize ( [ 0.485 , 0.456 , 0.406 ] , [ 0.229 , 0.224 , 0.225 ] ) ] ) val_transform = test_transform train_dataset = datasets . ImageFolder ( train_dir , transform = train_transform ) valid_dataset = datasets . ImageFolder ( valid_dir , transform = val_transform ) test_dataset = datasets . ImageFolder ( test_dir , transform = test_transform ) image_datasets = { 'train' : train_dataset , 'valid' : valid_dataset , 'test' : test_dataset } trainloader = torch . utils . data . DataLoader ( train_dataset , batch_size = bts , shuffle = True ) validloader = torch . utils . data . DataLoader ( valid_dataset , batch_size = bts , shuffle = True ) testloader = torch . utils . data . DataLoader ( test_dataset , batch_size = bts , shuffle = True ) dataloaders = { 'train' : trainloader , 'valid' : validloader , 'test' : testloader } return dataloaders , image_datasets
Loads image data from sprecified directory (the dataset needs to be split into
three parts, training, validation (respetive folders 'train', 'valid' and 'test' are
necesseray).
Loads image data from sprecified directory ( the dataset needs to be split into three parts training validation ( respetive folders ' train ' ' valid ' and ' test ' are necesseray ) .
"""
    Loads image data from sprecified directory (the dataset needs to be split into 
    three parts, training, validation (respetive folders 'train', 'valid' and 'test' are 
    necesseray). 
    
    For the training, transformations such as random scaling, cropping, and flipping are
    applied. Input data is resized to 224x224 pixels as required by the pre-trained 
    networks.
    
    The validation and testing sets are resized and cropped to appropoate sizes.
    For all three sets the means and standard deviations of the images are normalized
    to what the network expects. For the means, it's [0.485, 0.456, 0.406] and for the 
    standard deviations [0.229, 0.224, 0.225], calculated from the ImageNet images. 
    These values will shift each color channel to be centered at 0 and range from -1 to 1.
    Dataloaders for training, validation and testing data are defined.
    The function returns the transformed image data sets and the dataloaders.
    
    Parameters:
     data_directory - main directory where the image data can be found
     bts            - batch size: number of images per batch
     input_size     - required image input size by the model architecture
    Returns:
     dataloader     - provides transformed image data for training, validation and testing
     datasets       - transformed image data for training, validation and testing
    """ # Quick check if '/' was added at the end of directoy path # Define paths to train, validation and test data # Define transformations for the data sets # Load the datasets with ImageFolder # Create a dictionay with the loaded data # Define the dataloaders # Create a dictionay with the loaded data
data_directory
bts
input_size