Keras框架:Mobilenet網絡代碼實現

Mobilenet概念:

MobileNet模型是Google針對手機等嵌入式設備提出的一種輕量級的深層神經網絡,其使用的核心思想便是depthwise separable convolution。

Mobilenet思想:

通俗地理解就是3x3的卷積核厚度只有一層,然后在輸入張量上一層一層地滑動,每一次卷積完生成 一個輸出通道,當卷積完成后,在利用1x1的卷積調整厚度。
在這里插入圖片描述
對于一個卷積點而言: 假設有一個3×3大小的卷積層,其輸入通道為16、輸出通道為32。具體為,32個3×3大小的卷積核會 遍歷16個通道中的每個數據,最后可得到所需的32個輸出通道,所需參數為16×32×3×3=4608個。
應用深度可分離卷積,用16個3×3大小的卷積核分別遍歷16通道的數據,得到了16個特征圖譜。在 融合操作之前,接著用32個1×1大小的卷積核遍歷這16個特征圖譜,所需參數為 16×3×3+16×32×1×1=656個。
可以看出來depthwise separable convolution可以減少模型的參數。
在這里插入圖片描述

Mobilenet網絡代碼實現

網絡主體部分:

#-------------------------------------------------------------#
#   MobileNet的網絡部分
#-------------------------------------------------------------#
import warnings
import numpy as npfrom keras.preprocessing import imagefrom keras.models import Model
from keras.layers import DepthwiseConv2D,Input,Activation,Dropout,Reshape,BatchNormalization,GlobalAveragePooling2D,GlobalMaxPooling2D,Conv2D
from keras.applications.imagenet_utils import decode_predictions
from keras import backend as Kdef MobileNet(input_shape=[224,224,3],depth_multiplier=1,dropout=1e-3,classes=1000):img_input = Input(shape=input_shape)# 224,224,3 -> 112,112,32x = _conv_block(img_input, 32, strides=(2, 2))# 112,112,32 -> 112,112,64x = _depthwise_conv_block(x, 64, depth_multiplier, block_id=1)# 112,112,64 -> 56,56,128x = _depthwise_conv_block(x, 128, depth_multiplier,strides=(2, 2), block_id=2)# 56,56,128 -> 56,56,128x = _depthwise_conv_block(x, 128, depth_multiplier, block_id=3)# 56,56,128 -> 28,28,256x = _depthwise_conv_block(x, 256, depth_multiplier,strides=(2, 2), block_id=4)# 28,28,256 -> 28,28,256x = _depthwise_conv_block(x, 256, depth_multiplier, block_id=5)# 28,28,256 -> 14,14,512x = _depthwise_conv_block(x, 512, depth_multiplier,strides=(2, 2), block_id=6)# 14,14,512 -> 14,14,512x = _depthwise_conv_block(x, 512, depth_multiplier, block_id=7)x = _depthwise_conv_block(x, 512, depth_multiplier, block_id=8)x = _depthwise_conv_block(x, 512, depth_multiplier, block_id=9)x = _depthwise_conv_block(x, 512, depth_multiplier, block_id=10)x = _depthwise_conv_block(x, 512, depth_multiplier, block_id=11)# 14,14,512 -> 7,7,1024x = _depthwise_conv_block(x, 1024, depth_multiplier,strides=(2, 2), block_id=12)x = _depthwise_conv_block(x, 1024, depth_multiplier, block_id=13)# 7,7,1024 -> 1,1,1024x = GlobalAveragePooling2D()(x)x = Reshape((1, 1, 1024), name='reshape_1')(x)x = Dropout(dropout, name='dropout')(x)x = Conv2D(classes, (1, 1),padding='same', name='conv_preds')(x)x = Activation('softmax', name='act_softmax')(x)x = Reshape((classes,), name='reshape_2')(x)inputs = img_inputmodel = Model(inputs, x, name='mobilenet_1_0_224_tf')model_name = 'mobilenet_1_0_224_tf.h5'model.load_weights(model_name)return modeldef _conv_block(inputs, filters, kernel=(3, 3), strides=(1, 1)):x = Conv2D(filters, kernel,padding='same',use_bias=False,strides=strides,name='conv1')(inputs)x = BatchNormalization(name='conv1_bn')(x)return Activation(relu6, name='conv1_relu')(x)def _depthwise_conv_block(inputs, pointwise_conv_filters,depth_multiplier=1, strides=(1, 1), block_id=1):x = DepthwiseConv2D((3, 3),padding='same',depth_multiplier=depth_multiplier,strides=strides,use_bias=False,name='conv_dw_%d' % block_id)(inputs)x = BatchNormalization(name='conv_dw_%d_bn' % block_id)(x)x = Activation(relu6, name='conv_dw_%d_relu' % block_id)(x)x = Conv2D(pointwise_conv_filters, (1, 1),padding='same',use_bias=False,strides=(1, 1),name='conv_pw_%d' % block_id)(x)x = BatchNormalization(name='conv_pw_%d_bn' % block_id)(x)return Activation(relu6, name='conv_pw_%d_relu' % block_id)(x)def relu6(x):return K.relu(x, max_value=6)def preprocess_input(x):x /= 255.x -= 0.5x *= 2.return xif __name__ == '__main__':model = MobileNet(input_shape=(224, 224, 3))img_path = 'elephant.jpg'img = image.load_img(img_path, target_size=(224, 224))x = image.img_to_array(img)x = np.expand_dims(x, axis=0)x = preprocess_input(x)print('Input image shape:', x.shape)preds = model.predict(x)print(np.argmax(preds))print('Predicted:', decode_predictions(preds,1))  # 只顯示top1

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389467.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389467.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389467.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

