def __init__(self,drop_rate=0): super(Net,self).__init__() self.base_modelname = Configuration.instance().MODEL_NAME base_modelname = self.base_modelname if base_modelname == "resnet50": self.encoder = resnet50(pretrained=False) encoder_out_channels = (2048, 1024, 512, 256, 64) encoder_new_out_channels = (int(2048 / Configuration.instance().RESNET_SCALE), int(1024 / Configuration.instance().RESNET_SCALE), int(512 / Configuration.instance().RESNET_SCALE), int(256 / Configuration.instance().RESNET_SCALE), int(64 / Configuration.instance().RESNET_SCALE)) self.decoder: DNTD = DNTD(encoder_out_channels, encoder_new_out_channels,drop_rate=drop_rate) elif base_modelname == "efficientnet-b3": self.encoder:EfficientNet = EfficientNet.from_name('efficientnet-b3',override_params={'num_classes': None}) encoder_out_channels = (1536,96,48,32,40) encoder_new_out_channels = (1536 // 8,96 // 2,48 // 2,32 // 2,32 // 4) self.decoder:DNTD = DNTD(encoder_out_channels,encoder_new_out_channels,drop_rate=drop_rate) elif base_modelname == "efficientnet-b0": self.encoder:EfficientNet = EfficientNet.from_name('efficientnet-b0',override_params={'num_classes': None}) encoder_out_channels = (1280,80,40,24,32) encoder_new_out_channels = (1280 // 8,80 // 2,40 // 2,24 // 2,32 // 2) self.decoder:DNTD = DNTD(encoder_out_channels,encoder_new_out_channels,drop_rate=drop_rate) else: raise Exception("No model selected") for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0)
def __init__(self, num_class, use_pretrained = True): super(Double_Net, self).__init__() self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") self.num_class = num_class self.backbone_1 = EfficientNet.from_name('efficientnet-b0', num_classes=2, include_top = False, in_channels=3) self.backbone_2 = EfficientNet.from_name('efficientnet-b0', num_classes=2, include_top = False, in_channels=3) self.dense = nn.Sequential(nn.Linear(in_features = 1280 * 2, out_features = self.num_class), nn.Softmax(dim = 1))
def get_model(model_name, channels_num, classes_num=2, image_size=64): # resnext mode if 'resnext_50' in model_name: model = resnet.resnext50_32x4d(in_chs=channels_num, classes_num=classes_num) elif 'resnext_101' in model_name: model = resnet.resnext101_32x8d(in_chs=channels_num, classes_num=classes_num) # resnet mode elif 'resnet_34' in model_name: model = resnet.resnet34(in_chs=channels_num, classes_num=classes_num) elif 'resnet_50' in model_name: model = resnet.resnet50(in_chs=channels_num, classes_num=classes_num) elif 'resnet_101' in model_name: model = resnet.resnet101(in_chs=channels_num, classes_num=classes_num) elif 'wide_resnet50_2' in model_name: model = resnet.wide_resnet50_2(in_chs=channels_num, classes_num=classes_num) # efficientnet mode elif 'efficientnet_b4' in model_name: model = ef_net.from_name('efficientnet-b4', channels_num, image_size=image_size, num_classes=classes_num) elif 'efficientnet_b5' in model_name: model = ef_net.from_name('efficientnet-b5', channels_num, image_size=image_size, num_classes=classes_num) elif 'efficientnet_b7' in model_name: model = ef_net.from_name('efficientnet-b7', channels_num, image_size=image_size, num_classes=classes_num) else: # _, global_params = efficientnet(image_size=image_size, num_classes=classes_num) model = ef_net.from_name('efficientnet-b0', channels_num, image_size=image_size, num_classes=classes_num) return model
def mdl(type): if type == "res18": model_ft = models.resnet18(pretrained=False) num_ftrs = model_ft.fc.in_features model_ft.fc = nn.Linear(num_ftrs, 2) return model_ft elif type == "res50": model_ft = models.resnet50(pretrained=False) num_ftrs = model_ft.fc.in_features model_ft.fc = nn.Linear(num_ftrs, 2) return model_ft elif type == "eff-b6": model = EfficientNet.from_name('efficientnet-b6', num_classes=2) return model elif type == "eff-b3": model = EfficientNet.from_name('efficientnet-b3', num_classes=2) return model elif type == "dns201": model = torch.hub.load('pytorch/vision:v0.9.0', 'densenet201', pretrained=False) return model elif type == "rsnxt-50": model = pretrainedmodels.__dict__["se_resnext50_32x4d"]( num_classes=2, pretrained=None) return model
def create_pretrained_model(args): if args.gpu is not None: print("Use GPU: {} for training".format(args.gpu)) if args.distributed: if args.dist_url == "env://" and args.rank == -1: args.rank = int(os.environ["RANK"]) if args.multiprocessing_distributed: # For multiprocessing distributed training, rank needs to be the # global rank among all the processes args.rank = args.rank * ngpus_per_node + gpu dist.init_process_group(backend=args.dist_backend, init_method=args.dist_url, world_size=args.world_size, rank=args.rank) # create model if args.pretrained: print("=> using pre-trained model '{}'".format(args.arch)) os.environ['TORCH_HOME'] = '../pre-trained_model/pytorch' # 替换默认目录 if not mox.file.exists( '../pre-trained_model/pytorch/efficientnet-b7-dcc49843.pth'): mox.file.copy( 's3://xianbucket/efficientnet-b7-dcc49843.pth', '../pre-trained_model/pytorch/efficientnet-b7-dcc49843.pth') print( 'copy pre-trained model from OBS to: %s success' % (os.path.abspath( '../pre-trained_model/pytorch/efficientnet-b7-dcc49843.pth' ))) else: print('use exist pre-trained model at: %s' % (os.path.abspath( '../pre-trained_model/pytorch/efficientnet-b7-dcc49843.pth'))) print("*" * 10) # 加载模型结构 # model = models.__dict__[args.arch](pretrained=False) model = EfficientNet.from_name('efficientnet-b7') # 加载模型参数 model.load_state_dict( torch.load( os.path.abspath( '../pre-trained_model/pytorch/efficientnet-b7-dcc49843.pth' ))) # 用Adam优化器 model._fc.out_features = args.num_classes # print(model) else: print("=> creating model '{}'".format(args.arch)) # model = models.__dict__[args.arch]() model = EfficientNet.from_name('efficientnet-b7') # num_ftrs = model.fc.in_features # model.fc = nn.Linear(num_ftrs, args.num_classes) return model
def test(test_list): normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) test_dataset = MyDataset(test_list, transform=transforms.Compose([ transforms.Resize(opt.image_size), transforms.ToTensor(), normalize ])) if torch.cuda.is_available(): #gpu checkpoint = torch.load(opt.model) model = EfficientNet.from_name(opt.arch, override_params={ 'num_classes': opt.num_class }).cuda() test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False, num_workers=opt.worker, pin_memory=True) else: checkpoint = torch.load(opt.model, map_location='cpu') model = EfficientNet.from_name( opt.arch, override_params={'num_classes': opt.num_class}) test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False, num_workers=opt.worker, pin_memory=False) model.load_state_dict(checkpoint['net']) model.eval() with torch.no_grad(): for (image, name) in test_dataset: if torch.cuda.is_available(): image = image.cuda(0, non_blocking=True) image = image.unsqueeze(0) output = model(image) _, pred = torch.max(output, 1) if torch.cuda.is_available(): results.append({ "image_id": name, "disease_class": int(pred.cpu().numpy()[0]) + 1 }) else: results.append({ "image_id": name, "disease_class": int(pred.numpy()[0]) + 1 }) json_str = json.dumps(results) f.write(json_str) f.close()
def __init__(self, num_classes): super().__init__() self.effnet = EfficientNet.from_name("efficientnet-b4") self.dropout = nn.Dropout(0.1) self.out = nn.Linear(1792, num_classes) self.step_scheduler_after = "epoch"
def __init__(self): super(Mean_Model, self).__init__() d = torch.load('./pretrained_models/food-101-train-epoch-10.pth') self.body_3 = EfficientNet.from_name("efficientnet-b4") self.body_3.load_state_dict(d) self.body_1 = torchvision.models.mobilenet_v2(pretrained=False) self.body_1 = nn.Sequential(*(list(self.body_1.children())[:-1])) self.body_2 = torchvision.models.mobilenet_v2(pretrained=True) self.body_2 = nn.Sequential(*(list(self.body_2.children())[:-1])) self.mid = nn.Sequential( nn.MaxPool2d(7), Flatten() ) self.head = nn.Sequential( nn.Linear(4352, 512), nn.ReLU(inplace=True), nn.Linear(512, 256), nn.ReLU(inplace=True), nn.Linear(256, 1) ) # freeze model weight for name, param in self.body_2.named_parameters(): param.requires_grad = False for name, param in self.body_3.named_parameters(): param.requires_grad = False
def net(self, net): if net == "ResNet50": Net = models.resnet50(pretrained=self.pretrained) Net.fc = nn.Linear(2048, self.class_num) elif net == 'DenseNet121': Net = models.densenet121(pretrained=True) Net.fc = nn.Linear(1024, self.class_num) elif net == 'SENet': Net = models.squeezenet1_0(pretrained=True) Net.classifier = nn.Sequential( nn.Dropout(p=0.5), nn.Conv2d(512, self.class_num, kernel_size=1), nn.ReLU(inplace=True), nn.AdaptiveAvgPool2d((1, 1))) elif net == 'pnasnet': model_name = 'pnasnet5large' if self.pretrained: Net = pretrainedmodels.__dict__[model_name]( num_classes=1000, pretrained='imagenet') else: Net = pretrainedmodels.__dict__[model_name](num_classes=1000) Net.last_linear = nn.Linear(4320, 2) Net.eval() elif net == 'efficientNet': if self.pretrained: Net = EfficientNet.from_pretrained('efficientnet-b0') else: Net = EfficientNet.from_name('efficientnet-b0') Net._fc = nn.Linear(1280, self.class_num, bias=True) Net.eval() return Net
def __init__(self, extra_length, hyperparams): super().__init__(name="VideoNetEmbed", requires_embed=True, hyperparams=hyperparams) input_length = hyperparams["input_length"] hidden_length = hyperparams["hidden_length"] layer_number = hyperparams["layer_number"] linear_hidden_length = hyperparams["linear_hidden_length"] drop_out_rate = hyperparams["drop_out_rate"] self.ec_model = EfficientNet.from_name("efficientnet-b4") net_weight = torch.load( os.path.join(config.efficient_path, "efficientnet-b4-6ed6700e.pth")) self.ec_model.load_state_dict(net_weight) self.lstm = nn.LSTM( input_size=input_length, hidden_size=hyperparams["hidden_length"], num_layers=hyperparams["layer_number"], batch_first=True, dropout=hyperparams["drop_out_rate"], ) self.fc_input_length = hidden_length * layer_number self.fc = nn.Sequential( nn.Linear(hidden_length * layer_number + extra_length, linear_hidden_length), nn.ReLU(), nn.Dropout(drop_out_rate), nn.Linear(linear_hidden_length, config.bin_number), )
def get_model(opts): """ Load the model depending on the specified options """ if opts.model == 'dense-169': model = load_densenet169(opts.n_classes).cpu() elif opts.model == 'dense-121': model = load_densenet121(opts.n_classes).cpu() elif opts.model == 'dense-201': model = load_densenet201(opts.n_classes).cpu() elif opts.model == 'covidnet-small': model = CovidNet(opts, model='small').cpu() elif opts.model == 'covidnet-large': model = CovidNet(opts, model='large').cpu() elif 'efficientnet' in opts.model: model = EfficientNet.from_name(opts.model, override_params={ "num_classes": opts.n_classes, 'image_size': opts.img_size }) else: raise NotImplementedError model.n_classes = opts.n_classes return model
def __init__(self, model_name, num_classes, drop_rate=0, pretrained=True): """ Args: model_name: model_name: resnet模型的名称;类型为str num_classes: num_classes: 类别数目;类型为int drop_rate: float, 分类层中的drop out系数 pretrained: bool, 是否使用预训练权重 """ super(CustomModel, self).__init__() self.model_name = model_name self.num_classes = num_classes if self.model_name.startswith('efficientnet'): if pretrained: model = EfficientNet.from_pretrained( self.model_name, num_classes=self.num_classes) else: model = EfficientNet.from_name( self.model_name, override_params={'num_classes': self.num_classes}) # 声明特征提取层,池化层与全连接层 self.feature_layer = model in_features = model._conv_head.out_channels elif self.model_name in [ 'resnext101_32x8d_wsl', 'resnext101_32x16d_wsl', 'resnext101_32x32d_wsl', 'resnext101_32x48d_wsl' ]: model = getattr(resnext, self.model_name)(self.num_classes, pretrained=pretrained) self.feature_layer = nn.Sequential(*list(model.children())[:-2]) in_features = model.fc.in_features else: if pretrained: pretrained_type = 'imagenet' else: pretrained_type = None model = getattr(pretrainedmodels, self.model_name)(pretrained=pretrained_type) if hasattr(model, 'avgpool') or hasattr(model, 'avg_pool'): self.feature_layer = nn.Sequential( *list(model.children())[:-2]) else: self.feature_layer = nn.Sequential( *list(model.children())[:-1]) if hasattr(model.last_linear, 'in_features'): in_features = model.last_linear.in_features elif hasattr(model.last_linear, 'in_channels'): in_features = model.last_linear.in_channels else: assert NotImplementedError self.pool = nn.AdaptiveAvgPool2d(output_size=(1, 1)) add_block = [nn.Linear(in_features, 1024), nn.ReLU()] if drop_rate > 0: add_block += [nn.Dropout(p=drop_rate)] add_block += [nn.Linear(1024, self.num_classes)] self.classifier = nn.Sequential(*add_block)
def get_efficientnet_b0(task, weight=None, pretrained=False): """ Parameters ---------- task: str kind of task. weight: torch.dict pretrained weight in training section. so, this parameter use in inference. pretrained: bool load torchvision model weight. """ model = EfficientNet.from_name("efficientnet-b0") if pretrained: model.load_state_dict( torch.load( Path(__file__).parents[2] / "model" / "pretrain" / "efficientnet-b0.pth")) num_features = model._fc.in_features if task == "classifier": model._fc = nn.Sequential( nn.Linear(in_features=num_features, out_features=5), ) elif task == "regression": model._fc = nn.Sequential( nn.Linear(in_features=num_features, out_features=1)) else: print("{} task isn't implemented.".format(task)) raise NotImplementedError if weight is not None: model.load_state_dict(weight) return model
def load_model(self, model_file): checkpoint = torch.load(model_file, map_location=self.device) args = checkpoint['args'] try: rgb_mean = [float(mean) for mean in args.rgb_mean.split(',')] rgb_std = [float(std) for std in args.rgb_std.split(',')] except AttributeError: rgb_mean = args.rgb_mean rgb_std = args.rgb_std try: interpolation = args.interpolation except AttributeError: interpolation = 'BILINEAR' model_arch = checkpoint['arch'] num_classes = checkpoint.get('num_classes', 0) if model_arch.startswith('efficientnet'): model = EfficientNet.from_name(model_arch) num_features = model._fc.in_features model._fc = torch.nn.Linear(num_features, num_classes) else: model = make_model(model_arch, num_classes=num_classes, pretrained=False) model.load_state_dict(checkpoint['model']) class_names = checkpoint.get('class_names', []) model.to(self.device) model.eval() return model, class_names, rgb_mean, rgb_std, interpolation
def load_efficientnet(name, num_class=3): if not name in [ 'efficientnet-b0', 'efficientnet-b1', 'efficientnet-b2', 'efficientnet-b3', 'efficientnet-b4', 'efficientnet-b5', 'efficientnet-b6', 'efficientnet-b7' ]: # raise ValueError("name must be in {efficientnet-b1}") sys.exit() # print('load {}'.format(name)) # model = EfficientNet.from_pretrained(name, num_classes = num_class,advprop=False) model = EfficientNet.from_name(name) # for index,param in enumerate(model.parameters()): # # if index != 300: # param.requires_grad = False # model.fc = nn.Sequential( # nn.Linear(1280, 256), # nn.ReLU(), # nn.Dropout(0.4), # nn.Linear(256, num_class), # nn.LogSoftmax(dim=1)) # fc_layer = nn.Sequential() # fc_layer.add_module('fc_1', nn.Linear(1280, 256, bias = True)) # fc_layer.add_module('fc_1_act', nn.Sigmoid()) # fc_layer.add_module('fc_2', nn.Linear(265, num_class, bias = True)) # model.fc = fc_layer path = '/data1/phuong/checkpoints/Efficientnet_transfer/1592156276_efficient_transfer_500_epoch/Checkpoint.pth' model._fc = nn.Linear(1280, num_class) # checkpoint = torch.load(path) # model.load_state_dict(checkpoint['model_state_dict']) # unfreeze(model) return model
def predict(args): detect_net = build_ssd("test", 300, 21) # initialize SSD detect_net.load_state_dict( torch.load(args.detect_model, map_location="cpu")) transform = BaseTransform(detect_net.size, (104 / 256.0, 117 / 256.0, 123 / 256.0)) net = EfficientNet.from_name('efficientnet-b2', override_params={'num_classes': 11000}) net.load_state_dict(torch.load(args.classify_model, map_location='cpu')) net.eval() softmax = nn.Softmax(dim=1) img = cv2.imread(args.image_file) t0 = time.time() # Just find the most possible bird (if there are many) img = detect(img, detect_net, transform) img = cv2.resize(img, IMAGE_SHAPE) tensor_img = torch.from_numpy(img) result = net(tensor_img.unsqueeze(0).permute(0, 3, 1, 2).float()) result = softmax(result) values, indices = torch.topk(result, 10) t1 = time.time() print(indices) labelmap = load_label_map("labelmap.csv") for id, accu in zip(indices[0].tolist(), values[0].tolist()): print("{:1.4f}, {}".format(accu, labelmap.get(id, "Unknown"))) print('time:', t1 - t0)
def __init__(self, device): self.device = device self.model = {} self.model['lgsc'] = corelib.LGSC(drop_ratio=0.4) checkpoint = torch.load("weights/lgsc_siw_pretrained.pth", map_location=lambda storage, loc: storage) new_dict = {} for key in checkpoint['backbone'].keys(): new_dict[key.replace("module.", "")] = checkpoint['backbone'][key] self.model['lgsc'].load_state_dict(new_dict) self.model['lgsc'].to(self.device) self.model['lgsc'].eval() # core self.model['classtify'] = EfficientNet.from_name('efficientnet-b0', num_classes=2) checkpoint = torch.load("weights/Iter_035000_net.ckpt", map_location=lambda storage, loc: storage) # print(checkpoint.keys()) self.model['classtify'].load_state_dict(checkpoint['net_state_dict']) self.model['classtify'].to(self.device) self.model['classtify'].eval() self.tranform = {} self.tranform["classtify"] = transforms.Compose([ transforms.Resize((112, 112)), transforms.ToTensor(), # range [0, 255] -> [0.0,1.0] transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) # range [0.0, 1.0] -> [-1.0,1.0] ]) self.tranform["lgsc"] = alt.Compose([alt.Resize(224, 224, p=1), alt.Normalize(), ToTensor()])
def _efficientnet(model_name, pretrained): if pretrained: model = EfficientNet.from_pretrained(model_name, advprop=True) else: model = EfficientNet.from_name(model_name) return model
def __init__(self, class_n, rate=0.1, use_pretrain=True): super(CNN, self).__init__() model = EfficientNet.from_name('efficientnet-b2') pretrained_dict = OrderedDict() model_dict = model.state_dict() checkpoint = torch.load(ck_dir, map_location=torch.device('cpu')) for k, v in checkpoint.items(): name = k.replace("module.", "") pretrained_dict[name] = v pretrained_dict = { k: v for k, v in pretrained_dict.items() if k in model_dict } model_dict.update(pretrained_dict) model.load_state_dict(model_dict) # for n,p in model.named_parameters(): # if '_fc' not in n: # p.requires_grad = False # model = torch.nn.parallel.DistributedDataParallel(model) self.model = model
def __init__(self, model_name='efficientnet-b3', num_classes=1000, pretrained=True): super().__init__() MODEL_TYPE={'resnet-50','resnet-101','resnet-152',\ 'efficientnet-b0','efficientnet-b1','efficientnet-b2','efficientnet-b3'} assert model_name in MODEL_TYPE, f"Available : {MODEL_TYPE}" baseline, version = model_name.split('-') if baseline == 'resnet': if version == '50': self.backbone = models.resnet50(pretrained=pretrained) elif version == '101': self.backbone = models.resnet101(pretrained=pretrained) elif version == '152': self.backbone = models.resnet152(pretrained=pretrained) in_features = self.backbone.fc.in_features self.backbone.fc = nn.Linear(in_features, num_classes) elif baseline == 'efficientnet': if pretrained: self.backbone = EfficientNet.from_pretrained( model_name, num_classes=num_classes) else: self.backbone = EfficientNet.from_name(model_name, num_classes=num_classes)
def __init__(self): super(Classifier, self).__init__() self.resnet = EfficientNet.from_name('efficientnet-b0') self.l1 = nn.Linear(1000, 256) self.dropout = nn.Dropout(0.75) self.l2 = nn.Linear(256, 6) self.relu = nn.ReLU()
def __init__(self, model_name, hidden_dim, dropout=0.5, activation='relu', output_class=15): super().__init__() if activation.lower() == 'relu': activation = F.relu elif activation.lower() == 'mish': activation = Mish() self.model_name = model_name self.hidden_dim = hidden_dim if model_name.startswith('efficientnet'): self.cnn_model = EfficientNet.from_name(model_name) dim_feats = self.cnn_model._fc.in_features else: self.cnn_model = pretrainedmodels.__dict__[model_name]( num_classes=1000, pretrained='imagenet') dim_feats = self.cnn_model.last_linear.in_features self.linear1 = nn.Linear(dim_feats, hidden_dim) output_classes = output_class self.linear2 = nn.Linear(hidden_dim, output_classes) self.dropout = nn.Dropout(p=dropout) if dropout else None self.pool = nn.AdaptiveAvgPool2d((1, 1)) self.act = activation
def b7(pretrained=True, num_classes=1000): if pretrained: return EfficientNet.from_pretrained('efficientnet-b7', num_classes=num_classes) else: return EfficientNet.from_name('efficientnet-b7', num_classes=num_classes)
def __init__(self, num_classes: int): super(Model, self).__init__() self.n_mels = config.getint('AUDIO', 'n_mels') self.sr = config.getint('AUDIO', 'sr') self.num_classes = num_classes preprocess_steps = list() preprocess_steps.append(Normalize()) preprocess_steps.append( torchaudio.transforms.MelSpectrogram(sample_rate=self.sr, n_fft=400, win_length=400, hop_length=160, n_mels=self.n_mels)) preprocess_steps.append(Normalize3D()) self.eval_preprocess_steps = nn.Sequential(*tuple(preprocess_steps)) preprocess_steps.append( torchaudio.transforms.FrequencyMasking(freq_mask_param=10)) preprocess_steps.append( torchaudio.transforms.TimeMasking(time_mask_param=15)) self.train_preprocess_steps = nn.Sequential(*tuple(preprocess_steps)) self.net = EfficientNet.from_name("efficientnet-b0", include_top=False) self.net._change_in_channels(in_channels=1) self.classifier = nn.Linear(1280, self.num_classes) self.gelu = torch.nn.GELU() self.emb_fc = nn.Linear( 1280, config.getint("HYPERPARAMS", "embeddings_size"))
def __init__(self, hyperparameters, out_dims=config.bin_number, extra_length=0): super().__init__(name="PictureNet", requires_embed=False, hyperparams=hyperparameters) linear_hidden_length = hyperparameters["linear_hidden_length"] self.ec_model = EfficientNet.from_name("efficientnet-b4") net_weight = torch.load( os.path.join(config.efficient_path, "efficientnet-b4-6ed6700e.pth")) self.ec_model.load_state_dict(net_weight) self.grad_layer_name = hyperparameters["grad_layer_name"] grad = False for name, param in self.ec_model.named_parameters(): if self.grad_layer_name in name: grad = True param.requires_grad = grad feature_num = self.ec_model._fc.in_features self.fcs = nn.Sequential( nn.Linear(feature_num + extra_length, linear_hidden_length), nn.ReLU(), nn.Dropout(hyperparameters["drop_out_rate"]), nn.Linear(linear_hidden_length, out_dims), )
def load_model_EfficientNet(model_name, model_path, num_classes, device): model = EfficientNet.from_name(model_name, num_classes=18) model.to(device) model.load_state_dict(torch.load(model_path, map_location=device)) return model
def __init__(self, encoder='efficientnet-b0'): super(Model, self).__init__() n_channels_dict = { 'efficientnet-b0': 1280, 'efficientnet-b1': 1280, 'efficientnet-b2': 1408, 'efficientnet-b3': 1536, 'efficientnet-b4': 1792, 'efficientnet-b5': 2048, 'efficientnet-b6': 2304, 'efficientnet-b7': 2560 } params_dict = { # Coefficients: width,depth,res,dropout 'efficientnet-b0': (1.0, 1.0, 224, 0.2), 'efficientnet-b1': (1.0, 1.1, 240, 0.2), 'efficientnet-b2': (1.1, 1.2, 260, 0.3), 'efficientnet-b3': (1.2, 1.4, 300, 0.3), 'efficientnet-b4': (1.4, 1.8, 380, 0.4), 'efficientnet-b5': (1.6, 2.2, 456, 0.4), 'efficientnet-b6': (1.8, 2.6, 528, 0.5), 'efficientnet-b7': (2.0, 3.1, 600, 0.5), 'efficientnet-b8': (2.2, 3.6, 672, 0.5), 'efficientnet-l2': (4.3, 5.3, 800, 0.5), } self.out_chns = 0 self.net = EfficientNet.from_name(encoder) self.avg_pool = nn.AdaptiveAvgPool2d(1) self.out_chns += n_channels_dict[encoder] self.fc = nn.Linear(self.out_chns, 1) self.dropouts = nn.ModuleList( [nn.Dropout(config['dropout']) for _ in range(5)])
def eval_traindata(args): net = EfficientNet.from_name('efficientnet-b2', override_params={ 'image_size': 300, 'num_classes': 11000 }) net.load_state_dict(torch.load(args.trained_model)) net.eval().cuda() file_list = get_file_list(args.dataset_root) thread_list = [] for index in range(NR_THREADS): thread = FileThread(file_list, index, NR_THREADS) thread.start() thread_list.append(thread) eval_thread = EvalThread(net, NR_THREADS) eval_thread.start() eval_thread.join() eval_thread.dump() for thread in thread_list: thread.join()
def __init__(self, name='efficientnet-b0', head='mlp', feat_dim=128, pretrained=True, **kwargs): super().__init__() if pretrained: self.encoder = EfficientNet.from_pretrained(name, include_top=False) else: self.encoder = EfficientNet.from_name('efficientnet-b0', include_top=False) freeze_bn(self.encoder) # prj head in_feature = model_dict[name] if head == 'linear': self.head = nn.Linear(in_feature, feat_dim) elif head == 'mlp': self.head = nn.Sequential(nn.Linear(in_feature, in_feature), nn.ReLU(inplace=True), nn.Linear(in_feature, feat_dim)) # Classify layer num_classes = kwargs.get('num_classes') self._dropout = nn.Dropout(0.5) self._fc = nn.Linear(in_feature, num_classes)
def __init__(self, variant, num_classes, pretrained=True, activation=None): super().__init__() if 'efficientnet' not in variant: variant = f'efficientnet-{variant}' if pretrained: model = _EfficientNet.from_pretrained(variant, num_classes=num_classes) else: model = _EfficientNet.from_name(variant, {'num_classes': num_classes}) self.model = model self.model._fc = nn.Sequential( LambdaLayer(lambda x: x.unsqueeze_(0)), nn.AdaptiveAvgPool1d(self.model._fc.in_features), LambdaLayer(lambda x: x.squeeze_(0).view(x.size(0), -1)), self.model._fc) if callable(activation) or activation is None: self.activation = activation elif activation == 'softmax': self.activation = nn.Softmax(dim=1) elif activation == 'sigmoid': self.activation = nn.Sigmoid() else: raise ValueError( 'Activation should be "sigmoid"/"softmax"/callable/None')