build.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import logging
  2. import torch
  3. import torchvision.transforms as T
  4. from torch.utils.data import DataLoader
  5. # from datasets.sampler import RandomIdentitySampler
  6. # from datasets.sampler_ddp import RandomIdentitySampler_DDP
  7. from utils.comm import get_world_size
  8. from .cuhkpedes import CUHKPEDES
  9. from .bases import ImageDataset, TextDataset, ImageTextDataset, ImageTextMLMDataset
  10. # __factory = {'CUHK-PEDES': CUHKPEDES, 'ICFG-PEDES': ICFGPEDES, 'RSTPReid': RSTPReid}
  11. __factory = {'CUHK-PEDES': CUHKPEDES}
  12. def build_transforms(img_size=(384, 128), aug=False, is_train=True):
  13. height, width = img_size
  14. mean = [0.48145466, 0.4578275, 0.40821073]
  15. std = [0.26862954, 0.26130258, 0.27577711]
  16. if not is_train:
  17. transform = T.Compose([
  18. T.Resize((height, width)),
  19. T.ToTensor(),
  20. T.Normalize(mean=mean, std=std),
  21. ])
  22. return transform
  23. # transform for training
  24. if aug:
  25. transform = T.Compose([
  26. T.Resize((height, width)),
  27. T.RandomHorizontalFlip(0.5),
  28. T.Pad(10),
  29. T.RandomCrop((height, width)),
  30. T.ToTensor(),
  31. T.Normalize(mean=mean, std=std),
  32. T.RandomErasing(scale=(0.02, 0.4), value=mean),
  33. ])
  34. else:
  35. transform = T.Compose([
  36. T.Resize((height, width)),
  37. T.RandomHorizontalFlip(0.5),
  38. T.ToTensor(),
  39. T.Normalize(mean=mean, std=std),
  40. ])
  41. return transform
  42. def collate(batch):
  43. keys = set([key for b in batch for key in b.keys()])
  44. # turn list of dicts data structure to dict of lists data structure
  45. dict_batch = {k: [dic[k] if k in dic else None for dic in batch] for k in keys}
  46. batch_tensor_dict = {}
  47. for k, v in dict_batch.items():
  48. if isinstance(v[0], int):
  49. batch_tensor_dict.update({k: torch.tensor(v)})
  50. elif torch.is_tensor(v[0]):
  51. batch_tensor_dict.update({k: torch.stack(v)})
  52. else:
  53. raise TypeError(f"Unexpect data type: {type(v[0])} in a batch.")
  54. return batch_tensor_dict
  55. def build_dataloader(args, tranforms=None):
  56. logger = logging.getLogger("IRRA.dataset")
  57. num_workers = args.num_workers
  58. dataset = __factory[args.dataset_name](root=args.root_dir)
  59. num_classes = len(dataset.train_id_container)
  60. # build dataloader for testing
  61. if tranforms:
  62. test_transforms = tranforms
  63. else:
  64. test_transforms = build_transforms(img_size=args.img_size,
  65. is_train=False)
  66. ds = dataset.test
  67. test_img_set = ImageDataset(ds['image_pids'], ds['img_paths'],
  68. test_transforms)
  69. test_txt_set = TextDataset(ds['caption_pids'],
  70. ds['captions'],
  71. text_length=args.text_length)
  72. test_img_loader = DataLoader(test_img_set,
  73. batch_size=args.test_batch_size,
  74. shuffle=False,
  75. num_workers=num_workers)
  76. test_txt_loader = DataLoader(test_txt_set,
  77. batch_size=args.test_batch_size,
  78. shuffle=False,
  79. num_workers=num_workers)
  80. return test_img_loader, test_txt_loader, num_classes