用keras识别狗狗

一、需求场景

从照片从识别出狗狗

python 复制代码
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np

# 加载预训练的ResNet50模型
model = ResNet50(weights='imagenet')
def recognize_dog(img_path):
    # 加载图片并调整尺寸为224x224
    img = image.load_img(img_path, target_size=(224, 224))
    # 将图片转换为numpy数组
    x = image.img_to_array(img)
    # 扩展维度以适应模型的输入要求
    x = np.expand_dims(x, axis=0)
    # 预处理图片
    x = preprocess_input(x)
    # 使用模型进行预测
    preds = model.predict(x)
    # 解码预测结果,得到前3个最可能的类别及其概率
    results = decode_predictions(preds, top=3)[0]
    # 检查是否包含"dog"
    for result in results:
        print(result)

# 测试
print(recognize_dog('e:\\testImage\dog1.jpg'))
print(recognize_dog('e:\\testImage\dog2.jpg'))

"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\python.exe" E:\PycharmProjects\pythonProject\test20240503-001.py

2024-05-03 13:09:11.097463: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.

2024-05-03 13:09:11.755791: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.

2024-05-03 13:09:13.229779: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.

To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.

1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 1s/step

('n02099712', 'Labrador_retriever', 0.755565)

('n02109047', 'Great_Dane', 0.15936635)

('n02099267', 'flat-coated_retriever', 0.059484843)

None

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 78ms/step

('n02113186', 'Cardigan', 0.65025693)

('n02113023', 'Pembroke', 0.34579375)

('n02115641', 'dingo', 0.0023858186)

None

Process finished with exit code 0

二、知识点Keras

Keras是一个高级的神经网络框架,它用Python编写,旨在实现快速的实验和原型设计

解释说明

  • Keras是为了简化神经网络的构建和训练过程而设计的,它提供了一种简洁、直观的方式让用户能够轻松地定义和训练深度学习模型。
  • 作为一个高级API,Keras可以运行在多个后端之上,包括TensorFlow、Microsoft CNTK和Theano。这意味着用户可以利用这些后端引擎的强大功能,同时享受Keras提供的简洁和易用性。
  • Keras支持卷积神经网络(CNNs)和循环神经网络(RNNs),这两种网络是深度学习中非常重要的结构,广泛应用于图像识别、语音识别等领域。

使用示例

  • 安装Keras可以通过pip命令进行:pip install keras
  • 一个简单的Keras模型定义和训练的例子可能如下:
python 复制代码
from keras.models import Sequential
from keras.layers import Dense

model = Sequential()
model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5, batch_size=32)

Keras是一个强大的工具,特别适合那些希望快速开发和实验深度学习模型的用户。它的设计理念是简化复杂性,使得初学者和非专家也能够轻松上手深度学习项目。

三、知识点ImageNet

ImageNet是一个用于计算机视觉识别项目的庞大数据库,它对图像识别和机器视觉领域的研究起到了重要作用。

解释说明

  • ImageNet由美国斯坦福大学的计算机科学家创建,旨在模拟人类的视觉识别系统,以识别和分类图片中的物体。
  • 它是目前世界上最大的图像识别数据库之一,包含超过1400万张手动注释的图像,涵盖了2万多个类别。
  • 数据集是根据WordNet的层次结构组织起来的,每个节点或类别都包含了一系列相关的图像。
  • 在至少一百万个图像中,除了标注了物体类别,还提供了边界框信息,这对于目标检测任务尤其重要。

ImageNet作为一个计算机视觉领域的重要资源,为图像识别和机器学习提供了宝贵的数据支持。正确使用ImageNet可以显著提升图像识别模型的性能,但同时也要注意合理使用和遵守相关规定。

机器之魂,人工智能,无所不能,充满神奇。

相关推荐
万物智能信息科技1 分钟前
PWM散热风扇设置—【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
人工智能·华为·开源·harmonyos·鸿蒙
catcatuncle4 分钟前
读了一个开源 AI Agent 的源码后,我重新思考了"文件验收"这件事
人工智能
二川bro5 分钟前
Agent安全执行命令:命令分级+Hook+读写锁
人工智能
AI天行健5 分钟前
AI视频热缓存命中率89%:无限画布Vera1.1调参方案与数据验证
人工智能·ai
VXbishe6 分钟前
【课程设计】基于SpringBoot的乡村政务服务系统的设计与实现-计算机毕设77011
javascript·vue.js·spring boot·python·php·课程设计·政务
Fang_YuanAI8 分钟前
AIGC原生IP到底应该怎么做?
人工智能·ai·aigc·mcn·ai短剧·ip孵化·aigc创作
IamZJT_8 分钟前
01|先跑起来:用大模型和一个查询工具处理文字工单
人工智能
星火10249 分钟前
【从 0 到 1 动手造 Agent】(组件篇)06、向量库:让 Agent 按「意思」找东西
人工智能·agent
IamZJT_11 分钟前
Agent 系统工程 03|工具执行成功但回执丢了,重试还是不重试?
人工智能
名字还没想好☜11 分钟前
Python sqlite3 实战:事务提交、参数化防注入、WAL 并发与 row_factory 取字典
后端·python·编程语言