Exemplo n.º 1
0
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()
Exemplo n.º 2
0
def get_label_stats(prms, isForceCompute=False):
    if prms['lbNrmlz'] is None:
        return None
    fName = prms.paths.exp.window.nrmlz
    if osp.exists(fName) and not isForceCompute:
        dat = pickle.load(open(fName, 'r'))
        if prms['lbNrmlz'] == 'zscore':
            return dat['mu'], dat['sd']
        else:
            raise Exception('lbNrmlz %s not recognized' % lbNrmlz)
    print('RE-COMPUTING FEATURE NORMALIZATION FACTORS')
    #Get the folders in the training set
    folderIds = sp.get_train_test_defs(prms.geoFence, setName='train')
    print folderIds
    allLbs = []
    for fid in folderIds:
        wFile = prms.paths.exp.window.folderFile % fid
        wFid = mpio.GenericWindowReader(wFile)
        lbs = np.array(wFid.get_all_labels())
        allLbs.append(lbs)
    allLbs = np.concatenate(allLbs, axis=0)
    N = allLbs.shape[0]
    #Sample 10% of the labels - compute mean and var
    perm = np.random.permutation(N)
    perm = perm[0:int(0.1 * N)]
    allLbs = allLbs[perm, :]
    print(allLbs.shape)
    if prms['lbNrmlz'] == 'zscore':
        mu = np.mean(allLbs, 0)
        sd = np.std(allLbs, 0)
        dat = {'mu': mu, 'sd': sd}
    else:
        raise Exception('lbNrmlz %s not recognized' % lbNrmlz)
    pickle.dump(dat, open(fName, 'w'))
    return get_label_stats(prms, isForceCompute=False)
Exemplo n.º 3
0
def compute_label_stats(dPrms=None):
    if dPrms is None:
        dPrms = get_data_prms()
    if osp.exists(dPrms.paths.exp.labelStats):
        print('Label Stats already computed')
        return
    randState = np.random.RandomState(5)
    wFile = dPrms.paths.window.train
    wFid = mpio.GenericWindowReader(wFile)
    lbls = wFid.get_all_labels()
    N = lbls.shape[0]
    perm = randState.permutation(N)
    perm = perm[0:int(0.2 * N)]
    lbls = lbls[perm, :]
    print(lbls.shape)
    mu, sd = np.mean(lbls, 0), np.std(lbls, 0)
    aMu, aSd = np.mean(np.abs(lbls), 0), np.std(np.abs(lbls), 0)
    md, aMd = np.median(lbls, 0), np.median(np.abs(lbls), 0)
    print(mu, sd, aMu, aSd)
    print(md, aMd)
    stats = edict()
    stats['mu'], stats['sd'] = mu, sd
    stats['aMu'], stats['aSd'] = aMu, aSd
    stats['md'], stats['aMd'] = md, aMd
    wFid.close()
    pickle.dump(stats, open(dPrms.paths.exp.labelStats, 'w'))
Exemplo n.º 4
0
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()
Exemplo n.º 5
0
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
Exemplo n.º 6
0
def get_binned_normals_from_window_file(prms, setName='test'):
    wFileName = prms.paths.windowFile[setName]
    wFid = mpio.GenericWindowReader(wFileName)
    lbls = wFid.get_all_labels()
    N, nLb = lbls.shape
    nLb -= 1
    nBins = 100
    binned = []
    for n in range(nLb):
        binned.append(np.histogram(lbls[:, n], 100))
    return binned
