def _get_neuron_clear(self, x, y, orientation): coord = Coord(x=x, y=y) if coord not in self.pixels_map: return None neuron_coord = self.pixels_map[coord] neurons = [n for n in self.neurons if n.coord == neuron_coord and n.orientation == orientation] neuron = neurons[0] neuron.receptive_field.clear() return neuron
def _allocate_neuron(self, x, y, orientation): from vision.receptive_neuron import ReceptiveNeuron coord = Coord(x=x, y=y) neuron = ReceptiveNeuron(self.container.next_neuron_id(), container=self.container, coord=coord, layer=self, orientation=orientation) self.container.append_neuron(neuron) self.neurons.append(neuron) return neuron
def allocate(self): for y in range(self.regions_shape[0]): for x in range(self.regions_shape[1]): coord = Coord(x=x, y=y) region = SabComboLayer(layer_id=self.layer_id, container=self.container, num_units=self.num_sabs_per_region, coord=coord, parent_layer=self, sab_params=self.sab_params) # region.receptive_synapse_weight = self.receptive_synapse_weight region.allocate() self.regions.append(region)
def _create_pixels_2_neurons_map(self): width_pixel_map = {} height_pixel_map = {} if self.img.width > self.width: self._fill_pixel_map(width_pixel_map, self.img.width, self.width) else: self._pad_pixel_map(width_pixel_map, self.img.width, self.width) if self.img.height > self.height: self._fill_pixel_map(height_pixel_map, self.img.height, self.height) else: self._pad_pixel_map(height_pixel_map, self.img.height, self.height) self.pixels_map.clear() for x in width_pixel_map: neuron_x = width_pixel_map[x] if neuron_x is not None: for y in height_pixel_map: neuron_y = height_pixel_map[y] if neuron_y is not None: coord = Coord(x=x, y=y) self.pixels_map[coord] = Coord(x=neuron_x, y=neuron_y)
def _allocate_vertical_line(self, img: Image, x_coord): stride = img.height / self.height stride = 1 if stride < 1 else int(stride) num_dendrites = max(MIN_NUM_DENDRITES, stride * 2) for i in range(self.height - 1): neuron = self._get_neuron_clear(x_coord, i, Orientation.vertical) if not neuron: continue for y_shift in range(num_dendrites): y_coord = i * stride + y_shift if y_coord >= img.height: break neuron.receptive_field.append(Coord(x=x_coord, y=y_coord))
def _allocate_horizontal_line(self, img: Image, y_coord): stride = img.width / self.width stride = 1 if stride < 1 else int(stride) num_dendrites = max(MIN_NUM_DENDRITES, stride * 2) for i in range(self.width - 1): neuron = self._get_neuron_clear(i, y_coord, Orientation.horizontal) if not neuron: continue for x_shift in range(num_dendrites): x_coord = i * stride + x_shift if x_coord >= img.width: break neuron.receptive_field.append(Coord(x=x_coord, y=y_coord))
def _allocate_right_45(self, img: Image, x_coord): stride = img.height / self.height stride = 1 if stride < 1 else int(stride) num_dendrites = max(MIN_NUM_DENDRITES, stride * 2) if x_coord > img.width - num_dendrites: return middle_x = x_coord + round(num_dendrites / 2) for i in range(num_dendrites, self.height): middle_y = i - round(num_dendrites / 2) - 1 neuron = self._get_neuron_clear(middle_x, middle_y, Orientation.right_45) if not neuron: continue for shift in range(num_dendrites): receptive_y = i * stride - shift - 1 receptive_x = x_coord + shift if receptive_y < 0 or receptive_x >= img.width or receptive_y >= img.height: break neuron.receptive_field.append(Coord(x=receptive_x, y=receptive_y))