Example #1
0
    def frames(self):
        self.current_phase = 0
        while True:
            center = imagen.SineGrating(
                mask_shape=imagen.Disk(smoothing=0.0,
                                       size=self.center_radius * 2.0),
                orientation=self.center_orientation,
                frequency=self.spatial_frequency,
                phase=self.current_phase,
                bounds=BoundingBox(radius=self.size_x / 2.0),
                offset=0,
                scale=2 * self.background_luminance * self.contrast / 100.0,
                xdensity=self.density,
                ydensity=self.density)()
            r = (self.center_radius + self.surround_radius + self.gap) / 2.0
            t = (self.surround_radius - self.center_radius - self.gap) / 2.0
            surround = imagen.SineGrating(
                mask_shape=imagen.Ring(thickness=t * 2.0,
                                       smoothing=0.0,
                                       size=r * 2.0),
                orientation=self.surround_orientation,
                frequency=self.spatial_frequency,
                phase=self.current_phase,
                bounds=BoundingBox(radius=self.size_x / 2.0),
                offset=0,
                scale=2 * self.background_luminance * self.contrast / 100.0,
                xdensity=self.density,
                ydensity=self.density)()

            offset = imagen.Constant(
                mask_shape=imagen.Disk(smoothing=0.0,
                                       size=self.surround_radius * 2.0),
                bounds=BoundingBox(radius=self.size_x / 2.0),
                scale=self.background_luminance * (100.0 - self.contrast) /
                100.0,
                xdensity=self.density,
                ydensity=self.density)()

            background = (imagen.Disk(
                smoothing=0.0,
                size=self.surround_radius * 2.0,
                bounds=BoundingBox(radius=self.size_x / 2.0),
                xdensity=self.density,
                ydensity=self.density)() - 1) * -self.background_luminance

            yield (numpy.add.reduce(
                [numpy.maximum(center, surround), offset,
                 background]), [self.current_phase])
            self.current_phase += 2 * pi * (self.frame_duration /
                                            1000.0) * self.temporal_frequency
Example #2
0
    def frames(self):
        self.current_phase = 0
        while True:
            a = imagen.SineGrating(orientation=self.orientation,
                                   frequency=self.spatial_frequency,
                                   phase=self.current_phase,
                                   bounds=BoundingBox(radius=self.size_x / 2),
                                   offset=self.background_luminance *
                                   (100.0 - self.contrast) / 100.0,
                                   scale=2 * self.background_luminance *
                                   self.contrast / 100.0,
                                   xdensity=self.density,
                                   ydensity=self.density)()

            b = imagen.Null(scale=self.background_luminance,
                            bounds=BoundingBox(radius=self.size_x / 2),
                            xdensity=self.density,
                            ydensity=self.density)()
            c = imagen.Disk(smoothing=0.0,
                            size=self.radius * 2,
                            scale=1.0,
                            bounds=BoundingBox(radius=self.size_x / 2),
                            xdensity=self.density,
                            ydensity=self.density)()
            d1 = numpy.multiply(a, c)
            d2 = numpy.multiply(b, -(c - 1.0))
            d = numpy.add.reduce([d1, d2])
            yield (d, [self.current_phase])
            self.current_phase += 2 * pi * (self.frame_duration /
                                            1000.0) * self.temporal_frequency
