Skip to content

Commit 7bb3f4b

Browse files
committed
模型部署
1 parent 281475b commit 7bb3f4b

File tree

11 files changed

+3521
-0
lines changed

11 files changed

+3521
-0
lines changed

ai/day09_模型优化和部署/01_预训练模型.ipynb

Lines changed: 1382 additions & 0 deletions
Large diffs are not rendered by default.

ai/day09_模型优化和部署/02_迁移学习.ipynb

Lines changed: 1829 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
" * Serving Flask app \"__main__\" (lazy loading)\n",
13+
" * Environment: production\n",
14+
" WARNING: This is a development server. Do not use it in a production deployment.\n",
15+
" Use a production WSGI server instead.\n",
16+
" * Debug mode: off\n"
17+
]
18+
},
19+
{
20+
"name": "stderr",
21+
"output_type": "stream",
22+
"text": [
23+
" * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)\n",
24+
"127.0.0.1 - - [25/Oct/2020 16:04:03] \"\u001b[37mGET / HTTP/1.1\u001b[0m\" 200 -\n",
25+
"127.0.0.1 - - [25/Oct/2020 16:04:04] \"\u001b[37mGET / HTTP/1.1\u001b[0m\" 200 -\n",
26+
"127.0.0.1 - - [25/Oct/2020 16:04:09] \"\u001b[37mPOST / HTTP/1.1\u001b[0m\" 200 -\n"
27+
]
28+
}
29+
],
30+
"source": [
31+
"# flask : python的web框架\n",
32+
"\n",
33+
"from flask import *\n",
34+
"import cv2\n",
35+
"from tensorflow.keras.applications.mobilenet import *\n",
36+
"import numpy as np\n",
37+
"\n",
38+
"app = Flask(__name__)\n",
39+
"model = MobileNet(weights='imagenet')\n",
40+
"\n",
41+
"@app.route(\"/\", methods=['POST','GET'])\n",
42+
"def main_page():\n",
43+
" if request.method == \"POST\":\n",
44+
" file = request.files['file']\n",
45+
" file.save(file.filename)\n",
46+
" image = cv2.imread(file.filename)\n",
47+
" image = cv2.resize(image,(224,224))\n",
48+
" image = image /255.0\n",
49+
" outputs = model.predict(np.array([image]))\n",
50+
" result = decode_predictions(outputs,top=1)\n",
51+
" return \"预测的结果是:{}\".format(result)\n",
52+
" \n",
53+
" return render_template('upload.html')\n",
54+
"\n",
55+
"\n",
56+
"if __name__ == '__main__':\n",
57+
" app.run(host='0.0.0.0',port=8080)"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": null,
63+
"metadata": {},
64+
"outputs": [],
65+
"source": []
66+
}
67+
],
68+
"metadata": {
69+
"kernelspec": {
70+
"display_name": "Python 3",
71+
"language": "python",
72+
"name": "python3"
73+
},
74+
"language_info": {
75+
"codemirror_mode": {
76+
"name": "ipython",
77+
"version": 3
78+
},
79+
"file_extension": ".py",
80+
"mimetype": "text/x-python",
81+
"name": "python",
82+
"nbconvert_exporter": "python",
83+
"pygments_lexer": "ipython3",
84+
"version": "3.7.9"
85+
}
86+
},
87+
"nbformat": 4,
88+
"nbformat_minor": 4
89+
}
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# tflite tensorflow lite"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 2,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"# 把编译好的模型,可以转换成tflite的格式,适合在很弱的cpu的边缘设备上运行"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 2,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"## 模型的转化\n",
28+
"# 把h5模型文件转换成 tflite的类型\n",
29+
"import tensorflow as tf\n",
30+
"from tensorflow.keras.applications.mobilenet_v2 import *\n",
31+
"model = tf.keras.applications.mobilenet_v2.MobileNetV2(weights='imagenet')\n",
32+
"model.save(\"xx.h5\")"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 21,
38+
"metadata": {},
39+
"outputs": [
40+
{
41+
"name": "stdout",
42+
"output_type": "stream",
43+
"text": [
44+
"66058900\n"
45+
]
46+
}
47+
],
48+
"source": [
49+
"import cv2\n",
50+
"import numpy as np\n",
51+
"import time\n",
52+
"image = cv2.imread('ddd.jpg')\n",
53+
"x = cv2.resize(image,(224,224))\n",
54+
"## 模型预测\n",
55+
"decode_predictions(model.predict(np.array([x])/255.0))\n"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 5,
61+
"metadata": {},
62+
"outputs": [
63+
{
64+
"name": "stdout",
65+
"output_type": "stream",
66+
"text": [
67+
"WARNING:tensorflow:From C:\\Users\\Administrator\\Anaconda3\\envs\\tfenv\\lib\\site-packages\\tensorflow\\python\\training\\tracking\\tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.\n",
68+
"Instructions for updating:\n",
69+
"This property should not be used in TensorFlow 2.0, as updates are applied automatically.\n",
70+
"WARNING:tensorflow:From C:\\Users\\Administrator\\Anaconda3\\envs\\tfenv\\lib\\site-packages\\tensorflow\\python\\training\\tracking\\tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.\n",
71+
"Instructions for updating:\n",
72+
"This property should not be used in TensorFlow 2.0, as updates are applied automatically.\n",
73+
"INFO:tensorflow:Assets written to: C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\tmpimh742dq\\assets\n"
74+
]
75+
},
76+
{
77+
"data": {
78+
"text/plain": [
79+
"13988516"
80+
]
81+
},
82+
"execution_count": 5,
83+
"metadata": {},
84+
"output_type": "execute_result"
85+
}
86+
],
87+
"source": [
88+
"## tflite \n",
89+
"converter = tf.lite.TFLiteConverter.from_keras_model(model)\n",
90+
"## 使用转换器进行模型的转化\n",
91+
"tflite_model = converter.convert()\n",
92+
"\n",
93+
"## 保存tflite的模型\n",
94+
"\n",
95+
"open(\"xxx.tflite\",\"wb\").write(tflite_model)"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 6,
101+
"metadata": {},
102+
"outputs": [],
103+
"source": [
104+
"## 转换成tflite文件大小发生了变换,存储方式也发生了变换。\n"
105+
]
106+
},
107+
{
108+
"cell_type": "markdown",
109+
"metadata": {},
110+
"source": [
111+
"## 使用tflite的模型来预测数据"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 36,
117+
"metadata": {},
118+
"outputs": [],
119+
"source": [
120+
"import cv2\n",
121+
"import numpy as np\n",
122+
"image = cv2.imread('ddd.jpg')\n",
123+
"x = cv2.resize(image,(224,224))\n",
124+
"x = np.array([x])/255.0"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": 37,
130+
"metadata": {},
131+
"outputs": [
132+
{
133+
"name": "stdout",
134+
"output_type": "stream",
135+
"text": [
136+
"[{'name': 'input_1', 'index': 0, 'shape': array([ 1, 224, 224, 3]), 'shape_signature': array([ -1, 224, 224, 3]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]\n",
137+
"[{'name': 'Identity', 'index': 178, 'shape': array([ 1, 1000]), 'shape_signature': array([ -1, 1000]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]\n",
138+
"[[('n02480855', 'gorilla', 0.66071), ('n02480495', 'orangutan', 0.17576733), ('n02481823', 'chimpanzee', 0.00764068), ('n02484975', 'guenon', 0.004435742), ('n02486410', 'baboon', 0.0038167273)]]\n"
139+
]
140+
}
141+
],
142+
"source": [
143+
"interpreter = tf.lite.Interpreter(\"xxx.tflite\") # 得到tflite的模型解释器\n",
144+
"interpreter.allocate_tensors() ## 声明计算所需要的空间\n",
145+
"\n",
146+
"input_details = interpreter.get_input_details() # 输入\n",
147+
"output_details = interpreter.get_output_details() #输出\n",
148+
"\n",
149+
"print(input_details)\n",
150+
"print(output_details)\n",
151+
"\n",
152+
"interpreter.set_tensor(input_details[0]['index'],x.astype(np.float32)) #指定输入\n",
153+
"\n",
154+
"## 运行推理 \n",
155+
"interpreter.invoke()\n",
156+
"\n",
157+
"## 获取计算出来的结果\n",
158+
"results = interpreter.get_tensor(output_details[0]['index'])\n",
159+
"\n",
160+
"print(decode_predictions(results))"
161+
]
162+
},
163+
{
164+
"cell_type": "markdown",
165+
"metadata": {},
166+
"source": [
167+
"# 树莓派 cpu,arm平台。\n",
168+
"\n",
169+
"* 链接:https://pan.baidu.com/s/1xPxepH6eLdlW3HNmZvVmUw \n",
170+
"* 提取码:1234 \n"
171+
]
172+
},
173+
{
174+
"cell_type": "code",
175+
"execution_count": null,
176+
"metadata": {},
177+
"outputs": [],
178+
"source": [
179+
"sudo apt-get install libjpeg8-dev zlib1g-dev\n",
180+
"sudo apt-get install Pillow==7.2.0\n",
181+
"sudo apt-get install Cython\n",
182+
"pip3 install scikit-build\n",
183+
"\n",
184+
"pip3 install opencv-python 三个小时\n",
185+
"https://github.com/itheima1/tensorflow-on-aarch64"
186+
]
187+
}
188+
],
189+
"metadata": {
190+
"kernelspec": {
191+
"display_name": "Python 3",
192+
"language": "python",
193+
"name": "python3"
194+
},
195+
"language_info": {
196+
"codemirror_mode": {
197+
"name": "ipython",
198+
"version": 3
199+
},
200+
"file_extension": ".py",
201+
"mimetype": "text/x-python",
202+
"name": "python",
203+
"nbconvert_exporter": "python",
204+
"pygments_lexer": "ipython3",
205+
"version": "3.7.9"
206+
}
207+
},
208+
"nbformat": 4,
209+
"nbformat_minor": 4
210+
}

ai/day09_模型优化和部署/a.jpg

12.3 KB
Loading

ai/day09_模型优化和部署/bb.jpg

33.8 KB
Loading
35.4 KB
Loading
25.3 KB
Binary file not shown.
3.43 MB
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<html>
2+
<body>
3+
<form action="" enctype="multipart/form-data" method="POST">
4+
<input type="file" name="file"/>
5+
<input type="submit" value="上传"/>
6+
</form>
7+
8+
9+
</body>
10+
11+
</html>

0 commit comments

Comments
 (0)