import math import os import torch import torch.nn as nn import sys from training.file_utils import pt_load sys.path.append("..") from clipself.src.open_clip.tiny_clip.factory import create_model, get_tokenizer from prompts.imagenet_template import openai_imagenet_template, sub_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 from segment_anything import sam_model_registry from myutils import UnNormalize, visualize_ade20k, visualize_cityscapes, visualize_coco_stuff, visualize_voc_context59 from torchvision import transforms @MODELS.register_module() class TinyCLIPSegmentation(BaseSegmentor): def __init__(self, clip_type, name_path, vfm, checkpoint, mode, 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) # 使用tiny_clip的factory创建模型 # tiny_clip的create_model可以直接接受checkpoint路径作为pretrained参数 # 注意:tiny_clip的factory支持precision="fp32"或"fp16",不支持"amp" if checkpoint and os.path.exists(checkpoint): # 如果checkpoint是文件路径,直接使用 self.clip = create_model( clip_type, pretrained=checkpoint, precision="fp32", # 使用fp32,与CLIPselfSegmentation保持一致 device=device, cache_dir=None) else: # 如果没有checkpoint,创建空模型 self.clip = create_model( clip_type, pretrained="", precision="fp32", device=device, cache_dir=None) self.tokenizer = get_tokenizer(model_name=clip_type) 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 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.dtype = self.query_features.dtype self.logit_scale = logit_scale self.prob_thd = prob_thd self.slide_stride = slide_stride self.slide_crop = slide_crop # begin vfm # self.vfm=vfm if vfm: if vfm=="sam": self.vfm_model = sam_model_registry["vit_b"](checkpoint="sam_ckpts/sam_vit_b_01ec64.pth") elif vfm=="dino": self.vfm_model = torch.hub.load('facebookresearch/dino:main', 'dino_vitb8') else: self.vfm_model = torch.hub.load('facebookresearch/dinov2:main', 'dinov2_vitb14_reg') self.vfm_model = self.vfm_model.half() for p in self.vfm_model.parameters(): p.requires_grad = False self.vfm_model.eval().to(device) self.unnorm = UnNormalize([0.48145466, 0.4578275, 0.40821073], [0.26862954, 0.26130258, 0.27577711]) self.norm = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) else: self.vfm_model=None # end vfm # @torch.no_grad() def forward_feature(self, img, logit_size=None,): if type(img) == list: img = img[0] if self.vfm: imgs_norm = [self.norm(self.unnorm(img[i])) for i in range(len(img))] imgs_norm = torch.stack(imgs_norm, dim=0) imgs_norm = imgs_norm.half() if self.vfm=="sam": imgs_norm = F.interpolate(imgs_norm, size=(1024, 1024), mode='bilinear', align_corners=False) ex_feats = self.vfm_model.image_encoder(imgs_norm) elif self.vfm == 'dinov2': patch_size = self.vfm_model.patch_embed.patch_size I, J = imgs_norm.shape[-2] // patch_size[0], imgs_norm.shape[-2] // patch_size[1] imgs_norm = F.interpolate(imgs_norm, size=(896, 896), mode='bilinear', align_corners=False) ex_feats = self.vfm_model.get_intermediate_layers(imgs_norm, reshape=True)[0] else: imgs_norm = F.interpolate(imgs_norm, size=(512, 512), mode='bilinear', align_corners=False) feat = self.vfm_model.get_intermediate_layers(imgs_norm)[0] nb_im = feat.shape[0] patch_size = self.vfm_model.patch_embed.patch_size I, J = imgs_norm[0].shape[-2] // patch_size, imgs_norm[0].shape[-2] // patch_size ex_feats = feat[:, 1:, :].reshape(nb_im, I, J, -1).permute(0, 3, 1, 2) image_features = self.clip.encode_dense(img, normalize=True, keep_shape=False, mode=self.mode, ) # bs, N, C else: image_features = self.clip.encode_dense(img, normalize=True, keep_shape=False, mode=self.mode, ) # bs, N, C # Calculate h, w from image size and patch_size instead of sqrt(N) # This handles cases where N is not a perfect square due to padding clip_token_size = ( img.shape[-2] // self.clip.visual.patch_size[0], img.shape[-1] // self.clip.visual.patch_size[1], ) h, w = clip_token_size logits = image_features @ self.query_features.T logits = logits.permute(0, 2, 1).reshape(-1, logits.shape[-1], h, w) if logit_size == 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) # visualize_voc_context59(batch_img_metas,result) # visualize_ade20k(batch_img_metas,result) # visualize_coco_stuff(batch_img_metas,result) # visualize_cityscapes(batch_img_metas,result) return result def forward_slide(self, img, img_metas, stride=112, crop_size=224): """Inference by sliding-window with overlap. If h_crop > h_img or w_crop > w_img, the small patch will be used to decode without padding. """ 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:] # original image shape # Use CLIP patch_size for padding calculation clip_patch_size = self.clip.visual.patch_size[0] if hasattr(self.clip.visual, 'patch_size') else 16 pad = self.compute_padsize(H, W, clip_patch_size) if any(pad): crop_img = nn.functional.pad(crop_img, pad) # zero padding 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): 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 = seg_logits[i] * self.logit_scale seg_logits = seg_logits.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 = seg_logits.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 = (seg_logits * cls_index).max(1)[0] seg_pred = seg_logits.argmax(0, keepdim=True) seg_pred[seg_logits.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}), 'pred_sem_seg': PixelData(**{'data': seg_pred}) }) return data_samples def _forward(data_samples): """ """ def inference(self, img, batch_img_metas): """ """ def encode_decode(self, inputs, batch_img_metas): """ """ def extract_feat(self, inputs): """ """ def loss(self, inputs, data_samples): """ """ def get_cls_idx(path): 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