keras transferlearning预测班级上班和不上班

时间:2020-05-21 06:37:27

标签: python tensorflow machine-learning keras computer-vision

我正在使用转移学习并在“ inception_v3”模型的基础上进行构建。训练似乎进行得不错,我的val_accuracy为0.9526。之后,我也可以进行Forecast_class以获得新样本的预测标签-似乎也不错。 但是,由于某些原因,当我尝试使用预测函数时,它总是输出从0到3.31e-11的极低概率。我认为应该输出0到1范围内的概率? 也许我做错了什么?这是2类问题,这就是我正在做的事情:


import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
from tensorflow.keras.optimizers import Adam
from tensorflow.keras import regularizers

image_size = (128,128)
batch_size = 32

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=0.2,
    subset="training",
    label_mode = 'binary',
    seed = 1337,
    image_size=image_size,
    batch_size=batch_size,
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=0.2,
    subset="validation",
    label_mode = 'binary',
    seed = 1337,
    image_size=image_size,
    batch_size=batch_size,
)


global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
Dl_1 = tf.keras.layers.Dropout(rate = 0.2)
prediction_layer = tf.keras.layers.Dense(1,activation='sigmoid') # 120, softmax

#Add dropout Layer
model_V3 = tf.keras.Sequential([
  pre_trained_model,
  global_average_layer,
  Dl_1,
  prediction_layer
])

model_V3.compile(optimizer='adam',
              loss='binary_crossentropy',#sparse_categorical_crossentropy
              metrics=['accuracy'])

model_V3.summary()

# Callbacks

lr_reduce = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, verbose=2, mode='max')
early_stop = EarlyStopping(monitor='val_loss', min_delta=0.1, patience=1, mode='min')

hist = model_V3.fit(
           augmented_train_ds.repeat(), steps_per_epoch=int(8000/batch_size), 
           epochs=10, validation_data=augmented_val_ds.repeat(), 
           validation_steps=int(2000/batch_size) , callbacks=[lr_reduce])


Ypred = model_V3.predict(val_ds)  # <--- This always returns very low probabilities. Why?

1 个答案:

答案 0 :(得分:0)

在Yoskutik的帮助下,现在的代码如下所示,并且已经得到了改进,但是我仍然不明白为什么我会得到我想要的结果(请参见下面的代码)。

在最后一个时期我得到了这个输出

Epoch 5/5
414/414 [==============================] - ETA: 0s - loss: 0.1207 - accuracy: 0.9587
Epoch 00005: ReduceLROnPlateau reducing learning rate to 0.00020000000949949026.
414/414 [==============================] - 98s 236ms/step - loss: 0.1207 - accuracy: 0.9587 - val_loss: 0.1029 - val_accuracy: 0.9661

因此val_accuracy:0.9661。但是计算出的roc_auc_score显示为0.5150403105096363 混淆矩阵显示为:

[[2094  552]
 [ 545  114]]

具有96%准确性的模型如何给出如此差的结果?

import tensorflow as tf
import os
from tensorflow import keras
from tensorflow.keras import layers
import matplotlib.pyplot as plt
from tensorflow.keras.applications.inception_v3 import InceptionV3
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
from tensorflow.keras.optimizers import Adam
from tensorflow.keras import regularizers
from sklearn.metrics import classification_report, confusion_matrix,roc_auc_score

data_dir = '/path/to_dir/'

image_size = (128,128)
target_size = (128,128)
batch_size = 32

datagen = tf.keras.preprocessing.image.ImageDataGenerator(
    zoom_range=0.2,
    shear_range=0.2,
    rotation_range=10,
    rescale=1/255,
    validation_split=0.2,
    # other things...
)

train_ds = datagen.flow_from_directory(
    data_dir,
    subset="training",
    class_mode='binary',
    target_size=target_size,
    batch_size=batch_size,
)

val_ds = datagen.flow_from_directory(
    data_dir,
    subset="validation",
    class_mode='binary',
    target_size=target_size,
    batch_size=batch_size,
)



pre_trained_model = InceptionV3(input_shape=(128,128,3),
                                include_top=False,
                                weights='imagenet')

for layer in pre_trained_model.layers:
  layer.trainable = False


global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
Dl_1 = tf.keras.layers.Dropout(rate = 0.2)
prediction_layer = tf.keras.layers.Dense(1,activation='sigmoid')

model_V3 = tf.keras.Sequential([
  pre_trained_model,
  global_average_layer,
  Dl_1,
  prediction_layer
])

model_V3.compile(optimizer='adam',
                 loss='binary_crossentropy',
                 metrics=['accuracy'])

lr_reduce = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, verbose=2, mode='max')

hist = model_V3.fit(
    train_ds, 
    epochs=5,
    steps_per_epoch=len(train_ds), 
    validation_data=val_ds, 
    validation_steps=len(val_ds), 
    callbacks=[lr_reduce])

y_score = model_V3.predict(val_ds)
y_pclass = model_V3.predict_classes(val_ds)
print(confusion_matrix(val_ds.classes,y_pclass))
print(roc_auc_score(val_ds.classes, y_score))