Exemplo n.º 7
0
	def setup(self, bottom, top):
		self.param_ = PythonWindowDataRotsLayer.parse_args(self.param_str) 
		self.wfid_   = mpio.GenericWindowReader(self.param_.source)
		self.numIm_  = self.wfid_.numIm_
		self.lblSz_  = self.wfid_.lblSz_
		self.isV2    = False
		if self.param_.is_gray:
			self.ch_ = 1
		else:
			self.ch_ = 3
		assert not self.param_.nrmlz_file == 'None'
		self.rotPrms_ = {}
		self.rotPrms_['randomRoll']   = self.param_.randomRoll
		self.rotPrms_['randomRollMx'] = self.param_.randomRollMx
		if self.param_.nrmlz_file:
			nrmlzDat = pickle.load(open(self.param_.nrmlz_file, 'r'))
			self.rotPrms_['nrmlzMu'] = nrmlzDat['nrmlzMu']
			self.rotPrms_['nrmlzSd'] = nrmlzDat['nrmlzSd'] 
			

		top[0].reshape(self.param_.batch_size, self.numIm_ * self.ch_,
										self.param_.crop_size, self.param_.crop_size)
		top[1].reshape(self.param_.batch_size, self.lblSz_, 1, 1)
		#Load the mean
		self.load_mean()
		#If needed to resume	
		if self.param_.resume_iter > 0:
			N = self.param_.resume_iter * self.param_.batch_size
			N = np.mod(N, self.wfid_.num_)
			print ('SKIPPING AHEAD BY %d out of %d examples, BECAUSE resume_iter is NOT 0'\
							 % (N, self.wfid_.num_))
			for n in range(N):
				_, _ = self.wfid_.read_next()	
		#Create the pool
		self.pool_, self.jobs_ = [], []
		for n in range(self.numIm_):
			self.pool_.append(Pool(processes=self.param_.ncpu))
			self.jobs_.append([])
		
		self.imData_ = np.zeros((self.param_.batch_size, self.numIm_ * self.ch_,
						self.param_.crop_size, self.param_.crop_size), np.float32)
		if 'cv2' in globals():
			print('OPEN CV FOUND')
			if self.isV2:
				self.readfn_ = image_reader_list
			else:
				self.readfn_ = image_reader
		else:
			print('OPEN CV NOT FOUND, USING SCM')
			self.readfn_ = image_reader_scm
		#Launch the prefetching	
		self.launch_jobs()
		self.t_ = time.time()	
Exemplo n.º 8
0
def verify_pose_results(modelIter):
    #prms, cPrms = mepo.smallnetv5_fc5_pose_euler_crp192_rawImSz256(numFc5=512)
    prms, cPrms = mepo.smallnetv5_fc5_pose_euler_crp192_rawImSz256_lossl1()
    exp = se.setup_experiment(prms, cPrms)
    #Window File
    wFileName = 'test-files/test_pose_euler_mx90_geo-dc-v2_spDist100_imSz256.txt'
    wFile = mpio.GenericWindowReader(wFileName)
    #Setup the Net
    mainDataDr = cfg.STREETVIEW_DATA_MAIN
    meanFile = osp.join(
        mainDataDr,
        'pulkitag/caffe_models/ilsvrc2012_mean_for_siamese.binaryproto')
    rootFolder = osp.join(
        mainDataDr, 'pulkitag/data_sets/streetview/proc/resize-im/im256/')
    batchSz = 100
    testNet = mpu.CaffeTest.from_caffe_exp(exp)
    testNet.setup_network(opNames=['fc5'],
                          imH=101,
                          imW=101,
                          cropH=101,
                          cropW=101,
                          channels=6,
                          chSwap=(2, 1, 0, 5, 4, 3),
                          modelIterations=modelIter,
                          delAbove='pose_fc',
                          batchSz=batchSz,
                          isAccuracyTest=False,
                          dataLayerNames=['window_data'],
                          newDataLayerNames=['pair_data'],
                          meanFile=meanFile)
    predFeat, gtFeat = [], []
    #Send images and get features
    for st in range(0, 200, batchSz):
        en = min(200, st + batchSz)
        ims = []
        for i in range(st, en):
            im, lbls = wFile.read_next_processed(rootFolder)
            im = np.concatenate(im, axis=2)
            ims.append(im.reshape((1, ) + im.shape))
            gtFeat.append(lbls[0:2].reshape((1, ) + lbls[0:2].shape))
        ims = np.concatenate(ims)
        print ims.shape
        feats = testNet.net_.forward_all(blobs=['pose_fc'],
                                         **{'pair_data': ims})
        predFeat.append(copy.deepcopy(feats['pose_fc']))
    gtFeat = np.concatenate(gtFeat)
    predFeat = np.concatenate(predFeat)
    err = np.median((np.abs(gtFeat - predFeat) * 30), axis=0)
    print(err)
    return gtFeat, predFeat
