how to interpret predictions from model? The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Moderator Election Q&A - Questionnaire 2019 Community Moderator Election ResultsRecurrent (CNN) model on EEG dataPossible Reason for low Test accuracy and high AUCReinforcement Learning different patientsWhy does my loss value start at approximately -10,000 and my accuracy not improve?Interpreting confusion matrix and validation results in convolutional networksMy Keras bidirectional LSTM model is giving terrible predictionsClassifying Car Data By YearKeras multi-label time-series classification considering time-series as an input image vectorTraining multi-label classifier with unbalanced samples in KerasIs “adding the predictions to the real data for new training and prediction” a good idea for LSTM?

Was credit for the black hole image misattributed?

Finding the path in a graph from A to B then back to A with a minimum of shared edges

Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?

Didn't get enough time to take a Coding Test - what to do now?

Python - Fishing Simulator

How does ice melt when immersed in water

Did the new image of black hole confirm the general theory of relativity?

Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?

Are my PIs rude or am I just being too sensitive?

What was the last x86 CPU that did not have the x87 floating-point unit built in?

How many people can fit inside Mordenkainen's Magnificent Mansion?

Does Parliament hold absolute power in the UK?

The following signatures were invalid: EXPKEYSIG 1397BC53640DB551

How to test the equality of two Pearson correlation coefficients computed from the same sample?

how can a perfect fourth interval be considered either consonant or dissonant?

Mortgage adviser recommends a longer term than necessary combined with overpayments

What can I do if neighbor is blocking my solar panels intentionally?

How do you keep chess fun when your opponent constantly beats you?

What aspect of planet Earth must be changed to prevent the industrial revolution?

Single author papers against my advisor's will?

Arduino Pro Micro - switch off LEDs

What do you call a plan that's an alternative plan in case your initial plan fails?

Why not take a picture of a closer black hole?

First use of “packing” as in carrying a gun



how to interpret predictions from model?



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Moderator Election Q&A - Questionnaire
2019 Community Moderator Election ResultsRecurrent (CNN) model on EEG dataPossible Reason for low Test accuracy and high AUCReinforcement Learning different patientsWhy does my loss value start at approximately -10,000 and my accuracy not improve?Interpreting confusion matrix and validation results in convolutional networksMy Keras bidirectional LSTM model is giving terrible predictionsClassifying Car Data By YearKeras multi-label time-series classification considering time-series as an input image vectorTraining multi-label classifier with unbalanced samples in KerasIs “adding the predictions to the real data for new training and prediction” a good idea for LSTM?










5












$begingroup$


I'm working on a multi-classification problem - Recognizing flowers.



I trained the mode and I achieved accuracy of 0.99.



To predict, I did:



a = model.predict(train[:6])


output:



array([[5.12799371e-18, 2.08305119e-05, 1.14476855e-07, 1.28556788e-02,
1.46144101e-08, 1.85072349e-05],
[7.72907813e-32, 7.86612819e-09, 8.08554124e-13, 1.87227300e-08,
4.61950422e-10, 6.42609745e-02],
[0.00000000e+00, 1.34313246e-02, 9.67072342e-13, 2.82699081e-12,
1.10958222e-10, 4.68058548e-14],
[7.75535319e-27, 6.51194032e-09, 2.49026186e-07, 1.88803018e-08,
3.77964647e-03, 7.01414028e-05],
[7.24011743e-22, 5.85804628e-07, 1.61177505e-09, 2.27746829e-01,
5.44432410e-09, 3.94427252e-06],
[1.81492225e-15, 3.36600904e-04, 4.39262622e-05, 8.63518100e-04,
9.29966700e-06, 9.75337625e-02]], dtype=float32)


How do I interpret this? How do I know get the label it predicted? I have five labels 0-4, which are assigned to 5 types of flowers.



My notebook is here.



What am I doing wrong here?










share|improve this question











$endgroup$











  • $begingroup$
    Why do you have 6 output nodes? Can we get access to the data please?
    $endgroup$
    – JahKnows
    Sep 2 '18 at 20:21






  • 1




    $begingroup$
    @JahKnows link to the dataset is provided(1st line).
    $endgroup$
    – Jagadeesh Kotra
    Sep 2 '18 at 20:23















5












$begingroup$


I'm working on a multi-classification problem - Recognizing flowers.



I trained the mode and I achieved accuracy of 0.99.



To predict, I did:



a = model.predict(train[:6])


output:



