def window_file_ptch_gt_euler_5(): prms, cPrms = mev2.ptch_pose_euler_smallnet_v5_fc5_exp1_lossl1() wTest = prms.paths['windowFile']['test'] wFid = mpio.GenericWindowReader(wTest) oName = 'ptch_test_euler-gt5.txt' readFlag = True count = 0 imDat, lbDat = [], [] while readFlag: ims, lb = wFid.read_next() if lb is None: readFlag = False continue poseLb = lb[0][0:2] mxTheta = 30 * max(np.abs(poseLb)) if mxTheta < 5 and not (lb[0][3] == 0): continue count += 1 imDat.append(ims) lbDat.append([lb[0][3]]) if wFid.is_eof(): readFlag = False wFid.close() #Outside id oFid = mpio.GenericWindowWriter(oName, count, 2, 1) for c in range(count): oFid.write(lbDat[c], *imDat[c]) oFid.close()
def make_pascal3d_generic_window(): srcFile = '/data1/pulkitag/data_sets/pascal_3d/my/window_file_%s.txt' outFile = '/data1/pulkitag/data_sets/pascal_3d/my/generic_window_file_%s.txt' setNames = ['train', 'val'] for s in setNames: iFile = srcFile % s oFile = outFile % s with open(iFile) as fi: lines = fi.readlines() N = len(lines) oFid = mpio.GenericWindowWriter(oFile, N, 1, 3) for i, l in enumerate(lines[0:100]): if np.mod(i, 1000) == 1: print(i) print l fName, y1, y2, x1, x2, az, el, cl = l.strip().split() im = cv2.imread(fName) h, w, ch = im.shape fSplit = fName.split('/') assert fSplit[-3] == 'Images' fName = fSplit[-2] + '/' + fSplit[-1] y1, y2, x1, x2 = float(y1), float(y2), float(x1), float(x2) az, el = float(az), float(el) cl = int(cl) imLine = [[fName, [ch, h, w], [x1, y1, x2, y2]]] oFid.write([az, el, cl], *imLine)
def convert_pose_ptch_2_ptch(inFile, outFile): inFid = mpio.GenericWindowReader(inFile) outFid = mpio.GenericWindowWriter(outFile, inFid.num_, 2, 1) while not inFid.is_eof(): imData, lb = inFid.read_next() lbls = [[lb[0][2]]] outFid.write(lbls[0], *imData) inFid.close() outFid.close()
def reform_window_file(fName='test-files/vegas_ptch_test.txt'): fid = mpio.GenericWindowReader(fName) oid = mpio.GenericWindowWriter('vegas.txt', fid.num_, 2, 1) readFlag = True while readFlag: ims, lbs = fid.read_next() lb = [lbs[0][0]] oid.write(lb, *ims) if fid.is_eof(): readFlag = False
def make_window_file(prms, setNames=['test', 'train']): oldState = np.random.get_state() seqCounts = ku.get_num_images() for sNum, setName in enumerate(setNames): seqNums = ku.get_train_test_seqnum(prms, setName) setSeqCount = np.array([seqCounts[se] for se in seqNums]).astype(np.float32) sampleProb = setSeqCount / sum(setSeqCount) im1, im2, ps = [], [], [] imSz1, imSz2 = [], [] totalSamples = 0 for ii, seq in enumerate(seqNums): randSeed = (101 * sNum) + 2 * seq + 1 numSamples = int( round(prms['numSamples'][setName] * sampleProb[ii])) print "Storing %d samples for %d seq in set %s" % (numSamples, seq, setName) randState = np.random.RandomState(randSeed) imT1, imT2, imSzT1, imSzT2, psT = get_imnames_and_pose( prms, seq, numSamples, randState) im1 = im1 + imT1 im2 = im2 + imT2 imSz1 = imSz1 + imSzT1 imSz2 = imSz2 + imSzT2 ps = ps + psT totalSamples = totalSamples + len(imT1) #Permute all the sequences togther perm = randState.permutation(totalSamples) im1 = [im1[p] for p in perm] im2 = [im2[p] for p in perm] imSz1 = [imSz1[p] for p in perm] imSz2 = [imSz2[p] for p in perm] ps = [ps[p] for p in perm] #Save in the file gen = mpio.GenericWindowWriter(prms['paths']['windowFile'][setName], len(im1), 2, prms['labelSz']) for i in range(len(im1)): h, w, ch = imSz1[i] l1 = [im1[i], [ch, h, w], [0, 0, w, h]] h, w, ch = imSz2[i] l2 = [im2[i], [ch, h, w], [0, 0, w, h]] gen.write(ps[i], l1, l2) gen.close() np.random.set_state(oldState)
def make_window_file_by_folderid(prms, folderId, maxGroups=None): if len(prms.labelNames) == 1 and prms.labelNames[0] == 'nrml': numImPerExample = 1 else: numImPerExample = 2 #Assuming the size of images h, w, ch = prms.rawImSz, prms.rawImSz, 3 hCenter, wCenter = int(h / 2), int(w / 2) cr = int(prms.crpSz / 2) minH = max(0, hCenter - cr) maxH = min(h, hCenter + cr) minW = max(0, wCenter - cr) maxW = min(w, wCenter + cr) #Get the im-label data lb, prefix = sls.get_label_by_folderid(prms, folderId, maxGroups=maxGroups) #For the imNames imNames1 = [] print('Window file for %s' % folderId) imKeys = pickle.load( open(prms.paths.proc.im.folder.keyFile % folderId, 'r')) imKeys = imKeys['imKeys'] for pref in prefix: tmpNames = [] _, p1, _, p2 = pref tmpNames.append(osp.join(folderId, imKeys[p1])) if p2 is not None: tmpNames.append(osp.join(folderId, imKeys[p2])) imNames1.append(tmpNames) #Randomly permute the data N = len(imNames1) randState = np.random.RandomState(19) perm = randState.permutation(N) #The output file wFile = prms.paths.exp.window.folderFile % folderId wDir, _ = osp.split(wFile) sp._mkdir(wDir) gen = mpio.GenericWindowWriter(wFile, len(imNames1), numImPerExample, prms['labelSz']) for i in perm: line = [] for n in range(numImPerExample): line.append([imNames1[i][n], [ch, h, w], [minW, minH, maxW, maxH]]) gen.write(lb[i], *line) gen.close()
def make_window_file(): prms = get_prms() posDat = pickle.load(open(prms.paths.jpgPosKey, 'r'))['imNames'] negDat = pickle.load(open(prms.paths.jpgNegKey, 'r'))['imNames'] allDat = posDat + negDat labels = np.ones((len(allDat), )) labels[len(posDat):] = 0 perm = np.random.permutation(len(allDat)) allDat = [allDat[p] for p in perm] labels = [labels[p] for p in perm] N = len(allDat) wFid = mpio.GenericWindowWriter(prms.paths.wFile, N, 2, 1) for dat, lb in zip(allDat, labels): line = [] line.append([dat[0], [3, 64, 64], [0, 0, 64, 64]]) line.append([dat[1], [3, 64, 64], [0, 0, 64, 64]]) wFid.write([lb], *line) wFid.close()
def create_window_file(): setName = ['test', 'train'] for i,s in enumerate(setName): inName = 'pose-files/annotations_master_%s_pascal3d.txt' % s oName = 'pose-files/euler_%s_pascal3d.txt' % s 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 = 3 + (2 * i) randState = np.random.RandomState(randSeed) perm = randState.permutation(N) if s == 'train': numBad = 2 else: numBad = 0 oFid = mpio.GenericWindowWriter(oName, N-numBad, 1, 3) for p in perm: im, lb = imDat[p], lbls[p] fName, ch, h, w, x1, y1, x2, y2 = im[0].strip().split() x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) 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) if x2 > w or y2 > h: print ('Too big', x2, w, y2, h) rots = [] for theta in lb[0]: rots.append(su.rot_range(theta)/30.0) rots.append(1.0) oFid.write(rots, *im) oFid.close()
def make_window_file(prms): oldState = np.random.get_state() for sNum, setName in enumerate(['test', 'train']): im1, im2, label = get_names_and_label(prms, setName) N = len(im1) randSeed = 2 * sNum + 1 randState = np.random.RandomState(randSeed) #Permute all the sequences togther perm = randState.permutation(N) im1 = [im1[p] for p in perm] im2 = [im2[p] for p in perm] ps = [label[p] for p in perm] #Save in the file gen = mpio.GenericWindowWriter(prms['paths']['windowFile'][setName], len(im1), 2, prms['labelSz']) h, w, ch = 320, 480, 3 for i in range(len(im1)): l1 = [im1[i], [ch, h, w], [0, 0, w, h]] l2 = [im2[i], [ch, h, w], [0, 0, w, h]] gen.write(ps[i], l1, l2) gen.close() np.random.set_state(oldState)
def make_combined_window_file(prms, setName='train'): keys = sp.get_train_test_defs(prms.geoFence, setName=setName) wObjs, wNum = [], [] numIm = None for i, k in enumerate(keys): wFile = prms.paths.exp.window.folderFile % k wObj = mpio.GenericWindowReader(wFile) wNum.append(wObj.num_) wObjs.append(wObj) if i == 0: numIm = wObj.numIm_ else: assert numIm == wObj.numIm_, '%d, %d' % (numIm, wObj.num_) nExamples = sum(wNum) N = min(nExamples, int(prms.splits.num[setName])) mainWFile = mpio.GenericWindowWriter(prms['paths']['windowFile'][setName], N, numIm, prms['labelSz']) print('Total examples to chose from: %d' % sum(wNum)) wCount = copy.deepcopy(np.array(wNum)) wNum = np.array(wNum).astype(float) wNum = wNum / sum(wNum) pCum = np.cumsum(wNum) print(pCum) assert pCum[-1] == 1, 'Something is wrong' randState = np.random.RandomState(31) ignoreCount = 0 nrmlPrune = False if 'nrml' in prms.labelNames and len(prms.labelNames) == 1: if prms.nrmlMakeUni is not None: idx = prms.labelNames.index('nrml') lbInfo = prms.labels[idx] nrmlPrune = True if lbInfo.loss_ in ['l2', 'l1']: nrmlBins = np.linspace(-180, 180, 101) binCounts = np.zeros((2, 101)) elif lbInfo.loss_ == 'classify': nrmlBins = np.array(range(lbInfo.numBins_ + 1)) binCounts = np.zeros((2, lbInfo.numBins_)) mxBinCount = int(prms.nrmlMakeUni * np.sum(wCount)) print('mxBinCount :%d' % mxBinCount) #Get the normalization data if required if prms['lbNrmlz'] is not None: nrmlzDat = get_label_stats(prms) writeCount = 0 for i in range(N): sampleFlag = True idx = None while sampleFlag: rand = randState.rand() idx = su.find_first_false(rand >= pCum) if not wObjs[idx].is_eof(): sampleFlag = False else: ignoreCount += 1 if ignoreCount > 2000: print(ignoreCount, 'Resetting prob distribution') pCum = np.cumsum(wCount / float(sum(wCount))) print pCum ignoreCount = 0 wCount[idx] -= 1 imNames, lbls = wObjs[idx].read_next() if prms['lbNrmlz'] is not None: if prms['lbNrmlz'] == 'zscore': mu, sd = nrmlzDat else: raise Exception('lbNrmlz %s not recognized' % prms['lbNrmlz']) assert len(prms.labels ) == 1, 'This needs to be generalized for multi-labels' for lbIdx in range(prms.labels[0].lbSz_): lbls[0][lbIdx] = (lbls[0][lbIdx] - mu[lbIdx]) / sd[lbIdx] if nrmlPrune: nrmlIdx = randState.permutation(2)[0] binIdx = find_bin_index(nrmlBins, lbls[0][nrmlIdx]) if binCounts[nrmlIdx][binIdx] < mxBinCount: binCounts[nrmlIdx][binIdx] += 1 else: continue try: mainWFile.write(lbls[0], *imNames) except: print 'Error' pdb.set_trace() writeCount += 1 mainWFile.close() #Only if nrml labels are being considered #Get the count correct for nrmlPrune scenarios if nrmlPrune: imNames, lbls = [], [] mainWFile = mpio.GenericWindowReader(prms.paths.windowFile[setName]) readFlag = True readCount = 0 while readFlag: name, lb = mainWFile.read_next() imNames.append(name) lbls.append(lb) readCount += 1 if readCount == writeCount: readFlag = False mainWFile.close() #Write the corrected version mainWFile = mpio.GenericWindowWriter( prms['paths']['windowFile'][setName], writeCount, numIm, prms['labelSz']) for n in range(writeCount): mainWFile.write(lbls[n][0], *imNames[n]) mainWFile.close()
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