import math import torch import torch.nn as nn import sys import os from training.file_utils import pt_load sys.path.append("..") from clipself.src.open_clip.factory import create_model, get_tokenizer from prompts.imagenet_template import openai_imagenet_template from mmseg.models.segmentors import BaseSegmentor from mmengine.structures import PixelData from mmseg.registry import MODELS import torch.nn.functional as F from mmseg.models.data_preprocessor import SegDataPreProcessor @MODELS.register_module() class DeCLIPSegmentation(BaseSegmentor): def __init__(self, clip_type, name_path, checkpoint, mode, pretrained, vfm=None, device=torch.device('cuda:0'), prob_thd=0.0, logit_scale=40, slide_stride=112, slide_crop=336): data_preprocessor = SegDataPreProcessor( mean=[122.771, 116.746, 104.094], std=[68.501, 66.632, 70.323], bgr_to_rgb=True) super().__init__(data_preprocessor=data_preprocessor) if pretrained == "eva": self.clip = create_model( clip_type, pretrained, device=device, precision="amp", output_dict=True, cache_dir=checkpoint) self.tokenizer = get_tokenizer(model_name=clip_type) else: from open_clip import tokenizer self.clip = create_model( clip_type, pretrained, device=device, precision="amp", output_dict=True, cache_dir=None) self.tokenizer = tokenizer.tokenize if checkpoint: sd = pt_load(checkpoint, map_location='cpu')["state_dict"] self.clip.load_state_dict(sd) self.clip.eval().to(device) query_words, self.query_idx = get_cls_idx(name_path) self.num_queries = len(query_words) self.num_classes = max(self.query_idx) + 1 self.query_idx = torch.Tensor(self.query_idx).to(torch.int64).to(device) self.mode = mode # Pre-compute query features query_features = [] with torch.no_grad(): for qw in query_words: query = self.tokenizer([temp(qw) for temp in openai_imagenet_template]).to(device) feature = self.clip.encode_text(query) feature /= feature.norm(dim=-1, keepdim=True) feature = feature.mean(dim=0) feature /= feature.norm() query_features.append(feature.unsqueeze(0)) self.query_features = torch.cat(query_features, dim=0).detach() self.logit_scale = logit_scale self.prob_thd = prob_thd self.slide_stride = slide_stride self.slide_crop = slide_crop self.vfm = vfm @torch.no_grad() def forward_feature(self, img, logit_size=None): if type(img) == list: img = img[0] image_features = self.clip.encode_dense( img, normalize=True, keep_shape=False, mode=self.mode, ) # bs, N, C N = image_features.shape[1] h, w = int(math.sqrt(N)), int(math.sqrt(N)) logits = image_features @ self.query_features.T logits = logits.permute(0, 2, 1).reshape(-1, logits.shape[-1], h, w) if logit_size is None: logits = nn.functional.interpolate(logits, size=img.shape[-2:], mode='bilinear') else: logits = nn.functional.interpolate(logits, size=logit_size, mode='bilinear') return logits def predict(self, inputs, data_samples): if data_samples is not None: batch_img_metas = [data_sample.metainfo for data_sample in data_samples] else: batch_img_metas = [ dict( ori_shape=inputs.shape[2:], img_shape=inputs.shape[2:], pad_shape=inputs.shape[2:], padding_size=[0, 0, 0, 0]) ] * inputs.shape[0] ori_shape = batch_img_metas[0]['ori_shape'] resize_shape = batch_img_metas[0]['resize_shape'] img_shape = batch_img_metas[0]['img_shape'] if self.slide_crop > 0: seg_logits = self.forward_slide(inputs, batch_img_metas, self.slide_stride, self.slide_crop) else: seg_logits = self.forward_feature(inputs, img_shape) seg_logits = seg_logits[:, :, :resize_shape[0], :resize_shape[1]] seg_logits = nn.functional.interpolate(seg_logits, size=ori_shape, mode='bilinear') result = self.postprocess_result(seg_logits, data_samples) return result def forward_slide(self, img, img_metas, stride=112, crop_size=224): """Inference by sliding-window with overlap.""" if type(img) == list: img = img[0].unsqueeze(0) if type(stride) == int: stride = (stride, stride) if type(crop_size) == int: crop_size = (crop_size, crop_size) h_stride, w_stride = stride h_crop, w_crop = crop_size batch_size, _, h_img, w_img = img.shape out_channels = self.num_queries h_grids = max(h_img - h_crop + h_stride - 1, 0) // h_stride + 1 w_grids = max(w_img - w_crop + w_stride - 1, 0) // w_stride + 1 preds = img.new_zeros((batch_size, out_channels, h_img, w_img)) count_mat = img.new_zeros((batch_size, 1, h_img, w_img)) for h_idx in range(h_grids): for w_idx in range(w_grids): y1 = h_idx * h_stride x1 = w_idx * w_stride y2 = min(y1 + h_crop, h_img) x2 = min(x1 + w_crop, w_img) y1 = max(y2 - h_crop, 0) x1 = max(x2 - w_crop, 0) crop_img = img[:, :, y1:y2, x1:x2] # Pad image when (image_size % patch_size != 0) H, W = crop_img.shape[2:] pad = self.compute_padsize(H, W, 16) if any(pad): crop_img = nn.functional.pad(crop_img, pad) crop_seg_logit = self.forward_feature(crop_img).detach() torch.cuda.empty_cache() # Mask cutting for padded image if any(pad): l, t = pad[0], pad[2] crop_seg_logit = crop_seg_logit[:, :, t:t + H, l:l + W] preds += nn.functional.pad( crop_seg_logit, (int(x1), int(preds.shape[3] - x2), int(y1), int(preds.shape[2] - y2)) ) count_mat[:, :, y1:y2, x1:x2] += 1 assert (count_mat == 0).sum() == 0 preds = preds / count_mat img_size = img_metas[0]['ori_shape'][:2] logits = nn.functional.interpolate(preds, size=img_size, mode='bilinear') return logits def compute_padsize(self, H: int, W: int, patch_size: int): """Compute padding size to make H and W divisible by patch_size.""" l, r, t, b = 0, 0, 0, 0 if W % patch_size: lr = patch_size - (W % patch_size) l = lr // 2 r = lr - l if H % patch_size: tb = patch_size - (H % patch_size) t = tb // 2 b = tb - t return l, r, t, b def postprocess_result(self, seg_logits, data_samples): batch_size = seg_logits.shape[0] for i in range(batch_size): seg_logits_i = seg_logits[i] * self.logit_scale seg_logits_i = seg_logits_i.softmax(0) # n_queries * w * h num_cls, num_queries = max(self.query_idx) + 1, len(self.query_idx) if num_cls != num_queries: seg_logits_i = seg_logits_i.unsqueeze(0) cls_index = nn.functional.one_hot(self.query_idx) cls_index = cls_index.T.view(num_cls, num_queries, 1, 1) seg_logits_i = (seg_logits_i * cls_index).max(1)[0] seg_pred = seg_logits_i.argmax(0, keepdim=True) seg_pred[seg_logits_i.max(0, keepdim=True)[0] < self.prob_thd] = 0 if data_samples is None: return seg_pred else: data_samples[i].set_data({ 'seg_logits': PixelData(**{'data': seg_logits_i}), 'pred_sem_seg': PixelData(**{'data': seg_pred}) }) return data_samples def _forward(data_samples): """Placeholder for required abstract method.""" pass def encode_decode(self, inputs, batch_img_metas): """Placeholder for required abstract method.""" pass def extract_feat(self, inputs): """Placeholder for required abstract method.""" pass def loss(self, inputs, data_samples): """Placeholder for required abstract method.""" pass def inference(self, img, batch_img_metas): """ """ def get_cls_idx(path): """Load class names and indices from file.""" with open(path, 'r') as f: name_sets = f.readlines() num_cls = len(name_sets) class_names, class_indices = [], [] for idx in range(num_cls): names_i = name_sets[idx].split('; ') class_names += names_i class_indices += [idx for _ in range(len(names_i))] class_names = [item.replace('\n', '') for item in class_names] return class_names, class_indices