Пример #1
0
class OutputLayer(Layer):

	def __init__(self, size):
		Layer.__init__(self, size)

		self.previous_layer = None

		self.W = None
		self.B = None
		self.E = None
		
		self.activation_function = lambda x : 1 / (1 + math.exp(-x))


	def initialize(self, previous_layer):
		self.previous_layer = previous_layer
		self.W = Matrix(self.size, previous_layer.size).randomize(-1, 1)
		self.B = Matrix(self.size, 1).randomize(-1, 1)


	def feed_forward(self):
		self.values = ((self.W * self.previous_layer.values) + self.B).map_function(self.activation_function)


	def calculate_errors(self, target_arr):
		if len(target_arr) == self.size:
			self.E = Matrix.from_list(target_arr, self.size, 1) - self.values
		else: raise ValueError("Incorrect target size.")


	def adjust_parameters(self, learning_rate):
		gradients = self.values.map_function(lambda x : x * (1 - x)).get_hadamard_product(self.E).get_scalar_multiple(learning_rate)
		self.W.add(gradients * self.previous_layer.values.get_transpose())
		self.B.add(gradients)
Пример #2
0
def run(instructions):
    matrix = Matrix(set([61, 17]))
    for instruction in instructions:
        print("Intruction: %s" % instruction.strip())
        matrix.execute_instruction(instruction)
    matrix.done.wait()
    time.sleep(.1)  # because of independent IO
    print("===\nThe result is %s" % matrix.result)
Пример #3
0
class TestExample(unittest.TestCase):
    """
    Tests the given example
    """
    def setUp(self):
        self.matrix = Matrix(TARGET)

    def test__result_is_right(self):
        for instruction in INSTRUCTIONS:
            self.matrix.execute_instruction(instruction)
        self.matrix.done.wait()
        time.sleep(.1)
        self.assertEqual(RESULT, str(self.matrix.result))
Пример #4
0
    def __init__(self, config, lux, matrix11x7_available, level):
        super().__init__()
        global video_width, video_height, annotation_title
        self._log = Logger('video', level)
        if config is None:
            raise ValueError("no configuration provided.")
        self._config = config
        self._lux = lux
        _config = self._config['ros'].get('video')
        self._enable_streaming = _config.get('enable_streaming')
        self._port = _config.get('port')
        self._lux_threshold = _config.get('lux_threshold')
        self._counter = itertools.count()
        self._server = None

        # camera configuration
        self._ctrl_lights = _config.get('ctrl_lights')
        self._width = _config.get('width')
        self._height = _config.get('height')
        self._resolution = (self._width, self._height)
        self._framerate = _config.get('framerate')
        self._convert_mp4 = _config.get('convert_mp4')
        self._remove_h264 = _config.get('remove_h264')
        self._quality = _config.get('quality')
        self._annotate = _config.get('annotate')
        self._title = _config.get('title')
        self._basename = _config.get('basename')
        self._dirname = _config.get('dirname')
        # set globals
        video_width = self._width
        video_height = self._height
        annotation_title = self._title
        self._filename = None
        self._thread = None
        self._killer = None

        # lighting configuration
        if matrix11x7_available and self._ctrl_lights:
            self._port_light = Matrix(Orientation.PORT, Level.INFO)
            self._stbd_light = Matrix(Orientation.STBD, Level.INFO)
            self._log.info('camera lighting available.')
        else:
            self._port_light = None
            self._stbd_light = None
            self._log.info('no camera lighting available.')

        if self._enable_streaming:
            self._log.info('ready: streaming on port {:d}'.format(self._port))
        else:
            self._log.info('ready: save to file only, no streaming.')
    def predict(self, input_array):
        '''
        Predicts the answers based on some input
        '''
        # Generating the hidden outputs
        inputs = Matrix.from_array(input_array)
        hidden = Matrix.static_mult(self.weights_ih, inputs)
        hidden.add(self.bias_h)

        # Activation function!
        hidden.map(sigmoid)

        # Generating the output's output
        output = Matrix.static_mult(self.weights_ho, hidden)
        output.add(self.bias_o)
        output.map(sigmoid)

        # Sending back to caller!
        return output.to_array()
Пример #6
0
class HiddenLayer(Layer):

	def __init__(self, size):
		Layer.__init__(self, size)

		self.previous_layer = None
		self.next_layer = None
		
		self.W = None
		self.B = None
		self.E = None
		
		self.activation_function = lambda x : 1 / (1 + math.exp(-x))


	def initialize(self, previous_layer, next_layer):
		self.previous_layer = previous_layer
		self.next_layer = next_layer
		self.W = Matrix(self.size, previous_layer.size).randomize(-1, 1)
		self.B = Matrix(self.size, 1).randomize(-1, 1)


	def feed_forward(self):
		self.values = (self.W * self.previous_layer.values + self.B).map_function(self.activation_function)


	def calculate_errors(self):
		self.E = self.next_layer.W.get_transpose() * self.next_layer.E


	def adjust_parameters(self, learning_rate):
		# print(self.values)
		# print(self.E)
		gradients = self.values.map_function(lambda x : x * (1 - x)).get_hadamard_product(self.E).get_scalar_multiple(learning_rate)
		# print(gradients)





		self.W.add(gradients * self.previous_layer.values.get_transpose())
		self.B.add(gradients)
