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

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

相关推荐
兔子小灰灰5 小时前
jetson安装pytorch
人工智能·pytorch·python
weilaikeqi11115 小时前
拙诚育泽携手澳仕玛,夯实青少年AI科技竞争力
人工智能·科技
运维行者_5 小时前
OPM 与传统管理工具的区别,在网络修复与自动化运维方面的优势在哪里?
运维·服务器·开发语言·网络·自动化·php·ssl
卷心菜加农炮6 小时前
MPK(Mirage Persistent Kernel)源码笔记(3)--- 系统接口
人工智能
神算大模型APi--天枢6466 小时前
合规与高效兼得:国产全栈架构赋能行业大模型定制,从教育到工业的轻量化落地
大数据·前端·人工智能·架构·硬件架构
广州灵眸科技有限公司6 小时前
瑞芯微(EASY EAI)RV1126B 音频输入
linux·开发语言·网络·音视频
Swizard6 小时前
拒绝“裸奔”上线:FastAPI + Pytest 自动化测试实战指南
python
Coding茶水间6 小时前
基于深度学习的学生上课行为检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
图像处理·人工智能·深度学习·yolo·目标检测·机器学习·计算机视觉
吃喝不愁霸王餐APP开发者6 小时前
基于Spring Cloud Gateway实现对外卖API请求的统一鉴权与流量染色
java·开发语言
Channing Lewis6 小时前
脑机智能会成为意识迁移的过渡形态吗
人工智能