utility_functions.py
def initialize_model(arch, hidden_units, feature_extract=True):
    
    """
    Function for initializing the chosen model architecture. For this the following
    operations are conducted:
    1) Load the pre-trained model
    2) Sets gradient on/off for parameters of the existing model layers (default: no
       gradients required for existing model layers (only feature extraction - train
       the newly added layers)
    3) Specifiy input size of the images in variabel "input_size" (specific for model
       architecture)
    4) Create a new classifier and replace either the existing one of the model or
       replace the final layers of the existing model (specific for model
       architecture)
    5) Return updated model archictecture and required input size for the model.
    
    Parameters:
     arch - name of the choosen model architecture
     feature_extract - Boolean variable, default: "True", i.e. no gradients for all 
                       existing layers of the model
    Returns:
     model - updated model architecture
     input_size - required image size for input to the model
    """
    
    # Load pre-trained model from torchvision.models with attribute saved in 'arch'
    model = getattr(models, arch)(pretrained=True)
    
    # Set gradients off for existing model layers (only feature extraction)
    set_parameter_requires_grad(model, feature_extract)
    
    # Print the model architecture
    print('\nThis is the architecture of {}: \n{}\n '.format(arch, model))
    
    # Specify input size:
    input_size = 224
    
    ## Models w/o classifier:
    resnet = return_resnet_models()
    
    ## If architecture 'Resnet' was chosen
    if arch in resnet:
        
        # Get number of inputs for "fc" layer:
        in_classifier = model.fc.in_features
        out_fc1 = int(in_classifier/2)
        
        # Delete attribute 'fc' from model
        #model.fc = Identity()
        #delattr(model, 'fc')
        
        # Define the new classifier with a OrderedDict 
        model.fc = nn.Sequential(OrderedDict([
                                  ('fc1', nn.Linear(in_classifier, out_fc1)),
                                  ('relu1', nn.ReLU()),
                                  ('drop1', nn.Dropout(p=0.3)),
                                  ('fc2', nn.Linear(out_fc1, hidden_units)),
                                  ('relu2', nn.ReLU()),
                                  ('drop2', nn.Dropout(p=0.3)),
                                  ('fc3', nn.Linear(hidden_units, 102)),
                                  ('output', nn.LogSoftmax(dim=1))
                                  ]))

        
    ## Models with classifier:
    else: 
        
        # Specify input size:
        input_size = 224
        
        # Create list with layers of the classifier 
        # Code inspired from: 
        # https://discuss.pytorch.org/t/how-to-get-layer-index-by-name-in-nn-sequential/25188/2
        list_classifier =  list( dict( model.classifier.named_children() ).values() )
        
        # Check number of layers in classifier
        num_classifier = len(list_classifier)
        
        # If classifier consists only of one layer, e.g. model densenet
        if num_classifier == 0: 
            
            # Get number of inputs for existing classifier the model:
            in_classifier = model.classifier.in_features
            out_fc1 = int(in_classifier/2)
            
        
        # If classifier consists of more than one layer, e.g. we have to find first layer of classifier
        else: 
            
            # Get list of indices from the classifier list of type 'Linear' Or 'Conv2d'
            # Code inspired from:
            # https://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python
            # https://stackoverflow.com/questions/54846905/pytorch-get-all-layers-of-model
    
            indices = [i for i, s in enumerate(list_classifier) if isinstance(s, nn.modules.linear.Linear) 
                                                                or isinstance(s, nn.modules.conv.Conv2d)]
        
            # Get index of first linear layer of existing classifier of the model
            idx_lin = indices[0]
    
            # Get number of inputs for existing classifier the model:
            in_classifier = model.classifier[idx_lin].in_features
            out_fc1 = int(in_classifier/2)
            

        # Define the new classifier with a OrderedDict      
        model.classifier = nn.Sequential(OrderedDict([
                                  ('fc1', nn.Linear(in_classifier, out_fc1)),
                                  ('relu1', nn.ReLU()),
                                  ('drop1', nn.Dropout(p=0.3)),
                                  ('fc2', nn.Linear(out_fc1, hidden_units)),
                                  ('relu2', nn.ReLU()),
                                  ('drop2', nn.Dropout(p=0.3)),
                                  ('fc3', nn.Linear(hidden_units, 102)),
                                  ('output', nn.LogSoftmax(dim=1))
                                  ]))

    model.input_size = input_size
    
    print('This is the new model:\n{}.'.format(model))

        
    return model

    Function for initializing the chosen model architecture. For this the following
    operations are conducted:
    1) Load the pre-trained model
    2) Sets gradient on/off for parameters of the existing model layers (default: no
       gradients required for existing model layers (only feature extraction - train
       the newly added layers)
    3) Specifiy input size of the images in variabel "input_size" (specific for model
       architecture)
    4) Create a new classifier and replace either the existing one of the model or
       replace the final layers of the existing model (specific for model
       architecture)
    5) Return updated model archictecture and required input size for the model.
    
    Parameters:
     arch - name of the choosen model architecture
     feature_extract - Boolean variable, default: "True", i.e. no gradients for all 
                       existing layers of the model
    Returns:
     model - updated model architecture
     input_size - required image size for input to the model
    