array([[5.12799371e-18, 2.08305119e-05, 1.14476855e-07, 1.28556788e-02,
1.46144101e-08, 1.85072349e-05],
[7.72907813e-32, 7.86612819e-09, 8.08554124e-13, 1.87227300e-08,
4.61950422e-10, 6.42609745e-02],
[0.00000000e+00, 1.34313246e-02, 9.67072342e-13, 2.82699081e-12,
1.10958222e-10, 4.68058548e-14],
[7.75535319e-27, 6.51194032e-09, 2.49026186e-07, 1.88803018e-08,
3.77964647e-03, 7.01414028e-05],
[7.24011743e-22, 5.85804628e-07, 1.61177505e-09, 2.27746829e-01,
5.44432410e-09, 3.94427252e-06],
[1.81492225e-15, 3.36600904e-04, 4.39262622e-05, 8.63518100e-04,
9.29966700e-06, 9.75337625e-02]], dtype=float32)


How do I interpret this? How do I know get the label it predicted? I have five labels 0-4, which are assigned to 5 types of flowers.



My notebook is here.



What am I doing wrong here?










share|improve this question











$endgroup$











  • $begingroup$
    Why do you have 6 output nodes? Can we get access to the data please?
    $endgroup$
    – JahKnows
    Sep 2 '18 at 20:21






  • 1




    $begingroup$
    @JahKnows link to the dataset is provided(1st line).
    $endgroup$
    – Jagadeesh Kotra
    Sep 2 '18 at 20:23













5












5








5


3



$begingroup$


I'm working on a multi-classification problem - Recognizing flowers.



I trained the mode and I achieved accuracy of 0.99.



To predict, I did:



a = model.predict(train[:6])


output:



array([[5.12799371e-18, 2.08305119e-05, 1.14476855e-07, 1.28556788e-02,
1.46144101e-08, 1.85072349e-05],
[7.72907813e-32, 7.86612819e-09, 8.08554124e-13, 1.87227300e-08,
4.61950422e-10, 6.42609745e-02],
[0.00000000e+00, 1.34313246e-02, 9.67072342e-13, 2.82699081e-12,
1.10958222e-10, 4.68058548e-14],
[7.75535319e-27, 6.51194032e-09, 2.49026186e-07, 1.88803018e-08,
3.77964647e-03, 7.01414028e-05],
[7.24011743e-22, 5.85804628e-07, 1.61177505e-09, 2.27746829e-01,
5.44432410e-09, 3.94427252e-06],
[1.81492225e-15, 3.36600904e-04, 4.39262622e-05, 8.63518100e-04,
9.29966700e-06, 9.75337625e-02]], dtype=float32)


How do I interpret this? How do I know get the label it predicted? I have five labels 0-4, which are assigned to 5 types of flowers.



My notebook is here.



What am I doing wrong here?










share|improve this question











$endgroup$




I'm working on a multi-classification problem - Recognizing flowers.



I trained the mode and I achieved accuracy of 0.99.



To predict, I did:



a = model.predict(train[:6])


output:



array([[5.12799371e-18, 2.08305119e-05, 1.14476855e-07, 1.28556788e-02,
1.46144101e-08, 1.85072349e-05],
[7.72907813e-32, 7.86612819e-09, 8.08554124e-13, 1.87227300e-08,
4.61950422e-10, 6.42609745e-02],
[0.00000000e+00, 1.34313246e-02, 9.67072342e-13, 2.82699081e-12,
1.10958222e-10, 4.68058548e-14],
[7.75535319e-27, 6.51194032e-09, 2.49026186e-07, 1.88803018e-08,
3.77964647e-03, 7.01414028e-05],
[7.24011743e-22, 5.85804628e-07, 1.61177505e-09, 2.27746829e-01,
5.44432410e-09, 3.94427252e-06],
[1.81492225e-15, 3.36600904e-04, 4.39262622e-05, 8.63518100e-04,
9.29966700e-06, 9.75337625e-02]], dtype=float32)


How do I interpret this? How do I know get the label it predicted? I have five labels 0-4, which are assigned to 5 types of flowers.



My notebook is here.



What am I doing wrong here?







machine-learning keras image-classification prediction






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 2 '18 at 19:23









Stephen Rauch

1,52551330




1,52551330










asked Sep 2 '18 at 19:04









Jagadeesh KotraJagadeesh Kotra

283




