def selectNegativeWindowsFromPositiveImages(groundTruths,featuresDir,featuresExt,maxVectors,overlap,model=False): gtb = dict() for x in groundTruths: im,bx = x[0],map(float,x[1:]) try: gtb[im].append(bx) except: gtb[im] = [bx] task = NWFPIFilter(gtb,featuresDir,featuresExt,maxVectors/len(gtb.keys()),overlap,model) result = dp.processData(gtb.keys(),featuresDir,featuresExt,task) posIdx,posFeat,negIdx,negFeat = [],[],[],[] for r in result: posIdx += r[0] posFeat += r[1] negIdx += r[2] negFeat += r[3] Xp = emptyMatrix( (len(posIdx),posFeat[0].shape[1]) ) Xn = emptyMatrix( (len(negIdx),negFeat[0].shape[1]) ) k = 0 for i in range(len(posFeat)): Xp[k:k+posFeat[i].shape[0],:] = posFeat[i] k = k + posFeat[i].shape[0] k = 0 for i in range(len(negFeat)): Xn[k:k+negFeat[i].shape[0],:] = negFeat[i] k + k + negFeat[i].shape[0] print 'NegFromPos ready:',len(negIdx) return {'posIdx':posIdx, 'posFeat':Xp, 'negIdx':negIdx, 'negFeat':Xn}
def getHardNegatives(negativesDir,negativesList,featuresExt,numFeatures,maxVectors,currentModel): maxVectorsPerImage = maxVectors/len(negativesList) i = 0 task = HardNegativeMining(currentModel,maxVectorsPerImage) result = dp.processData(negativesList,negativesDir,featuresExt,task) hardng = emptyMatrix([2*maxVectors,numFeatures]) boxes = [] while len(result) > 0: data = result.pop(0) if data[0].shape[0]+i > hardng.shape[0]: print 'Not enough matrix space' hardng = np.concatenate( (hardng,emptyMatrix([maxVectors,numFeatures])) ) hardng[i:i+data[0].shape[0],:] = data[0] boxes += data[2] i = i + data[0].shape[0] return hardng[0:i,:],boxes[0:i]
def getActivations(self, imagePath, boxes, state): n = len(boxes) activations = cu.emptyMatrix([n, config.geti('outputActions')]) numBatches = (n + config.geti('deployBatchSize') - 1) / config.geti('deployBatchSize') boxes += [[0, 0, 0, 0] for x in range(numBatches * config.geti('deployBatchSize') - n)] stateFeatures = np.zeros((len(boxes), 20, 1, 1), dtype=np.float32) stateFeatures[0:n, :, :, :] = state dims = self.net.caffenet.InitializeImage(imagePath, config.geti('imageSize'), self.meanImage, config.geti('cropSize')) for k in range(numBatches): s, f = k * config.geti('deployBatchSize'), ( k + 1) * config.geti('deployBatchSize') e = config.geti('deployBatchSize') if f <= n else n - s # Forward this batch #self.net.caffenet.ForwardRegions(boxes[s:f], config.geti('contextPad')) self.net.caffenet.ForwardRegionsAndState( boxes[s:f], config.geti('contextPad'), [stateFeatures[s:f, :, :, :]]) outputs = self.net.caffenet.blobs f = n if f > n else f # Collect outputs activations[s:f, :] = outputs['prob'].data[0:e, :, :, :].reshape( [e, config.geti('outputActions')]) # Release image data self.net.caffenet.ReleaseImageData() return activations
def loadHardNegativesFromMatrix(featuresDir,imagesIdx,detMatrix,featuresExt,numFeatures,totalNegatives): i = 0 task = LoadHardNegatives(imagesIdx,detMatrix,numFeatures) result = dp.processData(imagesIdx.keys(),featuresDir,featuresExt,task) hardng = cu.emptyMatrix([totalNegatives,numFeatures]) while len(result) > 0: data = result.pop(0) hardng[i:i+data.shape[0],:] = data i = i + data.shape[0] return hardng[0:i,:]
def loadHardNegativesFromMatrix(featuresDir, imagesIdx, detMatrix, featuresExt, numFeatures, totalNegatives): i = 0 task = LoadHardNegatives(imagesIdx, detMatrix, numFeatures) result = dp.processData(imagesIdx.keys(), featuresDir, featuresExt, task) hardng = cu.emptyMatrix([totalNegatives, numFeatures]) while len(result) > 0: data = result.pop(0) hardng[i:i + data.shape[0], :] = data i = i + data.shape[0] return hardng[0:i, :]
def loadHardNegativesFromList(featuresDir,negativesInfo,featuresExt,numFeatures,totalNegatives,idx=False): i = 0 task = LoadHardNegatives(negativesInfo) result = dp.processData(negativesInfo.keys(),featuresDir,featuresExt,task) hardng = emptyMatrix([totalNegatives,numFeatures]) hardNames = [] boxes = [] while len(result) > 0: data,imgs,box = result.pop(0) hardng[i:i+data.shape[0],:] = data hardNames += imgs boxes += box i = i + data.shape[0] return (hardng[0:i,:],boxes)
def getRandomNegs(featuresDir,negativeList,featuresExt,numFeatures,maxVectors,maxNegativeImages): randomBoxes = maxVectors/maxNegativeImages cu.rnd.shuffle(negativeList) task = RandomNegativesFilter(numFeatures,randomBoxes) negatives = [negativeList.pop(0) for i in range(maxNegativeImages)] result = dp.processData(negatives,featuresDir,featuresExt,task) neg = emptyMatrix([maxVectors,numFeatures]) boxes = [] n = 0 while len(result) > 0: mat,box = result.pop() neg[n:n+mat.shape[0]] = mat n = n + mat.shape[0] boxes += box return (neg[0:n],boxes[0:n])
def getActivations(self, imagePath, boxes, state): n = len(boxes) activations = cu.emptyMatrix( [n, config.geti('outputActions')] ) numBatches = (n + config.geti('deployBatchSize') - 1) / config.geti('deployBatchSize') boxes += [ [0,0,0,0] for x in range(numBatches * config.geti('deployBatchSize') - n) ] stateFeatures = np.zeros( (len(boxes), 20, 1, 1), dtype=np.float32) stateFeatures[0:n,:,:,:] = state dims = self.net.caffenet.InitializeImage(imagePath, config.geti('imageSize'), self.meanImage, config.geti('cropSize')) for k in range(numBatches): s, f = k * config.geti('deployBatchSize'), (k + 1) * config.geti('deployBatchSize') e = config.geti('deployBatchSize') if f <= n else n - s # Forward this batch #self.net.caffenet.ForwardRegions(boxes[s:f], config.geti('contextPad')) self.net.caffenet.ForwardRegionsAndState(boxes[s:f], config.geti('contextPad'), [stateFeatures[s:f,:,:,:]]) outputs = self.net.caffenet.blobs f = n if f > n else f # Collect outputs activations[s:f,:] = outputs['prob'].data[0:e,:,:,:].reshape([e,config.geti('outputActions')]) # Release image data self.net.caffenet.ReleaseImageData() return activations