def QR_method_gram_sh(mtx):
    Q = mtx.gram_shmidt()
    m = mtx.data.shape[0]
    n = mtx.data.shape[1]
    R = np.zeros([n, n])

    for i in range(0, n):
        A = np.concatenate((Q, np.array([mtx.data[:, i]]).T), axis=1)
        r = gauss_elimination(Matrix(A))
        R[:, i] = r[0]

    return Q, R
    def __init__(self, numI, numH, numO):
        self.numI = numI
        self.numH = numH
        self.numO = numO

        self.weights_ih = Matrix(numH, numI)
        self.weights_ho = Matrix(numO, numH)
        self.weights_ih.randomize()
        self.weights_ho.randomize()

        self.bias_h = Matrix(numH, 1)
        self.bias_o = Matrix(numO, 1)
        self.bias_h.randomize()
        self.bias_o.randomize()

        self.learning_rate = 0.1
def eign_qr_hh(mtx):
    Q, R = QR_method_hh(mtx)

    eignval = np.diag(R)
    D = np.zeros([mtx.data.shape[0], eignval.shape[0]])
    for i in range(eignval.shape[0]):
        A = mtx.data - eignval[i] * np.eye(mtx.data.shape[0])
        b = np.zeros([1, A.shape[0]]).T
        b[A.shape[0] - 1, 0] = 1  # chute para sair da solução trivial
        A = np.concatenate((A, b), axis=1)

        x = gauss_elimination(Matrix(A))
        x = x / np.linalg.norm(x)
        D[:, i] = x

    return Q, eignval
def svd(mtx):
    A = mtx.data
    m = A.shape[0]
    n = A.shape[1]

    M = np.matmul(A, A.T)
    U, eig_M = eign_qr_hh(Matrix(M))

    S = np.eye(m)
    for i in range(0, m):
        S[i, i] = np.sqrt(np.abs(eig_M[i]))

    V = np.matmul(np.linalg.inv(S), U.T)
    V = np.matmul(V, A)

    return U, S, V
class NeuralNetwork:
    '''
    Simple Neural Network with three layers
    '''
    def __init__(self, numI, numH, numO):
        self.numI = numI
        self.numH = numH
        self.numO = numO

        self.weights_ih = Matrix(numH, numI)
        self.weights_ho = Matrix(numO, numH)
        self.weights_ih.randomize()
        self.weights_ho.randomize()

        self.bias_h = Matrix(numH, 1)
        self.bias_o = Matrix(numO, 1)
        self.bias_h.randomize()
        self.bias_o.randomize()

        self.learning_rate = 0.1

    @staticmethod
    def from_trained(info):
        '''
        Creates a Neural Network from the information
        of another one
        '''
        numI = info['dims'][0]
        numH = info['dims'][1]
        numO = info['dims'][2]
        nn = NeuralNetwork(numI, numH, numO)
        nn.weights_ho.data = info['weights_ho']
        nn.weights_ih.data = info['weights_ih']
        nn.bias_h.data = info['bias_h']
        nn.bias_o.data = info['bias_o']

        return nn

    def predict(self, input_array):
        '''
        Predicts the answers based on some input
        '''
        # Generating the hidden outputs
        inputs = Matrix.from_array(input_array)
        hidden = Matrix.static_mult(self.weights_ih, inputs)
        hidden.add(self.bias_h)

        # Activation function!
        hidden.map(sigmoid)

        # Generating the output's output
        output = Matrix.static_mult(self.weights_ho, hidden)
        output.add(self.bias_o)
        output.map(sigmoid)

        # Sending back to caller!
        return output.to_array()

    def train(self, input_array, target_array):
        '''
        Trains the NN based on some input_data and
        target_data
        '''
        # Generating the hidden outputs
        inputs = Matrix.from_array(input_array)
        hidden = Matrix.static_mult(self.weights_ih, inputs)
        hidden.add(self.bias_h)

        # Activation function!
        hidden.map(sigmoid)

        # Generating the output's output
        outputs = Matrix.static_mult(self.weights_ho, hidden)
        outputs.add(self.bias_o)
        outputs.map(sigmoid)

        # Convert array to matrix object
        targets = Matrix.from_array(target_array)

        # Calculate error
        # error = targets - outputs

        output_errors = Matrix.subtract(targets, outputs)
        # gradient = outputs * (1 - outputs)

        # Calculate gradient
        gradient = Matrix.static_map(outputs, dsigmoid)
        gradient.mult(output_errors)
        gradient.mult(self.learning_rate)

        # Calculate deltas
        hidden_T = Matrix.transpose(hidden)
        weight_ho_deltas = Matrix.static_mult(gradient, hidden_T)

        # Adjust the weigts by deltas
        self.weights_ho.add(weight_ho_deltas)
        self.bias_o.add(gradient)

        # Calculate the hidden layer errors
        weights_ho_T = Matrix.transpose(self.weights_ho)
        hidden_errors = Matrix.static_mult(weights_ho_T, output_errors)

        # Calculate hidden gradient
        hidden_gradient = Matrix.static_map(hidden, dsigmoid)
        hidden_gradient.mult(hidden_errors)
        hidden_gradient.mult(self.learning_rate)

        # Calculate input to hidden deltas
        inputs_T = Matrix.transpose(inputs)
        weight_ih_deltas = Matrix.static_mult(hidden_gradient, inputs_T)
        self.weights_ih.add(weight_ih_deltas)
        self.bias_h.add(hidden_gradient)

    def save(self):
        '''
        Saves the information of the NN to a .npz file, so it
        can be loaded later to create another NN
        '''
        dims = numpy.array([self.numI, self.numH, self.numO])
        weights_ih = numpy.array(self.weights_ih.to_array()).reshape(
            (self.numH, self.numI))
        weights_ho = numpy.array(self.weights_ho.to_array()).reshape(
            (self.numO, self.numH))
        bias_h = numpy.array(self.bias_h.to_array()).reshape((self.numH, 1))
        bias_o = numpy.array(self.bias_o.to_array()).reshape((self.numO, 1))

        numpy.savez("digit_predictor_NN.npz",
                    dims=dims,
                    weights_ih=weights_ih,
                    weights_ho=weights_ho,
                    bias_h=bias_h,
                    bias_o=bias_o)
