ResNet50
ResNet50是深度学习中的一个卷积神经网络模型,全名为"Residual Network 50",是微软研究团队在2015年提出的,用于解决深层神经网络训练过程中的梯度消失问题。ResNet50是ResNet系列中的一个变种,具有50个卷积层。
ResNet的核心创新是引入了"残差块"(Residual Block),这种块的结构允许信息跨层传递,从而解决了深层网络中梯度消失和训练难度的问题。在残差块中,通过在网络中添加"跳跃连接"(或称为"快捷连接")来绕过一些层,使得信息可以更直接地传递,避免了信息在深层传递过程中的损失。
ResNet50具有50个卷积层,其中包括若干个残差块。每个残差块通常包含多个卷积层、批归一化和激活函数。ResNet50以ImageNet数据集上的分类任务为目标进行了预训练,可以在计算机视觉任务中作为特征提取器或分类器的基础模型使用。
在实际应用中,通过迁移学习,可以使用预训练的ResNet50模型作为特征提取器,然后根据不同的任务在其之上添加自定义的神经网络层,从而在新的任务上取得优秀的性能。
示例
加载了ResNet50模型,输入,经过模型预测,可以输出一个结果
import numpy as np
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
from tensorflow.keras.preprocessing.image import load_img, img_to_array
# 加载预训练的ResNet50模型
model = ResNet50(weights='imagenet')
# 加载图像并进行预处理
img_path = './test.png'
img = load_img(img_path, target_size=(224, 224))
img_array = img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = preprocess_input(img_array)
# 使用ResNet50模型进行预测
predictions = model.predict(img_array)
# 解码预测结果
decoded_predictions = decode_predictions(predictions, top=2)[0]
# 打印预测结果
for _, label, score in decoded_predictions:
print(f"{label}: {score:.2f}")
这个代码示例可以被认为是对模型的一种简单的 serving 示例。在这个示例中,你加载了一个预训练的 ResNet50 模型,将输入图像进行预处理,然后使用该模型进行图像分类预测,最后输出预测结果。
在实际的生产环境中,模型的 serving 通常更加复杂。通常,你需要将模型部署到一个服务器上,为其提供一个 REST API 或其他类型的接口,以便可以通过网络请求将数据传递给模型并获得预测结果。
完整改成serving
用flask
python: flask启web,resnet模型提供serving
import numpy as np
from flask import Flask, request, render_template, jsonify
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
from tensorflow.keras.preprocessing.image import load_img, img_to_array
app = Flask(__name__)
# 加载预训练的ResNet50模型
model = ResNet50(weights='imagenet')
# 定义首页路由
@app.route('/5000', methods=['GET', 'POST'])
def index():
print("12345")
if request.method == 'POST':
# 获取上传的文件
file = request.files['file']
if file:
# 保存文件到本地
file_path = './uploaded_image.png'
file.save(file_path)
# 加载图像并进行预处理
img = load_img(file_path, target_size=(224, 224))
img_array = img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = preprocess_input(img_array)
# 使用ResNet50模型进行预测
predictions = model.predict(img_array)
# 解码预测结果
decoded_predictions = decode_predictions(predictions, top=5)[0]
# 格式化预测结果
formatted_predictions = [{'label': label, 'score': float(score)} for (_, label, score) in decoded_predictions]
return render_template('index.html', predictions=formatted_predictions, image_path=file_path)
return render_template('index.html', predictions=[], image_path='')
if __name__ == '__main__':
app.run(debug=True)
index.html
<!DOCTYPE html>
<html>
<head>
<title>ResNet50 Image Classifier</title>
</head>
<body>
<h1>ResNet50 Image Classifier</h1>
<form action="/5000" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">Upload and Predict</button>
</form>
{% if predictions %}
<h2>Predictions:</h2>
<ul>
{% for prediction in predictions %}
<li>{{ prediction.label }} - Score: {{ prediction.score }}</li>
{% endfor %}
</ul>
{% endif %}
{% if image_path %}
<h2>Uploaded Image:</h2>
<img src="{{ image_path }}" width="300">
{% endif %}
</body>
</html>
nginx.conf
location /5000 {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}