Esempio n. 1
0
# ==========================
# VISUALIZE IMAGE
# ==========================
# pl.gray()
# pl.matshow(img_arr_gray)
# pl.show()

# ==========================
# RETRIEVE AND SETTING WEIGHTS FROM FILE
# ==========================
Theta1_filename = 'Theta1' + postfix_filename
Theta2_filename = 'Theta2' + postfix_filename

# Read the array from disk
Theta1_raw = np.loadtxt(Theta1_filename)
Theta2_raw = np.loadtxt(Theta2_filename)

# re-set the weights from file
Theta1 = np.matrix(Theta1_raw.reshape((hidden_units, (_input_units + 1))))
Theta2 = np.matrix(Theta2_raw.reshape((_output_units, (hidden_units + 1))))

# ==========================
# TESTING PHASE (UNKNOWN IMAGES)
# ==========================
print("Testing phase (...)")
h = nnf.forward_propagate(input_img, Theta1, Theta2, output_only=True)
# Returns the indices of the maximum values along an axis and creates an array
y_testing_predicted = np.array(np.argmax(h, axis=1))
y_predicted_testing_norm = np.squeeze(np.asarray(y_testing_predicted))
print("Prediction: " + str(y_predicted_testing_norm))
                })
# (fmin.x) are the Theta values which minimize the function

Theta1 = np.matrix(
    np.reshape(fmin.x[:hidden_units * (_input_units + 1)],
               (hidden_units, (_input_units + 1))))

Theta2 = np.matrix(
    np.reshape(fmin.x[hidden_units * (_input_units + 1):],
               (_output_units, (hidden_units + 1))))

# ==========================
# TRAINING ACCURACY
# ==========================
print("Calculating the training accuracy (...)")
h = nnf.forward_propagate(X, Theta1, Theta2, output_only=True)

# Returns the indices of the maximum values along an axis and creates an array
y_training_predicted = np.array(np.argmax(h, axis=1))

y_predicted_training_norm = np.squeeze(np.asarray(y_training_predicted))
y_training_norm = np.squeeze(np.asarray(training_outputs))

training_accuracy = nnf.get_accuracy(y_predicted_training_norm,
                                     y_training_norm)
print("Accuracy: " + str(training_accuracy) + "%")

# ==========================
# TESTING PHASE (UNKNOWN IMAGES)
# ==========================
print("Testing phase (...)")