美文网首页
KERAS:使用INCEPTIONV3、RESNET50预训练模

KERAS:使用INCEPTIONV3、RESNET50预训练模

作者: 万州客 | 来源:发表于2022-06-27 10:28 被阅读0次

    最近在了解关于CNN的网络结构,找几个实例操作一下。

    预训练,第一次接触~~~,不用训练,直接从网上下载模型来搞。
    一,代码

    from keras.applications.inception_v3 import InceptionV3
    from keras.applications.resnet import ResNet50
    from keras.applications.resnet import preprocess_input,decode_predictions
    from keras.applications.inception_v3 import decode_predictions
    from keras.preprocessing import image
    import numpy as np
    import cv2
    
    model = InceptionV3(weights='imagenet', include_top=True)
    img_path = 'dog1.jpg'
    
    img = image.image_utils.load_img(img_path, target_size=(299, 299))
    img = image.image_utils.img_to_array(img) / 255.0
    img = np.expand_dims(img, axis=0)
    
    predictions = model.predict(img)
    print('Predicted:', decode_predictions(predictions, top=3))
    
    descripton = decode_predictions(predictions, top=3)[0][0][1]
    
    src = cv2.imread(img_path)
    cv2.putText(src, descripton, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)
    cv2.imshow('Predicted', src)
    cv2.waitKey()
    
    print('=========================')
    
    model = ResNet50(weights='imagenet')
    img_path = 'bird.jpg'
    
    img = image.image_utils.load_img(img_path, target_size=(224, 224))
    img = image.image_utils.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = preprocess_input(img)
    
    predictions = model.predict(img)
    print('Predicted:', decode_predictions(predictions, top=3))
    
    descripton = decode_predictions(predictions, top=3)[0][0][1]
    
    src = cv2.imread(img_path)
    cv2.putText(src, descripton, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)
    cv2.imshow('Predicted', src)
    cv2.waitKey()
    

    二,输出

    C:\Users\ccc\AppData\Local\Programs\Python\Python38\python.exe D:/tmp/tup_ai/codes/2.clustering/kmeans/tf_test.py
    2022-06-27 10:12:10.303860: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
    To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
    1/1 [==============================] - 2s 2s/step
    Predicted: [[('n02110185', 'Siberian_husky', 0.8130109), ('n02109961', 'Eskimo_dog', 0.14220986), ('n02110063', 'malamute', 0.0025941634)]]
    =========================
    1/1 [==============================] - 1s 1s/step
    Predicted: [[('n01531178', 'goldfinch', 0.82197833), ('n01560419', 'bulbul', 0.062186603), ('n01537544', 'indigo_bunting', 0.05275042)]]
    
    Process finished with exit code 0
    
    
    image.png

    相关文章

      网友评论

          本文标题:KERAS:使用INCEPTIONV3、RESNET50预训练模

          本文链接:https://www.haomeiwen.com/subject/qumnvrtx.html