我使用的是keras神经网络用于识别该数据所属的类别。
self.model.compile(loss='categorical_crossentropy',
optimizer=keras.optimizers.Adam(lr=0.001, decay=0.0001),
metrics=[categorical_accuracy])
拟合函数
history = self.model.fit(self.X,
{'output': self.Y},
validation_split=0.3,
epochs=400,
batch_size=32
)
我感兴趣的是找出哪些标签中得到验证步骤错误分类。似乎是一个很好的方式来了解什么是引擎盖下发生。
您可以使用model.predict_classes(validation_data)
得到的预测类的验证数据,并比较这些预测与实际的标签,找出该模型是错误的。事情是这样的:
predictions = model.predict_classes(validation_data)
wrong = np.where(predictions != Y_validation)
如果你有兴趣在“引擎盖下”看,我建议使用
model.predict(validation_data_x)
看到分数为每个类,用于验证组中的每个观察。这应该提供一些线索哪些类别的模式是不是在分类那么好。预测最后一类的方法是
scores = model.predict(validation_data_x)
preds = np.argmax(scores, axis=1)
一定要使用正确的轴为np.argmax
(我假设你的观察轴为1)。使用preds到再与真正的类进行比较。
此外,作为另一项探索是你想看到这个数据集的整体精度,使用
model.evaluate(x=validation_data_x, y=validation_data_y)
我结束了创建它打印在每次迭代的“表现最差的类ID +分数”的度量。从link思路
import tensorflow as tf
import numpy as np
class MaxIoU(object):
def __init__(self, num_classes):
super().__init__()
self.num_classes = num_classes
def max_iou(self, y_true, y_pred):
# Wraps np_max_iou method and uses it as a TensorFlow op.
# Takes numpy arrays as its arguments and returns numpy arrays as
# its outputs.
return tf.py_func(self.np_max_iou, [y_true, y_pred], tf.float32)
def np_max_iou(self, y_true, y_pred):
# Compute the confusion matrix to get the number of true positives,
# false positives, and false negatives
# Convert predictions and target from categorical to integer format
target = np.argmax(y_true, axis=-1).ravel()
predicted = np.argmax(y_pred, axis=-1).ravel()
# Trick from torchnet for bincounting 2 arrays together
# https://github.com/pytorch/tnt/blob/master/torchnet/meter/confusionmeter.py
x = predicted + self.num_classes * target
bincount_2d = np.bincount(x.astype(np.int32), minlength=self.num_classes**2)
assert bincount_2d.size == self.num_classes**2
conf = bincount_2d.reshape((self.num_classes, self.num_classes))
# Compute the IoU and mean IoU from the confusion matrix
true_positive = np.diag(conf)
false_positive = np.sum(conf, 0) - true_positive
false_negative = np.sum(conf, 1) - true_positive
# Just in case we get a division by 0, ignore/hide the error and set the value to 0
with np.errstate(divide='ignore', invalid='ignore'):
iou = false_positive / (true_positive + false_positive + false_negative)
iou[np.isnan(iou)] = 0
return np.max(iou).astype(np.float32) + np.argmax(iou).astype(np.float32)
〜 用法:
custom_metric = MaxIoU(len(catagories))
self.model.compile(loss='categorical_crossentropy',
optimizer=keras.optimizers.Adam(lr=0.001, decay=0.0001),
metrics=[categorical_accuracy, custom_metric.max_iou])