def get_anno(db_path): # target='' # TO Move to generation of data db. viewID2quat = pickle.load(open(os.path.join(db_path, 'viewID2quat.pkl'), 'rb'), encoding='latin1') viewID2euler = pickle.load(open(os.path.join(db_path, 'viewID2euler.pkl'), 'rb'), encoding='latin1') keys = np.array(list(viewID2quat.keys())) add_path(this_dir) from db_type import img_view_anno rcs = np.zeros((len(viewID2quat.keys()), ), dtype=img_view_anno).view(np.recarray) for i, (key, quat) in enumerate(viewID2quat.items()): rc = rcs[i] rc.img_id = key # bathtub_0107.v001 cad_id, viewId = key.split('.') category = cad_id[:cad_id.rfind('_')] rc.category = category rc.cad_id = cad_id rc.so3.quaternion = quat if quat[ 0] > 0 else -quat # q and -q give the same rotation matrix. # Make sure all q[0]>0, that is rotation angle in [0,pi] rc.so3.euler = viewID2euler[key] return keys, rcs
def import_module_v2(info): # for module_name, info in module_infos: print("[import_module] ", info['from']) if 'path' in info: add_path(info['path']) print(' add_path: ', info['path']) mod = _import_module(info['from']) if 'import' in info: comps = [] for comp, kwargs in info['import'].items(): # print '>>>>', comp, kwargs try: if kwargs is None: # comp is variable _var = getattr(mod, comp) comps.append(_var) else: # comp is function with kwargs _func = getattr(mod, comp) comps.append((_func, kwargs)) # (_func(**kwargs)) # except Exception as inst: print('\n[Exception] %s' % inst) pprint(dict(info)) # (comp, kwargs) return comps else: return mod
def __init__(self, *args, **kwargs): Dataset_Base.__init__(self, *args, **kwargs) from basic.common import add_path, env add_path(env.Home + '/working/eccv18varpose/code/prepare_data/rotationAnno/') from prepare_rot3d_anno import get_rotAnno_tbls self.rotAnno_tb = get_rotAnno_tbls(self.cates, collection=self.collection, filter='all', quiet=True)
def import_module(info): # for module_name, info in module_infos: print("[import_module] ", info['from']) if 'path' in info: add_path(info['path']) print(' add_path: ', info['path']) mod = _import_module(info['from']) if 'import' in info: comps = [] for comp in info['import']: comps.append(getattr(mod, comp)) return comps else: return mod
import numpy as np from math import ceil, floor, pi import torch from torch.utils.data import Dataset, DataLoader from torchvision import transforms, utils # from basic.common import add_path, env, rdict, cv2_wait, cv2_putText from lmdb_util import ImageData_lmdb from numpy_db import npy_table, npy_db, dtype_summary, reorder_dtype print('[Dataset_Base]: [TODO] get rid of the relative paths ../../') this_dir = os.path.dirname(os.path.realpath(__file__)) root_dir = os.path.realpath(this_dir + '/../../..') add_path(root_dir + '/dataset') from Pascal3D import categories, get_anno_dbs_tbl, get_anno_db_tbl __all__ = ['Dataset_Base', 'netcfg'] # syn_data_path = root_dir + '/dataset/Pascal3D/SynImages_r4cnn.cache' syn_imdb_path = os.path.join(syn_data_path, 'ImageData_gtbox_crop.Rawpng.lmdb') syn_anno_path = os.path.join(syn_data_path, 'obj_anno_db/{cate}.pkl') pascal3d_imdb_path = root_dir + '/dataset/Pascal3D/ImageData.Rawjpg.lmdb' pascal3d_gt_box_path = root_dir + '/dataset/Pascal3D/anno_db_v2/data.cache/objId2gtbox/cate12_{collection}.all.pkl' collection2filter = rdict(train='all', val='easy') ## Net configurations that are independent of task
def dtype_summery(dtype_list, dtype_name=''): from tabulate import tabulate this_dt = np.dtype(dtype_list) name2dt_off = this_dt.fields name2dt_off = [(name, dt.itemsize, off) for name, (dt, off) in name2dt_off.iteritems()] fields_list = sorted( name2dt_off, key=lambda x: x[2]) # ('field_name', 'itemsize', 'offset') # print tabulate(fields_list, headers=['field_name', 'itemsize', 'offset'], tablefmt='pipe') field_size = [(filedname, itemsize) for filedname, itemsize, offset in fields_list] print( tabulate(field_size, headers=['field_name', 'itemsize'], tablefmt='pipe')) if __name__ == '__main__': from basic.common import env, add_path add_path(env.Home + '/working/cvpr18align/dataset/') from PASCAL3D import * # PASCAL3D_Dir, categories, category2nrmodel, get_anno dtype_summery(viewpoint, 'viewpoint ') dtype_summery(proj_info, 'proj_info ') dtype_summery(image_info, 'image_info ') dtype_summery(object_anno, 'object_anno') dtype_summery(pose_hypo, 'pose_hypo ')
""" @Author : Shuai Liao """ import os, sys import numpy as np import cv2 from basic.common import env, add_path, cv2_wait from numpy_db import npy_table, npy_db, dtype_summary, reorder_dtype this_dir = os.path.realpath(os.path.dirname(__file__)) add_path(this_dir) from db_type import viewpoint, proj_info, image_info, object_anno PASCAL3D_Dir = os.path.realpath(this_dir + '/../') def get_anno_db_tbl(cate, collection="val", filter='easy', img_scale='Org', withCoarseVp=True, quiet=True): # obtain obj annotation for dataset anno_db. assert img_scale in ['Max500', 'Org'] if filter in ['easy', 'all', 'nonOccl', 'nonDiff']: filterStr = filter + '_withCoarseVp' if withCoarseVp else filter anno_folder = 'data.cache/{}.{}'.format(filterStr, img_scale) # else: print("Unidentified filter name=", filter) raise NotImplementedError
# from basic.common import Open, env, add_path, RefObj as rdict, argv2dict # from pytorch_util.libtrain.yaml_netconf import parse_yaml, import_module_v2 from pytorch_util.libtrain.tools import get_stripped_DataParallel_state_dict, patch_saved_DataParallel_state_dict from txt_table_v1 import TxtTable # from tensorboardX import SummaryWriter #=========== Parsing from working path ========= base_dir = os.path.dirname(os.path.abspath(__file__)) # parsing current dir pwd = os.getcwd() MtdFamily, MtdType = pwd.split(os.sep)[-2:] #================================================ add_path(base_dir + '/lib') #------- args from convenient run yaml --------- # For the purpose that no need to specific each run (without argparse) convenience_run_argv_yaml=\ ''' MtdFamily : {MtdFamily} MtdType : {MtdType} net_module : {net_module} net_arch : {net_arch} base_dir : {base_dir} LIB_DIR : {base_dir}/lib work_dir : './snapshots/{net_arch}' nr_epoch : 20 # 60
# [Table] for storing object annotation for Pascal3D+/Objectnet3D object_anno = [ ('obj_id' , (str,MAX_STRING_LEN) ), # <primary key> format: obj_id = {image_id}-{x1},{y1},{x2},{y2} e.g. 2008_000251-24,14,410,245 #--------------------- ('category' , (str,MAX_STRING_LEN) ), # e.g. aeroplane, car, bike ('cad_id' , (str,MAX_STRING_LEN) ), # e.g. aeroplane01 {category}{cad_idx} ('bbox' , np.float64 , 4 ), # xmin,ymin,xmax,ymax (float is because of scaling) #----- source image info. (src_img.ratio indicate what size of image the anno based on.) ('src_img' , image_info ), #----- viewpoint (camera model parameters) ('gt_view' , viewpoint ), #----- projection infor. # proj = proj_info # not in use for this project. #----- Other annotation. ('difficult' , np.int32 ), ('truncated' , np.bool ), ('occluded' , np.bool ), ] if __name__=="__main__": import os, sys from basic.common import env, add_path add_path(env.Home+'/WorkGallery/pytools/numpy_db') from formater import get_code_blocks, block2list_code this_script = os.path.basename(__file__) assert this_script.endswith('.def.py') out_script = this_script.replace('.def.py', '.py') # db_type.def.py --> db_type.py
@Author : Shuai Liao """ import os, sys from basic.common import add_path, env import numpy as np from scipy.linalg import logm, norm from math import pi, sqrt from multiprocessing import Pool from txt_table_v1 import TxtTable # add_path(env.Home+'/working/eccv18varpose/dataset') # from PASCAL3D import get_anno_dbs_tbl, get_anno, categories this_dir = os.path.dirname(os.path.realpath(__file__)) add_path(this_dir + '/../../../dataset') from Pascal3D import get_anno_dbs_tbl, get_anno, categories def compute_RotMats(a, e, t): """ #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!# # Warning from Shuai # # # # This function is just a replication of matlab implementation for reproducibility purpose only! # # However, I believe the logic is not correct. But since Pascal3D+ dataset itself is annotated # # in such way, we have to follow this definition for evaluation purpose. # # # # In short words: The resulting rotation matrix can still be valid since it guarantees the CAD model # # to be projected roughly aligned with the 2D object in image. However, the way in interpreting # # a, e, t used in this function to construct the rotation matrix is deviated from the true definition #
import os, sys import pickle from easydict import EasyDict as edict from collections import OrderedDict as odict import cv2 import numpy as np from basic.common import env, Open, add_path from basic.util import load_yaml from numpy_db import npy_table, npy_db, dtype_summary, reorder_dtype add_path('../anno_db_v2') from db_type import viewpoint, proj_info, image_info, object_anno # , pose_hypo from util_v2 import rescale_anno conf = load_yaml('config.yml') # odict Pascal3D_root = os.path.expanduser(conf['Pascal3D_release_root']) # Pascal3D_root = env.Home+'/working/cvpr17pose/dataset/PASCAL3D+_release1.1' # Image_sets/%s_imagenet_%s.txt PascalVOC_root = Pascal3D_root + '/PASCAL' assert os.path.exists( Pascal3D_root), "Please replace with your path/to/PASCAL3D+_release1.1" assert os.path.exists(PascalVOC_root), "Cannot find %s" % PascalVOC_root protocol = conf[ 'pkl_protocol'] # pickle dump protocol. Change -1 to 2 for python2.x compatibility. if True: print('\n\n--------- viewpoint --------') dtype_summary(np.dtype(viewpoint)) print('\n\n--------- proj_info --------') dtype_summary(np.dtype(proj_info))
""" @Author : Shuai Liao """ import os, sys from basic.common import env, add_path, Open add_path('../../') from Pascal3D import get_imgIDs, get_anno, categories, category2nrmodel import numpy as np import pickle from basic.util import load_yaml from tqdm import tqdm conf = load_yaml('config.yml') # odict Pascal3D_root = os.path.expanduser(conf['Pascal3D_release_root']) protocol = conf[ 'pkl_protocol'] # pickle dump protocol. Change -1 to 2 for python2.x compatibility. base_dir = '../' imgID2size = pickle.load( open(os.path.join(base_dir, 'ImageData.Rawjpg.lmdb/imgID2size.pkl'), 'rb')) #---------------------------------------------------------------------------------- # Note: # The purpose of the script is to generate objId2gtbox.pkl # Whereas rcobj has gt_bbox field, however, we still regenerate objId2gtbox.pkl # to make sure these gt_bbox is properly clamped by image boarding, # and has no problem in cropping, resizing (to avoid exception during training). #----------------------------------------------------------------------------------
def gen_anno_db(cate, MAX_STRING_LEN=64): import numpy as np from numpy_db import npy_table, npy_db, dtype_summary, reorder_dtype add_path('../../') from Pascal3D import image_info, object_anno cate2imgIDs = pickle.load( open(os.path.join(base_dir,'cate2imgIDs.pkl'), 'rb') ) imgID2size = pickle.load( open(os.path.join(db_path,'imgID2size.pkl'),'rb') ) # for cate in categories: if True: imgIDs = cate2imgIDs[cate] obj_rcs = np.zeros( (len(imgIDs),), dtype=object_anno ) for _k_, imgID in enumerate(imgIDs): if _k_%1000==0: print ('\r%-20s %6d / %6d ' % (cate, _k_, len(imgIDs))) sys.stdout.flush() synsetID, shapeID, a,e,t,d = imgID.split('_') # e.g. 02691156_b089abdb33c39321afd477f714c68df9_a357_e034_t-03_d002 assert a[0]=='a' and e[0]=='e' and t[0]=='t' and d[0]=='d', imgID a,e,t,d = float(a[1:]), float(e[1:]), float(t[1:]), float(d[1:]) """ NOTE: The tilt angle should be flipped as to what is written in the filename to make it consistent with PASCAL3D annotation. (https://github.com/ShapeNet/RenderForCNN)""" t = -t #h, w, c = datadb[imgID].shape #assert c==3, imgID # datadb[imgID].shape h,w = imgID2size[imgID] # It's quite annoying that 232 imgIDs (from chair category) are of 66 length, # which exceed the MAX_STRING_LEN=64 in db_type # So here the solution is as following: obj_id = imgID[:MAX_STRING_LEN] image_id = imgID[MAX_STRING_LEN:]+'.Syn' #====================================== #--[new object record] # rcobj_1arr = np.zeros( (1,), dtype=object_anno ) rcobj = obj_rcs[_k_].view(np.recarray) # cast as recarray # rcobj.obj_id = obj_id # "{}.{}".format(imgID, 'Syn') # Any obj_id ends up with '.Syn' is from this dataset. rcobj.category = cate # obj['class'] # cate rcobj.cad_id = shapeID # "%s%02d" % (obj['class'], obj.cad_index) rcobj.bbox[:] = [0,0,w,h] # just the image boarder (x1,y1,x2,y2). #-----[source image] rcobj.src_img.image_id = image_id # imgID rcobj.src_img.H = h # Original image size rcobj.src_img.W = w rcobj.src_img.C = 3 rcobj.src_img.h = h # The size of image this annotation based on, rcobj.src_img.w = w # it can be different from original image if rescaled by MAX_IMAGE_SIDE. #-----[viewpoint] rcobj.gt_view.a = a rcobj.gt_view.e = e rcobj.gt_view.t = t rcobj.gt_view.d = d # Note: if distance=0, this means a,e field is coarse vp annotation. rcobj.gt_view.px = 0 # --- Not-Available! --- rcobj.gt_view.py = 0 # --- Not-Available! --- rcobj.gt_view.f = 0 # --- Not-Available! --- rcobj.gt_view.mx = 0 # --- Not-Available! --- rcobj.gt_view.my = 0 # --- Not-Available! --- #--other annotation. rcobj.difficult = 0 # --- Not-Available! --- rcobj.truncated = 0 # --- Not-Available! --- rcobj.occluded = 0 # --- Not-Available! --- #====================================== # # in_file_path = os.path.join(out_dir, 'obj_anno_dump/%s.pkl' % cate) # obj_rcs = pickle.load(open(in_file_path)) # create npy_table and npy_db, and dumpy it. out_file_path = os.path.join(base_dir, 'obj_anno_db/%s.pkl' % cate) obj_tb = npy_table(obj_rcs) db = npy_db() db.add_table(obj_tb, name="obj_tb") db.dump(out_file_path) print ('%-20s %6d [dump to] %s' % (cate, len(imgIDs), out_file_path))
from basic.util import load_yaml, parse_yaml import os, sys, time, shutil from time import gmtime, strftime import numpy as np import pickle from math import pi from easydict import EasyDict as edict from ordered_easydict import Ordered_EasyDict as oedict from pprint import pprint from tensorboardX import SummaryWriter from tqdm import tqdm this_dir = os.path.dirname(os.path.realpath(__file__)) add_path(this_dir + '/../dataset') from Pascal3D import categories this_dir = os.path.dirname(os.path.abspath(__file__)) add_path(this_dir) from pytorch_util.libtrain.tools import get_stripped_DataParallel_state_dict, patch_saved_DataParallel_state_dict from lib.eval.eval_aet_multilevel import eval_cates, compute_geo_dists from txt_table_v1 import TxtTable DEBUG = True # False #------- args from convenient run yaml --------- # For the purpose that no need to specific each run (without argparse) convenience_run_argv_yaml=\ '''
import os import torch.nn as nn import torch.utils.model_zoo as model_zoo from torch.autograd import Variable import torch from basic.common import rdict import numpy as np from easydict import EasyDict as edict from collections import OrderedDict as odict from itertools import product from basic.common import add_path, env this_dir = os.path.dirname(os.path.abspath(__file__)) add_path(this_dir + '/../lib/') from helper import * from model import VGG16_Trunk from modelSE import VGG16_Trunk as VGG16SE_Trunk # net_arch2Trunk = dict( # vgg16 = VGG16_Trunk, # vgg16se = VGG16SE_Trunk, # ) net_arch2Trunk = dict(vgg16=dict( Sflat=VGG16_Trunk, Sexp=VGG16SE_Trunk, ), ) from pytorch_util.libtrain import copy_weights, init_weights_by_filling from pytorch_util.torch_v4_feature import LocalResponseNorm # *