clipboard 在 vue 中的使用

簡介 頁面中用 clipboard 可以進行復制粘貼&#xff0c;clipboard能將內容直接寫入剪切板 安裝 npm install --save clipboard 使用方法一 <template><span>{{ code }}</span><iclass"el-icon-document"title"點擊復制"click"co…

數據驅動開發_開發數據驅動的股票市場投資方法

數據驅動開發Data driven means that your decision are driven by data and not by emotions. This approach can be very useful in stock market investment. Here is a summary of a data driven approach which I have been taking recently數據驅動意味著您的決定是由數據…

前端之sublime text配置

接下來我們來了解如何調整sublime text的配置&#xff0c;可能很多同學下載sublime text的時候就是把它當成記事本來使用&#xff0c;也就是沒有做任何自定義的配置&#xff0c;做一些自定義的配置可以讓sublime text更適合我們的開發習慣。 那么在利用剛才的命令面板我們怎么打…

python 時間序列預測_使用Python進行動手時間序列預測

python 時間序列預測Time series analysis is the endeavor of extracting meaningful summary and statistical information from data points that are in chronological order. They are widely used in applied science and engineering which involves temporal measureme…

keras框架:目標檢測Faster-RCNN思想及代碼

Faster-RCNN&#xff08;RPN CNN ROI&#xff09;概念 Faster RCNN可以分為4個主要內容&#xff1a; Conv layers&#xff1a;作為一種CNN網絡目標檢測方法&#xff0c;Faster RCNN首先使用一組基礎的convrelupooling層提取 image的feature maps。該feature maps被共享用于…

算法偏見是什么_算法可能會使任何人(包括您)有偏見

算法偏見是什么在上一篇文章中&#xff0c;我們展示了當數據將情緒從動作中剝離時會發生什么 (In the last article, we showed what happens when data strip emotions out of an action) In Part 1 of this series, we argued that data can turn anyone into a psychopath, …

大數據筆記-0907

2019獨角獸企業重金招聘Python工程師標準>>> 復習: 1.clear清屏 2.vi vi xxx.log i-->edit esc-->command shift:-->end 輸入 wq 3.cat xxx.log 查看 --------------------------- 1.pwd 查看當前光標所在的path 2.家目錄 /boot swap / 根目錄 起始位置 家…

Tensorflow框架:目標檢測Yolo思想

