data_gen.py 1.49 KB
Newer Older
Artem Oppermann's avatar
Artem Oppermann committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
import numpy as np
import random

csv_path='./data.txt'

class Rectangle:
    
    def __init__(self, x, y, width, height):
        
        self.x=x
        self.y=y
        self.width=width
        self.height=height
        self.matrix_size=10
        self.matrix=self._init_zeros_matrix()
        self.fill()
        
    def _init_zeros_matrix(self):
       return np.zeros(shape=(self.matrix_size, self.matrix_size), dtype=np.float32)
     
    def add_noise(self):
        
        new_matrix=np.zeros(shape=(self.matrix_size, self.matrix_size), dtype=np.float32)
        
        for i in range(10):
            for j in range(10):
                new_matrix[i][j]+=self.matrix[i][j]+np.random.poisson(lam=2.0)
        return new_matrix
        
    def fill(self):
        
        for i in range(self.width):
            for j in range(self.height):
                self.matrix[self.x+i][self.y+j]=10.0
                           
    def get_matrix(self):
        return self.matrix
     
Artem Oppermann's avatar
Artem Oppermann committed
39
def gen_data(n_samples):
Artem Oppermann's avatar
Artem Oppermann committed
40 41 42 43 44 45 46
    
    rectangles={0:Rectangle(0,0,2,2),
                1:Rectangle(3,3,2,2),
                2:Rectangle(5,5,2,2),
                3:Rectangle(7,7,2,2),}
    data=[]
    
Artem Oppermann's avatar
Artem Oppermann committed
47
    for i in range(0,n_samples):
Artem Oppermann's avatar
Artem Oppermann committed
48 49 50 51 52 53 54 55 56 57 58 59
        label=i%4
        rect=np.array(rectangles[label].add_noise())
        rect_reshaped=np.reshape(rect,[1,100])
        
        data_sample=[]
        data_sample.append(rect_reshaped)
        data_sample.append(label)
        data.append(data_sample)
        
    return data