def load_net(dataset, net_type, nb_classes): if net_type == 'bn_inception': model = net.bn_inception(pretrained=True) model.last_linear = nn.Linear(1024, nb_classes) model.load_state_dict( torch.load('net/finetuned_' + dataset + '_' + net_type + '.pth')) elif net_type == 'densenet121': model = net.densenet121(pretrained=True) model.classifier = nn.Linear(1024, nb_classes) model.load_state_dict( torch.load('net/finetuned_' + dataset + '_' + net_type + '.pth')) elif net_type == 'densenet161': model = net.densenet161(pretrained=True) model.classifier = nn.Linear(2208, nb_classes) model.load_state_dict( torch.load('net/finetuned_' + dataset + '_' + net_type + '.pth')) elif net_type == 'densenet169': model = net.densenet169(pretrained=True) model.classifier = nn.Linear(1664, nb_classes) model.load_state_dict( torch.load('net/finetuned_' + dataset + '_' + net_type + '.pth')) elif net_type == 'densenet201': model = net.densenet201(pretrained=True) model.classifier = nn.Linear(1920, nb_classes) model.load_state_dict( torch.load('net/finetuned_' + dataset + '_' + net_type + '.pth')) return model
# -------------------------------- # Load pre-trained model & weights # -------------------------------- # Backbone Model trained_dataset = 'inshop' model_name = 'bn_inception' length = 6400 # CUB: 2500 | Cars: 4900 | SOP: 8100 | In-shop: 6400 if model_name == 'googlenet': model = googlenet(embedding_size=args.sz_embedding, pretrained=True, is_norm=args.l2_norm, bn_freeze = args.bn_freeze) elif model_name == 'bn_inception': model = bn_inception(embedding_size=args.sz_embedding, pretrained=True, is_norm=args.l2_norm, bn_freeze = args.bn_freeze) elif model_name == 'resnet50': model = Resnet50(embedding_size=args.sz_embedding, pretrained=True, is_norm=args.l2_norm, bn_freeze = args.bn_freeze) model = model.cuda() if args.gpu_id == -1: model = nn.DataParallel(model) # Load pre-trained weights if trained_dataset == 'cub': print("CUB!") path = 'path/to/cub_weights.pth' elif trained_dataset == 'cars': print("Cars!") path = 'path/to/cars_weights.pth'
num_workers=args.nb_workers, drop_last=True, pin_memory=True) dl_ev = torch.utils.data.DataLoader(dataset.Data.factory( type=type, root=args.root_folder, labels=list(range(args.nb_classes, 2 * args.nb_classes)), is_extracted=args.is_extracted, transform=dataset.utils.make_transform(is_train=False)), batch_size=args.sz_batch, shuffle=False, num_workers=args.nb_workers, pin_memory=True) model = net.bn_inception(pretrained=True) net.embed(model, sz_embedding=args.sz_embedding) model = model.cuda() criterion = proxynca.ProxyNCA(args.sz_embedding, args.nb_classes, args.sz_batch).cuda() opt = torch.optim.Adam( [ { # embedding parameters 'params': model.embedding_layer.parameters(), 'lr': args.lr_embedding }, { # proxy nca parameters 'params': criterion.parameters(), 'lr': args.lr_proxynca
def prepare_model(self, base_model_name, num_classes): """ base_model: string contains the model name this function is used to modify the last layer (fully connected layer) for a given architecture to suit our dataset number of actions. """ #add other architectures later if 'resnet' in base_model_name: #Load pretrained model self.base_model = getattr(torchvision.models, base_model_name)(pretrained=True) self.last_layer_name = 'fc' #set the input size for the model self.input_size = 224 self.input_mean = [0.485, 0.456, 0.406] self.input_std = [0.229, 0.224, 0.225] #There's no point of substarct means from RGBDiff frames if self.modality == 'RGBDiff': #[0.485, 0.456, 0.406 , 0, 0, 0, 0, 0,.....] self.input_mean = [0.485, 0.456, 0.406] + [0] * 3 * self.new_length #Expand the list with the average 0.452 self.input_std = self.input_std + [np.mean(self.input_std) * 2] * 3 * self.new_length #BNInception doesn't exist in torchvision models, so we have to get it from net folder elif base_model_name == 'BNInception': import net self.base_model = net.bn_inception(pretrained = True) self.last_layer_name = 'last_linear' self.input_size = 224 self.input_mean = [104, 117, 128] self.input_std = [1] #BNInception takes input in range of (0~255), so only mean subtraction should be used in preprocessing. #This is different from ResNet models, which take input in the range of (0~1). if self.modality == 'RGBDiff': self.input_mean = self.input_mean * (1+self.new_length) else: raise ValueError('Unknown base model: {}'.format(base_model_name)) #Get the input size for the last layer of CNN features_dim = getattr(self.base_model, self.last_layer_name).in_features #In case of no dropout,Only nn.Linear will be added if self.dropout == 0: setattr(self.base_model, self.last_layer_name, nn.Linear(features_dim, num_classes)) self.new_fc = None print('The modified linear layer is :', getattr(self.base_model, self.last_layer_name)) #In case of dropout, only nn.Dropout will be added and nn.Linear will be prepared to be added later else: setattr(self.base_model, self.last_layer_name, nn.Dropout(self.dropout)) self.new_fc = nn.Linear(features_dim, num_classes) print('Dropout Layer added and The modified linear layer is :',self.new_fc) #Modify Wighets of newly created Linear layer std=0.001 if self.new_fc == None: normal_(getattr(self.base_model, self.last_layer_name).weight,0,std) constant_(getattr(self.base_model, self.last_layer_name).bias,0) else: normal_(self.new_fc.weight, 0, std) constant_(self.new_fc.bias,0)
def setup_model(args=args): model = net.bn_inception(pretrained=True) net.embed(model, sz_embedding=args.sz_embedding) model = model.cuda() return model
parser.add_argument('--net_type', default='densenet121', type=str, help='type of net we want to use') parser.add_argument('--embed', default=False, type=bool, help='Number of iterations we want to do for GTG') args = parser.parse_args() batch_size = 32 # torch.cuda.set_device(args.gpu_id) if args.net_type == 'bn_inception': model = net.bn_inception(pretrained=True, nb_classes=args.nb_classes) model.last_linear = nn.Linear(1024, args.nb_classes) if args.embed: model = net.Inception_embed(model, 1024, args.sz_embedding, args.nb_classes) elif args.net_type == 'resnet18': model = net.resnet18(pretrained=True) model.fc = nn.Linear(512, args.nb_classes) elif args.net_type == 'resnet34': model = net.resnet34(pretrained=True) model.fc = nn.Linear(512, args.nb_classes) elif args.net_type == 'resnet50': model = net.resnet50(pretrained=True) model.fc = nn.Linear(2048, args.nb_classes) elif args.net_type == 'resnet101':
) dl_ev = torch.utils.data.DataLoader( dataset.Birds( root = args.cub_root, labels = list(range(args.nb_classes, 2 * args.nb_classes)), is_extracted = args.cub_is_extracted, transform = dataset.utils.make_transform(is_train = False) ), batch_size = args.sz_batch, shuffle = False, num_workers = args.nb_workers, pin_memory = True ) model = net.bn_inception(pretrained = True) net.embed(model, sz_embedding=args.sz_embedding) model = model.cuda() criterion = proxynca.ProxyNCA(args.sz_embedding, args.nb_classes, args.sz_batch).cuda() opt = torch.optim.Adam( [ { # embedding parameters 'params': model.embedding_layer.parameters(), 'lr' : args.lr_embedding }, { # proxy nca parameters 'params': criterion.parameters(), 'lr': args.lr_proxynca