def fix_frame_extract(frame_path, feats_path, frames_num, model, video_name): # load model C, H, W = 3, 224, 224 if model == 'resnet152': model = pretrainedmodels.resnet152(pretrained='imagenet') elif model == 'vgg16': model = pretrainedmodels.vgg16(pretrained='imagenet') elif model == 'inception_v4': C, H, W = 3, 299, 299 model = pretrainedmodels.inceptionv4(pretrained='imagenet') model.last_linear = utils.Identity() model = model.to(device) model.eval() load_image_fn = utils.LoadTransformImage(model) # load data img_list = sorted(frame_path.glob('*.jpg')) # get index samples_ix = np.linspace(0, len(img_list) - 1, frames_num).astype(int) img_list = [img_list[i] for i in samples_ix] # build tensor imgs = torch.zeros([len(img_list), C, H, W]) for i in range(len(img_list)): img = load_image_fn(img_list[i]) imgs[i] = img imgs = imgs.to(device) with torch.no_grad(): feats = model(imgs) feats = feats.cpu().numpy() # save np.save(os.path.join(feats_path, video_name + ".npy"), feats)
def prepare_model(model_path, hp, device): stump = None if hp['stump'] == 'inception': stump = ptm.inceptionv4() num_ftrs = stump.last_linear.in_features stump.last_linear = nn.Linear(num_ftrs, 2) for i, child in enumerate(stump.features.children()): if i < len(stump.features) * hp['freeze']: for param in child.parameters(): param.requires_grad = False dfs_freeze(child) elif hp['stump'] == 'alexnet': stump = models.alexnet(pretrained=True) num_ftrs = stump.classifier[-1].in_features stump.classifier[-1] = nn.Linear(num_ftrs, 2) if hp['pretraining']: stump.load_state_dict(torch.load(model_path, map_location=device)) print('Loaded stump: ', len(stump.features)) stump.train() net = RetinaNet2(frame_stump=stump, pooling_strategy=hp['pooling'], stump_type=hp['stump']) return net
def generate_C2D_model(opt): if opt.c2d_model_name == 'inception_v3': C, H, W = 3, 299, 299 model = pretrainedmodels.inceptionv3(num_classes=1000, pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) elif opt.c2d_model_name == 'resnet152': C, H, W = 3, 224, 224 model = pretrainedmodels.resnet152(num_classes=1000, pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) elif opt.c2d_model_name == 'inception_v4': C, H, W = 3, 299, 299 model = pretrainedmodels.inceptionv4(num_classes=1000, pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) elif opt.c2d_model_name == 'inceptionresnetv2': C, H, W = 3, 299, 299 model = pretrainedmodels.inceptionresnetv2(num_classes=1000, pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) model.last_linear = utils.Identity() if not opt.no_cuda: model = model.to(opt.device) return load_image_fn, model, (C, H, W)
def prepare_model(model_path, hp, device): stump = None if hp['multi_channel']: stump = my_inceptionv4(pretrained=False) hp['pretraining'] = False elif hp['narrow_model']: stump = NarrowInceptionV1(num_classes=2) hp['pretraining'] = False elif hp['network'] == 'inception': stump = ptm.inceptionv4() num_ftrs = stump.last_linear.in_features stump.last_linear = nn.Linear(num_ftrs, 2) elif hp['network'] == 'alexnet': stump = models.alexnet(pretrained=True) stump.classifier[-1] = nn.Linear(stump.classifier[-1].in_features, 2) if hp['pretraining']: stump.load_state_dict(torch.load(model_path, map_location=device)) print('Loaded stump: ', len(stump.features)) stump.train() #for i, child in enumerate(stump.features.children()): # if i < len(stump.features) * hp['freeze']: # for param in child.parameters(): # param.requires_grad = False # dfs_freeze(child) stump.to(device) return stump
def extract_feats(args): params = args if params['model'] == 'inception_v3': C, H, W = 3, 299, 299 model = pretrainedmodels.inceptionv3(pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) elif params['model'] == 'resnet152': C, H, W = 3, 224, 224 model = pretrainedmodels.resnet152(pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) elif params['model'] == 'inception_v4': C, H, W = 3, 299, 299 model = pretrainedmodels.inceptionv4(num_classes=1000, pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) else: print("doesn't support %s" % (params['model'])) model.last_linear = utils.Identity() model = nn.DataParallel(model) model = model.cuda() prepro_feats.extract_feats(params, model, load_image_fn)
def prepare_model(hp, device): net = None if hp['network'] == 'AlexNet': net = models.alexnet(pretrained=True) num_ftrs = net.classifier[-1].in_features net.classifier[-1] = Linear(num_ftrs, 2) elif hp['network'] == 'VGG': net = models.vgg13_bn(pretrained=True) num_ftrs = net.classifier[-1].in_features net.classifier[-1] = Linear(num_ftrs, 2) elif hp['network'] == 'Efficient': #net = EfficientNet.from_pretrained('efficientnet-b7') net = models.efficientnet_b7(pretrained=True) #num_ftrs = net._fc.in_features #net._fc = Linear(num_ftrs, 2) num_ftrs = net.classifier[-1].in_features net.classifier[-1] = Linear(num_ftrs, 2) elif hp['network'] == 'Inception': net = inceptionv4() num_ftrs = net.last_linear.in_features net.last_linear = Linear(num_ftrs, 2) for i, child in enumerate(net.features.children()): if i < len(net.features) * hp['freeze']: for param in child.parameters(): param.requires_grad = False nn_utils.dfs_freeze(child) if hp['pretraining']: net.load_state_dict(torch.load(hp['pretraining'], map_location=device)) print('Loaded stump: ', len(net.features)) net.train() #print(f'Model info: {net.__class__.__name__}, layer: {len(net)}, #frozen layer: {len(net) * hp["freeze"]}') return net
def prepare_model(model_path, device): stump = ptm.inceptionv4() # if not hp['multi_channel'] else my_inceptionv4(pretrained=False) num_ftrs = stump.last_linear.in_features stump.last_linear = nn.Linear(num_ftrs, 2) stump.load_state_dict(torch.load(model_path, map_location=device)) return stump
def set_model (model_name, num_class, neurons_reducer_block=0, comb_method=None, comb_config=None, pretrained=True, freeze_conv=False, p_dropout=0.5): if pretrained: pre_ptm = 'imagenet' pre_torch = True else: pre_torch = False pre_ptm = None if model_name not in _MODELS: raise Exception("The model {} is not available!".format(model_name)) model = None if model_name == 'resnet-50': model = MyResnet(models.resnet50(pretrained=pre_torch), num_class, neurons_reducer_block, freeze_conv, comb_method=comb_method, comb_config=comb_config) elif model_name == 'resnet-101': model = MyResnet(models.resnet101(pretrained=pre_torch), num_class, neurons_reducer_block, freeze_conv, comb_method=comb_method, comb_config=comb_config) elif model_name == 'densenet-121': model = MyDensenet(models.densenet121(pretrained=pre_torch), num_class, neurons_reducer_block, freeze_conv, comb_method=comb_method, comb_config=comb_config) elif model_name == 'vgg-13': model = MyVGGNet(models.vgg13_bn(pretrained=pre_torch), num_class, neurons_reducer_block, freeze_conv, comb_method=comb_method, comb_config=comb_config) elif model_name == 'vgg-16': model = MyVGGNet(models.vgg16_bn(pretrained=pre_torch), num_class, neurons_reducer_block, freeze_conv, comb_method=comb_method, comb_config=comb_config) elif model_name == 'vgg-19': model = MyVGGNet(models.vgg19_bn(pretrained=pre_torch), num_class, neurons_reducer_block, freeze_conv, comb_method=comb_method, comb_config=comb_config) elif model_name == 'mobilenet': model = MyMobilenet(models.mobilenet_v2(pretrained=pre_torch), num_class, neurons_reducer_block, freeze_conv, comb_method=comb_method, comb_config=comb_config) elif model_name == 'efficientnet-b4': if pretrained: model = MyEffnet(EfficientNet.from_pretrained(model_name), num_class, neurons_reducer_block, freeze_conv, comb_method=comb_method, comb_config=comb_config) else: model = MyEffnet(EfficientNet.from_name(model_name), num_class, neurons_reducer_block, freeze_conv, comb_method=comb_method, comb_config=comb_config) elif model_name == 'inceptionv4': model = MyInceptionV4(ptm.inceptionv4(num_classes=1000, pretrained=pre_ptm), num_class, neurons_reducer_block, freeze_conv, comb_method=comb_method, comb_config=comb_config) elif model_name == 'senet': model = MySenet(ptm.senet154(num_classes=1000, pretrained=pre_ptm), num_class, neurons_reducer_block, freeze_conv, comb_method=comb_method, comb_config=comb_config) return model
def __init__(self, num_classes): super(InceptionV4, self).__init__() self.name = "InceptionV4" self.pretrained_model = model_zoo.inceptionv4() cfg.mean = self.pretrained_model.mean cfg.std = self.pretrained_model.std self.fc = nn.Linear(1536, num_classes) nn.init.xavier_uniform(self.fc.weight, gain=2)
def __init__(self): self.model = inceptionv4(pretrained='imagenet') def base_arch(pretrained=True): all_layers = list(self.model.children()) return nn.Sequential(*all_layers[0], *all_layers[1:]) self.base_arch = base_arch
def __init__(self): super().__init__() self.conv_lrg = nn.Sequential(create_body(models.resnet50(True), -2), nn.AdaptiveAvgPool2d(1)) self.conv_small = create_body(pretrainedmodels.inceptionv4(), -2) self.cnn_lstm = cnn_lstm self.country_emb = nn.Embedding(codes_count, COUNTRY_EMB_SZ) head_inp_sz = num_features_model(self.conv_lrg) + \ model_sizes(self.conv_small, (96, 96), False)[-1][1] + self.country_emb.embedding_dim + cnn_lstm[6].rnn.hidden_size # self.head = create_head(head_inp_sz, 340)[2:] self.head = create_head(head_inp_sz, 340, ps=0, lin_ftrs=[4000, 2000])[2:]
def test_inceptionv4_model(input_var, pool, assert_equal_outputs): original_model = pretrainedmodels.inceptionv4(pretrained='imagenet', num_classes=1000) finetune_model = make_model( 'inception_v4', num_classes=1000, pool=pool, pretrained=True, ) copy_module_weights(original_model.last_linear, finetune_model._classifier) assert_equal_outputs(input_var, original_model, finetune_model)
def __init__(self, input_size=128, num_classes=340, pretrained='imagenet', dropout=0.): super().__init__() self.model = inceptionv4(num_classes=1000, pretrained=pretrained) self.features = self.model.features self.relu = nn.ReLU() self.avg_pool = nn.AvgPool2d(input_size // 64, stride=1, padding=0) self.dropout = nn.Dropout(p=dropout) self.last_linear = nn.Linear(1536, num_classes)
def nets(model, num_class): if model == 'inceptionv4': model = ptm.inceptionv4(num_classes=1000, pretrained='imagenet') model.last_linear = nn.Linear(model.last_linear.in_features, num_class) return model if model == 'senet154': model = ptm.senet154(num_classes=1000, pretrained='imagenet') model.last_linear = nn.Linear(model.last_linear.in_features, num_class) return model if model == 'pnasnet': model = ptm.pnasnet5large(num_classes=1000, pretrained='imagenet') model.last_linear = nn.Linear(model.last_linear.in_features, num_class) return model if model == 'xception': model = ptm.xception(num_classes=1000, pretrained='imagenet') model.last_linear = nn.Linear(model.last_linear.in_features, num_class) return model if model == 'incepresv2': model = ptm.inceptionresnetv2(num_classes=1000, pretrained='imagenet') model.last_linear = nn.Linear(model.last_linear.in_features, num_class) return model if model == 'resnet152': model = models.resnet152(pretrained=True) model.fc = nn.Linear(2048, num_class) return model if model == 'se_resxt101': model = ptm.se_resnext101_32x4d(num_classes=1000, pretrained='imagenet') model.last_linear = nn.Linear(model.last_linear.in_features, num_class) return model if model == 'nasnet': model = ptm.nasnetalarge(num_classes=1000, pretrained='imagenet') model.last_linear = nn.Linear(model.last_linear.in_features, num_class) return model if model == 'dpn': # 224 input size model = ptm.dpn107(num_classes=1000, pretrained='imagenet+5k') model.last_linear = nn.Conv2d(model.last_linear.in_channels, num_class, kernel_size=1, bias=True) return model if model == 'resnext101':# 320 input size model = torch.hub.load('facebookresearch/WSL-Images', 'resnext101_32x16d_wsl') model.fc = nn.Linear(2048, num_class) return model
def __init__(self): super(Model, self).__init__() pretrained = pretrainedmodels.inceptionv4( pretrained='imagenet+background') # self.features = nn.Sequential(*list(pretrained.children())[:-1]) self.features = pretrained.features # for layer in list(self.features.children())[:-2]: # for param in layer.parameters(): # param.requires_grad = False # for sublayer in list(list( # self.features.children())[-2].children())[:-1]: # for param in sublayer.parameters(): # param.requires_grad = False self.classifier = nn.Linear(1536, num_pigs)
def run(): device = torch.device(GPU_ID if torch.cuda.is_available() else "cpu") print(f'Using device {device}') hyperparameter = { 'learning_rate': [1e-2, 1e-3, 3e-4, 1e-4, 3e-5], # 1e-4 'weight_decay': [0, 1e-3, 3e-4, 1e-4], # 1e-4 'num_epochs': 60, # 100 'weights': [0.0, 0.2, 0.4, 0.6, 0.8, 1.0], # 0.6 'optimizer': [optim.Adam, optim.SGD], # Adam 'image_size': 320, 'crop_size': 299, 'freeze': 0.0 } loaders = prepare_dataset('retina', hyperparameter) #model: nn.Module = models.resnet50(pretrained=True) model = ptm.inceptionv4() for i, child in enumerate(model.features): if i < hyperparameter['freeze'] * len(model.features): for param in child.parameters(): param.requires_grad = False dfs_freeze(child) num_ftrs = model.last_linear.in_features model.last_linear = nn.Linear(num_ftrs, 2) optimizer_ft = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=hyperparameter['learning_rate'][3], weight_decay=hyperparameter['weight_decay'][3]) criterion = nn.CrossEntropyLoss() plateu_scheduler = lr_scheduler.ReduceLROnPlateau(optimizer_ft, mode='min', factor=0.3, patience=5, verbose=True) description = f"_exp3_processed90000_{model.__class__.__name__}_{hyperparameter['crop_size']}^2_{optimizer_ft.__class__.__name__}_{hyperparameter['freeze']}" writer = SummaryWriter(comment=description) #save_batch(next(iter(loaders[0])), '/tmp') model = train_model(model, criterion, optimizer_ft, plateu_scheduler, loaders, device, writer, num_epochs=hyperparameter['num_epochs'], desc=description)
def __init__(self, classifier=LogisticRegression(), feature_model=pretrainedmodels.inceptionv4(num_classes=1000, pretrained='imagenet'), image_transformer=None, image_type=None, padding=0): """ Arguments ========= classifier - Class that implements fit and predict The classifier that will be used. Is assumed to follow the scikit-learn API feature_model - Class that implements "eval" and "features" The feature extraction model. Assumed to follow the pretrainedmodels API image_transformer - None or callable The feature model is almost certainly assuming something about the size or pixel distribution of the data. The image transformer should be callable and convert an image to the format the feature_model is expecting. This is IMPORTANT! image_type - string The format the image should be in. RGB or something else padding - int The amount of padding to put around the bounding box (i.e., extra pixels to retain) """ self.classifier = classifier self.feature_model = feature_model self.feature_model.eval() # Setup the model # If it is none, then use pretrained models to get it. Otherwise, you gotta define this if image_transformer is None: transformer = utils.TransformImage(self.feature_model) self.image_transformer = lambda x: torch.autograd.Variable(transformer(x).unsqueeze(0), requires_grad=False) else: self.image_transformer = image_transformer if image_type is None: self.image_type = self.feature_model.input_space else: self.image_type = image_type self.padding = padding # Map strings to integers self.label_mapping = []
def load_models(conf_file): out = {} with open(conf_file) as f: doc = yaml.full_load(f) conf = doc['config'] for k, stat in conf.items(): if stat['net'] == 'inception_v4': out[k] = (pretrainedmodels.inceptionv4(pretrained=None), stat['sparisity']) elif stat['net'] == 'resnet18': out[k] = (pretrainedmodels.resnet18(pretrained=None), stat['sparisity']) elif stat['net'] == 'mobilenet_v2': out[k] = (models.mobilenet_v2(), stat['sparisity']) else: raise "Unknown net" return out
def generate_2D_model(opt): if opt['model'] == 'inception_v3': C, H, W = 3, 299, 299 model = pretrainedmodels.inceptionv3(pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) elif opt['model'] == 'vgg16': C, H, W = 3, 224, 224 model = pretrainedmodels.vgg16(pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) elif opt['model'] == 'vgg19': C, H, W = 3, 224, 224 model = pretrainedmodels.vgg19(pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) elif opt['model'] == 'resnet50': C, H, W = 3, 224, 224 model = pretrainedmodels.resnet50(pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) elif opt['model'] == 'resnet101': C, H, W = 3, 224, 224 model = pretrainedmodels.resnet101(pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) elif opt['model'] == 'resnet152': C, H, W = 3, 224, 224 model = pretrainedmodels.resnet152(pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) elif opt['model'] == 'inception_v4': C, H, W = 3, 299, 299 model = pretrainedmodels.inceptionv4(num_classes=1000, pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) elif opt['model'] == 'nasnet': C, H, W = 3, 331, 331 model = pretrainedmodels.nasnetalarge(num_classes=1001, pretrained='imagenet+background') load_image_fn = utils.LoadTransformImage(model) else: print("doesn't support %s" % (opt['model'])) model.last_linear = utils.Identity() model = nn.DataParallel(model) # if opt['saved_model'] != '': # model.load_state_dict(torch.load(opt['saved_model']), strict=False) model = model.cuda() return model
def extract_feats(frame_path, feats_path, interval, model, video_name): """ extract feature from frames of one video :param video_name: :param model: name of model :param frame_path: path of frames :param feats_path: path to store results :param interval: (str) The interval when extract frames from videos :return: None """ # load model C, H, W = 3, 224, 224 if model == 'resnet152': model = pretrainedmodels.resnet152(pretrained='imagenet') elif model == 'vgg16': model = pretrainedmodels.vgg16(pretrained='imagenet') elif model == 'inception_v4': C, H, W = 3, 299, 299 model = pretrainedmodels.inceptionv4(pretrained='imagenet') model.last_linear = utils.Identity() model = model.to(device) model.eval() load_image_fn = utils.LoadTransformImage(model) # load data img_list = sorted(frame_path.glob('*.jpg')) # get index samples_ix = np.arange(0, len(img_list), interval) img_list = [img_list[int(i)] for i in samples_ix] # build tensor imgs = torch.zeros([len(img_list), C, H, W]) for i in range(len(img_list)): img = load_image_fn(img_list[i]) imgs[i] = img imgs = imgs.to(device) with torch.no_grad(): feats = model(imgs) feats = feats.cpu().numpy() # save np.save(os.path.join(feats_path, video_name + ".npy"), feats)
def run(): device = torch.device(GPU_ID if torch.cuda.is_available() else "cpu") print(f'Using device {device}') hyperparameter = { 'learning_rate': [1e-2, 1e-3, 3e-4, 1e-4, 3e-5, 1e-7], # 1e-4 'weight_decay': [0, 1e-3, 5e-4, 1e-4, 1e-5], # 1e-4 'num_epochs': 70, # 100 'weights': [0.0, 0.2, 0.4, 0.6, 0.8, 1.0], # 0.6 'optimizer': [optim.Adam, optim.SGD], # Adam 'image_size': 300, 'crop_size': 299 } loaders = prepare_dataset('retina', hyperparameter) #model: nn.Module = models.resnet50(pretrained=True) #num_ftrs = model.fc.in_features #model.fc = nn.Linear(num_ftrs, 2) model = ptm.inceptionv4(num_classes=1000, pretrained='imagenet') num_ft = model.last_linear.in_features model.last_linear = nn.Linear(num_ft, 2) children = model.features.children() for i, child in enumerate(children): if i < 0.0 * len(list(children)): for param in child.parameters(): param.require_grad = False optimizer_ft = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=1e-7, weight_decay=0) criterion = nn.CrossEntropyLoss() lr_finder = LRFinder(model, optimizer_ft, criterion, device=device) lr_finder.range_test(loaders[0], end_lr=0.1, num_iter=100, step_mode='exp') lr_finder.plot() lr_finder.reset() return 0
def inceptionv4(input_size=(3, 299, 299), num_classes=1000, pretrained=None): # Minimum Input size = (96,96) one is must above than 97 model = models.inceptionv4(num_classes=1000, pretrained=pretrained) if input_size != (3, 299, 299): model.features[0].conv = nn.Conv2d(input_size[0], 32, kernel_size=3, stride=2, bias=False) model.input_size = input_size # calculate last avgpool_1a kernel size test_tensor = torch.randn(1, input_size[0], input_size[1], input_size[2]) x = model.features(test_tensor) print(x.shape) model.avg_pool = nn.AvgPool2d(kernel_size=(x.shape[2], x.shape[3]), padding=0) x = model.avg_pool(x) x = x.view(x.size(0), -1).shape[1] model.last_linear = nn.Linear(in_features=x, out_features=num_classes, bias=True) return model
def main(args): global C, H, W coco_labels = json.load(open(args.coco_labels)) num_classes = coco_labels['num_classes'] if args.model == 'inception_v3': C, H, W = 3, 299, 299 model = pretrainedmodels.inceptionv3(pretrained='imagenet') elif args.model == 'resnet152': C, H, W = 3, 224, 224 model = pretrainedmodels.resnet152(pretrained='imagenet') elif args.model == 'inception_v4': C, H, W = 3, 299, 299 model = pretrainedmodels.inceptionv4(num_classes=1000, pretrained='imagenet') else: print("doesn't support %s" % (args['model'])) load_image_fn = utils.LoadTransformImage(model) dim_feats = model.last_linear.in_features model = MILModel(model, dim_feats, num_classes) model = model.cuda() dataset = CocoDataset(coco_labels) dataloader = DataLoader(dataset, batch_size=args.batch_size, shuffle=True) optimizer = optim.Adam(model.parameters(), lr=args.learning_rate, weight_decay=args.weight_decay) exp_lr_scheduler = optim.lr_scheduler.StepLR( optimizer, step_size=args.learning_rate_decay_every, gamma=args.learning_rate_decay_rate) crit = nn.MultiLabelSoftMarginLoss() if not os.path.isdir(args.checkpoint_path): os.mkdir(args.checkpoint_path) train(dataloader, model, crit, optimizer, exp_lr_scheduler, load_image_fn, args)
def get_backbone(backbone, pretrained=True): if backbone in ['resnext50_32x4d_ssl', 'resnet18_ssl', 'resnet50_ssl', 'resnext101_32x4d_ssl']: if pretrained: model = torch.hub.load(panda_config.ARCH_TO_PRETRAINED[backbone], backbone) else: model = getattr(_models, backbone.split('_ssl')[0])(pretrained=pretrained) encoder = nn.Sequential(*list(model.children())[:-2]) in_features = model.fc.in_features elif backbone in ['resnet18', 'resnet34', 'resnet50']: pretrained = 'imagenet' if pretrained else None model = getattr(_models, backbone)(pretrained=pretrained) in_features = model.fc.in_features encoder = nn.Sequential(*list(model.children())[:-2]) elif backbone in ['se_resnext50_32x4d', 'se_resnext101_32x4d', 'se_resnet50', 'se_resnet101', 'se_resnet152']: pretrained = 'imagenet' if pretrained else None model = getattr(pretrainedmodels, backbone)(pretrained=pretrained) encoder = nn.Sequential(*list(model.children())[:-2]) in_features = model.last_linear.in_features elif backbone.startswith('efficientnet'): encoder = enet.EfficientNet.from_name(backbone) if pretrained: encoder.load_state_dict(torch.load(panda_config.ARCH_TO_PRETRAINED[backbone])) in_features = encoder._fc.in_features encoder._fc = nn.Identity() elif backbone == 'inception_resnet_v2': pretrained = 'imagenet' if pretrained else None encoder = pretrainedmodels.inceptionresnetv2(pretrained=pretrained) in_features = encoder.last_linear.in_features encoder.last_linear = nn.Identity() elif backbone == 'inception_v4': pretrained = 'imagenet' if pretrained else None encoder = pretrainedmodels.inceptionv4(pretrained=pretrained) in_features = encoder.last_linear.in_features encoder.last_linear = nn.Identity() else: raise ValueError(f'Unrecognized backbone {backbone}') return encoder, in_features
def __init__(self, num_classes=1001): super(InceptionV4, self).__init__() # Modules self.features = nn.Sequential( BasicConv2d(3, 32, kernel_size=3, stride=2), BasicConv2d(32, 32, kernel_size=3, stride=1), BasicConv2d(32, 64, kernel_size=3, stride=1, padding=1), Mixed_3a(), Mixed_4a(), Mixed_5a(), Inception_A(), Inception_A(), Inception_A(), Inception_A(), Reduction_A(), # Mixed_6a Inception_B(), Inception_B(), Inception_B(), Inception_B(), Inception_B(), Inception_B(), Inception_B(), Reduction_B(), # Mixed_7a Inception_C(), Inception_C(), Inception_C()) # load pre_train model pre_model = pretrainedmodels.inceptionv4(num_classes=1000, pretrained='imagenet') self.last_linear = pre_model.last_linear self.load_state_dict(pre_model.state_dict()) self.last_linear = nn.Linear(1536, num_classes) init_params(self.last_linear)
def get_original_model(self): if self.pretrained: model_kwargs = {'pretrained': 'imagenet', 'num_classes': 1000} else: model_kwargs = {'pretrained': None} return pretrainedmodels.inceptionv4(**model_kwargs)
def main(train_root, train_csv, train_split, val_root, val_csv, val_split, epochs, aug, model_name, batch_size, num_workers, val_samples, early_stopping_patience, n_classes, weighted_loss, balanced_loader, _run): assert (model_name in ('inceptionv4', 'resnet152', 'densenet161', 'senet154')) AUGMENTED_IMAGES_DIR = os.path.join(fs_observer.dir, 'images') CHECKPOINTS_DIR = os.path.join(fs_observer.dir, 'checkpoints') BEST_MODEL_PATH = os.path.join(CHECKPOINTS_DIR, 'model_best.pth') LAST_MODEL_PATH = os.path.join(CHECKPOINTS_DIR, 'model_last.pth') for directory in (AUGMENTED_IMAGES_DIR, CHECKPOINTS_DIR): os.makedirs(directory) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") if model_name == 'inceptionv4': model = ptm.inceptionv4(num_classes=1000, pretrained='imagenet') model.last_linear = nn.Linear(model.last_linear.in_features, n_classes) aug['size'] = 299 aug['mean'] = model.mean aug['std'] = model.std elif model_name == 'resnet152': model = models.resnet152(pretrained=True) model.fc = nn.Linear(model.fc.in_features, n_classes) aug['size'] = 224 aug['mean'] = [0.485, 0.456, 0.406] aug['std'] = [0.229, 0.224, 0.225] elif model_name == 'densenet161': model = models.densenet161(pretrained=True) model.classifier = nn.Linear(model.classifier.in_features, n_classes) aug['size'] = 224 aug['mean'] = [0.485, 0.456, 0.406] aug['std'] = [0.229, 0.224, 0.225] elif model_name == 'senet154': model = ptm.senet154(num_classes=1000, pretrained='imagenet') model.last_linear = nn.Linear(model.last_linear.in_features, n_classes) aug['size'] = model.input_size[1] aug['mean'] = model.mean aug['std'] = model.std model.to(device) augs = Augmentations(**aug) model.aug_params = aug train_ds = CSVDatasetWithName(train_root, train_csv, 'image', 'label', transform=augs.tf_transform, add_extension='.jpg', split=train_split) val_ds = CSVDatasetWithName(val_root, val_csv, 'image', 'label', transform=augs.tf_transform, add_extension='.jpg', split=val_split) datasets = {'train': train_ds, 'val': val_ds} if balanced_loader: data_sampler = sampler.WeightedRandomSampler(train_ds.sampler_weights, len(train_ds)) shuffle = False else: data_sampler = None shuffle = True dataloaders = { 'train': DataLoader(datasets['train'], batch_size=batch_size, shuffle=shuffle, num_workers=num_workers, sampler=data_sampler, worker_init_fn=set_seeds), 'val': DataLoader(datasets['val'], batch_size=batch_size, shuffle=False, num_workers=num_workers, worker_init_fn=set_seeds), } if weighted_loss: criterion = nn.CrossEntropyLoss( weight=torch.Tensor(datasets['train'].class_weights_list).cuda()) else: criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9, weight_decay=0.001) scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.1, min_lr=1e-5, patience=10) metrics = { 'train': pd.DataFrame(columns=['epoch', 'loss', 'acc']), 'val': pd.DataFrame(columns=['epoch', 'loss', 'acc']) } best_val_loss = 1000.0 epochs_without_improvement = 0 batches_per_epoch = None for epoch in range(epochs): print('train epoch {}/{}'.format(epoch + 1, epochs)) epoch_train_result = train_epoch(device, model, dataloaders, criterion, optimizer, 'train', batches_per_epoch) metrics['train'] = metrics['train'].append( { **epoch_train_result, 'epoch': epoch }, ignore_index=True) print('train', epoch_train_result) epoch_val_result = train_epoch(device, model, dataloaders, criterion, optimizer, 'val', batches_per_epoch) metrics['val'] = metrics['val'].append( { **epoch_val_result, 'epoch': epoch }, ignore_index=True) print('val', epoch_val_result) scheduler.step(epoch_val_result['loss']) if epoch_val_result['loss'] < best_val_loss: best_val_loss = epoch_val_result['loss'] epochs_without_improvement = 0 torch.save(model, BEST_MODEL_PATH) print('Best loss at epoch {}'.format(epoch)) else: epochs_without_improvement += 1 print('-' * 40) if epochs_without_improvement > early_stopping_patience: torch.save(model, LAST_MODEL_PATH) break if epoch == (epochs - 1): torch.save(model, LAST_MODEL_PATH) for phase in ['train', 'val']: metrics[phase].epoch = metrics[phase].epoch.astype(int) metrics[phase].to_csv(os.path.join(fs_observer.dir, phase + '.csv'), index=False) print('Best validation loss: {}'.format(best_val_loss)) # TODO: return more metrics return {'max_val_acc': metrics['val']['acc'].max()}
help='the CNN model you want to use to extract_feats') args = parser.parse_args() params = vars(args) if params['model'] == 'inception_v3': C, H, W = 3, 299, 299 model = pretrainedmodels.inceptionv3(pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) elif params['model'] == 'resnet152': C, H, W = 3, 224, 224 model = pretrainedmodels.resnet152(pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) elif params['model'] == 'inception_v4': C, H, W = 3, 299, 299 model = pretrainedmodels.inceptionv4(num_classes=1000, pretrained='imagenet') load_image_fn = utils.LoadTransformImage(model) else: print("doesn't support %s" % (params['model'])) model.last_linear = utils.Identity() # model = nn.DataParallel(model) if torch.cuda.is_available(): os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu device = torch.device('cuda') else: device = torch.device('cpu') model = model.to(device)
def __init__(self): super(Model, self).__init__() self.model = pretrainedmodels.inceptionv4(pretrained='imagenet')
import os import torch import torchvision import torch.nn as nn import torch.optim as optim from torch.optim import lr_scheduler import numpy as np from torch.utils.data import Dataset from torchvision import transforms, datasets from torch.autograd import Variable import matplotlib.pyplot as plt import time import pretrainedmodels model_ft = pretrainedmodels.inceptionv4(num_classes=1000, pretrained='imagenet') data_transforms = { 'train': transforms.Compose([ transforms.RandomSizedCrop(299), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean=model_ft.mean, std=model_ft.std) ]), 'val': transforms.Compose([ transforms.Scale(341), transforms.CenterCrop(299), transforms.ToTensor(), transforms.Normalize(mean=model_ft.mean, std=model_ft.std)
def load_inceptionv4(cls, num_classes): model = inceptionv4(num_classes=1000, pretrained="imagenet") model.last_linear = nn.Linear(model.last_linear.in_features, num_classes) return model