def save_rgbd(self, dirName=''): count = 0 imName = osp.join(dirName, 'im%06d.png') dpName = osp.join(dirName, 'dp%06d.png') while True: isExist = self.read_next() if isExist is None: break im, d = self.to_rgbd() print im.shape, d.shape ou.mkdir(osp.dirname(imName)) scm.imsave(imName % (count+1), im) scm.imsave(dpName % (count+1), d) count += 1
def produce(self, ip): """ Given vatic identifier string, create vatic output text and return file path :param ip: vatic identifier string :return: vatic output text file path """ # turkic dump identifier -o output.txt --merge --merge-threshold 0.5 basePath = self.prms_.vaticOpDr ou.mkdir(basePath) vaticFile = 'vaticOutput{0}.txt'.format(ip) turkicCmd = 'turkic dump {0} -o {1} --merge --merge-threshold %f; ' turkicCmd = (turkicCmd % self.prms_.mergeThreshold).format(ip, vaticFile) print (turkicCmd) sysCall = '(' + \ 'cd {0}; '.format(osp.join(self.prms_.vaticDr, 'vatic')) + turkicCmd + \ 'mv {0} {1}'.format(vaticFile, basePath) + \ ')' os.system(sysCall) return osp.join(basePath, vaticFile)
def save_crops(self, rootFolder, tgtDir, numIm=None): ''' rootFolder: the root folder for the window file tgtDir : the directory where the images should be saved ''' count = 0 readFlag = True ou.mkdir(tgtDir) while readFlag: ims, _, imNames, oNames = self.read_next_processed(rootFolder, returnName=True) for im, name, oName in zip(ims, imNames, oNames): svName = osp.join(tgtDir, oName) scm.imsave(svName, im) if self.is_eof(): readFlag = False count += 1 if numIm is not None and count >= numIm: readFlag = False
def save_collisions_seq(folderName, isSubFolder=True): fNames = [f for f in os.listdir(folderName) if osp.isdir(osp.join(folderName,f))] srcNames = [osp.join(folderName, f) for f in fNames] if isSubFolder: outFolder = '/' + folderName.strip('/') + '-collisions-only' else: outFolder = '/' + folderName.strip('/') + '-collisions-only-nosub' outNames = [osp.join(outFolder, f) for f in fNames] outCount = 0 for ifd,ofd in zip(srcNames, outNames): print (ifd, ofd) allfIdx, allbCol = detect_collision(ifd) inMat = sio.loadmat(osp.join(ifd, 'data.mat')) inIm = osp.join(ifd, 'im%06d.jpg') inPos, inForce = inMat['position'], inMat['force'] colIdx = [] for b in range(len(allfIdx)): colIdx = colIdx + allfIdx[b] if isSubFolder: outCount = 0 for c in colIdx: st = max(0, c - 7) en = min(c+7, inPos.shape[1]) if isSubFolder: colFolder = osp.join(ofd, 'sub-%04d' % outCount) else: colFolder = osp.join(outFolder, 'seq%06d' % outCount) outIm = osp.join(colFolder, 'im%06d.jpg') ou.mkdir(colFolder) outCount += 1 #Copy the images for i,idx in enumerate(range(st, en)): oIm = outIm % i iIm = inIm % idx callStr = 'cp %s %s' % (iIm, oIm) #print (i, idx, callStr) subprocess.check_call([callStr], shell=True) #Copy the data oPos, oForce = inPos[:,st:en], inForce[:,st:en] datFile = osp.join(colFolder, 'data.mat') sio.savemat(datFile, {'position': oPos, 'force': oForce}) return colCount, bColCount
def __init__(self, modelName=None, logDir='tf_logs/', modelDir='tf_models/', outputDir='tf_outputs/'): self.g_ = tf.Graph() self.lossCollection_ = 'losses' self.modelName_ = modelName self.logDir_ = logDir self.modelDir_ = modelDir self.outputDir_ = outputDir if modelName is not None: self.logDir_ = osp.join(self.logDir_, modelName) self.modelDir_ = osp.join(self.modelDir_, modelName) self.outputDir_ = osp.join(self.outputDir_, modelName) ou.mkdir(self.logDir_) ou.mkdir(self.modelDir_) ou.mkdir(self.outputDir_) print "View outputs with:" print "tail -F " + self.outputDir_+ "/" print "Launch tensorboard for this network:" print "tensorboard --logdir " + self.logDir_ self.summaryWriter_ = None
def create_pascal_filestore(imSz=256, padSz=24, debugMode=False): dName = '/data0/pulkitag/data_sets/pascal_3d/imCrop' dName = osp.join(dName, 'imSz%d_pad%d_hash') % (imSz, padSz) svFile = 'f%s/im%s.jpg' % (imSz, padSz, '%d', '%d') srcDir = '/data0/pulkitag/pascal3d/Images' setName = ['train', 'test'] count, fCount = 0, 0 fStore = edict() for si, s in enumerate([setName[0]]): inName = 'pose-files/annotations_master_%s_pascal3d.txt' % s storeFile = 'pose-files/pascal3d_dict_%s_imSz%d_pdSz%d.pkl' % (s, imSz, padSz) inFid = mpio.GenericWindowReader(inName) imDat, lbls = [], [] inFid.num_ = inFid.num_ - 1000 N = inFid.num_ for i in range(inFid.num_): im, lb = inFid.read_next() imDat.append(im) lbls.append(lb) inFid.close() randSeed = 7 randState = np.random.RandomState(randSeed) perm = randState.permutation(N) if s == 'train': numBad = 2 else: numBad = 0 count = 0 print (len(perm)) imList = [] lbList = [] for rep, p in enumerate(perm): if np.mod(rep,1000)==1: print (rep) im, lb = imDat[p], lbls[p] lb = lb[0] fName, ch, h, w, x1, y1, x2, y2 = im[0].strip().split() x1, y1, x2, y2, h, w = int(x1), int(y1), int(x2), int(y2), int(h), int(w) if x2 <= x1 or y2 <= y1: print ('Size is weird', x1,x2,y1,y2) print ('Skipping', s, im) continue if x1 <0 or y1<0: print ('Too small', x1, y1) continue if x2 > w or y2 > h: print ('Too big', x2, w, y2, h) continue fPrefix = fName[0:-4] svImName = svFile % (fCount, np.mod(count,1000)) lbFormat = format_raw_label(lb) if fPrefix not in fStore.keys(): fStore[fPrefix] = edict() fStore[fPrefix].name = [svImName] fStore[fPrefix].coords = [(x1,y1,x2,y2)] fStore[fPrefix].lbs = [lbFormat] else: fStore[fPrefix].name.append(svImName) fStore[fPrefix].coords.append((x1,y1,x2,y2)) fStore[fPrefix].lbs.append(lbFormat) count += 1 if np.mod(count,1000) == 0: fCount += 1 #Read and crop the image xOg1, yOg1, xOg2, yOg2 = x1, y1, x2, y2 x1, y1, x2, y2 , xPad, yPad= crop_for_imsize((h, w, x1, y1, x2, y2), imSz, padSz) im = scm.imread(osp.join(srcDir, fName)) if im.ndim == 2: im = color.gray2rgb(im) hIm, wIm, chIm = im.shape assert hIm==h and wIm==w and chIm==3,(hIm, wIm, chIm, h, w) im = cv2.resize(im[y1:y2, x1:x2,:], (imSz, imSz), interpolation=cv2.INTER_LINEAR) svImName = osp.join(dName, svImName) ou.mkdir(osp.dirname(svImName)) scm.imsave(svImName, im) pickle.dump({'fStore': fStore}, open(storeFile, 'w'))
def create_window_file_v2(imSz=256, padSz=24, debugMode=False): dName = '/data0/pulkitag/data_sets/pascal_3d/imCrop' dName = osp.join(dName, 'imSz%d_pad%d_hash') % (imSz, padSz) svFile = 'f%s/im%s.jpg' % ('%d', '%d') srcDir = '/data0/pulkitag/pascal3d/Images' setName = ['train', 'test'] count, fCount = 0, 0 fStore = edict() for si, s in enumerate(setName): inName = 'pose-files/annotations_master_%s_pascal3d.txt' % s oName = 'pose-files/euler_%s_pascal3d_imSz%d_pdSz%d.txt' % (s, imSz, padSz) oFile = 'pose-files/pascal3d_dict_%s_imSz%d_pdSz%d.pkl' % (s, imSz, padSz) inFid = mpio.GenericWindowReader(inName) imDat, lbls = [], [] N = inFid.num_ for i in range(inFid.num_): im, lb = inFid.read_next() imDat.append(im) lbls.append(lb) inFid.close() randSeed = 7 randState = np.random.RandomState(randSeed) perm = randState.permutation(N) if s == 'train': numBad = 2 else: numBad = 0 print (len(perm)) imList = [] lbList = [] for rep, p in enumerate(perm): if np.mod(rep,1000)==1: print (rep, fCount, count) im, lb = imDat[p], lbls[p] lb = lb[0] srcImName, ch, h, w, x1, y1, x2, y2 = im[0].strip().split() x1, y1, x2, y2, h, w = int(x1), int(y1), int(x2), int(y2), int(h), int(w) if x2 <= x1 or y2 <= y1: print ('Size is weird', x1,x2,y1,y2) print ('Skipping', s, im) continue if x1 <0 or y1<0: print ('Too small', x1, y1) continue if x2 > w or y2 > h: print ('Too big', x2, w, y2, h) continue srcImPrefix = srcImName[0:-4] svImName = svFile % (fCount, np.mod(count,1000)) if srcImPrefix not in fStore.keys(): fStore[srcImPrefix] = edict() fStore[srcImPrefix].name = [svImName] fStore[srcImPrefix].coords = [(x1,y1,x2,y2)] else: fStore[srcImPrefix].name.append(svImName) fStore[srcImPrefix].coords.append((x1,y1,x2,y2)) count += 1 if np.mod(count,1000) == 0: fCount += 1 #Read and crop the image xOg1, yOg1, xOg2, yOg2 = x1, y1, x2, y2 x1, y1, x2, y2 , xPad, yPad= crop_for_imsize((h, w, x1, y1, x2, y2), imSz, padSz) im = scm.imread(osp.join(srcDir, srcImName)) if im.ndim == 2: im = color.gray2rgb(im) hIm, wIm, chIm = im.shape assert hIm==h and wIm==w and chIm==3,(hIm, wIm, chIm, h, w) im = cv2.resize(im[y1:y2, x1:x2,:], (imSz, imSz), interpolation=cv2.INTER_LINEAR) #get filestr fStr = get_filename(svImName) fMirrorStr = get_filename(svImName, isMirror=True) svName = osp.join(dName, fStr) ou.mkdir(osp.dirname(svName)) scm.imsave(svName, im) #print (svName) #pdb.set_trace() if debugMode: imList.append([fStr, fName, (xOg1, yOg1, xOg2, yOg2), chIm, imSz, imSz, 0, 0, imSz, imSz, xPad, yPad]) else: imList.append([fStr, (chIm, imSz, imSz), (0, 0, imSz, imSz)]) lbList.append(format_raw_label(lb)) #Mirror the image im = im[:,::-1,:] lbMirror = copy.deepcopy(lb) #For pascal images, azimuth becomes negative, but elevation doesnot change lbMirror[0] = -lb[0] svName = osp.join(dName, fMirrorStr) scm.imsave(svName, im) if debugMode: imList.append([fMirrorStr, fName, (xOg1, yOg1, xOg2, yOg2), chIm, imSz, imSz, 0, 0, imSz, imSz, xPad, yPad]) else: imList.append([fMirrorStr, (chIm, imSz, imSz), (0, 0, imSz, imSz)]) lbList.append(format_raw_label(lbMirror)) #Write to window file N = len(imList) perm = randState.permutation(N) oFid = mpio.GenericWindowWriter(oName, N, 1, 2) for p in perm: oFid.write(lbList[p], imList[p]) oFid.close() if debugMode: return imList, lbList
def save_model(self, sess, step): svPath = osp.join(self.modelDir_, 'model') ou.mkdir(svPath) self.saver_.save(sess, svPath, global_step=step)