Function for initializing the chosen model architecture. For this the following
operations are conducted:
1) Load the pre-trained model
2) Sets gradient on/off for parameters of the existing model layers (default: no
gradients required for existing model layers (only feature extraction - train
the newly added layers)
3) Specifiy input size of the images in variabel "input_size" (specific for model
architecture)
4) Create a new classifier and replace either the existing one of the model or
replace the final layers of the existing model (specific for model
architecture)
5) Return updated model archictecture and required input size for the model.

name of the choosen model architecture
feature_extract - Boolean variable, default: "True", i.e. no gradients for all
existing layers of the model
Returns:
model - updated model architecture
input_size - required image size for input to the model
Function for initializing the chosen model architecture . For this the following operations are conducted : 1 ) Load the pre - trained model 2 ) Sets gradient on / off for parameters of the existing model layers ( default : no gradients required for existing model layers ( only feature extraction - train the newly added layers ) 3 ) Specifiy input size of the images in variabel " input_size " ( specific for model architecture ) 4 ) Create a new classifier and replace either the existing one of the model or replace the final layers of the existing model ( specific for model architecture ) 5 ) Return updated model archictecture and required input size for the model . name of the choosen model architecture feature_extract - Boolean variable default : " True " i . e . no gradients for all existing layers of the model Returns : model - updated model architecture input_size - required image size for input to the model
def initialize_model(arch, hidden_units, feature_extract=True):
    model = getattr(models, arch)(pretrained=True)
    set_parameter_requires_grad(model, feature_extract)
    print('\nThis is the architecture of {}: \n{}\n '.format(arch, model))
    input_size = 224
    resnet = return_resnet_models()
    if arch in resnet:
        in_classifier = model.fc.in_features
        out_fc1 = int(in_classifier/2)
        model.fc = nn.Sequential(OrderedDict([
                                  ('fc1', nn.Linear(in_classifier, out_fc1)),
                                  ('relu1', nn.ReLU()),
                                  ('drop1', nn.Dropout(p=0.3)),
                                  ('fc2', nn.Linear(out_fc1, hidden_units)),
                                  ('relu2', nn.ReLU()),
                                  ('drop2', nn.Dropout(p=0.3)),
                                  ('fc3', nn.Linear(hidden_units, 102)),
                                  ('output', nn.LogSoftmax(dim=1))
                                  ]))
    else: 
        input_size = 224
        list_classifier =  list( dict( model.classifier.named_children() ).values() )
        num_classifier = len(list_classifier)
        if num_classifier == 0: 
            in_classifier = model.classifier.in_features
            out_fc1 = int(in_classifier/2)
        else: 
            indices = [i for i, s in enumerate(list_classifier) if isinstance(s, nn.modules.linear.Linear) 
                                                                or isinstance(s, nn.modules.conv.Conv2d)]
            idx_lin = indices[0]
            in_classifier = model.classifier[idx_lin].in_features
            out_fc1 = int(in_classifier/2)
        model.classifier = nn.Sequential(OrderedDict([
                                  ('fc1', nn.Linear(in_classifier, out_fc1)),
                                  ('relu1', nn.ReLU()),
                                  ('drop1', nn.Dropout(p=0.3)),
                                  ('fc2', nn.Linear(out_fc1, hidden_units)),
                                  ('relu2', nn.ReLU()),
                                  ('drop2', nn.Dropout(p=0.3)),
                                  ('fc3', nn.Linear(hidden_units, 102)),
                                  ('output', nn.LogSoftmax(dim=1))
                                  ]))
    model.input_size = input_size
    print('This is the new model:\n{}.'.format(model))
    return model
