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'
Beispiel #2
0
 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'
Beispiel #3
0
 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()
         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
Beispiel #4
0
 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.priorMemory = PriorMemory(config.get('allObjectsBoxes'), self.groundTruth, self.cnn)
     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()
     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 __init__(self, alpha=0.5):
   ValueBasedLearner.__init__(self)
   self.alpha = alpha
   self.gamma = config.getf('gammaDiscountReward')
   self.netManager = CaffeMultiLayerPerceptronManagement(config.get('networkDir'))
__author__ = "Juan C. Caicedo, [email protected]"

import os
import random
import numpy as np
import scipy.io
import caffe

import learn.rl.RLConfig as config
import BoxSearchState as bs
from pybrain.rl.learners.valuebased.valuebased import ValueBasedLearner

DETECTION_REWARD = config.getf('detectionReward')
ACTION_HISTORY_SIZE = bs.NUM_ACTIONS*config.geti('actionHistoryLength')
ACTION_HISTORY_LENTH = config.geti('actionHistoryLength')
NETWORK_INPUTS = config.geti('stateFeatures')/config.geti('temporalWindow')
REPLAY_MEMORY_SIZE = config.geti('trainingIterationsPerBatch')*config.geti('trainingBatchSize')

def generateRandomActionHistory():
  actions = np.zeros((ACTION_HISTORY_SIZE))
  history = [i*bs.NUM_ACTIONS + np.random.randint(0,bs.PLACE_LANDMARK) for i in range(ACTION_HISTORY_LENTH)]
  actions[history] = 1
  return actions

class QLearning(ValueBasedLearner):

  offPolicy = True
  batchMode = True
  dataset = []

  trainingSamples = 0
Beispiel #8
0
__author__ = "Juan C. Caicedo, [email protected]"

from pybrain.rl.environments import Task
import BoxSearchState as bss

import utils.utils as cu
import utils.libDetection as det
import numpy as np

import learn.rl.RLConfig as config

MIN_ACCEPTABLE_IOU = config.getf('minAcceptableIoU')
DETECTION_REWARD = config.getf('detectionReward')


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 = []
Beispiel #9
0
X_COORD_UP         = 0
Y_COORD_UP         = 1
SCALE_UP           = 2
ASPECT_RATIO_UP    = 3
X_COORD_DOWN       = 4
Y_COORD_DOWN       = 5
SCALE_DOWN         = 6
ASPECT_RATIO_DOWN  = 7
PLACE_LANDMARK     = 8
SKIP_REGION        = 9

# BOX LIMITS
MIN_ASPECT_RATIO = 0.15
MAX_ASPECT_RATIO = 6.00
MIN_BOX_SIDE     = 10
STEP_FACTOR      = config.getf('boxResizeStep')
DELTA_SIZE       = config.getf('boxResizeStep')

# OTHER DEFINITIONS
NUM_ACTIONS = config.geti('outputActions')
RESET_BOX_FACTOR = 2
QUADRANT_SIZE = 0.7

def fingerprint(b):
  return '_'.join( map(str, map(int, b)) )

class BoxSearchState():

  def __init__(self, imageName, boxReset='Full', groundTruth=None):
    self.imageName = imageName
    self.visibleImage = Image.open(config.get('imageDir') + '/' + self.imageName + '.jpg')
Beispiel #10
0
 def __init__(self, alpha=0.5):
     ValueBasedLearner.__init__(self)
     self.alpha = alpha
     self.gamma = config.getf('gammaDiscountReward')
     self.netManager = CaffeMultiLayerPerceptronManagement(
         config.get('networkDir'))
__author__ = "Juan C. Caicedo, [email protected]"

from pybrain.rl.environments import Task
import BoxSearchState as bss

import utils.utils as cu
import utils.libDetection as det
import numpy as np

import learn.rl.RLConfig as config

MIN_ACCEPTABLE_IOU = config.getf('minAcceptableIoU')
DETECTION_REWARD = config.getf('detectionReward')

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 = []
Beispiel #12
0
__author__ = "Juan C. Caicedo, [email protected]"

