da
alien75 » 28 mar 2025, 19:00
Sto facendo prove con immagini mai "viste in fase di apprendimento":
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_closed.jpg'
IMAGE4 = 'night_open.jpg'
IMAGE5 = 'day_closed.jpg'
image1 = cv2.imread(IMAGE1)
image2 = cv2.imread(IMAGE2)
image3 = cv2.imread(IMAGE3)
image4 = cv2.imread(IMAGE4)
image4 = cv2.imread(IMAGE5)
gray_image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
gray_image1 = cv2.resize(gray_image1,(100, 100), interpolation = cv2.INTER_CUBIC)
gray_image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
gray_image2 = cv2.resize(gray_image2,(100, 100), interpolation = cv2.INTER_CUBIC)
gray_image3 = cv2.cvtColor(image3, cv2.COLOR_BGR2GRAY)
gray_image3 = cv2.resize(gray_image3,(100, 100), interpolation = cv2.INTER_CUBIC)
gray_image4 = cv2.cvtColor(image4, cv2.COLOR_BGR2GRAY)
gray_image4 = cv2.resize(gray_image4,(100, 100), interpolation = cv2.INTER_CUBIC)
gray_image5 = cv2.cvtColor(image4, cv2.COLOR_BGR2GRAY)
gray_image5 = cv2.resize(gray_image4,(100, 100), interpolation = cv2.INTER_CUBIC)
gray_image1 = gray_image1 / 255
gray_image2 = gray_image2 / 255
gray_image3 = gray_image3 / 255
gray_image4 = gray_image4 / 255
gray_image5 = gray_image5 / 255
X=np.array([gray_image1.reshape(10000),gray_image2.reshape(10000),gray_image3.reshape(10000),gray_image4.reshape(10000)])
y=np.array([[0,1],[0,0],[0,0],[0,1]])
print("output = ", y)
print(X.shape)
nn = NeuralNetwork(input_size=10000, hidden_size=300, 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))}')
risultati:
- Codice: Seleziona tutto
utput = [[0 1]
[0 0]
[0 0]
[0 1]]
(4, 10000)
training
Epoch 0, Loss:0.6077297865646656
Predictions after training:
image1 [[2.19096642e-10 1.00000000e+00]]
image2 [[4.47855458e-07 9.99934227e-01]]
image3 [[5.11130835e-06 1.36975050e-04]]
image4 [[4.47855458e-07 9.99934227e-01]]
image5 [[4.47855458e-07 9.99934227e-01]]
Noto un'incongruenza massiccia tra i valori di output in fase di trainig ed i valori di output in fase di esecuzione, perché questo ?