def initialize_model ( arch , hidden_units , feature_extract = True ) : model = getattr ( models , arch ) ( pretrained = True ) set_parameter_requires_grad ( model , feature_extract ) print ( '\nThis is the architecture of {}: \n{}\n ' . format ( arch , model ) ) input_size = 224 resnet = return_resnet_models ( ) if arch in resnet : in_classifier = model . fc . in_features out_fc1 = int ( in_classifier / 2 ) model . fc = nn . Sequential ( OrderedDict ( [ ( 'fc1' , nn . Linear ( in_classifier , out_fc1 ) ) , ( 'relu1' , nn . ReLU ( ) ) , ( 'drop1' , nn . Dropout ( p = 0.3 ) ) , ( 'fc2' , nn . Linear ( out_fc1 , hidden_units ) ) , ( 'relu2' , nn . ReLU ( ) ) , ( 'drop2' , nn . Dropout ( p = 0.3 ) ) , ( 'fc3' , nn . Linear ( hidden_units , 102 ) ) , ( 'output' , nn . LogSoftmax ( dim = 1 ) ) ] ) ) else : input_size = 224 list_classifier = list ( dict ( model . classifier . named_children ( ) ) . values ( ) ) num_classifier = len ( list_classifier ) if num_classifier == 0 : in_classifier = model . classifier . in_features out_fc1 = int ( in_classifier / 2 ) else : indices = [ i for i , s in enumerate ( list_classifier ) if isinstance ( s , nn . modules . linear . Linear ) or isinstance ( s , nn . modules . conv . Conv2d ) ] idx_lin = indices [ 0 ] in_classifier = model . classifier [ idx_lin ] . in_features out_fc1 = int ( in_classifier / 2 ) model . classifier = nn . Sequential ( OrderedDict ( [ ( 'fc1' , nn . Linear ( in_classifier , out_fc1 ) ) , ( 'relu1' , nn . ReLU ( ) ) , ( 'drop1' , nn . Dropout ( p = 0.3 ) ) , ( 'fc2' , nn . Linear ( out_fc1 , hidden_units ) ) , ( 'relu2' , nn . ReLU ( ) ) , ( 'drop2' , nn . Dropout ( p = 0.3 ) ) , ( 'fc3' , nn . Linear ( hidden_units , 102 ) ) , ( 'output' , nn . LogSoftmax ( dim = 1 ) ) ] ) ) model . input_size = input_size print ( 'This is the new model:\n{}.' . format ( model ) ) return model
Function for initializing the chosen model architecture.
Function for initializing the chosen model architecture .
"""
    Function for initializing the chosen model architecture. For this the following
    operations are conducted:
    1) Load the pre-trained model
    2) Sets gradient on/off for parameters of the existing model layers (default: no
       gradients required for existing model layers (only feature extraction - train
       the newly added layers)
    3) Specifiy input size of the images in variabel "input_size" (specific for model
       architecture)
    4) Create a new classifier and replace either the existing one of the model or
       replace the final layers of the existing model (specific for model
       architecture)
    5) Return updated model archictecture and required input size for the model.
    
    Parameters:
     arch - name of the choosen model architecture
     feature_extract - Boolean variable, default: "True", i.e. no gradients for all 
                       existing layers of the model
    Returns:
     model - updated model architecture
     input_size - required image size for input to the model
    """ # Load pre-trained model from torchvision.models with attribute saved in 'arch' # Set gradients off for existing model layers (only feature extraction) # Print the model architecture # Specify input size: ## Models w/o classifier: ## If architecture 'Resnet' was chosen # Get number of inputs for "fc" layer: # Delete attribute 'fc' from model #model.fc = Identity() #delattr(model, 'fc') # Define the new classifier with a OrderedDict  ## Models with classifier: # Specify input size: # Create list with layers of the classifier  # Code inspired from:  # https://discuss.pytorch.org/t/how-to-get-layer-index-by-name-in-nn-sequential/25188/2 # Check number of layers in classifier # If classifier consists only of one layer, e.g. model densenet # Get number of inputs for existing classifier the model: # If classifier consists of more than one layer, e.g. we have to find first layer of classifier # Get list of indices from the classifier list of type 'Linear' Or 'Conv2d' # Code inspired from: # https://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python # https://stackoverflow.com/questions/54846905/pytorch-get-all-layers-of-model # Get index of first linear layer of existing classifier of the model # Get number of inputs for existing classifier the model: # Define the new classifier with a OrderedDict      
arch
hidden_units
feature_extract