Keras之inception_v3使用(单张图片测试)

本文详细介绍如何使用Inception_v3深度学习模型进行图像分类,包括必要的环境搭建、模型权重下载及安装步骤。通过示例代码展示如何加载图片、预处理输入、运行模型预测并解析结果,最终将分类结果转化为可读的标签。

一、安装

必要:tensorflow,Keras

首次运行需要安装:

1)下载模型权重   inception_v3_weights_tf_dim_ordering_tf_kernels.h5  

路径见前一篇

2)安装h5py

pip install h5py

3)安装PIL

 遇到pip无法安装,以pillow替代,见Stack Overflow

 

二、参数说明

 

分类结果:

ImageNet的1000种object,对应模型分类结果的1000 classes:

text: imagenet 1000 class id to human readable labels

https://github.com/cjyanyi/keras_deep_learning_tutorial/blob/master/imagenet1000_clsid_to_human.txt

 

三、代码示例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

import numpy as np

from keras.preprocessing import image

from keras.applications import inception_v3

 

   img = image.load_img("xxx.jpg", target_size=(299299))

   input_image = image.img_to_array(img)

   input_image /= 255.

   input_image -= 0.5

   input_image *= 2.

   # Add a 4th dimension for batch size (Keras)

   input_image = np.expand_dims(input_image, axis=0)

 

    # Run the image through the NN

    predictions = model.predict(input_image)

 

    # Convert the predictions into text

    predicted_classes = inception_v3.decode_predictions(predictions, top=1)

    imagenet_id, name, confidence = predicted_classes[0][0]

    print("This is a {} with {:-4}% confidence!".format(name, confidence * 100))

  

input_image 是一个默认大小:1*299*299*3  的4维向量(列表) 

 

分类: machine learning

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值