Пример #12
0
import numpy as np
from lib.matrix import Matrix
from lib.linear_system import gauss_elimination, gauss_elimination_reduction
from lib.decompositions import QR_method_gram_sh, eign_qr_hh, svd

np.set_printoptions(precision=5)

a = np.array([[4, 3], [6, 3]]) # 2x2
b = np.array([[3, 3, 1], [4, 5, 9]]) # 2x3
c = np.array([[2, 1, 1, 3], [3, 7, 9, 1], [1, 4, -11, 10]]) # 3x4
d = np.array([[2, 1, 0, 3, 4], [1, 1, -1, 1, 1], [1, -1, -1, 2, -3], [-3, 2, 3, -1, 4]]) # 4x5
e = np.array([[3, 4, 1], [2, 2, 1], [3, 4, 5]]) # 3x3
f = np.array([[4, 2, -4], [2, 10, 4], [-4, 4, 9]]) # 3x3 - positiva definida e simétrica
g = np.array([[3.0, 2.0, 4.0], [1.0, 1.0, 2.0], [4.0, 3.0, -2.0]]) # 3x3 - teste fatoração LU

ma = Matrix(a)
mb = Matrix(b)
mc = Matrix(c)
md = Matrix(d)
me = Matrix(e)
mf = Matrix(f)
mg = Matrix(g)

# test scalar product

#scalar_prod_a = ma.scalar_product(2)
#scalar_prod_b = mb.scalar_product(3)

# print(scalar_prod_a.data)
# print(scalar_prod_b.data)
Пример #13
0
 def setUp(self):
     self.matrix = Matrix(TARGET)
Пример #14
0
from lib.matrix import Matrix

