Cos'è ElectroYou | Login Iscriviti

ElectroYou - la comunità dei professionisti del mondo elettrico

Problema con rete neurale BProp legge foto

Linguaggi e sistemi

Moderatori: Foto UtentePaolino, Foto Utentefairyvilje

0
voti

[11] Re: Problema con rete neurale BProp legge foto

Messaggioda Foto Utentealien75 » 26 mar 2025, 20:39

Date un'occhiata a queste modifiche, funziona:
Codice: Seleziona tutto
import numpy as np
import cv2
import cv2 as cv
import sys


class NeuralNetwork:
    def __init__(self, input_size, hidden_size, output_size):
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.output_size = output_size

        self.weights_input_hidden = np.random.randn(self.input_size, self.hidden_size)
        self.weights_hidden_output = np.random.randn(self.hidden_size, self.output_size)

        self.bias_hidden = np.zeros((1, self.hidden_size))
        self.bias_output = np.zeros((1, self.output_size))
       
    def sigmoid(self, x):
        return 1 / (1 + np.exp(-x))

    def sigmoid_derivative(self, x):
        return x * (1 - x)

    def feedforward(self, X):
        self.hidden_activation = np.dot(X, self.weights_input_hidden) + self.bias_hidden
        self.hidden_output = self.sigmoid(self.hidden_activation)

        self.output_activation = np.dot(self.hidden_output, self.weights_hidden_output) + self.bias_output
        self.predicted_output = self.sigmoid(self.output_activation)

        return self.predicted_output

    def backward(self, X, y, learning_rate):
        output_error = y - self.predicted_output
        output_delta = output_error * self.sigmoid_derivative(self.predicted_output)

        hidden_error = np.dot(output_delta, self.weights_hidden_output.T)
        hidden_delta = hidden_error * self.sigmoid_derivative(self.hidden_output)

        self.weights_hidden_output += np.dot(self.hidden_output.T, output_delta) * learning_rate
        self.bias_output += np.sum(output_delta, axis=0, keepdims=True) * learning_rate
        self.weights_input_hidden += np.dot(X.T, hidden_delta) * learning_rate
        self.bias_hidden += np.sum(hidden_delta, axis=0, keepdims=True) * learning_rate

    def train(self, X, y, epochs, learning_rate):
        for epoch in range(epochs):
            output = self.feedforward(X)
            self.backward(X, y, learning_rate)
            if epoch % 4000 == 0:
                loss = np.mean(np.square(y - output))
                print(f"Epoch {epoch}, Loss:{loss}")

IMAGE1 = 'day_open.jpg'
IMAGE2 = 'day_closed.jpg'
IMAGE3 = 'night_open.jpg'
IMAGE4 = 'night_closed.jpg'

image1 = cv2.imread(IMAGE1)
image2 = cv2.imread(IMAGE2)
image3 = cv2.imread(IMAGE3)
image4 = cv2.imread(IMAGE4)


gray_image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
res1 = cv.resize(gray_image1,(1800, 1800), interpolation = cv.INTER_CUBIC)
gray_image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
res2 = cv.resize(gray_image2,(1800, 1800), interpolation = cv.INTER_CUBIC)
gray_image3 = cv2.cvtColor(image3, cv2.COLOR_BGR2GRAY)
res3 = cv.resize(gray_image3,(1800, 1800), interpolation = cv.INTER_CUBIC)
gray_image4 = cv2.cvtColor(image4, cv2.COLOR_BGR2GRAY)
res4 = cv.resize(gray_image4,(1800, 1800), interpolation = cv.INTER_CUBIC)
res1 = res1 / 255
res2 = res2 / 255
res3 = res3 / 255
res4 = res4 / 255



X = np.array(res1)
y= np.array([0,0])



nn = NeuralNetwork(input_size=1800, hidden_size=500, output_size=2)
nn.train(X, y, epochs=200, learning_rate=1)
output = nn.feedforward(X)
print(output[1])

X = np.array(res2)
y= np.array([0,1])
nn.train(X, y, epochs=20000, learning_rate=1)
output = nn.feedforward(X)
print(output[1])

X= np.array(res3)
y= np.array([1,0])
nn.train(X, y, epochs=20000, learning_rate=1)
output = nn.feedforward(X)
print(output[1])

