zoukankan      html  css  js  c++  java
  • 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. 

  • 相关阅读:
    668. Kth Smallest Number in Multiplication Table
    658. Find K Closest Elements
    483. Smallest Good Base
    475. Heaters
    454. 4Sum II
    441. Arranging Coins
    436. Find Right Interval
    410. Split Array Largest Sum
    392. Is Subsequence
    378. Kth Smallest Element in a Sorted Matrix
  • 原文地址:https://www.cnblogs.com/leoking01/p/8058310.html
Copyright © 2011-2022 走看看