matrix = Matrix.fromString("""
    1 0 0 0 0 0 1 0 1 1
    0 1 0 0 0 0 0 1 1 0
    0 0 1 0 0 0 1 1 0 0
    0 0 0 1 0 0 1 0 1 0
    0 0 0 0 1 0 0 0 1 1
    0 0 0 0 0 1 0 1 0 1
""")
print(matrix)
span = matrix.to_minimal_span_form()
print(span)
print("profile: " + str(tuple(map(lambda num: "2^%d" % num, span.span_profile()))))
Пример #15
0
class Video():
    '''
    Provides a video-to-file and optional video-to-stream (web) functionality,
    with options for dynamically altering the camera settings to account for
    light levels based on a Pimoroni LTR-559 lux sensor (at 0x23), as well as 
    automatic and manual controls for camera lighting using either one or two 
    Pimoroni Matrix 11x7 displays (white LEDs, at 0x75 and 0x77).

    The camera image is annotated with a title and timestamp. The output filename
    is timestamped and written to a './videos' directory in H.264 video format.
    '''
    def __init__(self, config, level):
        super().__init__()
        global video_width, video_height, annotation_title
        self._log = Logger('video', level)
        if config is None:
            raise ValueError("no configuration provided.")
        self._config = config
        _config = self._config['ros'].get('video')
        self._enable_streaming = _config.get('enable_streaming')
        self._port = _config.get('port')
        self._lux_threshold = _config.get('lux_threshold')
        self._counter = itertools.count()
        self._server = None

        # camera configuration
        self._ctrl_lights = _config.get('ctrl_lights')
        self._width = _config.get('width')
        self._height = _config.get('height')
        self._resolution = (self._width, self._height)
        self._framerate = _config.get('framerate')
        self._convert_mp4 = _config.get('convert_mp4')
        self._remove_h264 = _config.get('remove_h264')
        self._quality = _config.get('quality')
        self._annotate = _config.get('annotate')
        self._title = _config.get('title')
        self._basename = _config.get('basename')
        self._dirname = _config.get('dirname')
        # set globals
        video_width = self._width
        video_height = self._height
        annotation_title = self._title
        self._filename = None
        self._thread = None
        self._killer = None

        # scan I2c bus for devices
        _i2c_scanner = I2CScanner(Level.DEBUG)
        if _i2c_scanner.has_address([0x23]):
            self._lux = Lux(Level.INFO)
            self._log.info(
                'LTR-559 lux sensor found: camera adjustment active.')
        else:
            self._lux = None
            self._log.warning(
                'no LTR-559 lux sensor found: camera adjustment inactive.')

        # lighting configuration
        self._port_light = None
        self._stbd_light = None
        if self._ctrl_lights:
            if _i2c_scanner.has_address([0x77]):  # port
                self._port_light = Matrix(Orientation.PORT, Level.INFO)
                self._log.info('port-side camera lighting available.')
            else:
                self._log.warning('no port-side camera lighting available.')
            if _i2c_scanner.has_address([0x75]):  # starboard
                self._stbd_light = Matrix(Orientation.STBD, Level.INFO)
                self._log.info('starboard-side camera lighting available.')
            else:
                self._log.warning(
                    'no starboard-side camera lighting available.')
        else:
            self._log.info('lighting control is disabled.')

        if self._enable_streaming:
            self._log.info('ready: streaming on port {:d}'.format(self._port))
        else:
            self._log.info('ready: save to file only, no streaming.')

    # ..........................................................................
    def set_compass(self, compass):
        global g_compass
        g_compass = compass  # used by annotation
        self._compass = compass

    # ..........................................................................
    @staticmethod
    def get_annotation():
        global annotation_title, g_compass
        _heading = ''  #g_compass.get_heading_message() if g_compass is not None else ''
        return '{} {} {}'.format(
            annotation_title,
            dt.now(tzlocal.get_localzone()).strftime('%Y-%m-%d %H:%M:%S %Z'),
            _heading)

    # ..........................................................................
    def is_night_mode(self):
        if self._lux is None:
            return False
        _value = self._lux.get_value()
        _threshold = self._lux_threshold
        self._log.debug('lux: {:>5.2f} <  threshold: {:>5.2f}?'.format(
            _value, _threshold))
        return _value < _threshold

    # ..........................................................................
    def _get_timestamp(self):
        return dt.utcfromtimestamp(
            dt.utcnow().timestamp()).isoformat().replace(':', '_').replace(
                '-', '_').replace('.', '_')

    # ..........................................................................
    def get_filename(self):
        return self._filename

    # ..........................................................................
    def get_ip_address(self):
        _socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        try:
            _socket.connect(('10.255.255.255', 1))
            _ip = _socket.getsockname()[0]
        except Exception:
            _ip = '127.0.0.1'
        finally:
            _socket.close()
        return _ip

    # ..........................................................................
    def _start(self, output_splitter, f_is_enabled):
        global output  # necessary for access from StreamingHandler, passed as type
        output = output_splitter
        self._output = output_splitter
        self._filename = output_splitter.get_filename()
        if self._enable_streaming:
            self._log.info('starting video with capture to file: {}'.format(
                output_splitter.get_filename()))
        else:
            self._log.info('starting capture to file: {}'.format(
                output_splitter.get_filename()))
        with picamera.PiCamera(resolution=self._resolution,
                               framerate=self._framerate) as camera:
            self._log.info('camera framerate: {}'.format(camera.framerate))
            self._log.info('camera ISO: {}'.format(camera.iso))
            self._log.info('camera mode: {}'.format(camera.exposure_mode))
            self._log.info('camera shutter speed: {}'.format(
                camera.shutter_speed))
            if self._annotate:
                camera.annotate_text_size = 12
                self.set_night_mode(camera, self.is_night_mode())
                #               camera.annotate_text = Video.get_annotation() # initial text
                # start video annotation thread
                self._annot = threading.Thread(
                    target=Video._annotate, args=[self, camera, f_is_enabled])
                self._annot.setDaemon(True)
                self._annot.start()
            if self._quality > 0:
                # values 1 (highest quality) to 40 (lowest quality), with typical values between 20 and 25
                self._log.info('camera quality: {}'.format(self._quality))
                camera.start_recording(output_splitter,
                                       format='mjpeg',
                                       quality=self._quality)
            else:
                camera.start_recording(output_splitter, format='mjpeg')
            # ............
            try:
                if self._enable_streaming:
                    if self._server is None:
                        self._log.info('starting streaming server...')
                        address = ('', self._port)
                        self._server = StreamingServer(address,
                                                       StreamingHandler,
                                                       f_is_enabled)
                        self._killer = lambda: self.close(
                            camera, output_splitter)
                        self._server.serve_forever()
                    else:
                        self._log.info('streaming server already active.')
                else:  # keepalive
                    while f_is_enabled():
                        self._log.debug('keep-alive...')
                        time.sleep(1.0)
                self._log.info(Fore.RED + 'exited video loop.')
            except Exception:
                self._log.error('error streaming video: {}'.format(
                    traceback.format_exc()))
            finally:
                self._log.info(Fore.RED + 'finally.')