X = np.array(res4)
y= np.array([1,1])
nn.train(X, y, epochs=20000, learning_rate=1)
output = nn.feedforward(X)
print(output[1])


risultato:
Codice: Seleziona tutto
Epoch 0, Loss:7.960687921508862e-06
[2.53768053e-13 1.32046885e-12]
Epoch 0, Loss:0.49999999597993483
/home/fabio/Scrivania/backpropagation.py:20: RuntimeWarning: overflow encountered in exp
  return 1 / (1 + np.exp(-x))
Epoch 4000, Loss:1.2217864469508828e-20


ed il risultato non è ancora completo: è ancora fermo all epoca 4000
Avatar utente
Foto Utentealien75
1 1 4 8
Sostenitore
Sostenitore
 
Messaggi: 588
Iscritto il: 31 lug 2011, 14:08

0
voti

[12] Re: Problema con rete neurale BProp legge foto

Messaggioda Foto UtenteGioArca67 » 26 mar 2025, 20:55

Il problema iniziale è che passi 4 immagini contemporaneamente, mentre la funzione di apprendimento se ne aspetta 1:

self.hidden_activation = np.dot(X, self.weights_input_hidden) + self.bias_hidden
ValueError: shapes (4,1,100,100) and (10000,500) not aligned: 100 (dim 3) != 10000 (dim 0).

adesso hai solo 100x100 invece che 10000x1

prova a fare un reshape:
Codice: Seleziona tutto
nn.train(res1.reshape(10000), y, epochs=20000, learning_rate=1)
Avatar utente
Foto UtenteGioArca67
4.570 4 6 9
Master EY
Master EY
 
Messaggi: 4591
Iscritto il: 12 mar 2021, 9:36

0
voti

[13] Re: Problema con rete neurale BProp legge foto

Messaggioda Foto UtenteGioArca67 » 26 mar 2025, 21:00

alien75 ha scritto:Date un'occhiata a queste modifiche, funziona:
Codice: Seleziona tutto
......
gray_image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
res1 = cv.resize(gray_image1,(1800, 1800), interpolation = cv.INTER_CUBIC)
.......
nn = NeuralNetwork(input_size=1800, hidden_size=500, output_size=2)
.......


Così dai in pasto alla MLP una sola riga dell'immagine.

Sei sicuro che l'algoritmo è implementato bene?
Avatar utente
Foto UtenteGioArca67
4.570 4 6 9
Master EY
Master EY
 
Messaggi: 4591
Iscritto il: 12 mar 2021, 9:36

0
voti

[14] Re: Problema con rete neurale BProp legge foto

Messaggioda Foto UtenteGioArca67 » 26 mar 2025, 21:13

Concettualmente quando dichiari la rete neurale con 10000 ingressi ed hai un'immagine 100x100 stai dedicando ciascun ingresso ad uno specifico pixel.
Se invece hai un'immagine 1800x1800 ed un ingresso della rete neurale di dimensione 1800 la rete "guarda" una riga alla volta.

comunque nell'implementazione che hai postato sembra che la back propagation non funzioni:
hidden_delta ha dimensioni 1x500 ma poi lo usi con l'ingresso di dimensione 10000...
Avatar utente
Foto UtenteGioArca67
4.570 4 6 9
Master EY
Master EY
 
Messaggi: 4591
Iscritto il: 12 mar 2021, 9:36

0
voti

[15] Re: Problema con rete neurale BProp legge foto

Messaggioda Foto Utentealien75 » 27 mar 2025, 8:00

GioArca67 ha scritto:Concettualmente quando dichiari la rete neurale con 10000 ingressi ed hai un'immagine 100x100 stai dedicando ciascun ingresso ad uno specifico pixel.
Se invece hai un'immagine 1800x1800 ed un ingresso della rete neurale di dimensione 1800 la rete "guarda" una riga alla volta.

comunque nell'implementazione che hai postato sembra che la back propagation non funzioni:
hidden_delta ha dimensioni 1x500 ma poi lo usi con l'ingresso di dimensione 10000...


Ecco il codice come l'ho modificato:
Codice: Seleziona tutto
import numpy as np
import cv2
import cv2 as cv
import sys


