From c49642a8cf2c201246259b28aaee64810bae3189 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Fri, 20 Apr 2018 16:13:22 +0900 Subject: [PATCH 01/33] add kitti datasets(not test) --- chainercv/datasets/__init__.py | 2 + chainercv/datasets/kitti/KITTI_dataset.py | 509 ++++++++++++++++++ chainercv/datasets/kitti/__init__.py | 0 chainercv/datasets/kitti/parseTrackletXML.py | 277 ++++++++++ .../datasets_tests/kitti_tests/test_KITTI.py | 75 +++ 5 files changed, 863 insertions(+) create mode 100644 chainercv/datasets/kitti/KITTI_dataset.py create mode 100644 chainercv/datasets/kitti/__init__.py create mode 100644 chainercv/datasets/kitti/parseTrackletXML.py create mode 100644 tests/datasets_tests/kitti_tests/test_KITTI.py diff --git a/chainercv/datasets/__init__.py b/chainercv/datasets/__init__.py index ad844d3143..2e75d7cac2 100644 --- a/chainercv/datasets/__init__.py +++ b/chainercv/datasets/__init__.py @@ -13,6 +13,8 @@ from chainercv.datasets.cub.cub_label_dataset import CUBLabelDataset # NOQA from chainercv.datasets.cub.cub_point_dataset import CUBPointDataset # NOQA from chainercv.datasets.cub.cub_utils import cub_label_names # NOQA +from chainercv.datasets.kitti.KITTI_dataset import KITTIDataset # NOQA +from chainercv.datasets.kitti.parseTrackletXML import parseXML # NOQA from chainercv.datasets.directory_parsing_label_dataset import directory_parsing_label_names # NOQA from chainercv.datasets.directory_parsing_label_dataset import DirectoryParsingLabelDataset # NOQA from chainercv.datasets.mixup_soft_label_dataset import MixUpSoftLabelDataset # NOQA diff --git a/chainercv/datasets/kitti/KITTI_dataset.py b/chainercv/datasets/kitti/KITTI_dataset.py new file mode 100644 index 0000000000..d3113ce311 --- /dev/null +++ b/chainercv/datasets/kitti/KITTI_dataset.py @@ -0,0 +1,509 @@ +import glob +import os +from urllib.parse import urljoin + +import numpy as np + +import chainer +from chainer.dataset import download +from chainercv import utils +from chainercv.utils.image import read_image + +# root = 'pfnet/chainercv/KITTI' +url_base = 'http://kitti.is.tue.mpg.de/kitti/raw_data/' + +# use pykitti +import pykitti +import itertools +# tracklet parser +# import parseTrackletXML as xmlParser +from chainercv.datasets.KITTI import parseTrackletXML as xmlParser +# from parseTrackletXML import parseXML + +import matplotlib.pyplot as plt +from chainercv.visualizations import vis_bbox + +# image_shape = 375, 1242 + +KITTI_category_names = ( + 'City', + 'Residential', + 'Road', + 'Campus', + 'Person', + 'Calibration' +) + +KITTI_label_names = ( + 'Car', + 'Van', + 'Truck', + 'Pedestrian', + 'Sitter', + 'Cyclist', + 'Tram', + 'Misc', +) + +KITTI_label_colors = ( + (128, 128, 128), + (128, 0, 0), + (192, 192, 128), + (128, 64, 128), + (60, 40, 222), + (128, 128, 0), + (192, 128, 128), + (64, 64, 128), +) +KITTI_ignore_label_color = (0, 0, 0) + + +class KITTIDataset(chainer.dataset.DatasetMixin): + + """Image dataset for test split of `KITTI dataset`_. + + .. _`KITTI dataset`: http://www.cvlibs.net/datasets/kitti/raw_data.php + + .. note:: + + Args: + data_dir (string): Path to the dataset directory. The directory should + contain the :obj:`---` directory. If :obj:`auto` is given, + it uses :obj:`$CHAINER_DATSET_ROOT/pfnet/chainercv/KITTI` by + default. + + """ + + def __init__(self, data_dir='auto', date='', driveNo='', color=True, sync=True, isLeft=True): + self.color = color + self.sync = sync + self.isLeft = isLeft + if data_dir == 'auto': + if sync == True: + # download sync data + # data_dir = self.get_KITTI_Sync_Data('pfnet/chainercv/KITTI', date, driveNo) + data_dir = self.get_KITTI_Sync_Data(os.path.join('pfnet', 'chainercv', 'KITTI'), date, driveNo) + else: + # download nosync data + # data_dir = self.get_KITTI_NoSync_Data('pfnet/chainercv/KITTI', date, driveNo) + data_dir = self.get_KITTI_NoSync_Data(os.path.join('pfnet', 'chainercv', 'KITTI'), date, driveNo) + + # no use pykitti + # if self.color == True: + # if self.isLeft == True: + # imgNo = '02' + # else: + # imgNo = '03' + # else: + # if self.isLeft == True: + # imgNo = '00' + # else: + # imgNo = '01' + # image + # self.get_KITTI_Image(data_dir, date, driveNo, imgNo) + # img = read_image(self.img_paths[0]) + ## + # use pykitti + # read All images + # imformat='None' + # self.dataset = pykitti.raw(data_dir, date, driveNo, frames=None) + self.dataset = pykitti.raw(data_dir, date, driveNo, frames=None, imformat='cv2') + + # current camera calibration R/P settings. + if self.color == True: + if self.isLeft == True: + # img02 + self.cur_R_rect = self.dataset.calib.R_rect_20 + self.cur_P_rect = self.dataset.calib.P_rect_20 + # set imformat='cv2' + self.imgs = np.array(list(self.dataset.cam2)) + # img = np.array(list(self.dataset.rgb)[0]) + # img = np.uint8(np.array(list(self.dataset.rgb)[0]) * 255).astype(np.float32) + else: + # img03 + self.cur_R_rect = self.dataset.calib.R_rect_30 + self.cur_P_rect = self.dataset.calib.P_rect_30 + self.imgs = np.array(list(self.dataset.cam3)) + # img = np.array(list(self.dataset.rgb)[1]) + # imformat='None' + # img = np.uint8(np.array(list(self.dataset.rgb)[1]) * 255).astype(np.float32) + else: + if self.isLeft == True: + # img00 + self.cur_R_rect = self.dataset.calib.R_rect_00 + self.cur_P_rect = self.dataset.calib.P_rect_00 + self.imgs = np.array(list(self.dataset.cam0)) + # img = np.array(list(self.dataset.gray)[0]) + else: + # img00 + self.cur_R_rect = self.dataset.calib.R_rect_10 + self.cur_P_rect = self.dataset.calib.P_rect_10 + self.imgs = np.array(list(self.dataset.cam1)) + # img = np.array(list(self.dataset.gray)[1]) + + # get object info(type/area/...) + self.tracklets = self.get_KITTI_Tracklets(data_dir, date, driveNo) + + # set arrays + # length = len(self.dataset.cam0) + length = self.__len__() + self.bboxes = [0] * length + self.labels = [0] * length + for idx in range(0, length): + self.bboxes[idx] = list() + self.labels[idx] = list() + + self.get_KITTI_Label(self.tracklets) + + + def __getitem__(self, index): + # Note : before setting datas. + # no use pykitti + # return read_image(self.img_paths[index]) + # use pykitti + img = self.imgs[index] + bbox = self.bboxes[index] + label = self.labels[index] + + # convert data is utils.read_image function return values + if img.ndim == 2: + # reshape (H, W) -> (1, H, W) + # return img[np.newaxis] + return img[np.newaxis], bbox, label + else: + # transpose (H, W, C) -> (C, H, W) + return img.transpose((2, 0, 1)), bbox, label + + + def __len__(self): + # no use pykitti + # return len(self.img_paths) + # use pykitti + # print(self.imgs) + return len(self.imgs) + + +# # img +# def get_example(self, i): +# """Returns the i-th test image. +# +# Returns a color image. The color image is in CHW format. +# +# Args: +# i (int): The index of the example. +# +# Returns: +# A color image whose shape is (3, H, W). H and W are height and +# width of the image. +# The dtype of the color image is :obj:`numpy.float32`. +# +# """ +# # return read_image(self.img_paths[i]) +### + +# 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 +# +### + + # point + def get_example(self, i): + """Returns the i-th test point. + + Returns a color point. The color point is in XYZRGB format. + + Args: + i (int): The index of the example. + + Returns: + A color point whose shape is (6, x, y, z). H and W are height and + width of the point. + The dtype of the color point is :obj:`numpy.float32`. + + """ + # use pykitti + # return self.dataset.gray[i] + # return next(iter(itertools.islice(self.dataset.velo, 0, None))) + label = self.labels[i] + # return next(iter(itertools.islice(self.dataset.velo, i, None))) + return next(iter(itertools.islice(self.dataset.velo, i, None))), bbox_3d, label + + + def get_KITTI_Sync_Data(self, root, date, driveNo): + data_root = download.get_dataset_directory(root) + # print('dst path : ' + data_root) + + # data + folder = date + '_drive_' + driveNo + # ok + # url_data = url_base + '/' + folder+ '/' + folder + '_sync.zip' + url_data = urljoin(url_base, folder+ '/' + folder + '_sync.zip') + # print('url : ' + url_data) + + # calibration + url_calib = url_base + date + '_calib.zip' + + # tracklet + url_tracklet = urljoin(url_base, folder+ '/' + folder + '_tracklets.zip') + + download_file_path = utils.cached_download(url_data) + ext = os.path.splitext(url_data)[1] + utils.extractall(download_file_path, data_root, ext) + + download_file_path = utils.cached_download(url_calib) + ext = os.path.splitext(url_calib)[1] + utils.extractall(download_file_path, data_root, ext) + + download_file_path = utils.cached_download(url_tracklet) + ext = os.path.splitext(url_tracklet)[1] + utils.extractall(download_file_path, data_root, ext) + + return data_root + + + def get_KITTI_NoSync_Data(self, root, date, driveNo): + data_root = download.get_dataset_directory(root) + + # data + folder = date + '_drive_' + driveNo + url_data = urljoin(url_base, folder+ '/' + folder + '_extract.zip') + + # calibration + url_calib = url_base + date + '_calib.zip' + + # tracklet + url_tracklet = urljoin(url_base, folder+ '/' + folder + '_tracklets.zip') + + download_file_path = utils.cached_download(url_data) + ext = os.path.splitext(url_data)[1] + utils.extractall(download_file_path, data_root, ext) + + download_file_path = utils.cached_download(url_calib) + ext = os.path.splitext(url_calib)[1] + utils.extractall(download_file_path, data_root, ext) + + download_file_path = utils.cached_download(url_tracklet) + ext = os.path.splitext(url_tracklet)[1] + utils.extractall(download_file_path, data_root, ext) + + return data_root + + + def get_KITTI_Image(self, data_root, date, driveNo, ImgNo): + folder = date + '_drive_' + driveNo + img_dir = os.path.join(data_root, os.path.join(date, folder + '_sync', 'image_' + ImgNo, 'data')) + + self.img_paths = list() + if not os.path.exists(img_dir): + raise ValueError( + 'KITTI dataset does not exist at the expected location.' + 'Please download it from http://www.cvlibs.net/datasets/kitti/raw_data.php.' + 'Then place directory image at {}.'.format(img_dir)) + + for img_path in sorted(glob.glob(os.path.join(img_dir, '*.png'))): + self.img_paths.append(img_path) + + + + + def get_KITTI_Tracklets(self, data_root, date, driveNo): + # read calibration files + kitti_dir = os.path.join(data_root, date) + # kitti_dir = kitti_dir.replace(os.path.sep, '/') + # calibration_dir = os.path.join(data_root, date) + # self.imu2velo = read_calib_file(os.path.join(kitti_dir, "calib_imu_to_velo.txt")) + # self.velo2cam = read_calib_file(os.path.join(kitti_dir, "calib_velo_to_cam.txt")) + # self.cam2cam = read_calib_file(os.path.join(kitti_dir, "calib_cam_to_cam.txt")) + # read tracklet + folder = date + '_drive_' + driveNo + '_sync' + # self.tracklet = read_tracklet_file(os.path.join(kitti_dir, folder, "calib_imu_to_velo.txt")) + # return tracklets + # get dir names + # read tracklets from file + myTrackletFile = os.path.join(kitti_dir, folder, 'tracklet_labels.xml') + tracklets = xmlParser.parseXML(myTrackletFile) + return tracklets + + + def get_KITTI_Label(self, tracklets): + twoPi = 2.*np.pi + # loop over tracklets + for iTracklet, tracklet in enumerate(tracklets): + # print('tracklet {0: 3d}: {1}'.format(iTracklet, tracklet)) + + # this part is inspired by kitti object development kit matlab code: computeBox3D + h,w,l = tracklet.size + trackletBox = np.array([ # in velodyne coordinates around zero point and without orientation yet\ + [-l/2, -l/2, l/2, l/2, -l/2, -l/2, l/2, l/2], \ + [ w/2, -w/2, -w/2, w/2, w/2, -w/2, -w/2, w/2], \ + [ 0.0, 0.0, 0.0, 0.0, h, h, h, h]]) + + # print('trackletBox : ' + trackletBox) + # print(trackletBox) + objTypeStr = tracklet.objectType + # print(objTypeStr) + + # loop over all data in tracklet + for translation, rotation, state, occlusion, truncation, amtOcclusion, amtBorders, absoluteFrameNumber \ + in tracklet: + + # determine if object is in the image; otherwise continue + if truncation not in (xmlParser.TRUNC_IN_IMAGE, xmlParser.TRUNC_TRUNCATED): + continue + + # re-create 3D bounding box in velodyne coordinate system + yaw = rotation[2] # other rotations are 0 in all xml files I checked + assert np.abs(rotation[:2]).sum() == 0, 'object rotations other than yaw given!' + rotMat = np.array([\ + [np.cos(yaw), -np.sin(yaw), 0.0], \ + [np.sin(yaw), np.cos(yaw), 0.0], \ + [ 0.0, 0.0, 1.0]]) + cornerPosInVelo = np.dot(rotMat, trackletBox) + np.tile(translation, (8,1)).T + # print(cornerPosInVelo) + # print(cornerPosInVelo[:,0]) + # print(cornerPosInVelo.shape) 3*8 + # print('tracklet for : ') + # print(iTracklet) + # print(translation) + # print(rotation) + # print(state) + # print(occlusion) + # print(truncation) + # print(amtOcclusion) + # print(amtBorders) + + # calc yaw as seen from the camera (i.e. 0 degree = facing away from cam), as opposed to + # car-centered yaw (i.e. 0 degree = same orientation as car). + # makes quite a difference for objects in periphery! + # Result is in [0, 2pi] + x, y, z = translation + # print(translation) + yawVisual = ( yaw - np.arctan2(y, x) ) % twoPi + # print(yaw) + # print(yawVisual) + # param = pykitti.utils.transform_from_rot_trans(rotMat, translation) + # print(param) + + # projection to image? + # print(self.dataset.calib.P_rect_20) + # param3 = translation.reshape(3, 1) * self.dataset.calib.P_rect_20 + # print(cornerPosInVelo[:, 0:1].shape) + pt3d = np.vstack((cornerPosInVelo[:,0:8], np.ones(8))) + # print(pt3d) + # print(pt3d.shape) + # print(self.dataset.calib.P_rect_20) + + # pt2d = self.project_velo_points_in_img(pt3d, self.dataset.calib.T_cam2_velo, self.dataset.calib.R_rect_20, self.dataset.calib.P_rect_20) + pt2d = self.project_velo_points_in_img(pt3d, self.dataset.calib.T_cam2_velo, self.cur_R_rect, self.cur_P_rect) + + # print(pt2d) + ymin = min(pt2d[1, :]) + xmin = min(pt2d[0, :]) + ymax = max(pt2d[1, :]) + xmax = max(pt2d[0, :]) + param = np.array((ymin, xmin, ymax, xmax)) + # bbox.append(param) + # bbox = np.stack(bbox).astype(np.float32) + # self.bboxes[absoluteFrameNumber] = bbox + self.bboxes[absoluteFrameNumber].append(param) + # print(self.bboxes[absoluteFrameNumber]) + # param_3d = cornerPosInVelo + self.bboxes_3d[absoluteFrameNumber].append(cornerPosInVelo) + # label.append(param2) + # label = np.stack(label).astype(np.int32) + # self.labels[absoluteFrameNumber] = label + # objectType + # label_names + # not search objTypeStr? process + param2 = KITTI_label_names.index(objTypeStr) + self.labels[absoluteFrameNumber].append(param2) + # print(self.bboxes[absoluteFrameNumber]) + + #end: for all frames in track + #end: for all tracks + + # def get_KITTI_Calibration(self, data_root, date, driveNo): + + # def prepare_velo_points(pts3d_raw): + # '''Replaces the reflectance value by 1, and tranposes the array, so + # points can be directly multiplied by the camera projection matrix''' + # + # pts3d = pts3d_raw + # # Reflectance > 0 + # pts3d = pts3d[pts3d[:, 3] > 0 ,:] + # pts3d[:,3] = 1 + # return pts3d.transpose() + + + def project_velo_points_in_img(self, pts3d, T_cam_velo, Rrect, Prect): + """Project 3D points into 2D image. Expects pts3d as a 4xN + numpy array. Returns the 2D projection of the points that + are in front of the camera only an the corresponding 3D points.""" + + # 3D points in camera reference frame. + pts3d_cam = Rrect.dot(T_cam_velo.dot(pts3d)) + + # Before projecting, keep only points with z>0 + # (points that are in fronto of the camera). + idx = (pts3d_cam[2,:]>=0) + pts2d_cam = Prect.dot(pts3d_cam[:,idx]) + + # return pts3d[:, idx], pts2d_cam/pts2d_cam[2,:] + return pts2d_cam/pts2d_cam[2,:] + + +if __name__ == '__main__': + # 00, 01 : gray + # d = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = True) + # print(len(d)) + # img = d[0] + # print(img) + # print(img.shape) + # d = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = True, isLeft=False) + + # print(len(d)) + # img = d[0] + # print(img) + # print(img.shape) + + # 02, 03 : color + # d = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True) + # d = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True, isLeft=False) + # local Folder + # d = KITTIDataset(date='2011_09_26', driveNo='0005', color=True, sync = True, isLeft=False) + d = KITTIDataset(date='2011_09_26', driveNo='0020', color=True, sync = True) + # use pykitti + # d = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True) + print(len(d)) + img, bbox, label = d[20] + # img, label = d[0] + # (3, 375, 1242) + # print(img) + # print(img.shape) + # Data no Sync + # d = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = False) + # print(img.debug_print()) + vis_bbox(img, bbox, label, score=None, label_names=KITTI_label_names) + plt.show() + diff --git a/chainercv/datasets/kitti/__init__.py b/chainercv/datasets/kitti/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/chainercv/datasets/kitti/parseTrackletXML.py b/chainercv/datasets/kitti/parseTrackletXML.py new file mode 100644 index 0000000000..cbfafdb4bc --- /dev/null +++ b/chainercv/datasets/kitti/parseTrackletXML.py @@ -0,0 +1,277 @@ +#!/usr/bin/env python +""" +parse XML files containing tracklet info for kitti data base (raw data section) +(http://cvlibs.net/datasets/kitti/raw_data.php) + +No guarantees that this code is correct, usage is at your own risk! + +created by Christian Herdtweck, Max Planck Institute for Biological Cybernetics + (christian.herdtweck@tuebingen.mpg.de) + +requires numpy! + +example usage: + import parseTrackletXML as xmlParser + kittiDir = '/path/to/kitti/data' + drive = '2011_09_26_drive_0001' + xmlParser.example(kittiDir, drive) +or simply on command line: + python parseTrackletXML.py +""" + +# Version History: +# 4/7/12 Christian Herdtweck: seems to work with a few random test xml tracklet files; +# converts file contents to ElementTree and then to list of Tracklet objects; +# Tracklet objects have str and iter functions +# 5/7/12 ch: added constants for state, occlusion, truncation and added consistency checks +# 30/1/14 ch: create example function from example code + +import sys + +from sys import argv as cmdLineArgs +from xml.etree.ElementTree import ElementTree +import numpy as np +import itertools +from warnings import warn + +STATE_UNSET = 0 +STATE_INTERP = 1 +STATE_LABELED = 2 +stateFromText = {'0':STATE_UNSET, '1':STATE_INTERP, '2':STATE_LABELED} + +OCC_UNSET = 255 # -1 as uint8 +OCC_VISIBLE = 0 +OCC_PARTLY = 1 +OCC_FULLY = 2 +occFromText = {'-1':OCC_UNSET, '0':OCC_VISIBLE, '1':OCC_PARTLY, '2':OCC_FULLY} + +TRUNC_UNSET = 255 # -1 as uint8, but in xml files the value '99' is used! +TRUNC_IN_IMAGE = 0 +TRUNC_TRUNCATED = 1 +TRUNC_OUT_IMAGE = 2 +TRUNC_BEHIND_IMAGE = 3 +truncFromText = {'99':TRUNC_UNSET, '0':TRUNC_IN_IMAGE, '1':TRUNC_TRUNCATED, \ + '2':TRUNC_OUT_IMAGE, '3': TRUNC_BEHIND_IMAGE} + + +class Tracklet(object): + r""" representation an annotated object track + + Tracklets are created in function parseXML and can most conveniently used as follows: + + for trackletObj in parseXML(trackletFile): + for translation, rotation, state, occlusion, truncation, amtOcclusion, amtBorders, absoluteFrameNumber in trackletObj: + ... your code here ... + #end: for all frames + #end: for all tracklets + + absoluteFrameNumber is in range [firstFrame, firstFrame+nFrames[ + amtOcclusion and amtBorders could be None + + You can of course also directly access the fields objType (string), size (len-3 ndarray), firstFrame/nFrames (int), + trans/rots (nFrames x 3 float ndarrays), states/truncs (len-nFrames uint8 ndarrays), occs (nFrames x 2 uint8 ndarray), + and for some tracklets amtOccs (nFrames x 2 float ndarray) and amtBorders (nFrames x 3 float ndarray). The last two + can be None if the xml file did not include these fields in poses + """ + + objectType = None + size = None # len-3 float array: (height, width, length) + firstFrame = None + trans = None # n x 3 float array (x,y,z) + rots = None # n x 3 float array (x,y,z) + states = None # len-n uint8 array of states + occs = None # n x 2 uint8 array (occlusion, occlusion_kf) + truncs = None # len-n uint8 array of truncation + amtOccs = None # None or (n x 2) float array (amt_occlusion, amt_occlusion_kf) + amtBorders = None # None (n x 3) float array (amt_border_l / _r / _kf) + nFrames = None + + def __init__(self): + r""" create Tracklet with no info set """ + self.size = np.nan*np.ones(3, dtype=float) + + def __str__(self): + r""" return human-readable string representation of tracklet object + + called implicitly in + print(trackletObj) + or in + text = str(trackletObj) + """ + return '[Tracklet over {0} frames for {1}]'.format(self.nFrames, self.objectType) + + def __iter__(self): + r""" returns an iterator that yields tuple of all the available data for each frame + + called whenever code iterates over a tracklet object, e.g. in + for translation, rotation, state, occlusion, truncation, amtOcclusion, amtBorders, absoluteFrameNumber in trackletObj: + ...do something ... + or + trackDataIter = iter(trackletObj) + """ + if self.amtOccs is None: + # return itertools.izip(self.trans, self.rots, self.states, self.occs, self.truncs, \ + # itertools.repeat(None), itertools.repeat(None), xrange(self.firstFrame, self.firstFrame+self.nFrames)) + # Python3 + return zip(self.trans, self.rots, self.states, self.occs, self.truncs, \ + repeat(None), repeat(None), range(self.firstFrame, self.firstFrame+self.nFrames)) + else: + # return itertools.izip(self.trans, self.rots, self.states, self.occs, self.truncs, \ + # self.amtOccs, self.amtBorders, xrange(self.firstFrame, self.firstFrame+self.nFrames)) + # Python3 + return zip(self.trans, self.rots, self.states, self.occs, self.truncs, \ + self.amtOccs, self.amtBorders, range(self.firstFrame, self.firstFrame+self.nFrames)) +#end: class Tracklet + + +def parseXML(trackletFile): + r""" parse tracklet xml file and convert results to list of Tracklet objects + + :param trackletFile: name of a tracklet xml file + :returns: list of Tracklet objects read from xml file + """ + + # convert tracklet XML data to a tree structure + eTree = ElementTree() + print('parsing tracklet file', trackletFile) + with open(trackletFile) as f: + eTree.parse(f) + + # now convert output to list of Tracklet objects + trackletsElem = eTree.find('tracklets') + tracklets = [] + trackletIdx = 0 + nTracklets = None + for trackletElem in trackletsElem: + #print('track:', trackletElem.tag) + if trackletElem.tag == 'count': + nTracklets = int(trackletElem.text) + print('file contains', nTracklets, 'tracklets') + elif trackletElem.tag == 'item_version': + pass + elif trackletElem.tag == 'item': + #print('tracklet {0} of {1}'.format(trackletIdx, nTracklets)) + # a tracklet + newTrack = Tracklet() + isFinished = False + hasAmt = False + frameIdx = None + for info in trackletElem: + #print('trackInfo:', info.tag) + if isFinished: + raise ValueError('more info on element after finished!') + if info.tag == 'objectType': + newTrack.objectType = info.text + elif info.tag == 'h': + newTrack.size[0] = float(info.text) + elif info.tag == 'w': + newTrack.size[1] = float(info.text) + elif info.tag == 'l': + newTrack.size[2] = float(info.text) + elif info.tag == 'first_frame': + newTrack.firstFrame = int(info.text) + elif info.tag == 'poses': + # this info is the possibly long list of poses + for pose in info: + #print('trackInfoPose:', pose.tag) + if pose.tag == 'count': # this should come before the others + if newTrack.nFrames is not None: + raise ValueError('there are several pose lists for a single track!') + elif frameIdx is not None: + raise ValueError('?!') + newTrack.nFrames = int(pose.text) + newTrack.trans = np.nan * np.ones((newTrack.nFrames, 3), dtype=float) + newTrack.rots = np.nan * np.ones((newTrack.nFrames, 3), dtype=float) + newTrack.states = np.nan * np.ones(newTrack.nFrames, dtype='uint8') + newTrack.occs = np.nan * np.ones((newTrack.nFrames, 2), dtype='uint8') + newTrack.truncs = np.nan * np.ones(newTrack.nFrames, dtype='uint8') + newTrack.amtOccs = np.nan * np.ones((newTrack.nFrames, 2), dtype=float) + newTrack.amtBorders = np.nan * np.ones((newTrack.nFrames, 3), dtype=float) + frameIdx = 0 + elif pose.tag == 'item_version': + pass + elif pose.tag == 'item': + # pose in one frame + if frameIdx is None: + raise ValueError('pose item came before number of poses!') + for poseInfo in pose: + #print('trackInfoPoseInfo:', poseInfo.tag) + if poseInfo.tag == 'tx': + newTrack.trans[frameIdx, 0] = float(poseInfo.text) + elif poseInfo.tag == 'ty': + newTrack.trans[frameIdx, 1] = float(poseInfo.text) + elif poseInfo.tag == 'tz': + newTrack.trans[frameIdx, 2] = float(poseInfo.text) + elif poseInfo.tag == 'rx': + newTrack.rots[frameIdx, 0] = float(poseInfo.text) + elif poseInfo.tag == 'ry': + newTrack.rots[frameIdx, 1] = float(poseInfo.text) + elif poseInfo.tag == 'rz': + newTrack.rots[frameIdx, 2] = float(poseInfo.text) + elif poseInfo.tag == 'state': + newTrack.states[frameIdx] = stateFromText[poseInfo.text] + elif poseInfo.tag == 'occlusion': + newTrack.occs[frameIdx, 0] = occFromText[poseInfo.text] + elif poseInfo.tag == 'occlusion_kf': + newTrack.occs[frameIdx, 1] = occFromText[poseInfo.text] + elif poseInfo.tag == 'truncation': + newTrack.truncs[frameIdx] = truncFromText[poseInfo.text] + elif poseInfo.tag == 'amt_occlusion': + newTrack.amtOccs[frameIdx,0] = float(poseInfo.text) + hasAmt = True + elif poseInfo.tag == 'amt_occlusion_kf': + newTrack.amtOccs[frameIdx,1] = float(poseInfo.text) + hasAmt = True + elif poseInfo.tag == 'amt_border_l': + newTrack.amtBorders[frameIdx,0] = float(poseInfo.text) + hasAmt = True + elif poseInfo.tag == 'amt_border_r': + newTrack.amtBorders[frameIdx,1] = float(poseInfo.text) + hasAmt = True + elif poseInfo.tag == 'amt_border_kf': + newTrack.amtBorders[frameIdx,2] = float(poseInfo.text) + hasAmt = True + else: + raise ValueError('unexpected tag in poses item: {0}!'.format(poseInfo.tag)) + frameIdx += 1 + else: + raise ValueError('unexpected pose info: {0}!'.format(pose.tag)) + elif info.tag == 'finished': + isFinished = True + else: + raise ValueError('unexpected tag in tracklets: {0}!'.format(info.tag)) + #end: for all fields in current tracklet + + # some final consistency checks on new tracklet + if not isFinished: + warn('tracklet {0} was not finished!'.format(trackletIdx)) + if newTrack.nFrames is None: + warn('tracklet {0} contains no information!'.format(trackletIdx)) + elif frameIdx != newTrack.nFrames: + warn('tracklet {0} is supposed to have {1} frames, but perser found {1}!'.format(\ + trackletIdx, newTrack.nFrames, frameIdx)) + if np.abs(newTrack.rots[:,:2]).sum() > 1e-16: + warn('track contains rotation other than yaw!') + + # if amtOccs / amtBorders are not set, set them to None + if not hasAmt: + newTrack.amtOccs = None + newTrack.amtBorders = None + + # add new tracklet to list + tracklets.append(newTrack) + trackletIdx += 1 + + else: + raise ValueError('unexpected tracklet info') + #end: for tracklet list items + + print('loaded', trackletIdx, 'tracklets') + + # final consistency check + if trackletIdx != nTracklets: + warn('according to xml information the file has {0} tracklets, but parser found {1}!'.format(nTracklets, trackletIdx)) + + return tracklets +#end: function parseXML + diff --git a/tests/datasets_tests/kitti_tests/test_KITTI.py b/tests/datasets_tests/kitti_tests/test_KITTI.py new file mode 100644 index 0000000000..e3843b6a29 --- /dev/null +++ b/tests/datasets_tests/kitti_tests/test_KITTI.py @@ -0,0 +1,75 @@ +import numpy as np +import os +import shutil +import tempfile +import unittest + +from chainer import testing +from chainer.testing import attr + +from chainercv.datasets import KITTIDataset +from chainercv.datasets.kitti.kitti_utils import kitti_labels +from chainercv.utils import assert_is_semantic_segmentation_dataset +from chainercv.utils.testing.assertions.assert_is_image import assert_is_image +from chainercv.utils import write_image + + +@testing.parameterize( + { + 'date': '2011_09_26', + 'driveNo': '0001', + 'imgNo': '00', + 'sync': True + }, + { + 'date': '2011_09_26', + 'driveNo': '0001', + 'imgNo': '00', + 'sync': False + }, + { + 'date': '2011_09_26', + 'driveNo': '0001', + 'imgNo': '02', + 'sync': True + }, + { + 'date': '2011_09_26', + 'driveNo': '0017', + 'imgNo': '00', + 'sync': True + }, + { + 'date': '2011_09_28', + 'driveNo': '0001', + 'imgNo': '00', + 'sync': True + }, + { + 'date': '2011_10_03', + 'driveNo': '0047', + 'imgNo': '00', + 'sync': True + }, +) + +class TestKITTIDataset(unittest.TestCase): + + def setUp(self): + self.dataset = KITTIDataset( + date=self.date, + driveNo=self.driveNo, + imgNo=self.imgNo, + sync=self.sync) + + + @attr.slow + @attr.disk + def test_kitti_semantic_segmentation_dataset(self): + indices = np.random.permutation(np.arange(len(self.dataset))) + for i in indices[:10]: + img = self.dataset[i] + assert_is_image(img, color=True) + + +testing.run_module(__name__, __file__) From 7475891202ead6f2e509ad9c6cb170a5db57e0b5 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Mon, 23 Apr 2018 16:15:53 +0900 Subject: [PATCH 02/33] import module miss(modified from KITTI[upper] to kitti[lower]) debug message off --- chainercv/datasets/kitti/KITTI_dataset.py | 50 +++++++------------- chainercv/datasets/kitti/parseTrackletXML.py | 18 +++---- 2 files changed, 26 insertions(+), 42 deletions(-) diff --git a/chainercv/datasets/kitti/KITTI_dataset.py b/chainercv/datasets/kitti/KITTI_dataset.py index d3113ce311..beb7d748b1 100644 --- a/chainercv/datasets/kitti/KITTI_dataset.py +++ b/chainercv/datasets/kitti/KITTI_dataset.py @@ -14,12 +14,12 @@ # use pykitti import pykitti + import itertools # tracklet parser -# import parseTrackletXML as xmlParser -from chainercv.datasets.KITTI import parseTrackletXML as xmlParser -# from parseTrackletXML import parseXML +from chainercv.datasets.kitti import parseTrackletXML as xmlParser +# check import matplotlib.pyplot as plt from chainercv.visualizations import vis_bbox @@ -115,7 +115,6 @@ def __init__(self, data_dir='auto', date='', driveNo='', color=True, sync=True, # img02 self.cur_R_rect = self.dataset.calib.R_rect_20 self.cur_P_rect = self.dataset.calib.P_rect_20 - # set imformat='cv2' self.imgs = np.array(list(self.dataset.cam2)) # img = np.array(list(self.dataset.rgb)[0]) # img = np.uint8(np.array(list(self.dataset.rgb)[0]) * 255).astype(np.float32) @@ -125,7 +124,6 @@ def __init__(self, data_dir='auto', date='', driveNo='', color=True, sync=True, self.cur_P_rect = self.dataset.calib.P_rect_30 self.imgs = np.array(list(self.dataset.cam3)) # img = np.array(list(self.dataset.rgb)[1]) - # imformat='None' # img = np.uint8(np.array(list(self.dataset.rgb)[1]) * 255).astype(np.float32) else: if self.isLeft == True: @@ -135,7 +133,7 @@ def __init__(self, data_dir='auto', date='', driveNo='', color=True, sync=True, self.imgs = np.array(list(self.dataset.cam0)) # img = np.array(list(self.dataset.gray)[0]) else: - # img00 + # img01 self.cur_R_rect = self.dataset.calib.R_rect_10 self.cur_P_rect = self.dataset.calib.P_rect_10 self.imgs = np.array(list(self.dataset.cam1)) @@ -247,7 +245,7 @@ def get_example(self, i): # return next(iter(itertools.islice(self.dataset.velo, 0, None))) label = self.labels[i] # return next(iter(itertools.islice(self.dataset.velo, i, None))) - return next(iter(itertools.islice(self.dataset.velo, i, None))), bbox_3d, label + return next(iter(itertools.islice(self.dataset.velo, i, None))), bbox, label def get_KITTI_Sync_Data(self, root, date, driveNo): @@ -311,6 +309,9 @@ def get_KITTI_NoSync_Data(self, root, date, driveNo): def get_KITTI_Image(self, data_root, date, driveNo, ImgNo): + """ + no use pykitti + """ folder = date + '_drive_' + driveNo img_dir = os.path.join(data_root, os.path.join(date, folder + '_sync', 'image_' + ImgNo, 'data')) @@ -325,8 +326,6 @@ def get_KITTI_Image(self, data_root, date, driveNo, ImgNo): self.img_paths.append(img_path) - - def get_KITTI_Tracklets(self, data_root, date, driveNo): # read calibration files kitti_dir = os.path.join(data_root, date) @@ -410,18 +409,16 @@ def get_KITTI_Label(self, tracklets): # param3 = translation.reshape(3, 1) * self.dataset.calib.P_rect_20 # print(cornerPosInVelo[:, 0:1].shape) pt3d = np.vstack((cornerPosInVelo[:,0:8], np.ones(8))) - # print(pt3d) # print(pt3d.shape) # print(self.dataset.calib.P_rect_20) - # pt2d = self.project_velo_points_in_img(pt3d, self.dataset.calib.T_cam2_velo, self.dataset.calib.R_rect_20, self.dataset.calib.P_rect_20) pt2d = self.project_velo_points_in_img(pt3d, self.dataset.calib.T_cam2_velo, self.cur_R_rect, self.cur_P_rect) # print(pt2d) - ymin = min(pt2d[1, :]) xmin = min(pt2d[0, :]) - ymax = max(pt2d[1, :]) xmax = max(pt2d[0, :]) + ymin = min(pt2d[1, :]) + ymax = max(pt2d[1, :]) param = np.array((ymin, xmin, ymax, xmax)) # bbox.append(param) # bbox = np.stack(bbox).astype(np.float32) @@ -429,7 +426,7 @@ def get_KITTI_Label(self, tracklets): self.bboxes[absoluteFrameNumber].append(param) # print(self.bboxes[absoluteFrameNumber]) # param_3d = cornerPosInVelo - self.bboxes_3d[absoluteFrameNumber].append(cornerPosInVelo) + # self.bboxes_3d[absoluteFrameNumber].append(cornerPosInVelo) # label.append(param2) # label = np.stack(label).astype(np.int32) # self.labels[absoluteFrameNumber] = label @@ -440,22 +437,11 @@ def get_KITTI_Label(self, tracklets): self.labels[absoluteFrameNumber].append(param2) # print(self.bboxes[absoluteFrameNumber]) - #end: for all frames in track - #end: for all tracks + # end : for all frames in track + # end : for all tracks # def get_KITTI_Calibration(self, data_root, date, driveNo): - # def prepare_velo_points(pts3d_raw): - # '''Replaces the reflectance value by 1, and tranposes the array, so - # points can be directly multiplied by the camera projection matrix''' - # - # pts3d = pts3d_raw - # # Reflectance > 0 - # pts3d = pts3d[pts3d[:, 3] > 0 ,:] - # pts3d[:,3] = 1 - # return pts3d.transpose() - - def project_velo_points_in_img(self, pts3d, T_cam_velo, Rrect, Prect): """Project 3D points into 2D image. Expects pts3d as a 4xN numpy array. Returns the 2D projection of the points that @@ -464,13 +450,13 @@ def project_velo_points_in_img(self, pts3d, T_cam_velo, Rrect, Prect): # 3D points in camera reference frame. pts3d_cam = Rrect.dot(T_cam_velo.dot(pts3d)) - # Before projecting, keep only points with z>0 + # Before projecting, keep only points with z > 0 # (points that are in fronto of the camera). - idx = (pts3d_cam[2,:]>=0) + idx = (pts3d_cam[2,:] >= 0) pts2d_cam = Prect.dot(pts3d_cam[:,idx]) - # return pts3d[:, idx], pts2d_cam/pts2d_cam[2,:] - return pts2d_cam/pts2d_cam[2,:] + # return pts3d[:, idx], pts2d_cam / pts2d_cam[2,:] + return pts2d_cam / pts2d_cam[2,:] if __name__ == '__main__': @@ -497,8 +483,6 @@ def project_velo_points_in_img(self, pts3d, T_cam_velo, Rrect, Prect): # d = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True) print(len(d)) img, bbox, label = d[20] - # img, label = d[0] - # (3, 375, 1242) # print(img) # print(img.shape) # Data no Sync diff --git a/chainercv/datasets/kitti/parseTrackletXML.py b/chainercv/datasets/kitti/parseTrackletXML.py index cbfafdb4bc..928fcf397b 100644 --- a/chainercv/datasets/kitti/parseTrackletXML.py +++ b/chainercv/datasets/kitti/parseTrackletXML.py @@ -94,7 +94,7 @@ def __str__(self): r""" return human-readable string representation of tracklet object called implicitly in - print(trackletObj) + # print(trackletObj) or in text = str(trackletObj) """ @@ -133,7 +133,7 @@ def parseXML(trackletFile): # convert tracklet XML data to a tree structure eTree = ElementTree() - print('parsing tracklet file', trackletFile) + # print('parsing tracklet file', trackletFile) with open(trackletFile) as f: eTree.parse(f) @@ -143,21 +143,21 @@ def parseXML(trackletFile): trackletIdx = 0 nTracklets = None for trackletElem in trackletsElem: - #print('track:', trackletElem.tag) + # print('track:', trackletElem.tag) if trackletElem.tag == 'count': nTracklets = int(trackletElem.text) - print('file contains', nTracklets, 'tracklets') + # print('file contains', nTracklets, 'tracklets') elif trackletElem.tag == 'item_version': pass elif trackletElem.tag == 'item': - #print('tracklet {0} of {1}'.format(trackletIdx, nTracklets)) + # print('tracklet {0} of {1}'.format(trackletIdx, nTracklets)) # a tracklet newTrack = Tracklet() isFinished = False hasAmt = False frameIdx = None for info in trackletElem: - #print('trackInfo:', info.tag) + # print('trackInfo:', info.tag) if isFinished: raise ValueError('more info on element after finished!') if info.tag == 'objectType': @@ -173,7 +173,7 @@ def parseXML(trackletFile): elif info.tag == 'poses': # this info is the possibly long list of poses for pose in info: - #print('trackInfoPose:', pose.tag) + # print('trackInfoPose:', pose.tag) if pose.tag == 'count': # this should come before the others if newTrack.nFrames is not None: raise ValueError('there are several pose lists for a single track!') @@ -195,7 +195,7 @@ def parseXML(trackletFile): if frameIdx is None: raise ValueError('pose item came before number of poses!') for poseInfo in pose: - #print('trackInfoPoseInfo:', poseInfo.tag) + # print('trackInfoPoseInfo:', poseInfo.tag) if poseInfo.tag == 'tx': newTrack.trans[frameIdx, 0] = float(poseInfo.text) elif poseInfo.tag == 'ty': @@ -266,7 +266,7 @@ def parseXML(trackletFile): raise ValueError('unexpected tracklet info') #end: for tracklet list items - print('loaded', trackletIdx, 'tracklets') + # print('loaded', trackletIdx, 'tracklets') # final consistency check if trackletIdx != nTracklets: From af944907ae18c60f52d870f3638b7b5dc0c65563 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Wed, 25 Apr 2018 17:49:45 +0900 Subject: [PATCH 03/33] add comment --- chainercv/datasets/kitti/KITTI_dataset.py | 21 ++++++++++++++++++++- docs/source/reference/datasets.rst | 8 ++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/chainercv/datasets/kitti/KITTI_dataset.py b/chainercv/datasets/kitti/KITTI_dataset.py index beb7d748b1..03db92d3c8 100644 --- a/chainercv/datasets/kitti/KITTI_dataset.py +++ b/chainercv/datasets/kitti/KITTI_dataset.py @@ -71,7 +71,26 @@ class KITTIDataset(chainer.dataset.DatasetMixin): contain the :obj:`---` directory. If :obj:`auto` is given, it uses :obj:`$CHAINER_DATSET_ROOT/pfnet/chainercv/KITTI` by default. - + date ({'2011_09_26', '2011_09_28', '2011_09_29', '2011_09_30', '2011_10_03'}): + reference Calibration datas. + driveNo ({'0xxx'}): get datas drive No. + color (bool): use glay/color image. + sync (bool): get timer sync/nosync data. + isLeft (bool): glay/color image use 2type. + + This dataset returns the following data. + + .. csv-table:: + :header: name, shape, dtype, format + + :obj:`img`, ":math:`(3, H, W)`", :obj:`float32`, \ + "RGB, :math:`[0, 255]`" + :obj:`label`, scalar, :obj:`int32`, ":math:`[0, \#class - 1]`" + :obj:`bbox` [#kitti_bbox_1]_, ":math:`(R, 4)`", :obj:`float32`, \ + ":math:`(y_{min}, x_{min}, y_{max}, x_{max})`" + + .. [#kitti_bbox_1] If :obj:`use_pykitty = False`, \ + :obj:`bbox` and :obj:`label` not contain instances. """ def __init__(self, data_dir='auto', date='', driveNo='', color=True, sync=True, isLeft=True): diff --git a/docs/source/reference/datasets.rst b/docs/source/reference/datasets.rst index b4ffcad70a..123ed9ccb1 100644 --- a/docs/source/reference/datasets.rst +++ b/docs/source/reference/datasets.rst @@ -63,6 +63,14 @@ CUBPointDataset .. autoclass:: CUBPointDataset +KITTI +----- + +KITTIDataset +~~~~~~~~~~~~ +.. autoclass:: KITTIDataset + + OnlineProducts -------------- From ace06ab0fee1b4bb60706715297f8ed627aaef82 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Mon, 21 May 2018 19:16:03 +0900 Subject: [PATCH 04/33] use GetterDataset --- chainercv/datasets/__init__.py | 3 + chainercv/datasets/kitti/KITTI_dataset.py | 344 +++++-------- chainercv/datasets/kitti/parseTrackletXML.py | 466 ++++++++++-------- .../datasets_tests/kitti_tests/test_KITTI.py | 75 --- .../kitti_tests/test_kitti_dataset.py | 74 +++ 5 files changed, 440 insertions(+), 522 deletions(-) delete mode 100644 tests/datasets_tests/kitti_tests/test_KITTI.py create mode 100644 tests/datasets_tests/kitti_tests/test_kitti_dataset.py diff --git a/chainercv/datasets/__init__.py b/chainercv/datasets/__init__.py index 2e75d7cac2..34b30c44bb 100644 --- a/chainercv/datasets/__init__.py +++ b/chainercv/datasets/__init__.py @@ -13,6 +13,9 @@ from chainercv.datasets.cub.cub_label_dataset import CUBLabelDataset # NOQA from chainercv.datasets.cub.cub_point_dataset import CUBPointDataset # NOQA from chainercv.datasets.cub.cub_utils import cub_label_names # NOQA +from chainercv.datasets.kitti.KITTI_dataset import KITTI_ignore_label_color # NOQA +from chainercv.datasets.kitti.KITTI_dataset import KITTI_label_colors # NOQA +from chainercv.datasets.kitti.KITTI_dataset import KITTI_label_names # NOQA from chainercv.datasets.kitti.KITTI_dataset import KITTIDataset # NOQA from chainercv.datasets.kitti.parseTrackletXML import parseXML # NOQA from chainercv.datasets.directory_parsing_label_dataset import directory_parsing_label_names # NOQA diff --git a/chainercv/datasets/kitti/KITTI_dataset.py b/chainercv/datasets/kitti/KITTI_dataset.py index 03db92d3c8..719ee2f11e 100644 --- a/chainercv/datasets/kitti/KITTI_dataset.py +++ b/chainercv/datasets/kitti/KITTI_dataset.py @@ -4,11 +4,13 @@ import numpy as np -import chainer from chainer.dataset import download + +from chainercv.chainer_experimental.datasets.sliceable import GetterDataset from chainercv import utils from chainercv.utils.image import read_image + # root = 'pfnet/chainercv/KITTI' url_base = 'http://kitti.is.tue.mpg.de/kitti/raw_data/' @@ -24,15 +26,14 @@ from chainercv.visualizations import vis_bbox # image_shape = 375, 1242 - -KITTI_category_names = ( - 'City', - 'Residential', - 'Road', - 'Campus', - 'Person', - 'Calibration' -) +# KITTI_category_names = ( +# 'City', +# 'Residential', +# 'Road', +# 'Campus', +# 'Person', +# 'Calibration' +# ) KITTI_label_names = ( 'Car', @@ -58,7 +59,63 @@ KITTI_ignore_label_color = (0, 0, 0) -class KITTIDataset(chainer.dataset.DatasetMixin): +def get_KITTI_Sync_Data(root, date, driveNo): + data_root = download.get_dataset_directory(root) + + # data + folder = date + '_drive_' + driveNo + url_data = urljoin(url_base, folder+ '/' + folder + '_sync.zip') + + # calibration + url_calib = url_base + date + '_calib.zip' + + # tracklet + url_tracklet = urljoin(url_base, folder+ '/' + folder + '_tracklets.zip') + + download_file_path = utils.cached_download(url_data) + ext = os.path.splitext(url_data)[1] + utils.extractall(download_file_path, data_root, ext) + + download_file_path = utils.cached_download(url_calib) + ext = os.path.splitext(url_calib)[1] + utils.extractall(download_file_path, data_root, ext) + + download_file_path = utils.cached_download(url_tracklet) + ext = os.path.splitext(url_tracklet)[1] + utils.extractall(download_file_path, data_root, ext) + + return data_root + + +def get_KITTI_NoSync_Data(root, date, driveNo): + data_root = download.get_dataset_directory(root) + + # data + folder = date + '_drive_' + driveNo + url_data = urljoin(url_base, folder+ '/' + folder + '_extract.zip') + + # calibration + url_calib = url_base + date + '_calib.zip' + + # tracklet + url_tracklet = urljoin(url_base, folder+ '/' + folder + '_tracklets.zip') + + download_file_path = utils.cached_download(url_data) + ext = os.path.splitext(url_data)[1] + utils.extractall(download_file_path, data_root, ext) + + download_file_path = utils.cached_download(url_calib) + ext = os.path.splitext(url_calib)[1] + utils.extractall(download_file_path, data_root, ext) + + download_file_path = utils.cached_download(url_tracklet) + ext = os.path.splitext(url_tracklet)[1] + utils.extractall(download_file_path, data_root, ext) + + return data_root + + +class KITTIDataset(GetterDataset): """Image dataset for test split of `KITTI dataset`_. @@ -100,28 +157,11 @@ def __init__(self, data_dir='auto', date='', driveNo='', color=True, sync=True, if data_dir == 'auto': if sync == True: # download sync data - # data_dir = self.get_KITTI_Sync_Data('pfnet/chainercv/KITTI', date, driveNo) - data_dir = self.get_KITTI_Sync_Data(os.path.join('pfnet', 'chainercv', 'KITTI'), date, driveNo) + data_dir = get_KITTI_Sync_Data(os.path.join('pfnet', 'chainercv', 'KITTI'), date, driveNo) else: # download nosync data - # data_dir = self.get_KITTI_NoSync_Data('pfnet/chainercv/KITTI', date, driveNo) - data_dir = self.get_KITTI_NoSync_Data(os.path.join('pfnet', 'chainercv', 'KITTI'), date, driveNo) - - # no use pykitti - # if self.color == True: - # if self.isLeft == True: - # imgNo = '02' - # else: - # imgNo = '03' - # else: - # if self.isLeft == True: - # imgNo = '00' - # else: - # imgNo = '01' - # image - # self.get_KITTI_Image(data_dir, date, driveNo, imgNo) - # img = read_image(self.img_paths[0]) - ## + data_dir = get_KITTI_NoSync_Data(os.path.join('pfnet', 'chainercv', 'KITTI'), date, driveNo) + # use pykitti # read All images # imformat='None' @@ -135,34 +175,27 @@ def __init__(self, data_dir='auto', date='', driveNo='', color=True, sync=True, self.cur_R_rect = self.dataset.calib.R_rect_20 self.cur_P_rect = self.dataset.calib.P_rect_20 self.imgs = np.array(list(self.dataset.cam2)) - # img = np.array(list(self.dataset.rgb)[0]) - # img = np.uint8(np.array(list(self.dataset.rgb)[0]) * 255).astype(np.float32) else: # img03 self.cur_R_rect = self.dataset.calib.R_rect_30 self.cur_P_rect = self.dataset.calib.P_rect_30 self.imgs = np.array(list(self.dataset.cam3)) - # img = np.array(list(self.dataset.rgb)[1]) - # img = np.uint8(np.array(list(self.dataset.rgb)[1]) * 255).astype(np.float32) else: if self.isLeft == True: # img00 self.cur_R_rect = self.dataset.calib.R_rect_00 self.cur_P_rect = self.dataset.calib.P_rect_00 self.imgs = np.array(list(self.dataset.cam0)) - # img = np.array(list(self.dataset.gray)[0]) else: # img01 self.cur_R_rect = self.dataset.calib.R_rect_10 self.cur_P_rect = self.dataset.calib.P_rect_10 self.imgs = np.array(list(self.dataset.cam1)) - # img = np.array(list(self.dataset.gray)[1]) - # get object info(type/area/...) + # get object info(type/area/bbox/...) self.tracklets = self.get_KITTI_Tracklets(data_dir, date, driveNo) # set arrays - # length = len(self.dataset.cam0) length = self.__len__() self.bboxes = [0] * length self.labels = [0] * length @@ -172,177 +205,30 @@ def __init__(self, data_dir='auto', date='', driveNo='', color=True, sync=True, self.get_KITTI_Label(self.tracklets) + self.add_getter('img', self._get_image) + self.add_getter('iabel', self._get_label) + self.add_getter('bbox', self._get_bbox) - def __getitem__(self, index): - # Note : before setting datas. - # no use pykitti - # return read_image(self.img_paths[index]) - # use pykitti - img = self.imgs[index] - bbox = self.bboxes[index] - label = self.labels[index] + def __len__(self): + return len(self.imgs) + def _get_image(self, i): + img = self.imgs[i] # convert data is utils.read_image function return values if img.ndim == 2: # reshape (H, W) -> (1, H, W) - # return img[np.newaxis] - return img[np.newaxis], bbox, label + return img[np.newaxis] else: # transpose (H, W, C) -> (C, H, W) - return img.transpose((2, 0, 1)), bbox, label - + return img.transpose((2, 0, 1)) - def __len__(self): - # no use pykitti - # return len(self.img_paths) - # use pykitti - # print(self.imgs) - return len(self.imgs) - - -# # img -# def get_example(self, i): -# """Returns the i-th test image. -# -# Returns a color image. The color image is in CHW format. -# -# Args: -# i (int): The index of the example. -# -# Returns: -# A color image whose shape is (3, H, W). H and W are height and -# width of the image. -# The dtype of the color image is :obj:`numpy.float32`. -# -# """ -# # return read_image(self.img_paths[i]) -### - -# 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 -# -### - - # point - def get_example(self, i): - """Returns the i-th test point. - - Returns a color point. The color point is in XYZRGB format. - - Args: - i (int): The index of the example. - - Returns: - A color point whose shape is (6, x, y, z). H and W are height and - width of the point. - The dtype of the color point is :obj:`numpy.float32`. - - """ - # use pykitti - # return self.dataset.gray[i] - # return next(iter(itertools.islice(self.dataset.velo, 0, None))) + def _get_label(self, i): label = self.labels[i] - # return next(iter(itertools.islice(self.dataset.velo, i, None))) - return next(iter(itertools.islice(self.dataset.velo, i, None))), bbox, label - - - def get_KITTI_Sync_Data(self, root, date, driveNo): - data_root = download.get_dataset_directory(root) - # print('dst path : ' + data_root) - - # data - folder = date + '_drive_' + driveNo - # ok - # url_data = url_base + '/' + folder+ '/' + folder + '_sync.zip' - url_data = urljoin(url_base, folder+ '/' + folder + '_sync.zip') - # print('url : ' + url_data) - - # calibration - url_calib = url_base + date + '_calib.zip' - - # tracklet - url_tracklet = urljoin(url_base, folder+ '/' + folder + '_tracklets.zip') - - download_file_path = utils.cached_download(url_data) - ext = os.path.splitext(url_data)[1] - utils.extractall(download_file_path, data_root, ext) - - download_file_path = utils.cached_download(url_calib) - ext = os.path.splitext(url_calib)[1] - utils.extractall(download_file_path, data_root, ext) - - download_file_path = utils.cached_download(url_tracklet) - ext = os.path.splitext(url_tracklet)[1] - utils.extractall(download_file_path, data_root, ext) - - return data_root - - - def get_KITTI_NoSync_Data(self, root, date, driveNo): - data_root = download.get_dataset_directory(root) - - # data - folder = date + '_drive_' + driveNo - url_data = urljoin(url_base, folder+ '/' + folder + '_extract.zip') + return label - # calibration - url_calib = url_base + date + '_calib.zip' - - # tracklet - url_tracklet = urljoin(url_base, folder+ '/' + folder + '_tracklets.zip') - - download_file_path = utils.cached_download(url_data) - ext = os.path.splitext(url_data)[1] - utils.extractall(download_file_path, data_root, ext) - - download_file_path = utils.cached_download(url_calib) - ext = os.path.splitext(url_calib)[1] - utils.extractall(download_file_path, data_root, ext) - - download_file_path = utils.cached_download(url_tracklet) - ext = os.path.splitext(url_tracklet)[1] - utils.extractall(download_file_path, data_root, ext) - - return data_root - - - def get_KITTI_Image(self, data_root, date, driveNo, ImgNo): - """ - no use pykitti - """ - folder = date + '_drive_' + driveNo - img_dir = os.path.join(data_root, os.path.join(date, folder + '_sync', 'image_' + ImgNo, 'data')) - - self.img_paths = list() - if not os.path.exists(img_dir): - raise ValueError( - 'KITTI dataset does not exist at the expected location.' - 'Please download it from http://www.cvlibs.net/datasets/kitti/raw_data.php.' - 'Then place directory image at {}.'.format(img_dir)) - - for img_path in sorted(glob.glob(os.path.join(img_dir, '*.png'))): - self.img_paths.append(img_path) + def _get_bbox(self, i): + bbox = self.bboxes[i] + return bbox def get_KITTI_Tracklets(self, data_root, date, driveNo): @@ -365,7 +251,7 @@ def get_KITTI_Tracklets(self, data_root, date, driveNo): def get_KITTI_Label(self, tracklets): - twoPi = 2.*np.pi + # twoPi = 2.*np.pi # loop over tracklets for iTracklet, tracklet in enumerate(tracklets): # print('tracklet {0: 3d}: {1}'.format(iTracklet, tracklet)) @@ -398,18 +284,6 @@ def get_KITTI_Label(self, tracklets): [np.sin(yaw), np.cos(yaw), 0.0], \ [ 0.0, 0.0, 1.0]]) cornerPosInVelo = np.dot(rotMat, trackletBox) + np.tile(translation, (8,1)).T - # print(cornerPosInVelo) - # print(cornerPosInVelo[:,0]) - # print(cornerPosInVelo.shape) 3*8 - # print('tracklet for : ') - # print(iTracklet) - # print(translation) - # print(rotation) - # print(state) - # print(occlusion) - # print(truncation) - # print(amtOcclusion) - # print(amtBorders) # calc yaw as seen from the camera (i.e. 0 degree = facing away from cam), as opposed to # car-centered yaw (i.e. 0 degree = same orientation as car). @@ -417,7 +291,7 @@ def get_KITTI_Label(self, tracklets): # Result is in [0, 2pi] x, y, z = translation # print(translation) - yawVisual = ( yaw - np.arctan2(y, x) ) % twoPi + # yawVisual = ( yaw - np.arctan2(y, x) ) % twoPi # print(yaw) # print(yawVisual) # param = pykitti.utils.transform_from_rot_trans(rotMat, translation) @@ -430,7 +304,6 @@ def get_KITTI_Label(self, tracklets): pt3d = np.vstack((cornerPosInVelo[:,0:8], np.ones(8))) # print(pt3d.shape) # print(self.dataset.calib.P_rect_20) - pt2d = self.project_velo_points_in_img(pt3d, self.dataset.calib.T_cam2_velo, self.cur_R_rect, self.cur_P_rect) # print(pt2d) @@ -459,8 +332,6 @@ def get_KITTI_Label(self, tracklets): # end : for all frames in track # end : for all tracks - # def get_KITTI_Calibration(self, data_root, date, driveNo): - def project_velo_points_in_img(self, pts3d, T_cam_velo, Rrect, Prect): """Project 3D points into 2D image. Expects pts3d as a 4xN numpy array. Returns the 2D projection of the points that @@ -480,32 +351,49 @@ def project_velo_points_in_img(self, pts3d, T_cam_velo, Rrect, Prect): if __name__ == '__main__': # 00, 01 : gray - # d = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = True) + # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = True) # print(len(d)) # img = d[0] # print(img) # print(img.shape) - # d = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = True, isLeft=False) + # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = True, isLeft=False) # print(len(d)) - # img = d[0] + # img, bbox, = dataset[0] # print(img) # print(img.shape) # 02, 03 : color - # d = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True) - # d = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True, isLeft=False) + # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True) + # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True, isLeft=False) # local Folder - # d = KITTIDataset(date='2011_09_26', driveNo='0005', color=True, sync = True, isLeft=False) - d = KITTIDataset(date='2011_09_26', driveNo='0020', color=True, sync = True) + # dataset = KITTIDataset(date='2011_09_26', driveNo='0005', color=True, sync = True, isLeft=False) + dataset = KITTIDataset(date='2011_09_26', driveNo='0020', color=True, sync = True) # use pykitti - # d = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True) - print(len(d)) - img, bbox, label = d[20] + # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True) + print(len(dataset)) + img, bbox, label = dataset[20] + + # keys returns the names of data + print(dataset.keys) # ('img', 'bbox', 'label') + # we can get an example by [] + img, bbox, label = dataset[0] + + # get a view of the first 50 examples + view = dataset.slice[:50] + print(len(view)) # 50 + + # get a view of image and label + view = dataset.slice[:, ('img', 'label')] + # the view also supports sliceable, so that we can call keys + print(view.keys) # ('img', 'label') + # we can get an example by [] + img, label = view[0] + # print(img) # print(img.shape) # Data no Sync - # d = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = False) + # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = False) # print(img.debug_print()) vis_bbox(img, bbox, label, score=None, label_names=KITTI_label_names) plt.show() diff --git a/chainercv/datasets/kitti/parseTrackletXML.py b/chainercv/datasets/kitti/parseTrackletXML.py index 928fcf397b..15b214c8e1 100644 --- a/chainercv/datasets/kitti/parseTrackletXML.py +++ b/chainercv/datasets/kitti/parseTrackletXML.py @@ -20,8 +20,8 @@ """ # Version History: -# 4/7/12 Christian Herdtweck: seems to work with a few random test xml tracklet files; -# converts file contents to ElementTree and then to list of Tracklet objects; +# 4/7/12 Christian Herdtweck: seems to work with a few random test xml tracklet files; +# converts file contents to ElementTree and then to list of Tracklet objects; # Tracklet objects have str and iter functions # 5/7/12 ch: added constants for state, occlusion, truncation and added consistency checks # 30/1/14 ch: create example function from example code @@ -37,241 +37,269 @@ STATE_UNSET = 0 STATE_INTERP = 1 STATE_LABELED = 2 -stateFromText = {'0':STATE_UNSET, '1':STATE_INTERP, '2':STATE_LABELED} +stateFromText = {'0': STATE_UNSET, '1': STATE_INTERP, '2': STATE_LABELED} OCC_UNSET = 255 # -1 as uint8 OCC_VISIBLE = 0 OCC_PARTLY = 1 OCC_FULLY = 2 -occFromText = {'-1':OCC_UNSET, '0':OCC_VISIBLE, '1':OCC_PARTLY, '2':OCC_FULLY} +occFromText = {'-1': OCC_UNSET, '0': OCC_VISIBLE, + '1': OCC_PARTLY, '2': OCC_FULLY} TRUNC_UNSET = 255 # -1 as uint8, but in xml files the value '99' is used! TRUNC_IN_IMAGE = 0 TRUNC_TRUNCATED = 1 TRUNC_OUT_IMAGE = 2 TRUNC_BEHIND_IMAGE = 3 -truncFromText = {'99':TRUNC_UNSET, '0':TRUNC_IN_IMAGE, '1':TRUNC_TRUNCATED, \ - '2':TRUNC_OUT_IMAGE, '3': TRUNC_BEHIND_IMAGE} +truncFromText = {'99': TRUNC_UNSET, '0': TRUNC_IN_IMAGE, '1': TRUNC_TRUNCATED, + '2': TRUNC_OUT_IMAGE, '3': TRUNC_BEHIND_IMAGE} class Tracklet(object): - r""" representation an annotated object track - - Tracklets are created in function parseXML and can most conveniently used as follows: - - for trackletObj in parseXML(trackletFile): - for translation, rotation, state, occlusion, truncation, amtOcclusion, amtBorders, absoluteFrameNumber in trackletObj: - ... your code here ... - #end: for all frames - #end: for all tracklets - - absoluteFrameNumber is in range [firstFrame, firstFrame+nFrames[ - amtOcclusion and amtBorders could be None - - You can of course also directly access the fields objType (string), size (len-3 ndarray), firstFrame/nFrames (int), - trans/rots (nFrames x 3 float ndarrays), states/truncs (len-nFrames uint8 ndarrays), occs (nFrames x 2 uint8 ndarray), - and for some tracklets amtOccs (nFrames x 2 float ndarray) and amtBorders (nFrames x 3 float ndarray). The last two - can be None if the xml file did not include these fields in poses - """ - - objectType = None - size = None # len-3 float array: (height, width, length) - firstFrame = None - trans = None # n x 3 float array (x,y,z) - rots = None # n x 3 float array (x,y,z) - states = None # len-n uint8 array of states - occs = None # n x 2 uint8 array (occlusion, occlusion_kf) - truncs = None # len-n uint8 array of truncation - amtOccs = None # None or (n x 2) float array (amt_occlusion, amt_occlusion_kf) - amtBorders = None # None (n x 3) float array (amt_border_l / _r / _kf) - nFrames = None - - def __init__(self): - r""" create Tracklet with no info set """ - self.size = np.nan*np.ones(3, dtype=float) - - def __str__(self): - r""" return human-readable string representation of tracklet object - - called implicitly in - # print(trackletObj) - or in - text = str(trackletObj) - """ - return '[Tracklet over {0} frames for {1}]'.format(self.nFrames, self.objectType) - - def __iter__(self): - r""" returns an iterator that yields tuple of all the available data for each frame - - called whenever code iterates over a tracklet object, e.g. in - for translation, rotation, state, occlusion, truncation, amtOcclusion, amtBorders, absoluteFrameNumber in trackletObj: - ...do something ... - or - trackDataIter = iter(trackletObj) + r""" representation an annotated object track + + Tracklets are created in function parseXML and can most conveniently used as follows: + + for trackletObj in parseXML(trackletFile): + for translation, rotation, state, occlusion, truncation, amtOcclusion, amtBorders, absoluteFrameNumber in trackletObj: + ... your code here ... + #end: for all frames + #end: for all tracklets + + absoluteFrameNumber is in range [firstFrame, firstFrame+nFrames[ + amtOcclusion and amtBorders could be None + + You can of course also directly access the fields objType (string), size (len-3 ndarray), firstFrame/nFrames (int), + trans/rots (nFrames x 3 float ndarrays), states/truncs (len-nFrames uint8 ndarrays), occs (nFrames x 2 uint8 ndarray), + and for some tracklets amtOccs (nFrames x 2 float ndarray) and amtBorders (nFrames x 3 float ndarray). The last two + can be None if the xml file did not include these fields in poses """ - if self.amtOccs is None: - # return itertools.izip(self.trans, self.rots, self.states, self.occs, self.truncs, \ - # itertools.repeat(None), itertools.repeat(None), xrange(self.firstFrame, self.firstFrame+self.nFrames)) - # Python3 - return zip(self.trans, self.rots, self.states, self.occs, self.truncs, \ - repeat(None), repeat(None), range(self.firstFrame, self.firstFrame+self.nFrames)) - else: - # return itertools.izip(self.trans, self.rots, self.states, self.occs, self.truncs, \ - # self.amtOccs, self.amtBorders, xrange(self.firstFrame, self.firstFrame+self.nFrames)) - # Python3 - return zip(self.trans, self.rots, self.states, self.occs, self.truncs, \ - self.amtOccs, self.amtBorders, range(self.firstFrame, self.firstFrame+self.nFrames)) -#end: class Tracklet + + objectType = None + size = None # len-3 float array: (height, width, length) + firstFrame = None + trans = None # n x 3 float array (x,y,z) + rots = None # n x 3 float array (x,y,z) + states = None # len-n uint8 array of states + occs = None # n x 2 uint8 array (occlusion, occlusion_kf) + truncs = None # len-n uint8 array of truncation + # None or (n x 2) float array (amt_occlusion, amt_occlusion_kf) + amtOccs = None + amtBorders = None # None (n x 3) float array (amt_border_l / _r / _kf) + nFrames = None + + def __init__(self): + r""" create Tracklet with no info set """ + self.size = np.nan*np.ones(3, dtype=float) + + def __str__(self): + r""" return human-readable string representation of tracklet object + + called implicitly in + # print(trackletObj) + or in + text = str(trackletObj) + """ + return '[Tracklet over {0} frames for {1}]'.format(self.nFrames, self.objectType) + + def __iter__(self): + r""" returns an iterator that yields tuple of all the available data for each frame + + called whenever code iterates over a tracklet object, e.g. in + for translation, rotation, state, occlusion, truncation, amtOcclusion, amtBorders, absoluteFrameNumber in trackletObj: + ...do something ... + or + trackDataIter = iter(trackletObj) + """ + if self.amtOccs is None: + # return itertools.izip(self.trans, self.rots, self.states, self.occs, self.truncs, \ + # itertools.repeat(None), itertools.repeat(None), xrange(self.firstFrame, self.firstFrame+self.nFrames)) + # Python3 + return zip(self.trans, self.rots, self.states, self.occs, self.truncs, + repeat(None), repeat(None), range(self.firstFrame, self.firstFrame+self.nFrames)) + else: + # return itertools.izip(self.trans, self.rots, self.states, self.occs, self.truncs, \ + # self.amtOccs, self.amtBorders, xrange(self.firstFrame, self.firstFrame+self.nFrames)) + # Python3 + return zip(self.trans, self.rots, self.states, self.occs, self.truncs, + self.amtOccs, self.amtBorders, range(self.firstFrame, self.firstFrame+self.nFrames)) +# end: class Tracklet def parseXML(trackletFile): - r""" parse tracklet xml file and convert results to list of Tracklet objects - - :param trackletFile: name of a tracklet xml file - :returns: list of Tracklet objects read from xml file - """ - - # convert tracklet XML data to a tree structure - eTree = ElementTree() - # print('parsing tracklet file', trackletFile) - with open(trackletFile) as f: - eTree.parse(f) - - # now convert output to list of Tracklet objects - trackletsElem = eTree.find('tracklets') - tracklets = [] - trackletIdx = 0 - nTracklets = None - for trackletElem in trackletsElem: - # print('track:', trackletElem.tag) - if trackletElem.tag == 'count': - nTracklets = int(trackletElem.text) - # print('file contains', nTracklets, 'tracklets') - elif trackletElem.tag == 'item_version': - pass - elif trackletElem.tag == 'item': - # print('tracklet {0} of {1}'.format(trackletIdx, nTracklets)) - # a tracklet - newTrack = Tracklet() - isFinished = False - hasAmt = False - frameIdx = None - for info in trackletElem: - # print('trackInfo:', info.tag) - if isFinished: - raise ValueError('more info on element after finished!') - if info.tag == 'objectType': - newTrack.objectType = info.text - elif info.tag == 'h': - newTrack.size[0] = float(info.text) - elif info.tag == 'w': - newTrack.size[1] = float(info.text) - elif info.tag == 'l': - newTrack.size[2] = float(info.text) - elif info.tag == 'first_frame': - newTrack.firstFrame = int(info.text) - elif info.tag == 'poses': - # this info is the possibly long list of poses - for pose in info: - # print('trackInfoPose:', pose.tag) - if pose.tag == 'count': # this should come before the others - if newTrack.nFrames is not None: - raise ValueError('there are several pose lists for a single track!') - elif frameIdx is not None: - raise ValueError('?!') - newTrack.nFrames = int(pose.text) - newTrack.trans = np.nan * np.ones((newTrack.nFrames, 3), dtype=float) - newTrack.rots = np.nan * np.ones((newTrack.nFrames, 3), dtype=float) - newTrack.states = np.nan * np.ones(newTrack.nFrames, dtype='uint8') - newTrack.occs = np.nan * np.ones((newTrack.nFrames, 2), dtype='uint8') - newTrack.truncs = np.nan * np.ones(newTrack.nFrames, dtype='uint8') - newTrack.amtOccs = np.nan * np.ones((newTrack.nFrames, 2), dtype=float) - newTrack.amtBorders = np.nan * np.ones((newTrack.nFrames, 3), dtype=float) - frameIdx = 0 - elif pose.tag == 'item_version': - pass - elif pose.tag == 'item': - # pose in one frame - if frameIdx is None: - raise ValueError('pose item came before number of poses!') - for poseInfo in pose: - # print('trackInfoPoseInfo:', poseInfo.tag) - if poseInfo.tag == 'tx': - newTrack.trans[frameIdx, 0] = float(poseInfo.text) - elif poseInfo.tag == 'ty': - newTrack.trans[frameIdx, 1] = float(poseInfo.text) - elif poseInfo.tag == 'tz': - newTrack.trans[frameIdx, 2] = float(poseInfo.text) - elif poseInfo.tag == 'rx': - newTrack.rots[frameIdx, 0] = float(poseInfo.text) - elif poseInfo.tag == 'ry': - newTrack.rots[frameIdx, 1] = float(poseInfo.text) - elif poseInfo.tag == 'rz': - newTrack.rots[frameIdx, 2] = float(poseInfo.text) - elif poseInfo.tag == 'state': - newTrack.states[frameIdx] = stateFromText[poseInfo.text] - elif poseInfo.tag == 'occlusion': - newTrack.occs[frameIdx, 0] = occFromText[poseInfo.text] - elif poseInfo.tag == 'occlusion_kf': - newTrack.occs[frameIdx, 1] = occFromText[poseInfo.text] - elif poseInfo.tag == 'truncation': - newTrack.truncs[frameIdx] = truncFromText[poseInfo.text] - elif poseInfo.tag == 'amt_occlusion': - newTrack.amtOccs[frameIdx,0] = float(poseInfo.text) - hasAmt = True - elif poseInfo.tag == 'amt_occlusion_kf': - newTrack.amtOccs[frameIdx,1] = float(poseInfo.text) - hasAmt = True - elif poseInfo.tag == 'amt_border_l': - newTrack.amtBorders[frameIdx,0] = float(poseInfo.text) - hasAmt = True - elif poseInfo.tag == 'amt_border_r': - newTrack.amtBorders[frameIdx,1] = float(poseInfo.text) - hasAmt = True - elif poseInfo.tag == 'amt_border_kf': - newTrack.amtBorders[frameIdx,2] = float(poseInfo.text) - hasAmt = True + r""" parse tracklet xml file and convert results to list of Tracklet objects + + :param trackletFile: name of a tracklet xml file + :returns: list of Tracklet objects read from xml file + """ + + # convert tracklet XML data to a tree structure + eTree = ElementTree() + # print('parsing tracklet file', trackletFile) + with open(trackletFile) as f: + eTree.parse(f) + + # now convert output to list of Tracklet objects + trackletsElem = eTree.find('tracklets') + tracklets = [] + trackletIdx = 0 + nTracklets = None + for trackletElem in trackletsElem: + # print('track:', trackletElem.tag) + if trackletElem.tag == 'count': + nTracklets = int(trackletElem.text) + # print('file contains', nTracklets, 'tracklets') + elif trackletElem.tag == 'item_version': + pass + elif trackletElem.tag == 'item': + # print('tracklet {0} of {1}'.format(trackletIdx, nTracklets)) + # a tracklet + newTrack = Tracklet() + isFinished = False + hasAmt = False + frameIdx = None + for info in trackletElem: + # print('trackInfo:', info.tag) + if isFinished: + raise ValueError('more info on element after finished!') + if info.tag == 'objectType': + newTrack.objectType = info.text + elif info.tag == 'h': + newTrack.size[0] = float(info.text) + elif info.tag == 'w': + newTrack.size[1] = float(info.text) + elif info.tag == 'l': + newTrack.size[2] = float(info.text) + elif info.tag == 'first_frame': + newTrack.firstFrame = int(info.text) + elif info.tag == 'poses': + # this info is the possibly long list of poses + for pose in info: + # print('trackInfoPose:', pose.tag) + if pose.tag == 'count': # this should come before the others + if newTrack.nFrames is not None: + raise ValueError( + 'there are several pose lists for a single track!') + elif frameIdx is not None: + raise ValueError('?!') + newTrack.nFrames = int(pose.text) + newTrack.trans = np.nan * \ + np.ones((newTrack.nFrames, 3), dtype=float) + newTrack.rots = np.nan * \ + np.ones((newTrack.nFrames, 3), dtype=float) + newTrack.states = np.nan * \ + np.ones(newTrack.nFrames, dtype='uint8') + newTrack.occs = np.nan * \ + np.ones((newTrack.nFrames, 2), dtype='uint8') + newTrack.truncs = np.nan * \ + np.ones(newTrack.nFrames, dtype='uint8') + newTrack.amtOccs = np.nan * \ + np.ones((newTrack.nFrames, 2), dtype=float) + newTrack.amtBorders = np.nan * \ + np.ones((newTrack.nFrames, 3), dtype=float) + frameIdx = 0 + elif pose.tag == 'item_version': + pass + elif pose.tag == 'item': + # pose in one frame + if frameIdx is None: + raise ValueError( + 'pose item came before number of poses!') + for poseInfo in pose: + # print('trackInfoPoseInfo:', poseInfo.tag) + if poseInfo.tag == 'tx': + newTrack.trans[frameIdx, 0] = float( + poseInfo.text) + elif poseInfo.tag == 'ty': + newTrack.trans[frameIdx, 1] = float( + poseInfo.text) + elif poseInfo.tag == 'tz': + newTrack.trans[frameIdx, 2] = float( + poseInfo.text) + elif poseInfo.tag == 'rx': + newTrack.rots[frameIdx, 0] = float( + poseInfo.text) + elif poseInfo.tag == 'ry': + newTrack.rots[frameIdx, 1] = float( + poseInfo.text) + elif poseInfo.tag == 'rz': + newTrack.rots[frameIdx, 2] = float( + poseInfo.text) + elif poseInfo.tag == 'state': + newTrack.states[frameIdx] = stateFromText[poseInfo.text] + elif poseInfo.tag == 'occlusion': + newTrack.occs[frameIdx, + 0] = occFromText[poseInfo.text] + elif poseInfo.tag == 'occlusion_kf': + newTrack.occs[frameIdx, + 1] = occFromText[poseInfo.text] + elif poseInfo.tag == 'truncation': + newTrack.truncs[frameIdx] = truncFromText[poseInfo.text] + elif poseInfo.tag == 'amt_occlusion': + newTrack.amtOccs[frameIdx, 0] = float( + poseInfo.text) + hasAmt = True + elif poseInfo.tag == 'amt_occlusion_kf': + newTrack.amtOccs[frameIdx, 1] = float( + poseInfo.text) + hasAmt = True + elif poseInfo.tag == 'amt_border_l': + newTrack.amtBorders[frameIdx, 0] = float( + poseInfo.text) + hasAmt = True + elif poseInfo.tag == 'amt_border_r': + newTrack.amtBorders[frameIdx, 1] = float( + poseInfo.text) + hasAmt = True + elif poseInfo.tag == 'amt_border_kf': + newTrack.amtBorders[frameIdx, 2] = float( + poseInfo.text) + hasAmt = True + else: + raise ValueError( + 'unexpected tag in poses item: {0}!'.format(poseInfo.tag)) + frameIdx += 1 + else: + raise ValueError( + 'unexpected pose info: {0}!'.format(pose.tag)) + elif info.tag == 'finished': + isFinished = True else: - raise ValueError('unexpected tag in poses item: {0}!'.format(poseInfo.tag)) - frameIdx += 1 - else: - raise ValueError('unexpected pose info: {0}!'.format(pose.tag)) - elif info.tag == 'finished': - isFinished = True + raise ValueError( + 'unexpected tag in tracklets: {0}!'.format(info.tag)) + # end: for all fields in current tracklet + + # some final consistency checks on new tracklet + if not isFinished: + warn('tracklet {0} was not finished!'.format(trackletIdx)) + if newTrack.nFrames is None: + warn('tracklet {0} contains no information!'.format( + trackletIdx)) + elif frameIdx != newTrack.nFrames: + warn('tracklet {0} is supposed to have {1} frames, but perser found {1}!'.format( + trackletIdx, newTrack.nFrames, frameIdx)) + if np.abs(newTrack.rots[:, :2]).sum() > 1e-16: + warn('track contains rotation other than yaw!') + + # if amtOccs / amtBorders are not set, set them to None + if not hasAmt: + newTrack.amtOccs = None + newTrack.amtBorders = None + + # add new tracklet to list + tracklets.append(newTrack) + trackletIdx += 1 + else: - raise ValueError('unexpected tag in tracklets: {0}!'.format(info.tag)) - #end: for all fields in current tracklet - - # some final consistency checks on new tracklet - if not isFinished: - warn('tracklet {0} was not finished!'.format(trackletIdx)) - if newTrack.nFrames is None: - warn('tracklet {0} contains no information!'.format(trackletIdx)) - elif frameIdx != newTrack.nFrames: - warn('tracklet {0} is supposed to have {1} frames, but perser found {1}!'.format(\ - trackletIdx, newTrack.nFrames, frameIdx)) - if np.abs(newTrack.rots[:,:2]).sum() > 1e-16: - warn('track contains rotation other than yaw!') - - # if amtOccs / amtBorders are not set, set them to None - if not hasAmt: - newTrack.amtOccs = None - newTrack.amtBorders = None - - # add new tracklet to list - tracklets.append(newTrack) - trackletIdx += 1 - - else: - raise ValueError('unexpected tracklet info') - #end: for tracklet list items - - # print('loaded', trackletIdx, 'tracklets') - - # final consistency check - if trackletIdx != nTracklets: - warn('according to xml information the file has {0} tracklets, but parser found {1}!'.format(nTracklets, trackletIdx)) - - return tracklets -#end: function parseXML + raise ValueError('unexpected tracklet info') + # end: for tracklet list items + + # print('loaded', trackletIdx, 'tracklets') + + # final consistency check + if trackletIdx != nTracklets: + warn('according to xml information the file has {0} tracklets, but parser found {1}!'.format( + nTracklets, trackletIdx)) + return tracklets +# end: function parseXML diff --git a/tests/datasets_tests/kitti_tests/test_KITTI.py b/tests/datasets_tests/kitti_tests/test_KITTI.py deleted file mode 100644 index e3843b6a29..0000000000 --- a/tests/datasets_tests/kitti_tests/test_KITTI.py +++ /dev/null @@ -1,75 +0,0 @@ -import numpy as np -import os -import shutil -import tempfile -import unittest - -from chainer import testing -from chainer.testing import attr - -from chainercv.datasets import KITTIDataset -from chainercv.datasets.kitti.kitti_utils import kitti_labels -from chainercv.utils import assert_is_semantic_segmentation_dataset -from chainercv.utils.testing.assertions.assert_is_image import assert_is_image -from chainercv.utils import write_image - - -@testing.parameterize( - { - 'date': '2011_09_26', - 'driveNo': '0001', - 'imgNo': '00', - 'sync': True - }, - { - 'date': '2011_09_26', - 'driveNo': '0001', - 'imgNo': '00', - 'sync': False - }, - { - 'date': '2011_09_26', - 'driveNo': '0001', - 'imgNo': '02', - 'sync': True - }, - { - 'date': '2011_09_26', - 'driveNo': '0017', - 'imgNo': '00', - 'sync': True - }, - { - 'date': '2011_09_28', - 'driveNo': '0001', - 'imgNo': '00', - 'sync': True - }, - { - 'date': '2011_10_03', - 'driveNo': '0047', - 'imgNo': '00', - 'sync': True - }, -) - -class TestKITTIDataset(unittest.TestCase): - - def setUp(self): - self.dataset = KITTIDataset( - date=self.date, - driveNo=self.driveNo, - imgNo=self.imgNo, - sync=self.sync) - - - @attr.slow - @attr.disk - def test_kitti_semantic_segmentation_dataset(self): - indices = np.random.permutation(np.arange(len(self.dataset))) - for i in indices[:10]: - img = self.dataset[i] - assert_is_image(img, color=True) - - -testing.run_module(__name__, __file__) diff --git a/tests/datasets_tests/kitti_tests/test_kitti_dataset.py b/tests/datasets_tests/kitti_tests/test_kitti_dataset.py new file mode 100644 index 0000000000..09dbc391c8 --- /dev/null +++ b/tests/datasets_tests/kitti_tests/test_kitti_dataset.py @@ -0,0 +1,74 @@ +import unittest + +from chainer import testing +from chainer.testing import attr + +from chainercv.datasets import KITTI_label_names +from chainercv.datasets import KITTIDataset +from chainercv.utils import assert_is_semantic_segmentation_dataset + + +@testing.parameterize( + { + 'date': '2011_09_26', + 'driveNo': '0001', + 'color': True, + 'sync': True, + 'isLeft': True + }, + { + 'date': '2011_09_26', + 'driveNo': '0001', + 'color': True, + 'sync': False, + 'isLeft': True + }, + { + 'date': '2011_09_26', + 'driveNo': '0001', + 'color': True, + 'sync': True, + 'isLeft': True + }, + { + 'date': '2011_09_26', + 'driveNo': '0017', + 'color': True, + 'sync': True, + 'isLeft': True + }, + { + 'date': '2011_09_28', + 'driveNo': '0001', + 'color': True, + 'sync': True, + 'isLeft': True + }, + { + 'date': '2011_10_03', + 'driveNo': '0047', + 'color': True, + 'sync': True, + 'isLeft': True + }, +) + +class TestKITTIDataset(unittest.TestCase): + + def setUp(self): + self.dataset = KITTIDataset( + date=self.date, + driveNo=self.driveNo, + color=self.color, + sync=self.sync, + isLeft=self.isLeft) + + @attr.slow + def test_kitti_semantic_segmentation_dataset(self): + indices = np.random.permutation(np.arange(len(self.dataset))) + for i in indices[:10]: + img = self.dataset[i] + assert_is_image(img, color=True) + + +testing.run_module(__name__, __file__) From 8c22e4956263705be4d88d885843dcbe602631d7 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Tue, 22 May 2018 11:00:12 +0900 Subject: [PATCH 05/33] Additional processing when using GetterDataset class list() to [] syntax miss(iable -> label) --- chainercv/datasets/kitti/KITTI_dataset.py | 24 +++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/chainercv/datasets/kitti/KITTI_dataset.py b/chainercv/datasets/kitti/KITTI_dataset.py index 719ee2f11e..c9efd51e46 100644 --- a/chainercv/datasets/kitti/KITTI_dataset.py +++ b/chainercv/datasets/kitti/KITTI_dataset.py @@ -151,6 +151,8 @@ class KITTIDataset(GetterDataset): """ def __init__(self, data_dir='auto', date='', driveNo='', color=True, sync=True, isLeft=True): + super(KITTIDataset, self).__init__() + self.color = color self.sync = sync self.isLeft = isLeft @@ -200,13 +202,13 @@ def __init__(self, data_dir='auto', date='', driveNo='', color=True, sync=True, self.bboxes = [0] * length self.labels = [0] * length for idx in range(0, length): - self.bboxes[idx] = list() - self.labels[idx] = list() + self.bboxes[idx] = [] + self.labels[idx] = [] self.get_KITTI_Label(self.tracklets) self.add_getter('img', self._get_image) - self.add_getter('iabel', self._get_label) + self.add_getter('label', self._get_label) self.add_getter('bbox', self._get_bbox) def __len__(self): @@ -230,7 +232,6 @@ def _get_bbox(self, i): bbox = self.bboxes[i] return bbox - def get_KITTI_Tracklets(self, data_root, date, driveNo): # read calibration files kitti_dir = os.path.join(data_root, date) @@ -371,24 +372,23 @@ def project_velo_points_in_img(self, pts3d, T_cam_velo, Rrect, Prect): dataset = KITTIDataset(date='2011_09_26', driveNo='0020', color=True, sync = True) # use pykitti # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True) - print(len(dataset)) - img, bbox, label = dataset[20] + img, label, bbox = dataset[30] # keys returns the names of data - print(dataset.keys) # ('img', 'bbox', 'label') + print(dataset.keys) # ('img', 'label', 'bbox') # we can get an example by [] - img, bbox, label = dataset[0] + # img, label, bbox = dataset[0] # get a view of the first 50 examples view = dataset.slice[:50] - print(len(view)) # 50 + # print(len(view)) # 50 # get a view of image and label - view = dataset.slice[:, ('img', 'label')] + # view = dataset.slice[:, ('img', 'label', 'bbox')] # the view also supports sliceable, so that we can call keys - print(view.keys) # ('img', 'label') + # print(view.keys) # ('img', 'label') # we can get an example by [] - img, label = view[0] + # img, label = view[0] # print(img) # print(img.shape) From fb70fd14926a3afcd9e7ecacc5e05087eecdef10 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Wed, 6 Jun 2018 15:27:50 +0900 Subject: [PATCH 06/33] functions replace lowercase letters rename filename(add _bbox_) rename class(add Bbox) testcode use bbox_assert Fit Bbox to image area bbox, label parameter replace from list to ndarray --- chainercv/datasets/__init__.py | 10 +- chainercv/datasets/kitti/KITTI_dataset.py | 400 ------------------ .../datasets/kitti/kitti_bbox_dataset.py | 215 ++++++++++ chainercv/datasets/kitti/kitti_utils.py | 268 ++++++++++++ chainercv/datasets/kitti/parseTrackletXML.py | 9 +- ..._dataset.py => test_kitti_bbox_dataset.py} | 31 +- 6 files changed, 509 insertions(+), 424 deletions(-) delete mode 100644 chainercv/datasets/kitti/KITTI_dataset.py create mode 100644 chainercv/datasets/kitti/kitti_bbox_dataset.py create mode 100644 chainercv/datasets/kitti/kitti_utils.py rename tests/datasets_tests/kitti_tests/{test_kitti_dataset.py => test_kitti_bbox_dataset.py} (57%) diff --git a/chainercv/datasets/__init__.py b/chainercv/datasets/__init__.py index 7021e390bd..86e221e9ae 100644 --- a/chainercv/datasets/__init__.py +++ b/chainercv/datasets/__init__.py @@ -15,13 +15,13 @@ from chainercv.datasets.cub.cub_label_dataset import CUBLabelDataset # NOQA from chainercv.datasets.cub.cub_point_dataset import CUBPointDataset # NOQA from chainercv.datasets.cub.cub_utils import cub_label_names # NOQA -from chainercv.datasets.kitti.KITTI_dataset import KITTI_ignore_label_color # NOQA -from chainercv.datasets.kitti.KITTI_dataset import KITTI_label_colors # NOQA -from chainercv.datasets.kitti.KITTI_dataset import KITTI_label_names # NOQA -from chainercv.datasets.kitti.KITTI_dataset import KITTIDataset # NOQA -from chainercv.datasets.kitti.parseTrackletXML import parseXML # NOQA from chainercv.datasets.directory_parsing_label_dataset import directory_parsing_label_names # NOQA from chainercv.datasets.directory_parsing_label_dataset import DirectoryParsingLabelDataset # NOQA +from chainercv.datasets.kitti.kitti_bbox_dataset import KITTIBboxDataset # NOQA +from chainercv.datasets.kitti.kitti_utils import kitti_ignore_bbox_label_color # NOQA +from chainercv.datasets.kitti.kitti_utils import kitti_bbox_label_colors # NOQA +from chainercv.datasets.kitti.kitti_utils import kitti_bbox_label_names # NOQA +from chainercv.datasets.kitti.parseTrackletXML import parseXML # NOQA from chainercv.datasets.mixup_soft_label_dataset import MixUpSoftLabelDataset # NOQA from chainercv.datasets.online_products.online_products_dataset import online_products_super_label_names # NOQA from chainercv.datasets.online_products.online_products_dataset import OnlineProductsDataset # NOQA diff --git a/chainercv/datasets/kitti/KITTI_dataset.py b/chainercv/datasets/kitti/KITTI_dataset.py deleted file mode 100644 index c9efd51e46..0000000000 --- a/chainercv/datasets/kitti/KITTI_dataset.py +++ /dev/null @@ -1,400 +0,0 @@ -import glob -import os -from urllib.parse import urljoin - -import numpy as np - -from chainer.dataset import download - -from chainercv.chainer_experimental.datasets.sliceable import GetterDataset -from chainercv import utils -from chainercv.utils.image import read_image - - -# root = 'pfnet/chainercv/KITTI' -url_base = 'http://kitti.is.tue.mpg.de/kitti/raw_data/' - -# use pykitti -import pykitti - -import itertools -# tracklet parser -from chainercv.datasets.kitti import parseTrackletXML as xmlParser - -# check -import matplotlib.pyplot as plt -from chainercv.visualizations import vis_bbox - -# image_shape = 375, 1242 -# KITTI_category_names = ( -# 'City', -# 'Residential', -# 'Road', -# 'Campus', -# 'Person', -# 'Calibration' -# ) - -KITTI_label_names = ( - 'Car', - 'Van', - 'Truck', - 'Pedestrian', - 'Sitter', - 'Cyclist', - 'Tram', - 'Misc', -) - -KITTI_label_colors = ( - (128, 128, 128), - (128, 0, 0), - (192, 192, 128), - (128, 64, 128), - (60, 40, 222), - (128, 128, 0), - (192, 128, 128), - (64, 64, 128), -) -KITTI_ignore_label_color = (0, 0, 0) - - -def get_KITTI_Sync_Data(root, date, driveNo): - data_root = download.get_dataset_directory(root) - - # data - folder = date + '_drive_' + driveNo - url_data = urljoin(url_base, folder+ '/' + folder + '_sync.zip') - - # calibration - url_calib = url_base + date + '_calib.zip' - - # tracklet - url_tracklet = urljoin(url_base, folder+ '/' + folder + '_tracklets.zip') - - download_file_path = utils.cached_download(url_data) - ext = os.path.splitext(url_data)[1] - utils.extractall(download_file_path, data_root, ext) - - download_file_path = utils.cached_download(url_calib) - ext = os.path.splitext(url_calib)[1] - utils.extractall(download_file_path, data_root, ext) - - download_file_path = utils.cached_download(url_tracklet) - ext = os.path.splitext(url_tracklet)[1] - utils.extractall(download_file_path, data_root, ext) - - return data_root - - -def get_KITTI_NoSync_Data(root, date, driveNo): - data_root = download.get_dataset_directory(root) - - # data - folder = date + '_drive_' + driveNo - url_data = urljoin(url_base, folder+ '/' + folder + '_extract.zip') - - # calibration - url_calib = url_base + date + '_calib.zip' - - # tracklet - url_tracklet = urljoin(url_base, folder+ '/' + folder + '_tracklets.zip') - - download_file_path = utils.cached_download(url_data) - ext = os.path.splitext(url_data)[1] - utils.extractall(download_file_path, data_root, ext) - - download_file_path = utils.cached_download(url_calib) - ext = os.path.splitext(url_calib)[1] - utils.extractall(download_file_path, data_root, ext) - - download_file_path = utils.cached_download(url_tracklet) - ext = os.path.splitext(url_tracklet)[1] - utils.extractall(download_file_path, data_root, ext) - - return data_root - - -class KITTIDataset(GetterDataset): - - """Image dataset for test split of `KITTI dataset`_. - - .. _`KITTI dataset`: http://www.cvlibs.net/datasets/kitti/raw_data.php - - .. note:: - - Args: - data_dir (string): Path to the dataset directory. The directory should - contain the :obj:`---` directory. If :obj:`auto` is given, - it uses :obj:`$CHAINER_DATSET_ROOT/pfnet/chainercv/KITTI` by - default. - date ({'2011_09_26', '2011_09_28', '2011_09_29', '2011_09_30', '2011_10_03'}): - reference Calibration datas. - driveNo ({'0xxx'}): get datas drive No. - color (bool): use glay/color image. - sync (bool): get timer sync/nosync data. - isLeft (bool): glay/color image use 2type. - - This dataset returns the following data. - - .. csv-table:: - :header: name, shape, dtype, format - - :obj:`img`, ":math:`(3, H, W)`", :obj:`float32`, \ - "RGB, :math:`[0, 255]`" - :obj:`label`, scalar, :obj:`int32`, ":math:`[0, \#class - 1]`" - :obj:`bbox` [#kitti_bbox_1]_, ":math:`(R, 4)`", :obj:`float32`, \ - ":math:`(y_{min}, x_{min}, y_{max}, x_{max})`" - - .. [#kitti_bbox_1] If :obj:`use_pykitty = False`, \ - :obj:`bbox` and :obj:`label` not contain instances. - """ - - def __init__(self, data_dir='auto', date='', driveNo='', color=True, sync=True, isLeft=True): - super(KITTIDataset, self).__init__() - - self.color = color - self.sync = sync - self.isLeft = isLeft - if data_dir == 'auto': - if sync == True: - # download sync data - data_dir = get_KITTI_Sync_Data(os.path.join('pfnet', 'chainercv', 'KITTI'), date, driveNo) - else: - # download nosync data - data_dir = get_KITTI_NoSync_Data(os.path.join('pfnet', 'chainercv', 'KITTI'), date, driveNo) - - # use pykitti - # read All images - # imformat='None' - # self.dataset = pykitti.raw(data_dir, date, driveNo, frames=None) - self.dataset = pykitti.raw(data_dir, date, driveNo, frames=None, imformat='cv2') - - # current camera calibration R/P settings. - if self.color == True: - if self.isLeft == True: - # img02 - self.cur_R_rect = self.dataset.calib.R_rect_20 - self.cur_P_rect = self.dataset.calib.P_rect_20 - self.imgs = np.array(list(self.dataset.cam2)) - else: - # img03 - self.cur_R_rect = self.dataset.calib.R_rect_30 - self.cur_P_rect = self.dataset.calib.P_rect_30 - self.imgs = np.array(list(self.dataset.cam3)) - else: - if self.isLeft == True: - # img00 - self.cur_R_rect = self.dataset.calib.R_rect_00 - self.cur_P_rect = self.dataset.calib.P_rect_00 - self.imgs = np.array(list(self.dataset.cam0)) - else: - # img01 - self.cur_R_rect = self.dataset.calib.R_rect_10 - self.cur_P_rect = self.dataset.calib.P_rect_10 - self.imgs = np.array(list(self.dataset.cam1)) - - # get object info(type/area/bbox/...) - self.tracklets = self.get_KITTI_Tracklets(data_dir, date, driveNo) - - # set arrays - length = self.__len__() - self.bboxes = [0] * length - self.labels = [0] * length - for idx in range(0, length): - self.bboxes[idx] = [] - self.labels[idx] = [] - - self.get_KITTI_Label(self.tracklets) - - self.add_getter('img', self._get_image) - self.add_getter('label', self._get_label) - self.add_getter('bbox', self._get_bbox) - - def __len__(self): - return len(self.imgs) - - def _get_image(self, i): - img = self.imgs[i] - # convert data is utils.read_image function return values - if img.ndim == 2: - # reshape (H, W) -> (1, H, W) - return img[np.newaxis] - else: - # transpose (H, W, C) -> (C, H, W) - return img.transpose((2, 0, 1)) - - def _get_label(self, i): - label = self.labels[i] - return label - - def _get_bbox(self, i): - bbox = self.bboxes[i] - return bbox - - def get_KITTI_Tracklets(self, data_root, date, driveNo): - # read calibration files - kitti_dir = os.path.join(data_root, date) - # kitti_dir = kitti_dir.replace(os.path.sep, '/') - # calibration_dir = os.path.join(data_root, date) - # self.imu2velo = read_calib_file(os.path.join(kitti_dir, "calib_imu_to_velo.txt")) - # self.velo2cam = read_calib_file(os.path.join(kitti_dir, "calib_velo_to_cam.txt")) - # self.cam2cam = read_calib_file(os.path.join(kitti_dir, "calib_cam_to_cam.txt")) - # read tracklet - folder = date + '_drive_' + driveNo + '_sync' - # self.tracklet = read_tracklet_file(os.path.join(kitti_dir, folder, "calib_imu_to_velo.txt")) - # return tracklets - # get dir names - # read tracklets from file - myTrackletFile = os.path.join(kitti_dir, folder, 'tracklet_labels.xml') - tracklets = xmlParser.parseXML(myTrackletFile) - return tracklets - - - def get_KITTI_Label(self, tracklets): - # twoPi = 2.*np.pi - # loop over tracklets - for iTracklet, tracklet in enumerate(tracklets): - # print('tracklet {0: 3d}: {1}'.format(iTracklet, tracklet)) - - # this part is inspired by kitti object development kit matlab code: computeBox3D - h,w,l = tracklet.size - trackletBox = np.array([ # in velodyne coordinates around zero point and without orientation yet\ - [-l/2, -l/2, l/2, l/2, -l/2, -l/2, l/2, l/2], \ - [ w/2, -w/2, -w/2, w/2, w/2, -w/2, -w/2, w/2], \ - [ 0.0, 0.0, 0.0, 0.0, h, h, h, h]]) - - # print('trackletBox : ' + trackletBox) - # print(trackletBox) - objTypeStr = tracklet.objectType - # print(objTypeStr) - - # loop over all data in tracklet - for translation, rotation, state, occlusion, truncation, amtOcclusion, amtBorders, absoluteFrameNumber \ - in tracklet: - - # determine if object is in the image; otherwise continue - if truncation not in (xmlParser.TRUNC_IN_IMAGE, xmlParser.TRUNC_TRUNCATED): - continue - - # re-create 3D bounding box in velodyne coordinate system - yaw = rotation[2] # other rotations are 0 in all xml files I checked - assert np.abs(rotation[:2]).sum() == 0, 'object rotations other than yaw given!' - rotMat = np.array([\ - [np.cos(yaw), -np.sin(yaw), 0.0], \ - [np.sin(yaw), np.cos(yaw), 0.0], \ - [ 0.0, 0.0, 1.0]]) - cornerPosInVelo = np.dot(rotMat, trackletBox) + np.tile(translation, (8,1)).T - - # calc yaw as seen from the camera (i.e. 0 degree = facing away from cam), as opposed to - # car-centered yaw (i.e. 0 degree = same orientation as car). - # makes quite a difference for objects in periphery! - # Result is in [0, 2pi] - x, y, z = translation - # print(translation) - # yawVisual = ( yaw - np.arctan2(y, x) ) % twoPi - # print(yaw) - # print(yawVisual) - # param = pykitti.utils.transform_from_rot_trans(rotMat, translation) - # print(param) - - # projection to image? - # print(self.dataset.calib.P_rect_20) - # param3 = translation.reshape(3, 1) * self.dataset.calib.P_rect_20 - # print(cornerPosInVelo[:, 0:1].shape) - pt3d = np.vstack((cornerPosInVelo[:,0:8], np.ones(8))) - # print(pt3d.shape) - # print(self.dataset.calib.P_rect_20) - pt2d = self.project_velo_points_in_img(pt3d, self.dataset.calib.T_cam2_velo, self.cur_R_rect, self.cur_P_rect) - - # print(pt2d) - xmin = min(pt2d[0, :]) - xmax = max(pt2d[0, :]) - ymin = min(pt2d[1, :]) - ymax = max(pt2d[1, :]) - param = np.array((ymin, xmin, ymax, xmax)) - # bbox.append(param) - # bbox = np.stack(bbox).astype(np.float32) - # self.bboxes[absoluteFrameNumber] = bbox - self.bboxes[absoluteFrameNumber].append(param) - # print(self.bboxes[absoluteFrameNumber]) - # param_3d = cornerPosInVelo - # self.bboxes_3d[absoluteFrameNumber].append(cornerPosInVelo) - # label.append(param2) - # label = np.stack(label).astype(np.int32) - # self.labels[absoluteFrameNumber] = label - # objectType - # label_names - # not search objTypeStr? process - param2 = KITTI_label_names.index(objTypeStr) - self.labels[absoluteFrameNumber].append(param2) - # print(self.bboxes[absoluteFrameNumber]) - - # end : for all frames in track - # end : for all tracks - - def project_velo_points_in_img(self, pts3d, T_cam_velo, Rrect, Prect): - """Project 3D points into 2D image. Expects pts3d as a 4xN - numpy array. Returns the 2D projection of the points that - are in front of the camera only an the corresponding 3D points.""" - - # 3D points in camera reference frame. - pts3d_cam = Rrect.dot(T_cam_velo.dot(pts3d)) - - # Before projecting, keep only points with z > 0 - # (points that are in fronto of the camera). - idx = (pts3d_cam[2,:] >= 0) - pts2d_cam = Prect.dot(pts3d_cam[:,idx]) - - # return pts3d[:, idx], pts2d_cam / pts2d_cam[2,:] - return pts2d_cam / pts2d_cam[2,:] - - -if __name__ == '__main__': - # 00, 01 : gray - # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = True) - # print(len(d)) - # img = d[0] - # print(img) - # print(img.shape) - # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = True, isLeft=False) - - # print(len(d)) - # img, bbox, = dataset[0] - # print(img) - # print(img.shape) - - # 02, 03 : color - # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True) - # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True, isLeft=False) - # local Folder - # dataset = KITTIDataset(date='2011_09_26', driveNo='0005', color=True, sync = True, isLeft=False) - dataset = KITTIDataset(date='2011_09_26', driveNo='0020', color=True, sync = True) - # use pykitti - # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True) - img, label, bbox = dataset[30] - - # keys returns the names of data - print(dataset.keys) # ('img', 'label', 'bbox') - # we can get an example by [] - # img, label, bbox = dataset[0] - - # get a view of the first 50 examples - view = dataset.slice[:50] - # print(len(view)) # 50 - - # get a view of image and label - # view = dataset.slice[:, ('img', 'label', 'bbox')] - # the view also supports sliceable, so that we can call keys - # print(view.keys) # ('img', 'label') - # we can get an example by [] - # img, label = view[0] - - # print(img) - # print(img.shape) - # Data no Sync - # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = False) - # print(img.debug_print()) - vis_bbox(img, bbox, label, score=None, label_names=KITTI_label_names) - plt.show() - diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py new file mode 100644 index 0000000000..62b595bcf3 --- /dev/null +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -0,0 +1,215 @@ +import numpy as np +import os +# use pykitti<=0.2.4 +import pykitti +import itertools + +from chainercv import utils + +from chainercv.datasets.kitti.kitti_utils import get_kitti_sync_data +from chainercv.datasets.kitti.kitti_utils import get_kitti_nosync_data +from chainercv.datasets.kitti.kitti_utils import get_kitti_tracklets +from chainercv.datasets.kitti.kitti_utils import get_kitti_label + +from chainercv.chainer_experimental.datasets.sliceable import GetterDataset + +# # checkcode(remove) +# # start +# import matplotlib.pyplot as plt +# from chainercv.visualizations import vis_bbox +# # end + +class KITTIBboxDataset(GetterDataset): + + """Image dataset for test split of `KITTI dataset`_. + + .. _`KITTI dataset`: http://www.cvlibs.net/datasets/kitti/raw_data.php + + .. note:: + + Args: + data_dir (string): Path to the dataset directory. The directory should + contain the :obj:`---` directory. If this is + :obj:`auto`, this class will automatically download data for you + under :obj:`$CHAINER_DATASET_ROOT/pfnet/chainercv/kitti`. + date ({'2011_09_26', '2011_09_28', '2011_09_29', + '2011_09_30', '2011_10_03'}): + reference Calibration datas. + driveNo ({'0xxx'}): get datas drive No. + color (bool): use glay/color image. + sync (bool): get timer sync/nosync data. + isLeft (bool): left/right camera image use 2type. + + This dataset returns the following data. + + .. csv-table:: + :header: name, shape, dtype, format + + :obj:`img`, ":math:`(3, H, W)`", :obj:`float32`, \ + "RGB, :math:`[0, 255]`" + :obj:`bbox` [#kitti_bbox_1]_, ":math:`(R, 4)`", :obj:`float32`, \ + ":math:`(y_{min}, x_{min}, y_{max}, x_{max})`" + :obj:`label`, scalar, :obj:`int32`, ":math:`[0, \#class - 1]`" + + .. [#kitti_bbox_1] If :obj:`use_pykitty = False`, \ + :obj:`bbox` and :obj:`label` not contain instances. + """ + + def __init__(self, data_dir='auto', date='', driveNo='', \ + color=True, sync=True, isLeft=True): + super(KITTIBboxDataset, self).__init__() + + self.color = color + self.sync = sync + self.isLeft = isLeft + if data_dir == 'auto': + if sync == True: + # download sync data + data_dir = get_kitti_sync_data(os.path.join( + 'pfnet', 'chainercv', 'KITTI'), date, driveNo) + else: + # download nosync data + data_dir = get_kitti_nosync_data(os.path.join( + 'pfnet', 'chainercv', 'KITTI'), date, driveNo) + + # use pykitti + # read All images + # imformat='None' + # self.dataset = pykitti.raw(data_dir, date, driveNo, frames=None) + self.dataset = pykitti.raw( + data_dir, date, driveNo, frames=None, imformat='cv2') + + # current camera calibration R/P settings. + if self.color == True: + if self.isLeft == True: + # img02 + self.cur_R_rect = self.dataset.calib.R_rect_20 + self.cur_P_rect = self.dataset.calib.P_rect_20 + self.imgs = np.array(list(self.dataset.cam2)) + else: + # img03 + self.cur_R_rect = self.dataset.calib.R_rect_30 + self.cur_P_rect = self.dataset.calib.P_rect_30 + self.imgs = np.array(list(self.dataset.cam3)) + else: + if self.isLeft == True: + # img00 + self.cur_R_rect = self.dataset.calib.R_rect_00 + self.cur_P_rect = self.dataset.calib.P_rect_00 + self.imgs = np.array(list(self.dataset.cam0)) + else: + # img01 + self.cur_R_rect = self.dataset.calib.R_rect_10 + self.cur_P_rect = self.dataset.calib.P_rect_10 + self.imgs = np.array(list(self.dataset.cam1)) + + # get object info(type/area/bbox/...) + self.tracklets = get_kitti_tracklets(data_dir, date, driveNo) + + self.bboxes, self.labels = get_kitti_label(self.tracklets, self.dataset.calib, self.cur_R_rect, self.cur_P_rect, self.__len__()) + + self.add_getter('img', self._get_image) + # self.add_getter('label', self._get_label) + # self.add_getter('bbox', self._get_bbox) + self.add_getter(['bbox', 'label'], self._get_annotations) + keys = ('img', 'bbox', 'label') + self.keys = keys + + def __len__(self): + return len(self.imgs) + + def _get_image(self, i): + img = self.imgs[i] + # convert data is utils.read_image function return values + if img.ndim == 2: + # reshape (H, W) -> (1, H, W) + return img[np.newaxis] + else: + # transpose (H, W, C) -> (C, H, W) + return img.transpose((2, 0, 1)) + + def _get_annotations(self, i): + # List[{'segmentation', 'area', 'iscrowd', + # 'image_id', 'bbox', 'category_id', 'id'}] + bbox = self.bboxes + label = self.labels + + # convert list to ndarray + if len(bbox[i]) == 0: + # bbox[i] = np.zeros((0, 4), dtype=np.float32) + bbox[i] = [[0.0, 0.0, 0.01, 0.01]] + + np_bbox = np.array(bbox[i], dtype=np.float32) + + if len(label[i]) == 0: + # label[i] = np.zeros((0, 1), dtype=np.int32) + label[i] = [0] + + np_label = np.array(label[i], dtype=np.int32) + + print(np_bbox) + print(np_label) + return np_bbox, np_label + +# def _get_label(self, i): +# label = self.labels[i] +# return label +# +# def _get_bbox(self, i): +# bbox = self.bboxes[i] +# return bbox + + +# # checkcode(remove) +# # start +# if __name__ == '__main__': +# # 00, 01 : gray +# # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = True) +# # print(len(d)) +# # img = d[0] +# # print(img) +# # print(img.shape) +# # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = True, isLeft=False) +# +# # print(len(d)) +# # img, bbox, = dataset[0] +# # print(img) +# # print(img.shape) +# +# # 02, 03 : color +# # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True) +# # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True, isLeft=False) +# # local Folder +# # dataset = KITTIDataset(date='2011_09_26', driveNo='0005', color=True, sync = True, isLeft=False) +# dataset = KITTIDataset( +# date='2011_09_26', driveNo='0020', color=True, sync=True) +# # use pykitti +# # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True) +# img, label, bbox = dataset[5] +# +# # keys returns the names of data +# # print(dataset.keys) # ('img', 'label', 'bbox') +# # we can get an example by [] +# # img, label, bbox = dataset[0] +# +# # get a view of the first 50 examples +# view = dataset.slice[:50] +# # print(len(view)) # 50 +# +# # get a view of image and label +# # view = dataset.slice[:, ('img', 'label', 'bbox')] +# # the view also supports sliceable, so that we can call keys +# # print(view.keys) # ('img', 'label') +# # we can get an example by [] +# # img, label = view[0] +# +# # print(img) +# # print(img.shape) +# # Data no Sync +# # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = False) +# # print(img.debug_print()) +# from chainercv.datasets.kitti.kitti_utils import kitti_bbox_label_names +# vis_bbox(img, bbox, label, score=None, label_names=kitti_bbox_label_names) +# plt.show() +# +# # end \ No newline at end of file diff --git a/chainercv/datasets/kitti/kitti_utils.py b/chainercv/datasets/kitti/kitti_utils.py new file mode 100644 index 0000000000..2630be192e --- /dev/null +++ b/chainercv/datasets/kitti/kitti_utils.py @@ -0,0 +1,268 @@ +import numpy as np +import os +from urllib.parse import urljoin + +from chainercv import utils + +from chainer.dataset import download + +# root = 'pfnet/chainercv/kitti' +url_base = 'http://kitti.is.tue.mpg.de/kitti/raw_data/' + +# tracklet parser +from chainercv.datasets.kitti import parseTrackletXML as xmlParser + + +def get_kitti_sync_data(root, date, driveNo): + data_root = download.get_dataset_directory(root) + + # data + folder = date + '_drive_' + driveNo + url_data = urljoin(url_base, folder + '/' + folder + '_sync.zip') + + # calibration + url_calib = url_base + date + '_calib.zip' + + # tracklet + url_tracklet = urljoin(url_base, folder + '/' + folder + '_tracklets.zip') + + download_file_path = utils.cached_download(url_data) + ext = os.path.splitext(url_data)[1] + utils.extractall(download_file_path, data_root, ext) + + download_file_path = utils.cached_download(url_calib) + ext = os.path.splitext(url_calib)[1] + utils.extractall(download_file_path, data_root, ext) + + download_file_path = utils.cached_download(url_tracklet) + ext = os.path.splitext(url_tracklet)[1] + utils.extractall(download_file_path, data_root, ext) + + return data_root + + +def get_kitti_nosync_data(root, date, driveNo): + data_root = download.get_dataset_directory(root) + + # data + folder = date + '_drive_' + driveNo + url_data = urljoin(url_base, folder + '/' + folder + '_extract.zip') + + # calibration + url_calib = url_base + date + '_calib.zip' + + # tracklet + url_tracklet = urljoin(url_base, folder + '/' + folder + '_tracklets.zip') + + download_file_path = utils.cached_download(url_data) + ext = os.path.splitext(url_data)[1] + utils.extractall(download_file_path, data_root, ext) + + download_file_path = utils.cached_download(url_calib) + ext = os.path.splitext(url_calib)[1] + utils.extractall(download_file_path, data_root, ext) + + download_file_path = utils.cached_download(url_tracklet) + ext = os.path.splitext(url_tracklet)[1] + utils.extractall(download_file_path, data_root, ext) + + return data_root + + +def get_kitti_tracklets(data_root, date, driveNo): + # read calibration files + kitti_dir = os.path.join(data_root, date) + # kitti_dir = kitti_dir.replace(os.path.sep, '/') + # calibration_dir = os.path.join(data_root, date) + # imu2velo = read_calib_file(os.path.join(kitti_dir, "calib_imu_to_velo.txt")) + # velo2cam = read_calib_file(os.path.join(kitti_dir, "calib_velo_to_cam.txt")) + # cam2cam = read_calib_file(os.path.join(kitti_dir, "calib_cam_to_cam.txt")) + # read tracklet + folder = date + '_drive_' + driveNo + '_sync' + # tracklet = read_tracklet_file(os.path.join(kitti_dir, folder, "calib_imu_to_velo.txt")) + # return tracklets + # get dir names + # read tracklets from file + myTrackletFile = os.path.join(kitti_dir, folder, 'tracklet_labels.xml') + tracklets = xmlParser.parseXML(myTrackletFile) + return tracklets + + +def get_kitti_label(tracklets, calib, cur_R_rect, cur_P_rect, framelength): + # set list + bboxes = [0] * framelength + labels = [0] * framelength + for idx in range(0, framelength): + bboxes[idx] = [] + labels[idx] = [] + + # set ndarray + # bboxes = np.zeros(framelength, dtype=np.float32) + # labels = np.zeros(framelength, dtype=np.int32) + + # twoPi = 2.*np.pi + # loop over tracklets + for iTracklet, tracklet in enumerate(tracklets): + # print('tracklet {0: 3d}: {1}'.format(iTracklet, tracklet)) + + # this part is inspired by kitti object development kit + # matlab code: computeBox3D + h, w, l = tracklet.size + # in velodyne coordinates around zero point and without orientation yet\ + trackletBox = np.array([ + [-l/2, -l/2, l/2, l/2, -l/2, -l/2, l/2, l/2], \ + [w/2, -w/2, -w/2, w/2, w/2, -w/2, -w/2, w/2], \ + [0.0, 0.0, 0.0, 0.0, h, h, h, h]]) + + # print('trackletBox : ' + trackletBox) + # print(trackletBox) + objTypeStr = tracklet.objectType + # print(objTypeStr) + + # loop over all data in tracklet + for translation, rotation, state, occlusion, truncation, \ + amtOcclusion, amtBorders, absoluteFrameNumber in tracklet: + + # determine if object is in the image; otherwise continue + if truncation not in (xmlParser.TRUNC_IN_IMAGE, \ + xmlParser.TRUNC_TRUNCATED): + continue + + # re-create 3D bounding box in velodyne coordinate system + # other rotations are 0 in all xml files I checked + yaw = rotation[2] + assert np.abs(rotation[:2]).sum( + ) == 0, 'object rotations other than yaw given!' + rotMat = np.array([ + [np.cos(yaw), -np.sin(yaw), 0.0], + [np.sin(yaw), np.cos(yaw), 0.0], + [0.0, 0.0, 1.0]]) + cornerPosInVelo = np.dot( + rotMat, trackletBox) + np.tile(translation, (8, 1)).T + + # calc yaw as seen from the camera + # (i.e. 0 degree = facing away from cam), + # as opposed to car-centered yaw + # (i.e. 0 degree = same orientation as car). + # makes quite a difference for objects in periphery! + # Result is in [0, 2pi] + x, y, z = translation + # print(translation) + # yawVisual = ( yaw - np.arctan2(y, x) ) % twoPi + # print(yaw) + # print(yawVisual) + # param = pykitti.utils.transform_from_rot_trans(rotMat, translation) + # print(param) + + # projection to image? + # print(calib.P_rect_20) + # param3 = translation.reshape(3, 1) * calib.P_rect_20 + # print(cornerPosInVelo[:, 0:1].shape) + pt3d = np.vstack((cornerPosInVelo[:, 0:8], np.ones(8))) + # print(pt3d.shape) + # print(calib.P_rect_20) + pt2d = project_velo_points_in_img( + pt3d, calib.T_cam2_velo, cur_R_rect, cur_P_rect) + + # print(pt2d) + xmin = min(pt2d[0, :]) + xmax = max(pt2d[0, :]) + ymin = min(pt2d[1, :]) + ymax = max(pt2d[1, :]) + if xmin < 0.0: + xmin = 0.0 + if ymin < 0.0: + ymin = 0.0 + if xmax < 0.0: + xmax = 0.0 + if ymax < 0.0: + ymax = 0.0 + + # img_size_x = img_size[0] + # img_size_y = img_size[1] + # image_shape = 375, 1242 + if xmin > 1242.0: + xmin = 1242.0 + if ymin > 375.0: + ymin = 375.0 + if xmax > 1242.0: + xmax = 1242.0 + if ymax > 375.0: + ymax = 375.0 + + param = np.array((ymin, xmin, ymax, xmax), dtype=np.float32) + # print(param) + # bbox.append(param) + # bbox = np.stack(bbox).astype(np.float32) + # bboxes[absoluteFrameNumber] = bbox + bboxes[absoluteFrameNumber].append(param) + # print(bboxes[absoluteFrameNumber]) + + # param_3d = cornerPosInVelo + # bboxes_3d[absoluteFrameNumber].append(cornerPosInVelo) + # label.append(param2) + # label = np.stack(label).astype(np.int32) + # labels[absoluteFrameNumber] = label + # objectType + # label_names + # not search objTypeStr? process + param2 = kitti_bbox_label_names.index(objTypeStr) + labels[absoluteFrameNumber].append(param2) + # labels[absoluteFrameNumber] = param2 + # print(bboxes[absoluteFrameNumber]) + + # end : for all frames in track + # end : for all tracks + + return bboxes, labels + + +def project_velo_points_in_img(pts3d, T_cam_velo, Rrect, Prect): + """Project 3D points into 2D image. Expects pts3d as a 4xN + numpy array. Returns the 2D projection of the points that + are in front of the camera only an the corresponding 3D points.""" + + # 3D points in camera reference frame. + pts3d_cam = Rrect.dot(T_cam_velo.dot(pts3d)) + + # Before projecting, keep only points with z > 0 + # (points that are in fronto of the camera). + idx = (pts3d_cam[2, :] >= 0) + pts2d_cam = Prect.dot(pts3d_cam[:, idx]) + + # return pts3d[:, idx], pts2d_cam / pts2d_cam[2,:] + return pts2d_cam / pts2d_cam[2, :] + + +# image_shape = 375, 1242 +# kitti_category_names = ( +# 'City', +# 'Residential', +# 'Road', +# 'Campus', +# 'Person', +# 'Calibration' +# ) + +kitti_bbox_label_names = ( + 'Car', + 'Van', + 'Truck', + 'Pedestrian', + 'Sitter', + 'Cyclist', + 'Tram', + 'Misc', +) + +kitti_bbox_label_colors = ( + (128, 128, 128), + (128, 0, 0), + (192, 192, 128), + (128, 64, 128), + (60, 40, 222), + (128, 128, 0), + (192, 128, 128), + (64, 64, 128), +) +kitti_ignore_bbox_label_color = (0, 0, 0) diff --git a/chainercv/datasets/kitti/parseTrackletXML.py b/chainercv/datasets/kitti/parseTrackletXML.py index 15b214c8e1..fb1ea915a7 100644 --- a/chainercv/datasets/kitti/parseTrackletXML.py +++ b/chainercv/datasets/kitti/parseTrackletXML.py @@ -20,10 +20,12 @@ """ # Version History: -# 4/7/12 Christian Herdtweck: seems to work with a few random test xml tracklet files; +# 4/7/12 Christian Herdtweck: +# seems to work with a few random test xml tracklet files; # converts file contents to ElementTree and then to list of Tracklet objects; # Tracklet objects have str and iter functions -# 5/7/12 ch: added constants for state, occlusion, truncation and added consistency checks +# 5/7/12 ch: added constants for state, occlusion, +# truncation and added consistency checks # 30/1/14 ch: create example function from example code import sys @@ -61,7 +63,8 @@ class Tracklet(object): Tracklets are created in function parseXML and can most conveniently used as follows: for trackletObj in parseXML(trackletFile): - for translation, rotation, state, occlusion, truncation, amtOcclusion, amtBorders, absoluteFrameNumber in trackletObj: + for translation, rotation, state, occlusion, \ + truncation, amtOcclusion, amtBorders, absoluteFrameNumber in trackletObj: ... your code here ... #end: for all frames #end: for all tracklets diff --git a/tests/datasets_tests/kitti_tests/test_kitti_dataset.py b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py similarity index 57% rename from tests/datasets_tests/kitti_tests/test_kitti_dataset.py rename to tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py index 09dbc391c8..545f4dd04e 100644 --- a/tests/datasets_tests/kitti_tests/test_kitti_dataset.py +++ b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py @@ -1,11 +1,13 @@ import unittest +import numpy as np + from chainer import testing from chainer.testing import attr -from chainercv.datasets import KITTI_label_names -from chainercv.datasets import KITTIDataset -from chainercv.utils import assert_is_semantic_segmentation_dataset +from chainercv.datasets import kitti_bbox_label_names +from chainercv.datasets import KITTIBboxDataset +from chainercv.utils import assert_is_bbox_dataset @testing.parameterize( @@ -52,23 +54,20 @@ 'isLeft': True }, ) - -class TestKITTIDataset(unittest.TestCase): +class TestKITTIBboxDataset(unittest.TestCase): def setUp(self): - self.dataset = KITTIDataset( - date=self.date, - driveNo=self.driveNo, - color=self.color, - sync=self.sync, - isLeft=self.isLeft) + self.dataset = KITTIBboxDataset( + date=self.date, + driveNo=self.driveNo, + color=self.color, + sync=self.sync, + isLeft=self.isLeft) @attr.slow - def test_kitti_semantic_segmentation_dataset(self): - indices = np.random.permutation(np.arange(len(self.dataset))) - for i in indices[:10]: - img = self.dataset[i] - assert_is_image(img, color=True) + def test_kitti_bbox_dataset(self): + assert_is_bbox_dataset( + self.dataset, len(kitti_bbox_label_names), n_example=10) testing.run_module(__name__, __file__) From 7ce5bd6ec782b97d8f4b59ea8f73dd136e42f656 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Wed, 6 Jun 2018 17:02:18 +0900 Subject: [PATCH 07/33] add pykitti import check logic --- .../datasets/kitti/kitti_bbox_dataset.py | 19 +++++++++++++++++-- environment.yml | 1 + 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index 62b595bcf3..85f306bd0d 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -1,7 +1,11 @@ import numpy as np import os -# use pykitti<=0.2.4 -import pykitti +try: + import pykitti + _available = True +except ImportError: + _available = False + import itertools from chainercv import utils @@ -19,6 +23,15 @@ # from chainercv.visualizations import vis_bbox # # end +def _check_available(): + if not _available: + warnings.warn( + 'pykitti is not installed in your environment,' + 'so the dataset cannot be loaded.' + 'Please install pykitti to load dataset.\n\n' + '$ pip install pykitti==0.2.4') + + class KITTIBboxDataset(GetterDataset): """Image dataset for test split of `KITTI dataset`_. @@ -59,6 +72,8 @@ def __init__(self, data_dir='auto', date='', driveNo='', \ color=True, sync=True, isLeft=True): super(KITTIBboxDataset, self).__init__() + _check_available() + self.color = color self.sync = sync self.isLeft = isLeft diff --git a/environment.yml b/environment.yml index 9358bdf031..38c90e7155 100644 --- a/environment.yml +++ b/environment.yml @@ -16,3 +16,4 @@ dependencies: - scipy - pip: - chainermn==1.3.0 + - pykitti==0.2.4 From 5af09ef060143baf38074a1dcb79af3e4e1a62d9 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Wed, 6 Jun 2018 17:39:32 +0900 Subject: [PATCH 08/33] pep8 --- .../datasets/kitti/kitti_bbox_dataset.py | 87 ++----------- chainercv/datasets/kitti/kitti_utils.py | 35 +++--- chainercv/datasets/kitti/parseTrackletXML.py | 115 +++++++++++------- 3 files changed, 103 insertions(+), 134 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index 85f306bd0d..c4bfdebad7 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -17,11 +17,6 @@ from chainercv.chainer_experimental.datasets.sliceable import GetterDataset -# # checkcode(remove) -# # start -# import matplotlib.pyplot as plt -# from chainercv.visualizations import vis_bbox -# # end def _check_available(): if not _available: @@ -46,7 +41,7 @@ class KITTIBboxDataset(GetterDataset): :obj:`auto`, this class will automatically download data for you under :obj:`$CHAINER_DATASET_ROOT/pfnet/chainercv/kitti`. date ({'2011_09_26', '2011_09_28', '2011_09_29', - '2011_09_30', '2011_10_03'}): + '2011_09_30', '2011_10_03'}): reference Calibration datas. driveNo ({'0xxx'}): get datas drive No. color (bool): use glay/color image. @@ -68,8 +63,8 @@ class KITTIBboxDataset(GetterDataset): :obj:`bbox` and :obj:`label` not contain instances. """ - def __init__(self, data_dir='auto', date='', driveNo='', \ - color=True, sync=True, isLeft=True): + def __init__(self, data_dir='auto', date='', driveNo='', + color=True, sync=True, isLeft=True): super(KITTIBboxDataset, self).__init__() _check_available() @@ -78,7 +73,7 @@ def __init__(self, data_dir='auto', date='', driveNo='', \ self.sync = sync self.isLeft = isLeft if data_dir == 'auto': - if sync == True: + if sync is True: # download sync data data_dir = get_kitti_sync_data(os.path.join( 'pfnet', 'chainercv', 'KITTI'), date, driveNo) @@ -95,8 +90,8 @@ def __init__(self, data_dir='auto', date='', driveNo='', \ data_dir, date, driveNo, frames=None, imformat='cv2') # current camera calibration R/P settings. - if self.color == True: - if self.isLeft == True: + if self.color is True: + if self.isLeft is True: # img02 self.cur_R_rect = self.dataset.calib.R_rect_20 self.cur_P_rect = self.dataset.calib.P_rect_20 @@ -107,7 +102,7 @@ def __init__(self, data_dir='auto', date='', driveNo='', \ self.cur_P_rect = self.dataset.calib.P_rect_30 self.imgs = np.array(list(self.dataset.cam3)) else: - if self.isLeft == True: + if self.isLeft is True: # img00 self.cur_R_rect = self.dataset.calib.R_rect_00 self.cur_P_rect = self.dataset.calib.P_rect_00 @@ -121,7 +116,10 @@ def __init__(self, data_dir='auto', date='', driveNo='', \ # get object info(type/area/bbox/...) self.tracklets = get_kitti_tracklets(data_dir, date, driveNo) - self.bboxes, self.labels = get_kitti_label(self.tracklets, self.dataset.calib, self.cur_R_rect, self.cur_P_rect, self.__len__()) + self.bboxes, self.labels = get_kitti_label( + self.tracklets, self.dataset.calib, + self.cur_R_rect, self.cur_P_rect, + self.__len__()) self.add_getter('img', self._get_image) # self.add_getter('label', self._get_label) @@ -165,66 +163,3 @@ def _get_annotations(self, i): print(np_bbox) print(np_label) return np_bbox, np_label - -# def _get_label(self, i): -# label = self.labels[i] -# return label -# -# def _get_bbox(self, i): -# bbox = self.bboxes[i] -# return bbox - - -# # checkcode(remove) -# # start -# if __name__ == '__main__': -# # 00, 01 : gray -# # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = True) -# # print(len(d)) -# # img = d[0] -# # print(img) -# # print(img.shape) -# # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = True, isLeft=False) -# -# # print(len(d)) -# # img, bbox, = dataset[0] -# # print(img) -# # print(img.shape) -# -# # 02, 03 : color -# # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True) -# # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True, isLeft=False) -# # local Folder -# # dataset = KITTIDataset(date='2011_09_26', driveNo='0005', color=True, sync = True, isLeft=False) -# dataset = KITTIDataset( -# date='2011_09_26', driveNo='0020', color=True, sync=True) -# # use pykitti -# # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=True, sync = True) -# img, label, bbox = dataset[5] -# -# # keys returns the names of data -# # print(dataset.keys) # ('img', 'label', 'bbox') -# # we can get an example by [] -# # img, label, bbox = dataset[0] -# -# # get a view of the first 50 examples -# view = dataset.slice[:50] -# # print(len(view)) # 50 -# -# # get a view of image and label -# # view = dataset.slice[:, ('img', 'label', 'bbox')] -# # the view also supports sliceable, so that we can call keys -# # print(view.keys) # ('img', 'label') -# # we can get an example by [] -# # img, label = view[0] -# -# # print(img) -# # print(img.shape) -# # Data no Sync -# # dataset = KITTIDataset(date='2011_09_26', driveNo='0001', color=False, sync = False) -# # print(img.debug_print()) -# from chainercv.datasets.kitti.kitti_utils import kitti_bbox_label_names -# vis_bbox(img, bbox, label, score=None, label_names=kitti_bbox_label_names) -# plt.show() -# -# # end \ No newline at end of file diff --git a/chainercv/datasets/kitti/kitti_utils.py b/chainercv/datasets/kitti/kitti_utils.py index 2630be192e..7f4c23a125 100644 --- a/chainercv/datasets/kitti/kitti_utils.py +++ b/chainercv/datasets/kitti/kitti_utils.py @@ -1,3 +1,6 @@ +# tracklet parser +from chainercv.datasets.kitti import parseTrackletXML as xmlParser + import numpy as np import os from urllib.parse import urljoin @@ -9,9 +12,6 @@ # root = 'pfnet/chainercv/kitti' url_base = 'http://kitti.is.tue.mpg.de/kitti/raw_data/' -# tracklet parser -from chainercv.datasets.kitti import parseTrackletXML as xmlParser - def get_kitti_sync_data(root, date, driveNo): data_root = download.get_dataset_directory(root) @@ -74,12 +74,16 @@ def get_kitti_tracklets(data_root, date, driveNo): kitti_dir = os.path.join(data_root, date) # kitti_dir = kitti_dir.replace(os.path.sep, '/') # calibration_dir = os.path.join(data_root, date) - # imu2velo = read_calib_file(os.path.join(kitti_dir, "calib_imu_to_velo.txt")) - # velo2cam = read_calib_file(os.path.join(kitti_dir, "calib_velo_to_cam.txt")) - # cam2cam = read_calib_file(os.path.join(kitti_dir, "calib_cam_to_cam.txt")) + # imu2velo = read_calib_file( + # os.path.join(kitti_dir, "calib_imu_to_velo.txt")) + # velo2cam = read_calib_file( + # os.path.join(kitti_dir, "calib_velo_to_cam.txt")) + # cam2cam = read_calib_file( + # os.path.join(kitti_dir, "calib_cam_to_cam.txt")) # read tracklet folder = date + '_drive_' + driveNo + '_sync' - # tracklet = read_tracklet_file(os.path.join(kitti_dir, folder, "calib_imu_to_velo.txt")) + # tracklet = read_tracklet_file( + # os.path.join(kitti_dir, folder, "calib_imu_to_velo.txt")) # return tracklets # get dir names # read tracklets from file @@ -108,10 +112,10 @@ def get_kitti_label(tracklets, calib, cur_R_rect, cur_P_rect, framelength): # this part is inspired by kitti object development kit # matlab code: computeBox3D h, w, l = tracklet.size - # in velodyne coordinates around zero point and without orientation yet\ - trackletBox = np.array([ - [-l/2, -l/2, l/2, l/2, -l/2, -l/2, l/2, l/2], \ - [w/2, -w/2, -w/2, w/2, w/2, -w/2, -w/2, w/2], \ + # in velodyne coordinates around zero point and without orientation yet + trackletBox = np.array([ + [-l/2, -l/2, l/2, l/2, -l/2, -l/2, l/2, l/2], + [w/2, -w/2, -w/2, w/2, w/2, -w/2, -w/2, w/2], [0.0, 0.0, 0.0, 0.0, h, h, h, h]]) # print('trackletBox : ' + trackletBox) @@ -121,10 +125,10 @@ def get_kitti_label(tracklets, calib, cur_R_rect, cur_P_rect, framelength): # loop over all data in tracklet for translation, rotation, state, occlusion, truncation, \ - amtOcclusion, amtBorders, absoluteFrameNumber in tracklet: + amtOcclusion, amtBorders, absoluteFrameNumber in tracklet: # determine if object is in the image; otherwise continue - if truncation not in (xmlParser.TRUNC_IN_IMAGE, \ + if truncation not in (xmlParser.TRUNC_IN_IMAGE, xmlParser.TRUNC_TRUNCATED): continue @@ -142,7 +146,7 @@ def get_kitti_label(tracklets, calib, cur_R_rect, cur_P_rect, framelength): # calc yaw as seen from the camera # (i.e. 0 degree = facing away from cam), - # as opposed to car-centered yaw + # as opposed to car-centered yaw # (i.e. 0 degree = same orientation as car). # makes quite a difference for objects in periphery! # Result is in [0, 2pi] @@ -151,7 +155,8 @@ def get_kitti_label(tracklets, calib, cur_R_rect, cur_P_rect, framelength): # yawVisual = ( yaw - np.arctan2(y, x) ) % twoPi # print(yaw) # print(yawVisual) - # param = pykitti.utils.transform_from_rot_trans(rotMat, translation) + # param = pykitti.utils.transform_from_rot_trans( + # rotMat, translation) # print(param) # projection to image? diff --git a/chainercv/datasets/kitti/parseTrackletXML.py b/chainercv/datasets/kitti/parseTrackletXML.py index fb1ea915a7..f0ce171cef 100644 --- a/chainercv/datasets/kitti/parseTrackletXML.py +++ b/chainercv/datasets/kitti/parseTrackletXML.py @@ -58,13 +58,15 @@ class Tracklet(object): - r""" representation an annotated object track + r""" representation an annotated object track - Tracklets are created in function parseXML and can most conveniently used as follows: + Tracklets are created in function parseXML + and can most conveniently used as follows: for trackletObj in parseXML(trackletFile): for translation, rotation, state, occlusion, \ - truncation, amtOcclusion, amtBorders, absoluteFrameNumber in trackletObj: + truncation, amtOcclusion, amtBorders, \ + absoluteFrameNumber in trackletObj: ... your code here ... #end: for all frames #end: for all tracklets @@ -72,9 +74,13 @@ class Tracklet(object): absoluteFrameNumber is in range [firstFrame, firstFrame+nFrames[ amtOcclusion and amtBorders could be None - You can of course also directly access the fields objType (string), size (len-3 ndarray), firstFrame/nFrames (int), - trans/rots (nFrames x 3 float ndarrays), states/truncs (len-nFrames uint8 ndarrays), occs (nFrames x 2 uint8 ndarray), - and for some tracklets amtOccs (nFrames x 2 float ndarray) and amtBorders (nFrames x 3 float ndarray). The last two + You can of course also directly access the fields + objType (string), size (len-3 ndarray), firstFrame/nFrames (int), + trans/rots (nFrames x 3 float ndarrays), + states/truncs (len-nFrames uint8 ndarrays), + occs (nFrames x 2 uint8 ndarray), + and for some tracklets amtOccs (nFrames x 2 float ndarray) + and amtBorders (nFrames x 3 float ndarray). The last two can be None if the xml file did not include these fields in poses """ @@ -98,34 +104,48 @@ def __init__(self): def __str__(self): r""" return human-readable string representation of tracklet object - called implicitly in + called implicitly in # print(trackletObj) - or in + or in text = str(trackletObj) """ - return '[Tracklet over {0} frames for {1}]'.format(self.nFrames, self.objectType) + return '[Tracklet over {0} frames for {1}]'.format( + self.nFrames, self.objectType) def __iter__(self): - r""" returns an iterator that yields tuple of all the available data for each frame + r""" returns an iterator + that yields tuple of all the available data for each frame - called whenever code iterates over a tracklet object, e.g. in - for translation, rotation, state, occlusion, truncation, amtOcclusion, amtBorders, absoluteFrameNumber in trackletObj: + called whenever code iterates over a tracklet object, e.g. in + for translation, rotation, state, occlusion, truncation, + amtOcclusion, amtBorders, absoluteFrameNumber in trackletObj: ...do something ... or trackDataIter = iter(trackletObj) """ if self.amtOccs is None: - # return itertools.izip(self.trans, self.rots, self.states, self.occs, self.truncs, \ - # itertools.repeat(None), itertools.repeat(None), xrange(self.firstFrame, self.firstFrame+self.nFrames)) + # Python2 + # return itertools.izip( + # self.trans, self.rots, self.states, + # self.occs, self.truncs, + # itertools.repeat(None), itertools.repeat(None), + # xrange(self.firstFrame, self.firstFrame+self.nFrames)) # Python3 - return zip(self.trans, self.rots, self.states, self.occs, self.truncs, - repeat(None), repeat(None), range(self.firstFrame, self.firstFrame+self.nFrames)) + return zip(self.trans, self.rots, self.states, + self.occs, self.truncs, + repeat(None), repeat(None), + range(self.firstFrame, self.firstFrame+self.nFrames)) else: - # return itertools.izip(self.trans, self.rots, self.states, self.occs, self.truncs, \ - # self.amtOccs, self.amtBorders, xrange(self.firstFrame, self.firstFrame+self.nFrames)) + # Python2 + # return itertools.izip(self.trans, self.rots, self.states, + # self.occs, self.truncs, + # self.amtOccs, self.amtBorders, + # xrange(self.firstFrame, self.firstFrame+self.nFrames)) # Python3 - return zip(self.trans, self.rots, self.states, self.occs, self.truncs, - self.amtOccs, self.amtBorders, range(self.firstFrame, self.firstFrame+self.nFrames)) + return zip(self.trans, self.rots, self.states, + self.occs, self.truncs, + self.amtOccs, self.amtBorders, + range(self.firstFrame, self.firstFrame+self.nFrames)) # end: class Tracklet @@ -136,6 +156,9 @@ def parseXML(trackletFile): :returns: list of Tracklet objects read from xml file """ + newTrack_nFrames_isNone_ErrorStr = \ + 'there are several pose lists for a single track!' + # convert tracklet XML data to a tree structure eTree = ElementTree() # print('parsing tracklet file', trackletFile) @@ -179,10 +202,11 @@ def parseXML(trackletFile): # this info is the possibly long list of poses for pose in info: # print('trackInfoPose:', pose.tag) - if pose.tag == 'count': # this should come before the others + # this should come before the others + if pose.tag == 'count': if newTrack.nFrames is not None: raise ValueError( - 'there are several pose lists for a single track!') + newTrack_nFrames_isNone_ErrorStr) elif frameIdx is not None: raise ValueError('?!') newTrack.nFrames = int(pose.text) @@ -229,38 +253,41 @@ def parseXML(trackletFile): newTrack.rots[frameIdx, 2] = float( poseInfo.text) elif poseInfo.tag == 'state': - newTrack.states[frameIdx] = stateFromText[poseInfo.text] + newTrack.states[frameIdx] = \ + stateFromText[poseInfo.text] elif poseInfo.tag == 'occlusion': - newTrack.occs[frameIdx, - 0] = occFromText[poseInfo.text] + newTrack.occs[frameIdx, 0] = \ + occFromText[poseInfo.text] elif poseInfo.tag == 'occlusion_kf': - newTrack.occs[frameIdx, - 1] = occFromText[poseInfo.text] + newTrack.occs[frameIdx, 1] = \ + occFromText[poseInfo.text] elif poseInfo.tag == 'truncation': - newTrack.truncs[frameIdx] = truncFromText[poseInfo.text] + newTrack.truncs[frameIdx] = \ + truncFromText[poseInfo.text] elif poseInfo.tag == 'amt_occlusion': - newTrack.amtOccs[frameIdx, 0] = float( - poseInfo.text) + newTrack.amtOccs[frameIdx, 0] = \ + float(poseInfo.text) hasAmt = True elif poseInfo.tag == 'amt_occlusion_kf': - newTrack.amtOccs[frameIdx, 1] = float( - poseInfo.text) + newTrack.amtOccs[frameIdx, 1] = \ + float(poseInfo.text) hasAmt = True elif poseInfo.tag == 'amt_border_l': - newTrack.amtBorders[frameIdx, 0] = float( - poseInfo.text) + newTrack.amtBorders[frameIdx, 0] = \ + float(poseInfo.text) hasAmt = True elif poseInfo.tag == 'amt_border_r': - newTrack.amtBorders[frameIdx, 1] = float( - poseInfo.text) + newTrack.amtBorders[frameIdx, 1] = \ + float(poseInfo.text) hasAmt = True elif poseInfo.tag == 'amt_border_kf': - newTrack.amtBorders[frameIdx, 2] = float( - poseInfo.text) + newTrack.amtBorders[frameIdx, 2] = \ + float(poseInfo.text) hasAmt = True else: raise ValueError( - 'unexpected tag in poses item: {0}!'.format(poseInfo.tag)) + 'unexpected tag in poses item: {0}!' + .format(poseInfo.tag)) frameIdx += 1 else: raise ValueError( @@ -279,8 +306,10 @@ def parseXML(trackletFile): warn('tracklet {0} contains no information!'.format( trackletIdx)) elif frameIdx != newTrack.nFrames: - warn('tracklet {0} is supposed to have {1} frames, but perser found {1}!'.format( - trackletIdx, newTrack.nFrames, frameIdx)) + warn( + 'tracklet {0} is supposed to have {1} frames, \ + but perser found {1}!'.format( + trackletIdx, newTrack.nFrames, frameIdx)) if np.abs(newTrack.rots[:, :2]).sum() > 1e-16: warn('track contains rotation other than yaw!') @@ -301,8 +330,8 @@ def parseXML(trackletFile): # final consistency check if trackletIdx != nTracklets: - warn('according to xml information the file has {0} tracklets, but parser found {1}!'.format( - nTracklets, trackletIdx)) + warn('according to xml information the file has {0} tracklets, \ + but parser found {1}!'.format(nTracklets, trackletIdx)) return tracklets # end: function parseXML From 42acd2b56cc0f4f400e6c638803e3abab2970019 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Thu, 7 Jun 2018 16:32:09 +0900 Subject: [PATCH 09/33] flake8 --- chainercv/datasets/__init__.py | 2 +- .../datasets/kitti/kitti_bbox_dataset.py | 9 ++--- chainercv/datasets/kitti/kitti_utils.py | 8 ++-- chainercv/datasets/kitti/parseTrackletXML.py | 37 +++++++++---------- .../kitti_tests/test_kitti_bbox_dataset.py | 2 - 5 files changed, 26 insertions(+), 32 deletions(-) diff --git a/chainercv/datasets/__init__.py b/chainercv/datasets/__init__.py index 86e221e9ae..b760346c38 100644 --- a/chainercv/datasets/__init__.py +++ b/chainercv/datasets/__init__.py @@ -18,9 +18,9 @@ from chainercv.datasets.directory_parsing_label_dataset import directory_parsing_label_names # NOQA from chainercv.datasets.directory_parsing_label_dataset import DirectoryParsingLabelDataset # NOQA from chainercv.datasets.kitti.kitti_bbox_dataset import KITTIBboxDataset # NOQA -from chainercv.datasets.kitti.kitti_utils import kitti_ignore_bbox_label_color # NOQA from chainercv.datasets.kitti.kitti_utils import kitti_bbox_label_colors # NOQA from chainercv.datasets.kitti.kitti_utils import kitti_bbox_label_names # NOQA +from chainercv.datasets.kitti.kitti_utils import kitti_ignore_bbox_label_color # NOQA from chainercv.datasets.kitti.parseTrackletXML import parseXML # NOQA from chainercv.datasets.mixup_soft_label_dataset import MixUpSoftLabelDataset # NOQA from chainercv.datasets.online_products.online_products_dataset import online_products_super_label_names # NOQA diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index c4bfdebad7..294cf18738 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -1,19 +1,16 @@ import numpy as np import os +import warnings try: import pykitti _available = True except ImportError: _available = False -import itertools - -from chainercv import utils - -from chainercv.datasets.kitti.kitti_utils import get_kitti_sync_data +from chainercv.datasets.kitti.kitti_utils import get_kitti_label from chainercv.datasets.kitti.kitti_utils import get_kitti_nosync_data +from chainercv.datasets.kitti.kitti_utils import get_kitti_sync_data from chainercv.datasets.kitti.kitti_utils import get_kitti_tracklets -from chainercv.datasets.kitti.kitti_utils import get_kitti_label from chainercv.chainer_experimental.datasets.sliceable import GetterDataset diff --git a/chainercv/datasets/kitti/kitti_utils.py b/chainercv/datasets/kitti/kitti_utils.py index 7f4c23a125..a5960fe731 100644 --- a/chainercv/datasets/kitti/kitti_utils.py +++ b/chainercv/datasets/kitti/kitti_utils.py @@ -223,9 +223,11 @@ def get_kitti_label(tracklets, calib, cur_R_rect, cur_P_rect, framelength): def project_velo_points_in_img(pts3d, T_cam_velo, Rrect, Prect): - """Project 3D points into 2D image. Expects pts3d as a 4xN - numpy array. Returns the 2D projection of the points that - are in front of the camera only an the corresponding 3D points.""" + """ + Project 3D points into 2D image. Expects pts3d as a 4xN + numpy array. Returns the 2D projection of the points that + are in front of the camera only an the corresponding 3D points. + """ # 3D points in camera reference frame. pts3d_cam = Rrect.dot(T_cam_velo.dot(pts3d)) diff --git a/chainercv/datasets/kitti/parseTrackletXML.py b/chainercv/datasets/kitti/parseTrackletXML.py index f0ce171cef..c10abe9ee1 100644 --- a/chainercv/datasets/kitti/parseTrackletXML.py +++ b/chainercv/datasets/kitti/parseTrackletXML.py @@ -28,12 +28,9 @@ # truncation and added consistency checks # 30/1/14 ch: create example function from example code -import sys - -from sys import argv as cmdLineArgs from xml.etree.ElementTree import ElementTree -import numpy as np import itertools +import numpy as np from warnings import warn STATE_UNSET = 0 @@ -75,13 +72,13 @@ class Tracklet(object): amtOcclusion and amtBorders could be None You can of course also directly access the fields - objType (string), size (len-3 ndarray), firstFrame/nFrames (int), - trans/rots (nFrames x 3 float ndarrays), - states/truncs (len-nFrames uint8 ndarrays), - occs (nFrames x 2 uint8 ndarray), - and for some tracklets amtOccs (nFrames x 2 float ndarray) - and amtBorders (nFrames x 3 float ndarray). The last two - can be None if the xml file did not include these fields in poses + objType (string), size (len-3 ndarray), firstFrame/nFrames (int), + trans/rots (nFrames x 3 float ndarrays), + states/truncs (len-nFrames uint8 ndarrays), + occs (nFrames x 2 uint8 ndarray), + and for some tracklets amtOccs (nFrames x 2 float ndarray) + and amtBorders (nFrames x 3 float ndarray). The last two + can be None if the xml file did not include these fields in poses """ objectType = None @@ -103,7 +100,6 @@ def __init__(self): def __str__(self): r""" return human-readable string representation of tracklet object - called implicitly in # print(trackletObj) or in @@ -124,23 +120,26 @@ def __iter__(self): trackDataIter = iter(trackletObj) """ if self.amtOccs is None: - # Python2 + # Python2/3 # return itertools.izip( # self.trans, self.rots, self.states, # self.occs, self.truncs, # itertools.repeat(None), itertools.repeat(None), - # xrange(self.firstFrame, self.firstFrame+self.nFrames)) - # Python3 + # range(self.firstFrame, self.firstFrame+self.nFrames)) + # xrange(self.firstFrame, self.firstFrame+self.nFrames)) + Python3 return zip(self.trans, self.rots, self.states, self.occs, self.truncs, repeat(None), repeat(None), range(self.firstFrame, self.firstFrame+self.nFrames)) else: - # Python2 - # return itertools.izip(self.trans, self.rots, self.states, + # Python2/3 + # return itertools.izip( + # self.trans, self.rots, self.states, # self.occs, self.truncs, # self.amtOccs, self.amtBorders, - # xrange(self.firstFrame, self.firstFrame+self.nFrames)) + # range(self.firstFrame, self.firstFrame+self.nFrames)) + # xrange(self.firstFrame, self.firstFrame+self.nFrames)) # Python3 return zip(self.trans, self.rots, self.states, self.occs, self.truncs, @@ -151,11 +150,9 @@ def __iter__(self): def parseXML(trackletFile): r""" parse tracklet xml file and convert results to list of Tracklet objects - :param trackletFile: name of a tracklet xml file :returns: list of Tracklet objects read from xml file """ - newTrack_nFrames_isNone_ErrorStr = \ 'there are several pose lists for a single track!' diff --git a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py index 545f4dd04e..e51613d791 100644 --- a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py +++ b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py @@ -1,7 +1,5 @@ import unittest -import numpy as np - from chainer import testing from chainer.testing import attr From 4c634969bd6ac72f15b36b89cfeabc8ef22f46b1 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Fri, 8 Jun 2018 17:37:52 +0900 Subject: [PATCH 10/33] flake8(docstring) --- chainercv/datasets/kitti/kitti_utils.py | 7 +- chainercv/datasets/kitti/parseTrackletXML.py | 89 ++++++++++--------- .../kitti_tests/test_kitti_bbox_dataset.py | 2 +- 3 files changed, 50 insertions(+), 48 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_utils.py b/chainercv/datasets/kitti/kitti_utils.py index a5960fe731..081c823074 100644 --- a/chainercv/datasets/kitti/kitti_utils.py +++ b/chainercv/datasets/kitti/kitti_utils.py @@ -223,12 +223,11 @@ def get_kitti_label(tracklets, calib, cur_R_rect, cur_P_rect, framelength): def project_velo_points_in_img(pts3d, T_cam_velo, Rrect, Prect): - """ - Project 3D points into 2D image. Expects pts3d as a 4xN - numpy array. Returns the 2D projection of the points that + """Project 3D points into 2D image. Expects pts3d as a 4xN numpy array. + + Returns the 2D projection of the points that are in front of the camera only an the corresponding 3D points. """ - # 3D points in camera reference frame. pts3d_cam = Rrect.dot(T_cam_velo.dot(pts3d)) diff --git a/chainercv/datasets/kitti/parseTrackletXML.py b/chainercv/datasets/kitti/parseTrackletXML.py index c10abe9ee1..950550039e 100644 --- a/chainercv/datasets/kitti/parseTrackletXML.py +++ b/chainercv/datasets/kitti/parseTrackletXML.py @@ -1,22 +1,20 @@ -#!/usr/bin/env python -""" -parse XML files containing tracklet info for kitti data base (raw data section) -(http://cvlibs.net/datasets/kitti/raw_data.php) +"""Parse XML files containing tracklet info for kitti data base. + (http://cvlibs.net/datasets/kitti/raw_data.php) -No guarantees that this code is correct, usage is at your own risk! + No guarantees that this code is correct, usage is at your own risk! -created by Christian Herdtweck, Max Planck Institute for Biological Cybernetics - (christian.herdtweck@tuebingen.mpg.de) + created by Christian Herdtweck, Max Planck Institute for Biological Cybernetics + (christian.herdtweck@tuebingen.mpg.de) -requires numpy! + requires numpy! -example usage: - import parseTrackletXML as xmlParser - kittiDir = '/path/to/kitti/data' - drive = '2011_09_26_drive_0001' - xmlParser.example(kittiDir, drive) -or simply on command line: - python parseTrackletXML.py + example usage: + import parseTrackletXML as xmlParser + kittiDir = '/path/to/kitti/data' + drive = '2011_09_26_drive_0001' + xmlParser.example(kittiDir, drive) + or simply on command line: + python parseTrackletXML.py """ # Version History: @@ -55,7 +53,7 @@ class Tracklet(object): - r""" representation an annotated object track + r"""Representation an annotated object track. Tracklets are created in function parseXML and can most conveniently used as follows: @@ -77,8 +75,9 @@ class Tracklet(object): states/truncs (len-nFrames uint8 ndarrays), occs (nFrames x 2 uint8 ndarray), and for some tracklets amtOccs (nFrames x 2 float ndarray) - and amtBorders (nFrames x 3 float ndarray). The last two - can be None if the xml file did not include these fields in poses + and amtBorders (nFrames x 3 float ndarray). + The last two can be None if the xml file + did not include these fields in poses """ objectType = None @@ -95,11 +94,12 @@ class Tracklet(object): nFrames = None def __init__(self): - r""" create Tracklet with no info set """ + """Create Tracklet with no info set.""" self.size = np.nan*np.ones(3, dtype=float) def __str__(self): - r""" return human-readable string representation of tracklet object + """Return human-readable string representation of tracklet object. + called implicitly in # print(trackletObj) or in @@ -109,8 +109,9 @@ def __str__(self): self.nFrames, self.objectType) def __iter__(self): - r""" returns an iterator - that yields tuple of all the available data for each frame + """Returns an iterator. + + that yields tuple of all the available data for each frame called whenever code iterates over a tracklet object, e.g. in for translation, rotation, state, occlusion, truncation, @@ -121,38 +122,40 @@ def __iter__(self): """ if self.amtOccs is None: # Python2/3 - # return itertools.izip( - # self.trans, self.rots, self.states, - # self.occs, self.truncs, - # itertools.repeat(None), itertools.repeat(None), - # range(self.firstFrame, self.firstFrame+self.nFrames)) + return zip( + self.trans, self.rots, self.states, + self.occs, self.truncs, + itertools.repeat(None), itertools.repeat(None), + range(self.firstFrame, self.firstFrame+self.nFrames)) # xrange(self.firstFrame, self.firstFrame+self.nFrames)) - Python3 - return zip(self.trans, self.rots, self.states, - self.occs, self.truncs, - repeat(None), repeat(None), - range(self.firstFrame, self.firstFrame+self.nFrames)) + # tmpAmtOccs = repeat(None) + # tmpAmtBorders = repeat(None) + # return zip(self.trans, self.rots, self.states, + # self.occs, self.truncs, + # tmpAmtOccs, tmpAmtBorders, + # range(self.firstFrame, self.firstFrame + self.nFrames)) else: # Python2/3 - # return itertools.izip( - # self.trans, self.rots, self.states, - # self.occs, self.truncs, - # self.amtOccs, self.amtBorders, - # range(self.firstFrame, self.firstFrame+self.nFrames)) + return zip( + self.trans, self.rots, self.states, + self.occs, self.truncs, + self.amtOccs, self.amtBorders, + range(self.firstFrame, self.firstFrame + self.nFrames)) # xrange(self.firstFrame, self.firstFrame+self.nFrames)) - # Python3 - return zip(self.trans, self.rots, self.states, - self.occs, self.truncs, - self.amtOccs, self.amtBorders, - range(self.firstFrame, self.firstFrame+self.nFrames)) + # return zip(self.trans, self.rots, self.states, + # self.occs, self.truncs, + # self.amtOccs, self.amtBorders, + # range(self.firstFrame, self.firstFrame + self.nFrames)) # end: class Tracklet def parseXML(trackletFile): - r""" parse tracklet xml file and convert results to list of Tracklet objects + r"""Parse tracklet xml file and convert results to list of Tracklet objects. + :param trackletFile: name of a tracklet xml file :returns: list of Tracklet objects read from xml file """ + newTrack_nFrames_isNone_ErrorStr = \ 'there are several pose lists for a single track!' diff --git a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py index e51613d791..72c6406c1f 100644 --- a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py +++ b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py @@ -28,7 +28,7 @@ 'driveNo': '0001', 'color': True, 'sync': True, - 'isLeft': True + 'isLeft': False }, { 'date': '2011_09_26', From 55f1f8a0399737886cc6e49ec5f0d06dbc498e66 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Mon, 11 Jun 2018 12:19:31 +0900 Subject: [PATCH 11/33] flake8(docstyle) --- .../datasets/kitti/kitti_bbox_dataset.py | 51 ++-- chainercv/datasets/kitti/kitti_utils.py | 76 +++--- chainercv/datasets/kitti/parseTrackletXML.py | 234 +++++++++--------- 3 files changed, 183 insertions(+), 178 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index 294cf18738..730d01c565 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -1,19 +1,19 @@ -import numpy as np import os import warnings + +import numpy as np try: import pykitti _available = True except ImportError: _available = False +from chainercv.chainer_experimental.datasets.sliceable import GetterDataset from chainercv.datasets.kitti.kitti_utils import get_kitti_label from chainercv.datasets.kitti.kitti_utils import get_kitti_nosync_data from chainercv.datasets.kitti.kitti_utils import get_kitti_sync_data from chainercv.datasets.kitti.kitti_utils import get_kitti_tracklets -from chainercv.chainer_experimental.datasets.sliceable import GetterDataset - def _check_available(): if not _available: @@ -25,8 +25,7 @@ def _check_available(): class KITTIBboxDataset(GetterDataset): - - """Image dataset for test split of `KITTI dataset`_. + r"""Image dataset for test split of `KITTI dataset`_. .. _`KITTI dataset`: http://www.cvlibs.net/datasets/kitti/raw_data.php @@ -40,10 +39,10 @@ class KITTIBboxDataset(GetterDataset): date ({'2011_09_26', '2011_09_28', '2011_09_29', '2011_09_30', '2011_10_03'}): reference Calibration datas. - driveNo ({'0xxx'}): get datas drive No. + drive_num ({'0xxx'}): get datas drive No. color (bool): use glay/color image. sync (bool): get timer sync/nosync data. - isLeft (bool): left/right camera image use 2type. + is_left (bool): left/right camera image use 2type. This dataset returns the following data. @@ -60,62 +59,62 @@ class KITTIBboxDataset(GetterDataset): :obj:`bbox` and :obj:`label` not contain instances. """ - def __init__(self, data_dir='auto', date='', driveNo='', - color=True, sync=True, isLeft=True): + def __init__(self, data_dir='auto', date='', drive_num='', + color=True, sync=True, is_left=True): super(KITTIBboxDataset, self).__init__() _check_available() self.color = color self.sync = sync - self.isLeft = isLeft + self.is_left = is_left if data_dir == 'auto': if sync is True: # download sync data data_dir = get_kitti_sync_data(os.path.join( - 'pfnet', 'chainercv', 'KITTI'), date, driveNo) + 'pfnet', 'chainercv', 'KITTI'), date, drive_num) else: # download nosync data data_dir = get_kitti_nosync_data(os.path.join( - 'pfnet', 'chainercv', 'KITTI'), date, driveNo) + 'pfnet', 'chainercv', 'KITTI'), date, drive_num) # use pykitti # read All images # imformat='None' - # self.dataset = pykitti.raw(data_dir, date, driveNo, frames=None) + # self.dataset = pykitti.raw(data_dir, date, drive_num, frames=None) self.dataset = pykitti.raw( - data_dir, date, driveNo, frames=None, imformat='cv2') + data_dir, date, drive_num, frames=None, imformat='cv2') # current camera calibration R/P settings. if self.color is True: - if self.isLeft is True: + if self.is_left is True: # img02 - self.cur_R_rect = self.dataset.calib.R_rect_20 - self.cur_P_rect = self.dataset.calib.P_rect_20 + self.cur_rotation_matrix = self.dataset.calib.R_rect_20 + self.cur_position_matrix = self.dataset.calib.P_rect_20 self.imgs = np.array(list(self.dataset.cam2)) else: # img03 - self.cur_R_rect = self.dataset.calib.R_rect_30 - self.cur_P_rect = self.dataset.calib.P_rect_30 + self.cur_rotation_matrix = self.dataset.calib.R_rect_30 + self.cur_position_matrix = self.dataset.calib.P_rect_30 self.imgs = np.array(list(self.dataset.cam3)) else: - if self.isLeft is True: + if self.is_left is True: # img00 - self.cur_R_rect = self.dataset.calib.R_rect_00 - self.cur_P_rect = self.dataset.calib.P_rect_00 + self.cur_rotation_matrix = self.dataset.calib.R_rect_00 + self.cur_position_matrix = self.dataset.calib.P_rect_00 self.imgs = np.array(list(self.dataset.cam0)) else: # img01 - self.cur_R_rect = self.dataset.calib.R_rect_10 - self.cur_P_rect = self.dataset.calib.P_rect_10 + self.cur_rotation_matrix = self.dataset.calib.R_rect_10 + self.cur_position_matrix = self.dataset.calib.P_rect_10 self.imgs = np.array(list(self.dataset.cam1)) # get object info(type/area/bbox/...) - self.tracklets = get_kitti_tracklets(data_dir, date, driveNo) + self.tracklets = get_kitti_tracklets(data_dir, date, drive_num) self.bboxes, self.labels = get_kitti_label( self.tracklets, self.dataset.calib, - self.cur_R_rect, self.cur_P_rect, + self.cur_rotation_matrix, self.cur_position_matrix, self.__len__()) self.add_getter('img', self._get_image) diff --git a/chainercv/datasets/kitti/kitti_utils.py b/chainercv/datasets/kitti/kitti_utils.py index 081c823074..aee519ec43 100644 --- a/chainercv/datasets/kitti/kitti_utils.py +++ b/chainercv/datasets/kitti/kitti_utils.py @@ -1,23 +1,21 @@ -# tracklet parser -from chainercv.datasets.kitti import parseTrackletXML as xmlParser - -import numpy as np import os from urllib.parse import urljoin -from chainercv import utils - from chainer.dataset import download +import numpy as np + +from chainercv import utils +from chainercv.datasets.kitti import parseTrackletXML as xmlParser # root = 'pfnet/chainercv/kitti' url_base = 'http://kitti.is.tue.mpg.de/kitti/raw_data/' -def get_kitti_sync_data(root, date, driveNo): +def get_kitti_sync_data(root, date, drive_num): data_root = download.get_dataset_directory(root) # data - folder = date + '_drive_' + driveNo + folder = date + '_drive_' + drive_num url_data = urljoin(url_base, folder + '/' + folder + '_sync.zip') # calibration @@ -41,11 +39,11 @@ def get_kitti_sync_data(root, date, driveNo): return data_root -def get_kitti_nosync_data(root, date, driveNo): +def get_kitti_nosync_data(root, date, drive_num): data_root = download.get_dataset_directory(root) # data - folder = date + '_drive_' + driveNo + folder = date + '_drive_' + drive_num url_data = urljoin(url_base, folder + '/' + folder + '_extract.zip') # calibration @@ -69,7 +67,7 @@ def get_kitti_nosync_data(root, date, driveNo): return data_root -def get_kitti_tracklets(data_root, date, driveNo): +def get_kitti_tracklets(data_root, date, drive_num): # read calibration files kitti_dir = os.path.join(data_root, date) # kitti_dir = kitti_dir.replace(os.path.sep, '/') @@ -81,18 +79,21 @@ def get_kitti_tracklets(data_root, date, driveNo): # cam2cam = read_calib_file( # os.path.join(kitti_dir, "calib_cam_to_cam.txt")) # read tracklet - folder = date + '_drive_' + driveNo + '_sync' + folder = date + '_drive_' + drive_num + '_sync' # tracklet = read_tracklet_file( # os.path.join(kitti_dir, folder, "calib_imu_to_velo.txt")) # return tracklets # get dir names # read tracklets from file - myTrackletFile = os.path.join(kitti_dir, folder, 'tracklet_labels.xml') - tracklets = xmlParser.parseXML(myTrackletFile) + tracklet_filepath = os.path.join(kitti_dir, folder, 'tracklet_labels.xml') + tracklets = xmlParser.parseXML(tracklet_filepath) return tracklets -def get_kitti_label(tracklets, calib, cur_R_rect, cur_P_rect, framelength): +def get_kitti_label( + tracklets, calib, + cur_rotation_matrix, cur_position_matrix, + framelength): # set list bboxes = [0] * framelength labels = [0] * framelength @@ -113,15 +114,15 @@ def get_kitti_label(tracklets, calib, cur_R_rect, cur_P_rect, framelength): # matlab code: computeBox3D h, w, l = tracklet.size # in velodyne coordinates around zero point and without orientation yet - trackletBox = np.array([ + tracklet_box = np.array([ [-l/2, -l/2, l/2, l/2, -l/2, -l/2, l/2, l/2], [w/2, -w/2, -w/2, w/2, w/2, -w/2, -w/2, w/2], [0.0, 0.0, 0.0, 0.0, h, h, h, h]]) - # print('trackletBox : ' + trackletBox) - # print(trackletBox) - objTypeStr = tracklet.objectType - # print(objTypeStr) + # print('tracklet_box : ' + tracklet_box) + # print(tracklet_box) + objtype_str = tracklet.objectType + # print(objtype_str) # loop over all data in tracklet for translation, rotation, state, occlusion, truncation, \ @@ -137,12 +138,12 @@ def get_kitti_label(tracklets, calib, cur_R_rect, cur_P_rect, framelength): yaw = rotation[2] assert np.abs(rotation[:2]).sum( ) == 0, 'object rotations other than yaw given!' - rotMat = np.array([ + rot_mat = np.array([ [np.cos(yaw), -np.sin(yaw), 0.0], [np.sin(yaw), np.cos(yaw), 0.0], [0.0, 0.0, 1.0]]) - cornerPosInVelo = np.dot( - rotMat, trackletBox) + np.tile(translation, (8, 1)).T + cornerpos_in_velo = np.dot( + rot_mat, tracklet_box) + np.tile(translation, (8, 1)).T # calc yaw as seen from the camera # (i.e. 0 degree = facing away from cam), @@ -156,18 +157,19 @@ def get_kitti_label(tracklets, calib, cur_R_rect, cur_P_rect, framelength): # print(yaw) # print(yawVisual) # param = pykitti.utils.transform_from_rot_trans( - # rotMat, translation) + # rot_mat, translation) # print(param) # projection to image? # print(calib.P_rect_20) # param3 = translation.reshape(3, 1) * calib.P_rect_20 - # print(cornerPosInVelo[:, 0:1].shape) - pt3d = np.vstack((cornerPosInVelo[:, 0:8], np.ones(8))) + # print(cornerpos_in_velo[:, 0:1].shape) + pt3d = np.vstack((cornerpos_in_velo[:, 0:8], np.ones(8))) # print(pt3d.shape) # print(calib.P_rect_20) pt2d = project_velo_points_in_img( - pt3d, calib.T_cam2_velo, cur_R_rect, cur_P_rect) + pt3d, calib.T_cam2_velo, + cur_rotation_matrix, cur_position_matrix) # print(pt2d) xmin = min(pt2d[0, :]) @@ -203,15 +205,15 @@ def get_kitti_label(tracklets, calib, cur_R_rect, cur_P_rect, framelength): bboxes[absoluteFrameNumber].append(param) # print(bboxes[absoluteFrameNumber]) - # param_3d = cornerPosInVelo - # bboxes_3d[absoluteFrameNumber].append(cornerPosInVelo) + # param_3d = cornerpos_in_velo + # bboxes_3d[absoluteFrameNumber].append(cornerpos_in_velo) # label.append(param2) # label = np.stack(label).astype(np.int32) # labels[absoluteFrameNumber] = label # objectType # label_names - # not search objTypeStr? process - param2 = kitti_bbox_label_names.index(objTypeStr) + # not search objtype_str? process + param2 = kitti_bbox_label_names.index(objtype_str) labels[absoluteFrameNumber].append(param2) # labels[absoluteFrameNumber] = param2 # print(bboxes[absoluteFrameNumber]) @@ -222,19 +224,21 @@ def get_kitti_label(tracklets, calib, cur_R_rect, cur_P_rect, framelength): return bboxes, labels -def project_velo_points_in_img(pts3d, T_cam_velo, Rrect, Prect): - """Project 3D points into 2D image. Expects pts3d as a 4xN numpy array. - +def project_velo_points_in_img( + pts3d, transform_cam_velo, + rotaion_matrix, position_matrix): + """Project 3D points into 2D imag e. Expects pts3d as a 4xN numpy array. + Returns the 2D projection of the points that are in front of the camera only an the corresponding 3D points. """ # 3D points in camera reference frame. - pts3d_cam = Rrect.dot(T_cam_velo.dot(pts3d)) + pts3d_cam = rotaion_matrix.dot(transform_cam_velo.dot(pts3d)) # Before projecting, keep only points with z > 0 # (points that are in fronto of the camera). idx = (pts3d_cam[2, :] >= 0) - pts2d_cam = Prect.dot(pts3d_cam[:, idx]) + pts2d_cam = position_matrix.dot(pts3d_cam[:, idx]) # return pts3d[:, idx], pts2d_cam / pts2d_cam[2,:] return pts2d_cam / pts2d_cam[2, :] diff --git a/chainercv/datasets/kitti/parseTrackletXML.py b/chainercv/datasets/kitti/parseTrackletXML.py index 950550039e..d393fddf53 100644 --- a/chainercv/datasets/kitti/parseTrackletXML.py +++ b/chainercv/datasets/kitti/parseTrackletXML.py @@ -3,7 +3,8 @@ No guarantees that this code is correct, usage is at your own risk! - created by Christian Herdtweck, Max Planck Institute for Biological Cybernetics + created by Christian Herdtweck, + Max Planck Institute for Biological Cybernetics (christian.herdtweck@tuebingen.mpg.de) requires numpy! @@ -26,10 +27,11 @@ # truncation and added consistency checks # 30/1/14 ch: create example function from example code -from xml.etree.ElementTree import ElementTree import itertools -import numpy as np from warnings import warn +from xml.etree.ElementTree import ElementTree + +import numpy as np STATE_UNSET = 0 STATE_INTERP = 1 @@ -58,24 +60,24 @@ class Tracklet(object): Tracklets are created in function parseXML and can most conveniently used as follows: - for trackletObj in parseXML(trackletFile): + for trackletObj in parseXML(tracklet_filepath): for translation, rotation, state, occlusion, \ - truncation, amtOcclusion, amtBorders, \ + truncation, amt_occlusion, amt_borders, \ absoluteFrameNumber in trackletObj: ... your code here ... #end: for all frames #end: for all tracklets absoluteFrameNumber is in range [firstFrame, firstFrame+nFrames[ - amtOcclusion and amtBorders could be None + amt_occlusion and amt_borders could be None You can of course also directly access the fields objType (string), size (len-3 ndarray), firstFrame/nFrames (int), trans/rots (nFrames x 3 float ndarrays), states/truncs (len-nFrames uint8 ndarrays), occs (nFrames x 2 uint8 ndarray), - and for some tracklets amtOccs (nFrames x 2 float ndarray) - and amtBorders (nFrames x 3 float ndarray). + and for some tracklets amt_occs (nFrames x 2 float ndarray) + and amt_borders (nFrames x 3 float ndarray). The last two can be None if the xml file did not include these fields in poses """ @@ -89,8 +91,8 @@ class Tracklet(object): occs = None # n x 2 uint8 array (occlusion, occlusion_kf) truncs = None # len-n uint8 array of truncation # None or (n x 2) float array (amt_occlusion, amt_occlusion_kf) - amtOccs = None - amtBorders = None # None (n x 3) float array (amt_border_l / _r / _kf) + amt_occs = None + amt_borders = None # None (n x 3) float array (amt_border_l / _r / _kf) nFrames = None def __init__(self): @@ -109,229 +111,229 @@ def __str__(self): self.nFrames, self.objectType) def __iter__(self): - """Returns an iterator. + """Return an iterator object. that yields tuple of all the available data for each frame called whenever code iterates over a tracklet object, e.g. in for translation, rotation, state, occlusion, truncation, - amtOcclusion, amtBorders, absoluteFrameNumber in trackletObj: + amt_occlusion, amt_borders, absoluteFrameNumber in trackletObj: ...do something ... or trackDataIter = iter(trackletObj) """ - if self.amtOccs is None: + if self.amt_occs is None: # Python2/3 return zip( - self.trans, self.rots, self.states, - self.occs, self.truncs, - itertools.repeat(None), itertools.repeat(None), - range(self.firstFrame, self.firstFrame+self.nFrames)) + self.trans, self.rots, self.states, + self.occs, self.truncs, + itertools.repeat(None), itertools.repeat(None), + range(self.firstFrame, self.firstFrame+self.nFrames)) # xrange(self.firstFrame, self.firstFrame+self.nFrames)) # tmpAmtOccs = repeat(None) # tmpAmtBorders = repeat(None) # return zip(self.trans, self.rots, self.states, - # self.occs, self.truncs, - # tmpAmtOccs, tmpAmtBorders, - # range(self.firstFrame, self.firstFrame + self.nFrames)) + # self.occs, self.truncs, + # tmp_amt_occs, tmp_amt_borders, + # range(self.firstFrame, self.firstFrame + self.nFrames)) else: # Python2/3 return zip( - self.trans, self.rots, self.states, - self.occs, self.truncs, - self.amtOccs, self.amtBorders, - range(self.firstFrame, self.firstFrame + self.nFrames)) + self.trans, self.rots, self.states, + self.occs, self.truncs, + self.amt_occs, self.amt_borders, + range(self.firstFrame, self.firstFrame + self.nFrames)) # xrange(self.firstFrame, self.firstFrame+self.nFrames)) # return zip(self.trans, self.rots, self.states, - # self.occs, self.truncs, - # self.amtOccs, self.amtBorders, - # range(self.firstFrame, self.firstFrame + self.nFrames)) + # self.occs, self.truncs, + # self.amt_occs, self.amt_borders, + # range(self.firstFrame, self.firstFrame + self.nFrames)) # end: class Tracklet -def parseXML(trackletFile): - r"""Parse tracklet xml file and convert results to list of Tracklet objects. +def parseXML(tracklet_filepath): + r"""Parse tracklet xml file and convert list of Tracklet objects. - :param trackletFile: name of a tracklet xml file + :param tracklet_filepath: name of a tracklet xml file :returns: list of Tracklet objects read from xml file """ - - newTrack_nFrames_isNone_ErrorStr = \ + new_track_nframes_isnone_errorstr = \ 'there are several pose lists for a single track!' # convert tracklet XML data to a tree structure - eTree = ElementTree() - # print('parsing tracklet file', trackletFile) - with open(trackletFile) as f: - eTree.parse(f) + element_tree = ElementTree() + # print('parsing tracklet file', tracklet_filepath) + with open(tracklet_filepath) as f: + element_tree.parse(f) # now convert output to list of Tracklet objects - trackletsElem = eTree.find('tracklets') + tracklets_element = element_tree.find('tracklets') tracklets = [] - trackletIdx = 0 - nTracklets = None - for trackletElem in trackletsElem: - # print('track:', trackletElem.tag) - if trackletElem.tag == 'count': - nTracklets = int(trackletElem.text) - # print('file contains', nTracklets, 'tracklets') - elif trackletElem.tag == 'item_version': + tracklet_idx = 0 + numeric_tracklets = None + for tracklet_element in tracklets_element: + # print('track:', tracklet_element.tag) + if tracklet_element.tag == 'count': + numeric_tracklets = int(tracklet_element.text) + # print('file contains', numeric_tracklets, 'tracklets') + elif tracklet_element.tag == 'item_version': pass - elif trackletElem.tag == 'item': - # print('tracklet {0} of {1}'.format(trackletIdx, nTracklets)) + elif tracklet_element.tag == 'item': + # print( + # 'tracklet {0} of {1}'.format(tracklet_idx, numeric_tracklets)) # a tracklet - newTrack = Tracklet() - isFinished = False - hasAmt = False - frameIdx = None - for info in trackletElem: + new_track = Tracklet() + is_finished = False + has_amt = False + frame_idx = None + for info in tracklet_element: # print('trackInfo:', info.tag) - if isFinished: + if is_finished: raise ValueError('more info on element after finished!') if info.tag == 'objectType': - newTrack.objectType = info.text + new_track.objectType = info.text elif info.tag == 'h': - newTrack.size[0] = float(info.text) + new_track.size[0] = float(info.text) elif info.tag == 'w': - newTrack.size[1] = float(info.text) + new_track.size[1] = float(info.text) elif info.tag == 'l': - newTrack.size[2] = float(info.text) + new_track.size[2] = float(info.text) elif info.tag == 'first_frame': - newTrack.firstFrame = int(info.text) + new_track.firstFrame = int(info.text) elif info.tag == 'poses': # this info is the possibly long list of poses for pose in info: # print('trackInfoPose:', pose.tag) # this should come before the others if pose.tag == 'count': - if newTrack.nFrames is not None: + if new_track.nFrames is not None: raise ValueError( - newTrack_nFrames_isNone_ErrorStr) - elif frameIdx is not None: + new_track_nframes_isnone_errorstr) + elif frame_idx is not None: raise ValueError('?!') - newTrack.nFrames = int(pose.text) - newTrack.trans = np.nan * \ - np.ones((newTrack.nFrames, 3), dtype=float) - newTrack.rots = np.nan * \ - np.ones((newTrack.nFrames, 3), dtype=float) - newTrack.states = np.nan * \ - np.ones(newTrack.nFrames, dtype='uint8') - newTrack.occs = np.nan * \ - np.ones((newTrack.nFrames, 2), dtype='uint8') - newTrack.truncs = np.nan * \ - np.ones(newTrack.nFrames, dtype='uint8') - newTrack.amtOccs = np.nan * \ - np.ones((newTrack.nFrames, 2), dtype=float) - newTrack.amtBorders = np.nan * \ - np.ones((newTrack.nFrames, 3), dtype=float) - frameIdx = 0 + new_track.nFrames = int(pose.text) + new_track.trans = np.nan * \ + np.ones((new_track.nFrames, 3), dtype=float) + new_track.rots = np.nan * \ + np.ones((new_track.nFrames, 3), dtype=float) + new_track.states = np.nan * \ + np.ones(new_track.nFrames, dtype='uint8') + new_track.occs = np.nan * \ + np.ones((new_track.nFrames, 2), dtype='uint8') + new_track.truncs = np.nan * \ + np.ones(new_track.nFrames, dtype='uint8') + new_track.amt_occs = np.nan * \ + np.ones((new_track.nFrames, 2), dtype=float) + new_track.amt_borders = np.nan * \ + np.ones((new_track.nFrames, 3), dtype=float) + frame_idx = 0 elif pose.tag == 'item_version': pass elif pose.tag == 'item': # pose in one frame - if frameIdx is None: + if frame_idx is None: raise ValueError( 'pose item came before number of poses!') for poseInfo in pose: # print('trackInfoPoseInfo:', poseInfo.tag) if poseInfo.tag == 'tx': - newTrack.trans[frameIdx, 0] = float( + new_track.trans[frame_idx, 0] = float( poseInfo.text) elif poseInfo.tag == 'ty': - newTrack.trans[frameIdx, 1] = float( + new_track.trans[frame_idx, 1] = float( poseInfo.text) elif poseInfo.tag == 'tz': - newTrack.trans[frameIdx, 2] = float( + new_track.trans[frame_idx, 2] = float( poseInfo.text) elif poseInfo.tag == 'rx': - newTrack.rots[frameIdx, 0] = float( + new_track.rots[frame_idx, 0] = float( poseInfo.text) elif poseInfo.tag == 'ry': - newTrack.rots[frameIdx, 1] = float( + new_track.rots[frame_idx, 1] = float( poseInfo.text) elif poseInfo.tag == 'rz': - newTrack.rots[frameIdx, 2] = float( + new_track.rots[frame_idx, 2] = float( poseInfo.text) elif poseInfo.tag == 'state': - newTrack.states[frameIdx] = \ + new_track.states[frame_idx] = \ stateFromText[poseInfo.text] elif poseInfo.tag == 'occlusion': - newTrack.occs[frameIdx, 0] = \ + new_track.occs[frame_idx, 0] = \ occFromText[poseInfo.text] elif poseInfo.tag == 'occlusion_kf': - newTrack.occs[frameIdx, 1] = \ + new_track.occs[frame_idx, 1] = \ occFromText[poseInfo.text] elif poseInfo.tag == 'truncation': - newTrack.truncs[frameIdx] = \ + new_track.truncs[frame_idx] = \ truncFromText[poseInfo.text] elif poseInfo.tag == 'amt_occlusion': - newTrack.amtOccs[frameIdx, 0] = \ + new_track.amt_occs[frame_idx, 0] = \ float(poseInfo.text) - hasAmt = True + has_amt = True elif poseInfo.tag == 'amt_occlusion_kf': - newTrack.amtOccs[frameIdx, 1] = \ + new_track.amt_occs[frame_idx, 1] = \ float(poseInfo.text) - hasAmt = True + has_amt = True elif poseInfo.tag == 'amt_border_l': - newTrack.amtBorders[frameIdx, 0] = \ + new_track.amt_borders[frame_idx, 0] = \ float(poseInfo.text) - hasAmt = True + has_amt = True elif poseInfo.tag == 'amt_border_r': - newTrack.amtBorders[frameIdx, 1] = \ + new_track.amt_borders[frame_idx, 1] = \ float(poseInfo.text) - hasAmt = True + has_amt = True elif poseInfo.tag == 'amt_border_kf': - newTrack.amtBorders[frameIdx, 2] = \ + new_track.amt_borders[frame_idx, 2] = \ float(poseInfo.text) - hasAmt = True + has_amt = True else: raise ValueError( 'unexpected tag in poses item: {0}!' .format(poseInfo.tag)) - frameIdx += 1 + frame_idx += 1 else: raise ValueError( 'unexpected pose info: {0}!'.format(pose.tag)) elif info.tag == 'finished': - isFinished = True + is_finished = True else: raise ValueError( 'unexpected tag in tracklets: {0}!'.format(info.tag)) # end: for all fields in current tracklet # some final consistency checks on new tracklet - if not isFinished: - warn('tracklet {0} was not finished!'.format(trackletIdx)) - if newTrack.nFrames is None: + if not is_finished: + warn('tracklet {0} was not finished!'.format(tracklet_idx)) + if new_track.nFrames is None: warn('tracklet {0} contains no information!'.format( - trackletIdx)) - elif frameIdx != newTrack.nFrames: + tracklet_idx)) + elif frame_idx != new_track.nFrames: warn( 'tracklet {0} is supposed to have {1} frames, \ but perser found {1}!'.format( - trackletIdx, newTrack.nFrames, frameIdx)) - if np.abs(newTrack.rots[:, :2]).sum() > 1e-16: + tracklet_idx, new_track.nFrames, frame_idx)) + if np.abs(new_track.rots[:, :2]).sum() > 1e-16: warn('track contains rotation other than yaw!') - # if amtOccs / amtBorders are not set, set them to None - if not hasAmt: - newTrack.amtOccs = None - newTrack.amtBorders = None + # if amt_occs / amt_borders are not set, set them to None + if not has_amt: + new_track.amt_occs = None + new_track.amt_borders = None # add new tracklet to list - tracklets.append(newTrack) - trackletIdx += 1 + tracklets.append(new_track) + tracklet_idx += 1 else: raise ValueError('unexpected tracklet info') # end: for tracklet list items - # print('loaded', trackletIdx, 'tracklets') + # print('loaded', tracklet_idx, 'tracklets') # final consistency check - if trackletIdx != nTracklets: + if tracklet_idx != numeric_tracklets: warn('according to xml information the file has {0} tracklets, \ - but parser found {1}!'.format(nTracklets, trackletIdx)) + but parser found {1}!'.format(numeric_tracklets, tracklet_idx)) return tracklets # end: function parseXML From b866091297e7322f5ea61c7b6c43ec570636a32a Mon Sep 17 00:00:00 2001 From: sirokujira Date: Mon, 11 Jun 2018 13:08:38 +0900 Subject: [PATCH 12/33] flake8 modified TestCode(variant lowercase) --- chainercv/datasets/kitti/kitti_utils.py | 18 +++++++----- chainercv/datasets/kitti/parseTrackletXML.py | 5 ++-- .../kitti_tests/test_kitti_bbox_dataset.py | 28 +++++++++---------- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_utils.py b/chainercv/datasets/kitti/kitti_utils.py index aee519ec43..63c6e963e0 100644 --- a/chainercv/datasets/kitti/kitti_utils.py +++ b/chainercv/datasets/kitti/kitti_utils.py @@ -1,11 +1,17 @@ import os -from urllib.parse import urljoin +try: + # 3.x + from urllib.parse import urljoin +except ImportError: + # 2.7 + from urlparse import urljoin from chainer.dataset import download -import numpy as np -from chainercv import utils from chainercv.datasets.kitti import parseTrackletXML as xmlParser +from chainercv import utils + +import numpy as np # root = 'pfnet/chainercv/kitti' url_base = 'http://kitti.is.tue.mpg.de/kitti/raw_data/' @@ -90,8 +96,7 @@ def get_kitti_tracklets(data_root, date, drive_num): return tracklets -def get_kitti_label( - tracklets, calib, +def get_kitti_label(tracklets, calib, cur_rotation_matrix, cur_position_matrix, framelength): # set list @@ -224,8 +229,7 @@ def get_kitti_label( return bboxes, labels -def project_velo_points_in_img( - pts3d, transform_cam_velo, +def project_velo_points_in_img(pts3d, transform_cam_velo, rotaion_matrix, position_matrix): """Project 3D points into 2D imag e. Expects pts3d as a 4xN numpy array. diff --git a/chainercv/datasets/kitti/parseTrackletXML.py b/chainercv/datasets/kitti/parseTrackletXML.py index d393fddf53..85fd4a5e1b 100644 --- a/chainercv/datasets/kitti/parseTrackletXML.py +++ b/chainercv/datasets/kitti/parseTrackletXML.py @@ -310,8 +310,9 @@ def parseXML(tracklet_filepath): elif frame_idx != new_track.nFrames: warn( 'tracklet {0} is supposed to have {1} frames, \ - but perser found {1}!'.format( - tracklet_idx, new_track.nFrames, frame_idx)) + but perser found {1}!'.format(tracklet_idx, + new_track.nFrames, + frame_idx)) if np.abs(new_track.rots[:, :2]).sum() > 1e-16: warn('track contains rotation other than yaw!') diff --git a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py index 72c6406c1f..9c93acb6da 100644 --- a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py +++ b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py @@ -11,45 +11,45 @@ @testing.parameterize( { 'date': '2011_09_26', - 'driveNo': '0001', + 'drive_num': '0001', 'color': True, 'sync': True, - 'isLeft': True + 'is_left': True }, { 'date': '2011_09_26', - 'driveNo': '0001', + 'drive_num': '0001', 'color': True, 'sync': False, - 'isLeft': True + 'is_left': True }, { 'date': '2011_09_26', - 'driveNo': '0001', + 'drive_num': '0001', 'color': True, 'sync': True, - 'isLeft': False + 'is_left': False }, { 'date': '2011_09_26', - 'driveNo': '0017', + 'drive_num': '0017', 'color': True, 'sync': True, - 'isLeft': True + 'is_left': True }, { 'date': '2011_09_28', - 'driveNo': '0001', + 'drive_num': '0001', 'color': True, 'sync': True, - 'isLeft': True + 'is_left': True }, { 'date': '2011_10_03', - 'driveNo': '0047', + 'drive_num': '0047', 'color': True, 'sync': True, - 'isLeft': True + 'is_left': True }, ) class TestKITTIBboxDataset(unittest.TestCase): @@ -57,10 +57,10 @@ class TestKITTIBboxDataset(unittest.TestCase): def setUp(self): self.dataset = KITTIBboxDataset( date=self.date, - driveNo=self.driveNo, + drive_num=self.drive_num, color=self.color, sync=self.sync, - isLeft=self.isLeft) + is_left=self.is_left) @attr.slow def test_kitti_bbox_dataset(self): From 856d70672569ea89db41d3a0b695c97af6488a09 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Mon, 11 Jun 2018 17:51:44 +0900 Subject: [PATCH 13/33] download base_url changed --- chainercv/datasets/kitti/kitti_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chainercv/datasets/kitti/kitti_utils.py b/chainercv/datasets/kitti/kitti_utils.py index 63c6e963e0..c02962cc6d 100644 --- a/chainercv/datasets/kitti/kitti_utils.py +++ b/chainercv/datasets/kitti/kitti_utils.py @@ -14,7 +14,7 @@ import numpy as np # root = 'pfnet/chainercv/kitti' -url_base = 'http://kitti.is.tue.mpg.de/kitti/raw_data/' +url_base = 'https://s3.eu-central-1.amazonaws.com/avg-kitti/raw_data/' def get_kitti_sync_data(root, date, drive_num): From d79c6346ff2955f7e1b33d6abcf9c81d3df87cbb Mon Sep 17 00:00:00 2001 From: sirokujira Date: Mon, 11 Jun 2018 18:54:06 +0900 Subject: [PATCH 14/33] check tracklets(It exists only in the data of 2011_09_26) --- .../datasets/kitti/kitti_bbox_dataset.py | 23 ++++++++---- chainercv/datasets/kitti/kitti_utils.py | 35 +++++++++++-------- .../kitti_tests/test_kitti_bbox_dataset.py | 19 ++++++---- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index 730d01c565..d7343c06e9 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -43,6 +43,7 @@ class KITTIBboxDataset(GetterDataset): color (bool): use glay/color image. sync (bool): get timer sync/nosync data. is_left (bool): left/right camera image use 2type. + tracklet (bool): 3d bblox data. date only 2011_09_26. This dataset returns the following data. @@ -60,7 +61,7 @@ class KITTIBboxDataset(GetterDataset): """ def __init__(self, data_dir='auto', date='', drive_num='', - color=True, sync=True, is_left=True): + color=True, sync=True, is_left=True, tracklet=False): super(KITTIBboxDataset, self).__init__() _check_available() @@ -68,15 +69,22 @@ def __init__(self, data_dir='auto', date='', drive_num='', self.color = color self.sync = sync self.is_left = is_left + if date == '2011_09_26': + self.tracklet = tracklet + else: + self.tracklet = False + if data_dir == 'auto': if sync is True: # download sync data - data_dir = get_kitti_sync_data(os.path.join( - 'pfnet', 'chainercv', 'KITTI'), date, drive_num) + data_dir = get_kitti_sync_data( + os.path.join('pfnet', 'chainercv', 'KITTI'), + date, drive_num, self.tracklet) else: # download nosync data - data_dir = get_kitti_nosync_data(os.path.join( - 'pfnet', 'chainercv', 'KITTI'), date, drive_num) + data_dir = get_kitti_nosync_data( + os.path.join('pfnet', 'chainercv', 'KITTI'), + date, drive_num, self.tracklet) # use pykitti # read All images @@ -110,7 +118,10 @@ def __init__(self, data_dir='auto', date='', drive_num='', self.imgs = np.array(list(self.dataset.cam1)) # get object info(type/area/bbox/...) - self.tracklets = get_kitti_tracklets(data_dir, date, drive_num) + if self.tracklet is True: + self.tracklets = get_kitti_tracklets(data_dir, date, drive_num) + else: + self.tracklets = None self.bboxes, self.labels = get_kitti_label( self.tracklets, self.dataset.calib, diff --git a/chainercv/datasets/kitti/kitti_utils.py b/chainercv/datasets/kitti/kitti_utils.py index c02962cc6d..04df2313c0 100644 --- a/chainercv/datasets/kitti/kitti_utils.py +++ b/chainercv/datasets/kitti/kitti_utils.py @@ -17,7 +17,7 @@ url_base = 'https://s3.eu-central-1.amazonaws.com/avg-kitti/raw_data/' -def get_kitti_sync_data(root, date, drive_num): +def get_kitti_sync_data(root, date, drive_num, tracklet): data_root = download.get_dataset_directory(root) # data @@ -27,9 +27,6 @@ def get_kitti_sync_data(root, date, drive_num): # calibration url_calib = url_base + date + '_calib.zip' - # tracklet - url_tracklet = urljoin(url_base, folder + '/' + folder + '_tracklets.zip') - download_file_path = utils.cached_download(url_data) ext = os.path.splitext(url_data)[1] utils.extractall(download_file_path, data_root, ext) @@ -38,14 +35,19 @@ def get_kitti_sync_data(root, date, drive_num): ext = os.path.splitext(url_calib)[1] utils.extractall(download_file_path, data_root, ext) - download_file_path = utils.cached_download(url_tracklet) - ext = os.path.splitext(url_tracklet)[1] - utils.extractall(download_file_path, data_root, ext) + if tracklet is True: + # tracklet + url_tracklet = \ + urljoin(url_base, folder + '/' + folder + '_tracklets.zip') + + download_file_path = utils.cached_download(url_tracklet) + ext = os.path.splitext(url_tracklet)[1] + utils.extractall(download_file_path, data_root, ext) return data_root -def get_kitti_nosync_data(root, date, drive_num): +def get_kitti_nosync_data(root, date, drive_num, tracklet): data_root = download.get_dataset_directory(root) # data @@ -55,9 +57,6 @@ def get_kitti_nosync_data(root, date, drive_num): # calibration url_calib = url_base + date + '_calib.zip' - # tracklet - url_tracklet = urljoin(url_base, folder + '/' + folder + '_tracklets.zip') - download_file_path = utils.cached_download(url_data) ext = os.path.splitext(url_data)[1] utils.extractall(download_file_path, data_root, ext) @@ -66,9 +65,14 @@ def get_kitti_nosync_data(root, date, drive_num): ext = os.path.splitext(url_calib)[1] utils.extractall(download_file_path, data_root, ext) - download_file_path = utils.cached_download(url_tracklet) - ext = os.path.splitext(url_tracklet)[1] - utils.extractall(download_file_path, data_root, ext) + if tracklet is True: + # tracklet + url_tracklet = \ + urljoin(url_base, folder + '/' + folder + '_tracklets.zip') + + download_file_path = utils.cached_download(url_tracklet) + ext = os.path.splitext(url_tracklet)[1] + utils.extractall(download_file_path, data_root, ext) return data_root @@ -106,6 +110,9 @@ def get_kitti_label(tracklets, calib, bboxes[idx] = [] labels[idx] = [] + if tracklets is None: + return bboxes, labels + # set ndarray # bboxes = np.zeros(framelength, dtype=np.float32) # labels = np.zeros(framelength, dtype=np.int32) diff --git a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py index 9c93acb6da..b8ff141965 100644 --- a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py +++ b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py @@ -14,21 +14,24 @@ 'drive_num': '0001', 'color': True, 'sync': True, - 'is_left': True + 'is_left': True, + 'tracklet': True }, { 'date': '2011_09_26', 'drive_num': '0001', 'color': True, 'sync': False, - 'is_left': True + 'is_left': True, + 'tracklet': True }, { 'date': '2011_09_26', 'drive_num': '0001', 'color': True, 'sync': True, - 'is_left': False + 'is_left': False, + 'tracklet': True }, { 'date': '2011_09_26', @@ -36,20 +39,23 @@ 'color': True, 'sync': True, 'is_left': True + 'tracklet': False }, { 'date': '2011_09_28', 'drive_num': '0001', 'color': True, 'sync': True, - 'is_left': True + 'is_left': True, + 'tracklet': False }, { 'date': '2011_10_03', 'drive_num': '0047', 'color': True, 'sync': True, - 'is_left': True + 'is_left': True, + 'tracklet': True }, ) class TestKITTIBboxDataset(unittest.TestCase): @@ -60,7 +66,8 @@ def setUp(self): drive_num=self.drive_num, color=self.color, sync=self.sync, - is_left=self.is_left) + is_left=self.is_left, + tracklet=self.tracklet) @attr.slow def test_kitti_bbox_dataset(self): From 3d9ef78942f9a75009fb0d3eb66f1ad97344c523 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Mon, 11 Jun 2018 19:00:37 +0900 Subject: [PATCH 15/33] bug fix(Forget to add ,) --- tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py index b8ff141965..155b8cf346 100644 --- a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py +++ b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py @@ -38,7 +38,7 @@ 'drive_num': '0017', 'color': True, 'sync': True, - 'is_left': True + 'is_left': True, 'tracklet': False }, { From e0d3a11397f34a2b1e8fc9a2c8af0d287cd4ec6d Mon Sep 17 00:00:00 2001 From: sirokujira Date: Wed, 13 Jun 2018 17:23:55 +0900 Subject: [PATCH 16/33] use pykitti 0.3.0 add gray image test --- .../datasets/kitti/kitti_bbox_dataset.py | 28 ++++++++++++------- environment.yml | 2 +- .../kitti_tests/test_kitti_bbox_dataset.py | 8 ++++++ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index d7343c06e9..ada3734b22 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -21,7 +21,7 @@ def _check_available(): 'pykitti is not installed in your environment,' 'so the dataset cannot be loaded.' 'Please install pykitti to load dataset.\n\n' - '$ pip install pykitti==0.2.4') + '$ pip install pykitti>=0.3.0') class KITTIBboxDataset(GetterDataset): @@ -99,23 +99,30 @@ def __init__(self, data_dir='auto', date='', drive_num='', # img02 self.cur_rotation_matrix = self.dataset.calib.R_rect_20 self.cur_position_matrix = self.dataset.calib.P_rect_20 - self.imgs = np.array(list(self.dataset.cam2)) + # pykitti>=0.3.0 + self.imgs = np.array(list(self.dataset.get_cam2)) else: # img03 self.cur_rotation_matrix = self.dataset.calib.R_rect_30 self.cur_position_matrix = self.dataset.calib.P_rect_30 - self.imgs = np.array(list(self.dataset.cam3)) + # pykitti>=0.3.0 + self.imgs = np.array(list(self.dataset.get_cam3)) else: + # warnings.warn( + # 'pykitti is gray image return (H, W, C=1) array,' + # 'use color dataset.\n\n') if self.is_left is True: # img00 self.cur_rotation_matrix = self.dataset.calib.R_rect_00 self.cur_position_matrix = self.dataset.calib.P_rect_00 - self.imgs = np.array(list(self.dataset.cam0)) + # pykitti>=0.3.0 + self.imgs = np.array(list(self.dataset.get_cam0)) else: # img01 self.cur_rotation_matrix = self.dataset.calib.R_rect_10 self.cur_position_matrix = self.dataset.calib.P_rect_10 - self.imgs = np.array(list(self.dataset.cam1)) + # pykitti>=0.3.0 + self.imgs = np.array(list(self.dataset.get_cam1)) # get object info(type/area/bbox/...) if self.tracklet is True: @@ -149,24 +156,25 @@ def _get_image(self, i): return img.transpose((2, 0, 1)) def _get_annotations(self, i): - # List[{'segmentation', 'area', 'iscrowd', - # 'image_id', 'bbox', 'category_id', 'id'}] bbox = self.bboxes label = self.labels # convert list to ndarray if len(bbox[i]) == 0: - # bbox[i] = np.zeros((0, 4), dtype=np.float32) + # Data Padding(Pass Bbox Test) + # NG + # bbox[i] = [[0.0, 0.0, 0.0, 0.0]] bbox[i] = [[0.0, 0.0, 0.01, 0.01]] np_bbox = np.array(bbox[i], dtype=np.float32) if len(label[i]) == 0: - # label[i] = np.zeros((0, 1), dtype=np.int32) - label[i] = [0] + # Data Padding(Pass Bbox Test) + label[i] = [7] np_label = np.array(label[i], dtype=np.int32) + # debug print print(np_bbox) print(np_label) return np_bbox, np_label diff --git a/environment.yml b/environment.yml index 38c90e7155..4903990d21 100644 --- a/environment.yml +++ b/environment.yml @@ -16,4 +16,4 @@ dependencies: - scipy - pip: - chainermn==1.3.0 - - pykitti==0.2.4 + - pykitti==0.3.0 diff --git a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py index 155b8cf346..57d2c7f533 100644 --- a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py +++ b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py @@ -25,6 +25,14 @@ 'is_left': True, 'tracklet': True }, + { + 'date': '2011_09_26', + 'drive_num': '0001', + 'color': False, + 'sync': True, + 'is_left': True, + 'tracklet': True + }, { 'date': '2011_09_26', 'drive_num': '0001', From 89428ebe8eb9d4ec00cd5a15801d7327f561673a Mon Sep 17 00:00:00 2001 From: sirokujira Date: Wed, 20 Jun 2018 15:02:11 +0900 Subject: [PATCH 17/33] fix bbox/label empty data. --- .../datasets/kitti/kitti_bbox_dataset.py | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index ada3734b22..e9f9d9dc60 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -161,20 +161,22 @@ def _get_annotations(self, i): # convert list to ndarray if len(bbox[i]) == 0: - # Data Padding(Pass Bbox Test) # NG # bbox[i] = [[0.0, 0.0, 0.0, 0.0]] - bbox[i] = [[0.0, 0.0, 0.01, 0.01]] - - np_bbox = np.array(bbox[i], dtype=np.float32) + # Data Padding(Pass Bbox Test) + # bbox[i] = [[0.0, 0.0, 0.01, 0.01]] + np_bbox = np.zeros((0, 4), dtype=np.float32) + else: + np_bbox = np.array(bbox[i], dtype=np.float32) if len(label[i]) == 0: # Data Padding(Pass Bbox Test) - label[i] = [7] - - np_label = np.array(label[i], dtype=np.int32) + # label[i] = [0] + np_label = np.zeros((0, 1), dtype=np.int32) + else + np_label = np.array(label[i], dtype=np.int32) # debug print - print(np_bbox) - print(np_label) + # print(np_bbox) + # print(np_label) return np_bbox, np_label From 8fc33693af6af1d035d1331f12914c8a597068d0 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Wed, 20 Jun 2018 15:10:27 +0900 Subject: [PATCH 18/33] sintax miss(:) --- chainercv/datasets/kitti/kitti_bbox_dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index e9f9d9dc60..cc30e8ec2b 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -173,7 +173,7 @@ def _get_annotations(self, i): # Data Padding(Pass Bbox Test) # label[i] = [0] np_label = np.zeros((0, 1), dtype=np.int32) - else + else: np_label = np.array(label[i], dtype=np.int32) # debug print From ad6e84dc5fa38ec9d47676cd7c1343d43a82ece3 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Wed, 27 Jun 2018 09:44:39 +0900 Subject: [PATCH 19/33] use pykitti 0.2.4(revert) test code success testdata.(comment ng test) --- .../datasets/kitti/kitti_bbox_dataset.py | 9 ++-- environment.yml | 2 +- .../kitti_tests/test_kitti_bbox_dataset.py | 50 ++++++++++++++++--- 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index cc30e8ec2b..8c08813134 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -100,13 +100,13 @@ def __init__(self, data_dir='auto', date='', drive_num='', self.cur_rotation_matrix = self.dataset.calib.R_rect_20 self.cur_position_matrix = self.dataset.calib.P_rect_20 # pykitti>=0.3.0 - self.imgs = np.array(list(self.dataset.get_cam2)) + self.imgs = np.array(list(self.dataset.cam2)) else: # img03 self.cur_rotation_matrix = self.dataset.calib.R_rect_30 self.cur_position_matrix = self.dataset.calib.P_rect_30 # pykitti>=0.3.0 - self.imgs = np.array(list(self.dataset.get_cam3)) + self.imgs = np.array(list(self.dataset.cam3)) else: # warnings.warn( # 'pykitti is gray image return (H, W, C=1) array,' @@ -116,13 +116,13 @@ def __init__(self, data_dir='auto', date='', drive_num='', self.cur_rotation_matrix = self.dataset.calib.R_rect_00 self.cur_position_matrix = self.dataset.calib.P_rect_00 # pykitti>=0.3.0 - self.imgs = np.array(list(self.dataset.get_cam0)) + self.imgs = np.array(self.dataset.cam0) else: # img01 self.cur_rotation_matrix = self.dataset.calib.R_rect_10 self.cur_position_matrix = self.dataset.calib.P_rect_10 # pykitti>=0.3.0 - self.imgs = np.array(list(self.dataset.get_cam1)) + self.imgs = np.array(self.dataset.cam1) # get object info(type/area/bbox/...) if self.tracklet is True: @@ -152,6 +152,7 @@ def _get_image(self, i): # reshape (H, W) -> (1, H, W) return img[np.newaxis] else: + # pykitti img data # transpose (H, W, C) -> (C, H, W) return img.transpose((2, 0, 1)) diff --git a/environment.yml b/environment.yml index 4903990d21..38c90e7155 100644 --- a/environment.yml +++ b/environment.yml @@ -16,4 +16,4 @@ dependencies: - scipy - pip: - chainermn==1.3.0 - - pykitti==0.3.0 + - pykitti==0.2.4 diff --git a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py index 57d2c7f533..e3086ba761 100644 --- a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py +++ b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py @@ -43,28 +43,64 @@ }, { 'date': '2011_09_26', - 'drive_num': '0017', + 'drive_num': '0005', 'color': True, 'sync': True, 'is_left': True, - 'tracklet': False + 'tracklet': True }, { - 'date': '2011_09_28', - 'drive_num': '0001', + 'date': '2011_09_26', + 'drive_num': '0056', 'color': True, 'sync': True, 'is_left': True, - 'tracklet': False + 'tracklet': True }, { - 'date': '2011_10_03', - 'drive_num': '0047', + 'date': '2011_09_26', + 'drive_num': '0057', 'color': True, 'sync': True, 'is_left': True, 'tracklet': True }, + # Test NG(not Tracklet data) + # { + # 'date': '2011_09_26', + # 'drive_num': '0001', + # 'color': True, + # 'sync': True, + # 'is_left': True, + # 'tracklet': False + # }, + # Test NG(Part of Framerate not Bbox/label data) + # { + # 'date': '2011_09_26', + # 'drive_num': '0017', + # 'color': True, + # 'sync': True, + # 'is_left': True, + # 'tracklet': True + # }, + # Test NG(not Tracklet data) + # { + # 'date': '2011_09_28', + # 'drive_num': '0001', + # 'color': True, + # 'sync': True, + # 'is_left': True, + # 'tracklet': False + # }, + # Test NG(not Tracklet data) + # { + # 'date': '2011_10_03', + # 'drive_num': '0047', + # 'color': True, + # 'sync': True, + # 'is_left': True, + # 'tracklet': True + # }, ) class TestKITTIBboxDataset(unittest.TestCase): From 6474c1526b8ea5fc9a2a86d01a789fa1d9d7cd80 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Wed, 27 Jun 2018 10:23:50 +0900 Subject: [PATCH 20/33] add test condition --- .../kitti_tests/test_kitti_bbox_dataset.py | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py index e3086ba761..c17a0c43ab 100644 --- a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py +++ b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py @@ -43,7 +43,7 @@ }, { 'date': '2011_09_26', - 'drive_num': '0005', + 'drive_num': '0009', 'color': True, 'sync': True, 'is_left': True, @@ -65,6 +65,30 @@ 'is_left': True, 'tracklet': True }, + { + 'date': '2011_09_26', + 'drive_num': '0064', + 'color': True, + 'sync': True, + 'is_left': True, + 'tracklet': True + }, + { + 'date': '2011_09_26', + 'drive_num': '0032', + 'color': True, + 'sync': True, + 'is_left': True, + 'tracklet': True + }, + { + 'date': '2011_09_26', + 'drive_num': '0052', + 'color': True, + 'sync': True, + 'is_left': True, + 'tracklet': True + }, # Test NG(not Tracklet data) # { # 'date': '2011_09_26', From 73892d1f17f260ff234f54c11d7087623a4ec158 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Wed, 27 Jun 2018 10:29:28 +0900 Subject: [PATCH 21/33] test remove --- .../kitti_tests/test_kitti_bbox_dataset.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py index c17a0c43ab..d6112ee032 100644 --- a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py +++ b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py @@ -25,14 +25,14 @@ 'is_left': True, 'tracklet': True }, - { - 'date': '2011_09_26', - 'drive_num': '0001', - 'color': False, - 'sync': True, - 'is_left': True, - 'tracklet': True - }, + # { + # 'date': '2011_09_26', + # 'drive_num': '0001', + # 'color': False, + # 'sync': True, + # 'is_left': True, + # 'tracklet': True + # }, { 'date': '2011_09_26', 'drive_num': '0001', From 9fa2e205fafebb35a9f6d7b4fca26831be831279 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Wed, 27 Jun 2018 11:54:02 +0900 Subject: [PATCH 22/33] test passed data set. --- .../kitti_tests/test_kitti_bbox_dataset.py | 70 +++++++++++++------ 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py index d6112ee032..71967c2134 100644 --- a/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py +++ b/tests/datasets_tests/kitti_tests/test_kitti_bbox_dataset.py @@ -9,6 +9,7 @@ @testing.parameterize( + # category : City { 'date': '2011_09_26', 'drive_num': '0001', @@ -41,14 +42,33 @@ 'is_left': False, 'tracklet': True }, - { - 'date': '2011_09_26', - 'drive_num': '0009', - 'color': True, - 'sync': True, - 'is_left': True, - 'tracklet': True - }, + # Test NG(not Tracklet data) + # { + # 'date': '2011_09_26', + # 'drive_num': '0001', + # 'color': True, + # 'sync': True, + # 'is_left': True, + # 'tracklet': False + # }, + # Test NG(Part of Framerate not Bbox/label data) + # { + # 'date': '2011_09_26', + # 'drive_num': '0009', + # 'color': True, + # 'sync': True, + # 'is_left': True, + # 'tracklet': True + # }, + # Test NG(Part of Framerate not Bbox/label data) + # { + # 'date': '2011_09_26', + # 'drive_num': '0017', + # 'color': True, + # 'sync': True, + # 'is_left': True, + # 'tracklet': True + # }, { 'date': '2011_09_26', 'drive_num': '0056', @@ -65,6 +85,16 @@ 'is_left': True, 'tracklet': True }, + # Test NG(not Tracklet data) + # { + # 'date': '2011_09_28', + # 'drive_num': '0001', + # 'color': True, + # 'sync': True, + # 'is_left': True, + # 'tracklet': False + # }, + # category : Residential { 'date': '2011_09_26', 'drive_num': '0064', @@ -73,6 +103,7 @@ 'is_left': True, 'tracklet': True }, + # category : Road { 'date': '2011_09_26', 'drive_num': '0032', @@ -91,35 +122,28 @@ }, # Test NG(not Tracklet data) # { - # 'date': '2011_09_26', - # 'drive_num': '0001', - # 'color': True, - # 'sync': True, - # 'is_left': True, - # 'tracklet': False - # }, - # Test NG(Part of Framerate not Bbox/label data) - # { - # 'date': '2011_09_26', - # 'drive_num': '0017', + # 'date': '2011_10_03', + # 'drive_num': '0047', # 'color': True, # 'sync': True, # 'is_left': True, # 'tracklet': True # }, + # category : Campus # Test NG(not Tracklet data) # { # 'date': '2011_09_28', - # 'drive_num': '0001', + # 'drive_num': '0016', # 'color': True, # 'sync': True, # 'is_left': True, - # 'tracklet': False + # 'tracklet': True # }, + # category : Person # Test NG(not Tracklet data) # { - # 'date': '2011_10_03', - # 'drive_num': '0047', + # 'date': '2011_09_28', + # 'drive_num': '0053', # 'color': True, # 'sync': True, # 'is_left': True, From 8c68355bda8d6a07cabd6cff964bf9e269278a9a Mon Sep 17 00:00:00 2001 From: sirokujira Date: Tue, 7 Aug 2018 13:26:31 +0900 Subject: [PATCH 23/33] change comment(pykitti use under 0.2.4[0.3.0 data access change]) --- chainercv/datasets/kitti/kitti_bbox_dataset.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index 8c08813134..834c190492 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -21,7 +21,7 @@ def _check_available(): 'pykitti is not installed in your environment,' 'so the dataset cannot be loaded.' 'Please install pykitti to load dataset.\n\n' - '$ pip install pykitti>=0.3.0') + '$ pip install pykitti<=0.2.4') class KITTIBboxDataset(GetterDataset): @@ -99,13 +99,13 @@ def __init__(self, data_dir='auto', date='', drive_num='', # img02 self.cur_rotation_matrix = self.dataset.calib.R_rect_20 self.cur_position_matrix = self.dataset.calib.P_rect_20 - # pykitti>=0.3.0 + # pykitti<=0.2.4 self.imgs = np.array(list(self.dataset.cam2)) else: # img03 self.cur_rotation_matrix = self.dataset.calib.R_rect_30 self.cur_position_matrix = self.dataset.calib.P_rect_30 - # pykitti>=0.3.0 + # pykitti<=0.2.4 self.imgs = np.array(list(self.dataset.cam3)) else: # warnings.warn( @@ -115,14 +115,14 @@ def __init__(self, data_dir='auto', date='', drive_num='', # img00 self.cur_rotation_matrix = self.dataset.calib.R_rect_00 self.cur_position_matrix = self.dataset.calib.P_rect_00 - # pykitti>=0.3.0 - self.imgs = np.array(self.dataset.cam0) + # pykitti<=0.2.4 + self.imgs = np.array(list(self.dataset.cam0)) else: # img01 self.cur_rotation_matrix = self.dataset.calib.R_rect_10 self.cur_position_matrix = self.dataset.calib.P_rect_10 - # pykitti>=0.3.0 - self.imgs = np.array(self.dataset.cam1) + # pykitti<=0.2.4 + self.imgs = np.array(list(self.dataset.cam1)) # get object info(type/area/bbox/...) if self.tracklet is True: From 624fac601045dde986692cf65b4a2decbbdae1cc Mon Sep 17 00:00:00 2001 From: sirokujira Date: Thu, 23 Aug 2018 11:10:16 +0900 Subject: [PATCH 24/33] check pykitti version --- .../datasets/kitti/kitti_bbox_dataset.py | 47 +++++++++++++++---- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index 834c190492..83d078df58 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -1,10 +1,19 @@ import os import warnings +from pkg_resources import get_distribution +from pkg_resources import parse_version import numpy as np try: import pykitti - _available = True + pykitti_version = get_distribution('pykitti').version + if parse_version(pykitti_version) < parse_version('0.3.0'): + # pykitti<0.3.0 + _available = True + else: + # pykitti>=0.3.0 + warnings.warn('not support pykitti version : ' + pykitti_version) + _available = False except ImportError: _available = False @@ -99,14 +108,24 @@ def __init__(self, data_dir='auto', date='', drive_num='', # img02 self.cur_rotation_matrix = self.dataset.calib.R_rect_20 self.cur_position_matrix = self.dataset.calib.P_rect_20 - # pykitti<=0.2.4 - self.imgs = np.array(list(self.dataset.cam2)) + if parse_version(pykitti_version) < parse_version('0.3.0'): + # pykitti<0.3.0 + self.imgs = np.array(list(self.dataset.cam2)) + else: + # pykitti>=0.3.0 + warnings.warn('not support pykitti version : ' + pykitti_version) + pass else: # img03 self.cur_rotation_matrix = self.dataset.calib.R_rect_30 self.cur_position_matrix = self.dataset.calib.P_rect_30 - # pykitti<=0.2.4 - self.imgs = np.array(list(self.dataset.cam3)) + if parse_version(pykitti_version) < parse_version('0.3.0'): + # pykitti<0.3.0 + self.imgs = np.array(list(self.dataset.cam3)) + else: + # pykitti>=0.3.0 + warnings.warn('not support pykitti version : ' + pykitti_version) + pass else: # warnings.warn( # 'pykitti is gray image return (H, W, C=1) array,' @@ -115,14 +134,24 @@ def __init__(self, data_dir='auto', date='', drive_num='', # img00 self.cur_rotation_matrix = self.dataset.calib.R_rect_00 self.cur_position_matrix = self.dataset.calib.P_rect_00 - # pykitti<=0.2.4 - self.imgs = np.array(list(self.dataset.cam0)) + if parse_version(pykitti_version) < parse_version('0.3.0'): + # pykitti<0.3.0 + self.imgs = np.array(list(self.dataset.cam0)) + else: + # pykitti>=0.3.0 + warnings.warn('not support pykitti version : ' + pykitti_version) + pass else: # img01 self.cur_rotation_matrix = self.dataset.calib.R_rect_10 self.cur_position_matrix = self.dataset.calib.P_rect_10 - # pykitti<=0.2.4 - self.imgs = np.array(list(self.dataset.cam1)) + if parse_version(pykitti_version) < parse_version('0.3.0'): + # pykitti<0.3.0 + self.imgs = np.array(list(self.dataset.cam1)) + else: + # pykitti>=0.3.0 + warnings.warn('not support pykitti version : ' + pykitti_version) + pass # get object info(type/area/bbox/...) if self.tracklet is True: From 9ad059d442ded2dd1f5c246a78a7351288041a70 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Thu, 23 Aug 2018 13:11:40 +0900 Subject: [PATCH 25/33] pykitti 0.3.0 Image data set --- .../datasets/kitti/kitti_bbox_dataset.py | 41 ++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index 83d078df58..ff64cb6edc 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -12,8 +12,9 @@ _available = True else: # pykitti>=0.3.0 - warnings.warn('not support pykitti version : ' + pykitti_version) - _available = False + # warnings.warn('not support pykitti version : ' + pykitti_version) + # _available = False + _available = True except ImportError: _available = False @@ -30,7 +31,7 @@ def _check_available(): 'pykitti is not installed in your environment,' 'so the dataset cannot be loaded.' 'Please install pykitti to load dataset.\n\n' - '$ pip install pykitti<=0.2.4') + '$ pip install pykitti==0.2.4') class KITTIBboxDataset(GetterDataset): @@ -113,7 +114,23 @@ def __init__(self, data_dir='auto', date='', drive_num='', self.imgs = np.array(list(self.dataset.cam2)) else: # pykitti>=0.3.0 - warnings.warn('not support pykitti version : ' + pykitti_version) + # warnings.warn('not support pykitti version : ' + pykitti_version) + # get PIL Image + # convert from PIL.Image to numpy + # print(np.asarray(list(self.dataset.cam2))) + from PIL import Image + dataArray = list() + for cam2 in self.dataset.cam2: + print(cam2) + data = np.asarray(cam2) + print(data) + # Convert RGB to BGR + if len(data.shape) > 2: + data = data[:, :, ::-1] + dataArray.append(data) + + print(dataArray) + self.imgs = dataArray pass else: # img03 @@ -124,7 +141,21 @@ def __init__(self, data_dir='auto', date='', drive_num='', self.imgs = np.array(list(self.dataset.cam3)) else: # pykitti>=0.3.0 - warnings.warn('not support pykitti version : ' + pykitti_version) + # warnings.warn('not support pykitti version : ' + pykitti_version) + # get PIL Image + from PIL import Image + dataArray = list() + for cam2 in self.dataset.cam2: + print(cam2) + data = np.asarray(cam2) + print(data) + # Convert RGB to BGR + if len(data.shape) > 2: + data = data[:, :, ::-1] + dataArray.append(data) + + print(dataArray) + self.imgs = dataArray pass else: # warnings.warn( From 71125428873b7ae52057c9c7d145f99221b9c62f Mon Sep 17 00:00:00 2001 From: sirokujira Date: Thu, 23 Aug 2018 13:17:49 +0900 Subject: [PATCH 26/33] remove print debug check pep8(remove whitespace blank) --- .../datasets/kitti/kitti_bbox_dataset.py | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index ff64cb6edc..af08662548 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -114,22 +114,17 @@ def __init__(self, data_dir='auto', date='', drive_num='', self.imgs = np.array(list(self.dataset.cam2)) else: # pykitti>=0.3.0 - # warnings.warn('not support pykitti version : ' + pykitti_version) # get PIL Image # convert from PIL.Image to numpy - # print(np.asarray(list(self.dataset.cam2))) from PIL import Image dataArray = list() for cam2 in self.dataset.cam2: - print(cam2) data = np.asarray(cam2) - print(data) - # Convert RGB to BGR + # Convert RGB to BGR if len(data.shape) > 2: - data = data[:, :, ::-1] + data = data[:, :, ::-1] dataArray.append(data) - print(dataArray) self.imgs = dataArray pass else: @@ -141,20 +136,17 @@ def __init__(self, data_dir='auto', date='', drive_num='', self.imgs = np.array(list(self.dataset.cam3)) else: # pykitti>=0.3.0 - # warnings.warn('not support pykitti version : ' + pykitti_version) # get PIL Image + # convert from PIL.Image to numpy from PIL import Image dataArray = list() for cam2 in self.dataset.cam2: - print(cam2) data = np.asarray(cam2) - print(data) # Convert RGB to BGR if len(data.shape) > 2: data = data[:, :, ::-1] dataArray.append(data) - print(dataArray) self.imgs = dataArray pass else: @@ -170,7 +162,8 @@ def __init__(self, data_dir='auto', date='', drive_num='', self.imgs = np.array(list(self.dataset.cam0)) else: # pykitti>=0.3.0 - warnings.warn('not support pykitti version : ' + pykitti_version) + warnings.warn( + 'not support pykitti version : ' + pykitti_version) pass else: # img01 @@ -181,7 +174,8 @@ def __init__(self, data_dir='auto', date='', drive_num='', self.imgs = np.array(list(self.dataset.cam1)) else: # pykitti>=0.3.0 - warnings.warn('not support pykitti version : ' + pykitti_version) + warnings.warn( + 'not support pykitti version : ' + pykitti_version) pass # get object info(type/area/bbox/...) From 01e6f78607e5ed93d9f6bd592994c48c0d94d8f0 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Sat, 25 Aug 2018 00:01:05 +0900 Subject: [PATCH 27/33] dataArray parameter change(not use list) --- chainercv/datasets/kitti/kitti_bbox_dataset.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index af08662548..a6b546d0d5 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -117,7 +117,7 @@ def __init__(self, data_dir='auto', date='', drive_num='', # get PIL Image # convert from PIL.Image to numpy from PIL import Image - dataArray = list() + dataArray = [] for cam2 in self.dataset.cam2: data = np.asarray(cam2) # Convert RGB to BGR @@ -139,7 +139,7 @@ def __init__(self, data_dir='auto', date='', drive_num='', # get PIL Image # convert from PIL.Image to numpy from PIL import Image - dataArray = list() + dataArray = [] for cam2 in self.dataset.cam2: data = np.asarray(cam2) # Convert RGB to BGR From 22b4eee320c926c7028fc45d9581c595487ae733 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Mon, 27 Aug 2018 09:47:44 +0900 Subject: [PATCH 28/33] remove unused package(PIL) --- chainercv/datasets/kitti/kitti_bbox_dataset.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index a6b546d0d5..1cc220a220 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -1,5 +1,6 @@ import os import warnings + from pkg_resources import get_distribution from pkg_resources import parse_version @@ -116,7 +117,6 @@ def __init__(self, data_dir='auto', date='', drive_num='', # pykitti>=0.3.0 # get PIL Image # convert from PIL.Image to numpy - from PIL import Image dataArray = [] for cam2 in self.dataset.cam2: data = np.asarray(cam2) @@ -138,7 +138,6 @@ def __init__(self, data_dir='auto', date='', drive_num='', # pykitti>=0.3.0 # get PIL Image # convert from PIL.Image to numpy - from PIL import Image dataArray = [] for cam2 in self.dataset.cam2: data = np.asarray(cam2) From 31128453add8540fd9ab508a525330726c7e6556 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Wed, 19 Sep 2018 15:26:13 +0900 Subject: [PATCH 29/33] check stylecode(flake8) remove comment --- .../datasets/kitti/kitti_bbox_dataset.py | 3 - chainercv/datasets/kitti/kitti_utils.py | 61 ++++--------------- 2 files changed, 11 insertions(+), 53 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index 1cc220a220..e415411f87 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -230,7 +230,4 @@ def _get_annotations(self, i): else: np_label = np.array(label[i], dtype=np.int32) - # debug print - # print(np_bbox) - # print(np_label) return np_bbox, np_label diff --git a/chainercv/datasets/kitti/kitti_utils.py b/chainercv/datasets/kitti/kitti_utils.py index 04df2313c0..f8ca7fd8d4 100644 --- a/chainercv/datasets/kitti/kitti_utils.py +++ b/chainercv/datasets/kitti/kitti_utils.py @@ -80,20 +80,10 @@ def get_kitti_nosync_data(root, date, drive_num, tracklet): def get_kitti_tracklets(data_root, date, drive_num): # read calibration files kitti_dir = os.path.join(data_root, date) - # kitti_dir = kitti_dir.replace(os.path.sep, '/') - # calibration_dir = os.path.join(data_root, date) - # imu2velo = read_calib_file( - # os.path.join(kitti_dir, "calib_imu_to_velo.txt")) - # velo2cam = read_calib_file( - # os.path.join(kitti_dir, "calib_velo_to_cam.txt")) - # cam2cam = read_calib_file( - # os.path.join(kitti_dir, "calib_cam_to_cam.txt")) + # read tracklet folder = date + '_drive_' + drive_num + '_sync' - # tracklet = read_tracklet_file( - # os.path.join(kitti_dir, folder, "calib_imu_to_velo.txt")) - # return tracklets - # get dir names + # read tracklets from file tracklet_filepath = os.path.join(kitti_dir, folder, 'tracklet_labels.xml') tracklets = xmlParser.parseXML(tracklet_filepath) @@ -113,28 +103,21 @@ def get_kitti_label(tracklets, calib, if tracklets is None: return bboxes, labels - # set ndarray - # bboxes = np.zeros(framelength, dtype=np.float32) - # labels = np.zeros(framelength, dtype=np.int32) - - # twoPi = 2.*np.pi # loop over tracklets for iTracklet, tracklet in enumerate(tracklets): - # print('tracklet {0: 3d}: {1}'.format(iTracklet, tracklet)) - # this part is inspired by kitti object development kit # matlab code: computeBox3D - h, w, l = tracklet.size + # h: height + # w: width + # lg : length + h, w, lg = tracklet.size # in velodyne coordinates around zero point and without orientation yet tracklet_box = np.array([ - [-l/2, -l/2, l/2, l/2, -l/2, -l/2, l/2, l/2], - [w/2, -w/2, -w/2, w/2, w/2, -w/2, -w/2, w/2], - [0.0, 0.0, 0.0, 0.0, h, h, h, h]]) + [-lg/2, -lg/2, lg/2, lg/2, -lg/2, -lg/2, lg/2, lg/2], + [ w/2, -w/2, -w/2, w/2, w/2, -w/2, -w/2, w/2], + [ 0.0, 0.0, 0.0, 0.0, h, h, h, h]]) - # print('tracklet_box : ' + tracklet_box) - # print(tracklet_box) objtype_str = tracklet.objectType - # print(objtype_str) # loop over all data in tracklet for translation, rotation, state, occlusion, truncation, \ @@ -164,26 +147,18 @@ def get_kitti_label(tracklets, calib, # makes quite a difference for objects in periphery! # Result is in [0, 2pi] x, y, z = translation - # print(translation) + # yawVisual = ( yaw - np.arctan2(y, x) ) % twoPi - # print(yaw) - # print(yawVisual) # param = pykitti.utils.transform_from_rot_trans( # rot_mat, translation) - # print(param) # projection to image? - # print(calib.P_rect_20) # param3 = translation.reshape(3, 1) * calib.P_rect_20 - # print(cornerpos_in_velo[:, 0:1].shape) pt3d = np.vstack((cornerpos_in_velo[:, 0:8], np.ones(8))) - # print(pt3d.shape) - # print(calib.P_rect_20) pt2d = project_velo_points_in_img( pt3d, calib.T_cam2_velo, cur_rotation_matrix, cur_position_matrix) - # print(pt2d) xmin = min(pt2d[0, :]) xmax = max(pt2d[0, :]) ymin = min(pt2d[1, :]) @@ -210,25 +185,11 @@ def get_kitti_label(tracklets, calib, ymax = 375.0 param = np.array((ymin, xmin, ymax, xmax), dtype=np.float32) - # print(param) - # bbox.append(param) - # bbox = np.stack(bbox).astype(np.float32) - # bboxes[absoluteFrameNumber] = bbox bboxes[absoluteFrameNumber].append(param) - # print(bboxes[absoluteFrameNumber]) - - # param_3d = cornerpos_in_velo - # bboxes_3d[absoluteFrameNumber].append(cornerpos_in_velo) - # label.append(param2) - # label = np.stack(label).astype(np.int32) - # labels[absoluteFrameNumber] = label - # objectType - # label_names + # not search objtype_str? process param2 = kitti_bbox_label_names.index(objtype_str) labels[absoluteFrameNumber].append(param2) - # labels[absoluteFrameNumber] = param2 - # print(bboxes[absoluteFrameNumber]) # end : for all frames in track # end : for all tracks From 10752b985e5162d36aaa5bd8bab5984bf7c24454 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Wed, 19 Sep 2018 16:18:15 +0900 Subject: [PATCH 30/33] code style check(flake8) --- chainercv/datasets/kitti/kitti_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_utils.py b/chainercv/datasets/kitti/kitti_utils.py index f8ca7fd8d4..6cbfc9e797 100644 --- a/chainercv/datasets/kitti/kitti_utils.py +++ b/chainercv/datasets/kitti/kitti_utils.py @@ -114,8 +114,8 @@ def get_kitti_label(tracklets, calib, # in velodyne coordinates around zero point and without orientation yet tracklet_box = np.array([ [-lg/2, -lg/2, lg/2, lg/2, -lg/2, -lg/2, lg/2, lg/2], - [ w/2, -w/2, -w/2, w/2, w/2, -w/2, -w/2, w/2], - [ 0.0, 0.0, 0.0, 0.0, h, h, h, h]]) + [w/2, -w/2, -w/2, w/2, w/2, -w/2, -w/2, w/2], + [0.0, 0.0, 0.0, 0.0, h, h, h, h]]) objtype_str = tracklet.objectType From 016b0cc32c3d31d6b57aa2b61075064d6834dfdc Mon Sep 17 00:00:00 2001 From: sirokujira Date: Sat, 22 Sep 2018 07:29:22 +0900 Subject: [PATCH 31/33] fix comment --- chainercv/datasets/kitti/kitti_bbox_dataset.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index e415411f87..a97d354ba0 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -32,11 +32,12 @@ def _check_available(): 'pykitti is not installed in your environment,' 'so the dataset cannot be loaded.' 'Please install pykitti to load dataset.\n\n' - '$ pip install pykitti==0.2.4') + '$ pip install pykitti') class KITTIBboxDataset(GetterDataset): - r"""Image dataset for test split of `KITTI dataset`_. + + """Image dataset for test split of `KITTI dataset`_. .. _`KITTI dataset`: http://www.cvlibs.net/datasets/kitti/raw_data.php @@ -67,8 +68,8 @@ class KITTIBboxDataset(GetterDataset): ":math:`(y_{min}, x_{min}, y_{max}, x_{max})`" :obj:`label`, scalar, :obj:`int32`, ":math:`[0, \#class - 1]`" - .. [#kitti_bbox_1] If :obj:`use_pykitty = False`, \ - :obj:`bbox` and :obj:`label` not contain instances. + .. [#kitti_bbox_1] If :obj:`tracklet = True`, \ + :obj:`bbox` and :obj:`label` contain crowded instances. """ def __init__(self, data_dir='auto', date='', drive_num='', @@ -98,9 +99,6 @@ def __init__(self, data_dir='auto', date='', drive_num='', date, drive_num, self.tracklet) # use pykitti - # read All images - # imformat='None' - # self.dataset = pykitti.raw(data_dir, date, drive_num, frames=None) self.dataset = pykitti.raw( data_dir, date, drive_num, frames=None, imformat='cv2') @@ -189,8 +187,6 @@ def __init__(self, data_dir='auto', date='', drive_num='', self.__len__()) self.add_getter('img', self._get_image) - # self.add_getter('label', self._get_label) - # self.add_getter('bbox', self._get_bbox) self.add_getter(['bbox', 'label'], self._get_annotations) keys = ('img', 'bbox', 'label') self.keys = keys From 786197a176954f8e7c3ee32fb35c4261cf39fd92 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Wed, 31 Oct 2018 10:03:58 +0900 Subject: [PATCH 32/33] flake8 check --- chainercv/datasets/__init__.py | 2 +- .../datasets/kitti/kitti_bbox_dataset.py | 22 ++++++++++--------- chainercv/datasets/kitti/kitti_utils.py | 2 +- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/chainercv/datasets/__init__.py b/chainercv/datasets/__init__.py index d9c261e769..c4db3fedf3 100644 --- a/chainercv/datasets/__init__.py +++ b/chainercv/datasets/__init__.py @@ -23,7 +23,7 @@ from chainercv.datasets.kitti.kitti_utils import kitti_bbox_label_colors # NOQA from chainercv.datasets.kitti.kitti_utils import kitti_bbox_label_names # NOQA from chainercv.datasets.kitti.kitti_utils import kitti_date_lists # NOQA -from chainercv.datasets.kitti.kitti_utils import kitti_date_num_dictionaries # NOQA +from chainercv.datasets.kitti.kitti_utils import kitti_date_num_dicts # NOQA from chainercv.datasets.kitti.kitti_utils import kitti_ignore_bbox_label_color # NOQA from chainercv.datasets.kitti.parseTrackletXML import parseXML # NOQA from chainercv.datasets.mixup_soft_label_dataset import MixUpSoftLabelDataset # NOQA diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index 16727f6e7d..24c025b2c7 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -24,7 +24,7 @@ from chainercv.datasets.kitti.kitti_utils import get_kitti_sync_data from chainercv.datasets.kitti.kitti_utils import get_kitti_tracklets from chainercv.datasets.kitti.kitti_utils import kitti_date_lists -from chainercv.datasets.kitti.kitti_utils import kitti_date_num_dictionaries +from chainercv.datasets.kitti.kitti_utils import kitti_date_num_dicts def _check_available(): @@ -55,9 +55,11 @@ class KITTIBboxDataset(GetterDataset): instances. Please see more detail in the Fig. ... (?) of the summary paper [#]_. - .. [#] Andreas Geiger and Philip Lenz and Christoph Stiller and Raquel Urtasun. \ + .. [#] Andreas Geiger and Philip Lenz \ + and Christoph Stiller and Raquel Urtasun. \ `Vision meets Robotics: The KITTI Dataset \ - `_. Geiger2013IJRR. + `_. \ + Geiger2013IJRR. Args: @@ -98,14 +100,14 @@ def __init__(self, data_dir='auto', date='', drive_num='', self.is_left = is_left if date not in kitti_date_lists: - raise ValueError('\'date\' argment must be one of the ' + - str(date_lists) + 'values.') + raise ValueError('\'date\' argment must be one of the ' + + str(kitti_date_lists) + 'values.') # date(key map) # if drive_num not in ['0001', '0002', ...]: - if drive_num not in kitti_date_num_dictionaries[date]: - raise ValueError('\'drive_num\' argment must be one of the ' + - str(kitti_date_num_dictionaries[date]) + 'values.') + if drive_num not in kitti_date_num_dicts[date]: + raise ValueError('\'drive_num\' argment must be one of the ' + + str(kitti_date_num_dicts[date]) + 'values.') if date == '2011_09_26': self.tracklet = tracklet @@ -128,8 +130,8 @@ def __init__(self, data_dir='auto', date='', drive_num='', raise ValueError( 'kitti dataset does not exist at the expected location.' 'Please download it from http://www.cvlibs.net/datasets/kitti/' - 'Then place directory "date + "_drive_" + drive_num" at {} and {} at {}.'.format( - os.path.join(data_dir, 'date + "_drive_" + drive_num'), resol, label_dir)) + 'Then place directory at {}.' + .format(os.path.join(data_dir, date + '_drive_' + drive_num))) # use pykitti self.dataset = pykitti.raw( diff --git a/chainercv/datasets/kitti/kitti_utils.py b/chainercv/datasets/kitti/kitti_utils.py index 379b1859a9..fd4670d869 100644 --- a/chainercv/datasets/kitti/kitti_utils.py +++ b/chainercv/datasets/kitti/kitti_utils.py @@ -252,7 +252,7 @@ def project_velo_points_in_img(pts3d, transform_cam_velo, kitti_date_lists = ['2011_09_26', '2011_09_28', '2011_09_29', '2011_09_30', '2011_10_03'] -kitti_date_num_dictionaries = { +kitti_date_num_dicts = { # calibration date_num 0119(not nse) '2011_09_26': ['0001', '0002', '0005', '0009', '0011', '0013', '0014', '0015', '0017', '0018', '0019', '0020', '0022', '0023', From 4396fc129a2c869d6d8d05260dd54d2cbcc3e973 Mon Sep 17 00:00:00 2001 From: sirokujira Date: Tue, 6 Nov 2018 18:01:01 +0900 Subject: [PATCH 33/33] Changed download folder path to use lower case letters. fixed comment. --- .../datasets/kitti/kitti_bbox_dataset.py | 42 ++++++++----------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/chainercv/datasets/kitti/kitti_bbox_dataset.py b/chainercv/datasets/kitti/kitti_bbox_dataset.py index 24c025b2c7..655d10f901 100644 --- a/chainercv/datasets/kitti/kitti_bbox_dataset.py +++ b/chainercv/datasets/kitti/kitti_bbox_dataset.py @@ -38,33 +38,12 @@ def _check_available(): class KITTIBboxDataset(GetterDataset): - """Image dataset for test split of `KITTI dataset`_. + """Bounding box dataset for `KITTI dataset`_. .. _`KITTI dataset`: http://www.cvlibs.net/datasets/kitti/raw_data.php - When queried by an index, if :obj:`return_crowded == False`, - this dataset returns a corresponding - :obj:`img, bbox, mask, label, crowded, area`, a tuple of an image, bounding - boxes, masks, labels, crowdness indicators and areas of masks. - The parameters :obj:`return_crowded` and :obj:`return_area` decide - whether to return :obj:`crowded` and :obj:`area`. - :obj:`crowded` is a boolean array - that indicates whether bounding boxes are for crowd labeling. - When there are more than ten objects from the same category, - bounding boxes correspond to crowd of instances instead of individual - instances. Please see more detail in the Fig. ... (?) of the summary - paper [#]_. - - .. [#] Andreas Geiger and Philip Lenz \ - and Christoph Stiller and Raquel Urtasun. \ - `Vision meets Robotics: The KITTI Dataset \ - `_. \ - Geiger2013IJRR. - - Args: - data_dir (string): Path to the dataset directory. The directory should - contain the :obj:`---` directory. If this is + data_dir (string): Path to the root of the training data. If this is :obj:`auto`, this class will automatically download data for you under :obj:`$CHAINER_DATASET_ROOT/pfnet/chainercv/kitti`. date ({'2011_09_26', '2011_09_28', '2011_09_29', @@ -88,6 +67,19 @@ class KITTIBboxDataset(GetterDataset): .. [#kitti_bbox_1] If :obj:`tracklet = True`, \ :obj:`bbox` and :obj:`label` contain crowded instances. + + When queried by an index, if :obj:`tracklet == True`, + this dataset returns a corresponding + :obj:`img, bbox, label`, a tuple of an image, bounding boxes, labels. + + Please see more detail in the Fig. 6 of the summary paper [#]_. + + .. [#] Andreas Geiger and Philip Lenz \ + and Christoph Stiller and Raquel Urtasun. \ + `Vision meets Robotics: The KITTI Dataset \ + `_. \ + Geiger2013IJRR. + """ def __init__(self, data_dir='auto', date='', drive_num='', @@ -118,12 +110,12 @@ def __init__(self, data_dir='auto', date='', drive_num='', if sync is True: # download sync data data_dir = get_kitti_sync_data( - os.path.join('pfnet', 'chainercv', 'KITTI'), + os.path.join('pfnet', 'chainercv', 'kitti'), date, drive_num, self.tracklet) else: # download nosync data data_dir = get_kitti_nosync_data( - os.path.join('pfnet', 'chainercv', 'KITTI'), + os.path.join('pfnet', 'chainercv', 'kitti'), date, drive_num, self.tracklet) if not os.path.exists(data_dir) or not os.path.exists(data_dir):