#               self.close(camera, output_splitter)
        self._log.info('_start: complete.')

    # ..........................................................................
    def _annotate(self, camera, f_is_enabled):
        '''
            Update the video annotation every second.
        '''
        self._camera = camera
        _count = 0
        while f_is_enabled():
            #           _count = next(self._counter)
            self._camera.annotate_text = Video.get_annotation()
            #           if ( _count % 5 ) == 0: # every five seconds
            self.set_night_mode(camera, self.is_night_mode())
            time.sleep(1.0)

    # ..........................................................................
    def set_night_mode(self, camera, enabled):
        # NOTE: setting 'iso' overrides exposure mode
        _compass_calibrated = False  # True if self._compass and self._compass.is_calibrated() else False
        if enabled:
            self._log.debug('night mode.')
            #           camera.exposure_mode = 'nightpreview'
            '''
                off
                auto: use automatic exposure mode
                night: select setting for night shooting
                nightpreview:
                backlight: select setting for backlit subject
                spotlight:
                sports: select setting for sports (fast shutter etc.)
                snow: select setting optimised for snowy scenery
                beach: select setting optimised for beach
                verylong: select setting for long exposures
                fixedfps: constrain fps to a fixed value
                antishake: antishake mode
                fireworks: select setting optimised for fireworks

                source: https://www.raspberrypi.org/documentation/raspbian/applications/camera.md
            '''
            camera.iso = 800
            camera.led = False
            camera.annotate_foreground = Color.from_string('#ffdada')
            if _compass_calibrated:
                camera.annotate_background = Color.from_string('#440000')
            else:
                camera.annotate_background = Color.from_string('#440088')
            if self._ctrl_lights:
                self.set_lights(True)
        else:
            self._log.debug('day mode.')
            camera.iso = 100
            #           camera.exposure_mode = 'off'
            camera.led = True
            camera.annotate_foreground = Color.from_string('#111111')
            if _compass_calibrated:
                camera.annotate_background = Color.from_string('#ffffff')
            else:
                camera.annotate_background = Color.from_string('#ffff00')
            if self._ctrl_lights:
                self.set_lights(False)

    # ..........................................................................
    def set_lights(self, on):
        if self._port_light:
            if on:
                self._port_light.light()
            else:
                self._port_light.clear()
        if self._stbd_light:
            if on:
                self._stbd_light.light()
            else:
                self._stbd_light.clear()

    # ..........................................................................
    def is_enabled(self):
        return self._enabled

    # ..........................................................................
    @property
    def active(self):
        return self._thread is not None

    # ..........................................................................
    def start(self):
        if self._thread is not None:
            self._log.info('video already started.')
            return
        self._log.info('start.')
        self._enabled = True
        if not os.path.isdir(self._dirname):
            os.makedirs(self._dirname)
        self._filename = os.path.join(
            self._dirname,
            self._basename + '_' + self._get_timestamp() + '.h264')
        _output = OutputSplitter(self._filename)
        self._thread = threading.Thread(target=Video._start,
                                        args=[
                                            self,
                                            _output,
                                            lambda: self.is_enabled(),
                                        ])
        self._thread.setDaemon(True)
        self._thread.start()
        _ip = self.get_ip_address()
        self._log.info(
            Fore.MAGENTA + Style.BRIGHT +
            'video started on:\thttp://{}:{:d}/'.format(_ip, self._port))

    # ..........................................................................
    def stop(self):
        if self._thread is None:
            self._log.info('video already stopped.')
            return
        self._log.info('stopping video capture on file: {}'.format(
            self._filename))
        print(Fore.GREEN + 'setting enabled flag to False.' + Style.RESET_ALL)
        self._enabled = False

        if self._killer is not None:
            print(Fore.GREEN + 'KILLING...' + Style.RESET_ALL)
            self._killer()
        else:
            print(Fore.GREEN + 'NO KILLER.' + Style.RESET_ALL)

        if self._output is not None:
            print(Fore.GREEN + 'flushing and closing...' + Style.RESET_ALL)
            self._output.flush()
            self._output.close()
            self._output = None
            print(Fore.GREEN + 'closed.' + Style.RESET_ALL)
        self._log.info('joining thread...')
        self._thread.join(timeout=1.0)
        self._thread = None
        if self._convert_mp4:
            self._convert_to_mp4()
        self._log.info('stopped.')

    # ..........................................................................
    def _convert_to_mp4(self):
        if os.path.exists(self._filename):
            self._log.info('converting file {} to mp4...'.format(
                self._filename))
            _mp4_filename = Path(self._filename).stem + ".mp4"
            os.system('ffmpeg -loglevel panic -hide_banner -r {:d} -i {:s} -vcodec copy {:s}'.format(\
                    self._framerate, self._filename, _mp4_filename))
            self._log.info('mp4 conversion complete.')
            if self._remove_h264:
                os.remove(self._filename)
                self._log.info('removed h264 video source.')
        else:
            self._log.warning(
                'could not convert to mp4: file {} did not exist.'.format(
                    self._filename))

    # ..........................................................................
    def close(self, camera, output):
        self._log.info('closing video...')
        if self._ctrl_lights:
            self.set_lights(False)
        if camera:
            if camera.recording:
                camera.stop_recording()
                self._log.debug('camera stopped recording.')
            if not camera.closed:
                camera.close()
                self._log.debug('camera closed.')
        if output:
            output.flush()
            self._log.debug('output flushed.')
            output.close()
            self._log.debug('output closed.')
        if self._server is not None:
            self._log.info('shutting down server...')
            self._server.shutdown()
            self._log.info('server shut down.')
        self._log.info(Fore.MAGENTA + Style.BRIGHT + 'video closed.')
    def train(self, input_array, target_array):
        '''
        Trains the NN based on some input_data and
        target_data
        '''
        # Generating the hidden outputs
        inputs = Matrix.from_array(input_array)
        hidden = Matrix.static_mult(self.weights_ih, inputs)
        hidden.add(self.bias_h)

        # Activation function!
        hidden.map(sigmoid)

        # Generating the output's output
        outputs = Matrix.static_mult(self.weights_ho, hidden)
        outputs.add(self.bias_o)
        outputs.map(sigmoid)

        # Convert array to matrix object
        targets = Matrix.from_array(target_array)

        # Calculate error
        # error = targets - outputs

        output_errors = Matrix.subtract(targets, outputs)
        # gradient = outputs * (1 - outputs)

        # Calculate gradient
        gradient = Matrix.static_map(outputs, dsigmoid)
        gradient.mult(output_errors)
        gradient.mult(self.learning_rate)

        # Calculate deltas
        hidden_T = Matrix.transpose(hidden)
        weight_ho_deltas = Matrix.static_mult(gradient, hidden_T)

        # Adjust the weigts by deltas
        self.weights_ho.add(weight_ho_deltas)
        self.bias_o.add(gradient)

        # Calculate the hidden layer errors
        weights_ho_T = Matrix.transpose(self.weights_ho)
        hidden_errors = Matrix.static_mult(weights_ho_T, output_errors)

        # Calculate hidden gradient
        hidden_gradient = Matrix.static_map(hidden, dsigmoid)
        hidden_gradient.mult(hidden_errors)
        hidden_gradient.mult(self.learning_rate)

        # Calculate input to hidden deltas
        inputs_T = Matrix.transpose(inputs)
        weight_ih_deltas = Matrix.static_mult(hidden_gradient, inputs_T)
        self.weights_ih.add(weight_ih_deltas)
        self.bias_h.add(hidden_gradient)