Yolo-You Only Look Once YOLO算法采用一個單獨的CNN模型實現end-to-end的目標檢測&#xff1a; Resize成448448&#xff0c;圖片分割得到77網格(cell)CNN提取特征和預測&#xff1a;卷積部分負責提取特征。全鏈接部分負責預測&#xff1a;過濾bbox&#xff08;通過nms&#…

線性回歸非線性回歸_了解線性回歸

線性回歸非線性回歸Let’s say you’re looking to buy a new PC from an online store (and you’re most interested in how much RAM it has) and you see on their first page some PCs with 4GB at $100, then some with 16 GB at $1000. Your budget is $500. So, you es…

樸素貝葉斯和貝葉斯估計_貝葉斯估計收入增長的方法

樸素貝葉斯和貝葉斯估計Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works wi…

numpy統計分布顯示

import numpy as np from sklearn.datasets import load_iris dataload_iris()petal_lengthnumpy.array(list(len[2]for len in data[data]))#取出花瓣長度數據 print(np.max(petal_length))#花瓣長度最大值 print(np.mean(petal_length))#花瓣長度平均值 print(np.std(petal_l…

python數據結構:進制轉化探索

*********************************第一部分******************************************************************************************************************************************************************************************# 輸入excel的行號&#xff0c;…

Keras框架:人臉檢測-mtcnn思想及代碼

人臉檢測-mtcnn 概念&#xff1a; MTCNN&#xff0c;英文全稱是Multi-task convolutional neural network&#xff0c;中文全稱是多任務卷積神經網絡&#xff0c; 該神經網絡將人臉區域檢測與人臉關鍵點檢測放在了一起。 從工程實踐上&#xff0c;MTCNN是一種檢測速度和準確率…

python中格式化字符串_Python中所有字符串格式化的指南

python中格式化字符串Strings are one of the most essential and used datatypes in programming. It allows the computer to interact and communicate with the world, such as printing instructions or reading input from the user. The ability to manipulate and form…

Javassist實現JDK動態代理

提到JDK動態代理&#xff0c;相信很多人并不陌生。然而&#xff0c;對于動態代理的實現原理&#xff0c;以及如何編碼實現動態代理功能&#xff0c;可能知道的人就比較少了。接下一來&#xff0c;我們就一起來看看JDK動態代理的基本原理&#xff0c;以及如何通過Javassist進行模…

數據圖表可視化_數據可視化如何選擇正確的圖表第1部分

數據圖表可視化According to the World Economic Forum, the world produces 2.5 quintillion bytes of data every day. With so much data, it’s become increasingly difficult to manage and make sense of it all. It would be impossible for any person to wade throug…

Keras框架:實例分割Mask R-CNN算法實現及實現

實例分割 實例分割&#xff08;instance segmentation&#xff09;的難點在于&#xff1a; 需要同時檢測出目標的位置并且對目標進行分割&#xff0c;所以這就需要融合目標檢測&#xff08;框出目標的位置&#xff09;以及語義分割&#xff08;對像素進行分類&#xff0c;分割…

機器學習 缺陷檢測_球檢測-體育中的機器學習。

機器學習 缺陷檢測&#x1f6a9; 目標 (&#x1f6a9;Objective) We want to evaluate the quickest way to detect the ball in a sport event in order to develop an Sports AI without spending a million dollars on tech or developers. Quickly we find out that detec…

莫煩Pytorch神經網絡第二章代碼修改

import torch import numpy as np""" Numpy Torch對比課程 """ # #tensor與numpy格式數據相互轉換 # np_data np.arange(6).reshape((2,3)) # print(np_data) # # torch_data torch.from_numpy(np_data) # print(\n,torch_data) # # tensor2ar…

自定義字符類

當 VC不使用MFC&#xff0c;無法使用屬于MFC的CString&#xff0c;為此自定義一個&#xff0c;先暫時使用&#xff0c;后續完善。 頭文件&#xff1a; #pragma once#define MAX_LOADSTRING 100 // 最大字符數class CString {public:char *c_str, cSAr[MAX_LOADSTRING];WCHAR *w…