text: import os
  import sys
  from config import Config
  import torch
  from torch.backends import cudnn
  import torchvision.transforms as T
  from PIL import Image
  sys.path.append('.')
  from model import make_model
  from torch.utils.data import DataLoader, Dataset
  import numpy as np
  import cv2
  from utils.metrics import cosine_similarity
  class gallaryDataSet(Dataset):
      def __init__(self, gallary_dir, transform):
          self.gallary_dir = gallary_dir
          self.transform = transform
          self.all_imgs = os.listdir(gallary_dir)
      def __len__(self):
          return len(self.all_imgs)
      def __getitem__(self, idx):
          img_loc = os.path.join(self.gallary_dir, self.all_imgs[idx])
          image = Image.open(img_loc).convert("RGB")
          tensor_image = self.transform(image)
          return tensor_image, img_loc
  def get_save_gallary_feats(model, gallary_loader):
      img_path_list = []
      gfeats=[]
      model.eval()
      for i,(img, imgpath)in enumerate(gallary_loader):
          with torch.no_grad():
              img = img.to('cuda')
              feat = model(img)
              gfeats.append(feat)
              img_path_list.extend(imgpath)
      gfeats = torch.cat(gfeats, dim=0)
      torch.save(gfeats, "gfeats.pth")
      np.save("img_path.npy", img_path_list)
  def visualizer(test_img, camid, top_k = 10, img_size=[128,128]):
      figure = np.asarray(query_img.resize((img_size[1],img_size[0])))
      for k in range(top_k):
          name = str(indices[0][k]).zfill(6)
          img = np.asarray(Image.open(img_path[indices[0][k]]).resize((img_size[1],img_size[0])))
          figure = np.hstack((figure, img))
          title=name
      figure = cv2.cvtColor(figure,cv2.COLOR_BGR2RGB)
      if not os.path.exists("./results"):
          os.mkdir("./results")
      cv2.imwrite("./results/{}-cam{}.png".format(test_img,camid),figure)
  if __name__ == "__main__":
      Cfg = Config()
      model = make_model(Cfg, 255)
      model.load_param(Cfg.TEST_WEIGHT)
  
      input_size=[256, 128]
      gallary_dir = "./gallary/"
      g_feat_path='./gfeats.pth'
      g_imgpath = './img_path.npy'
      query_dir = "./query/"
      os.environ['CUDA_VISIBLE_DEVICES'] = "0"
      cudnn.benchmark = True
      device = 'cuda'
      model = model.to(device)
      transform = T.Compose([
          T.Resize(input_size),
          T.ToTensor(),
          T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
      ])
      gallery_loader = DataLoader(gallaryDataSet(gallary_dir, transform), batch_size=64)
      # get_save_gallary_feats(model, gallery_loader)
  
      model.eval()
      gallery_feats = torch.load(g_feat_path)
      # gallery_feats = torch.stack(, dim=0)
      for test_img in os.listdir(query_dir):
          print('Finding ID {} ...'.format(test_img))
          img_path = np.load(g_imgpath)
          print(gallery_feats.shape, len(img_path))
          query_img = Image.open(query_dir + test_img)
          input = torch.unsqueeze(transform(query_img), 0)
          input = input.to(device)
          with torch.no_grad():
              query_feat = model(input)
          dist_mat = cosine_similarity(query_feat, gallery_feats)
          indices = np.argsort(dist_mat, axis=1)
          visualizer(test_img, camid='mixed', top_k=10, img_size=input_size)
      