Ejemplo n.º 1
0
def load_model():
    #return torch.load("D:\\deployment\\hotdog-not-hotdog-master\\uploads/finetunned_model")
    '''model = models.vgg16(pretrained=True)
   prepare_for_finetunning(model)
   model.load_state_dict(torch.load("D:\\deployment\\hotdog-not-hotdog-master\\finetunned_model_state.pth"))'''
    resume = './net_model/weights_31_501_0.9806_0.9973.pth'
    model = MainModel()
    model_dict = model.state_dict()
    pretrained_dict = torch.load(resume)
    pretrained_dict = {
        k[7:]: v
        for k, v in pretrained_dict.items() if k[7:] in model_dict
    }
    model_dict.update(pretrained_dict)
    model.load_state_dict(model_dict)
    return model
Ejemplo n.º 2
0
    dataloader['val'] = torch.utils.data.DataLoader(val_set,\
                                                batch_size=args.val_batch,\
                                                shuffle=False,\
                                                num_workers=args.val_num_workers,\
                                                collate_fn=collate_fn4test if not Config.use_backbone else collate_fn4backbone,
                                                drop_last=True if Config.use_backbone else False,
                                                pin_memory=True)

    setattr(dataloader['val'], 'total_item_len', len(val_set))
    setattr(dataloader['val'], 'num_cls', Config.numcls)

    cudnn.benchmark = True

    print('Choose model and train set', flush=True)
    model = MainModel(Config)

    # load model
    if (args.resume is None) and (not args.auto_resume):
        print('train from imagenet pretrained models ...', flush=True)
    else:
        if not args.resume is None:
            resume = args.resume
            print('load from pretrained checkpoint %s ...'% resume, flush=True)
        elif args.auto_resume:
            resume = auto_load_resume(Config.save_dir)
            print('load from %s ...'%resume, flush=True)
        else:
            raise Exception("no checkpoints to load")

        model_dict = model.state_dict()
Ejemplo n.º 3
0
                        type=int)
    parser.add_argument('--tnw', dest='train_num_workers', default=0, type=int)
    parser.add_argument('--vnw', dest='val_num_workers', default=32, type=int)
    parser.add_argument('--detail', dest='discribe', default='', type=str)
    parser.add_argument('--size',
                        dest='resize_resolution',
                        default=512,
                        type=int)
    parser.add_argument('--crop',
                        dest='crop_resolution',
                        default=448,
                        type=int)
    parser.add_argument('--cls_2', dest='cls_2', action='store_true')
    parser.add_argument('--cls_mul', dest='cls_mul', action='store_true')
    parser.add_argument('--swap_num',
                        default=[7, 7],
                        nargs=2,
                        metavar=('swap1', 'swap2'),
                        type=int,
                        help='specify a range')
    args = parser.parse_args()
    return args


if __name__ == '__main__':
    args = parse_args()
    print(args, flush=True)
    Config = LoadConfig(args, 'train')
    model = MainModel(Config).cuda()
    summary(model, (3, 224, 224))
