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

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

相关推荐
IT_陈寒19 分钟前
React 18实战:7个被低估的Hooks技巧让你的开发效率提升50%
前端·人工智能·后端
数据智能老司机1 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
逛逛GitHub1 小时前
飞书多维表“独立”了!功能强大的超出想象。
人工智能·github·产品
机器之心2 小时前
刚刚,DeepSeek-R1论文登上Nature封面,通讯作者梁文锋
人工智能·openai
数据智能老司机2 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机2 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机2 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i3 小时前
drf初步梳理
python·django
每日AI新事件3 小时前
python的异步函数
python
这里有鱼汤4 小时前
miniQMT下载历史行情数据太慢怎么办?一招提速10倍!
前端·python