Exemple #1
0
def main():
    # Load data
    camera = skimage.data.camera()
    astronaut = rgb2gray(skimage.data.astronaut())
    horse = skimage.data.horse()
    coffee = rgb2gray(skimage.data.coffee())

    # Marge data
    data = [camera, astronaut, horse, coffee]

    # Preprocessing
    print("Start to data preprocessing...")
    data = [preprocessing(d) for d in data]

    # Create Hopfield Network Model
    model = network.HopfieldNetwork()
    model.train_weights(data)

    # Generate testset
    test = [get_corrupted_input(d, 0.3) for d in data]

    predicted = model.predict(test, threshold=0, asyn=False)
    print("Show prediction results...")
    plot(data, test, predicted)
    print("Show network weights matrix...")
    print("Number of Epochs %i: ", self.count)
def main():
    # Load data
    pateta = io.imread('pateta.png')
    renato = io.imread('renato.png')
    lenna = io.imread('lenna.png')

    # Marge data
    data = [pateta, renato, lenna]

    # Preprocessing
    print("Start to data preprocessing...")
    data = [preprocessing(d) for d in data]

    # Create Hopfield Network Model
    model = network.HopfieldNetwork()
    model.train_weights(data)

    # Generate testset
    test = [get_corrupted_input(d, 0.15) for d in data]

    predicted = model.predict(test, threshold=0, asyn=True)
    print("Show prediction results...")
    plot(data, test, predicted)
    print("Show network weights matrix...")
    model.plot_weights()
Exemple #3
0
def main():
    # Load data

    import cv2
    import glob
    img_dir = "train_custom/"  # Enter Directory of all images
    data_path = os.path.join(img_dir, '*g')
    files = glob.glob(data_path)
    data = []
    for f1 in files:
        img = rgb2gray(cv2.imread(f1))
        data.append(img)

    # Preprocessing
    print("Start to data preprocessing...")
    data = [preprocessing(d) for d in data]

    # Create Hopfield Network Model
    model = network.HopfieldNetwork()
    model.train_weights(data)

    # Generate testset
    img_dir = "test_custom/"  # Enter Directory of all images
    data_path = os.path.join(img_dir, '*g')
    files = glob.glob(data_path)
    test = []
    # Since same name order will be the same
    for f1 in files:
        img = rgb2gray(cv2.imread(f1))
        test.append(img)
    test = [preprocessing(d) for d in test]

    predicted = model.predict(test, threshold=0, asyn=False)
    print("Show prediction results...")
    plot(data, test, predicted)
Exemple #4
0
def main():
    # Load data
    (x_train, y_train), (_, _) = mnist.load_data()
    data = []
    for i in range(3):
        xi = x_train[y_train == i]
        data.append(xi[0])

    # Preprocessing
    print("Start to data preprocessing...")
    data = [preprocessing(d) for d in data]

    # Create Hopfield Network Model
    model = network.HopfieldNetwork()
    model.train_weights(data)

    # Make test datalist
    test = []
    for i in range(3):
        xi = x_train[y_train == i]
        test.append(xi[1])
    test = [preprocessing(d) for d in test]

    predicted = model.predict(test, threshold=50, asyn=True)
    print("Show prediction results...")
    plot(data, test, predicted, figsize=(5, 5))
    print("Show network weights matrix...")
    model.plot_weights()
def main():

    #Debug
    np.set_printoptions(threshold=np.inf)

    print("[LOG] Creando set de caracteres - ", datetime.datetime.now())

    train_images = init_train_images()
    test_images = init_test_images()

    print("=>train_images[0]")
    print(train_images[0])
    print("=>test_images[0][0]:")
    print(test_images[0][0])

    print("[LOG] Training -        ", datetime.datetime.now())

    # No of neurons
    n_neurons = get_pattern_length()
    print("n neurons:")
    print(n_neurons)
    #W = train(n_neurons, train_images)
    # Create Hopfield Network Model
    model = network.HopfieldNetwork()
    model.train_weights(train_images)

    predicted = model.predict(test_images, threshold=0, asyn=False)
    print("Show prediction results...")
    plot(train_images, test_images, predicted)
    print("Show network weights matrix...")
    model.plot_weights()

    exit(0)
Exemple #6
0
def main():
    # Load data
    camera = skimage.data.camera()
    astronaut = rgb2gray(skimage.data.astronaut())
    horse = skimage.data.horse()
    coffee = rgb2gray(skimage.data.coffee())
    print(coffee.shape)
    man = rgb2gray(mpi.imread("yosuke.jpg"))
    print(man.shape)

    # Marge data
    data = [camera, astronaut, horse, coffee, man]
    # Preprocessing
    print("Start to data preprocessing...")
    data = [preprocessing(d) for d in data]

    # Create Hopfield Network Model
    model = network.HopfieldNetwork()
    model.train_weights(data)

    # Generate testset
    test = [get_corrupted_input(d, 0.3) for d in data]

    predicted = model.predict(test, threshold=0, asyn=False)
    print("Show prediction results...")
    plot(data, test, predicted)
    print("Show network weights matrix...")
    model.plot_weights()