Ejemplo n.º 4
0
    def startup(self, args):
        i_debug = 18
        if 1 == i_debug:
            # 为无锡所招标预留功能开发
            app = WxsApp()
            app.startup(args)
            return
        print('模型热力图绘制应用 v0.1.0')
        os.environ['CUDA_VISIBLE_DEVICES'] = '2'
        args = self.parse_args()
        # arg_dict = vars(args)
        args.train_num_workers = 0
        args.val_num_workers = 0
        print(args, flush=True)
        Config = LoadConfig(args, 'train')
        Config.cls_2 = args.cls_2
        Config.cls_2xmul = args.cls_mul
        assert Config.cls_2 ^ Config.cls_2xmul
        transformers = load_data_transformers(args.resize_resolution,
                                              args.crop_resolution,
                                              args.swap_num)
        # inital dataloader
        train_set = dataset(Config = Config,\
                            anno = Config.train_anno,\
                            common_aug = transformers["common_aug"],\
                            swap = transformers["swap"],\
                            swap_size=args.swap_num, \
                            totensor = transformers["train_totensor"],\
                            train = True)
        trainval_set = dataset(Config = Config,\
                            anno = Config.val_anno,\
                            common_aug = transformers["None"],\
                            swap = transformers["None"],\
                            swap_size=args.swap_num, \
                            totensor = transformers["val_totensor"],\
                            train = False,
                            train_val = True)
        val_set = dataset(Config = Config,\
                          anno = Config.val_anno,\
                          common_aug = transformers["None"],\
                          swap = transformers["None"],\
                            swap_size=args.swap_num, \
                          totensor = transformers["test_totensor"],\
                          test=True)
        dataloader = {}
        dataloader['train'] = torch.utils.data.DataLoader(train_set,\
                                                    batch_size=args.train_batch,\
                                                    shuffle=True,\
                                                    num_workers=args.train_num_workers,\
                                                    collate_fn=collate_fn4train if not Config.use_backbone else collate_fn4backbone,
                                                    drop_last=True if Config.use_backbone else False,
                                                    pin_memory=True)
        setattr(dataloader['train'], 'total_item_len', len(train_set))
        dataloader['trainval'] = torch.utils.data.DataLoader(trainval_set,\
                                                    batch_size=args.val_batch,\
                                                    shuffle=False,\
                                                    num_workers=args.val_num_workers,\
                                                    collate_fn=collate_fn4val if not Config.use_backbone else collate_fn4backbone,
                                                    drop_last=True if Config.use_backbone else False,
                                                    pin_memory=True)
        setattr(dataloader['trainval'], 'total_item_len', len(trainval_set))
        setattr(dataloader['trainval'], 'num_cls', Config.num_brands)
        dataloader['val'] = torch.utils.data.DataLoader(val_set,\
                                                    batch_size=args.val_batch,\
                                                    shuffle=False,\
                                                    num_workers=args.val_num_workers,\
                                                    collate_fn=collate_fn4test if not Config.use_backbone else collate_fn4backbone,
                                                    drop_last=True if Config.use_backbone else False,
                                                    pin_memory=True)
        setattr(dataloader['val'], 'total_item_len', len(val_set))
        setattr(dataloader['val'], 'num_cls', Config.num_brands)
        cudnn.benchmark = True
        print('Choose model and train set', flush=True)
        print('Choose model and train set', flush=True)
        model = MainModel(Config)

        # load model
        if (args.resume is None) and (not args.auto_resume):
            print('train from imagenet pretrained models ...', flush=True)
        else:
            if not args.resume is None:
                resume = args.resume
                print('load from pretrained checkpoint %s ...' % resume,
                      flush=True)
            elif args.auto_resume:
                resume = self.auto_load_resume(Config.save_dir)
                print('load from %s ...' % resume, flush=True)
            else:
                raise Exception("no checkpoints to load")

            model_dict = model.state_dict()
            pretrained_dict = torch.load(resume)
            print('train.py Ln193 resume={0};'.format(resume))
            pretrained_dict = {
                k[7:]: v
                for k, v in pretrained_dict.items() if k[7:] in model_dict
            }
            model_dict.update(pretrained_dict)
            model.load_state_dict(model_dict)
        print('Set cache dir', flush=True)
        time = datetime.datetime.now()
        filename = '%s_%d%d%d_%s' % (args.cam, time.month, time.day, time.hour,
                                     Config.dataset)
        save_dir = os.path.join(Config.save_dir, filename)
        print('save_dir: {0} + {1};'.format(Config.save_dir, filename))
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        model.cuda()
        cam_main_model = model
        cam_model = model.model
        model = nn.DataParallel(model)
        # optimizer prepare
        if Config.use_backbone:
            ignored_params = list(map(id, model.module.classifier.parameters())) \
                        + list(map(id, model.module.brand_clfr.parameters()))
        else:
            ignored_params1 = list(
                map(id, model.module.classifier.parameters()))
            ignored_params1x = list(
                map(id, model.module.brand_clfr.parameters()))
            ignored_params2 = list(
                map(id, model.module.classifier_swap.parameters()))
            ignored_params3 = list(map(id, model.module.Convmask.parameters()))
            ignored_params = ignored_params1 + ignored_params1x + ignored_params2 + ignored_params3
        print('the num of new layers:', len(ignored_params), flush=True)
        base_params = filter(lambda p: id(p) not in ignored_params,
                             model.module.parameters())
        lr_ratio = args.cls_lr_ratio
        base_lr = args.base_lr
        momentum = 0.9
        if Config.use_backbone:
            optimizer = optim.SGD(
                [{
                    'params': base_params
                }, {
                    'params': model.module.classifier.parameters(),
                    'lr': base_lr
                }, {
                    'params': model.module.brand_clfr.parameters(),
                    'lr': base_lr
                }],
                lr=base_lr,
                momentum=momentum)
        else:
            optimizer = optim.SGD([
                {
                    'params': base_params
                },
                {
                    'params': model.module.classifier.parameters(),
                    'lr': lr_ratio * base_lr
                },
                {
                    'params': model.module.brand_clfr.parameters(),
                    'lr': lr_ratio * base_lr
                },
                {
                    'params': model.module.classifier_swap.parameters(),
                    'lr': lr_ratio * base_lr
                },
                {
                    'params': model.module.Convmask.parameters(),
                    'lr': lr_ratio * base_lr
                },
            ],
                                  lr=base_lr,
                                  momentum=momentum)

        exp_lr_scheduler = lr_scheduler.StepLR(optimizer,
                                               step_size=args.decay_step,
                                               gamma=0.1)
        # *******************
        # *******************
        print('model: {0};'.format(cam_model))
        print('avgpoo: {0};'.format(cam_main_model.avgpool))
        headers = {
            'avgpool': cam_main_model.avgpool,
            'classifier': cam_main_model.brand_clfr
        }
        grad_cam = GradCam(model=cam_model, feature_module=cam_model[7], \
                       target_layer_names=["2"], headers=headers, use_cuda=True)
        # 读入图片数据
        img = None
        img_file = '/media/ps/0A9AD66165F33762/yantao/dcl/support/ds_files/wxs_ds/head/car/d00/d00/d00/d00/d96/SC7168CH5_冀B591C5_02_120000100604_120000702916290242.jpg'
        with open(img_file, 'rb') as f:
            with Image.open(f) as img:
                img = img.convert('RGB')

        crop_reso = 224
        to_tensor = transforms.Compose([
            transforms.Resize((crop_reso, crop_reso)),
            # ImageNetPolicy(),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
        ])
        img_obj = to_tensor(img)
        input = img_obj.reshape(1, 3, 224, 224)
        input.cuda()
        input.requires_grad_(True)
        print('input: {0};'.format(input.shape))
        # If None, returns the map for the highest scoring category.
        # Otherwise, targets the requested index.
        target_index = None
        mask = grad_cam(input, target_index)
        #
        self.show_cam_on_image(img_file, mask)
        #
        gb_model = GuidedBackpropReLUModel(model=cam_main_model, use_cuda=True)
        gb = gb_model(input, index=target_index)
        gb = gb.transpose((1, 2, 0))
        cam_mask = cv2.merge([mask, mask, mask])
        cam_gb = self.deprocess_image(cam_mask * gb)
        gb = self.deprocess_image(gb)
        cv2.imwrite('gb.jpg', gb)
        cv2.imwrite('cam_gb.jpg', cam_gb)

        print('^_^ The End! 002 ^_^')