Having problems working out how to get a confusion matrix when using TFLearn for the creation of a convolutional neural network. The code I have so far is as follows:
from __future__ import division, print_function, absolute_import
import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression
from sklearn.metrics import confusion_matrix
import h5py
hdf5Test = h5py.File('/path', 'r')
X = hdf5Test['X']
Y = hdf5Test['Y']
# Building convolutional network
network = input_data(shape=[None, 240, 320, 3], name='input')
network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = fully_connected(network, 128, activation='tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 256, activation='tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 2, activation='softmax')
network = regression(
network,
optimizer='sgd',
learning_rate=0.01,
loss='categorical_crossentropy',
name='target'
)
# Training
model = tflearn.DNN(network, tensorboard_verbose=0)
model.load('/path.tflearn')
predictions = model.predict(X)
print(confusion_matrix(Y, predictions))
Everytime I try to run this code I am given the following error message:
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted (core dumped)
Any advice would be great, new to TFLearn.
In the end, it found that it was due to the size of the data I was trying to predict. I fixed this by inserting it within a loop:
# Predict Classes
predictions = []
count = 0
length = len(X)
for line in X:
print('Line ' + str(count) + ' of ' + str(length))
tmp = model.predict_label([line])
predictions.append(tmp[0])
count += 1
With some formatting I was then able to use Sklearn to produce a confusion matrix:
predictedClasses = np.argmin(predictions, axis=1)
actualClasses = np.argmax(Y, axis=1)
print(confusion_matrix(actualClasses, predictedClasses))
This approach worked for me and may work for you... I think TFLearn should look into a streamlined approach for the production of confusion matrix so other don't have the same problem.