Exemplo n.º 9
0
def benchmark_reads(prms, setName='train'):
    wFile = prms.paths.windowFile[setName]
    wFid = mpio.GenericWindowReader(wFile)
    rootDir = se.get_windowfile_rootdir(prms)
    allNames = []
    for i in range(2000):
        imName, lbls = wFid.read_next()
        allNames.append(osp.join(rootDir, imName[0].strip().split()[0]))

    t1 = time.time()
    for i in range(2000):
        im = cv2.imread(allNames[i])
    t2 = time.time()
    print('TIme Elapsed: %f' % (t2 - t1))
    return allNames
Exemplo n.º 10
0
def get_cls_set_files(setName='train', cls='car'):
	imSz, padSz = 256, 36
	inFile = 'pose-files/euler_%s_pascal3d_imSz%d_pdSz%d.txt' % (setName, imSz, padSz)
	fid    = mpio.GenericWindowReader(inFile)
	fNames, lbs = [], []
	while True:
		if fid.is_eof():
			break
		imDat, lbDat = fid.read_next()
		fName, ch, h, w, x1, y1, x2, y2 = imDat[0].strip().split()
		if cls in fName:
			fNames.append(fName)
			lbs.append(lbDat[0])
	fid.close()
	return fNames, lbs
 def launch_jobs(self):
     argList = []
     for n in range(self.numIm_):
         argList.append([])
     self.labels_ = np.zeros((self.param_.batch_size, self.lblSz_, 1, 1),
                             np.float32)
     #Form the list of images and labels
     for b in range(self.param_.batch_size):
         if self.wfid_.is_eof():
             self.wfid_.close()
             self.wfid_ = mpio.GenericWindowReader(self.param_.source)
             glog.info('RESTARTING READ WINDOW FILE')
         imNames, lbls = self.wfid_.read_next()
         self.labels_[b, :, :, :] = lbls.reshape(self.lblSz_, 1,
                                                 1).astype(np.float32)
         #Read images
         fNames, coords = [], []
         for n in range(self.numIm_):
             fName, ch, h, w, x1, y1, x2, y2 = imNames[n].strip().split()
             fNames.append(osp.join(self.param_.root_folder, fName))
             x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
             coords.append((x1, y1, x2, y2))
         #Computing jittering if required
         dx, dy = self.get_jitter(coords)
         for n in range(self.numIm_):
             fName = fNames[n]
             x1, y1, x2, y2 = coords[n]
             #Jitter the box
             x1 = max(0, x1 + dx)
             y1 = max(0, y1 + dy)
             x2 = min(w, x2 + dx)
             y2 = min(h, y2 + dy)
             #glog.info('%d, %d, %d, %d' % (x1, y1, x2, y2))
             argList[n].append([
                 fName, (x1, y1, x2, y2), self.param_.crop_size, b,
                 self.param_.is_gray, self.param_.is_mirror
             ])
     #Launch the jobs
     for n in range(self.numIm_):
         try:
             #print (argList[n])
             self.jobs_[n] = self.pool_[n].map_async(
                 self.readfn_, argList[n])
         except KeyboardInterrupt:
             print 'Keyboard Interrupt received - terminating in launch jobs'
             self.pool_[n].terminate()
