Keras model.predict对于二进制分类任务仅返回一个数字

时间:2019-03-23 00:52:24

标签: python tensorflow keras deep-learning

我已经按照“ 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)

我应该如何修复代码以生成期望的结果(两个数字)?

1 个答案:

答案 0 :(得分:0)

感谢Matias和amityadav,我解决了这个问题。 我不得不追赶。

  1. 使用'categorical_crossentropy'损失
  2. 使用“ softmax”进行最终激活
  3. 将'2'赋予最终的Dense函数
  4. 将'categorical'用于flow_from_directory的class_mode

我的最终代码如下:

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)