keras----resnet-vgg-xception-inception
来源:
https://www.pyimagesearch.com/2017/03/20/imagenet-vggnet-resnet-inception-xception-keras/
classify_image.py
#encoding:utf8
import keras # import the necessary packages
from keras.applications import ResNet50
from keras.applications import InceptionV3
from keras.applications import Xception # TensorFlow ONLY
from keras.applications import VGG16
from keras.applications import VGG19
from keras.applications import imagenet_utils
from keras.applications.inception_v3 import preprocess_input
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import load_img
import numpy as np
import argparse
import cv2 print "hello, keras. " # construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to the input image")
ap.add_argument("-model", "--model", type=str, default="vgg16",
help="name of pre-trained network to use")
args = vars(ap.parse_args()) # define a dictionary that maps model names to their classes
# inside Keras
MODELS = {
"vgg16": VGG16,
"vgg19": VGG19,
"inception": InceptionV3,
"xception": Xception, # TensorFlow ONLY
"resnet": ResNet50
} # esnure a valid model name was supplied via command line argument
if args["model"] not in MODELS.keys():
raise AssertionError("The --model command line argument should "
"be a key in the `MODELS` dictionary") # initialize the input image shape (224x224 pixels) along with
# the pre-processing function (this might need to be changed
# based on which model we use to classify our image)
inputShape = (224, 224)
preprocess = imagenet_utils.preprocess_input # if we are using the InceptionV3 or Xception networks, then we
# need to set the input shape to (299x299) [rather than (224x224)]
# and use a different image processing function
if args["model"] in ("inception", "xception"):
inputShape = (299, 299)
preprocess = preprocess_input # Net, ResNet, Inception, and Xception with KerasPython # import the necessary packages
# from keras.applications import ResNet50
# from keras.applications import InceptionV3
# from keras.applications import Xception # TensorFlow ONLY
# from keras.applications import VGG16
# from keras.applications import VGG19
# from keras.applications import imagenet_utils
# from keras.applications.inception_v3 import preprocess_input
# from keras.preprocessing.image import img_to_array
# from keras.preprocessing.image import load_img
# import numpy as np
# import argparse
# import cv2 # construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to the input image")
ap.add_argument("-model", "--model", type=str, default="vgg16",
help="name of pre-trained network to use")
args = vars(ap.parse_args()) # define a dictionary that maps model names to their classes
# inside Keras
MODELS = {
"vgg16": VGG16,
"vgg19": VGG19,
"inception": InceptionV3,
"xception": Xception, # TensorFlow ONLY
"resnet": ResNet50
} # esnure a valid model name was supplied via command line argument
if args["model"] not in MODELS.keys():
raise AssertionError("The --model command line argument should "
"be a key in the `MODELS` dictionary") # initialize the input image shape (224x224 pixels) along with
# the pre-processing function (this might need to be changed
# based on which model we use to classify our image)
inputShape = (224, 224)
preprocess = imagenet_utils.preprocess_input # if we are using the InceptionV3 or Xception networks, then we
# need to set the input shape to (299x299) [rather than (224x224)]
# and use a different image processing function
if args["model"] in ("inception", "xception"):
inputShape = (299, 299)
preprocess = preprocess_input # load our the network weights from disk (NOTE: if this is the
# first time you are running this script for a given network, the
# weights will need to be downloaded first -- depending on which
# network you are using, the weights can be 90-575MB, so be
# patient; the weights will be cached and subsequent runs of this
# script will be *much* faster)
print("[INFO] loading {}...".format(args["model"]))
Network = MODELS[args["model"]]
model = Network(weights="imagenet") # load our the network weights from disk (NOTE: if this is the
# first time you are running this script for a given network, the
# weights will need to be downloaded first -- depending on which
# network you are using, the weights can be 90-575MB, so be
# patient; the weights will be cached and subsequent runs of this
# script will be *much* faster)
print("[INFO] loading {}...".format(args["model"]))
Network = MODELS[args["model"]]
model = Network(weights="imagenet") # load the input image using the Keras helper utility while ensuring
# the image is resized to `inputShape`, the required input dimensions
# for the ImageNet pre-trained network
print("[INFO] loading and pre-processing image...")
image = load_img(args["image"], target_size=inputShape)
image = img_to_array(image) # our input image is now represented as a NumPy array of shape
# (inputShape[0], inputShape[1], 3) however we need to expand the
# dimension by making the shape (1, inputShape[0], inputShape[1], 3)
# so we can pass it through thenetwork
image = np.expand_dims(image, axis=0) # pre-process the image using the appropriate function based on the
# model that has been loaded (i.e., mean subtraction, scaling, etc.)
image = preprocess(image) # classify the image
print("[INFO] classifying image with '{}'...".format(args["model"]))
preds = model.predict(image)
P = imagenet_utils.decode_predictions(preds) # loop over the predictions and display the rank-5 predictions +
# probabilities to our terminal
for (i, (imagenetID, label, prob)) in enumerate(P[0]):
print("{}. {}: {:.2f}%".format(i + 1, label, prob * 100)) # load the image via OpenCV, draw the top prediction on the image,
# and display the image to our screen
orig = cv2.imread(args["image"])
(imagenetID, label, prob) = P[0][0]
cv2.putText(orig, "Label: {}, {:.2f}%".format(label, prob * 100),
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
cv2.imshow("Classification", orig)
cv2.waitKey(0) print "finished . all. "
classfy.sh
python classify_image.py --image /home/sea/Downloads/images/a.jpg --model vgg19
1. tobacco_shop: 19.85%
2. confectionery: 12.88%
3. bakery: 11.10%
4. barbershop: 4.98%
5. restaurant: 4.29%
finished . all.
keras----resnet-vgg-xception-inception的更多相关文章
- 比较 VGG, resnet和inception的图像分类效果
简介 VGG, resnet和inception是3种典型的卷积神经网络结构. VGG采用了3*3的卷积核,逐步扩大通道数量 resnet中,每两层卷积增加一个旁路 inception实现了卷积核的并 ...
- 1、VGG16 2、VGG19 3、ResNet50 4、Inception V3 5、Xception介绍——迁移学习
ResNet, AlexNet, VGG, Inception: 理解各种各样的CNN架构 本文翻译自ResNet, AlexNet, VGG, Inception: Understanding va ...
- Keras Xception Multi loss 细粒度图像分类
作者: 梦里茶 如果觉得我的工作对你有帮助,就点个star吧 关于 这是百度举办的一个关于狗的细粒度分类比赛,比赛链接: http://js.baidu.com/ 框架 Keras Tensorflo ...
- CNN Architectures(AlexNet,VGG,GoogleNet,ResNet,DenseNet)
AlexNet (2012) The network had a very similar architecture as LeNet by Yann LeCun et al but was deep ...
- Keras vs. PyTorch in Transfer Learning
We perform image classification, one of the computer vision tasks deep learning shines at. As traini ...
- keras调用预训练模型分类
在网上看到一篇博客,地址https://www.pyimagesearch.com/2017/03/20/imagenet-vggnet-resnet-inception-xception-keras ...
- 转:TensorFlow和Caffe、MXNet、Keras等其他深度学习框架的对比
http://geek.csdn.net/news/detail/138968 Google近日发布了TensorFlow 1.0候选版,这第一个稳定版将是深度学习框架发展中的里程碑的一步.自Tens ...
- ResNeXt——与 ResNet 相比,相同的参数个数,结果更好:一个 101 层的 ResNeXt 网络,和 200 层的 ResNet 准确度差不多,但是计算量只有后者的一半
from:https://blog.csdn.net/xuanwu_yan/article/details/53455260 背景 论文地址:Aggregated Residual Transform ...
- 探索学习率设置技巧以提高Keras中模型性能 | 炼丹技巧
学习率是一个控制每次更新模型权重时响应估计误差而调整模型程度的超参数.学习率选取是一项具有挑战性的工作,学习率设置的非常小可能导致训练过程过长甚至训练进程被卡住,而设置的非常大可能会导致过快学习到 ...
- keras中VGG19预训练模型的使用
keras提供了VGG19在ImageNet上的预训练权重模型文件,其他可用的模型还有VGG16.Xception.ResNet50.InceptionV3 4个. VGG19在keras中的定义: ...
随机推荐
- [AGC008F] Black Radius(树形dp)
神题啊!! Description 给你一棵有N个节点的树,节点编号为1到N,所有边的长度都为1 "全"对某些节点情有独钟,这些他喜欢的节点的信息会以一个长度为N的字符串s的形式给 ...
- Docker(七):仓库
登录 可以通过执行docker login命令来输入用户名和密码,密码和邮箱来完成注册和登录.注册成功之后,本地用户目录的.dockerfig中将保存用户的认证信息. 使用$sudo docker s ...
- java中的序列化与反序列化,还包括将多个对象序列化到一个文件中
package Serialize; /** * Created by hu on 2015/11/7. */ //实现序列化必须实现的接口,这就是一个空接口,起到标识的作用 import java. ...
- [论文]Clustering-Based Ensembles as an Alternative to Stacking
Clustering-Based Ensembles as an Alternative to Stacking 作者:Anna Jurek, Yaxin Bi, Shengli Wu, and Ch ...
- 此时不应有 \Microsoft (转)
原文转自 http://www.tuicool.com/articles/J7RFRz 下载boost库后,在cmd中运行bootstrap.bat ,输出 "此时不应有 \Microsof ...
- [SaltStack] Multi-Master配置
Multi Master配置 从salt 0.16.0版本开始, 管理minions可以使用multi-master特性. 当使用multi-master时, 所有masters均可以正常运行, 任何 ...
- Python 数据类型-2
序列 包括:字符串 列表 元组 索引操作和切片操作 索引操作:可以从序列中抓取一个特定的项目 切片操作: 获取序列的一个切片,即一部分序列 序列的通用方法: len() 求序列的长度 + 连接2个序列 ...
- Python学习杂记_7_文件操作
文件操作 Python3用open()方法打开文件并返回文件句柄,有了文件句柄就可以对文件进行各种操作了. 打开文件: open(“文件名” , 打开方式) 如: f=open( ...
- python3列表推导式和生成器。
1.把一个字符串变成 Unicode 码位的列表 >>> symbols = '$¢£¥€¤' >>> codes = [] >>> for sy ...
- JavaScript闭包 循环输出i
html <body> <p>产品一</p> <p>产品二</p> <p>产品三</p> <p>产品四& ...