[英]Keras model.predict with sigmoid activation and binary cross entropy returns only 0 or 1, not probability
[英]Keras model.predict only returns one figure for binary classification task
我根據這個指令“ https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html ”用Keras訓練了一個二進制分類任務。
但是,model.predict 只返回一個數字,例如 [[0.6343]]。 我認為它應該返回兩個數字,例如 [[0.6343, 0.1245]] 其中每個數字代表每個類別的概率。
我正在使用 2.2.4 版的 Keras 和 1.13.1 版的 Tensorflow。
這是我的代碼。
from keras.models import Sequential
from keras.layers import Activation, Dense, Dropout, Flatten, Conv2D, MaxPooling2D
from keras.utils import np_utils
from sklearn.datasets import fetch_mldata
from keras.datasets import mnist
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import tensorflowjs as tfjs
##############
# Train model
##############
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(96, 128, 3), data_format="channels_last"))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
################################
# Read image data from directory
################################
batch_size = 16
# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
rescale=1./255,
shear_range=0.2,
fill_mode='wrap',
zoom_range=0.2,
horizontal_flip=True,
vertical_flip=True
)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1./255)
# this is a generator that will read pictures found in
# subfolers of 'data/train', and indefinitely generate
# batches of augmented image data
train_generator = train_datagen.flow_from_directory(
'dataset/train', # this is the target directory
target_size=(96, 128), # all images will be resized to 150x150
batch_size=batch_size,
class_mode='binary') # since we use binary_crossentropy loss, we need binary labels
# this is a similar generator, for validation data
validation_generator = test_datagen.flow_from_directory(
'dataset/validation',
target_size=(96, 128),
batch_size=batch_size,
class_mode='binary')
##############
# Fit model
##############
model.fit_generator(
train_generator,
steps_per_epoch=2000 // batch_size,
epochs=30,
validation_data=validation_generator,
validation_steps=800 // batch_size)
model.save('model.h5') # always save your weights after training or during training
tfjs.converters.save_keras_model(model, './')
##############
# Predict class
##############
img = load_img('./dataset/validation/dog/image001.png')
if (img.size == (96, 128)):
img = img.rotate(90, expand=True)
x = img_to_array(img) # this is a Numpy array with shape (3, 150, 150)
x = x / 255
x = x.reshape((1,) + x.shape) # this is a Numpy array with shape (1, 3, 150, 150)
model.predict(x, batch_size=None, verbose=0, steps=None)
我應該如何修復代碼以生成我期望的(兩個數字)?
感謝 Matias 和 amityadav,我解決了這個問題。 我不得不進行以下更改。
我的最終代碼如下所示
from keras.models import Sequential
from keras.layers import Activation, Dense, Dropout, Flatten, Conv2D, MaxPooling2D
from keras.utils import np_utils
from sklearn.datasets import fetch_mldata
from keras.datasets import mnist
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import tensorflowjs as tfjs
##############
# Train model
##############
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(96, 128, 3), data_format="channels_last"))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Dense(2))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
################################
# Read image data from directory
################################
batch_size = 16
# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
rescale=1./255,
shear_range=0.2,
fill_mode='wrap',
zoom_range=0.2,
horizontal_flip=True,
vertical_flip=True
)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1./255)
# this is a generator that will read pictures found in
# subfolers of 'data/train', and indefinitely generate
# batches of augmented image data
train_generator = train_datagen.flow_from_directory(
'dataset/train', # this is the target directory
target_size=(96, 128), # all images will be resized to 150x150
batch_size=batch_size,
class_mode='categorical') # since we use binary_crossentropy loss, we need binary labels
# this is a similar generator, for validation data
validation_generator = test_datagen.flow_from_directory(
'dataset/validation',
target_size=(96, 128),
batch_size=batch_size,
class_mode='categorical')
##############
# Fit model
##############
model.fit_generator(
train_generator,
steps_per_epoch=2000 // batch_size,
epochs=30,
validation_data=validation_generator,
validation_steps=800 // batch_size)
model.save('model.h5') # always save your weights after training or during training
tfjs.converters.save_keras_model(model, './')
##############
# Predict class
##############
img = load_img('./dataset/validation/dog/image001.png')
if (img.size == (96, 128)):
img = img.rotate(90, expand=True)
x = img_to_array(img) # this is a Numpy array with shape (3, 150, 150)
x = x / 255
x = x.reshape((1,) + x.shape) # this is a Numpy array with shape (1, 3, 150, 150)
model.predict(x, batch_size=None, verbose=0, steps=None)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.