Example #3
0
    def V1_afferent(self, src_properties, dest_properties):
        params = super(ModelSCALSparse,
                       self).V1_afferent(src_properties, dest_properties)
        # Adjust density for size of disk mask
        disk = ig.Disk(smoothing=0.0)
        output_fns = [CFPOF_DivisiveNormalizeL1_Sparse]
        if self.sprout_and_retract:
            cf_shape = disk
            same_shape = True
            output_fns = [
                CFSPOF_SproutRetract(target_sparsity=self.afferent_density)
            ] + output_fns
        else:
            density = self.afferent_density * (1 / (np.pi / 4))
            cf_shape = ig.random.BinaryUniformRandom(on_probability=min(
                [1, density]),
                                                     mask_shape=disk,
                                                     name='AffDensity')
            same_shape = False

        return dict(params[0],
                    cf_shape=cf_shape,
                    cf_type=SparseConnectionField,
                    same_cf_shape_for_all_cfs=same_shape,
                    response_fn=CFPRF_DotProduct_Sparse,
                    learning_fn=CFPLF_Hebbian_Sparse,
                    weights_output_fns=output_fns)
    def __init__(self, name, source, target, strength, delay, radius,
                 initial_connection_kernel):
        """
        Each Projection has a source sheet and target sheet.
        
        Parameters
        ----------
        radius : float
                 How big the connections fields will be in the source sheet coordinates!   
        source : Sheet
                 The sheet from which projection passes rates.

        target : Sheet
                 The sheet to which projection passes rates.
                 
        strength : float
                 The strength of the projection.
                 
        inititial_connection_kernel: ndarray
                 The initial connection kernel. 
        """
        Projection.__init__(self, name, source, target, strength, delay)
        self.radius = radius

        mask = imagen.Disk(xdensity=source.density,
                           ydensity=source.density,
                           bounds=BoundingBox(radius=source.size / 2),
                           size=self.radius * 2,
                           smoothing=0)
        initial_connection_kernel.xdensity = source.density
        initial_connection_kernel.ydensity = source.density
        initial_connection_kernel.bounds = BoundingBox(radius=source.size /
                                                       2.0)

        source_dim = self.source.unit_diameter
        target_dim = self.target.unit_diameter
        self.cfs = numpy.zeros(
            (target_dim * target_dim, source_dim * source_dim),
            dtype=numpy.float32)
        self.masks = numpy.zeros(
            (target_dim * target_dim, source_dim * source_dim),
            dtype=numpy.float32)

        for i in xrange(target_dim):
            for j in xrange(target_dim):
                ci, cj = self.target.index_to_coord(i, j)
                self.cfs[i * target_dim + j, :] = initial_connection_kernel(
                    x=cj, y=-ci).flatten()
                self.masks[i * target_dim + j, :] = mask(x=cj, y=-ci).flatten()

        #apply masks and normalize
        self.cfs = numpy.multiply(self.cfs, self.masks)
        self.cfs = self.cfs / numpy.sum(numpy.abs(self.cfs),
                                        axis=1)[:, numpy.newaxis]

        assert numpy.min(self.masks) == 0 and numpy.max(self.masks) == 1
        self.activity = numpy.zeros((target_dim, target_dim),
                                    dtype=numpy.float32)
Example #5
0
    def property_setup(self, properties):
        properties = super(ModelGCALInheritingColor, self).property_setup(properties)
        "Specify weight initialization, response function, and learning function"

        projection.CFProjection.cf_shape=imagen.Disk(smoothing=0.0)
        projection.CFProjection.response_fn=optimized.CFPRF_DotProduct_cython()
        projection.CFProjection.learning_fn=optimized.CFPLF_Hebbian_cython()
        projection.CFProjection.weights_output_fns=[transferfn.optimized.CFPOF_DivisiveNormalize_L1_cython()]
        projection.SharedWeightCFProjection.response_fn=responsefn.optimized.CFPRF_DotProduct_cython()
        return properties
Example #6
0
    def frames(self):
        self.current_phase = 0
        while True:
            d = imagen.Disk(smoothing=0.0,
                            size=self.radius * 2,
                            offset=self.background_luminance,
                            scale=self.background_luminance *
                            (self.contrast / 100.0),
                            bounds=BoundingBox(radius=self.size_x / 2),
                            xdensity=self.density,
                            ydensity=self.density)()

            yield (d, [self.current_phase])