class NeuralNetwork:
    def __init__(self, input_size, hidden_size, output_size):
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.output_size = output_size

        self.weights_input_hidden = np.random.randn(self.input_size, self.hidden_size)
        self.weights_hidden_output = np.random.randn(self.hidden_size, self.output_size)

        self.bias_hidden = np.zeros((1, self.hidden_size))
        self.bias_output = np.zeros((1, self.output_size))
       
    def sigmoid(self, x):
        return 1 / (1 + np.exp(-x))

    def sigmoid_derivative(self, x):
        return x * (1 - x)

    def feedforward(self, X):
        self.hidden_activation = np.dot(X, self.weights_input_hidden) + self.bias_hidden
        self.hidden_output = self.sigmoid(self.hidden_activation)

        self.output_activation = np.dot(self.hidden_output, self.weights_hidden_output) + self.bias_output
        self.predicted_output = self.sigmoid(self.output_activation)

        return self.predicted_output

    def backward(self, X, y, learning_rate):
        output_error = y - self.predicted_output
        output_delta = output_error * self.sigmoid_derivative(self.predicted_output)

        hidden_error = np.dot(output_delta, self.weights_hidden_output.T)
        hidden_delta = hidden_error * self.sigmoid_derivative(self.hidden_output)

        self.weights_hidden_output += np.dot(self.hidden_output.T, output_delta) * learning_rate
        self.bias_output += np.sum(output_delta, axis=0, keepdims=True) * learning_rate
        self.weights_input_hidden += np.dot(X.T, hidden_delta) * learning_rate
        self.bias_hidden += np.sum(hidden_delta, axis=0, keepdims=True) * learning_rate

    def train(self, X, y, epochs, learning_rate):
        for epoch in range(epochs):
            output = self.feedforward(X)
            self.backward(X, y, learning_rate)
            if epoch % 4000 == 0:
                loss = np.mean(np.square(y - output))
                print(f"Epoch {epoch}, Loss:{loss}")

IMAGE1 = 'day_open.jpg'
IMAGE2 = 'day_closed.jpg'
IMAGE3 = 'night_open.jpg'
IMAGE4 = 'night_closed.jpg'

image1 = cv2.imread(IMAGE1)
image2 = cv2.imread(IMAGE2)
image3 = cv2.imread(IMAGE3)
image4 = cv2.imread(IMAGE4)


gray_image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
res1 = cv.resize(gray_image1,(100, 100), interpolation = cv.INTER_CUBIC)
#gray_image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
#res2 = cv.resize(gray_image2,(100, 100), interpolation = cv.INTER_CUBIC)
#gray_image3 = cv2.cvtColor(image3, cv2.COLOR_BGR2GRAY)
#res3 = cv.resize(gray_image3,(100, 100), interpolation = cv.INTER_CUBIC)
#gray_image4 = cv2.cvtColor(image4, cv2.COLOR_BGR2GRAY)
#res4 = cv.resize(gray_image4,(100, 100), interpolation = cv.INTER_CUBIC)
res1 = res1 / 255
#res2 = res2 / 255
#res3 = res3 / 255
#res4 = res4 / 255



X = np.array(res1)
y= np.array([0,0])



nn = NeuralNetwork(input_size=10000, hidden_size=1000, output_size=2)
nn.train(res1.reshape(10000), y, epochs=20000, learning_rate=1)
output = nn.feedforward(X)
print(output)


ed ecco il risultato:
Codice: Seleziona tutto
Traceback (most recent call last):
  File "/home/fabio/Scrivania/backpropagation.py", line 86, in <module>
    nn.train(res1.reshape(10000), y, epochs=20000, learning_rate=1)
  File "/home/fabio/Scrivania/backpropagation.py", line 49, in train
    self.backward(X, y, learning_rate)
  File "/home/fabio/Scrivania/backpropagation.py", line 43, in backward
    self.weights_input_hidden += np.dot(X.T, hidden_delta) * learning_rate