283











  • $begingroup$
    Why do you have 6 output nodes? Can we get access to the data please?
    $endgroup$
    – JahKnows
    Sep 2 '18 at 20:21






  • 1




    $begingroup$
    @JahKnows link to the dataset is provided(1st line).
    $endgroup$
    – Jagadeesh Kotra
    Sep 2 '18 at 20:23
















  • $begingroup$
    Why do you have 6 output nodes? Can we get access to the data please?
    $endgroup$
    – JahKnows
    Sep 2 '18 at 20:21






  • 1




    $begingroup$
    @JahKnows link to the dataset is provided(1st line).
    $endgroup$
    – Jagadeesh Kotra
    Sep 2 '18 at 20:23















$begingroup$
Why do you have 6 output nodes? Can we get access to the data please?
$endgroup$
– JahKnows
Sep 2 '18 at 20:21




$begingroup$
Why do you have 6 output nodes? Can we get access to the data please?
$endgroup$
– JahKnows
Sep 2 '18 at 20:21




1




1




$begingroup$
@JahKnows link to the dataset is provided(1st line).
$endgroup$
– Jagadeesh Kotra
Sep 2 '18 at 20:23




$begingroup$
@JahKnows link to the dataset is provided(1st line).
$endgroup$
– Jagadeesh Kotra
Sep 2 '18 at 20:23










1 Answer
1






active

oldest

votes


















9












$begingroup$

Alright so I rewrote some parts of your model such that it makes more sense for a classification problem. The first and most obvious reason your network was not working is due to the number of output nodes you selected. For a classification task the number of output nodes should be the same as the number of classes in your data. In this case we have 5 kinds of flowers, thus 5 labels which I reassigned to $y in 0, 1, 2, 3, 4$, thus we will have 5 output nodes.



So let's go through the code. First we bring the data into the notebook using the code you wrote.



from os import listdir
import cv2

daisy_path = "flowers/daisy/"
dandelion_path = "flowers/dandelion/"
rose_path = "flowers/rose/"
sunflower_path = "flowers/sunflower/"
tulip_path = "flowers/tulip/"

def iter_images(images,directory,size,label):
try:
for i in range(len(images)):
img = cv2.imread(directory + images[i])
img = cv2.resize(img,size)
img_data.append(img)
labels.append(label)
except:
pass

img_data = []
labels = []
size = 64,64
iter_images(listdir(daisy_path),daisy_path,size,0)
iter_images(listdir(dandelion_path),dandelion_path,size,1)
iter_images(listdir(rose_path),rose_path,size,2)
iter_images(listdir(sunflower_path),sunflower_path,size,3)
iter_images(listdir(tulip_path),tulip_path,size,4)


We can visualize the data to get a better idea of the distribution of the classes.



import matplotlib.pyplot as plt
%matplotlib inline

n_classes = 5

training_counts = [None] * n_classes
testing_counts = [None] * n_classes
for i in range(n_classes):
training_counts[i] = len(y_train[y_train == i])/len(y_train)
testing_counts[i] = len(y_test[y_test == i])/len(y_test)

# the histogram of the data
train_bar = plt.bar(np.arange(n_classes)-0.2, training_counts, align='center', color = 'r', alpha=0.75, width = 0.41, label='Training')
test_bar = plt.bar(np.arange(n_classes)+0.2, testing_counts, align='center', color = 'b', alpha=0.75, width = 0.41, label = 'Testing')

plt.xlabel('Labels')
plt.xticks((0,1,2,3,4))
plt.ylabel('Count (%)')
plt.title('Label distribution in the training and test set')
plt.legend(bbox_to_anchor=(1.05, 1), handles=[train_bar, test_bar], loc=2)
plt.grid(True)
plt.show()


enter image description here



We will now transform the data and the labels to matrices.



import numpy as np
data = np.array(img_data)
data.shape
data = data.astype('float32') / 255.0
labels = np.asarray(labels)


Then we will split the data.. Notice that you do not need to shuffle the data yourself since sklearn can do it for you.



from sklearn.model_selection import train_test_split

# Split the data
x_train, x_test, y_train, y_test = train_test_split(data, labels, test_size=0.33, shuffle= True)


Let's construct our model. I changed the last layer to use the softmax activation function. This will allow the outputs of the network to sum up to a total probability of 1. This is the usual activation function to use for classification tasks.



from keras.models import Sequential
from keras.layers import Dense,Flatten,Convolution2D,MaxPool2D
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.callbacks import ModelCheckpoint
from keras.models import model_from_json
from keras import backend as K