Exemplo n.º 12
0
def vis_liberty_ptch():
    libPrms = rlp.get_prms()
    wFile = libPrms.paths.wFile
    wDat = mpio.GenericWindowReader(wFile)
    rootDir = libPrms.paths.jpgDir
    plt.ion()
    fig = plt.figure()
    while True:
        imNames, lbs = wDat.read_next()
        imNames = [osp.join(rootDir, n.split()[0]) for n in imNames]
        figTitle = '%d' % lbs[0][0]
        im1 = plt.imread(imNames[0])
        im2 = plt.imread(imNames[1])
        vu.plot_pairs(im1, im2, fig=fig, figTitle=figTitle)
        inp = raw_input('Press a key to continue')
        if inp == 'q':
            return
    def launch_jobs_v2(self):
        argList = []
        for n in range(self.numIm_):
            argList.append([])
        self.labels_ = np.zeros((self.param_.batch_size, self.lblSz_, 1, 1),
                                np.float32)
        #Form the list of images and labels
        for b in range(self.param_.batch_size):
            if self.wfid_.is_eof():
                self.wfid_.close()
                self.wfid_ = mpio.GenericWindowReader(self.param_.source)
                print('RESTARTING READ WINDOW FILE')
            imNames, lbls = self.wfid_.read_next()
            self.labels_[b, :, :, :] = lbls.reshape(self.lblSz_, 1,
                                                    1).astype(np.float32)
            #Read images
            for n in range(self.numIm_):
                fName, ch, h, w, x1, y1, x2, y2 = imNames[n].strip().split()
                fName = osp.join(self.param_.root_folder, fName)
                x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
                argList[n].append([
                    fName, (x1, y1, x2, y2), self.param_.crop_size, b,
                    self.param_.is_gray
                ])

        #Launch the jobs
        for n in range(self.numIm_):
            #Distribute the jobs
            jobPerm = [
                int(np.ceil(j)) for j in np.linspace(0, self.param_.batch_size,
                                                     self.num_threads + 1)
            ]
            jobArgs = []
            for j in range(self.num_threads):
                st = jobPerm[j]
                en = min(self.param_.batch_size, jobPerm[j + 1])
                jobArgs.append(argList[n][st:en])
            assert (en >= self.param_.batch_size)
            try:
                print(jobArgs)
                self.jobs_[n] = self.pool_[n].map_async(self.readfn_, jobArgs)
            except KeyboardInterrupt:
                print 'Keyboard Interrupt received - terminating'
                self.pool_[n].terminate()
 def setup(self, bottom, top):
     self.param_ = PythonWindowDataLayer.parse_args(self.param_str)
     self.wfid_ = mpio.GenericWindowReader(self.param_.source)
     self.numIm_ = self.wfid_.numIm_
     self.lblSz_ = self.wfid_.lblSz_
     if self.param_.is_gray:
         self.ch_ = 1
     else:
         self.ch_ = 3
     top[0].reshape(self.param_.batch_size, self.numIm_ * self.ch_,
                    self.param_.crop_size, self.param_.crop_size)
     top[1].reshape(self.param_.batch_size, self.lblSz_, 1, 1)
     self.load_mean()
     #Skip the number of examples so that the same examples
     #are not read back
     if self.param_.resume_iter > 0:
         N = self.param_.resume_iter * self.param_.batch_size
         N = np.mod(N, self.wl_.num_)
         for n in range(N):
             _, _ = self.read_next()
Exemplo n.º 15
0
 def launch_jobs(self):
     self.argList = []
     self.labelList = []
     #Form the list of images and labels
     for b in range(self.param_.batch_size):
         if self.wfid_.is_eof():
             self.wfid_.close()
             self.wfid_ = mpio.GenericWindowReader(self.param_.window_file)
             glog.info('RESTARTING READ WINDOW FILE')
         imNames, lbls = self.wfid_.read_next()
         lb = self.format_label(lbls[0])
         self.labelList.append(lb)
         #Read images
         fName, ch, h, w, x1, y1, x2, y2 = imNames[0].strip().split()
         x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
         h, w = int(h), int(w)
         fName = osp.join(self.param_.im_root_folder, fName)
         xCenter, yCenter = (x1 + x2) / 2.0, (y1 + y2) / 2.0
         xCr1 = int(max(0, xCenter - self.param_.crop_size / 2.0))
         yCr1 = int(max(0, yCenter - self.param_.crop_size / 2.0))
         xCr2 = xCr1 + self.param_.crop_size
         yCr2 = yCr1 + self.param_.crop_size
         maxYJit = max(0, (h - self.param_.crop_size) / 2.0)
         maxXJit = max(0, (w - self.param_.crop_size) / 2.0)
         xJit, yJit = get_jitter(maxXJit, maxYJit)
         x1 = max(0, int(xCr1 + xJit))
         y1 = max(0, int(yCr1 + yJit))
         x2 = min(w, int(xCr2 + xJit))
         y2 = min(h, int(yCr2 + yJit))
         #print (x1, x2, y1, y2)
         self.argList.append([
             fName, (x1, y1, x2, y2), self.param_.im_size,
             self.param_.crop_size, self.param_.is_gray
         ])
     #Launch the jobs
     if self.param_.ncpu > 0:
         try:
             self.jobs_ = self.pool_.map_async(self.readfn_, argList)
         except KeyboardInterrupt:
             print 'Keyboard Interrupt received - terminating in launch jobs'
             self.pool_.terminate()
