Keras model.predict возвращает только одну цифру для задачи двоичной классификации - PullRequest
0 голосов
/ 23 марта 2019

Я обучил заданию бинарной классификации с Keras согласно этой инструкции "https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html".

Однако, model.predict возвращает только одну цифру, такую ​​как [[0.6343]]. Я думаю, что он должен вернуть две цифры, такие как [[0.6343, 0.1245]], где каждая цифра представляет вероятность каждого класса.

Я использую Keras версии 2.2.4 и Tensorflow версии 1.13.1.

Вот мой код.

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 голосов
/ 30 марта 2019

Благодаря Матиасу и Амитьяду я решил эту проблему.Я должен был сделать следующие изменения:

  1. Использовать потерю «categoryorical_crossentropy»
  2. Использовать «softmax» для окончательной активации
  3. Дать «2» для окончательной функции Dense
  4. Используйте 'категорический' для class_mode потока_from_directory

Мой окончательный код выглядит следующим образом

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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...