示例#1
0
xsc.fit(X)

ylb = LabelBinarizer()
ylb.fit(y)


# split the dataset
X_train, X_test, y_train, y_test = train_test_split(xsc.transform(X), y, random_state=0)


# load nn if exists else train
nn_fname = 'models/nn_iris_3000epochs.pickle'
if os.path.exists(nn_fname):
    # load
    print('loading the nn')
    nn = deserialize(nn_fname)
else:
    # train
    print('training the nn')
    nn = ANN([4, 10, 3])

    nn.train(X_train, ylb.transform(y_train), 3000)

    serialize(nn, nn_fname)

# predict
preds = np.array([nn.predict(example) for example in X_test])
y_pred = ylb.inverse_transform(preds)

# evaluate
print(confusion_matrix(y_test, y_pred))
# load the image
digit = cv2.imread("./data/mnist/3.png", cv2.IMREAD_GRAYSCALE)
#digit = cv2.imread("./data/mnist/7.png", cv2.IMREAD_GRAYSCALE)
cv2.imshow("original digit", digit)

# resize it, it needs to be 28 x 28 for our MNIST ANN
small = cv2.resize(digit, (28, 28), interpolation=cv2.INTER_NEAREST)
cv2.imshow("smaller digit", small)

# scale the values to 0..1
small -= small.min()
small /= small.max()

# from a matrix of 28x28 make it a vector of 768x1
print small.shape
small = small.reshape((784, 1))
print small.shape

# load the ANN
nn = ann_util.deserialize('models/nn_mnist_iter800000.pickle')

# predict output vector
prediction = nn.predict(small)
print prediction

# the correct label is the index at which we have the largest values
label = np.argmax(prediction)
print "The predicted label is:", label

# blocking wait (press any key)
k = cv2.waitKey(0)
# load the image
digit = cv2.imread("./data/mnist/3.png", cv2.IMREAD_GRAYSCALE)
#digit = cv2.imread("./data/mnist/7.png", cv2.IMREAD_GRAYSCALE)
cv2.imshow("original digit", digit)

# resize it, it needs to be 28 x 28 for our MNIST ANN
small = cv2.resize(digit, (28, 28), interpolation=cv2.INTER_NEAREST)
cv2.imshow("smaller digit", small)

# scale the values to 0..1
small -= small.min()
small /= small.max()

# from a matrix of 28x28 make it a vector of 768x1
print small.shape
small = small.reshape((784, 1))
print small.shape

# load the ANN
nn = ann_util.deserialize('models/nn_mnist_iter800000.pickle')

# predict output vector 
prediction = nn.predict(small)
print prediction

# the correct label is the index at which we have the largest values
label = np.argmax(prediction)
print "The predicted label is:",  label

# blocking wait (press any key)
k = cv2.waitKey(0)
示例#4
0
xsc.fit(X)

ylb = LabelBinarizer()
ylb.fit(y)

# split the dataset
X_train, X_test, y_train, y_test = train_test_split(xsc.transform(X),
                                                    y,
                                                    random_state=0)

# load nn if exists else train
nn_fname = 'models/nn_iris_3000epochs.pickle'
if os.path.exists(nn_fname):
    # load
    print('loading the nn')
    nn = deserialize(nn_fname)
else:
    # train
    print('training the nn')
    nn = ANN([4, 10, 3])

    nn.train(X_train, ylb.transform(y_train), 3000)

    serialize(nn, nn_fname)

# predict
preds = np.array([nn.predict(example) for example in X_test])
y_pred = ylb.inverse_transform(preds)

# evaluate
print(confusion_matrix(y_test, y_pred))