Пример #17
0
	def calculate_errors(self, target_arr):
		if len(target_arr) == self.size:
			self.E = Matrix.from_list(target_arr, self.size, 1) - self.values
		else: raise ValueError("Incorrect target size.")
Пример #18
0
	def initialize(self, previous_layer):
		self.previous_layer = previous_layer
		self.W = Matrix(self.size, previous_layer.size).randomize(-1, 1)
		self.B = Matrix(self.size, 1).randomize(-1, 1)
Пример #19
0
# import matrix.matrix
from lib.matrix import Matrix

import traceback
import logging

if __name__ == "__main__":

    # logging.basicConfig(filename='log.txt',level=logging.DEBUG)
    logging.basicConfig(filename='matrix_waterfall.log', level=logging.INFO)

    matrix = Matrix()

    try:
        matrix.run()
    except Exception as e:
        matrix.finish()
        traceback.print_exc()
    except KeyboardInterrupt:
        matrix.finish()

    # with Matrix() as matrix:
    #     matrix.run()
Пример #20
0
	def set_values(self, input_arr):
		if len(input_arr) == self.size: self.values = Matrix.from_list(input_arr, self.size, 1)
		else: raise ValueError("Incorrect input size. {}".format(' '.join(input_arr)))
Пример #21
0
def task2():
    print("=== TASK 2 ===")
    print("")

    oct_parsed = int(str(task2_input), 8)

    # wheter to interpret 1011 as 1 + x2 + x^3 or 1 + x + x^3
    # else clause: takes parsed number, converts to binary, adds zeroes to the left, reverses, parses as binary
    # (it's the same as reversing number's binary form)
    # e.g. 101001 -> 100101
    modulus = oct_parsed if rtl else int(
        tobin(oct_parsed).zfill(len(str(task2_input)) * 3)[::-1], 2)

    binary_modulus = tobin(modulus)
    mod_length = len(binary_modulus)
    print("modulus = %s" % binary_modulus)
    print("GF(2^%s)" % (mod_length - 1))
    print("")

    field = Field(modulus)
    elements = field.get_all_elems()
    bin_elements = map(lambda it: tobin(it).zfill(mod_length - 1), elements)
    for i, item in enumerate(bin_elements):
        print("%s: %s" % (str(i).zfill(2), item))

    print("")

    code_polynomial = binaryToPolynomial(
        task2_code) if task2_code_is_binary else octToPolynomial(
            task2_code, field)
    print("g(x) = %s" % str(code_polynomial))

    syndrome_len = task2_errors * 2
    syndrome_values_show = [(i, code_polynomial.calc({"x": field.get(i)},
                                                     field))
                            for i in range(1, syndrome_len + 1)]
    for (i, val) in syndrome_values_show:
        if val == 0:
            print("g(a^%d) = 0" % i)
        else:
            p = field.powerOf(val)
            print("g(a^%d) = a^%d" % (i, p))
    syndrome_values = list(
        map(lambda tuple: numToAlpha(tuple[1], field), syndrome_values_show))
    syndrome_values_raw = list(
        map(lambda a: a.calc({}, field), syndrome_values))
    syndrome = Polynomial(syndrome_values)
    print("syndrome(x) = %s" % str(syndrome))
    v = task2_errors + 1
    D = 0
    M = None
    while D == 0:
        v = v - 1
        print(syndrome_values_raw[0:v + 1])
        M = get_m_matrix(syndrome_values_raw[0:v + 1], field)
        print("with M = ")
        print(M.str_field())
        D = M.determinant_and_ref()
        print("v = %d, D = %d" % (v, D))
    M = get_m_matrix(syndrome_values_raw[0:v + 1], field)
    MSolve = M.append_col(syndrome_values_raw[len(syndrome_values_raw) - v:])
    print(MSolve.str_field())
    lambdas = MSolve.solve()
    print(MSolve.str_field())
    lambdas_coef = list(map(lambda it: numToAlpha(it, field), lambdas))
    lambda_poly = Polynomial(lambdas_coef + [1])
    print(lambda_poly)
    roots = lambda_poly.find_roots(field)
    print("roots: %s" %
          list(map(lambda it: str(numToAlpha(it, field)), roots)))
    inverse_roots = list(map(lambda it: field.reciprocal(it), roots))
    print("inverse_roots (locators): %s" %
          list(map(lambda it: str(numToAlpha(it, field)), inverse_roots)))
    y_left = Matrix(len(inverse_roots), len(inverse_roots), field)
    y_left.values = [
        list(map(lambda it: field.power(it, i), inverse_roots))
        for i in range(1,
                       len(inverse_roots) + 1)
    ]
    system = y_left.append_col(syndrome_values_raw[:len(inverse_roots)])
    print("values system: ")
    print(system.str_field())
    y_arr = system.solve()
    print("solved: ")
    print(system.str_field())
