def pull_item(self, idx): file = self.files[self.index[idx]] file = file.replace('\\', '/') image = file.replace('.json', '.jpg') json_data = json.loads(open(file).read()) image = io.imread(image) image, window, scale, padding, crop = resize_image_fixed_size( image, self.image_size) label_ix = [] box = [] for s in json_data['shapes']: label_ix.append(self.class_to_idx[s['label']]) box.append([ s['points'][0][0], s['points'][0][1], s['points'][2][0], s['points'][2][1] ]) box = np.asarray(box) box = box * scale box[:, 0] = box[:, 0] + padding[1][0] box[:, 1] = box[:, 1] + padding[0][0] box[:, 2] = box[:, 2] + padding[1][1] box[:, 3] = box[:, 3] + padding[0][1] box = box / np.asarray([ self.image_size[0], self.image_size[1], self.image_size[0], self.image_size[1] ]) return image, box, label_ix
def run(): batch_size = 1 image = tf.placeholder(dtype=tf.float32, shape=(batch_size, 512, 512, 3)) global_step = tf.train.get_or_create_global_step() out_put = resnet50.fpn(image) out_put = tf.nn.sigmoid(out_put) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, 'drive/model.ckpt-53581') for x in glob.glob( '/media/dsl/20d6b919-92e1-4489-b2be-a092290668e4/BDD100K/bdd100k/images/100k/val/*.*' ): ig = io.imread(x) org, window, scale, padding, crop = utils.resize_image_fixed_size( ig, config.image_size) ig = org - [123.15, 115.90, 103.06] ig = np.expand_dims(ig, 0) fd = {image: ig} o_put = sess.run(out_put, feed_dict=fd) plt.subplot(221) plt.title('step1') plt.imshow(o_put[0, :, :, 0], aspect="auto", cmap='gray') plt.subplot(222) plt.title('step2') plt.imshow(o_put[0, :, :, 1], aspect="auto", cmap='gray') plt.subplot(223) plt.title('org') plt.imshow(org, aspect="auto") plt.show()
def pull_item(self, idx): item_data = self.data[idx] image_name = item_data['name'] labels = item_data['labels'] label_ix = [] box = [] ig_data = io.imread(os.path.join(self.image_dr, image_name)) for ll in labels: category_name = ll['category'] if self.class_mapix.get(category_name, None) is not None: box2d = ll['box2d'] label_ix.append(self.class_mapix[category_name]) box.append( [box2d['x1'], box2d['y1'], box2d['x2'], box2d['y2']]) box = np.asarray(box) if self.is_crop: ig, box, label_ix = utils.crop_image_with_box( ig_data, self.image_size, box, label_ix) else: ig, window, scale, padding, crop = utils.resize_image_fixed_size( ig_data, self.image_size) box = box * scale box[:, 0] = box[:, 0] + padding[1][0] box[:, 1] = box[:, 1] + padding[0][0] box[:, 2] = box[:, 2] + padding[1][1] box[:, 3] = box[:, 3] + padding[0][1] box = box / np.asarray([ self.image_size[1], self.image_size[0], self.image_size[1], self.image_size[0] ]) return ig, box, label_ix
def pull_item(self, idx): json_pth = self.data[idx] image_pth = json_pth.replace('.json', '.png') ig_data = io.imread(image_pth)[:, :, 0:3] instance_masks = np.zeros( shape=[self.image_size[0], self.image_size[1], 3]) js_data = json.loads(open(json_pth).read()) msk = [] ids = [] for b in js_data['boundary']: label = b['correctionType'] if label == 'land': points = b['points'] p = [] for pp in points: p.append([pp['pix_x'], pp['pix_y']]) poly_land = np.zeros( shape=[self.image_size[0], self.image_size[1], 3], dtype=np.uint8) cv2.fillPoly(poly_land, np.asarray([p], np.int), (255, 255, 255)) msk.append(poly_land[:, :, 0:1]) ids.append(0) if len(msk) > 1: msk = np.concatenate(msk, axis=2) elif len(msk) == 1: msk = msk[0] else: return None ig, window, scale, padding, crop = utils.resize_image_fixed_size( ig_data, self.image_size) msk = utils.resize_mask(msk, scale, padding, crop) if random.randint(0, 1) == 2: ag = self.aug.to_deterministic() ig_data = ag.augment_image(ig_data) msk = ag.augment_image(msk) box = utils.extract_bboxes(msk) ids = np.asarray(ids) mask = np.sum(msk, 2) mask[np.where(mask > 255)] = 255 box = box / np.asarray([ self.image_size[0], self.image_size[1], self.image_size[0], self.image_size[1] ]) return ig, box, ids, mask
def pull_item(self, idx): item_data = self.data[idx] image_name = item_data['name'] labels = item_data['labels'] label_ix = [] box = [] ig_data = io.imread(os.path.join(self.image_dr, image_name)) direct = np.zeros(shape=ig_data.shape, dtype=np.uint8) alter = np.zeros(shape=ig_data.shape, dtype=np.uint8) for ll in labels: category_name = ll['category'] if category_name == 'drivable area': if ll['attributes']['areaType'] == 'direct': pts = ll['poly2d'] for x in pts: cv2.fillPoly(direct, [np.asarray(x['vertices'], np.int)], (255, 255, 255)) elif ll['attributes']['areaType'] == 'alternative': pts = ll['poly2d'] for x in pts: cv2.fillPoly(alter, [np.asarray(x['vertices'], np.int)], (255, 255, 255)) ig, window, scale, padding, crop = utils.resize_image_fixed_size( ig_data, self.image_size) direct, window, scale, padding, crop = utils.resize_image_fixed_size( direct, self.image_size) alter, window, scale, padding, crop = utils.resize_image_fixed_size( alter, self.image_size) labels = np.zeros(shape=(ig.shape[0], ig.shape[1], 2), dtype=np.uint8) labels[:, :, 0] = direct[:, :, 0] labels[:, :, 1] = alter[:, :, 0] return ig, labels
def pull_item(self, idx): item_data = self.data[idx] image_name = item_data['name'] labels = item_data['labels'] label_ix = [] box = [] ig_data = io.imread(os.path.join(self.image_dr, image_name)) instance_masks = [] cls_ids = [] for ll in labels: category_name = ll['category'] if category_name == 'drivable area': if ll['attributes']['areaType'] == 'direct': pts = ll['poly2d'] for x in pts: direct = np.zeros(shape=ig_data.shape, dtype=np.uint8) cv2.fillPoly(direct, [np.asarray(x['vertices'], np.int)], (255, 255, 255)) cls_ids.append(0) instance_masks.append(direct[:, :, 0]) elif ll['attributes']['areaType'] == 'alternative': pts = ll['poly2d'] for x in pts: alter = np.zeros(shape=ig_data.shape, dtype=np.uint8) cv2.fillPoly(alter, [np.asarray(x['vertices'], np.int)], (255, 255, 255)) cls_ids.append(1) instance_masks.append(alter[:, :, 0]) mask = np.asarray(instance_masks) ig, window, scale, padding, crop = utils.resize_image_fixed_size( ig_data, self.image_size) if len(labels) == 0: return mask = np.transpose(mask, axes=[1, 2, 0]) mask = utils.resize_mask(mask, scale, padding, crop) image, mask = coco_handler.aug(ig, mask) boxes = utils.extract_bboxes(mask) mask = utils.minimize_mask(boxes, mask, mini_shape=(self.mask_shape, self.mask_shape)) #boxes = boxes /np.asarray([self.image_size[1], self.image_size[0],self.image_size[1], self.image_size[0]]) return ig, cls_ids, boxes, mask
def pull_item(self, index): img_id = os.path.join(self.image_path, self.images[index]) boxes = self.boxes[index] labels = np.ones(len(boxes)) * 0 if len(boxes) == 0: return None, None, None img = io.imread(img_id) box = np.asarray(boxes) if True: ig, window, scale, padding, crop = utils.resize_image_fixed_size( img, self.image_size) if len(labels) == 0: return ig, box, labels box = box * scale box[:, 0] = box[:, 0] + padding[1][0] box[:, 1] = box[:, 1] + padding[0][0] box[:, 2] = box[:, 2] + padding[1][1] box[:, 3] = box[:, 3] + padding[0][1] bb = [] for ix, x in enumerate(box): bb.append(ia.BoundingBox(x[0], x[1], x[2], x[3], labels[ix])) bbs = ia.BoundingBoxesOnImage(bb, shape=self.image_size) seq_det = self.img_aug.to_deterministic() image_aug = seq_det.augment_images([ig])[0] bbs_aug = seq_det.augment_bounding_boxes([bbs])[0] box = [] labels = [] for i in range(len(bbs.bounding_boxes)): after = bbs_aug.bounding_boxes[i] box.append([after.x1, after.y1, after.x2, after.y2]) labels.append(after.label) box = np.asarray(box) box = box / np.asarray([ self.image_size[1], self.image_size[0], self.image_size[1], self.image_size[0] ]) box = np.clip(box, 0, 1) return image_aug, box, labels
def pull_item(self, index): image_path = self.images[index] json_path = image_path.replace('.jpg', '.json') bundry = json.loads(open(json_path).read().encode('utf8')) img = cv2.imread(image_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) labels = [] box = [] for x in bundry['shapes']: pt = x['points'] labels.append(self.labels_index[x['label']]) box.append([pt[0][0],pt[0][1],pt[2][0],pt[2][1]]) box = np.asarray(box) if False: ig, window, scale, padding, crop = utils.resize_image_fixed_size(img, self.image_size) if len(labels)==0: return ig, box, labels box = box * scale box[:, 0] = box[:, 0] + padding[1][0] box[:, 1] = box[:, 1] + padding[0][0] box[:, 2] = box[:, 2] + padding[1][1] box[:, 3] = box[:, 3] + padding[0][1] else: ig, box, labels = utils.crop_image_with_box(img, self.image_size, box, labels) bb = [] for ix, x in enumerate(box): bb.append(ia.BoundingBox(x[0],x[1],x[2],x[3],labels[ix])) bbs = ia.BoundingBoxesOnImage(bb, shape=self.image_size) seq_det = self.img_aug.to_deterministic() image_aug = seq_det.augment_images([ig])[0] bbs_aug = seq_det.augment_bounding_boxes([bbs])[0] box = [] labels = [] for i in range(len(bbs.bounding_boxes)): after = bbs_aug.bounding_boxes[i] box.append([after.x1, after.y1, after.x2, after.y2]) labels.append(after.label) box = np.asarray(box) box = box / np.asarray([self.image_size[1], self.image_size[0], self.image_size[1], self.image_size[0]]) box = np.clip(box, 0,1) return image_aug, box, labels
def run(json_pth, save_pth, image_pth, check_dr): js = json.loads(open(json_pth).read()) lbses = js['label_ix'] new_lbs = dict() for x in lbses: new_lbs[lbses[x]] = x print(new_lbs) base_dr = image_pth # base_dr = 'D:/deep_learn_data/luntai/crop/crop' model = resnet18(num_classes=1000, pretrained=None) model.fc = nn.Linear(512, 1) model.load_state_dict(torch.load(check_dr), strict=True) model.cuda() model.eval() for k1 in glob.glob(os.path.join(base_dr, '*.*')): imag = Image.open(k1) ig = np.asarray(imag) img, _, _, _, _ = resize_image_fixed_size(ig, [512, 512]) ig = Image.fromarray(img) ig = trans(ig) ig = ig.unsqueeze(0) ig = torch.autograd.Variable(ig.cuda()) output = model(ig) output = output.squeeze() ot = F.sigmoid(output) print(ot) pred = ot.ge(0.5).float() pred_lb = pred.cpu().numpy() pred_lb = int(pred_lb) pred_label = new_lbs[pred_lb] name = k.replace('\\', '/').split('/')[-1] new_path = os.path.join(save_pth, pred_label) if not os.path.exists(new_path): os.makedirs(new_path) shutil.copy(k1, new_path)
def run(json_pth, save_pth, image_pth, check_dr): js = json.loads(open(json_pth).read()) lbses = js['label_ix'] new_lbs = dict() for x in lbses: new_lbs[lbses[x]] = x print(new_lbs) base_dr = image_pth # base_dr = 'D:/deep_learn_data/luntai/crop/crop' model = se_resnext50_32x4d(num_classes=1000, pretrained=None) model.fc1 = nn.Linear(2048, 2) model.fc2 = nn.Linear(2, len(new_lbs)) model.load_state_dict(torch.load(check_dr), strict=False) model.cuda() model.eval() for k1 in glob.glob(os.path.join(base_dr, '*.*')): imag = Image.open(k1) ig = np.asarray(imag) img, _, _, _, _ = resize_image_fixed_size(ig, [512, 512]) ig = Image.fromarray(img) ig = trans(ig) ig = ig.unsqueeze(0) ig = torch.autograd.Variable(ig.cuda()) fc1, output = model(ig) output = F.softmax(output, dim=1) pred = output.data.squeeze(dim=0).cpu().numpy() pred_lb = np.argmax(pred) sc = np.max(pred) pred_label = new_lbs[pred_lb] name = k.replace('\\', '/').split('/')[-1] new_path = os.path.join(save_pth, pred_label) if not os.path.exists(new_path): os.makedirs(new_path) shutil.copy(k1, new_path)
def pull_item(self, idx): image_path = self.data[idx] js_path = self.data[idx].replace('.jpg', '.json') js_data = json.loads(open(js_path).read()) label_ix = [] box = [] ig_data = io.imread(image_path) for b in js_data: category_name = b['label'] if self.class_mapix.get(category_name, None) is not None: bound = np.asarray(b['points']) minx, miny, maxx, maxy = min(bound[:, 0]), min( bound[:, 1]), max(bound[:, 0]), max(bound[:, 1]), box.append([minx, miny, maxx, maxy]) label_ix.append(self.class_mapix[category_name]) box = np.asarray(box) if box.shape[0] == 0: return None, None, None if self.is_crop: ig, box, label_ix = utils.crop_image_with_box( ig_data, self.image_size, box, label_ix) else: ig, window, scale, padding, crop = utils.resize_image_fixed_size( ig_data, self.image_size) box = box * scale box[:, 0] = box[:, 0] + padding[1][0] box[:, 1] = box[:, 1] + padding[0][0] box[:, 2] = box[:, 2] + padding[1][1] box[:, 3] = box[:, 3] + padding[0][1] box = box / np.asarray([ self.image_size[1], self.image_size[0], self.image_size[1], self.image_size[0] ]) return ig, box, label_ix
def pull_item(self, index): image_path = self.images[index] json_path = image_path.replace('.png', '.json') bundry = json.loads(open(json_path).read().encode('utf8')) ig = cv2.imread(image_path) ig = cv2.cvtColor(ig, cv2.COLOR_BGR2RGB) ig = aug_utils.pytorch_aug_color(ig) shape = ig.shape[0:2] total = len(bundry) msk = np.zeros((shape[0], shape[1], total), dtype=np.uint8) ids = [] orchard = np.zeros(shape=(shape[0], shape[1], 3), dtype=np.uint8) msk = [] for idx, b in enumerate(bundry): if b['correction_type'] == 'tree': mask = np.zeros(shape=(shape[0], shape[1], 3), dtype=np.uint8) pts = [] for p in b['boundary']: pts.append([int(p['x']), int(p['y'])]) pts = np.array(pts, np.int32) cv2.fillPoly(mask, [pts], color=(255, 255, 255)) msk.append(mask[:, :, 0:1]) ids.append(0) elif b['correction_type'] == 'orchard': mask = np.zeros(shape=(shape[0], shape[1], 3), dtype=np.uint8) pts = [] for p in b['boundary']: pts.append([int(p['x']), int(p['y'])]) pts = np.array(pts, np.int32) cv2.fillPoly(mask, [pts], color=(255, 255, 255)) orchard += mask ig = ig * (orchard / 255) if len(msk) > 1: msk = np.concatenate(msk, axis=2) elif len(msk) == 1: msk = msk[0] else: return None ig, window, scale, padding, crop = utils.resize_image_fixed_size( ig, self.image_size) msk = utils.resize_mask(msk, scale, padding, crop) if random.randint(0, 1) != 1: ag = self.aug.to_deterministic() ig = ag.augment_image(ig) msk = ag.augment_image(msk) box = utils.extract_bboxes(msk) ids = np.asarray(ids) mj = (box[:, 3] - box[:, 1]) * (box[:, 2] - box[:, 0]) mk = np.where(mj > self.image_size[0] * self.image_size[1] / 32 / 32) box = box[mk] ids = ids[mk] mj = (box[:, 3] - box[:, 1]) / (box[:, 2] - box[:, 0]) mk = np.where(mj > 0.25) box = box[mk] ids = ids[mk] mj = (box[:, 3] - box[:, 1]) / (box[:, 2] - box[:, 0]) mk = np.where(mj < 4) box = box[mk] ids = ids[mk] box = box / np.asarray([ self.image_size[0], self.image_size[1], self.image_size[0], self.image_size[1] ]) return ig, box, ids
def pull_item(self, index): image_path = self.images[index] json_path = image_path.replace('.png', '.json') bundry = json.loads(open(json_path).read().encode('utf8')) ig = cv2.imread(image_path) ig = cv2.cvtColor(ig, cv2.COLOR_BGR2RGB) shape = ig.shape[0:2] orchard = [] msk = [] ids = [] for idx, b in enumerate(bundry): if b['correction_type'] == 'tree': mask = np.zeros(shape=(shape[0], shape[1], 3)) pts = [] for p in b['boundary']: pts.append([int(p['x']), int(p['y'])]) pts = np.array(pts, np.int32) cv2.fillPoly(mask, [pts], color=(255, 255, 255)) msk.append(mask[:, :, 0:1]) ids.append(0) elif b['correction_type'] == 'orchard': mask = np.zeros(shape=(shape[0], shape[1], 3)) pts = [] for p in b['boundary']: pts.append([int(p['x']), int(p['y'])]) pts = np.array(pts, np.int32) cv2.fillPoly(mask, [pts], color=(255, 255, 255)) orchard.append(mask[:, :, 0:1]) if len(msk) > 1: msk = np.concatenate(msk, axis=2) elif len(msk) == 1: msk = msk[0] else: return None if len(orchard) > 1: orchard = np.concatenate(orchard, axis=2) elif len(orchard) == 1: orchard = orchard[0] else: return None ig, window, scale, padding, crop = utils.resize_image_fixed_size( ig, self.image_size) msk = utils.resize_mask(msk, scale, padding, crop) orchard = utils.resize_mask(orchard, scale, padding, crop) if random.randint(0, 1) == 1: ag = self.aug.to_deterministic() ig = ag.augment_image(ig) msk = ag.augment_image(msk) orchard = ag.augment_image(orchard) box = utils.extract_bboxes(msk) ids = np.asarray(ids) mask = np.transpose(msk, [2, 0, 1]) mj = (box[:, 3] - box[:, 1]) * (box[:, 2] - box[:, 0]) mk = np.where(mj < self.image_size[0] * self.image_size[1] / 3 / 3) box = box[mk] ids = ids[mk] mask = mask[mk] mj = (box[:, 3] - box[:, 1]) * (box[:, 2] - box[:, 0]) mk = np.where(mj > self.image_size[0] * self.image_size[1] / 32 / 32) box = box[mk] ids = ids[mk] mask = mask[mk] mj = (box[:, 3] - box[:, 1]) / (box[:, 2] - box[:, 0]) mk = np.where(mj > 0.3) box = box[mk] ids = ids[mk] mask = mask[mk] mj = (box[:, 3] - box[:, 1]) / (box[:, 2] - box[:, 0]) mk = np.where(mj < 3) box = box[mk] ids = ids[mk] mask = mask[mk] orchard = np.sum(orchard, axis=2) mask = np.transpose(mask, [1, 2, 0]) #mask = utils.minimize_mask(box, mask, mini_shape=(28, 28)) mask = np.sum(mask, 2) mask[np.where(mask > 255)] = 255 box = box / np.asarray([ self.image_size[0], self.image_size[1], self.image_size[0], self.image_size[1] ]) #ig = ig*(orchard/255).astype(np.uint8) return ig, box, ids, mask
def pull_item(self, index): img_id = self.ids[index] boxes = [] labels = [] target = ET.parse(self._annopath % img_id).getroot() for obj in target.iter('object'): difficult = int(obj.find('difficult').text) == 1 if False and difficult: continue name = obj.find('name').text.lower().strip() bbox = obj.find('bndbox') pts = ['xmin', 'ymin', 'xmax', 'ymax'] bndbox = [] for i, pt in enumerate(pts): cur_pt = int(bbox.find(pt).text) - 1 bndbox.append(cur_pt) if self.class_mapix.get(name, None) is not None: label_idx = self.class_mapix[name] labels.append(label_idx) #labels.append(0) boxes.append(bndbox) if len(boxes) == 0: return None, None, None img = io.imread(self._imgpath % img_id) box = np.asarray(boxes) if True: ig, window, scale, padding, crop = utils.resize_image_fixed_size( img, self.image_size) if len(labels) == 0: return ig, box, labels box = box * scale box[:, 0] = box[:, 0] + padding[1][0] box[:, 1] = box[:, 1] + padding[0][0] box[:, 2] = box[:, 2] + padding[1][1] box[:, 3] = box[:, 3] + padding[0][1] else: ig, box, labels = utils.crop_image_with_box( img, self.image_size, box, labels) bb = [] for ix, x in enumerate(box): bb.append(ia.BoundingBox(x[0], x[1], x[2], x[3], labels[ix])) bbs = ia.BoundingBoxesOnImage(bb, shape=self.image_size) seq_det = self.img_aug.to_deterministic() image_aug = seq_det.augment_images([ig])[0] bbs_aug = seq_det.augment_bounding_boxes([bbs])[0] box = [] labels = [] for i in range(len(bbs.bounding_boxes)): after = bbs_aug.bounding_boxes[i] box.append([after.x1, after.y1, after.x2, after.y2]) labels.append(after.label) box = np.asarray(box) box = box / np.asarray([ self.image_size[1], self.image_size[0], self.image_size[1], self.image_size[0] ]) box = np.clip(box, 0, 1) return image_aug, box, labels