ValueError: shapes (10000,) and (1,500) not aligned: 10000 (dim 0) != 1 (d


Che te ne pare? Mi da errore di nuovo.
Avatar utente
Foto Utentealien75
1 1 4 8
Sostenitore
Sostenitore
 
Messaggi: 588
Iscritto il: 31 lug 2011, 14:08

0
voti

[16] Re: Problema con rete neurale BProp legge foto

Messaggioda Foto UtenteGioArca67 » 27 mar 2025, 18:19

Vedendo le dimensioni che assegna sembra che voglia un vettore colonna:
Codice: Seleziona tutto
nn = NeuralNetwork(input_size=10000, hidden_size=1000, output_size=2)
nn.train(res1.reshape(1,10000), y, epochs=4000, learning_rate=1)
output=nn.feedforward(res1.reshape(1,10000))
Avatar utente
Foto UtenteGioArca67
4.570 4 6 9
Master EY
Master EY
 
Messaggi: 4591
Iscritto il: 12 mar 2021, 9:36

0
voti

[17] Re: Problema con rete neurale BProp legge foto

Messaggioda Foto Utentealien75 » 27 mar 2025, 19:01

Ho modificato come mi hai detto e computa ecco il codice:
Codice: Seleziona tutto
import numpy as np
import cv2
import cv2 as cv
import sys


class NeuralNetwork:
    def __init__(self, input_size, hidden_size, output_size):
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.output_size = output_size

        self.weights_input_hidden = np.random.randn(self.input_size, self.hidden_size)
        self.weights_hidden_output = np.random.randn(self.hidden_size, self.output_size)

        self.bias_hidden = np.zeros((1, self.hidden_size))
        self.bias_output = np.zeros((1, self.output_size))
       
    def sigmoid(self, x):
        return 1 / (1 + np.exp(-x))

    def sigmoid_derivative(self, x):
        return x * (1 - x)

    def feedforward(self, X):
        self.hidden_activation = np.dot(X, self.weights_input_hidden) + self.bias_hidden
        self.hidden_output = self.sigmoid(self.hidden_activation)

        self.output_activation = np.dot(self.hidden_output, self.weights_hidden_output) + self.bias_output
        self.predicted_output = self.sigmoid(self.output_activation)

        return self.predicted_output

    def backward(self, X, y, learning_rate):
        output_error = y - self.predicted_output
        output_delta = output_error * self.sigmoid_derivative(self.predicted_output)

        hidden_error = np.dot(output_delta, self.weights_hidden_output.T)
        hidden_delta = hidden_error * self.sigmoid_derivative(self.hidden_output)

        self.weights_hidden_output += np.dot(self.hidden_output.T, output_delta) * learning_rate
        self.bias_output += np.sum(output_delta, axis=0, keepdims=True) * learning_rate
        self.weights_input_hidden += np.dot(X.T, hidden_delta) * learning_rate
        self.bias_hidden += np.sum(hidden_delta, axis=0, keepdims=True) * learning_rate

    def train(self, X, y, epochs, learning_rate):
        for epoch in range(epochs):
            output = self.feedforward(X)
            self.backward(X, y, learning_rate)
            if epoch % 4000 == 0:
                loss = np.mean(np.square(y - output))
                print(f"Epoch {epoch}, Loss:{loss}")

IMAGE1 = 'day_open.jpg'
IMAGE2 = 'day_closed.jpg'
IMAGE3 = 'night_open.jpg'
IMAGE4 = 'night_closed.jpg'

image1 = cv2.imread(IMAGE1)
image2 = cv2.imread(IMAGE2)
image3 = cv2.imread(IMAGE3)
image4 = cv2.imread(IMAGE4)


gray_image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
res1 = cv.resize(gray_image1,(100, 100), interpolation = cv.INTER_CUBIC)
gray_image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
res2 = cv.resize(gray_image2,(100, 100), interpolation = cv.INTER_CUBIC)
gray_image3 = cv2.cvtColor(image3, cv2.COLOR_BGR2GRAY)
res3 = cv.resize(gray_image3,(100, 100), interpolation = cv.INTER_CUBIC)
#gray_image4 = cv2.cvtColor(image4, cv2.COLOR_BGR2GRAY)
#res4 = cv.resize(gray_image4,(100, 100), interpolation = cv.INTER_CUBIC)
res1 = res1 / 255
res2 = res2 / 255
res3 = res3 / 255
#res4 = res4 / 255



X = np.array(res1)
y= np.array([0,0])



nn = NeuralNetwork(input_size=10000, hidden_size=500, output_size=2)
nn.train(res1.reshape(1,10000), y, epochs=4000, learning_rate=1)
output=nn.feedforward(res1.reshape(1,10000))
print(output)

X = np.array(res2)
y= np.array([0,1])
nn.train(res2.reshape(1,10000), y, epochs=4000, learning_rate=1)
output = nn.feedforward(res2.reshape(1,10000))
print(output)

X= np.array(res3)
y= np.array([1,0])
nn.train(res3.reshape(1,10000), y, epochs=4000, learning_rate=1)
output = nn.feedforward(res3.reshape(1,10000))
print(output)

#X = np.array(res4)
#y= np.array([1,1])
#nn.train(X, y, epochs=20000, learning_rate=1)
#output = nn.feedforward(X)
#print(output)


risultato:
Codice: Seleziona tutto
Epoch 0, Loss:0.9808743075861155
[[4.38682782e-09 1.00000000e+00]]
/home/fabio/Scrivania/backpropagation.py:20: RuntimeWarning: overflow encountered in exp
  return 1 / (1 + np.exp(-x))
Epoch 0, Loss:0.3547766246312235
[[5.10706907e-13 1.00000000e+00]]
Epoch 0, Loss:0.9999999386093856
[[6.53725073e-08 1.00000000e+00]]


mi da sempre output = [0,1] con tre immagini apprese, perché?
Forse colpa dello strato hidden troppo piccolo con poche unità?
Quante unità hidden dovrei mettere per avere un apprendimento corretto?
Avatar utente
Foto Utentealien75
1 1 4 8
Sostenitore
Sostenitore
 
Messaggi: 588
Iscritto il: 31 lug 2011, 14:08

0
voti

[18] Re: Problema con rete neurale BProp legge foto

Messaggioda Foto UtenteGioArca67 » 27 mar 2025, 19:11

La rete neurale funziona, ho provato con un set di dati semplice ed ha appreso un XOR.
La usiamo male noi
Avatar utente
Foto UtenteGioArca67
4.570 4 6 9
Master EY
Master EY
 
Messaggi: 4591
Iscritto il: 12 mar 2021, 9:36

0
voti

[19] Re: Problema con rete neurale BProp legge foto

Messaggioda Foto Utentealien75 » 27 mar 2025, 19:21

GioArca67 ha scritto:La rete neurale funziona, ho provato con un set di dati semplice ed ha appreso un XOR.
La usiamo male noi


Allora non capisco a sto punto che sta succedendo io le ho detto di associare in fase di apprendimento res1 a [0,0] , res2 a [0,1], res3 a [1,0] e res4 a [1,1], ma in fase di esecuzione nn mi da tali risultati anzi roba del tutto sballata con print(output).
Non capisco dove sbaglio.
Le unintà hidden le ho messe a 500; vanno bene tale numero?
Avatar utente
Foto Utentealien75
1 1 4 8
Sostenitore
Sostenitore
 
Messaggi: 588
Iscritto il: 31 lug 2011, 14:08

0
voti

[20] Re: Problema con rete neurale BProp legge foto

Messaggioda Foto UtenteGioArca67 » 27 mar 2025, 19:31

No.
Impostiamo male X e y.
prova così:
Codice: Seleziona tutto
X=np.array([gray_image1.reshape(10000),gray_image2.reshape(10000),gray_image3.reshape(10000),gray_image4.reshape(10000)])
y=np.array([[0,0],[0,1],[1,0],[1,1]])

print(X.shape)

nn = NeuralNetwork(input_size=10000, hidden_size=100, output_size=2)
print(f'training')
nn.train(X, y, epochs=700, learning_rate=0.8)

print("Predictions after training:")
print(f'  image1 {nn.feedforward(gray_image1.reshape(1,10000))}')
print(f'  image2 {nn.feedforward(gray_image2.reshape(1,10000))}')
print(f'  image3 {nn.feedforward(gray_image3.reshape(1,10000))}')
print(f'  image4 {nn.feedforward(gray_image4.reshape(1,10000))}')
print(f'  image5 {nn.feedforward(gray_image5.reshape(1,10000))}')



prima prepara una quinta immagine che non dai in pasto all'MLP
Avatar utente
Foto UtenteGioArca67
4.570 4 6 9
Master EY
Master EY
 
Messaggi: 4591
Iscritto il: 12 mar 2021, 9:36

PrecedenteProssimo

Torna a PC e informatica

Chi c’è in linea

Visitano il forum: Nessuno e 51 ospiti