model = Sequential()
model.add(Convolution2D(32, (3,3),input_shape=(64, 64, 3),activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dense(5,activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])


Then we can train our network. This will result in about 60% accuracy on the test set. This is pretty good considering the baseline for this task is 20%.



batch_size = 128
epochs = 10
model.fit(x_train, y_train_binary,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test_binary))


After the model is trained you can predict instances using. Don't forget that the network needs to take the same shape in. Thus we must maintain the dimensionality of the matrix, that's why I use the [0:1].



print('Predict the classes: ')
prediction = model.predict_classes(x_test[0:1])
print('Predicted class: ', prediction)
print('Real class: ', y_test[0:1])


This gives




Predict the classes: 1/1
[==============================] - 0s
6ms/step
Predicted class: [4]
Real class: [4]




Some suggestions



The model you are currently using is the one that is most common for MNIST. However, that data only has a single channel thus we don't need as many layers. You can increase the performance by increasing the complexity of your model. Or by reducing the complexity of your data, for example you can train using the grayscale equivalent of the images, thus reducing the problem to a single channel.






share|improve this answer











$endgroup$












  • $begingroup$
    thanks for your answer,if you don't mind, can you share the full notebook?
    $endgroup$
    – Jagadeesh Kotra
    Sep 2 '18 at 21:28






  • 1




    $begingroup$
    Yeah sure Ill put it on github one moment
    $endgroup$
    – JahKnows
    Sep 2 '18 at 21:28






  • 1




    $begingroup$
    github.com/kelhallaoui/tutorials/blob/master/Machine%20Learning/…
    $endgroup$
    – JahKnows
    Sep 2 '18 at 21:33











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "557"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f37722%2fhow-to-interpret-predictions-from-model%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









9












$begingroup$

Alright so I rewrote some parts of your model such that it makes more sense for a classification problem. The first and most obvious reason your network was not working is due to the number of output nodes you selected. For a classification task the number of output nodes should be the same as the number of classes in your data. In this case we have 5 kinds of flowers, thus 5 labels which I reassigned to $y in 0, 1, 2, 3, 4$, thus we will have 5 output nodes.



So let's go through the code. First we bring the data into the notebook using the code you wrote.



from os import listdir
import cv2

daisy_path = "flowers/daisy/"
dandelion_path = "flowers/dandelion/"
rose_path = "flowers/rose/"
sunflower_path = "flowers/sunflower/"
tulip_path = "flowers/tulip/"

def iter_images(images,directory,size,label):
try:
for i in range(len(images)):
img = cv2.imread(directory + images[i])
img = cv2.resize(img,size)
img_data.append(img)
labels.append(label)
except:
pass

img_data = []
labels = []
size = 64,64
iter_images(listdir(daisy_path),daisy_path,size,0)
iter_images(listdir(dandelion_path),dandelion_path,size,1)
iter_images(listdir(rose_path),rose_path,size,2)
iter_images(listdir(sunflower_path),sunflower_path,size,3)
iter_images(listdir(tulip_path),tulip_path,size,4)


We can visualize the data to get a better idea of the distribution of the classes.



import matplotlib.pyplot as plt
%matplotlib inline

n_classes = 5

training_counts = [None] * n_classes
testing_counts = [None] * n_classes
for i in range(n_classes):
training_counts[i] = len(y_train[y_train == i])/len(y_train)
testing_counts[i] = len(y_test[y_test == i])/len(y_test)

# the histogram of the data
train_bar = plt.bar(np.arange(n_classes)-0.2, training_counts, align='center', color = 'r', alpha=0.75, width = 0.41, label='Training')
test_bar = plt.bar(np.arange(n_classes)+0.2, testing_counts, align='center', color = 'b', alpha=0.75, width = 0.41, label = 'Testing')

plt.xlabel('Labels')
plt.xticks((0,1,2,3,4))
plt.ylabel('Count (%)')
plt.title('Label distribution in the training and test set')
plt.legend(bbox_to_anchor=(1.05, 1), handles=[train_bar, test_bar], loc=2)
plt.grid(True)
plt.show()


enter image description here



We will now transform the data and the labels to matrices.



import numpy as np
data = np.array(img_data)
data.shape
data = data.astype('float32') / 255.0
labels = np.asarray(labels)


Then we will split the data.. Notice that you do not need to shuffle the data yourself since sklearn can do it for you.



from sklearn.model_selection import train_test_split

# Split the data
x_train, x_test, y_train, y_test = train_test_split(data, labels, test_size=0.33, shuffle= True)


Let's construct our model. I changed the last layer to use the softmax activation function. This will allow the outputs of the network to sum up to a total probability of 1. This is the usual activation function to use for classification tasks.