Exemplo n.º 16
0
def get_normals():
    prms, cPrms = mev2.smallnetv5_fc5_nrml_crp192_rawImSz256_nojitter_l1loss()
    wFile = prms.paths['windowFile']['test']
    wFid = mpio.GenericWindowReader(wFile)
    print(wFid.num_)
    allLb = []
    imDat = []
    for i in range(4000):
        imd, lbls = wFid.read_next()
        allLb.append(lbls[0][0:2].reshape((1, 2)))
        imDat.append(imd)
    allLb = np.concatenate(allLb, axis=0)
    mag = np.sum(allLb * allLb, 1)
    idx = np.argsort(mag)

    oFile = open('nrml-frontal.txt', 'w')
    for i in idx[0:100]:
        imd = imDat[i][0].strip()
        nrmlStr = '%f \t %f' % (allLb[i][0], allLb[i][1])
        oFile.write('%s \t %s \n' % (imd, nrmlStr))
    oFile.close()
Exemplo n.º 17
0
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()
Exemplo n.º 18
0
def hack_window_file():
    targetDir = '/data0/pulkitag/hack'
    inDir = '/data0/pulkitag/data_sets/streetview/proc/resize-im/im256/'
    prms = sp.get_prms_vegas_ptch()
    wFile = mpio.GenericWindowReader(prms.paths['windowFile']['test'])
    readFlag = True
    count = 0
    while readFlag:
        if np.mod(count, 1000) == 0:
            print(count)
        count += 1
        ims, lb = wFile.read_next()
        for im in ims:
            fName = im.strip().split()[0]
            inName = osp.join(inDir, fName)
            inDat = scm.imread(inName)
            outName = osp.join(targetDir, fName)
            dirName, _ = osp.split(outName)
            sp._mkdir(dirName)
            scm.imsave(outName, inDat)
        if wFile.is_eof():
            readFlag = False
Exemplo n.º 19
0
 def common_setup(self):
     self.param_ = PascalWindowLayer.parse_args(self.param_str)
     #Read the window file
     self.wfid_ = mpio.GenericWindowReader(self.param_.window_file)
     self.numIm_ = self.wfid_.numIm_
     #Check for grayness
     if self.param_.is_gray:
         self.ch_ = 1
     else:
         self.ch_ = 3
     assert self.numIm_ == 1, 'Only 1 image'
     #If needed to resume
     if self.param_.resume_iter > 0:
         N = self.param_.resume_iter * self.param_.batch_size
         N = np.mod(N, self.wfid_.num_)
         print ('SKIPPING AHEAD BY %d out of %d examples, BECAUSE resume_iter is NOT 0'\
              % (N, self.wfid_.num_))
         for n in range(N):
             _, _ = self.wfid_.read_next()
     #Function for reading the images
     if 'cv2' in globals():
         print('OPEN CV FOUND')
         self.readfn_ = image_reader
     else:
         print('OPEN CV NOT FOUND, USING SCM')
         self.readfn_ = image_reader_scm
     #The batch list
     self.argList = []
     self.labelList = []
     #Load the mean
     self.load_mean()
     #Initialize image data
     self.imData_ = np.zeros(
         (self.param_.batch_size, self.numIm_ * self.ch_,
          self.param_.im_size, self.param_.im_size), np.float32)
     #open the label info file
     self.lbInfo_ = pickle.load(open(self.param_.lb_info_file, 'r'))
    def forward(self, bottom, top):
        t1 = time.time()
        tIm, tProc = 0, 0
        for b in range(self.param_.batch_size):
            if self.wfid_.is_eof():
                self.wfid_.close()
                self.wfid_ = mpio.GenericWindowReader(self.param_.source)
                print('RESTARTING READ WINDOW FILE')
            imNames, lbls = self.wfid_.read_next()
            #Read images
            for n in range(self.numIm_):
                #Load images
                imName, ch, h, w, x1, y1, x2, y2 = imNames[n].strip().split()
                imName = osp.join(self.param_.root_folder, imName)
                x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
                tImSt = time.time()
                im, _ = image_reader(imName, (x1, y1, x2, y2),
                                     self.param_.crop_size, 0)
                tImEn = time.time()
                tIm += (tImEn - tImSt)

                #Process the image
                if self.mu_ is not None:
                    im = im - self.mu_

                #Feed the image
                cSt = n * self.ch_
                cEn = cSt + self.ch_
                top[0].data[b, cSt:cEn, :, :] = im.astype(np.float32)
                tEn = time.time()
                tProc += (tEn - tImEn)
            #Read the labels
            top[1].data[b, :, :, :] = lbls.reshape(self.lblSz_, 1,
                                                   1).astype(np.float32)
        t2 = time.time()
        print('Forward: %fs, Reading: %fs, Processing: %fs' %
              (t2 - t1, tIm, tProc))
 def launch_jobs(self):
     if self.isV2:
         self.launch_jobs_v2()
         return
     argList = []
     for n in range(self.numIm_):
         argList.append([])
     self.labels_ = np.zeros((self.param_.batch_size, self.lblSz_, 1, 1),
                             np.float32)
     #Form the list of images and labels
     for b in range(self.param_.batch_size):
         if self.wfid_.is_eof():
             self.wfid_.close()
             self.wfid_ = mpio.GenericWindowReader(self.param_.source)
             glog.info('RESTARTING READ WINDOW FILE')
         imNames, lbls = self.wfid_.read_next()
         self.labels_[b, :, :, :] = lbls.reshape(self.lblSz_, 1,
                                                 1).astype(np.float32)
         #Read images
         for n in range(self.numIm_):
             fName, ch, h, w, x1, y1, x2, y2 = imNames[n].strip().split()
             fName = osp.join(self.param_.root_folder, fName)
             x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
             argList[n].append([
                 fName, (x1, y1, x2, y2), self.param_.crop_size, b,
                 self.param_.is_gray
             ])
     #Launch the jobs
     for n in range(self.numIm_):
         try:
             print(argList[n])
             self.jobs_[n] = self.pool_[n].map_async(
                 self.readfn_, argList[n])
         except KeyboardInterrupt:
             print 'Keyboard Interrupt received - terminating in launch jobs'
             self.pool_[n].terminate()
