3 @brief Neural Network: Pulse Coupled Neural Network 4 @details Implementation based on paper @cite book::image_processing_using_pcnn. 6 @authors Andrei Novikov (pyclustering@yandex.ru) 8 @copyright GNU Public License 10 @cond GNU_PUBLIC_LICENSE 11 PyClustering is free software: you can redistribute it and/or modify 12 it under the terms of the GNU General Public License as published by 13 the Free Software Foundation, either version 3 of the License, or 14 (at your option) any later version. 16 PyClustering is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 GNU General Public License for more details. 21 You should have received a copy of the GNU General Public License 22 along with this program. If not, see <http://www.gnu.org/licenses/>. 32 import matplotlib.pyplot
as plt
33 import matplotlib.animation
as animation
34 except Exception
as error_instance:
35 warnings.warn(
"Impossible to import matplotlib (please, install 'matplotlib'), pyclustering's visualization " 36 "functionality is partially not available (details: '%s')." % str(error_instance))
40 except Exception
as error_instance:
41 warnings.warn(
"Impossible to import PIL (please, install 'PIL'), pyclustering's visualization " 42 "functionality is partially not available (details: '%s')." % str(error_instance))
46 from pyclustering.core.wrapper
import ccore_library
48 import pyclustering.core.pcnn_wrapper
as wrapper
55 @brief Parameters for pulse coupled neural network. 61 @brief Default constructor of parameters for pulse-coupled neural network. 62 @details Constructor initializes parameters by default non-zero values that can be 63 used for simple simulation. 102 @brief Represents output dynamic of PCNN (pulse-coupled neural network). 109 @brief (list) Returns oscillato outputs during simulation. 121 @brief (list) Returns sampling times when dynamic is measured during simulation. 127 return list(range(len(self)))
132 @brief Constructor of PCNN dynamic. 134 @param[in] dynamic (list): Dynamic of oscillators on each step of simulation. If ccore pointer is specified than it can be ignored. 135 @param[in] ccore (ctypes.pointer): Pointer to CCORE pcnn_dynamic instance in memory. 147 @brief Default destructor of PCNN dynamic. 156 @brief (uint) Returns number of simulation steps that are stored in dynamic. 167 @brief Allocate clusters in line with ensembles of synchronous oscillators where each 168 synchronous ensemble corresponds to only one cluster. 170 @return (list) Grours (lists) of indexes of synchronous oscillators. 171 For example, [ [index_osc1, index_osc3], [index_osc2], [index_osc4, index_osc5] ]. 179 traverse_oscillators = set()
181 number_oscillators = len(self.
__dynamic[0])
183 for t
in range(len(self.
__dynamic) - 1, 0, -1):
185 for i
in range(number_oscillators):
187 if i
not in traverse_oscillators:
188 sync_ensemble.append(i)
189 traverse_oscillators.add(i)
191 if sync_ensemble != []:
192 sync_ensembles.append(sync_ensemble)
194 return sync_ensembles
199 @brief Analyses output dynamic of network and allocates spikes on each iteration as a list of indexes of oscillators. 200 @details Each allocated spike ensemble represents list of indexes of oscillators whose output is active. 202 @return (list) Spike ensembles of oscillators. 210 number_oscillators = len(self.
__dynamic[0])
215 for index
in range(number_oscillators):
217 spike_ensemble.append(index)
219 if len(spike_ensemble) > 0:
220 spike_ensembles.append(spike_ensemble)
222 return spike_ensembles
227 @brief Analyses output dynamic and calculates time signal (signal vector information) of network output. 229 @return (list) Time signal of network output. 236 signal_vector_information = []
238 signal_vector_information.append(sum(self.
__dynamic[t]))
240 return signal_vector_information
245 @brief Visualizer of output dynamic of pulse-coupled neural network (PCNN). 252 @brief Shows time signal (signal vector information) using network dynamic during simulation. 254 @param[in] pcnn_output_dynamic (pcnn_dynamic): Output dynamic of the pulse-coupled neural network. 258 time_signal = pcnn_output_dynamic.allocate_time_signal()
259 time_axis = range(len(time_signal))
262 plt.plot(time_axis, time_signal,
'-')
263 plt.ylabel(
"G (time signal)")
264 plt.xlabel(
"t (iteration)")
272 @brief Shows output dynamic (output of each oscillator) during simulation. 274 @param[in] pcnn_output_dynamic (pcnn_dynamic): Output dynamic of the pulse-coupled neural network. 275 @param[in] separate_representation (list): Consists of lists of oscillators where each such list consists of oscillator indexes that will be shown on separated stage. 279 draw_dynamics(pcnn_output_dynamic.time, pcnn_output_dynamic.output, x_title =
"t", y_title =
"y(t)", separate = separate_representation)
284 @brief Shows animation of output dynamic (output of each oscillator) during simulation. 286 @param[in] pcnn_output_dynamic (pcnn_dynamic): Output dynamic of the pulse-coupled neural network. 287 @param[in] image_size (tuple): Image size represented as (height, width). 291 figure = plt.figure()
293 time_signal = pcnn_output_dynamic.allocate_time_signal()
294 spike_ensembles = pcnn_output_dynamic.allocate_spike_ensembles()
298 for t
in range(len(time_signal)):
299 image_color_segments = [(255, 255, 255)] * (image_size[0] * image_size[1])
301 if time_signal[t] > 0:
302 for index_pixel
in spike_ensembles[ensemble_index]:
303 image_color_segments[index_pixel] = (0, 0, 0)
307 stage = numpy.array(image_color_segments, numpy.uint8)
308 stage = numpy.reshape(stage, image_size + ((3),))
309 image_cluster = Image.fromarray(stage,
'RGB')
311 spike_animation.append( [ plt.imshow(image_cluster, interpolation=
'none') ] )
314 im_ani = animation.ArtistAnimation(figure, spike_animation, interval=75, repeat_delay=3000, blit=
True)
320 @brief Model of oscillatory network that is based on the Eckhorn model. 322 @details CCORE option can be used to use the pyclustering core - C/C++ shared library for processing that significantly increases performance. 324 Here is an example how to perform PCNN simulation: 326 from pyclustering.nnet.pcnn import pcnn_network, pcnn_visualizer 328 # Create Pulse-Coupled neural network with 10 oscillators. 329 net = pcnn_network(10) 331 # Perform simulation during 100 steps using binary external stimulus. 332 dynamic = net.simulate(50, [1, 1, 1, 0, 0, 0, 0, 1, 1, 1]) 334 # Allocate synchronous ensembles from the output dynamic. 335 ensembles = dynamic.allocate_sync_ensembles() 337 # Show output dynamic. 338 pcnn_visualizer.show_output_dynamic(dynamic, ensembles) 346 def __init__(self, num_osc, parameters=None, type_conn=conn_type.ALL_TO_ALL, type_conn_represent=conn_represent.MATRIX, height=None, width=None, ccore=True):
348 @brief Constructor of oscillatory network is based on Kuramoto model. 350 @param[in] num_osc (uint): Number of oscillators in the network. 351 @param[in] parameters (pcnn_parameters): Parameters of the network. 352 @param[in] type_conn (conn_type): Type of connection between oscillators in the network (all-to-all, grid, bidirectional list, etc.). 353 @param[in] type_conn_represent (conn_represent): Internal representation of connection in the network: matrix or list. 354 @param[in] height (uint): Number of oscillators in column of the network, this argument is used 355 only for network with grid structure (GRID_FOUR, GRID_EIGHT), for other types this argument is ignored. 356 @param[in] width (uint): Number of oscillotors in row of the network, this argument is used only 357 for network with grid structure (GRID_FOUR, GRID_EIGHT), for other types this argument is ignored. 358 @param[in] ccore (bool): If True then all interaction with object will be performed via CCORE library (C++ implementation of pyclustering). 373 if parameters
is not None:
378 if (ccore
is True)
and ccore_library.workable():
379 network_height = height
380 network_width = width
382 if (type_conn == conn_type.GRID_FOUR)
or (type_conn == conn_type.GRID_EIGHT):
383 if (network_height
is None)
or (network_width
is None):
384 side_size = num_osc ** (0.5)
385 if side_size - math.floor(side_size) > 0:
386 raise NameError(
'Invalid number of oscillators in the network in case of grid structure')
388 network_height = int(side_size)
389 network_width = int(side_size)
396 super().
__init__(num_osc, type_conn, type_conn_represent, height, width)
407 @brief Default destructor of PCNN. 417 @brief (uint) Returns size of oscillatory network. 429 @brief Performs static simulation of pulse coupled neural network using. 431 @param[in] steps (uint): Number steps of simulations during simulation. 432 @param[in] stimulus (list): Stimulus for oscillators, number of stimulus should be equal to number of oscillators. 434 @return (pcnn_dynamic) Dynamic of oscillatory network - output of each oscillator on each step of simulation. 438 if len(stimulus) != len(self):
439 raise NameError(
'Number of stimulus should be equal to number of oscillators. Each stimulus corresponds to only one oscillators.')
448 for step
in range(1, steps, 1):
456 def _calculate_states(self, stimulus):
458 @brief Calculates states of oscillators in the network for current step and stored them except outputs of oscillators. 460 @param[in] stimulus (list): Stimulus for oscillators, number of stimulus should be equal to number of oscillators. 462 @return (list) New outputs for oscillators (do not stored it). 471 for index
in range(0, self.
_num_osc, 1):
474 feeding_influence = 0.0
475 linking_influence = 0.0
477 for index_neighbour
in neighbors:
481 feeding_influence *= self.
_params.VF
482 linking_influence *= self.
_params.VL
484 feeding[index] = self.
_params.AF * self.
_feeding[index] + stimulus[index] + feeding_influence
485 linking[index] = self.
_params.AL * self.
_linking[index] + linking_influence
488 internal_activity = feeding[index] * (1.0 + self.
_params.B * linking[index])
491 if internal_activity > self.
_threshold[index]:
497 if self.
_params.FAST_LINKING
is not True:
501 if self.
_params.FAST_LINKING
is True:
503 previous_outputs = outputs[:]
505 while output_change
is True:
506 current_output_change =
False 508 for index
in range(0, self.
_num_osc, 1):
509 linking_influence = 0.0
512 for index_neighbour
in neighbors:
513 linking_influence += previous_outputs[index_neighbour] * self.
_params.W
515 linking_influence *= self.
_params.VL
516 linking[index] = linking_influence
518 internal_activity = feeding[index] * (1.0 + self.
_params.B * linking[index])
521 if internal_activity > self.
_threshold[index]:
526 current_output_change |= (outputs[index] != previous_outputs[index])
528 output_change = current_output_change
530 if output_change
is True:
531 previous_outputs = outputs[:]
534 if self.
_params.FAST_LINKING
is True:
535 for index
in range(0, self.
_num_osc, 1):
Model of oscillatory network that is based on the Eckhorn model.
M
Synaptic weight - neighbours influence on feeding compartment.
FAST_LINKING
Enable/disable Fast-Linking mode.
AF
Multiplier for the feeding compartment at the previous step.
def __del__(self)
Default destructor of PCNN dynamic.
def _calculate_states(self, stimulus)
Calculates states of oscillators in the network for current step and stored them except outputs of os...
AT
Multiplier for the threshold at the previous step.
def animate_spike_ensembles(pcnn_output_dynamic, image_size)
Shows animation of output dynamic (output of each oscillator) during simulation.
def time(self)
(list) Returns sampling times when dynamic is measured during simulation.
Utils that are used by modules of pyclustering.
AL
Multiplier for the linking compartment at the previous step.
Parameters for pulse coupled neural network.
def simulate(self, steps, stimulus)
Performs static simulation of pulse coupled neural network using.
W
Synaptic weight - neighbours influence on linking compartment.
def allocate_spike_ensembles(self)
Analyses output dynamic of network and allocates spikes on each iteration as a list of indexes of osc...
B
Linking strength in the network.
def get_neighbors(self, index)
Finds neighbors of the oscillator with specified index.
def __init__(self, num_osc, parameters=None, type_conn=conn_type.ALL_TO_ALL, type_conn_represent=conn_represent.MATRIX, height=None, width=None, ccore=True)
Constructor of oscillatory network is based on Kuramoto model.
__ccore_pcnn_dynamic_pointer
def __init__(self)
Default constructor of parameters for pulse-coupled neural network.
VT
Multiplier for the threshold at the current step.
Represents output dynamic of PCNN (pulse-coupled neural network).
def __del__(self)
Default destructor of PCNN.
Common network description that consists of information about oscillators and connection between them...
def __len__(self)
(uint) Returns number of simulation steps that are stored in dynamic.
def show_output_dynamic(pcnn_output_dynamic, separate_representation=False)
Shows output dynamic (output of each oscillator) during simulation.
VF
Multiplier for the feeding compartment at the current step.
def allocate_sync_ensembles(self)
Allocate clusters in line with ensembles of synchronous oscillators where each synchronous ensemble c...
def __len__(self)
(uint) Returns size of oscillatory network.
VL
Multiplier for the linking compartment at the current step.
Visualizer of output dynamic of pulse-coupled neural network (PCNN).
def allocate_time_signal(self)
Analyses output dynamic and calculates time signal (signal vector information) of network output...
def __init__(self, dynamic, ccore=None)
Constructor of PCNN dynamic.
def output(self)
(list) Returns oscillato outputs during simulation.
Neural and oscillatory network module.
def show_time_signal(pcnn_output_dynamic)
Shows time signal (signal vector information) using network dynamic during simulation.