What does an array with 3 probabilities imply when you run model.predict on a model that has been trained on 2 classes

Viewed 32

A model was trained on 2 classes. When running a prediction on a sample image an array with 3 probabilities is returned. What do they imply?

Code for predicting the sample image:

x_sample = x_test[600].reshape(-1, img_size, img_size, 3)
y_pred = model.predict(x_sample)
print(y_pred )

Returned result:

[[2.6878263e-03 9.9722868e-01 8.3401377e-05]]

What does the above array with what seems to be 3 probabilities imply?

How I imported the VGG16 pre-trained model:

#Importing VGG16 Model

conv_base = VGG16(weights = 'imagenet',
                 include_top = False,
                 input_shape=(img_size, img_size, 3))

VGG16 Model Architecture:

Model: "vgg16"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_1 (InputLayer)        [(None, 224, 224, 3)]     0         
                                                                 
 block1_conv1 (Conv2D)       (None, 224, 224, 64)      1792      
                                                                 
 block1_conv2 (Conv2D)       (None, 224, 224, 64)      36928     
                                                                 
 block1_pool (MaxPooling2D)  (None, 112, 112, 64)      0         
                                                                 
 block2_conv1 (Conv2D)       (None, 112, 112, 128)     73856     
                                                                 
 block2_conv2 (Conv2D)       (None, 112, 112, 128)     147584    
                                                                 
 block2_pool (MaxPooling2D)  (None, 56, 56, 128)       0         
                                                                 
 block3_conv1 (Conv2D)       (None, 56, 56, 256)       295168    
                                                                 
 block3_conv2 (Conv2D)       (None, 56, 56, 256)       590080    
                                                                 
 block3_conv3 (Conv2D)       (None, 56, 56, 256)       590080    
                                                                 
 block3_pool (MaxPooling2D)  (None, 28, 28, 256)       0         
                                                                 
 block4_conv1 (Conv2D)       (None, 28, 28, 512)       1180160   
                                                                 
 block4_conv2 (Conv2D)       (None, 28, 28, 512)       2359808   
                                                                 
 block4_conv3 (Conv2D)       (None, 28, 28, 512)       2359808   
                                                                 
 block4_pool (MaxPooling2D)  (None, 14, 14, 512)       0         
                                                                 
 block5_conv1 (Conv2D)       (None, 14, 14, 512)       2359808   
                                                                 
 block5_conv2 (Conv2D)       (None, 14, 14, 512)       2359808   
                                                                 
 block5_conv3 (Conv2D)       (None, 14, 14, 512)       2359808   
                                                                 
 block5_pool (MaxPooling2D)  (None, 7, 7, 512)         0
=================================================================
Total params: 14,714,688
Trainable params: 14,714,688
Non-trainable params: 0 

Adding top layers:

#Adding top layers

model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dropout(0.25))
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dropout(0.25))
model.add(layers.Dense(3, activation='softmax'))

Model architecture after adding top layers:

Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 vgg16 (Functional)          (None, 7, 7, 512)         14714688  
                                                                 
 flatten (Flatten)           (None, 25088)             0         
                                                                 
 dense (Dense)               (None, 256)               6422784   
                                                                 
 dropout (Dropout)           (None, 256)               0         
                                                                 
 dense_1 (Dense)             (None, 256)               65792     
                                                                 
 dropout_1 (Dropout)         (None, 256)               0         
                                                                 
 dense_2 (Dense)             (None, 3)                 771       
                                                                 
=================================================================
Total params: 21,204,035
Trainable params: 21,204,035
Non-trainable params: 0

Function for preprocessing data to conform to the input layer of the VGG16 model we want to train:

# Function for preparing our data to conform to the last layer of our VGG16 model we want to add and train

dir_labels = ['CLASS_A', 'CLASS_B']
img_size = 224
def get_training_data(data_dir):
    data = [] 
    for label in dir_labels: 
        path = os.path.join(data_dir, label)
        for img in os.listdir(path):
            try:
                img_arr = cv2.imread(os.path.join(path, img), cv2.IMREAD_COLOR)
                resized_arr = cv2.resize(img_arr, (img_size, img_size)) # Reshaping images to preferred size
                if label == 'CLASS_A':
                    data.append([resized_arr, 0])
                if label == 'CLASS_B':
                    data.append([resized_arr, 1])
            except Exception as e:
                print(e)
    return np.array(data)

Calling the function to make train, validation and test list:

train = get_training_data('/content/.../train')
test = get_training_data('/content/.../test')
val = get_training_data('/content/.../val')

Making images and label lists:

x_train = []
y_train = []

x_val = []
y_val = []

x_test = []
y_test = []

for feature, label in train:
    x_train.append(feature)
    y_train.append(label)

for feature, label in test:
    x_test.append(feature)
    y_test.append(label)
    
for feature, label in val:
    x_val.append(feature)
    y_val.append(label)
0 Answers
Related