Example #7
0
    def frames(self):
        fieldsize_x = self.size_x * self.density
        fieldsize_y = self.size_y * self.density
        folder_name = Global.root_directory + "/TextureImagesStimuli"
        libpath = __file__.replace(
            "/texture_based.py",
            "") + "/textureLib"  #path to the image processing library
        matlabPyrToolspath = os.path.join(libpath, "textureSynth",
                                          "matlabPyrTools")
        if not os.path.isdir(matlabPyrToolspath):
            raise IOError(
                "matlabPyrTools should be downloaded from https://github.com/LabForComputationalVision/matlabPyrTools and its content should be put in the directory "
                + matlabPyrToolspath)

        octave.addpath(libpath)
        im = octave.textureBasedStimulus(self.texture_path, self.stats_type,
                                         self.seed, fieldsize_x, fieldsize_y,
                                         libpath)
        scale = 2. * self.background_luminance / (numpy.max(im) -
                                                  numpy.min(im))
        im = (im - numpy.min(im)) * scale

        if not os.path.exists(folder_name):
            os.mkdir(folder_name)

        assert (im.shape == (fieldsize_x, fieldsize_y)
                ), "Image dimensions do not correspond to visual field size"

        b = imagen.Constant(scale=self.background_luminance,
                            bounds=BoundingBox(radius=self.size_x / 2),
                            xdensity=self.density,
                            ydensity=self.density)()
        c = imagen.Disk(smoothing=0.0,
                        size=self.radius * 2,
                        scale=1.0,
                        bounds=BoundingBox(radius=self.size_x / 2),
                        xdensity=self.density,
                        ydensity=self.density)()

        d1 = numpy.multiply(im, c)
        d2 = numpy.multiply(b, -(c - 1.0))
        d = numpy.add.reduce([d1, d2])

        d = d.astype(numpy.uint8)
        IM = Image.fromarray(d)
        IM.save(folder_name + "/" + self.texture + "sample" +
                str(self.sample) + "type" + str(self.stats_type) + 'radius' +
                str(self.radius) + '.pgm')

        while True:
            yield (d, [0])
Example #8
0
    def setup_attributes(self, attrs):
        attrs = super(ModelGCAL, self).setup_attributes(attrs)
        "Specify weight initialization, response function, and learning function"

        projection.CFProjection.cf_shape = imagen.Disk(smoothing=0.0)
        projection.CFProjection.response_fn = responsefn.optimized.CFPRF_DotProduct_opt(
        )
        projection.CFProjection.learning_fn = learningfn.optimized.CFPLF_Hebbian_opt(
        )
        projection.CFProjection.weights_output_fns = [
            transferfn.optimized.CFPOF_DivisiveNormalizeL1_opt()
        ]
        projection.SharedWeightCFProjection.response_fn = responsefn.optimized.CFPRF_DotProduct_opt(
        )
        return attrs
Example #9
0
    def property_setup(self, properties):
        "Specify weight initialization, response function, and learning function"
        properties = super(ModelSCAL, self).property_setup(properties)

        projection.CFProjection.cf_shape = ig.Disk(smoothing=0.0)
        projection.CFProjection.response_fn = optimized.CFPRF_DotProduct_cython(
        )
        projection.CFProjection.learning_fn = optimized.CFPLF_Hebbian_cython()
        projection.CFProjection.weights_output_fns = [
            optimized.CFPOF_DivisiveNormalize_L1_cython()
        ]
        projection.SharedWeightCFProjection.response_fn = optimized.CFPRF_DotProduct_cython(
        )
        sheet.SettlingCFSheet.joint_norm_fn = optimized.compute_joint_norm_totals_cython
        return properties
Example #10
0
    def V1_afferent(self, src_properties, dest_properties):
        "Projection delay and learning rate modified"
        paramlist = super(ModelTCAL, self).V1_afferent(
            src_properties, dest_properties)

        scatter = IdentityTF()  # Default when distribution is None

        if self.lgn_scatter_distribution is not None:
            scale, offset = self._scatter_scale_offset()
            distribution = self.lgn_scatter_distribution(
                name='LGN Latency Scatter',
                offset=offset, scale=scale)

            scatter = TemporalScatter(timestep=self.timestep,
                                      span=self.lgn_scatter_span,
                                      distribution=distribution)

        return dict(paramlist,
                    cf_shape = imagen.Disk(),
                    same_cf_shape_for_all_cfs=True,
                    input_fns=[scatter],
                    delay = paramlist['delay']-(self.lgn_scatter_span/2.0))
