tensorflow 第一课没有分类,有什么问题?

t8e9dugd  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(40)

我在一个项目中使用了深度学习,在训练了一个模型后,我使用X_test & y_test来验证模型,准确率超过99%。然而,当我使用TFLite压缩模型,然后使用解释器进行推理时,我的混淆矩阵和分类报告的输出令我感到惊讶,因为只有“0”类被分类,而“1”没有被分类。请,有人可以帮助我,我需要做什么,才能分类2类(0和1)。这是我使用的代码

data.head(5)
         V1        V2          V3         V4     Labels
0   -0.253908   0.088630    0.247481    -0.358719   0
1   -0.153446   0.676119    0.390619    -0.303980   0
2   -0.153728   0.676059    0.390683    -0.303741   0
3   -0.146797   0.670025    0.405856    -0.302517   0
4   -0.253907   0.088625    0.247481    -0.358713   0

data.Labels.value_counts()

Out[] 1    128025
      0     97686
      Name: Labels, dtype: int64

y = data[['Labels']]
X = data.drop(['Labels'], axis=1)

y.shape, X.shape

Out[] ((225711, 1), (225711, 4))

# spliting the dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
X_train.shape, y_train.shape, X_test.shape, y_test.shape

Out[] ((151226, 4), (151226, 1), (74485, 4), (74485, 1))

model = Sequential()
model.add(Dense(12, input_shape=(4,), activation='relu'))
model.add(Dense(2, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

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

model.fit(X_train, y_train, epochs=150, batch_size=300)

model.evaluate(X_test, y_test)

       2328/2328 [==============================] - 3s 1ms/step - loss: 0.0125 - accuracy: 0.9971
Out[] [0.01249629631638527, 0.9971001148223877]

converter = tf.lite.TFLiteConverter.from_saved_model(r"C:\Users\44759\df3")
tflite_model = converter.convert()

converter = tf.lite.TFLiteConverter.from_saved_model(r"C:\Users\44759\df3")
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_quant_model = converter.convert()

q_aware_model.evaluate(X_test, y_test)

      2328/2328 [==============================] - 4s 2ms/step - loss: 0.0133 - accuracy: 0.9968
Out[] [0.013261106796562672, 0.9967510104179382]

#Check Input Tensor Shape
interpreter = tf.lite.Interpreter(model_path = "tflite_qaware_model.tflite")
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
print("Input Shape:", input_details[0]['shape'])
print("Input Type:", input_details[0]['dtype'])
print("Output Shape:", output_details[0]['shape'])
print("Output Type:", output_details[0]['dtype'])

Input Shape: [1 4]
Input Type: <class 'numpy.float32'>
Output Shape: [1 1]
Output Type: <class 'numpy.float32'>

#Resize Tensor Shape
interpreter.resize_tensor_input(input_details[0]['index'], (74485, 4))
interpreter.resize_tensor_input(output_details[0]['index'], (74485, 1))
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
print("Input Shape:", input_details[0]['shape'])
print("Input Type:", input_details[0]['dtype'])
print("Output Shape:", output_details[0]['shape'])
print("Output Type:", output_details[0]['dtype'])

Input Shape: [74485     4]
Input Type: <class 'numpy.float32'>
Output Shape: [74485     1]
Output Type: <class 'numpy.float32'>

X_test_numpy = np.array(X_test, dtype=np.float32)

interpreter.set_tensor(input_details[0]['index'], X_test_numpy)
interpreter.invoke()
tflite_model_predictions = interpreter.get_tensor(output_details[0]['index'])
print("Prediction results shape:", tflite_model_predictions.shape)
prediction_classes = np.argmax(tflite_model_predictions, axis=1)

Prediction results shape: (74485, 1)

confusion_mat = confusion_matrix(y_test, prediction_classes)
print(confusion_mat)

[[32302     0]
 [42183     0]]

print(classification_report(prediction_classes, y_test))

               precision    recall  f1-score   support

           0       1.00      0.43      0.60     74485
           1       0.00      0.00      0.00         0

    accuracy                           0.43     74485
   macro avg       0.50      0.22      0.30     74485
weighted avg       1.00      0.43      0.60     74485
huwehgph

huwehgph1#

我找到了解决办法。只有“0”标签类别被分类的问题来自此行prediction_classes = np.argmax(tflite_model_predictions, axis=1)。显然,argmax()函数返回的是最大值的索引,这不是我想要的。显然,当我打印tflite_model_predictions时,值是0., 0.99945, 0.99945, 0.99945, 0., 0.99945, ...。因此,我必须忽略prediction_classes = np.argmax(tflite_model_predictions, axis=1)行,并使用

p = np.where(tflite_model_predictions)>0.5,1,0
confusion_mat = confusion_matrix(p, y_test)
print(confusion_mat)

相关问题