如何使CNN预测函数输出为二进制数(0或1)?

发布于 2025-02-04 09:54:40 字数 1367 浏览 3 评论 0原文

我使用带有keras的CNN模型进行图像二进制分类,在最终预测部分,我在下面定义了这样的功能以输出预测结果:

model = keras.Sequential()
model.add(Conv2D(filters = 64, kernel_size = (3, 3), activation = 'relu', input_shape = ((256,256,3))))
model.add(MaxPooling2D(pool_size = (2, 2), strides=(2, 2)))
model.add(Conv2D(filters = 128, kernel_size = (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2), strides=(2, 2)))
model.add(Conv2D(filters = 256, kernel_size = (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(units = 512, activation = 'relu'))
model.add(Dense(units = 1,activation='sigmoid'))

model.compile(optimizer='adam', 
                      loss=tf.keras.losses.BinaryCrossentropy(),
                      metrics=['accuracy'])

history = model.fit(
    train_ds,
    validation_data=valid_ds,
    epochs=10)

def testing_image(image_directory):
    test_image = image.load_img(image_directory, target_size = (256, 256))
    test_image = image.img_to_array(test_image)
    test_image = np.expand_dims(test_image, axis = 0)
    result = model.predict(test_image)
    print(result)

testing_image('/content/drive/MyDrive/testing/01.jpg')

输出是:

[[0.4733843]]

输出始终是小数数字,但我希望输出输出结果结果。只有 01,而没有数组表示。

任何帮助都将受到赞赏。

I used the CNN model with Keras to make an image binary classification, during the final prediction part, I defined such function below to output the prediction result:

model = keras.Sequential()
model.add(Conv2D(filters = 64, kernel_size = (3, 3), activation = 'relu', input_shape = ((256,256,3))))
model.add(MaxPooling2D(pool_size = (2, 2), strides=(2, 2)))
model.add(Conv2D(filters = 128, kernel_size = (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2), strides=(2, 2)))
model.add(Conv2D(filters = 256, kernel_size = (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(units = 512, activation = 'relu'))
model.add(Dense(units = 1,activation='sigmoid'))

model.compile(optimizer='adam', 
                      loss=tf.keras.losses.BinaryCrossentropy(),
                      metrics=['accuracy'])

history = model.fit(
    train_ds,
    validation_data=valid_ds,
    epochs=10)

def testing_image(image_directory):
    test_image = image.load_img(image_directory, target_size = (256, 256))
    test_image = image.img_to_array(test_image)
    test_image = np.expand_dims(test_image, axis = 0)
    result = model.predict(test_image)
    print(result)

testing_image('/content/drive/MyDrive/testing/01.jpg')

The output is:

[[0.4733843]]

The output is always a decimal number, but I want the output the result as only
0or 1 and without the array representation.

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话里做英雄 2025-02-11 09:54:40

sigmoid 激活功能返回0至1之间的值值< 0.5意味着零类(0)和> 0.5意味着二进制分类中的一(1)类。

要获取这些二进制数字,您需要在testing_image()中添加一行代码,如下:

固定代码:

def testing_image(image_directory):
    test_image = image.load_img(image_directory, target_size = (256, 256))
    test_image = image.img_to_array(test_image)
    test_image = np.expand_dims(test_image, axis = 0)
#Changes in code
    pred = model.predict(test_image)
    result = np.where(pred > 0.5, 1, 0) #<--to get the binary category
    print(result)
testing_image('/content/drive/MyDrive/testing/01.jpg')

Sigmoid activation function returns the values between 0 to 1 where the values <0.5 implies to category zero(0) and >0.5 implies to category one(1) in binary classification.

To get these binary numbers, you need to add one more line of code in testing_image() as below:

Fixed code:

def testing_image(image_directory):
    test_image = image.load_img(image_directory, target_size = (256, 256))
    test_image = image.img_to_array(test_image)
    test_image = np.expand_dims(test_image, axis = 0)
#Changes in code
    pred = model.predict(test_image)
    result = np.where(pred > 0.5, 1, 0) #<--to get the binary category
    print(result)
testing_image('/content/drive/MyDrive/testing/01.jpg')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文