import os
import random
import numpy as np
import scipy.io
import caffe

import learn.rl.RLConfig as config
import BoxSearchState as bs
from pybrain.rl.learners.valuebased.valuebased import ValueBasedLearner

DETECTION_REWARD = config.getf('detectionReward')
ACTION_HISTORY_SIZE = bs.NUM_ACTIONS * config.geti('actionHistoryLength')
ACTION_HISTORY_LENTH = config.geti('actionHistoryLength')
NETWORK_INPUTS = config.geti('stateFeatures') / config.geti('temporalWindow')
REPLAY_MEMORY_SIZE = config.geti('trainingIterationsPerBatch') * config.geti(
    'trainingBatchSize')


def generateRandomActionHistory():
    actions = np.zeros((ACTION_HISTORY_SIZE))
    history = [
        i * bs.NUM_ACTIONS + np.random.randint(0, bs.PLACE_LANDMARK)
        for i in range(ACTION_HISTORY_LENTH)
    ]
    actions[history] = 1
    return actions


class QLearning(ValueBasedLearner):
Beispiel #13
0
 def test(self):
     interactions = config.geti('testInteractions')
     self.controller.setEpsilonGreedy(config.getf('testEpsilon'))
     self.runEpoch(interactions, len(self.environment.imageList))
import learn.rl.RLConfig as config

import numpy as np
import scipy.io
import utils.MemoryUsage

import BoxSearchState as bss
import PriorMemory as prm
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
    self.priorMemory = None
Beispiel #15
0
__author__ = "Juan C. Caicedo, [email protected]"

import os
import utils.utils as cu
import numpy as np

import caffe
from caffe import wrapperv0

import learn.rl.RLConfig as config

LAYER = config.get('convnetLayer')
MARK_WIDTH = config.getf('markWidth')

class ConvNet():

  def __init__(self):
    self.net = None
    self.image = ''
    self.id = 0
    self.loadNetwork()

  def loadNetwork(self):
    self.imgDim = config.geti('imageDim')
    self.cropSize = config.geti('cropSize')
    self.contextPad = config.geti('contextPad')
    #self.stateContextFactor = config.geti('stateContextFactor')
    modelFile = config.get('convnetDir') + config.get('convNetDef')
    networkFile = config.get('convnetDir') + config.get('trainedConvNet')
    self.net = wrapperv0.ImageNetClassifier(modelFile, networkFile, IMAGE_DIM=self.imgDim, CROPPED_DIM=self.cropSize, MEAN_IMAGE=config.get('meanImage'))
    self.net.caffenet.set_mode_gpu()
 def test(self):
   interactions = config.geti('testInteractions')
   self.controller.setEpsilonGreedy(config.getf('testEpsilon'))
   self.runEpoch(interactions, len(self.environment.imageList))
Beispiel #17
0
import learn.rl.RLConfig as config

import numpy as np
import scipy.io
import utils.MemoryUsage

import BoxSearchState as bss
import PriorMemory as prm
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
Beispiel #18
0
__author__ = "Juan C. Caicedo, [email protected]"

import os
import utils.utils as cu
import numpy as np

import caffe
from caffe import wrapperv0

import learn.rl.RLConfig as config

LAYER = config.get('convnetLayer')
MARK_WIDTH = config.getf('markWidth')


class ConvNet():
    def __init__(self):
        self.net = None
        self.image = ''
        self.id = 0
        self.loadNetwork()

    def loadNetwork(self):
        self.imgDim = config.geti('imageDim')
        self.cropSize = config.geti('cropSize')
        self.contextPad = config.geti('contextPad')
        #self.stateContextFactor = config.geti('stateContextFactor')
        modelFile = config.get('convnetDir') + config.get('convNetDef')
        networkFile = config.get('convnetDir') + config.get('trainedConvNet')
        self.net = wrapperv0.ImageNetClassifier(
            modelFile,