from keras.models import Sequential
from keras.layers import Dense,Flatten,Convolution2D,MaxPool2D
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.callbacks import ModelCheckpoint
from keras.models import model_from_json
from keras import backend as K

model = Sequential()
model.add(Convolution2D(32, (3,3),input_shape=(64, 64, 3),activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dense(5,activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])


Then we can train our network. This will result in about 60% accuracy on the test set. This is pretty good considering the baseline for this task is 20%.



batch_size = 128
epochs = 10
model.fit(x_train, y_train_binary,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test_binary))


After the model is trained you can predict instances using. Don't forget that the network needs to take the same shape in. Thus we must maintain the dimensionality of the matrix, that's why I use the [0:1].



print('Predict the classes: ')
prediction = model.predict_classes(x_test[0:1])
print('Predicted class: ', prediction)
print('Real class: ', y_test[0:1])


This gives




Predict the classes: 1/1
[==============================] - 0s
6ms/step
Predicted class: [4]
Real class: [4]




Some suggestions



The model you are currently using is the one that is most common for MNIST. However, that data only has a single channel thus we don't need as many layers. You can increase the performance by increasing the complexity of your model. Or by reducing the complexity of your data, for example you can train using the grayscale equivalent of the images, thus reducing the problem to a single channel.






share|improve this answer











$endgroup$












  • $begingroup$
    thanks for your answer,if you don't mind, can you share the full notebook?
    $endgroup$
    – Jagadeesh Kotra
    Sep 2 '18 at 21:28






  • 1




    $begingroup$
    Yeah sure Ill put it on github one moment
    $endgroup$
    – JahKnows
    Sep 2 '18 at 21:28






  • 1




    $begingroup$
    github.com/kelhallaoui/tutorials/blob/master/Machine%20Learning/…
    $endgroup$
    – JahKnows
    Sep 2 '18 at 21:33















9












$begingroup$

Alright so I rewrote some parts of your model such that it makes more sense for a classification problem. The first and most obvious reason your network was not working is due to the number of output nodes you selected. For a classification task the number of output nodes should be the same as the number of classes in your data. In this case we have 5 kinds of flowers, thus 5 labels which I reassigned to $y in 0, 1, 2, 3, 4$, thus we will have 5 output nodes.



So let's go through the code. First we bring the data into the notebook using the code you wrote.



from os import listdir
import cv2

daisy_path = "flowers/daisy/"
dandelion_path = "flowers/dandelion/"
rose_path = "flowers/rose/"
sunflower_path = "flowers/sunflower/"
tulip_path = "flowers/tulip/"

def iter_images(images,directory,size,label):
try:
for i in range(len(images)):
img = cv2.imread(directory + images[i])
img = cv2.resize(img,size)
img_data.append(img)
labels.append(label)
except:
pass

img_data = []
labels = []
size = 64,64
iter_images(listdir(daisy_path),daisy_path,size,0)
iter_images(listdir(dandelion_path),dandelion_path,size,1)
iter_images(listdir(rose_path),rose_path,size,2)
iter_images(listdir(sunflower_path),sunflower_path,size,3)
iter_images(listdir(tulip_path),tulip_path,size,4)


We can visualize the data to get a better idea of the distribution of the classes.



import matplotlib.pyplot as plt
%matplotlib inline

n_classes = 5

training_counts = [None] * n_classes
testing_counts = [None] * n_classes
for i in range(n_classes):
training_counts[i] = len(y_train[y_train == i])/len(y_train)
testing_counts[i] = len(y_test[y_test == i])/len(y_test)

# the histogram of the data
train_bar = plt.bar(np.arange(n_classes)-0.2, training_counts, align='center', color = 'r', alpha=0.75, width = 0.41, label='Training')
test_bar = plt.bar(np.arange(n_classes)+0.2, testing_counts, align='center', color = 'b', alpha=0.75, width = 0.41, label = 'Testing')

plt.xlabel('Labels')
plt.xticks((0,1,2,3,4))
plt.ylabel('Count (%)')
plt.title('Label distribution in the training and test set')
plt.legend(bbox_to_anchor=(1.05, 1), handles=[train_bar, test_bar], loc=2)
plt.grid(True)
plt.show()


enter image description here



We will now transform the data and the labels to matrices.



import numpy as np
data = np.array(img_data)
data.shape
data = data.astype('float32') / 255.0
labels = np.asarray(labels)


