加载中…
正文 字体大小:

keras常见CNN网络结构应用

(2017-06-16 19:47:50)
标签:

keras

vgg

resnet

inception

分类: 深度学习
keras vgg16 vgg19 resnet inception模型对比
# -*- coding: utf-8 -*-
# @DATE    : 2017/6/14 14:15
# @Author  : 
# @File    : classify_image.py

import os

import tensorflow as tf

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


MODEL =  {
    "vgg16": VGG16,
    "vgg19": VGG19,
    "inception": InceptionV3,
    "xception": Xception,
    "resnet": ResNet50
}

def load_preprocess_image(model_name, image_path):
    # vgg resnet
    input_shape = (224, 224)
    preprocess_func = imagenet_utils.preprocess_input

    if model_name in ["inception", "xception"]:
        input_shape = (299, 299)
        preprocess_func = preprocess_input

    # load image
    image = load_img(image_path, target_size=input_shape)

    # preprocess image
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    image = preprocess_func(image)

    return image


def image_model(model_name):
    network = MODEL[model_name]
    network = network(weights="imagenet")
    return network

def classify_image(model, image):
    preds = model.predict(image)
    preds = imagenet_utils.decode_predictions(preds)
    return preds


if __name__ == "__main__":
    data_dir = "data"
    image_names = ["ball.jpg"]
    image_paths = [ os.path.join(data_dir, image_name)  for image_name in image_names]
    model_names = ["vgg16", "vgg19", "resnet", "inception", "xception"]
    for image_path in image_paths:
        print("Image: {}".format(os.path.split(image_path)[-1]))
        for model_name in model_names:
            image = load_preprocess_image(model_name, image_path)
            model = image_model(model_name)
            preds = classify_image(model, image)
            print("Model: {}".format(model_name))
            print("Results(Top 5): ")
            for (i, (imagenetID, label, prob)) in enumerate(preds[0]):
                print("{}. {}: {:.2f}%".format(i + 1, label, prob * 100))


运行日志:
keras常见CNN网络结构应用


Using TensorFlow backend.
Image: ball.jpg
2017-06-16 19:50:03.635390: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-16 19:50:03.635405: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-06-16 19:50:03.635408: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-16 19:50:03.635413: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
Model: vgg16
Results(Top 5): 
1. soccer_ball: 93.73%
2. rugby_ball: 5.88%
3. volleyball: 0.13%
4. golf_ball: 0.11%
5. tennis_ball: 0.04%
Model: vgg19
Results(Top 5): 
1. soccer_ball: 98.07%
2. rugby_ball: 1.24%
3. golf_ball: 0.32%
4. volleyball: 0.20%
5. croquet_ball: 0.07%
Model: resnet
Results(Top 5): 
1. soccer_ball: 99.54%
2. rugby_ball: 0.39%
3. volleyball: 0.06%
4. running_shoe: 0.00%
5. football_helmet: 0.00%
Model: inception
Results(Top 5): 
1. soccer_ball: 99.88%
2. volleyball: 0.11%
3. rugby_ball: 0.00%
4. sea_urchin: 0.00%
5. silky_terrier: 0.00%
Model: xception
Results(Top 5): 
1. soccer_ball: 90.85%
2. volleyball: 2.48%
3. rugby_ball: 1.37%
4. balloon: 0.11%
5. airship: 0.10%

参考:http://www.pyimagesearch.com/2017/03/20/imagenet-vggnet-resnet-inception-xception-keras/





0

阅读 评论 收藏 转载 喜欢 打印举报
已投稿到:
  • 评论加载中,请稍候...
发评论

       

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 不良信息反馈 电话:4006900000 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有