Пример #22
0
def test_matrix():

    _port_light = None
    _stbd_light = None

    _log = Logger('matrix-test', Level.INFO)

    try:

        _log.info(Fore.CYAN + 'start matrix test...')

        _i2c_scanner = I2CScanner(Level.DEBUG)
        if _i2c_scanner.has_address([0x77]):  # port
            _port_light = Matrix(Orientation.PORT, Level.INFO)
            _log.info('port-side matrix available.')
        else:
            _log.warning('no port-side matrix available.')
        if _i2c_scanner.has_address([0x75]):  # starboard
            _stbd_light = Matrix(Orientation.STBD, Level.INFO)
            _log.info('starboard-side matrix available.')
        else:
            _log.warning('no starboard-side matrix available.')

        if not _port_light and not _stbd_light:
            _log.warning('skipping test: no matrix displays available.')
            return

        if _port_light:
            _port_light.text('Y', False, False)
        if _stbd_light:
            _stbd_light.text('N', False, False)
        time.sleep(2)

        if _port_light:
            _port_light.disable()
            _port_light.clear()
        if _stbd_light:
            _stbd_light.disable()
            _stbd_light.clear()
        time.sleep(1)

        _log.info('matrix on...')
        if _port_light:
            _port_light.light()
        if _stbd_light:
            _stbd_light.light()
        time.sleep(2)

        if _port_light:
            _port_light.clear()
        if _stbd_light:
            _stbd_light.clear()
        time.sleep(1)

        _log.info('matrix gradient...')
        for x in range(3):
            for i in range(1, 8):
                _log.info('matrix at {:d}'.format(i))
                if _port_light:
                    _port_light.gradient(i)
                if _stbd_light:
                    _stbd_light.gradient(i)
                time.sleep(0.01)
            if _port_light:
                _port_light.clear()
            if _stbd_light:
                _stbd_light.clear()
            time.sleep(0.1)

    except KeyboardInterrupt:
        _log.info(Fore.MAGENTA + 'Ctrl-C caught: interrupted.')
    finally:
        _log.info('closing matrix test...')
        if _port_light:
            _port_light.clear()
        if _stbd_light:
            _stbd_light.clear()
