def __init__(self, data_dir='auto', split='train'): super(ADE20KSemanticSegmentationDataset, self).__init__() if data_dir is 'auto': data_dir = get_ade20k(root, url) if split == 'train' or split == 'val': img_dir = os.path.join( data_dir, 'ADEChallengeData2016', 'images', 'training' if split == 'train' else 'validation') label_dir = os.path.join( data_dir, 'ADEChallengeData2016', 'annotations', 'training' if split == 'train' else 'validation') else: raise ValueError( 'Please give \'split\' argument with either \'train\' or ' '\'val\'.') self.img_paths = sorted(glob.glob(os.path.join(img_dir, '*.jpg'))) self.label_paths = sorted(glob.glob(os.path.join(label_dir, '*.png'))) self.add_getter('img', lambda i: read_image(self.img_paths[i])) self.add_getter( 'iabel', lambda i: read_image( self.label_paths[i], dtype=np.int32, color=False)[0])
def get_example(self, i): """Returns the i-th example. Returns a color image and a label image. The color image is in CHW format and the label image is in HW format. Args: i (int): The index of the example. Returns: tuple of a color image and a label whose shapes are (3, H, W) and (H, W) respectively. H and W are height and width of the image. The dtype of the color image is :obj:`numpy.float32` and the dtype of the label image is :obj:`numpy.int32`. """ img = read_image(self.img_paths[i]) label_orig = read_image(self.label_paths[i], dtype=np.int32, color=False)[0] if self.ignore_labels: label_out = np.ones(label_orig.shape, dtype=np.int32) * -1 for label in cityscapes_labels: if not label.ignoreInEval: label_out[label_orig == label.id] = label.trainId else: label_out = label_orig return img, label_out
def get_example(self, i): """Returns the i-th example. Args: i (int): The index of the example. Returns: tuple of an image and its label. The image is in CHW format and its color channel is ordered in RGB. If :obj:`return_bb = True`, a bounding box is appended to the returned value. If :obj:`return_mask = True`, a probability map is appended to the returned value. """ img = utils.read_image( os.path.join(self.data_dir, 'images', self.paths[i]), color=True) label = self._labels[i] if not self.return_prob_map: if self.return_bb: return img, label, self.bbs[i] else: return img, label prob_map = utils.read_image(self.prob_map_paths[i], dtype=np.uint8, color=False) prob_map = prob_map.astype(np.float32) / 255 # [0, 255] -> [0, 1] prob_map = prob_map[0] # (1, H, W) --> (H, W) if self.return_bb: return img, label, self.bbs[i], prob_map else: return img, label, prob_map
def get_example(self, i): # this i is transformed to id for the entire dataset img = utils.read_image(os.path.join(self.data_dir, 'images', self.paths[i]), color=True) keypoint = np.array(self.kp_dict[i], dtype=np.float32) kp_mask = np.array(self.kp_mask_dict[i], dtype=np.bool) if self.crop_bbox: # (y_min, x_min, y_max, x_max) bbox = self.bboxes[i].astype(np.int32) img = img[:, bbox[0]:bbox[2], bbox[1]:bbox[3]] keypoint[:, :2] = keypoint[:, :2] - bbox[:2] if not self.return_mask: return img, keypoint, kp_mask path, _ = os.path.splitext(self.paths[i]) mask = utils.read_image(os.path.join(self.mask_dir, path + '.png'), dtype=np.uint8, color=False) if self.crop_bbox: mask = mask[:, bbox[0]:bbox[2], bbox[1]:bbox[3]] return img, keypoint, kp_mask, mask
def evaluate_single(self, model): path = '/home/mmvc/Documents/test_photos_1080/_DSC3158.JPG' image_np = read_image(path, color=True) # image_np = read_image("/home/mmvc/Git/point-to-tell/hands_data/hands_right_v1/hand_371.png", color=True) # image_np = read_image("/home/mmvc/Git/point-to-tell/VocHand_3/VOCHand_v3/VOCHand_1.jpg", color=True) # image_np = read_image("/home/mmvc/Documents/hand.jpg", color=True) original_image = cv2.imread(path) original_image = np.uint32(original_image) image_np = np.array([image_np]) image_chainer = Variable(image_np) image_chainer.to_gpu() cl_scores = model.classify(image_chainer, dropout_ratio=None) print("hand: " + str(cl_scores)) path = '/home/mmvc/Documents/test_photos_1080/_DSC3133.JPG' image_np = read_image(path, color=True) # image_np = read_image("/home/mmvc/Git/point-to-tell/hands_data/hands_right_v1/hand_371.png", color=True) # image_np = read_image("/home/mmvc/Git/point-to-tell/VocHand_3/VOCHand_v3/VOCHand_1.jpg", color=True) # image_np = read_image("/home/mmvc/Documents/hand.jpg", color=True) original_image = cv2.imread(path) original_image = np.uint32(original_image) image_np = np.array([image_np]) image_chainer = Variable(image_np) image_chainer.to_gpu() cl_scores = model.classify(image_chainer, dropout_ratio=None) print("no hand: " + str(cl_scores))
def get_example(self, i): # this i is transformed to id for the entire dataset img = utils.read_image( os.path.join(self.data_dir, 'images', self.fns[i]), color=True) keypoint = np.array(self.kp_dict[i], dtype=np.float32) kp_mask = np.array(self.kp_mask_dict[i], dtype=np.bool) if self.crop_bbox: bbox = self.bboxes[i] # (x, y, width, height) img = img[bbox[1]: bbox[1] + bbox[3], bbox[0]: bbox[0] + bbox[2]] keypoint[:, :2] = keypoint[:, :2] - np.array([bbox[0], bbox[1]]) if not self.return_mask: return img, keypoint, kp_mask mask = utils.read_image( os.path.join(self.mask_dir, self.fns[i][:-4] + '.png'), dtype=np.uint8, color=False) if self.crop_bbox: mask = mask[:, bbox[1]: bbox[1] + bbox[3], bbox[0]: bbox[0] + bbox[2]] return img, keypoint, kp_mask, mask
def demo_enet(): """Demo ENet.""" chainer.config.train = False chainer.config.enable_backprop = False config, img_path = parse_args() test_data = load_dataset_test(config["dataset"]) test_iter = create_iterator_test(test_data, config['iterator']) model = get_model(config["model"]) devices = parse_devices(config['gpus']) if devices: model.to_gpu(devices['main']) img = read_image(img_path) img = img.transpose(1, 2, 0) img = cv2.resize(img, (1024, 512)).transpose(2, 0, 1) for i in range(2): s = time.time() pred = model.predict(img)[0] print("time: {}".format(time.time() - s)) # Save the result image ax = vis_image(img) _, legend_handles = vis_semantic_segmentation( pred, label_colors=cityscapes_label_colors, label_names=cityscapes_label_names, alpha=1.0, ax=ax) ax.legend(handles=legend_handles, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) plot.axis('off') plot.show() fig, (ax1, ax2) = plot.subplots(ncols=2, figsize=(10, 3)) img = read_image( img_path.replace("leftImg8bit", "gtFine").replace("gtFine.png", "gtFine_color.png")) img = img.transpose(1, 2, 0) ax1.set_title("GroundTruth") img = cv2.resize(img, (1024, 512)).transpose(2, 0, 1) ax1 = vis_image(img, ax1) ax2 = vis_image(img, ax2) ax2, legend_handles = vis_semantic_segmentation( pred, label_colors=cityscapes_label_colors, label_names=cityscapes_label_names, alpha=1.0, ax=ax2) ax2.set_title("Prediction") ax1.axis("off") ax2.axis("off") plot.tight_layout() plot.savefig("./compare.png", dpi=400, bbox_inches='tight') plot.show()
def test_read_image_as_grayscale(self): if self.dtype == np.float32: img = read_image(self.path, color=False) else: img = read_image(self.path, dtype=self.dtype, color=False) self.assertEqual(img.shape, (1, ) + self.size) self.assertEqual(img.dtype, self.dtype) if self.suffix in {'bmp', 'png'} and not self.color: np.testing.assert_equal(img, self.img.astype(self.dtype))
def test_read_image_as_grayscale(self): if self.dtype == np.float32: img = read_image(self.path, color=False) else: img = read_image(self.path, dtype=self.dtype, color=False) self.assertEqual(img.shape, (1,) + self.size) self.assertEqual(img.dtype, self.dtype) if self.suffix in {'bmp', 'png'} and not self.color: np.testing.assert_equal(img, self.img.astype(self.dtype))
def test_read_image_different_backends_as_color(self): chainer.config.cv_read_image_backend = 'cv2' cv2_img = read_image(self.path, dtype=self.dtype, color=True) chainer.config.cv_read_image_backend = 'PIL' pil_img = read_image(self.path, dtype=self.dtype, color=True) if self.suffix != 'jpg': np.testing.assert_equal(cv2_img, pil_img) else: # jpg decoders are differnet, so they produce different results assert np.mean(cv2_img == pil_img) > 0.99
def _load_label_inst(self, data_id): label_file = os.path.join( self.data_dir, 'SegmentationClass', data_id + '.png') inst_file = os.path.join( self.data_dir, 'SegmentationObject', data_id + '.png') label_img = read_image(label_file, dtype=np.int32, color=False) label_img = label_img[0] label_img[label_img == 255] = -1 inst_img = read_image(inst_file, dtype=np.int32, color=False) inst_img = inst_img[0] inst_img[inst_img == 0] = -1 inst_img[inst_img == 255] = -1 return label_img, inst_img
def test_read_image_as_color(self): if self.dtype == np.float32: img = read_image(self.path) else: img = read_image(self.path, dtype=self.dtype) self.assertEqual(img.shape, (3,) + self.size) self.assertEqual(img.dtype, self.dtype) if self.suffix in {'bmp', 'png'}: np.testing.assert_equal( img, np.broadcast_to(self.img, (3,) + self.size).astype(self.dtype))
def _load_label_inst(self, data_id): label_file = os.path.join(self.data_dir, 'SegmentationClass', data_id + '.png') inst_file = os.path.join(self.data_dir, 'SegmentationObject', data_id + '.png') label_img = read_image(label_file, dtype=np.int32, color=False) label_img = label_img[0] label_img[label_img == 255] = -1 inst_img = read_image(inst_file, dtype=np.int32, color=False) inst_img = inst_img[0] inst_img[inst_img == 0] = -1 inst_img[inst_img == 255] = -1 return label_img, inst_img
def _get_mask(self, i): id_ = self.all_masks[i] if id_ is None or not os.path.exists(id_): image_id = self.all_images[i] image_ = read_image(image_id, color=True) _, h, w = image_.shape mask = np.ones([h, w], dtype=np.int32) * -1 return mask # TODO(yuyu2172): Find an option to load properly even with cv2. with chainer.using_config('cv_read_image_backend', 'PIL'): label = read_image(id_, dtype=np.int32, color=False) label[label == 255] = -1 # (1, H, W) -> (H, W) return label[0]
def get_example(self, i): img_filename = self.img_filenames[i] img = read_image(img_filename) anno_filename = os.path.splitext(img_filename)[0] + '__labels.json' with open(anno_filename, 'r') as f: anno = json.load(f) anno = anno['labels'] bbox = [] label = [] for anno_i in anno: h = anno_i['size']['y'] w = anno_i['size']['x'] center_y = anno_i['centre']['y'] center_x = anno_i['centre']['x'] try: l = self.label_names.index(anno_i['label_class']) except Exception as e: print("Failed to index label class: {}".format(anno_i), file=sys.stderr) print("image file name: {}".format(img_filename), file=sys.stderr) print("annotation file name: {}".format(anno_filename), file=sys.stderr) continue bbox.append([ center_y - h / 2, center_x - w / 2, center_y + h / 2, center_x + w / 2 ]) label.append(l) return img, np.array(bbox, dtype=np.float32), np.array(label, dtype=np.int32)
def _load_label(self, data_dir, id_): label_file = os.path.join( data_dir, 'SegmentationClass', id_ + '.png') label = read_image(label_file, dtype=np.int32, color=False) label[label == 255] = -1 # (1, H, W) -> (H, W) return label[0]
def _get_label(self, i): label_path = os.path.join( self.data_dir, 'SegmentationClass', self.ids[i] + '.png') label = read_image(label_path, dtype=np.int32, color=False) label[label == 255] = -1 # (1, H, W) -> (H, W) return label[0]
def get_example(self, i): img_filename = self.img_filenames[i] img = read_image(img_filename) anno_filename = os.path.splitext(img_filename)[0] + '__labels.json' with open(anno_filename, 'r') as f: anno = json.load(f) anno = anno['labels'] bbox = [] label = [] for anno_i in anno: h = anno_i['size']['y'] w = anno_i['size']['x'] center_y = anno_i['centre']['y'] center_x = anno_i['centre']['x'] try: l = self.label_names.index(anno_i['label_class']) except Exception as e: print >> sys.stderr, "Failed to index label class: {}".format(anno_i) print >> sys.stderr, "image file name: {}".format(img_filename) print >> sys.stderr, "annotation file name: {}".format(anno_filename) continue bbox.append( [center_y - h / 2, center_x - w / 2, center_y + h / 2, center_x + w / 2]) label.append(l) return img, np.array(bbox, dtype=np.float32), np.array(label, dtype=np.int32)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model') parser.add_argument('image') args = parser.parse_args() model = ResNet50( pretrained_model=args.pretrained_model, n_class=len(voc_bbox_label_names)) model.pick = 'fc6' if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) predict_func = PredictFunc(model, thresh=0.5) labels, scores = predict_func([img]) label = labels[0] score = scores[0] print('predicted labels') for lb, sc in zip(label, score): print('names={} score={:.4f}'.format( voc_bbox_label_names[lb], sc)) vis_image(img) plt.show()
def _get_example(self, i): img_path = os.path.join(self.base_dir, self.img_paths[i].rstrip()) img = read_image(img_path) anno_path = img_path.replace( 'images', 'labels').replace( 'JPEGImages', 'labels').replace( '.jpg', '.txt').replace('.png', '.txt') anno = np.zeros(50*21) if os.path.getsize(anno_path): _, H, W = img.shape tmp = read_truths_args(anno_path, 8.0/W) size = tmp.size if size > 50*21: anno = tmp[0:50*21] elif size > 0: anno[0:size] = tmp anno = anno.reshape(-1, 21) anno = anno[:truths_length(anno)] point = anno[:, 1:19].reshape(-1, 9, 2).astype(np.float32) point[:, :, 0] *= W point[:, :, 1] *= H label = anno[:, 0].astype(np.int32) return img, point, label
def main(): parser = argparse.ArgumentParser() parser.add_argument( '--model', choices=('ssd300', 'ssd512'), default='ssd300') parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained_model', default='voc0712') parser.add_argument('image') args = parser.parse_args() if args.model == 'ssd300': model = SSD300( n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) elif args.model == 'ssd512': model = SSD512( n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) bboxes, labels, scores = model.predict([img]) bbox, label, score = bboxes[0], labels[0], scores[0] vis_bbox( img, bbox, label, score, label_names=voc_bbox_label_names) plot.show()
def get_example(self, i): """Returns the i-th example. Returns a color image, bounding boxes, masks and labels. The color image is in CHW format. Args: i (int): The index of the example. Returns: A tuple of color image, bounding boxes, masks and labels whose shapes are :math:`(3, H, W), (R, 4), (R, H, W), (R, )` respectively. :math:`H` and :math:`W` are height and width of the images, and :math:`R` is the number of objects in the image. The dtype of the color image and the bounding boxes are :obj:`numpy.float32`, that of the masks is :obj: `numpy.bool`, and that of the labels is :obj:`numpy.int32`. """ data_id = self.ids[i] img_file = os.path.join( self.data_dir, 'img', data_id + '.jpg') img = read_image(img_file, color=True) label_img, inst_img = self._load_label_inst(data_id) bbox, mask, label = voc_utils.image_wise_to_instance_wise( label_img, inst_img) return img, bbox, mask, label
def get_example(self, i): if i >= len(self): raise IndexError("index is too large") id_ = self.id_list[i] matdata = scipy.io.loadmat(os.path.join(self.data_dir, str(self.mode), "annotation", "{0}.mat".format(id_))) bboxs = matdata["BBoxs"] for i in range(len(bboxs)): bboxs[i] = [bboxs[i][1], bboxs[i][0], bboxs[i][1] + bboxs[i][3] - 1, bboxs[i][0] + bboxs[i][2] - 1] labels = matdata["class_ids"] if len(labels) != 0: labels = labels[0] # 1 ~ n -> 0 ~ n -1 for i in range(len(labels)): labels[i] = labels[i] - 1 if len(bboxs) == 0: bboxs = [[]] labels = np.asarray(labels, dtype=np.int32) bboxs = np.asarray(bboxs, dtype=np.float32) img_file = os.path.join( self.data_dir, str(self.mode), "image", "{0}.png".format(id_)) img = read_image(img_file, color=True) return img, bboxs, labels
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model', default='cityscapes') parser.add_argument('--min-input-size', type=int, default=None) parser.add_argument('image') args = parser.parse_args() model = DeepLabV3plusXception65( pretrained_model=args.pretrained_model, min_input_size=args.min_input_size) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) labels = model.predict([img]) label = labels[0] fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) vis_image(img, ax=ax1) ax2 = fig.add_subplot(1, 2, 2) # Do not overlay the label image on the color image vis_semantic_segmentation( None, label, voc_semantic_segmentation_label_names, voc_semantic_segmentation_label_colors, ax=ax2) plt.show()
def main(): chainer.config.train = False parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained_model', default='camvid') parser.add_argument('image') args = parser.parse_args() model = SegNetBasic( n_class=len(camvid_label_names), pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) labels = model.predict([img]) label = labels[0] fig = plot.figure() ax1 = fig.add_subplot(1, 2, 1) vis_image(img, ax=ax1) ax2 = fig.add_subplot(1, 2, 2) vis_label(label, camvid_label_names, camvid_label_colors, ax=ax2) plot.show()
def get_example(self, i): """Returns the i-th example. Returns a color image, bounding boxes, masks and labels. The color image is in CHW format. Args: i (int): The index of the example. Returns: A tuple of color image, masks and labels whose shapes are :math:`(3, H, W), (R, H, W), (R, )` respectively. :math:`H` and :math:`W` are height and width of the images, and :math:`R` is the number of objects in the image. The dtype of the color image is :obj:`numpy.float32`, that of the masks is :obj: `numpy.bool`, and that of the labels is :obj:`numpy.int32`. """ data_id = self.ids[i] img_file = os.path.join(self.data_dir, 'img', data_id + '.jpg') img = read_image(img_file, color=True) label_img, inst_img = self._load_label_inst(data_id) mask, label = voc_utils.image_wise_to_instance_wise( label_img, inst_img) return img, mask, label
def _get_prob_map(self, i): prob_map = utils.read_image(self.prob_map_paths[i], dtype=np.uint8, color=False) prob_map = prob_map.astype(np.float32) / 255 # [0, 255] -> [0, 1] prob_map = prob_map[0] # (1, H, W) --> (H, W) return prob_map
def main(): chainer.config.train = False parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained_model', default='camvid') parser.add_argument('image') args = parser.parse_args() model = SegNetBasic( n_class=len(camvid_label_names), pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) labels = model.predict([img]) label = labels[0] fig = plot.figure() ax1 = fig.add_subplot(1, 2, 1) vis_image(img, ax=ax1) ax2 = fig.add_subplot(1, 2, 2) vis_semantic_segmentation( label, camvid_label_names, camvid_label_colors, ax=ax2) plot.show()
def visualize_dataset(dataset_dir): from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D # NOQA from chainercv.utils import read_image from pose.visualizations import vis_point, vis_edge dataset = GANeratedBaseDataset(dataset_dir, debug=False, mode="train") idx = np.random.choice(len(dataset)) print(idx, len(dataset)) example = dataset.get_example(idx) rgb_joint = example["rgb_joint"] rgb_path = example["rgb_path"] rgb = read_image(rgb_path) fig = plt.figure(figsize=(5, 10)) ax2 = fig.add_subplot(211) ax4 = fig.add_subplot(212, projection="3d") color = [COLOR_MAP[k] for k in KEYPOINT_NAMES] edge_color = [COLOR_MAP[s, t] for s, t in EDGES] rgb_vu = example["rgb_camera"].zyx2vu(rgb_joint) vis_point(rgb_vu, img=rgb, color=color, ax=ax2) vis_edge(rgb_vu, indices=EDGES, color=edge_color, ax=ax2) vis_point(rgb_joint, color=color, ax=ax4) vis_edge(rgb_joint, indices=EDGES, color=edge_color, ax=ax4) for ax in [ax4]: ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") ax.view_init(-65, -90) plt.show()
def __init__(self, datalist, DataDir, from_col, to_col, crop=(None, None), random=False, grey=False): self.dataset = [] ## an input/output image can consist of multiple images; they are stacked as channels with open(datalist) as input: for line in input: files = line.strip().split('\t') if (len(files) < len(set(from_col).union(set(to_col)))): print("Error in reading data file: ", files) exit() self.dataset.append( [[os.path.join(DataDir, files[i]) for i in from_col], [os.path.join(DataDir, files[i]) for i in to_col]]) for i in range(len(files)): if not os.path.isfile(os.path.join(DataDir, files[i])): print("{} not found!".format( os.path.join(DataDir, files[i]))) exit() if not crop[0] or crop[1]: img = read_image(self.dataset[i][0][0]) self.crop = (16 * ((img.shape[1] - random) // 16), 16 * ((img.shape[2] - random) // 16)) else: self.crop = crop self.grey = grey self.random = random print("Cropped size: ", self.crop) print("loaded {} images".format(len(self.dataset)))
def get_example(self, i): if i >= self.__len__(): raise IndexError img = read_image(self.filepaths[i][0]) xml = ET.parse(self.filepaths[i][1]) bboxes = [] labels = [] for anno in xml.findall('.object'): bbox = anno.find('bndbox') ymin = int(bbox.find('ymin').text) xmin = int(bbox.find('xmin').text) ymax = int(bbox.find('ymax').text) xmax = int(bbox.find('xmax').text) bboxes.append([ymin, xmin, ymax, xmax]) label_name = anno.find('name').text if label_name == 'no_building': labels.append(1) else: labels.append(0) # labels.append(self.label_names.index(anno.find('name').text)) bboxes = np.array(bboxes, dtype=np.float32) labels = np.array(labels, dtype=np.int32) return img, bboxes, labels
def get_example(self, i): if self.imgtype == "npy": img = np.load(self.get_img_path(i)) img = 2 * (np.clip(img, self.base, self.base + self.range) - self.base) / self.range - 1.0 if len(img.shape) == 2: img = img[np.newaxis, ] else: img = self.img2var( read_image(self.get_img_path(i), color=self.color)) # img = resize(img, (self.resize_to, self.resize_to)) if self.crop: H, W = self.crop else: H, W = (16 * ((img.shape[1] - 2 * self.random) // 16), 16 * ((img.shape[2] - 2 * self.random) // 16)) if img.shape[1] < H + 2 * self.random or img.shape[ 2] < W + 2 * self.random: p = max(H + 2 * self.random - img.shape[1], W + 2 * self.random - img.shape[2]) img = np.pad(img, ((0, 0), (p, p), (p, p)), 'edge') img = random_crop( center_crop(img, (H + 2 * self.random, W + 2 * self.random)), (H, W)) if self.random: img = random_flip(img, x_random=True) return img.astype(self.dtype)
def runYOLO(): '''parser = argparse.ArgumentParser() parser.add_argument( '--model', choices=('yolo_v2', 'yolo_v3'), default='yolo_v2') parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model', default='voc0712') parser.add_argument('image') args = parser.parse_args()''' aimage = 'sample7.jpg' amodel = 'yolo_v2' apretrained_model = 'voc0712' agpu = -1 if amodel == 'yolo_v2': model = YOLOv2(n_fg_class=len(voc_bbox_label_names), pretrained_model=apretrained_model) elif amodel == 'yolo_v3': model = YOLOv3(n_fg_class=len(voc_bbox_label_names), pretrained_model=apretrained_model) if agpu >= 0: chainer.cuda.get_device_from_id(agpu).use() model.to_gpu() img = utils.read_image(aimage, color=True) bboxes, labels, scores = model.predict([img]) bbox, label, score = bboxes[0], labels[0], scores[0] vis_bbox(img, bbox, label, score, label_names=voc_bbox_label_names)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--base-network', choices=('vgg16', 'resnet101'), default='vgg16', help='Base network') parser.add_argument('--pretrained-model', required=True) parser.add_argument('image') args = parser.parse_args() if args.base_network == 'vgg16': model = chainercv.links.SSD300( n_fg_class=len(roaddamage_label_names), pretrained_model=args.pretrained_model) elif args.base_network == 'resnet101': model = ssd_resnet101.SSD224( n_fg_class=len(roaddamage_label_names), pretrained_model=args.pretrained_model) else: raise ValueError('Invalid base network') if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) bboxes, labels, scores = model.predict([img]) bbox, label, score = bboxes[0], labels[0], scores[0] vis_bbox( img, bbox, label, score, label_names=roaddamage_label_names) plt.axis('off') plt.show()
def visualize_dataset(dataset_dir): from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D # NOQA from chainercv.utils import read_image from .visualizations import vis_point, vis_edge dataset = FHBaseDataset(dataset_dir, debug=True, mode="train") idx = np.random.choice(len(dataset)) example = dataset.get_example(idx) camera_joint = example["rgb_joint"] rgb_path = example["rgb_path"] img = read_image(rgb_path) fig = plt.figure(figsize=(8, 8)) ax1 = fig.add_subplot(221) ax3 = fig.add_subplot(223, projection="3d") rgb_vu = example["rgb_camera"].zyx2vu(camera_joint) color = [COLOR_MAP[k] for k in STANDARD_KEYPOINT_NAMES] edge_color = [COLOR_MAP[s, t] for s, t in EDGES] vis_point(rgb_vu, img=img, color=color, ax=ax1) vis_edge(rgb_vu, indices=EDGES, color=edge_color, ax=ax1) vis_point(camera_joint, color=color, ax=ax3) vis_edge(camera_joint, indices=EDGES, color=edge_color, ax=ax3) for ax in [ax3]: ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") ax.view_init(-65, -90) plt.show()
def annotate(args, path_in, path_out): chainer.config.train = False with open("model/label_names_coco_container.yml", 'r') as f: label_names = tuple(yaml.load(f)) model = SSD300( n_fg_class=len(label_names), pretrained_model=args.model) # Change the threshold for showing labels # model.score_thresh=0.4 # model.nms_thres = args.threshold count = 1 numfiles = len([f for f in os.listdir(path_in) if ".jpg" in f]) for file in [f for f in os.listdir(path_in) if ".jpg" in f]: img = utils.read_image(path_in+file, color=True) bboxes, labels, scores = model.predict([img]) bbox, label, score = bboxes[0], labels[0], scores[0] nb_containers = sum([1 for l in label if label_names[int(l)] == 'container']) a = vis_bbox( img, bbox, label, score, label_names=label_names) a.annotate("Number of containers = {}".format(nb_containers), xytext=(0, 0), xy=(0, 0)) plot.axis('off') plot.savefig(path_out + "frame{}.png".format(str(count).zfill(5)), bbox_inches='tight') plot.close() if count % 10 == 0: print("Progress: %0.2f%%" % (count / numfiles * 100,), flush=True) count += 1
def get_example(self, i): img_filename = self.img_filenames[i] anno_filename = self.anno_filenames[i] img = read_image(img_filename) with open(anno_filename, 'r') as f: anno = json.load(f) anno = anno['labels'] bbox = [] label = [] for anno_i in anno: h = anno_i['size']['y'] w = anno_i['size']['x'] center_y = anno_i['centre']['y'] center_x = anno_i['centre']['x'] if anno_i['label_class'] not in self.label_names: raise ValueError('The class does not exist {}'.format( anno_i['label_class'])) l = self.label_names.index(anno_i['label_class']) bbox.append([ center_y - h / 2, center_x - w / 2, center_y + h / 2, center_x + w / 2 ]) label.append(l) bbox = np.array(bbox, dtype=np.float32) label = np.array(label, dtype=np.int32) return img, bbox, label
def stack_imgs(fns, crop, resize=False, grey=False): imgs_in = [] for fn in fns: fn1, ext = os.path.splitext(fn) # image can be given as csv or jpg/png... etc if ext == ".csv": img_in = np.loadtxt(fn, delimiter=",")[np.newaxis, ] elif ext == ".npy": img_in = (np.load(fn)[np.newaxis, ]).astype(np.float32) img_in = (np.sqrt(np.clip(img_in, 0, 100))) / 10.0 ## nasty preprocess # img_in = (img_in - np.mean(img_in))/2*np.std(img_in) # standardize else: img_in = read_image(fn, color=not grey) / 127.5 - 1.0 # resize if the image is too small if resize: if img_in.shape[1] < crop[0] or img_in.shape[2] < crop[1]: if crop[0] / img_in.shape[1] < crop[1] / img_in.shape[2]: img_in = resize(img_in, (int( crop[1] / img_in.shape[2] * img_in.shape[1]), crop[1])) else: img_in = resize( img_in, (crop[0], int(crop[0] / img_in.shape[1] * img_in.shape[2]))) imgs_in.append(img_in) # an input/output image can consist of multiple images; they are stacked as channels # print(imgs_in.shape) return (np.concatenate(imgs_in, axis=0))
def export_onnx(input_image_path, output_path, gpu, only_output=True): """Export YOLOv2 Tiny model to ONNX graph 'model.onnx' file will be exported under ``output_path``. """ model = YOLOv2Tiny(pretrained_model='voc0712') input_image = read_image(input_image_path) input_image = input_image[None, :] if gpu >= 0: model.to_gpu() input_image = chainer.cuda.to_gpu(input_image) if only_output: os.makedirs(output_path, exist_ok=True) name = os.path.join(output_path, 'model.onnx') export( model, input_image, filename=name, output_names=('locs', 'objs', 'confs')) else: # an input and output given by Chainer will be also emitted # for using as test dataset export_testcase( model, input_image, output_path, output_names=('locs', 'objs', 'confs'))
def main(): parser = argparse.ArgumentParser() parser.add_argument('--model', choices=('ssd300', 'ssd512'), default='ssd300') parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model', default='voc0712') parser.add_argument('image') args = parser.parse_args() if args.model == 'ssd300': model = SSD300(n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) elif args.model == 'ssd512': model = SSD512(n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) bboxes, labels, scores = model.predict([img]) bbox, label, score = bboxes[0], labels[0], scores[0] vis_bbox(img, bbox, label, score, label_names=voc_bbox_label_names) plt.show()
def get_example(self, i): """Returns the i-th example. Args: i (int): The index of the example. Returns: Returns a tuple consited of a color image and a label whose shapes are (3, H, W) and (H, W), respectively. H and W are height and width of the image. The dtype of the color image is :obj:`numpy.float32` and the dtype of the label image is :obj:`numpy.int32`. """ img = read_image(self.img_paths[i]) label = read_image(self.label_paths[i], dtype=np.int32, color=False)[0] return img, label
def get_example(self, i): """Returns the i-th example. Args: i (int): The index of the example. Returns: Returns a color image whose shape is (3, H, W). H and W are height and width of the image. The dtype of the image is :obj:`numpy.float32`. """ return read_image(self.img_paths[i])
def get_example(self, i): """Returns the i-th example. Args: i (int): The index of the example. Returns: tuple of an image, keypoints and a keypoint mask. The image is in CHW format and its color channel is ordered in RGB. If :obj:`return_bb = True`, a bounding box is appended to the returned value. If :obj:`return_mask = True`, a probability map is appended to the returned value. """ img = utils.read_image( os.path.join(self.data_dir, 'images', self.paths[i]), color=True) keypoint = np.array(self.kp_dict[i], dtype=np.float32) kp_mask = np.array(self.kp_mask_dict[i], dtype=np.bool) if not self.return_prob_map: if self.return_bb: return img, keypoint, kp_mask, self.bbs[i] else: return img, keypoint, kp_mask prob_map = utils.read_image(self.prob_map_paths[i], dtype=np.uint8, color=False) prob_map = prob_map.astype(np.float32) / 255 # [0, 255] -> [0, 1] prob_map = prob_map[0] # (1, H, W) --> (H, W) if self.return_bb: return img, keypoint, kp_mask, self.bbs[i], prob_map else: return img, keypoint, kp_mask, prob_map
def get_example(self, i): """Returns the i-th example. Returns a color image and a label image. The color image is in CHW format and the label image is in HW format. Args: i (int): The index of the example. Returns: tuple of a color image and a label whose shapes are (3, H, W) and (H, W) respectively. H and W are height and width of the image. The dtype of the color image is :obj:`numpy.float32` and the dtype of the label image is :obj:`numpy.int32`. """ if i >= len(self): raise IndexError('index is too large') img_path, label_path = self.paths[i] img = read_image(img_path, color=True) label = read_image(label_path, dtype=np.int32, color=False)[0] # Label id 11 is for unlabeled pixels. label[label == 11] = -1 return img, label
def get_example(self, i): """Returns the i-th example. Returns a color image, class_id and super_class_id. The image is in CHW format. The returned image is RGB. Args: i (int): The index of the example. Returns: i-th example """ class_id = np.array(self.class_ids[i], np.int32) super_class_id = np.array(self.super_class_ids[i], np.int32) img = utils.read_image(self.paths[i], color=True) return img, class_id, super_class_id
def get_example(self, i): """Returns the i-th example. Returns a color image and bounding boxes. The image is in CHW format. The returned image is RGB. Args: i (int): The index of the example. Returns: tuple of an image and bounding boxes """ id_ = self.ids[i] anno = ET.parse( os.path.join(self.data_dir, 'Annotations', id_ + '.xml')) bbox = [] label = [] difficult = [] for obj in anno.findall('object'): # when in not using difficult split, and the object is # difficult, skipt it. if not self.use_difficult and int(obj.find('difficult').text) == 1: continue difficult.append(int(obj.find('difficult').text)) bndbox_anno = obj.find('bndbox') # subtract 1 to make pixel indexes 0-based bbox.append([ int(bndbox_anno.find(tag).text) - 1 for tag in ('ymin', 'xmin', 'ymax', 'xmax')]) name = obj.find('name').text.lower().strip() label.append(voc_utils.voc_bbox_label_names.index(name)) bbox = np.stack(bbox).astype(np.float32) label = np.stack(label).astype(np.int32) # When `use_difficult==False`, all elements in `difficult` are False. difficult = np.array(difficult, dtype=np.bool) # Load a image img_file = os.path.join(self.data_dir, 'JPEGImages', id_ + '.jpg') img = read_image(img_file, color=True) if self.return_difficult: return img, bbox, label, difficult return img, bbox, label
def main(): parser = argparse.ArgumentParser() parser.add_argument( '--model', choices=('yolo_v2', 'yolo_v2_tiny', 'yolo_v3'), default='yolo_v2') parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model', default='voc0712') parser.add_argument('--export', action='store_true') parser.add_argument('image') args = parser.parse_args() if args.model == 'yolo_v2': model = YOLOv2( n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) elif args.model == 'yolo_v2_tiny': model = YOLOv2Tiny( n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) elif args.model == 'yolo_v3': model = YOLOv3( n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) if args.export: import onnx_chainer x = model.xp.stack([img]) onnx_chainer.export_testcase(model, x, args.model) return bboxes, labels, scores = model.predict([img]) bbox, label, score = bboxes[0], labels[0], scores[0] vis_bbox( img, bbox, label, score, label_names=voc_bbox_label_names) plt.show()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--pretrained-model') parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('image') args = parser.parse_args() img = read_image(args.image) model = SSPYOLOv2() chainer.serializers.load_npz(args.pretrained_model, model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() points, labels, scores = model.predict([img]) point = points[0] label = labels[0] score = scores[0] vis_point(img, point[:1]) plt.show()
def get_example(self, i): """Returns the i-th example. Returns a color image and a label image. The color image is in CHW format and the label image is in HW format. Args: i (int): The index of the example. Returns: tuple of color image and label whose shapes are (3, H, W) and (H, W) respectively. H and W are height and width of the images. The dtype of the color image is :obj:`numpy.float32` and the dtype of the label image is :obj:`numpy.int32`. """ if i >= len(self): raise IndexError('index is too large') img_file = os.path.join( self.data_dir, 'JPEGImages', self.ids[i] + '.jpg') img = read_image(img_file, color=True) label = self._load_label(self.data_dir, self.ids[i]) return img, label
def _get_image(self, i): img_path = os.path.join( self.data_dir, 'JPEGImages', self.ids[i] + '.jpg') img = read_image(img_path, color=True) return img
def test_read_image_mutable(self): img = read_image(self.path) img[:] = 0 np.testing.assert_equal(img, 0)
def _get_msk(self, i): img_path = os.path.join(self.base_dir, self.img_paths[i].rstrip()) mskpath = img_path.replace('JPEGImages', 'mask').replace( '/00', '/').replace('.jpg', '.png') msk = read_image(mskpath, color=False)[0] return msk > 0
def _get_image(self, i): data_id = self.ids[i] img_file = os.path.join( self.data_dir, 'img', data_id + '.jpg') return read_image(img_file, color=True)
def get_example(self, i): img = read_image(self.img_paths[i], color=self.color) label = self.labels[i] return img, label