Exemplo n.º 22
0
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'))	
Exemplo n.º 23
0
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()
Exemplo n.º 24
0
def get_nrml_results(modelIter=20000):
    #prms, cPrms = mept.smallnetv5_fc5_ptch_crp192_rawImSz256()
    prms, cPrms = mept.smallnetv2_pool4_ptch_crp192_rawImSz256(
        isPythonLayer=True)
    exp = se.setup_experiment(prms, cPrms)
    #Setup the Net
    mainDataDr = cfg.STREETVIEW_DATA_MAIN
    meanFile = osp.join(
        mainDataDr,
        'pulkitag/caffe_models/ilsvrc2012_mean_for_siamese.binaryproto')
    batchSz = 500
    testNet = mpu.CaffeTest.from_caffe_exp(exp)
    testNet.setup_network(opNames=['ptch_fc'],
                          imH=101,
                          imW=101,
                          cropH=101,
                          cropW=101,
                          modelIterations=modelIter,
                          delAbove='ptch_fc',
                          batchSz=batchSz,
                          channels=6,
                          chSwap=(2, 1, 0, 5, 4, 3),
                          isAccuracyTest=False,
                          dataLayerNames=['window_data'],
                          newDataLayerNames=['pair_data'],
                          meanFile=meanFile)

    #Just read the first 10K images
    wFile = prms.paths['windowFile']['train']
    wFid = mpio.GenericWindowReader(wFile)
    rootFolder = '/data0/pulkitag/data_sets/streetview/proc/resize-im/im256'
    ims, lbs = [], []
    #N, numTest = 10000, 1000
    N, numTest = 100, 10
    for i in range(N):
        im, lb = wFid.read_next_processed(rootFolder)
        ims.append(im[0])
        lbs.append(lb)

    #Read the test data
    wFile = prms.paths['windowFile']['test']
    wFid = mpio.GenericWindowReader(wFile)
    rootFolder = '/data0/pulkitag/data_sets/streetview/proc/resize-im/im256'
    gtIms, gtLbs = [], []
    for i in range(numTest):
        im, lb = wFid.read_next_processed(rootFolder)
        gtIms.append(im[0])
        gtLbs.append(lb)

    #return ims, gtIms
    predFeat = []
    for te in range(numTest):
        imTe = gtIms[te].reshape((1, ) + gtIms[te].shape)
        for tr in range(0, N, batchSz):
            batchIm = []
            for i in range(tr, min(tr + batchSz, N)):
                im = ims[i].reshape((1, ) + ims[i].shape)
                batchIm.append(np.concatenate([imTe, im], axis=3))
            batchIm = np.concatenate(batchIm, axis=0)
            feats = testNet.net_.forward_all(blobs=['ptch_fc'],
                                             **{'pair_data': batchIm})
            predFeat.append(copy.deepcopy(feats['ptch_fc']))
    return predFeat
