【发布时间】:2020-07-07 03:40:02
【问题描述】:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import VGG16
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from keras.applications.vgg16 import decode_predictions
from imutils import paths
from pathlib import path
import numpy as np
import argparse
import cv2
import os
imagePaths = list(paths.list_images('D:/keras-cat-dog/dataset'))
data = []
labels = []
for imagePath in imagePaths:
# extract the class label from the filename
label = imagePath.split(os.path.sep)[-2]
# load the image, swap color channels, and resize it to be a fixed
# 224x224 pixels while ignoring aspect ratio
image = cv2.imread(imagePath)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (224, 224))
# update the data and labels lists, respectively
data.append(image)
labels.append(label)
# convert the data and labels to NumPy arrays while scaling the pixel
# intensities to the range [0, 255]
data = np.array(data) / 255.0
labels = np.array(labels)
# perform one-hot encoding on the labels
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
labels = to_categorical(labels)
# partition the data into training and testing splits using 80% of
# the data for training and the remaining 20% for testing
(trainX, testX, trainY, testY) = train_test_split(data, labels,
test_size=0.20, stratify=labels, random_state=42)
# initialize the training data augmentation object
trainAug = ImageDataGenerator(
rotation_range=15,
fill_mode="nearest")
# load the VGG16 network, ensuring the head FC layer sets are left
# off
baseModel = VGG16(weights="imagenet", include_top=False,
input_tensor=Input(shape=(224, 224, 3)))
# construct the head of the model that will be placed on top of the
# the base model
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(4, 4))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(64, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(2, activation="softmax")(headModel)
# place the head FC model on top of the base model (this will become
# the actual model we will train)
model = Model(inputs=baseModel.input, outputs=headModel)
# loop over all layers in the base model and freeze them so they will
# *not* be updated during the first training process
for layer in baseModel.layers:
layer.trainable = False
INIT_LR = 1e-3
EPOCHS = 25
BS = 8
# compile our model
print("[INFO] compiling model...")
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="binary_crossentropy", optimizer=opt,
metrics=["accuracy"])
# train the head of the network
print("[INFO] training head...")
H = model.fit_generator(
trainAug.flow(trainX, trainY, batch_size=BS),
steps_per_epoch=len(trainX) // BS,
validation_data=(testX, testY),
validation_steps=len(testX) // BS,
epochs=EPOCHS)
# make predictions on the testing set
print("[INFO] evaluating network...")
predIdxs = model.predict(testX, batch_size=BS)
# for each image in the testing set we need to find the index of the
# label with corresponding largest predicted probability
predIdxs = np.argmax(predIdxs, axis=1)
imagePath_1 = os.path.normpath('D:/Classification-master/data_two_class/test/cat/NORMAL2-IM-
1396-0001.jpeg')
label_1=imagePath_1.split(os.sep)[-2]
image_pred = cv2.imread(imagePath)
image_pred = cv2.cvtColor(image_pred, cv2.COLOR_BGR2RGB)
img_pred = cv2.resize(image_pred, (224, 224))
img_pred = np.array(img_pred) / 255.0
rslt = model.predict(img_pred.reshape(1,224,224,3))
#decode_predictions(rslt)
我正在使用上面的代码使用 keras 和 tensorflow 对图像进行分类,但我很难理解预测的标签,因为我已经对它们进行了一次热编码,现在当我实际预测单个图像时,它是显示两个概率的数组。使用 argmax 函数后,我得到一个 0 或 1 并且无法理解它的含义。
In [209]: rslt
Out[209]: array([[0.9550967 , 0.04490325]], dtype=float32)
rslt = np.argmax(rslt)
Out[219]: 0
如果有人能展示一种方法,我可以在数据处理阶段的编码期间看到哪个类标签将变为“0”/“1”以及在验证期间存在哪些类标签(testY ) 以及当我预测单个图像时图像的类标签。
问候,
苏布拉
【问题讨论】:
-
在你用
LabelBinarizer转换它们之后,请发布一个示例:1) 你的初始labels2) 你的labels,而不是所有这些(大部分与问题无关的)代码.了解如何创建minimal reproducible example。
标签: python tensorflow keras deep-learning one-hot-encoding