我为我的一个课程编写了一个图像识别代码。我正在对“好”和“坏”心脏超声图像进行分类。我遇到的问题是分类器总是预测图像“好”。我目前没有太多的图像需要整理,所以准确率只有 50% 左右,但是我不确定为什么机器总是认为图像很好。图片示例:我提供了以下代码:#required imports#using sequential from tensorflow from keras.models import Sequentialfrom keras.layers import Conv2Dfrom keras.layers import MaxPooling2Dfrom keras.layers import Flattenfrom keras.layers import Dense#classification model to be sequentialclassifier = Sequential()classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))classifier.add(MaxPooling2D(pool_size = (2, 2)))classifier.add(Flatten())classifier.add(Dense(units = 128, activation = 'relu'))#output layerclassifier.add(Dense(units = 1, activation = 'sigmoid'))#compilationclassifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])#trainingfrom keras.preprocessing.image import ImageDataGeneratortrain_datagen = ImageDataGenerator(rescale = 1./255, shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True)test_datagen = ImageDataGenerator(rescale = 1./255)training_set = train_datagen.flow_from_directory("/home/jovyan/dataset/training_set/", target_size = (64, 64), batch_size = 32, class_mode = 'binary')test_set = test_datagen.flow_from_directory("/home/jovyan/dataset/test_set/", target_size = (64, 64), batch_size = 32, class_mode = 'binary')classifier.fit_generator(training_set, steps_per_epoch = 85, epochs = 25, validation_data=test_set, validation_steps=2000)#predictionsimport numpy as npfrom keras.preprocessing import imagetest_image=image.load_img("/home/jovyan/dataset/test_set/test_bad_1.jpg", target_size=(64, 64))test_image=image.img_to_array(test_image)test_image=np.expand_dims(test_image, axis=0)result=classifier.predict(test_image)training_set.class_indicesif result[0][0]==1: prediction='good'else: prediction='bad'print(prediction)`
2 回答
鸿蒙传说
TA贡献1865条经验 获得超7个赞
该test_image
需求由1./255一样被重新调整train_datagen
和test_datagen
。由于“好”在有更多白色区域时出现,因此较高的值test_image
很可能使输出激活始终为“好”。
添加回答
举报
0/150
提交
取消