Exemple #7
0
def main():
    # Load data
    shannon = io.imread('shannon.png')
    renato = io.imread('renato.png')
    boltzmann = io.imread('boltzmann.png')

    # Marge data
    data = [shannon, renato, boltzmann]

    # Preprocessing
    print("Start to data preprocessing...")
    data = [preprocessing(d) for d in data]

    # Create Hopfield Network Model
    model = network.HopfieldNetwork()
    model.train_weights(data)

    model.shuffle()

    # Generate testset
    test = [get_corrupted_input(d, 0.30) for d in data]

    predicted = model.predict(test, threshold=0, asyn=True)
    print("Show prediction results...")
    plot(data, test, predicted)
    print("Show network weights matrix...")
    model.plot_weights()
Exemple #8
0
        if toString(col): #print("not mpty col:", col)
            gooddata.append(col)

def toMinusOne(goodDataSample):
    tempstr = toString(goodDataSample)
    tmp = toBinary(tempstr)
    tmp = tmp.replace(" ", "")  #max len 408
    if len(tmp)>599: print("WARNING")
    tmp = "".join("0" for i in range(600-len(tmp)))+tmp
    bi = list(tmp)
    for j in range(len(bi)):
        if bi[j]=="0": bi[j]=-1
        if bi[j]=="1": bi[j]=1
    return bi

model = network.HopfieldNetwork()

gooddata=[gooddata[0], gooddata[1], gooddata[2], gooddata[3]]
print(gooddata[0])
print(gooddata[1])
print(gooddata[2])
print(gooddata[3])

bingooddata = [toMinusOne(d) for d in gooddata]
cmon = numpy.array(bingooddata)

#print("----")
#print(fromBin(cmon[3]))
#print("----")

model.train_weights(cmon)
def main():
    # Load data
    animals = dataload.load('data\\animals-14x9.csv')  # h = 9, w = 14
    large25 = dataload.load('data\\large-25x25.csv')  # h = 25, w = 25
    large25plus = dataload.load('data\\large-25x25.plus.csv')  # h = 25, w = 25
    large50 = dataload.load('data\\large-25x50.csv')  # h = 50, w = 25
    letters = dataload.load('data\\letters-14x20.csv')  # h = 20, w = 14
    lettersabc = dataload.load('data\\letters-abc-8x12.csv')  # h = 12, w = 8
    ocra = dataload.load('data\\OCRA-12x30-cut.csv')  # h = 30, w = 12
    small = dataload.load('data\\small-7x7.csv')  # h = 7, w = 7

    cats = dataload.load('data\\cats')  # h = 60, w = 60
    cats = np.rint(cats + 0.2) * 2 - 1

    custom_data = np.array([
        [
            1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1,
            -1, 1, 1, 1, 1, 1
        ],
        [
            1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1,
            1, 1, -1, 1, -1, 1
        ],
        [
            1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1,
            1, 1, 1, 1
        ],
        [
            -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, 1, -1,
            -1, -1, 1, -1, -1
        ],
        [
            1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1,
            -1, 1, -1, -1, -1, 1
        ],
        [
            -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, -1, 1,
            -1, 1, 1, 1, -1
        ],
        [
            -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1,
            -1, -1, -1, -1, 1, 1
        ],
        [
            1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1,
            1, 1, 1, 1, -1, -1, -1
        ],
        [
            -i for i in [
                1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1,
                -1, 1, 1, -1, 1, -1, 1
            ]
        ],
        [
            -i for i in [
                -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, 1,
                -1, -1, -1, 1, -1, -1
            ]
        ],
        [
            1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1,
            -1, 1, -1, 1, -1, 1
        ],  # szachownica
        [
            -i for i in [
                1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1,
                1, -1, 1, -1, 1, -1, 1
            ]
        ]  # szachownica
    ])

    # Marge data
    data = custom_data
    height = 5
    width = 5
    # Create Hopfield Network Model
    model = network.HopfieldNetwork()
    model.train_weights(data, 'Hebb')

    # Generate testset
    test = [get_corrupted_input(d, 0.) for d in data]  # 0.1

    predicted = model.predict(test, threshold=0, asyn=True)
    print("Show prediction results...")
    plot(data, test, predicted, height, width)
    print("Show network weights matrix...")
    # model.plot_weights()
    print(check_stability(data, predicted, 0.1))