def _resnext(arch, block, layers, pretrained, progress, **kwargs): model = ResNet(block, layers, **kwargs) if pretrained == True: state_dict = load_state_dict_from_url(model_urls[arch], progress=progress) model.load_state_dict(state_dict) return model
def se_resnet50(num_classes=199, pretrained=False): model = ResNet(SEBottleneck, [3, 4, 6, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) if pretrained: model.load_state_dict( load_state_dict_from_url( "https://github.com/moskomule/senet.pytorch/releases/download/archive/seresnet50-60a8950a85b2b.pkl" )) return model
def resnest269(pretrained=False, root='~/.encoding/models', **kwargs): model = ResNet(Bottleneck, [3, 30, 48, 8], radix=2, groups=1, bottleneck_width=64, deep_stem=True, stem_width=64, avg_down=True, avd=True, avd_first=False, **kwargs) if pretrained: model.load_state_dict(torch.hub.load_state_dict_from_url( resnest_model_urls['resnest269'], progress=True, check_hash=True)) return model
def build(self, config): """Build Mask R-CNN architecture. """ # Image size must be dividable by 2 multiple times h, w = config.TRAIN.IMAGE_SHAPE[:2] if h / 2**6 != int(h / 2**6) or w / 2**6 != int(w / 2**6): raise Exception( "Image size must be dividable by 2 at least 6 times " "to avoid fractions when downscaling and upscaling." "For example, use 256, 320, 384, 448, 512, ... etc. ") # Build the shared convolutional layers. # Bottom-up Layers # Returns a list of the last layers of each stage, 5 in total. # Don't create the thead (stage 5), so we pick the 4th item in the list. resnet = ResNet("resnet101", stage5=True) C1, C2, C3, C4, C5 = resnet.stages() # Top-down Layers # TODO: add assert to varify feature map sizes match what's in config self.fpn = FPN(C1, C2, C3, C4, C5, out_channels=256) # Generate Anchors self.anchors = Variable(torch.from_numpy( utils.generate_pyramid_anchors(config.RPN.ANCHOR_SCALES, config.RPN.ANCHOR_RATIOS, config.FPN.BACKBONE_SHAPES, config.FPN.BACKBONE_STRIDES, config.RPN.ANCHOR_STRIDE)).float(), requires_grad=False) if self.config.GPU_COUNT: self.anchors = self.anchors.cuda() # RPN self.rpn = RPN(len(config.RPN.ANCHOR_RATIOS), config.RPN.ANCHOR_STRIDE, 256) # FPN Classifier self.classifier = Classifier(256, config.ROIS.POOL_SIZE, config.TRAIN.IMAGE_SHAPE, config.MODEL.NUM_CLASSES) # FPN Mask self.mask = Mask(256, config.MRCNN.POOL_SIZE, config.TRAIN.IMAGE_SHAPE, config.MODEL.NUM_CLASSES) # Fix batch norm layers def set_bn_fix(m): classname = m.__class__.__name__ if classname.find('BatchNorm') != -1: for p in m.parameters(): p.requires_grad = False self.apply(set_bn_fix)
class ResNetLift(nn.Module): def __init__(self, network, num_joints, num_layers, num_features, mode, model_2d_path, model_lift_path): super(ResNetLift, self).__init__() # 2d pose estimation module self.model_2d = ResNetInt(network, num_joints) if model_2d_path is not None: if os.path.isfile(model_2d_path): print('Load pretrained 2D pose estimation model..') state = torch.load(model_2d_path) pretrained_dict = state['model'] model_dict = self.model_2d.state_dict() new_pretrained_dict = { k[7:]: v for k, v in pretrained_dict.items() if k[7:] in model_dict } model_dict.update(new_pretrained_dict) self.model_2d.load_state_dict(model_dict) else: raise ValueError('model does not exist: %s' % model_2d_path) # 2d-to-3d pose lifting module self.model_lift = ResNet(mode, num_joints, num_layers, num_features) if model_lift_path is not None: if os.path.isfile(model_lift_path): print('Load pretrained 2D pose estimation model..') state = torch.load(model_lift_path) pretrained_dict = state['model'] model_dict = self.model_lift.state_dict() new_pretrained_dict = { k[7:]: v for k, v in pretrained_dict.items() if k[7:] in model_dict } model_dict.update(new_pretrained_dict) self.model_lift.load_state_dict(model_dict) else: raise ValueError('model does not exist: %s' % model_lift_path) def forward(self, inp): [img, bbox, cam_c] = inp # 2d prediction [H, pred2d] = self.model_2d(img) # 3d prediction pose2d = pred2d.clone() [pose_local, depth_root] = self.model_lift([pose2d, bbox, cam_c]) return [H, pred2d, pose_local, depth_root] def set_fliptest(self, val): self.model_2d.set_fliptest(val) self.model_lift.set_fliptest(val)
def test_params_resnet(full_pca_img, partial_pca_img, mask, loss_name, grad_clipping, num_blocks, num_channels): start_time = time.time() img_var = np_to_torch(partial_pca_img).type(dtype) mask_var = np_to_torch(mask).type(dtype) LR = 0.01 INPUT = 'noise' input_depth = partial_pca_img.shape[0] output_depth = partial_pca_img.shape[0] net = ResNet(input_depth, output_depth, num_blocks, num_channels, act_fun='LeakyReLU') net = net.type(dtype) net_input = get_noise(input_depth, INPUT, partial_pca_img.shape[1:],var=1).type(dtype) net_input_saved = net_input.detach().clone() noise = net_input.detach().clone() net_input = net_input_saved optimizer = torch.optim.AdamW(net.parameters(), lr=LR) scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.1, verbose=False, patience=100, threshold=0.0005, threshold_mode='rel', cooldown=0, min_lr=5e-6) for j in range(num_iter): out = net(net_input) optimizer.zero_grad() if loss_name == 'mse': mse = torch.nn.MSELoss().type(dtype) total_loss = mse(out * mask_var, img_var * mask_var) elif loss_name == 'master_metric': total_loss = -master_metric((out * mask_var), (img_var * mask_var), 1, 1, 1, 'product') else: raise ValueError("Input a correct loss name (among 'mse' | 'master_metric'") total_loss.backward() if grad_clipping: for param in net.parameters(): param.grad.data.clamp_(-1, 1) optimizer.step() scheduler.step(total_loss) out_np = torch_to_np(out) elapsed = time.time() - start_time return get_final_metrics(out_np,full_pca_img), elapsed
def main(): loader = prepare_cifar10() last_epoch = 0 # model = GoogleNet(mode='improved', aux=False).to(device) model = ResNet(layer_num='50').to(device) model_name = model.__class__.__name__ + '_' + model.mode criterion = nn.CrossEntropyLoss().to(device) optimizer = optim.Adam(model.parameters(), lr=learning_rate, weight_decay=5e-4) if pretrained is not None: print('load %s...' % pretrained) checkpoint = torch.load(os.path.join('./saved_models', pretrained)) pattern = r'_[0-9]+\.' last_epoch = int(re.findall(pattern, pretrained)[-1][1:-1]) if device.type == 'cuda': load_parallel_state_dict(model, checkpoint['state_dict']) else: model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) print('loading pretrained model finished') hyperparameters = { 'batch_size': batch_size, 'learning_rate': learning_rate, 'num_epochs': num_epochs, 'optimizer': optimizer, 'loss_function': criterion } settings = { 'print_every': print_every, 'verbose': verbose, 'save_log': is_log, 'start_epoch': last_epoch + 1, 'save_model': save_frequency, 'name': model_name, 'device': device } trainer = ResNetTrainer(model, loader, hyperparameters, settings) # trainer = GoogleNetTrainer(model, loader, hyperparameters, settings) if is_train: trainer.train() else: trainer.test()
def __init__(self, backbone, neck, head, train_cfg=None, test_cfg=None, pretrained=None): super(Classifier, self).__init__() self.dropout = backbone.pop('dropout') self.backbone = ResNet(**backbone) self.train_cfg = train_cfg self.test_cfg = test_cfg self.triple_loss = TripletLoss() self.avgpool = nn.Pool2D(pool_type='avg', global_pooling=True) self.fc = nn.Linear(512, 2, act='softmax') self.init_weights(pretrained=pretrained)
def get_model(model_name, classes=264, pretrained=True): print(f'loading {model_name}') if model_name == "resnet50": model = ResNet( # type: ignore base_model_name=model_name, pretrained=pretrained, num_classes=classes) return model elif "resnest50" in model_name: model = get_resnest("resnest50", classes=classes, pretrained=pretrained) return model elif "efficientnet" in model_name: model = get_effdet(model_name, classes=classes) return model elif "pyconvhgresnet" in model_name: model = get_pyconvhgresnet(classes=classes, pretrained=pretrained) return model elif "resnet_sk2" in model_name: model = get_resnet_sk2(classes=classes, pretrained=pretrained) return model elif "se_resnet50_32x4d" in model_name: model = get_se_resnet50_32x4d(classes=classes, pretrained=pretrained) return model else: raise NotImplementedError
def resnest18(pretrained=False, root='~/.encoding/models', **kwargs): model = ResNet(Bottleneck, [2, 2, 2, 2], radix=2, groups=1, bottleneck_width=64, deep_stem=True, stem_width=32, avg_down=True, avd=True, avd_first=False, **kwargs) if pretrained: # 官方没有提供resnest18的预训练模型,我这里用resnest50的预训练模型加载 weight = torch.hub.load_state_dict_from_url( resnest_model_urls['resnest50'], progress=True, check_hash=True) model_dict = model.state_dict() for k,v in weight.items(): if k in model_dict.keys(): model_dict[k] = v model.load_state_dict(model_dict) return model
def build_model(model, input_shape, feature_dims, num_classes): if model == "resnet": model = ResNet(input_shape, feature_dims, num_classes) else: raise NotImplementedError return model
def get_net(input_depth, NET_TYPE, pad, upsample_mode, n_channels=3, act_fun='LeakyReLU', skip_n33d=128, skip_n33u=128, skip_n11=4, num_scales=5, downsample_mode='stride'): if NET_TYPE == 'ResNet': # TODO net = ResNet(input_depth, 3, 10, 16, 1, nn.BatchNorm2d, False) elif NET_TYPE == 'skip': net = skip(input_depth, n_channels, num_channels_down = [skip_n33d]*num_scales if isinstance(skip_n33d, int) else skip_n33d, num_channels_up = [skip_n33u]*num_scales if isinstance(skip_n33u, int) else skip_n33u, num_channels_skip = [skip_n11]*num_scales if isinstance(skip_n11, int) else skip_n11, upsample_mode=upsample_mode, downsample_mode=downsample_mode, need_sigmoid=True, need_bias=True, pad=pad, act_fun=act_fun) elif NET_TYPE == 'texture_nets': net = get_texture_nets(inp=input_depth, ratios = [32, 16, 8, 4, 2, 1], fill_noise=False,pad=pad) elif NET_TYPE =='UNet': net = UNet(num_input_channels=input_depth, num_output_channels=3, feature_scale=4, more_layers=0, concat_x=False, upsample_mode=upsample_mode, pad=pad, norm_layer=nn.BatchNorm2d, need_sigmoid=True, need_bias=True) elif NET_TYPE == 'identity': assert input_depth == 3 net = nn.Sequential() else: assert False return net
def train(): parser = ArgumentParser() parser = Trainer.add_argparse_args(parser) parser.add_argument('--job_name', type=str) parser.add_argument('--bottleneck', type=int, default=128) parser.add_argument('--net_name', type=str) args = parser.parse_args() dataset_root = join_path('datasets', 'CelebA') batch_size = 64 num_workers = 0 attributes_size = CelebaDataset.image_attributes_size celebA_data_module = CelebADataModule(dataset_root, batch_size, num_workers) logger = TensorBoardLogger(join_path('output', args.job_name, 'tb_logs'), name='') checkpoint_callback = ModelCheckpoint( monitor='val_loss', dirpath=join_path('output', args.job_name, 'checkpoints'), filename=args.job_name + '-{epoch:02d}-{val_loss:.2f}', save_top_k=3, mode='min') if args.net_name == 'VAE': model = ConvVAE(args.bottleneck) elif args.net_name == 'ResNet': model = ResNet(args.bottleneck) elif args.net_name == 'CVAE': model = ConditionalConvVAE(args.bottleneck, attributes_size) elif args.net_name == 'VQVAE': model = VQVAE() else: raise ValueError('Invalid net_name, valid values would be: "VAE", "ResNet", "CVAE", "VQVAE".') trainer = Trainer.from_argparse_args(args, logger=logger, callbacks=[checkpoint_callback]) trainer.fit(model, datamodule=celebA_data_module)
def load_model(args): mode = args.mode if mode == 'n_o': print("Getting N-O distance prediction for %s" % (target)) model = NOModel(in_channels=51, out_channels_n_o=38, channels=64, num_blocks=25, dropout=0.1, dilation_list=[1, 2, 4]) model_name = 'model_n_o' elif mode == 'sce': print("Getting Sidechain Center distance prediction for %s" % (target)) model = ResNet(in_channels=51, out_channels=38, channels=64, num_blocks=25, dropout=0.1, dilation_list=[1, 2, 4]) model_name = 'model_sce' else: raise Exception("%s is not a valid mode" % (str(mode))) if args.cuda: model.cuda("cuda") device = torch.device('cuda') else: device = torch.device('cpu') model.load_state_dict( torch.load(join(trained_models, model_name), map_location=device)) model.eval() return model
class Classifier(fluid.dygraph.Layer): def __init__(self, backbone, neck, head, train_cfg=None, test_cfg=None, pretrained=None): super(Classifier, self).__init__() self.dropout = backbone.pop('dropout') self.backbone = ResNet(**backbone) self.train_cfg = train_cfg self.test_cfg = test_cfg self.triple_loss = TripletLoss() self.avgpool = nn.Pool2D(pool_type='avg', global_pooling=True) self.fc = nn.Linear(512, 2, act='softmax') self.init_weights(pretrained=pretrained) def init_weights(self, pretrained=None): self.backbone.init_weights(pretrained=pretrained) model_size(self) def get_losses(self, out, cls_out, mask, gt_labels): loss_cls = L.mean(L.cross_entropy(cls_out, gt_labels)) * self.train_cfg['w_cls'] loss_tir = 0 for feat in out[:-1]: feat = L.squeeze(self.avgpool(feat), axes=[2, 3]) loss_tir += self.triple_loss(feat, gt_labels) * self.train_cfg['w_tri'] loss = loss_cls + loss_tir return dict(loss_cls=loss_cls, loss_tir=loss_tir, loss=loss) def forward(self, img, label, mask=None, return_loss=True): outs = self.backbone(img) cls_out = self.avgpool(outs[-1]) if return_loss: cls_out = L.dropout(cls_out, dropout_prob=self.dropout, is_test=False) cls_out = self.fc(L.squeeze(cls_out, axes=[2, 3])) losses = self.get_losses(outs, cls_out, mask, label) return losses else: cls_out = self.fc(L.squeeze(cls_out, axes=[2, 3])) cls_out = L.softmax(cls_out).numpy()[:, 0] return cls_out
def search_policy(): model = ResNet(conf, arch_name=conf.arch, input_size=conf.image_size) model.load_state_dict( torch.load("result/baseline_2020_02_20_14_35_56/model_0.pkl")) val_df = pd.read_csv("val_index.csv") val_images = np.load("val_images.npy") faa = FastAutoAugment(val_df, val_images, model) # faa.test() study = optuna.create_study() study.optimize(faa.search, n_trials=200) print(study.best_trial) print(study.best_params)
def resnet18(pretrained=False): """Constructs a ResNet-34 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(BasicBlock, [2, 2, 2, 2]) if pretrained: model_dict = model.state_dict() pretrained_dict = model_zoo.load_url(model_urls['resnet18']) pretrained_dict = { k: v for k, v in pretrained_dict.items() if not k.startswith("fc") } model_dict.update(pretrained_dict) model.load_state_dict(model_dict) # model.load_state_dict(model_zoo.load_url(model_urls['resnet34'])) return model
def main(args): transform = getTransforms() data_path = args.input_data if not os.path.exists(data_path): print('ERROR: No dataset named {}'.format(data_path)) exit(1) dataset = EvalDataset(data_path, transform=transform) dataloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=False, num_workers=1) with open(args.class_list, 'r') as class_file: class_names = [] for class_name in class_file.readlines(): if len(class_name.strip()) > 0: class_names.append(class_name.strip()) model = ResNet(num_layers=18, num_classes=len(class_names)).to(DEVICE) model = model.eval() output_dir = os.path.join(data_path, 'out') os.makedirs(output_dir, exist_ok=True) model_file = args.model_file if os.path.exists(model_file): checkpoint = torch.load(model_file) if 'state_dict' in checkpoint.keys(): model.load_state_dict(checkpoint['state_dict'], strict=False) else: model.load_state_dict(checkpoint, strict=False) print('=> loaded {}'.format(model_file)) else: print('model_file "{}" does not exists.'.format(model_file)) exit(1) font = cv2.FONT_HERSHEY_SIMPLEX with torch.no_grad(): for data, path in dataloader: outputs = model(data.to(DEVICE)) _, predicted = torch.max(outputs.data, 1) predicted = predicted.to('cpu')[0].item() class_text = class_names[predicted] print(class_text, path) image = cv2.imread(path[0], cv2.IMREAD_COLOR) image = cv2.rectangle(image, (0, 0), (150, 25), (255, 255, 255), -1) image = cv2.rectangle(image, (0, 0), (150, 25), (255, 0, 0), 2) cv2.putText(image, class_text, (5, 15), font, 0.5, ( 255, 0, ), 1, cv2.LINE_AA) cv2.imwrite(os.path.join(output_dir, os.path.basename(path[0])), image)
def main(): global args, best_acc1, device criterion = PrototypicalLoss().to(device) cudnn.benchmark = True runs_path = glob('runs/*') max_len_exp = max([len(x) for x in runs_path]) + 2 print(f"|{'Experiment':^{max_len_exp}}|{'Loss':^17}|{'ACC':^17}|") except_list = [] pl_mi = u"\u00B1" for exp in glob('runs/*'): checkpoint, args = None, None files = glob(exp + '/*') for file in files: if file.endswith('model_best.pth'): checkpoint = torch.load(os.path.abspath(file)) elif file.endswith('.json'): params = json.load(open(os.path.abspath(file))) args = SimpleNamespace(**params) if checkpoint is None or args is None: except_list.append(f"checkpoint and params are not exist in {exp}") continue if args.dataset == 'omniglot': test_loader = get_dataloader(args, 'test') else: test_loader = get_dataloader(args, 'val') input_dim = 1 if args.dataset == 'omniglot' else 3 if args.model == 'protonet': model = ProtoNet(input_dim).to(device) else: model = ResNet(input_dim).to(device) model.load_state_dict(checkpoint['model_state_dict']) best_acc1 = checkpoint['best_acc1'] loss_list, acc_list = test(test_loader, model, criterion) loss, loss_moe = margin_of_error(loss_list) acc, acc_moe = margin_of_error(acc_list) loss_string = f'{loss:.3f} {pl_mi} {loss_moe:.3f}' acc_string = f'{acc:.3f} {pl_mi} {acc_moe:.3f}' print(f"|{exp:^{max_len_exp}}|{loss_string:^16}|{acc_string:^16}|") if len(except_list): pp(except_list)
def main(): exp_name = f'baseline_{now()}' device, log, result_dir = setup(exp_name, conf) train_df = load_csv(conf.train_csv) if conf.npy: train_images = np.load(conf.train_images) else: train_images = pd.read_parquet(conf.train_images) test_df = load_csv(conf.test_csv) if conf.npy: test_images = np.load(conf.test_images) else: test_images = pd.read_parquet(conf.test_images) log.info('done') for i in range(5): if i != conf.fold: continue if "resnet" in conf.arch or "resnext" in conf.arch: model_ft = ResNet(conf, arch_name=conf.arch, input_size=conf.image_size) model_ft.load_state_dict( torch.load("result/baseline_2020_03_21_13_01_08/model_0.pkl")) elif "densenet" in conf.arch: model_ft = DenseNet(conf, arch_name=conf.arch, input_size=conf.image_size) elif "efficientnet" in conf.arch: model_ft = EfficientNet(conf, arch_name=conf.arch) criterion = [ nn.CrossEntropyLoss(reduction="none"), nn.CrossEntropyLoss(reduction="none"), nn.CrossEntropyLoss(reduction="none") ] criterion = [c.to(device) for c in criterion] model_ft, val_preds = train_model(train_df, train_images, test_df, test_images, model_ft, criterion, log, device, result_dir, fold=i, num_epoch=conf.num_epoch) torch.save(model_ft.state_dict(), result_dir / f'model_{i}.pkl') np.save(result_dir / f'val_preds_{i}.npy', val_preds)
def get_mask_rcnn_model(layers,num_classes,out_channels=256,cfg=None): backbone = ResNet(layers,cfg) return_layers = {'layer1': '0', 'layer2': '1', 'layer3': '2', 'layer4': '3'} in_channels_list = [] cfg = backbone.cfg for i in range(1, len(cfg)): layer_size = len(cfg[i]) in_channels_list.append(cfg[i][layer_size - 1]) print(in_channels_list) backbone = BackboneWithFPN(backbone, return_layers, in_channels_list, out_channels) model = models.detection.MaskRCNN(backbone, num_classes) return model
def _reconstructor(self): """ Build an image reconstructor, that fuses an anatomy (binary mask) and Z to reconstructs the input image. :return: a Keras model of the reconstructor """ mask_input = Input(shape=self.conf.input_shape) round = Rounding()( mask_input ) # rounding layer that binarises the anatomical representation. resnet = ResNet(self.conf.input_shape, norm='instance', nb_blocks=3, name='Reconstructor') # Map Z into a 8-channel feature map resd_input = Input((16, )) modality = Dense(32)(resd_input) modality = LeakyReLU()(modality) modality = Dense(self.conf.input_shape[0] * self.conf.input_shape[1])(modality) modality = LeakyReLU()(modality) modality = Reshape((int(self.conf.input_shape[0] / 4), int(self.conf.input_shape[1] / 4), 16))(modality) modality = UpSampling2D(size=2)(modality) modality = Conv2D(16, 3, padding='same')(modality) modality = BatchNormalization()(modality) modality = LeakyReLU()(modality) modality = UpSampling2D(size=2)(modality) modality = Conv2D(8, 3, padding='same')(modality) modality = BatchNormalization()(modality) modality = LeakyReLU()(modality) # Concatenate Mask and Z conc_lr = Concatenate()([round, modality]) l = resnet.residuals(conc_lr, f=9) resnet.output([mask_input, resd_input], l) resnet.model.summary(print_fn=log.info) return resnet.model
def get_models(args, train=True, as_ensemble=False, model_file=None, leaky_relu=False): models = [] mean = torch.tensor([0.4914, 0.4822, 0.4465], dtype=torch.float32).cuda() std = torch.tensor([0.2023, 0.1994, 0.2010], dtype=torch.float32).cuda() normalizer = NormalizeByChannelMeanStd(mean=mean, std=std) if model_file: state_dict = torch.load(model_file) if train: print('Loading pre-trained models...') iter_m = state_dict.keys() if model_file else range(args.model_num) for i in iter_m: if args.arch.lower() == 'resnet': model = ResNet(depth=args.depth, leaky_relu=leaky_relu) else: raise ValueError('[{:s}] architecture is not supported yet...') # we include input normalization as a part of the model model = ModelWrapper(model, normalizer) if model_file: model.load_state_dict(state_dict[i]) if train: model.train() else: model.eval() model = model.cuda() models.append(model) if as_ensemble: assert not train, 'Must be in eval mode when getting models to form an ensemble' ensemble = Ensemble(models) ensemble.eval() return ensemble else: return models
def __init__(self, network, num_joints, num_layers, num_features, mode, model_2d_path, model_lift_path): super(ResNetLift, self).__init__() # 2d pose estimation module self.model_2d = ResNetInt(network, num_joints) if model_2d_path is not None: if os.path.isfile(model_2d_path): print('Load pretrained 2D pose estimation model..') state = torch.load(model_2d_path) pretrained_dict = state['model'] model_dict = self.model_2d.state_dict() new_pretrained_dict = { k[7:]: v for k, v in pretrained_dict.items() if k[7:] in model_dict } model_dict.update(new_pretrained_dict) self.model_2d.load_state_dict(model_dict) else: raise ValueError('model does not exist: %s' % model_2d_path) # 2d-to-3d pose lifting module self.model_lift = ResNet(mode, num_joints, num_layers, num_features) if model_lift_path is not None: if os.path.isfile(model_lift_path): print('Load pretrained 2D pose estimation model..') state = torch.load(model_lift_path) pretrained_dict = state['model'] model_dict = self.model_lift.state_dict() new_pretrained_dict = { k[7:]: v for k, v in pretrained_dict.items() if k[7:] in model_dict } model_dict.update(new_pretrained_dict) self.model_lift.load_state_dict(model_dict) else: raise ValueError('model does not exist: %s' % model_lift_path)
def __init__(self, in_channels, mid_channels, num_blocks, mask_type, reverse_mask): super(CouplingLayer, self).__init__() # Save mask info self.mask_type = mask_type self.reverse_mask = reverse_mask # Build scale and translate network if self.mask_type == MaskType.CHANNEL_WISE: in_channels //= 2 self.st_net = ResNet(in_channels, mid_channels, 2 * in_channels, num_blocks=num_blocks, kernel_size=3, padding=1, double_after_norm=(self.mask_type == MaskType.CHECKERBOARD)) # Learnable scale for s self.rescale = nn.utils.weight_norm(Rescale(in_channels))
def main(args): transform = getTransforms() data_path = os.path.join('data', args.data) if not os.path.exists(data_path): print('ERROR: No dataset named {}'.format(args.data)) exit(1) testset = BaseDataset(list_path=os.path.join(data_path, 'val.lst'), transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=1, shuffle=True, num_workers=1) class_list = getClassList(data_path) model = ResNet(num_layers=18, num_classes=len(class_list)).to(DEVICE) model.eval() output_dir = os.path.join('outputs', args.data) model_state_file = os.path.join(output_dir, 'checkpoint.pth.tar') model_file = args.model_file if len(model_file) == 0: model_file = model_state_file if os.path.exists(model_file): checkpoint = torch.load(model_file) if 'state_dict' in checkpoint.keys(): model.load_state_dict(checkpoint['state_dict'], strict=False) else: model.load_state_dict(checkpoint, strict=False) print('=> loaded {}'.format(model_file)) else: print('model_file "{}" does not exists.'.format(model_file)) exit(1) accuracy = test(model=model, dataloader=testloader, device=DEVICE, classes=class_list) print('Accuracy: {:.2f}%'.format(100 * accuracy))
def __init__(self, out_dim, v_hdim, cnn_fdim, dtype, device, frame_num=10, camera_num=3, frame_shape=(3, 224, 224), mlp_dim=(128, 64), v_net_type='lstm', v_net_param=None, bi_dir=False, training=True, is_dropout=False): super().__init__() self.out_dim = out_dim self.cnn_fdim = cnn_fdim self.v_hdim = v_hdim self.frame_shape = frame_shape self.camera_num = camera_num self.cnn = ResNet(cnn_fdim, running_stats=training) self.dtype = dtype self.device = device self.v_net_type = v_net_type self.mlp = MLP(v_hdim * 2, mlp_dim, 'leaky', is_dropout=is_dropout) self.linear = nn.Linear(self.mlp.out_dim, out_dim) self.softmax = nn.Softmax(dim=1)
def __init__( self, depth: int = 50, categories: int = 512, num_classes: int = 2, scale: int = 64, training: bool = True, input_shape=(112, 112, 3), ): super(SrfrFrOnly, self).__init__() self._face_recognition = ResNet(depth, categories, training, input_shape) self._arcloss_layer = ArcLossLayer( units=num_classes, scale=scale, dtype="float32", name="arcloss_layer", )
def main(): exp_name = f'baseline_{now()}' device, log, result_dir = setup(exp_name, conf) train_df = load_csv(conf.train_csv) if conf.npy: train_images = np.load(conf.train_images) else: train_images = pd.read_parquet(conf.train_images) train_df["gr"] = 0 train_df["cd"] = 0 train_df["vd"] = 0 train_df["image_mean"] = 0 models = [f"se_resnext50_f{i}.pkl" for i in range(5)] preds = np.zeros((len(train_df), conf.gr_size + conf.vd_size + conf.cd_size)) image_stats = np.zeros((len(train_df), 2)) log.info('done') for i in range(5): model = ResNet(conf, arch_name=conf.arch, input_size=conf.image_size) model.load_state_dict(torch.load(models[i])) model.to(device) ds = val_split(train_df, train_images, fold=i) _, val_ds, _, val_images = ds['train'], ds['val'], ds['train_images'], ds['val_images'] test_preds = predict(model, val_ds, val_images, valid_transform, device) print(test_preds.shape) te_ind = ds['te_ind'] preds[te_ind] += test_preds image_stats[te_ind, 0] = val_images.mean((1, 2)) image_stats[te_ind, 0] = val_images.std((1, 2)) preds = np.concatenate([preds, image_stats], axis=1) for t in ["grapheme_root", "vowel_diacritic", "consonant_diacritic"]: rf = RandomForestClassifier(n_jobs=16) # train = xgb.DMatrix(preds, label=train_df[t]) # params = {"max_depth": 4, "nthread": 16, "objective": "multi:softmax", # "eval_metric": ["merror", "mlogloss"], "num_class": conf.gr_size} # xgb.cv(params, train, num_boost_round=1000, nfold=5, seed=conf.seed, # early_stopping_rounds=40, verbose_eval=10) rf.fit(preds, train_df[t]) with open(f"{t}_rf2.pkl", "wb") as f: joblib.dump(rf, f)
def __init__(self, out_dim, v_hdim, cnn_fdim, no_cnn=False, frame_shape=(3, 224, 224), mlp_dim=(300, 200), cnn_type='resnet', v_net_type='lstm', v_net_param=None, causal=False): super().__init__() self.out_dim = out_dim self.cnn_fdim = cnn_fdim self.v_hdim = v_hdim self.no_cnn = no_cnn self.frame_shape = frame_shape if no_cnn: self.cnn = None elif cnn_type == 'resnet': self.cnn = ResNet(cnn_fdim) elif cnn_type == 'mobile': self.cnn = MobileNet(cnn_fdim) self.v_net_type = v_net_type if v_net_type == 'lstm': self.v_net = RNN(cnn_fdim, v_hdim, v_net_type, bi_dir=not causal) elif v_net_type == 'tcn': if v_net_param is None: v_net_param = {} tcn_size = v_net_param.get('size', [64, 128]) dropout = v_net_param.get('dropout', 0.2) kernel_size = v_net_param.get('kernel_size', 3) assert tcn_size[-1] == v_hdim self.v_net = TemporalConvNet(cnn_fdim, tcn_size, kernel_size=kernel_size, dropout=dropout, causal=causal) self.mlp = MLP(v_hdim, mlp_dim, 'relu') self.linear = nn.Linear(self.mlp.out_dim, out_dim)