用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可以显著提升图像识别模型的性能,但同时也要注意合理使用和遵守相关规定。

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

相关推荐
Ankie Wan1 分钟前
notepad++技巧:查找和替换:扩展 or 正则表达式
python·正则表达式·notepad++
带娃的IT创业者1 分钟前
《AI大模型趣味实战》智能Agent和MCP协议的应用实例:搭建一个能阅读DOC文件并实时显示润色改写过程的Python Flask应用
人工智能·python·flask
一只韩非子6 分钟前
什么是MCP?为什么引入MCP?(通俗易懂版)
人工智能·aigc·mcp
JavaEdge在掘金9 分钟前
启动nginx报错,80 failed (97: Address family not supported by protocol)
python
新智元10 分钟前
毛骨悚然!o3 精准破译照片位置,只靠几行 Python 代码?人类在 AI 面前已裸奔
人工智能·openai
纪元A梦16 分钟前
华为OD机试真题——绘图机器(2025A卷:100分)Java/python/JavaScript/C++/C/GO最佳实现
java·javascript·c++·python·华为od·go·华为od机试题
钢铁男儿19 分钟前
C# 深入理解类:面向对象编程的核心数据结构
开发语言·数据结构·c#
程序员小远28 分钟前
接口测试和单元测试详解
自动化测试·软件测试·python·测试工具·单元测试·测试用例·接口测试
Tech Synapse37 分钟前
电商商品推荐系统实战:基于TensorFlow Recommenders构建智能推荐引擎
人工智能·python·tensorflow
帅帅的Python38 分钟前
2015-2023 各省 GDP 数据,用QuickBI 进行数据可视化——堆叠图!
大数据·人工智能