张量模型形状问题

2024-04-26 07:35:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用EfficientB5制作糖尿病视网膜病变的深度学习算法。我从功能强大的计算机上获取模型权重,并希望进行预测。 所以首先

    from efficientnet.keras import EfficientNetB5
        
    from keras.models import Sequential
    from keras import layers
    from keras.optimizers import Adam
    
    model = Sequential()
    model.add(EfficientNetB5(weights='imagenet',include_top=False, input_shape=(400,400,3)))
    model.add(layers.GlobalAveragePooling2D())
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(5,activation = 'sigmoid'))
    
    model.compile(loss='binary_crossentropy',optimizer=Adam(lr=0.00005),metrics=['accuracy'])
    model.load_weights('400weight.h5')

第二,我需要优化我的测试图片

image = "dr.jpg"
image = cv2.imread(path + "dr.jpg")
image = cv2.resize(image,(400,400))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
kopya = image.copy()
kopya = cv2.cvtColor(kopya,cv2.COLOR_RGB2GRAY)
blur = cv2.GaussianBlur(kopya,(5,5),0)
thresh = cv2.threshold(blur,10,255,cv2.RETR_EXTERNAL)[1]
kontur = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
kontur = kontur[0][0]
kontur = kontur[:,0,:]
x1 = tuple(kontur[kontur[:,0].argmin()])[0]
y1 = tuple(kontur[kontur[:,1].argmin()])[1]
x2 = tuple(kontur[kontur[:,0].argmax()])[0]
y2 = tuple(kontur[kontur[:,1].argmax()])[1]
x = int(x2-x1)*4//50
y = int(y2-y1)*5//50
kopya2 = image.copy()
if x2-x1 > 100 and y2-y1 > 100:
    kopya2 = kopya2[y1+y : y2-y, x1+x : x2-x]
    kopya2 = cv2.resize(kopya2, (400,400))
lab = cv2.cvtColor(kopya2, cv2.COLOR_RGB2LAB)
l,a,b = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=5.0,tileGridSize=((8,8)))
cl = clahe.apply(l)
limg = cv2.merge((cl,a,b))
son = cv2.cvtColor(limg, cv2.COLOR_LAB2RGB)
med_son = cv2.medianBlur(son,3)
arka_plan = cv2.medianBlur(son,37)
maske = cv2.addWeighted(med_son,1,arka_plan,-1,255)
son_img = cv2.bitwise_and(maske,med_son)
img_list.append(son_img)

当我试图预测

result = model.predict(son_img)

我犯了一个错误

WARNING:tensorflow:Model was constructed with shape (None, 400, 400, 3) for input Tensor("efficientnet-b5_input:0", shape=(None, 400, 400, 3), dtype=float32), but it was called on an input with incompatible shape (None, 400, 3).
WARNING:tensorflow:Model was constructed with shape (None, 400, 400, 3) for input Tensor("input_1:0", shape=(None, 400, 400, 3), dtype=float32), but it was called on an input with incompatible shape (None, 400, 3).

我应该如何处理这个错误,以及如何获得带有图片的预测输出