Then we will split the data.. Notice that you do not need to shuffle the data yourself since sklearn can do it for you.



from sklearn.model_selection import train_test_split

# Split the data
x_train, x_test, y_train, y_test = train_test_split(data, labels, test_size=0.33, shuffle= True)


Let's construct our model. I changed the last layer to use the softmax activation function. This will allow the outputs of the network to sum up to a total probability of 1. This is the usual activation function to use for classification tasks.



from keras.models import Sequential
from keras.layers import Dense,Flatten,Convolution2D,MaxPool2D
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.callbacks import ModelCheckpoint
from keras.models import model_from_json
from keras import backend as K

model = Sequential()
model.add(Convolution2D(32, (3,3),input_shape=(64, 64, 3),activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dense(5,activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])


Then we can train our network. This will result in about 60% accuracy on the test set. This is pretty good considering the baseline for this task is 20%.



batch_size = 128
epochs = 10
model.fit(x_train, y_train_binary,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test_binary))


After the model is trained you can predict instances using. Don't forget that the network needs to take the same shape in. Thus we must maintain the dimensionality of the matrix, that's why I use the [0:1].



print('Predict the classes: ')
prediction = model.predict_classes(x_test[0:1])
print('Predicted class: ', prediction)
print('Real class: ', y_test[0:1])


This gives




Predict the classes: 1/1
[==============================] - 0s
6ms/step
Predicted class: [4]
Real class: [4]




Some suggestions



The model you are currently using is the one that is most common for MNIST. However, that data only has a single channel thus we don't need as many layers. You can increase the performance by increasing the complexity of your model. Or by reducing the complexity of your data, for example you can train using the grayscale equivalent of the images, thus reducing the problem to a single channel.






share|improve this answer











$endgroup$












  • $begingroup$
    thanks for your answer,if you don't mind, can you share the full notebook?
    $endgroup$
    – Jagadeesh Kotra
    Sep 2 '18 at 21:28






  • 1




    $begingroup$
    Yeah sure Ill put it on github one moment
    $endgroup$
    – JahKnows
    Sep 2 '18 at 21:28






  • 1




    $begingroup$
    github.com/kelhallaoui/tutorials/blob/master/Machine%20Learning/…
    $endgroup$
    – JahKnows
    Sep 2 '18 at 21:33













9












9








9





$begingroup$

Alright so I rewrote some parts of your model such that it makes more sense for a classification problem. The first and most obvious reason your network was not working is due to the number of output nodes you selected. For a classification task the number of output nodes should be the same as the number of classes in your data. In this case we have 5 kinds of flowers, thus 5 labels which I reassigned to $y in 0, 1, 2, 3, 4$, thus we will have 5 output nodes.



So let's go through the code. First we bring the data into the notebook using the code you wrote.



from os import listdir
import cv2

daisy_path = "flowers/daisy/"
dandelion_path = "flowers/dandelion/"
rose_path = "flowers/rose/"
sunflower_path = "flowers/sunflower/"
tulip_path = "flowers/tulip/"

def iter_images(images,directory,size,label):
try:
for i in range(len(images)):
img = cv2.imread(directory + images[i])
img = cv2.resize(img,size)
img_data.append(img)
labels.append(label)
except:
pass

img_data = []
labels = []
size = 64,64
iter_images(listdir(daisy_path),daisy_path,size,0)
iter_images(listdir(dandelion_path),dandelion_path,size,1)
iter_images(listdir(rose_path),rose_path,size,2)
iter_images(listdir(sunflower_path),sunflower_path,size,3)
iter_images(listdir(tulip_path),tulip_path,size,4)


We can visualize the data to get a better idea of the distribution of the classes.



import matplotlib.pyplot as plt
%matplotlib inline

n_classes = 5

training_counts = [None] * n_classes
testing_counts = [None] * n_classes
for i in range(n_classes):
training_counts[i] = len(y_train[y_train == i])/len(y_train)
testing_counts[i] = len(y_test[y_test == i])/len(y_test)

# the histogram of the data
train_bar = plt.bar(np.arange(n_classes)-0.2, training_counts, align='center', color = 'r', alpha=0.75, width = 0.41, label='Training')
test_bar = plt.bar(np.arange(n_classes)+0.2, testing_counts, align='center', color = 'b', alpha=0.75, width = 0.41, label = 'Testing')

plt.xlabel('Labels')
plt.xticks((0,1,2,3,4))
plt.ylabel('Count (%)')
plt.title('Label distribution in the training and test set')
plt.legend(bbox_to_anchor=(1.05, 1), handles=[train_bar, test_bar], loc=2)
plt.grid(True)
plt.show()


