dataset_LD.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. from torch.utils.data.dataset import T_co
  2. from models.base.base_dataset import BaseDataset
  3. import glob
  4. import json
  5. import math
  6. import os
  7. import random
  8. import cv2
  9. import PIL
  10. import matplotlib.pyplot as plt
  11. import matplotlib as mpl
  12. from torchvision.utils import draw_bounding_boxes
  13. import numpy as np
  14. import numpy.linalg as LA
  15. import torch
  16. from skimage import io
  17. from torch.utils.data import Dataset
  18. from torch.utils.data.dataloader import default_collate
  19. import matplotlib.pyplot as plt
  20. from models.dataset_tool import line_boxes, read_masks_from_txt_wire, read_masks_from_pixels_wire, adjacency_matrix
  21. from tools.presets import DetectionPresetTrain
  22. def line_boxes1(target):
  23. boxs = []
  24. lines = target.cpu().numpy()
  25. if len(lines) > 0 and not (lines[0] == 0).all():
  26. for i, ((a, b)) in enumerate(lines):
  27. if i > 0 and (lines[i] == lines[0]).all():
  28. break
  29. if a[1] > b[1]:
  30. ymax = a[1] + 10
  31. ymin = b[1] - 10
  32. else:
  33. ymin = a[1] - 10
  34. ymax = b[1] + 10
  35. if a[0] > b[0]:
  36. xmax = a[0] + 10
  37. xmin = b[0] - 10
  38. else:
  39. xmin = a[0] - 10
  40. xmax = b[0] + 10
  41. boxs.append([ymin, xmin, ymax, xmax])
  42. # if boxs == []:
  43. # print(target)
  44. return torch.tensor(boxs)
  45. class WirePointDataset(BaseDataset):
  46. def __init__(self, dataset_path, transforms=None, dataset_type=None, target_type='pixel'):
  47. super().__init__(dataset_path)
  48. self.data_path = dataset_path
  49. print(f'data_path:{dataset_path}')
  50. self.transforms = transforms
  51. self.img_path = os.path.join(dataset_path, "images/" + dataset_type)
  52. self.lbl_path = os.path.join(dataset_path, "labels/" + dataset_type)
  53. self.imgs = os.listdir(self.img_path)
  54. self.lbls = os.listdir(self.lbl_path)
  55. self.target_type = target_type
  56. # self.default_transform = DefaultTransform()
  57. def __getitem__(self, index) -> T_co:
  58. img_path = os.path.join(self.img_path, self.imgs[index])
  59. lbl_path = os.path.join(self.lbl_path, self.imgs[index][:-3] + 'json')
  60. img = PIL.Image.open(img_path).convert('RGB')
  61. w, h = img.size
  62. # wire_labels, target = self.read_target(item=index, lbl_path=lbl_path, shape=(h, w))
  63. target = self.read_target(item=index, lbl_path=lbl_path, shape=(h, w))
  64. if self.transforms:
  65. img, target = self.transforms(img, target)
  66. else:
  67. img = self.default_transform(img)
  68. # print(f'img:{img.shape}')
  69. return img, target
  70. def __len__(self):
  71. return len(self.imgs)
  72. def read_target(self, item, lbl_path, shape, extra=None):
  73. # print(f'lbl_path:{lbl_path}')
  74. with open(lbl_path, 'r') as file:
  75. lable_all = json.load(file)
  76. n_stc_posl = 300
  77. n_stc_negl = 40
  78. use_cood = 0
  79. use_slop = 0
  80. wire = lable_all["wires"][0] # ??
  81. line_pos_coords = np.random.permutation(wire["line_pos_coords"]["content"])[: n_stc_posl] # ?????????
  82. line_neg_coords = np.random.permutation(wire["line_neg_coords"]["content"])[: n_stc_negl]
  83. npos, nneg = len(line_pos_coords), len(line_neg_coords)
  84. lpre = np.concatenate([line_pos_coords, line_neg_coords], 0) # ??????????
  85. for i in range(len(lpre)):
  86. if random.random() > 0.5:
  87. lpre[i] = lpre[i, ::-1]
  88. ldir = lpre[:, 0, :2] - lpre[:, 1, :2]
  89. ldir /= np.clip(LA.norm(ldir, axis=1, keepdims=True), 1e-6, None)
  90. feat = [
  91. lpre[:, :, :2].reshape(-1, 4) / 512 * use_cood,
  92. ldir * use_slop,
  93. lpre[:, :, 2],
  94. ]
  95. feat = np.concatenate(feat, 1)
  96. wire_labels = {
  97. "junc_coords": torch.tensor(wire["junc_coords"]["content"])[:, :2],
  98. "jtyp": torch.tensor(wire["junc_coords"]["content"])[:, 2].byte(),
  99. "line_pos_idx": adjacency_matrix(len(wire["junc_coords"]["content"]), wire["line_pos_idx"]["content"]),
  100. # ???????????
  101. "line_neg_idx": adjacency_matrix(len(wire["junc_coords"]["content"]), wire["line_neg_idx"]["content"]),
  102. # ??????????
  103. "lpre": torch.tensor(lpre)[:, :, :2],
  104. "lpre_label": torch.cat([torch.ones(npos), torch.zeros(nneg)]), # ?????? 1?0
  105. "lpre_feat": torch.from_numpy(feat),
  106. "junc_map": torch.tensor(wire['junc_map']["content"]),
  107. "junc_offset": torch.tensor(wire['junc_offset']["content"]),
  108. "line_map": torch.tensor(wire['line_map']["content"]),
  109. }
  110. labels = []
  111. #
  112. # if self.target_type == 'polygon':
  113. # labels, masks = read_masks_from_txt_wire(lbl_path, shape)
  114. # elif self.target_type == 'pixel':
  115. # labels = read_masks_from_pixels_wire(lbl_path, shape)
  116. # print(torch.stack(masks).shape) # [???, 512, 512]
  117. target = {}
  118. # target["labels"] = torch.stack(labels)
  119. target["image_id"] = torch.tensor(item)
  120. # return wire_labels, target
  121. target["wires"] = wire_labels
  122. # target["boxes"] = line_boxes(target)
  123. target["boxes"] = line_boxes1(torch.tensor(wire["line_pos_coords"]["content"]))
  124. target["labels"]= torch.ones(len(target["boxes"]),dtype=torch.int64)
  125. # print(f'target["labels"]:{ target["labels"]}')
  126. # print(f'boxes:{target["boxes"].shape}')
  127. if target["boxes"].numel() == 0:
  128. print("Tensor is empty")
  129. print(f'path:{lbl_path}')
  130. return target
  131. def show(self, idx):
  132. image, target = self.__getitem__(idx)
  133. cmap = plt.get_cmap("jet")
  134. norm = mpl.colors.Normalize(vmin=0.4, vmax=1.0)
  135. sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
  136. sm.set_array([])
  137. def imshow(im):
  138. plt.close()
  139. plt.tight_layout()
  140. plt.imshow(im)
  141. plt.colorbar(sm, fraction=0.046)
  142. plt.xlim([0, im.shape[0]])
  143. plt.ylim([im.shape[0], 0])
  144. def draw_vecl(lines, sline, juncs, junts, fn=None):
  145. img_path = os.path.join(self.img_path, self.imgs[idx])
  146. imshow(io.imread(img_path))
  147. if len(lines) > 0 and not (lines[0] == 0).all():
  148. for i, ((a, b), s) in enumerate(zip(lines, sline)):
  149. if i > 0 and (lines[i] == lines[0]).all():
  150. break
  151. plt.plot([a[1], b[1]], [a[0], b[0]], c="red", linewidth=1) # a[1], b[1]?????
  152. if not (juncs[0] == 0).all():
  153. for i, j in enumerate(juncs):
  154. if i > 0 and (i == juncs[0]).all():
  155. break
  156. plt.scatter(j[1], j[0], c="red", s=2, zorder=100) # ? s=64
  157. img_path = os.path.join(self.img_path, self.imgs[idx])
  158. img = PIL.Image.open(img_path).convert('RGB')
  159. boxed_image = draw_bounding_boxes((self.default_transform(img) * 255).to(torch.uint8), target["boxes"],
  160. colors="yellow", width=1)
  161. plt.imshow(boxed_image.permute(1, 2, 0).numpy())
  162. plt.show()
  163. plt.show()
  164. if fn != None:
  165. plt.savefig(fn)
  166. junc = target['wires']['junc_coords'].cpu().numpy()
  167. jtyp = target['wires']['jtyp'].cpu().numpy()
  168. juncs = junc[jtyp == 0]
  169. junts = junc[jtyp == 1]
  170. lpre = target['wires']["lpre"].cpu().numpy()
  171. vecl_target = target['wires']["lpre_label"].cpu().numpy()
  172. lpre = lpre[vecl_target == 1]
  173. # draw_vecl(lpre, np.ones(lpre.shape[0]), juncs, junts, save_path)
  174. draw_vecl(lpre, np.ones(lpre.shape[0]), juncs, junts)
  175. def show_img(self, img_path):
  176. pass