Exemplo n.º 25
0
def vis_window_file(prms, setName='test', isSave=False,
										labelType='pose_ptch', isEuler=True,
										wFileName=None):
	if prms == 'pascal3d':
		rootDir = '/data0/pulkitag/data_sets/pascal_3d-my-copy/PASCAL3D+_release1.1/Images/'
		wFile   = 'pose-files/euler_test_pascal3d.txt'
		labelType = 'pose'
		pascalFlag = True
	else:
		pascalFlag = False
		rootDir = se.get_windowfile_rootdir(prms)
		if wFileName is None:
			wFile = prms.paths.windowFile[setName]
		else:
			wFile = wFileName 
	wDat    = mpio.GenericWindowReader(wFile)
	runFlag = True
	if labelType == 'pose_ptch':
		if isEuler:
			lbStr   = 'yaw: %.2f, pitch: %.2f, isRot: %d'\
								+ '\n patchMatch: %.2f'
		else:
			lbStr   = 'roll: %.2f, yaw: %.2f, pitch: %.2f, isRot: %d'\
								+ '\n patchMatch: %.2f'
	elif labelType == 'pose':
		if pascalFlag:
			lbStr   = 'yaw: %.2f, pitch: %.2f'
		else:
			lbStr   = 'roll: %.2f, yaw: %.2f, pitch: %.2f, isRot: %d'
	elif labelType == 'ptch':
		lbStr   = 'IsMatch: %d'
	elif labelType == 'nrml':
		lbStr = 'nx: %.4f, ny: %.4f'
	else:
		raise Exception('%s not recognized' % labelType)	
	
	plt.ion()
	fig = plt.figure()
	count = 0
	maxCount = 100				
	while runFlag:
		imNames, lbs = wDat.read_next()
		coords   = [n.strip().split()[4:8] for n in imNames]
		print coords
		coords   = [int(c) for c in coords[0]]
		imNames  = [osp.join(rootDir, n.split()[0]) for n in imNames]
		#pdb.set_trace()
		if 'pose' in labelType:
			if isEuler:
				yaw, pitch = lbs[0][0:2]
				yaw        = (yaw * 180./6.)
				pitch      = (pitch * 180./6.)
				if not pascalFlag:
					isRot          = lbs[0][2]
			else:
				q1, q2, q3, q4 = lbs[0][0:4]
				roll, yaw, pitch = ru.quat2euler([q1, q2, q3, q4])
				roll  = 180 * (roll/np.pi)
				yaw   = 180 * (yaw/np.pi)
				pitch = 180 * (pitch/np.pi)
				isRot          = lbs[0][4]

		if labelType == 'pose_ptch':
			if isEuler:
				pMatch = lbs[0][3]
				figTitle = lbStr % (yaw, pitch, isRot, pMatch)
			else:
				pMatch = lbs[0][5]
				figTitle = lbStr % (roll, yaw, pitch, isRot, pMatch)
		elif labelType == 'pose':
			if pascalFlag:
				figTitle = lbStr % (yaw, pitch)
			else:
				figTitle = lbStr % (roll, yaw, pitch, isRot)
		elif labelType == 'ptch':
			figTitle = lbStr % lbs[0][0]
		elif labelType == 'nrml':
			figTitle = lbStr % (lbs[0][0], lbs[0][1])

		print (figTitle, count)
		im1      = plt.imread(imNames[0])
		if len(imNames) == 2:
			im2      = plt.imread(imNames[1])
			vu.plot_pairs(im1, im2, fig=fig, figTitle=figTitle)
		else:
			x1, y1, x2, y2 = coords
			plt.imshow(im1[y1:y2, x1:x2,:])
			plt.title(figTitle)	
		if isSave:
			outName = osp.join('debug-data', '%05d.jpg' % count)
			plt.savefig(outName)
		else:	
			inp = raw_input('Press a key to continue')
			if inp=='q':
				return
		count = count + 1
		if count >= maxCount:
			runFlag = False
Exemplo n.º 26
0
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