enter image description here



We will now transform the data and the labels to matrices.



import numpy as np
data = np.array(img_data)
data.shape
data = data.astype('float32') / 255.0
labels = np.asarray(labels)


Then we will split the data.. Notice that you do not need to shuffle the data yourself since sklearn can do it for you.



from sklearn.model_selection import train_test_split

# Split the data
x_train, x_test, y_train, y_test = train_test_split(data, labels, test_size=0.33, shuffle= True)


Let's construct our model. I changed the last layer to use the softmax activation function. This will allow the outputs of the network to sum up to a total probability of 1. This is the usual activation function to use for classification tasks.



from keras.models import Sequential
from keras.layers import Dense,Flatten,Convolution2D,MaxPool2D
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.callbacks import ModelCheckpoint
from keras.models import model_from_json
from keras import backend as K

model = Sequential()
model.add(Convolution2D(32, (3,3),input_shape=(64, 64, 3),activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dense(5,activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])


Then we can train our network. This will result in about 60% accuracy on the test set. This is pretty good considering the baseline for this task is 20%.



batch_size = 128
epochs = 10
model.fit(x_train, y_train_binary,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test_binary))


After the model is trained you can predict instances using. Don't forget that the network needs to take the same shape in. Thus we must maintain the dimensionality of the matrix, that's why I use the [0:1].



print('Predict the classes: ')
prediction = model.predict_classes(x_test[0:1])
print('Predicted class: ', prediction)
print('Real class: ', y_test[0:1])


This gives




Predict the classes: 1/1
[==============================] - 0s
6ms/step
Predicted class: [4]
Real class: [4]




Some suggestions



The model you are currently using is the one that is most common for MNIST. However, that data only has a single channel thus we don't need as many layers. You can increase the performance by increasing the complexity of your model. Or by reducing the complexity of your data, for example you can train using the grayscale equivalent of the images, thus reducing the problem to a single channel.






share|improve this answer











$endgroup$



Alright so I rewrote some parts of your model such that it makes more sense for a classification problem. The first and most obvious reason your network was not working is due to the number of output nodes you selected. For a classification task the number of output nodes should be the same as the number of classes in your data. In this case we have 5 kinds of flowers, thus 5 labels which I reassigned to $y in 0, 1, 2, 3, 4$, thus we will have 5 output nodes.



So let's go through the code. First we bring the data into the notebook using the code you wrote.



from os import listdir
import cv2

daisy_path = "flowers/daisy/"
dandelion_path = "flowers/dandelion/"
rose_path = "flowers/rose/"
sunflower_path = "flowers/sunflower/"
tulip_path = "flowers/tulip/"

def iter_images(images,directory,size,label):
try:
for i in range(len(images)):
img = cv2.imread(directory + images[i])
img = cv2.resize(img,size)
img_data.append(img)
labels.append(label)
except:
pass

img_data = []
labels = []
size = 64,64
iter_images(listdir(daisy_path),daisy_path,size,0)
iter_images(listdir(dandelion_path),dandelion_path,size,1)
iter_images(listdir(rose_path),rose_path,size,2)
iter_images(listdir(sunflower_path),sunflower_path,size,3)
iter_images(listdir(tulip_path),tulip_path,size,4)


We can visualize the data to get a better idea of the distribution of the classes.



import matplotlib.pyplot as plt
%matplotlib inline

n_classes = 5

training_counts = [None] * n_classes
testing_counts = [None] * n_classes
for i in range(n_classes):
training_counts[i] = len(y_train[y_train == i])/len(y_train)
testing_counts[i] = len(y_test[y_test == i])/len(y_test)

# the histogram of the data
train_bar = plt.bar(np.arange(n_classes)-0.2, training_counts, align='center', color = 'r', alpha=0.75, width = 0.41, label='Training')
test_bar = plt.bar(np.arange(n_classes)+0.2, testing_counts, align='center', color = 'b', alpha=0.75, width = 0.41, label = 'Testing')

plt.xlabel('Labels')
plt.xticks((0,1,2,3,4))
plt.ylabel('Count (%)')
plt.title('Label distribution in the training and test set')
plt.legend(bbox_to_anchor=(1.05, 1), handles=[train_bar, test_bar], loc=2)
plt.grid(True)
plt.show()


enter image description here



We will now transform the data and the labels to matrices.



