def mergeDatasetAndRecords(self, train, val): numTrain = len(self.dataset) * (1 - config.getf('percentOfValidation')) numVal = len(self.dataset) * config.getf('percentOfValidation') random.shuffle(self.dataset) for i in range(len(self.dataset)): imgPath = config.get('imageDir') + self.dataset[i]['img'] + '.jpg' # record format: Action, reward, discountedMaxQ, x1, y1, x2, y2, record = [self.dataset[i]['A'], self.dataset[i]['R'], 0.0 ] + self.dataset[i]['box'] record += [self.dataset[i]['Sp']] + self.dataset[i]['Xp'] + [ self.dataset[i]['Sc'] ] + self.dataset[i]['Xc'] record += [self.dataset[i]['Ap']] if i < numTrain: try: train[imgPath].append(record) except: train[imgPath] = [record] else: try: val[imgPath].append(record) except: val[imgPath] = [record] return train, val
def __init__(self, workingDir): self.directory = workingDir self.writeSolverFile() self.solver = caffe.SGDSolver(self.directory + 'solver.prototxt') self.iter = 0 self.itersPerEpisode = config.geti('trainingIterationsPerBatch') self.lr = config.getf('learningRate') self.stepSize = config.geti('stepSize') self.gamma = config.getf('gamma') print 'CAFFE SOLVER INITALIZED'
def train(self): interactions = config.geti('trainInteractions') minEpsilon = config.getf('minTrainingEpsilon') epochSize = len(self.environment.db.images) / 2 epsilon = 1.0 self.controller.setEpsilonGreedy(epsilon) print 'Epoch 0: Exploration' self.runEpoch(interactions, len(self.environment.db.images)) self.learner = QLearning() self.agent.learner = self.learner epoch = 1 egEpochs = config.geti('epsilonGreedyEpochs') while epoch <= egEpochs: epsilon = epsilon - (1.0 - minEpsilon) / float(egEpochs) if epsilon < minEpsilon: epsilon = minEpsilon self.controller.setEpsilonGreedy(epsilon) print 'Epoch', epoch, '(epsilon-greedy:{:5.3f})'.format(epsilon) self.runEpoch(interactions, epochSize) epoch += 1 epoch = 1 maxEpochs = config.geti('exploitLearningEpochs') while epoch <= maxEpochs: print 'Epoch', epoch + egEpochs, '(exploitation mode: epsilon={:5.3f})'.format( epsilon) self.runEpoch(interactions, epochSize) epoch += 1
def train(self): interactions = config.geti('trainInteractions') minEpsilon = config.getf('minTrainingEpsilon') epochSize = len(self.environment.db.images)/2 epsilon = 1.0 self.controller.setEpsilonGreedy(epsilon) print 'Epoch 0: Exploration' self.runEpoch(interactions, len(self.environment.db.images)) self.learner = QLearning() self.agent.learner = self.learner epoch = 1 egEpochs = config.geti('epsilonGreedyEpochs') while epoch <= egEpochs: epsilon = epsilon - (1.0-minEpsilon)/float(egEpochs) if epsilon < minEpsilon: epsilon = minEpsilon self.controller.setEpsilonGreedy(epsilon) print 'Epoch',epoch ,'(epsilon-greedy:{:5.3f})'.format(epsilon) self.runEpoch(interactions, epochSize) epoch += 1 epoch = 1 maxEpochs = config.geti('exploitLearningEpochs') while epoch <= maxEpochs: print 'Epoch',epoch+egEpochs,'(exploitation mode: epsilon={:5.3f})'.format(epsilon) self.runEpoch(interactions, epochSize) epoch += 1
def __init__(self, imageList, mode, groundTruthFile=None): self.mode = mode self.cnn = cn.ConvNet() self.testRecord = None self.idx = -1 self.imageList = [x.strip() for x in open(imageList)] self.groundTruth = cu.loadBoxIndexFile(groundTruthFile) #self.imageList = self.rankImages() #self.imageList = self.imageList[0:10] allImgs = set([x.strip() for x in open(config.get('allImagesList'))]) self.negativeSamples = list(allImgs.difference(set(self.groundTruth.keys()))) self.negativeEpisode = False if self.mode == 'train': self.negativeProbability = config.getf('negativeEpisodeProb') random.shuffle(self.imageList) self.loadNextEpisode()
def __init__(self, imageList, mode, groundTruthFile=None): self.mode = mode self.cnn = cn.ConvNet() self.testRecord = None self.idx = -1 self.imageList = [x.strip() for x in open(imageList)] self.groundTruth = cu.loadBoxIndexFile(groundTruthFile) #self.imageList = self.rankImages() #self.imageList = self.imageList[0:10] allImgs = set([x.strip() for x in open(config.get('allImagesList'))]) self.negativeSamples = list( allImgs.difference(set(self.groundTruth.keys()))) self.negativeEpisode = False if self.mode == 'train': self.negativeProbability = config.getf('negativeEpisodeProb') random.shuffle(self.imageList) self.loadNextEpisode()
def train(self): networkFile = config.get('networkDir') + config.get( 'snapshotPrefix') + '_iter_' + config.get( 'trainingIterationsPerBatch') + '.caffemodel' interactions = config.geti('trainInteractions') minEpsilon = config.getf('minTrainingEpsilon') epochSize = len(self.environment.imageList) / 1 epsilon = 1.0 self.controller.setEpsilonGreedy(epsilon, self.environment.sampleAction) epoch = 1 exEpochs = config.geti('explorationEpochs') while epoch <= exEpochs: s = cu.tic() print 'Epoch', epoch, ': Exploration (epsilon=1.0)' self.runEpoch(interactions, len(self.environment.imageList)) self.task.flushStats() self.doValidation(epoch) s = cu.toc('Epoch done in ', s) epoch += 1 self.learner = QLearning() self.agent.learner = self.learner egEpochs = config.geti('epsilonGreedyEpochs') while epoch <= egEpochs + exEpochs: s = cu.tic() epsilon = epsilon - (1.0 - minEpsilon) / float(egEpochs) if epsilon < minEpsilon: epsilon = minEpsilon self.controller.setEpsilonGreedy(epsilon, self.environment.sampleAction) print 'Epoch', epoch, '(epsilon-greedy:{:5.3f})'.format(epsilon) self.runEpoch(interactions, epochSize) self.task.flushStats() self.doValidation(epoch) s = cu.toc('Epoch done in ', s) epoch += 1 maxEpochs = config.geti('exploitLearningEpochs') + exEpochs + egEpochs while epoch <= maxEpochs: s = cu.tic() print 'Epoch', epoch, '(exploitation mode: epsilon={:5.3f})'.format( epsilon) self.runEpoch(interactions, epochSize) self.task.flushStats() self.doValidation(epoch) s = cu.toc('Epoch done in ', s) shutil.copy(networkFile, networkFile + '.' + str(epoch)) epoch += 1
def balanceTrainingExamples(self, candidates): pos, neg = {},{} psc, ngc = 0,0 for k in candidates.keys(): for box in candidates[k]: if box[5] > config.getf('minPositiveOverlap'): # and box[6] >= 1.0: try: pos[k].append(box) except: pos[k] = [box] psc += 1 else: try: neg[k].append(box) except: neg[k] = [box] ngc += 1 self.pos = ImageBoxIndex(pos,True) self.neg = ImageBoxIndex(neg,True) self.probNeg = max(float(psc)/float(ngc),0.05) print 'Positives:',psc,'Negatives:',ngc
def train(self): networkFile = config.get('networkDir') + config.get('snapshotPrefix') + '_iter_' + config.get('trainingIterationsPerBatch') + '.caffemodel' interactions = config.geti('trainInteractions') minEpsilon = config.getf('minTrainingEpsilon') epochSize = len(self.environment.imageList)/1 epsilon = 1.0 self.controller.setEpsilonGreedy(epsilon, self.environment.sampleAction) epoch = 1 exEpochs = config.geti('explorationEpochs') while epoch <= exEpochs: s = cu.tic() print 'Epoch',epoch,': Exploration (epsilon=1.0)' self.runEpoch(interactions, len(self.environment.imageList)) self.task.flushStats() self.doValidation(epoch) s = cu.toc('Epoch done in ',s) epoch += 1 self.learner = QLearning() self.agent.learner = self.learner egEpochs = config.geti('epsilonGreedyEpochs') while epoch <= egEpochs + exEpochs: s = cu.tic() epsilon = epsilon - (1.0-minEpsilon)/float(egEpochs) if epsilon < minEpsilon: epsilon = minEpsilon self.controller.setEpsilonGreedy(epsilon, self.environment.sampleAction) print 'Epoch',epoch ,'(epsilon-greedy:{:5.3f})'.format(epsilon) self.runEpoch(interactions, epochSize) self.task.flushStats() self.doValidation(epoch) s = cu.toc('Epoch done in ',s) epoch += 1 maxEpochs = config.geti('exploitLearningEpochs') + exEpochs + egEpochs while epoch <= maxEpochs: s = cu.tic() print 'Epoch',epoch,'(exploitation mode: epsilon={:5.3f})'.format(epsilon) self.runEpoch(interactions, epochSize) self.task.flushStats() self.doValidation(epoch) s = cu.toc('Epoch done in ',s) shutil.copy(networkFile, networkFile + '.' + str(epoch)) epoch += 1
def balanceTrainingExamples(self, candidates): pos, neg = {}, {} psc, ngc = 0, 0 for k in candidates.keys(): for box in candidates[k]: if box[5] > config.getf( 'minPositiveOverlap'): # and box[6] >= 1.0: try: pos[k].append(box) except: pos[k] = [box] psc += 1 else: try: neg[k].append(box) except: neg[k] = [box] ngc += 1 self.pos = ImageBoxIndex(pos, True) self.neg = ImageBoxIndex(neg, True) self.probNeg = max(float(psc) / float(ngc), 0.05) print 'Positives:', psc, 'Negatives:', ngc
def test(self): interactions = config.geti('testInteractions') self.controller.setEpsilonGreedy(config.getf('testEpsilon')) self.runEpoch(interactions, len(self.environment.db.images))
def __init__(self, alpha=0.5): ValueBasedLearner.__init__(self) self.alpha = alpha self.gamma = config.getf('gammaDiscountReward') self.netManager = CaffeMultiLayerPerceptronManagement(config.get('networkDir'))
import RLConfig as config import numpy as np import scipy.io import MemoryUsage import RLConfig as config import BoxSearchState as bss import random STATE_FEATURES = config.geti('stateFeatures')/config.geti('temporalWindow') NUM_ACTIONS = config.geti('outputActions') TEMPORAL_WINDOW = config.geti('temporalWindow') HISTORY_FACTOR = config.geti('historyFactor') NEGATIVE_PROBABILITY = config.getf('negativeEpisodeProb') class BoxSearchAgent(): image = None observation = None action = None reward = None timer = 0 def __init__(self, qnet, learner=None): self.controller = qnet self.learner = learner self.avgReward = 0 self.replayMemory = None
def test(self): interactions = config.geti('testInteractions') self.controller.setEpsilonGreedy(config.getf('testEpsilon')) self.runEpoch(interactions, len(self.environment.imageList))
__author__ = "Juan C. Caicedo, [email protected]" from pybrain.rl.environments import Task import BoxSearchState as bss import utils as cu import libDetection as det import numpy as np import RLConfig as config MIN_ACCEPTABLE_IOU = config.getf('minAcceptableIoU') def center(box): return [ (box[2] + box[0])/2.0 , (box[3] + box[1])/2.0 ] def euclideanDist(c1, c2): return (c1[0] - c2[0])**2 + (c1[1] - c2[1])**2 class BoxSearchTask(Task): def __init__(self, environment=None, groundTruthFile=None): Task.__init__(self, environment) if groundTruthFile is not None: self.groundTruth = cu.loadBoxIndexFile(groundTruthFile) self.image = '' self.epochRecall = [] self.epochMaxIoU = [] self.epochLandmarks = [] def getReward(self):
import RLConfig as config import numpy as np import scipy.io import MemoryUsage import RLConfig as config import BoxSearchState as bss import random STATE_FEATURES = config.geti('stateFeatures') / config.geti('temporalWindow') NUM_ACTIONS = config.geti('outputActions') TEMPORAL_WINDOW = config.geti('temporalWindow') HISTORY_FACTOR = config.geti('historyFactor') NEGATIVE_PROBABILITY = config.getf('negativeEpisodeProb') class BoxSearchAgent(): image = None observation = None action = None reward = None timer = 0 def __init__(self, qnet, learner=None): self.controller = qnet self.learner = learner self.avgReward = 0 self.replayMemory = None
__author__ = "Juan C. Caicedo, [email protected]" from pybrain.rl.environments import Task import BoxSearchState as bss import utils as cu import libDetection as det import numpy as np import RLConfig as config MIN_ACCEPTABLE_IOU = config.getf('minAcceptableIoU') def center(box): return [(box[2] + box[0]) / 2.0, (box[3] + box[1]) / 2.0] def euclideanDist(c1, c2): return (c1[0] - c2[0])**2 + (c1[1] - c2[1])**2 class BoxSearchTask(Task): def __init__(self, environment=None, groundTruthFile=None): Task.__init__(self, environment) if groundTruthFile is not None: self.groundTruth = cu.loadBoxIndexFile(groundTruthFile) self.image = '' self.epochRecall = [] self.epochMaxIoU = [] self.epochLandmarks = []
def __init__(self, alpha=0.5): ValueBasedLearner.__init__(self) self.alpha = alpha self.gamma = config.getf('gammaDiscountReward') self.netManager = CaffeMultiLayerPerceptronManagement( config.get('networkDir'))