Example #11
0
    def __call__(self, projection, **params):
        time = math.ceil(topo.sim.time())

        if self.disk_mask:
            self.disk = ig.Disk(size=1.0,smoothing=0.0)

        # Get CF and src sheet shapes
        cf_x,cf_y = projection.dest.activity.shape
        src_x,src_y = projection.src.activity.shape

        # Initialize sparse triplet arrays
        y_array = np.zeros((src_x*src_y*cf_y),dtype=np.int32)
        x_array = np.zeros((src_x*src_y*cf_y),dtype=np.int32)
        val_array = np.zeros((src_x*src_y*cf_y),dtype=sparse_type)

        # Create new sparse matrix to accumulate into
        sum_sparse = sparse.csarray_float(projection.src.activity.shape,projection.dest.activity.shape)

        # Counters for logging
        sprout_sum = 0; prune_sum = 0; unit_total = 0
        self.mask_total = 0

        if (time == 0):
            if not hasattr(self,"initial_conns"):
                self.initial_conns = {}
            self.initial_conns[projection.name] = projection.n_conns()
        elif (time % self.interval) == 0:
            idx=0
            for cidx,cf in enumerate(projection.flatcfs):
                temp_weights = cf.weights
                dense_unit_mask = (1.0 - (temp_weights>0.0))
                dim1,dim2 = temp_weights.shape

                sprout_count,prune_idx,nnz = self.calc_ratios(temp_weights)

                self.prune(temp_weights,prune_idx)
                nnz_pp = np.count_nonzero(temp_weights)
                prune_sum += (nnz_pp-nnz)

                if sprout_count:
                    self.sprout(temp_weights,dense_unit_mask,sprout_count)
                nnz_ps = np.count_nonzero(temp_weights)
                sprout_sum += nnz_ps - nnz_pp
                unit_total += nnz_ps

                # Populate sparse array chunk
                temp_sparse = sparse.csarray_float(projection.src.activity.shape,projection.dest.activity.shape)
                x1,x2,y1,y2 = cf.input_sheet_slice.tolist()
                for cnx in range(dim1):
                    val_array[idx:idx+dim2] = temp_weights[cnx,:]
                    x_val = (x1+cnx) * src_y + y1
                    x_array[idx:idx+dim2] = range(x_val,x_val+dim2)
                    y_array[idx:idx+dim2] = cidx
                    idx += dim2

                # Populate combined sparse array with sparse array chunk
                if (cidx+1)%cf_y == 0:
                    nnz_idx = val_array.nonzero()
                    temp_sparse.setTriplets(x_array[nnz_idx],y_array[nnz_idx],val_array[nnz_idx])
                    sum_sparse += temp_sparse
                    x_array *= 0; y_array *= 0; val_array *= 0.0
                    idx=0
            projection.weights = sum_sparse
            del temp_sparse, sum_sparse
            projection.weights.compress()
ipython.magic("load_ext holoviews.ipython")
ipython.magic("opts Layout [vertical_spacing=0.0 horizontal_spacing=0.0] Image [show_xaxis=None show_yaxis=None show_frame=False show_title=False padding=0]")

def illusion(pat): 
    h, w = 256, 256
    uni = Image.new('RGB', (h,w))
    for i in range(0, w, bw):
        for j in range(0, h, bh):
            uni.paste(pat, (i, j))
    box = (50, 50, h-50, w-50)
    blur = uni.filter(ImageFilter.BLUR)
    blur = blur.crop(box)
    uni.paste(blur, box)
    return uni

shapes = [ig.Rectangle(), ig.RawRectangle(), ig.Ring(), ig.Disk()]
c = 0
for shape in shapes: 
    for i in range(-0.95, 0.01, 0.95):
        for j in range(-0.95,0.01, 0.95):
            shape.x = i
            shape.y = j

            c += 1
            hv.save(shape[:], 'illusion_image/blur/uniform/{}.png'.format(c));
            pat = Image.open('illusion_image/blur/uniform/{}.png'.format(c))
            pat = pat.resize((16,16))
            pat.save('illusion_image/blur/uniform/{}.png'.format(c))
            ill_0 = illusion(pat)
            ill_0.save('illusion_image/blur/blurred/{}.png'.format(c))