2
$\begingroup$

I have a very basic convolutional neural net built in Keras with a TensorFlow backend. The model is based of this Kaggle kernel full model. The input training data are 256x256 images with a corresponding set of class labels in a csv file. For example, here are the class labels in the csv file:

image_name  |  tags
----------------------------------------
train_0     |  class1 class2 class3
train_1     |  class2
train_2     |  class3
and so on ...

The following code shows how I am building, training, and predicting results.

# Build model
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=(32, 32, 3))) # Originally (32, 32, 3)

model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(3, activation='sigmoid')) # Originally 17

model.compile(loss='binary_crossentropy', # We NEED binary here, since categorical_crossentropy l1 norms the output before calculating loss.
              optimizer='adam',
              metrics=['accuracy'])

# Train the model              
model.fit(x_train, y_train,
          batch_size=128,
          epochs=4,
          verbose=1,
          validation_data=(x_valid, y_valid))

# Predict
p_valid = model.predict(x_valid, batch_size=128)

You can see the predicted results are an array of class probabilities:

p_valid
Out[29]: 
array([[2.4571007e-01, 1.4929530e-02, 9.3575776e-01],
       [2.6206359e-01, 1.2429485e-02, 9.5172155e-01],
       [3.3679003e-01, 2.8344743e-02, 8.5209453e-01],
       ...,
       [8.2605546e-03, 2.0513092e-07, 9.9999821e-01],
       [8.2605546e-03, 2.0513092e-07, 9.9999821e-01],
       [8.2605667e-03, 2.0513131e-07, 9.9999821e-01]], dtype=float32)

How do I know which probability array is corresponding to which image?

$\endgroup$

1 Answer 1

1
$\begingroup$

Keras Model.predict() method doesn't shuffle the data, so each row in p_valid corresponds to the row in x_valid. For instance, p_valid[2] holds the probabilities for the image x_valid[2].

By the way, note that the values in each row don't necessarily sum up to 1, because they correspond to the probability of class i being present, thus are not exclusive.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.