def first(self): """Set up the position. BaseExplorer picks image 0, offset (0,0), etc., but explorers that wish to set a different first position should extend this method. Such explorers may wish to call BaseExplorer.first(center=False), which initializes the position tuple but does not call centerImage() (which could cause unnecessary filtering to occur). """ BaseExplorer.first(self) self.directionIndex = 0 if self.numImages: self._firstSweepPosition()
def first(self, seeking=False): """Set up the position. BaseExplorer picks image 0, offset (0,0), etc., but explorers that wish to set a different first position should extend this method. Such explorers may wish to call BaseExplorer.first(center=False), which initializes the position tuple but does not call centerImage() (which could cause unnecessary filtering to occur). Args: seeking: Passed from seek() through next() to avoid loading images unnecessarily when seeking """ BaseExplorer.first(self, center=False) if not self.numImages: return if not self.replacement \ and len(self.history) == self.getNumIterations(None): # All images have been visited self.history = [] if self.equalizeCategories: # Breakdown the images by category if self.imagesByCat is None: categoryIndex = [] for k in range(self.numImages): categoryIndex += [self.getImageInfo(k)['categoryIndex']] categories = list(set(categoryIndex)) numCats = len(categories) catPopulation = {} imagesByCat = {} for catIndex in categories: #catPopulation[catIndex] = len([c for c in categoryIndex if c == catIndex]) imagesByCat[catIndex] = [k for k, c in enumerate(categoryIndex) if c == catIndex] catPopulation[catIndex] = len(imagesByCat[catIndex]) minNumSamples = min([pop for (cat, pop) in catPopulation.items()]) totalNumSamples = minNumSamples * numCats # Store self.imagesByCat = imagesByCat self.categories = categories self.numCategories = numCats self.nextCatIndex = 0 # Pick random image from next category thisCat = self.imagesByCat[self.nextCatIndex] #randomImageIndex = random.randint(0, len(thisCat)) self.position['image'] = self.random.choice(thisCat) self.position['filters'] = self.pickRandomFilters(self.random) self.nextCatIndex = (self.nextCatIndex + 1) % self.numCategories else: # Pick a random image and set of filters while self.start >= 0: finished = False while not finished: # Pick a position randomly self.position['image'] = self.pickRandomImage(self.random) self.position['filters'] = self.pickRandomFilters(self.random) # Pick again if not replacing and this position has been visited if self.replacement or (self.position['image'], self.position['filters']) not in self.history: finished = True if not self.replacement: # Remember this position self.history.append( (self.position['image'], self.position['filters'][:])) self.start -= 1 self.start = 0 if not seeking: self.centerImage()
def first(self, seeking=False): """Set up the position. BaseExplorer picks image 0, offset (0,0), etc., but explorers that wish to set a different first position should extend this method. Such explorers may wish to call BaseExplorer.first(center=False), which initializes the position tuple but does not call centerImage() (which could cause unnecessary filtering to occur). Args: seeking: Passed from seek() through next() to avoid loading images unnecessarily when seeking """ BaseExplorer.first(self, center=False) if not self.numImages: return if not self.replacement \ and len(self.history) == self.getNumIterations(None): # All images have been visited self.history = [] if self.equalizeCategories: # Breakdown the images by category if self.imagesByCat is None: categoryIndex = [] for k in range(self.numImages): categoryIndex += [self.getImageInfo(k)['categoryIndex']] categories = list(set(categoryIndex)) numCats = len(categories) catPopulation = {} imagesByCat = {} for catIndex in categories: #catPopulation[catIndex] = len([c for c in categoryIndex if c == catIndex]) imagesByCat[catIndex] = [ k for k, c in enumerate(categoryIndex) if c == catIndex ] catPopulation[catIndex] = len(imagesByCat[catIndex]) minNumSamples = min( [pop for (cat, pop) in catPopulation.items()]) totalNumSamples = minNumSamples * numCats # Store self.imagesByCat = imagesByCat self.categories = categories self.numCategories = numCats self.nextCatIndex = 0 # Pick random image from next category thisCat = self.imagesByCat[self.nextCatIndex] #randomImageIndex = random.randint(0, len(thisCat)) self.position['image'] = self.random.choice(thisCat) self.position['filters'] = self.pickRandomFilters(self.random) self.nextCatIndex = (self.nextCatIndex + 1) % self.numCategories else: # Pick a random image and set of filters while self.start >= 0: finished = False while not finished: # Pick a position randomly self.position['image'] = self.pickRandomImage(self.random) self.position['filters'] = self.pickRandomFilters( self.random) # Pick again if not replacing and this position has been visited if self.replacement or ( self.position['image'], self.position['filters']) not in self.history: finished = True if not self.replacement: # Remember this position self.history.append( (self.position['image'], self.position['filters'][:])) self.start -= 1 self.start = 0 if not seeking: self.centerImage()
def first(self): """Set up the position. BaseExplorer picks image 0, offset (0,0), etc., but explorers that wish to set a different first position should extend this method. Such explorers may wish to call BaseExplorer.first(center=False), which initializes the position tuple but does not call centerImage() (which could cause unnecessary filtering to occur). """ BaseExplorer.first(self, center=False) if not self.numImages: return # Pick a random direction and filtered image self.direction = self.random.choice(self.sweepDirections) self.position['image'] = self.random.randint(0, self.numImages - 1) for i in range(self.numFilters): self.position['filters'][i] = self.random.randint(0, self.numFilterOutputs[i] - 1) filteredImages = self.getFilteredImages() # Pick a random starting position on the appropriate edge of the image sbbox = self._getSweepBoundingBox(filteredImages[0]) if self.direction == 'left': self.position['offset'][0] = sbbox[2] - 1 self.position['offset'][1] = self.random.randint(sbbox[1], sbbox[3] - 1) elif self.direction == 'right': self.position['offset'][0] = sbbox[0] self.position['offset'][1] = self.random.randint(sbbox[1], sbbox[3] - 1) elif self.direction == 'up': self.position['offset'][0] = self.random.randint(sbbox[0], sbbox[2] - 1) self.position['offset'][1] = sbbox[3] - 1 elif self.direction == 'down': self.position['offset'][0] = self.random.randint(sbbox[0], sbbox[2] - 1) self.position['offset'][1] = sbbox[1] elif self.direction in ('leftup', 'upleft'): if self.random.randint(0, 1): self.position['offset'][0] = \ self.random.randint(int(sbbox[0] + (sbbox[2] - sbbox[0]) / 2), sbbox[2] - 1) self.position['offset'][1] = sbbox[3] - 1 else: self.position['offset'][0] = sbbox[2] - 1 self.position['offset'][1] = \ self.random.randint(int(sbbox[1] + (sbbox[3] - sbbox[1]) / 2), sbbox[3] - 1) elif self.direction in ('leftdown', 'downleft'): if self.random.randint(0, 1): self.position['offset'][0] = \ self.random.randint(int(sbbox[0] + (sbbox[2] - sbbox[0]) / 2), sbbox[2] - 1) self.position['offset'][1] = sbbox[1] else: self.position['offset'][0] = sbbox[2] - 1 self.position['offset'][1] = \ self.random.randint(sbbox[1], int(sbbox[3] - 1 - (sbbox[3] - sbbox[1]) / 2)) elif self.direction in ('rightup', 'upright'): if self.random.randint(0, 1): self.position['offset'][0] = \ self.random.randint(sbbox[0], int(sbbox[2] - 1 - (sbbox[2] - sbbox[0]) / 2)) self.position['offset'][1] = sbbox[3] - 1 else: self.position['offset'][0] = sbbox[0] self.position['offset'][1] = \ self.random.randint(int(sbbox[1] + (sbbox[3] - sbbox[1]) / 2), sbbox[3] - 1) elif self.direction in ('rightdown', 'downright'): if self.random.randint(0, 1): self.position['offset'][0] = \ self.random.randint(sbbox[0], int(sbbox[2] - 1 - (sbbox[2] - sbbox[0]) / 2)) self.position['offset'][1] = sbbox[1] else: self.position['offset'][0] = sbbox[0] self.position['offset'][1] = \ self.random.randint(sbbox[1], int(sbbox[3] - 1 - (sbbox[3] - sbbox[1]) / 2)) # Increment the position by a random amount in the range # [0, shiftDuringSweep) if self.shiftDuringSweep > 1: prevShiftDuringSweep = self.shiftDuringSweep self.shiftDuringSweep = self.random.randint(0, self.shiftDuringSweep) self._nextSweepPosition() self.shiftDuringSweep = prevShiftDuringSweep if self.position['reset']: self.first() self.position['reset'] = True