<reponame>andreaberti11235/ProtoPNet
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 29 14:38:03 2022
prova: aggiunta di Dropout ad una pretrained net
@author: si-lab
"""

from __future__ import print_function
from __future__ import division
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
import matplotlib.pyplot as plt
import time
import os
import copy



def set_parameter_requires_grad(model, feature_extracting):
    if feature_extracting:
        for param in model.parameters():
            param.requires_grad = False
            
            

def initialize_model(model_name, num_classes, feature_extract, use_pretrained=True):
    # Initialize these variables which will be set in this if statement. Each of these
    #   variables is model specific.
    model_ft = None
    input_size = 0

    if model_name == "resnet50":
        """ Resnet50
        """
        model_ft = models.resnet50(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        num_ftrs = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_ftrs, num_classes)
        input_size = 224   

    return model_ft, input_size


model_name ='resnet50'




# Flag for feature extracting. When False, we finetune the whole model,
#   when True we only update the reshaped layer params
feature_extract = True


device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")


# Initialize the model for this run
model_ft, input_size = initialize_model(model_name, 2, feature_extract, use_pretrained=True)

print(model_ft)

print('--------------------------------------||||||||||||||||||||||||||||||||||---------------------')
print()

#%% 
def append_dropout(model, rate=0.4):
        for name, module in model.named_children():
            if len(list(module.children())) > 0:
                append_dropout(module)
            if isinstance(module, nn.ReLU):
                new = nn.Sequential(module, nn.Dropout2d(p=rate, inplace=True))
                setattr(model, name, new)


append_dropout(model_ft)
print(model_ft)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 29 14:38:03 2022
prova: aggiunta di Dropout ad una pretrained net
@author: si-lab
"""

from __future__ import print_function
from __future__ import division
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
import matplotlib.pyplot as plt
import time
import os
import copy



def set_parameter_requires_grad(model, feature_extracting):
    if feature_extracting:
        for param in model.parameters():
            param.requires_grad = False
            
            

def initialize_model(model_name, num_classes, feature_extract, use_pretrained=True):
    # Initialize these variables which will be set in this if statement. Each of these
    #   variables is model specific.
    model_ft = None
    input_size = 0

    if model_name == "resnet50":
        """ Resnet50
        """
        model_ft = models.resnet50(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        num_ftrs = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_ftrs, num_classes)
        input_size = 224   

    return model_ft, input_size


model_name ='resnet50'




# Flag for feature extracting. When False, we finetune the whole model,
#   when True we only update the reshaped layer params
feature_extract = True


device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")


# Initialize the model for this run
model_ft, input_size = initialize_model(model_name, 2, feature_extract, use_pretrained=True)

print(model_ft)

print('--------------------------------------||||||||||||||||||||||||||||||||||---------------------')
print()

#%% 
def append_dropout(model, rate=0.4):
        for name, module in model.named_children():
            if len(list(module.children())) > 0:
                append_dropout(module)
            if isinstance(module, nn.ReLU):
                new = nn.Sequential(module, nn.Dropout2d(p=rate, inplace=True))
                setattr(model, name, new)


append_dropout(model_ft)
print(model_ft)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- Created on Tue Mar 29 14:38:03 2022 prova: aggiunta di Dropout ad una pretrained net @author: si-lab # Initialize these variables which will be set in this if statement. Each of these #   variables is model specific. Resnet50 # Flag for feature extracting. When False, we finetune the whole model, #   when True we only update the reshaped layer params # Initialize the model for this run #%%