请告诉我以下代码的预测是否正确,这是 CNN 的测试代码, 我正在使用 Jupyter、Tensorflow 2.9.0 和 Keras 2.9.0
import math
import os
import numpy as np
import cv2
from keras.models import load_model
from PIL import Image
########### PARAMETERS ##############
threshold = 0.90 # MINIMUM PROBABILITY TO CLASSIFY
#### LOAD THE TRAINNED MODEL
model = load_model('model_trained.h5')
path = r'C:\Users\Issam\AppData\Local\Programs\Python\Python36\Digits-Classification-master\Weed_Detection\MyTestData\test9.jpg'
# Reading an image in default mode and creating subimages
i = Image.open (path)
width, height = i.size
print(width)
print(height)
L=0
T=0
R=width
B=height
imgOriginal= frame1
img = np.asarray(imgOriginal)
img = cv2.resize(img,(200, 200))
img = preProcessing(img)
cv2.imshow("Processed Image",img)
img = img.reshape(1,200,200,1)
frame1 = i.crop(((L, T, R, B/3)))
def preProcessing(img):
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.equalizeHist(img)
img = img / 255
return img
# Predict
predictions = model.predict(img)
classIndex = np.argmax(predictions,axis=1)
probVal = np.amax(predictions)
if probVal > threshold:
if classIndex == 0:
item1 = "No_Weed"
prob1=probVal
print(item1, "Probability: ",probVal)
elif classIndex == 1:
item1 = "Weed"
prob1=probVal
print(item1, "Probability: ",probVal)
模型结果如下
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 196, 196, 60) 1560
conv2d_1 (Conv2D) (None, 192, 192, 60) 90060
max_pooling2d (MaxPooling2D (None, 96, 96, 60) 0
)
conv2d_2 (Conv2D) (None, 94, 94, 30) 16230
conv2d_3 (Conv2D) (None, 92, 92, 30) 8130
max_pooling2d_1 (MaxPooling (None, 46, 46, 30) 0
2D)
dropout (Dropout) (None, 46, 46, 30) 0
flatten (Flatten) (None, 63480) 0
dense (Dense) (None, 500) 31740500
dropout_1 (Dropout) (None, 500) 0
dense_1 (Dense) (None, 2) 1002
=================================================================
Total params: 31,857,482
Trainable params: 31,857,482
Non-trainable params: 0
_________________________________________________________________
None
由于我没有足够的积分来发表评论,因此添加评论作为答案。 如果不真正了解模型的架构(尤其是最后一层),就不可能检查模型的输出。您愿意做一个
print(model.summary())
吗?