class PrototypicalCalibrationBlock: def __init__(self, cfg): super().__init__() self.cfg = cfg self.device = torch.device(cfg.MODEL.DEVICE) self.alpha = self.cfg.TEST.PCB_ALPHA self.imagenet_model = self.build_model() self.dataloader = build_detection_test_loader(self.cfg, self.cfg.DATASETS.TRAIN[0]) self.roi_pooler = ROIPooler(output_size=(1, 1), scales=(1 / 32,), sampling_ratio=(0), pooler_type="ROIAlignV2") self.prototypes = self.build_prototypes() self.exclude_cls = self.clsid_filter() def build_model(self): logger.info("Loading ImageNet Pre-train Model from {}".format(self.cfg.TEST.PCB_MODELPATH)) if self.cfg.TEST.PCB_MODELTYPE == 'resnet': imagenet_model = resnet101() else: raise NotImplementedError state_dict = torch.load(self.cfg.TEST.PCB_MODELPATH) imagenet_model.load_state_dict(state_dict) imagenet_model = imagenet_model.to(self.device) imagenet_model.eval() return imagenet_model def build_prototypes(self): all_features, all_labels = [], [] for index in range(len(self.dataloader.dataset)): inputs = [self.dataloader.dataset[index]] assert len(inputs) == 1 # load support images and gt-boxes img = cv2.imread(inputs[0]['file_name']) # BGR img_h, img_w = img.shape[0], img.shape[1] ratio = img_h / inputs[0]['instances'].image_size[0] inputs[0]['instances'].gt_boxes.tensor = inputs[0]['instances'].gt_boxes.tensor * ratio boxes = [x["instances"].gt_boxes.to(self.device) for x in inputs] # extract roi features features = self.extract_roi_features(img, boxes) all_features.append(features.cpu().data) gt_classes = [x['instances'].gt_classes for x in inputs] all_labels.append(gt_classes[0].cpu().data)
时间: 2024-04-07 10:29:45 浏览: 143
这段代码是一个名为PrototypicalCalibrationBlock的类的定义,它包含了一些方法和属性。__init__方法接受一个cfg参数,用来初始化一些属性。其中包括设备类型、alpha值、预训练模型、数据加载器、RoI池化器和类别原型等。build_model方法用于加载ImageNet预训练模型,支持resnet101模型。build_prototypes方法用于提取RoI特征和类别标签,并将其存储为特征向量和类别原型。这个类的作用是在目标检测任务上进行模型校准。
相关问题
class DoubleFastRCNNOutputLayers(nn.Module): def __init__( self, cfg, input_size, num_classes, cls_agnostic_bbox_reg, box_dim=4 ): super(DoubleFastRCNNOutputLayers, self).__init__() if not isinstance(input_size, int): input_size = np.prod(input_size) self.cls_score = nn.Linear(input_size, num_classes + 1) num_bbox_reg_classes = 1 if cls_agnostic_bbox_reg else num_classes self.bbox_pred = nn.Linear(input_size, num_bbox_reg_classes * box_dim) nn.init.normal_(self.cls_score.weight, std=0.01) nn.init.normal_(self.bbox_pred.weight, std=0.001) for l in [self.cls_score, self.bbox_pred]: nn.init.constant_(l.bias, 0) self._do_cls_dropout = cfg.MODEL.ROI_HEADS.CLS_DROPOUT self._dropout_ratio = cfg.MODEL.ROI_HEADS.DROPOUT_RATIO def forward(self, x_s, x_l): if x_s.dim() > 2: x_s = torch.flatten(x_s, start_dim=1) if x_l.dim() > 2: x_l = torch.flatten(x_l, start_dim=1) proposal_deltas = self.bbox_pred(x_l) if self._do_cls_dropout: x_s = F.dropout(x_s, self._dropout_ratio, training=self.training) scores = self.cls_score(x_s) return scores, proposal_deltas
这段代码是一个双输入的Fast R-CNN输出层的实现,其中包括一个分类得分层和一个边界框回归层。它接受两个输入x_s和x_l,分别代表短边和长边的特征。在前向传播时,它首先对输入进行扁平化处理,然后通过bbox_pred层获得边界框预测值,通过cls_score层获得分类得分。在进行分类得分的计算时,可以进行dropout操作来防止过拟合。最终,返回分类得分和边界框预测值。
class ImageNetDataset(Dataset): def __init__(self, cfg, mode='train'): super(ImageNetDataset, self).__init__() self.mode = mode self.train_file = open(cfg.train_file, 'r').readlines() self.val_file = open(cfg.val_file, 'r').readlines() self.train_file = [(Path(s.strip())) for s in self.train_file] self.val_file = [(Path(s.strip())) for s in self.val_file] if mode == 'train': self.map_file = self.train_file elif mode == 'val': self.map_file = self.val_file elif mode == 'test': self.map_file = self.val_file self.labels = [s.split()[1].strip() for s in open(cfg.label_map, 'r').readlines()] self.labels = sorted(self.labels[:1000]) if getattr(cfg, 'trim_class_count', None) is not None: self.labels = self.labels[:cfg.trim_class_count] self.map_file = list(filter(lambda s: s.parent.stem in self.labels, self.map_file)) self.label_map = {s: idx for idx, s in enumerate(self.labels)} self.cfg = cfg self.augment_type = getattr(cfg, 'augment_type', None) self.loader_type = getattr(cfg, 'loader_type', None) self.parser_type = getattr(cfg, 'parser_type', 'normal') assert self.parser_type in ['normal']这个类是什么意思
这个类是一个 PyTorch 数据集类,用于加载 ImageNet 数据集。在初始化时,它会读取训练集和验证集的文件列表,以及标签映射文件。它还可以根据模式(训练、验证、测试)选择相应的数据集。如果指定了要截断的类别数,则会根据类别名称筛选文件列表。标签映射将标签名称映射为数字标签。此外,该类还可以指定数据增强类型、加载器类型和解析器类型。在数据集的获取过程中,它会根据指定的解析器类型对数据进行解析,并返回图像和标签。
阅读全文