ResNet50基于ImageNet数据集给图片打标签

# -*- coding: UTF-8 -*-
# -------------------------------------------
# 任 务:利用ResNet50网络进行ImageNet分类
# 数 据:网上下载的测试图片‘elephant.jpg’
# -------------------------------------------
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
import os
import tensorflow as tf

os.environ["CUDA_VISIBLE_DEVICES"] = "6"
# gpu_options = tf.GPUOptions(allow_growth=True)
gpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=0.333)

sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options))
# from keras.utils import plot_model
# from matplotlib import pyplot as plt

# 【0】ResNet50模型,加载预训练权重
model = ResNet50(weights='imagenet')
print(model.summary())  # 打印模型概况
# plot_model(model,to_file = 'a simple convnet.png')  # 画出模型结构图,并保存成图片

# 【1】从网上下载一张图片,保存在当前路径下
img_path = './elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))

# 【2】显示图片
# plt.imshow(img)
# plt.show()

# 【3】将图片转化为4d tensor形式
x = image.img_to_array(img)
print(x.shape)  # (224, 224, 3)
x = np.expand_dims(x, axis=0)
print(x.shape)  # (1, 224, 224, 3)

# 【4】数据预处理
"""
def preprocess_input(x, data_format=None, mode='caffe'):
   Preprocesses a tensor or Numpy array encoding a batch of images.

    # Arguments
        x: Input Numpy or symbolic tensor, 3D or 4D.
        data_format: Data format of the image tensor/array.
        mode: One of "caffe", "tf".
            - caffe: will convert the images from RGB to BGR,
                then will zero-center each color channel with
                respect to the ImageNet dataset,
                without scaling.
            - tf: will scale pixels between -1 and 1,
                sample-wise.

    # Returns
        Preprocessed tensor or Numpy array.

    # Raises
        ValueError: In case of unknown `data_format` argument.
"""
x = preprocess_input(x)  # 去均值中心化,preprocess_input函数详细功能见注释

# 【5】测试数据
preds = model.predict(x)
print(preds.shape)  # (1,1000)

# 【6】将测试结果解码为如下形式:
# [(class1, description1, prob1),(class2, description2, prob2)...]
print('Predicted:', decode_predictions(preds, top=10)[0])
# 'Predicted:', [(u'n02504458', u'African_elephant', 0.8791098), (u'n02504013', u'Indian_elephant', 0.066597864), (u'n01871265', u'tusker', 0.054188617)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值