Пример #23
0
    def __init__(self, config, level):
        super().__init__()
        global video_width, video_height, annotation_title
        self._log = Logger('video', level)
        if config is None:
            raise ValueError("no configuration provided.")
        self._config = config
        _config = self._config['ros'].get('video')
        self._enable_streaming = _config.get('enable_streaming')
        self._port = _config.get('port')
        self._lux_threshold = _config.get('lux_threshold')
        self._counter = itertools.count()
        self._server = None

        # camera configuration
        self._ctrl_lights = _config.get('ctrl_lights')
        self._width = _config.get('width')
        self._height = _config.get('height')
        self._resolution = (self._width, self._height)
        self._framerate = _config.get('framerate')
        self._convert_mp4 = _config.get('convert_mp4')
        self._remove_h264 = _config.get('remove_h264')
        self._quality = _config.get('quality')
        self._annotate = _config.get('annotate')
        self._title = _config.get('title')
        self._basename = _config.get('basename')
        self._dirname = _config.get('dirname')
        # set globals
        video_width = self._width
        video_height = self._height
        annotation_title = self._title
        self._filename = None
        self._thread = None
        self._killer = None

        # scan I2c bus for devices
        _i2c_scanner = I2CScanner(Level.DEBUG)
        if _i2c_scanner.has_address([0x23]):
            self._lux = Lux(Level.INFO)
            self._log.info(
                'LTR-559 lux sensor found: camera adjustment active.')
        else:
            self._lux = None
            self._log.warning(
                'no LTR-559 lux sensor found: camera adjustment inactive.')

        # lighting configuration
        self._port_light = None
        self._stbd_light = None
        if self._ctrl_lights:
            if _i2c_scanner.has_address([0x77]):  # port
                self._port_light = Matrix(Orientation.PORT, Level.INFO)
                self._log.info('port-side camera lighting available.')
            else:
                self._log.warning('no port-side camera lighting available.')
            if _i2c_scanner.has_address([0x75]):  # starboard
                self._stbd_light = Matrix(Orientation.STBD, Level.INFO)
                self._log.info('starboard-side camera lighting available.')
            else:
                self._log.warning(
                    'no starboard-side camera lighting available.')
        else:
            self._log.info('lighting control is disabled.')

        if self._enable_streaming:
            self._log.info('ready: streaming on port {:d}'.format(self._port))
        else:
            self._log.info('ready: save to file only, no streaming.')
Пример #24
0
    0 0 0 1 1 0 0 1 1 0
"""

input_len_1 = 9
input_dim_1 = 3
input_len_2 = 9
input_dist_2 = 4
rtl = True
input_polynom = 15
input_a = 3
input_b = 5
input_c = 5
input_d = 3
input_x0 = 3

H = Matrix.fromString(input_matrix_str)
print('input (check matrix aka proverochnaya): ')
print(str(H))

# prints both steps and itself
(sys, ii, jj) = H.systematic_form()

trimmed = sys.rtrim()
print('after right-trimming:')
print(str(trimmed))
trans = trimmed.transpose()
print('after transposing:')
print(str(trans))
G = trans.lextend()
G.swap_cols(ii, jj)
print('after left-extending (generator matrix aka porojdayuschaya):')