import numpy as np
data = np.array(img_data)
data.shape
data = data.astype('float32') / 255.0
labels = np.asarray(labels)


Then we will split the data.. Notice that you do not need to shuffle the data yourself since sklearn can do it for you.



from sklearn.model_selection import train_test_split

# Split the data
x_train, x_test, y_train, y_test = train_test_split(data, labels, test_size=0.33, shuffle= True)


Let's construct our model. I changed the last layer to use the softmax activation function. This will allow the outputs of the network to sum up to a total probability of 1. This is the usual activation function to use for classification tasks.



from keras.models import Sequential
from keras.layers import Dense,Flatten,Convolution2D,MaxPool2D
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.callbacks import ModelCheckpoint
from keras.models import model_from_json
from keras import backend as K

model = Sequential()
model.add(Convolution2D(32, (3,3),input_shape=(64, 64, 3),activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dense(5,activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])


Then we can train our network. This will result in about 60% accuracy on the test set. This is pretty good considering the baseline for this task is 20%.



batch_size = 128
epochs = 10
model.fit(x_train, y_train_binary,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test_binary))


After the model is trained you can predict instances using. Don't forget that the network needs to take the same shape in. Thus we must maintain the dimensionality of the matrix, that's why I use the [0:1].



print('Predict the classes: ')
prediction = model.predict_classes(x_test[0:1])
print('Predicted class: ', prediction)
print('Real class: ', y_test[0:1])


This gives




Predict the classes: 1/1
[==============================] - 0s
6ms/step
Predicted class: [4]
Real class: [4]




Some suggestions



The model you are currently using is the one that is most common for MNIST. However, that data only has a single channel thus we don't need as many layers. You can increase the performance by increasing the complexity of your model. Or by reducing the complexity of your data, for example you can train using the grayscale equivalent of the images, thus reducing the problem to a single channel.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 31 at 19:49









Glorfindel

1511210




1511210










answered Sep 2 '18 at 21:11









JahKnowsJahKnows

5,277727




5,277727











  • $begingroup$
    thanks for your answer,if you don't mind, can you share the full notebook?
    $endgroup$
    – Jagadeesh Kotra
    Sep 2 '18 at 21:28






  • 1




    $begingroup$
    Yeah sure Ill put it on github one moment
    $endgroup$
    – JahKnows
    Sep 2 '18 at 21:28






  • 1




    $begingroup$
    github.com/kelhallaoui/tutorials/blob/master/Machine%20Learning/…
    $endgroup$
    – JahKnows
    Sep 2 '18 at 21:33
















  • $begingroup$
    thanks for your answer,if you don't mind, can you share the full notebook?
    $endgroup$
    – Jagadeesh Kotra
    Sep 2 '18 at 21:28






  • 1




    $begingroup$
    Yeah sure Ill put it on github one moment
    $endgroup$
    – JahKnows
    Sep 2 '18 at 21:28






  • 1




    $begingroup$
    github.com/kelhallaoui/tutorials/blob/master/Machine%20Learning/…
    $endgroup$
    – JahKnows
    Sep 2 '18 at 21:33















$begingroup$
thanks for your answer,if you don't mind, can you share the full notebook?
$endgroup$
– Jagadeesh Kotra
Sep 2 '18 at 21:28




$begingroup$
thanks for your answer,if you don't mind, can you share the full notebook?
$endgroup$
– Jagadeesh Kotra
Sep 2 '18 at 21:28




1




1




$begingroup$
Yeah sure Ill put it on github one moment
$endgroup$
– JahKnows
Sep 2 '18 at 21:28




$begingroup$
Yeah sure Ill put it on github one moment
$endgroup$
– JahKnows
Sep 2 '18 at 21:28




1




1




$begingroup$
github.com/kelhallaoui/tutorials/blob/master/Machine%20Learning/…
$endgroup$
– JahKnows
Sep 2 '18 at 21:33




$begingroup$
github.com/kelhallaoui/tutorials/blob/master/Machine%20Learning/…
$endgroup$
– JahKnows
Sep 2 '18 at 21:33

















draft saved

draft discarded
















































Thanks for contributing an answer to Data Science Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f37722%2fhow-to-interpret-predictions-from-model%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

Luettelo Yhdysvaltain laivaston lentotukialuksista Lähteet | Navigointivalikko

Gary (muusikko) Sisällysluettelo Historia | Rockin' High | Lähteet | Aiheesta muualla | NavigointivalikkoInfobox OKTuomas "Gary" Keskinen Ancaran kitaristiksiProjekti Rockin' High