mobilenetv2的模型和Resnet50模型的测试对比

mobilenetV2模型

都说谷歌发布轻量级视觉架构MobileNetV2,速度快准确率高,近期有时间做了一点体验和笔记心得.

V1的MobileNet应用了深度可分离卷积(Depth-wise Seperable Convolution)并提出两个超参来控制网络容量,这种卷积背后的假设是跨channel相关性和跨spatial相关性的解耦。深度可分离卷积能够节省参数量省,在保持移动端可接受的模型复杂性的基础上达到了相当的高精度。而在V2中,MobileNet应用了新的单元:Inverted residual with linear bottleneck,主要的改动是为Bottleneck添加了linear激活输出以及将残差网络的skip-connection结构转移到低维Bottleneck层。

具体理论可参考这篇论文: Paper:Inverted Residuals and Linear Bottlenecks Mobile Networks for Classification, Detection and Segmentation

本篇主要是在使用mobilenetV2中的笔记和心得,记下来分享给大家.

主要是写一部分代码,和实践工作.
我找了几张图片作为测试对象,当然多篇论文中都严谨地做过多个数据集的测试和各个模型的对比,我只是兴趣,希望多了解一下.
本次都使用imagenet的预训练权重做实验.

mobilenetv2的模型和Resnet50模型的测试对比_第1张图片
cat.jpg

mobilenetv2的模型和Resnet50模型的测试对比_第2张图片
dog.jpg

mobilenetv2的模型和Resnet50模型的测试对比_第3张图片

mobilenetv2的模型和Resnet50模型的测试对比_第4张图片
gin.jpg
# 加载 mobilenetv2模型 (model1)
from keras.preprocessing import image
from keras.applications.mobilenetv2 import MobileNetV2
from keras.applications.mobilenetv2 import decode_predictions, preprocess_input
import numpy as np
model1 = MobileNetV2(weights='imagenet')
size = 224


# 加载我最喜欢的resnet50模型 (model2)
from keras.applications.resnet50 import ResNet50
from keras.applications.resnet50 import decode_predictions, preprocess_input
model2 = ResNet50(weights='imagenet')


/usr/local/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
# 预测测试 
import time

def mypredict(myimg):
    print('---------------------')
    print(myimg)
    img_path = 'data/%s.jpg' % myimg
    img = image.load_img(img_path, target_size=(size, size))


    # mobilenetv2
    t0 = time.time()
    x = image.img_to_array(img)/255 
    x = np.expand_dims(x, axis=0)
    preds = model1.predict(x)
    t1 = time.time()
    print('mobilenetv2 Predicted:', decode_predictions(preds, top=3))
    print('times:',t1-t0)

    # resnet50 
    t0 = time.time()
    x = image.img_to_array(img) 
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    preds = model2.predict(x)
    t1 = time.time()
    print('resnet50 Predicted:', decode_predictions(preds, top=3))
    print('times:',t1-t0)

mypredict('dog')
mypredict('cat')
mypredict('pandas')
mypredict('gin')

    
    
---------------------
dog
mobilenetv2 Predicted: [[('n02099601', 'golden_retriever', 0.6191928), ('n02099712', 'Labrador_retriever', 0.30996346), ('n02088466', 'bloodhound', 0.0066356426)]]
times: 0.22446393966674805
resnet50 Predicted: [[('n02088094', 'Afghan_hound', 0.48267168), ('n02099601', 'golden_retriever', 0.45850614), ('n02102318', 'cocker_spaniel', 0.018602183)]]
times: 0.6418988704681396
---------------------
cat
mobilenetv2 Predicted: [[('n02124075', 'Egyptian_cat', 0.28851244), ('n02123045', 'tabby', 0.22701205), ('n02123159', 'tiger_cat', 0.10923036)]]
times: 0.16522526741027832
resnet50 Predicted: [[('n02123045', 'tabby', 0.4466786), ('n02124075', 'Egyptian_cat', 0.27759436), ('n02123159', 'tiger_cat', 0.24237783)]]
times: 0.8371748924255371
---------------------
pandas
mobilenetv2 Predicted: [[('n02510455', 'giant_panda', 0.89715785), ('n02134084', 'ice_bear', 0.0032464499), ('n02133161', 'American_black_bear', 0.0027724444)]]
times: 0.22717905044555664
resnet50 Predicted: [[('n02510455', 'giant_panda', 0.96066725), ('n02447366', 'badger', 0.025910942), ('n02509815', 'lesser_panda', 0.0096278675)]]
times: 0.5430850982666016
---------------------
gin
mobilenetv2 Predicted: [[('n03888257', 'parachute', 0.96247214), ('n09193705', 'alp', 0.0018421151), ('n02782093', 'balloon', 0.0012920955)]]
times: 0.21444201469421387
resnet50 Predicted: [[('n03888257', 'parachute', 0.99946123), ('n09193705', 'alp', 0.00023966064), ('n02782093', 'balloon', 3.8610644e-05)]]
times: 0.510300874710083

做了几个图形的测试,识别率和识别速度上mobilenetV2还是很不错的,尽管猫和狗的品种上有差异,大体上还不错,尤其是速度上的确要快resnet50一大截啊.

你可能感兴趣的:(mobilenetv2的模型和Resnet50模型的测试对比)