|
|
@@ -18,13 +18,236 @@ TensorFlow是由Google Brain团队开发并在2015年首次发布的一款开源
|
|
|
* **可扩展性**:支持大规模数据处理和训练,能够处理从移动设备到大规模分布式集群的各种计算任务。
|
|
|
* **自动微分**:提供自动求导功能,通过自动计算梯度,简化了模型训练中的反向传播过程。
|
|
|
|
|
|
-
|
|
|
+## 一般的过程
|
|
|
+
|
|
|
+TensorFlow是一个开源的机器学习框架,被广泛用于构建和训练各种机器学习模型,特别是深度学习模型。以下为你介绍其基本用法,包含安装、张量操作、构建和训练模型等内容:
|
|
|
+
|
|
|
+### 1. 安装TensorFlow
|
|
|
+可以使用`pip`或`conda`进行安装,以下是使用`pip`安装的命令:
|
|
|
+```bash
|
|
|
+pip install tensorflow
|
|
|
+```
|
|
|
+如果你使用的是GPU版本,还需要安装相应的CUDA和cuDNN库,命令如下:
|
|
|
+```bash
|
|
|
+pip install tensorflow-gpu
|
|
|
+```
|
|
|
+
|
|
|
+### 2. 导入TensorFlow
|
|
|
+在Python脚本或Jupyter Notebook中,使用以下语句导入TensorFlow:
|
|
|
+```python
|
|
|
+import tensorflow as tf
|
|
|
+```
|
|
|
+
|
|
|
+### 3. 张量(Tensor)操作
|
|
|
+TensorFlow中的基本数据结构是张量,类似于多维数组。以下是一些常见的张量操作:
|
|
|
+```python
|
|
|
+# 创建张量
|
|
|
+scalar = tf.constant(10) # 标量
|
|
|
+vector = tf.constant([1, 2, 3]) # 向量
|
|
|
+matrix = tf.constant([[1, 2], [3, 4]]) # 矩阵
|
|
|
+
|
|
|
+# 查看张量的形状和类型
|
|
|
+print("Scalar shape:", scalar.shape)
|
|
|
+print("Vector shape:", vector.shape)
|
|
|
+print("Matrix shape:", matrix.shape)
|
|
|
+
|
|
|
+# 张量运算
|
|
|
+result = tf.add(matrix, matrix) # 加法运算
|
|
|
+print("Addition result:", result)
|
|
|
+
|
|
|
+# 类型转换
|
|
|
+float_tensor = tf.cast(matrix, dtype=tf.float32)
|
|
|
+print("Float tensor:", float_tensor)
|
|
|
+```
|
|
|
+
|
|
|
+### 4. 构建简单的神经网络模型
|
|
|
+以构建一个简单的全连接神经网络来对MNIST手写数字数据集进行分类为例:
|
|
|
+```python
|
|
|
+# 加载数据集
|
|
|
+mnist = tf.keras.datasets.mnist
|
|
|
+(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
|
|
+
|
|
|
+# 数据预处理
|
|
|
+x_train, x_test = x_train / 255.0, x_test / 255.0
|
|
|
+
|
|
|
+# 构建模型
|
|
|
+model = tf.keras.models.Sequential([
|
|
|
+ tf.keras.layers.Flatten(input_shape=(28, 28)),
|
|
|
+ tf.keras.layers.Dense(128, activation='relu'),
|
|
|
+ tf.keras.layers.Dropout(0.2),
|
|
|
+ tf.keras.layers.Dense(10, activation='softmax')
|
|
|
+])
|
|
|
+
|
|
|
+# 编译模型
|
|
|
+model.compile(optimizer='adam',
|
|
|
+ loss='sparse_categorical_crossentropy',
|
|
|
+ metrics=['accuracy'])
|
|
|
+
|
|
|
+# 训练模型
|
|
|
+model.fit(x_train, y_train, epochs=5)
|
|
|
+
|
|
|
+# 评估模型
|
|
|
+model.evaluate(x_test, y_test)
|
|
|
+```
|
|
|
+
|
|
|
+### 5. 使用自定义训练循环
|
|
|
+除了使用`fit`方法,还可以使用自定义训练循环来训练模型,这样可以更灵活地控制训练过程:
|
|
|
+```python
|
|
|
+# 定义模型
|
|
|
+model = tf.keras.models.Sequential([
|
|
|
+ tf.keras.layers.Dense(10, activation='softmax')
|
|
|
+])
|
|
|
+
|
|
|
+# 定义损失函数和优化器
|
|
|
+loss_object = tf.keras.losses.SparseCategoricalCrossentropy()
|
|
|
+optimizer = tf.keras.optimizers.Adam()
|
|
|
+
|
|
|
+# 定义训练步骤
|
|
|
+@tf.function
|
|
|
+def train_step(images, labels):
|
|
|
+ with tf.GradientTape() as tape:
|
|
|
+ predictions = model(images)
|
|
|
+ loss = loss_object(labels, predictions)
|
|
|
+ gradients = tape.gradient(loss, model.trainable_variables)
|
|
|
+ optimizer.apply_gradients(zip(gradients, model.trainable_variables))
|
|
|
+ return loss
|
|
|
+
|
|
|
+# 训练循环
|
|
|
+epochs = 5
|
|
|
+for epoch in range(epochs):
|
|
|
+ total_loss = 0
|
|
|
+ for images, labels in train_dataset:
|
|
|
+ loss = train_step(images, labels)
|
|
|
+ total_loss += loss
|
|
|
+ print(f'Epoch {epoch + 1}, Loss: {total_loss / len(train_dataset)}')
|
|
|
+```
|
|
|
+
|
|
|
+### 6. 保存和加载模型
|
|
|
+训练好的模型可以保存下来,以便后续使用:
|
|
|
+```python
|
|
|
+# 保存模型
|
|
|
+model.save('my_model.h5')
|
|
|
+
|
|
|
+# 加载模型
|
|
|
+loaded_model = tf.keras.models.load_model('my_model.h5')
|
|
|
+```
|
|
|
+
|
|
|
+以上就是TensorFlow的一些基本用法,通过这些操作,你可以构建、训练和评估各种机器学习模型。
|
|
|
+
|
|
|
+## 用 TensorFlow 做身份证识别
|
|
|
+
|
|
|
+
|
|
|
+使用TensorFlow进行身份证识别通常可以分为数据准备、模型选择与构建、模型训练、模型评估以及实际应用这几个主要步骤,以下是详细介绍:
|
|
|
+
|
|
|
+### 1. 数据准备
|
|
|
+- **数据收集**
|
|
|
+ - 收集大量包含身份证的图像数据,这些图像应具有不同的拍摄角度、光照条件、分辨率等,以增加数据的多样性。可以从公开数据集获取部分数据,也可以自己使用相机拍摄。
|
|
|
+ - 确保数据集中包含正面和反面身份证图像,且涵盖不同地区、不同样式的身份证。
|
|
|
+- **数据标注**
|
|
|
+ - 对于身份证识别任务,需要标注出身份证上关键信息的位置和内容,如姓名、性别、民族、出生日期、住址、身份证号码等。可以使用标注工具(如LabelImg)进行矩形框标注,标记出每个信息区域的位置。
|
|
|
+ - 将标注信息保存为合适的格式,如XML、JSON等,方便后续处理。
|
|
|
+- **数据预处理**
|
|
|
+ - **图像缩放**:将所有图像调整为统一的尺寸,例如常见的 224x224 或 512x512 等,以适应模型的输入要求。
|
|
|
+ - **归一化**:将图像像素值归一化到 [0, 1] 或 [-1, 1] 范围内,有助于模型的收敛和训练稳定性。
|
|
|
+ - **数据增强**:通过随机旋转、翻转、裁剪、亮度调整等操作增加数据的多样性,提高模型的泛化能力。在 TensorFlow 中,可以使用 `tf.keras.preprocessing.image` 模块或 `tf.data` 进行数据增强。
|
|
|
+- **数据集划分**
|
|
|
+将数据集按照一定比例(如 70% 训练集、15% 验证集、15% 测试集)划分为训练集、验证集和测试集。训练集用于模型的训练,验证集用于在训练过程中评估模型的性能,调整超参数,测试集用于最终评估模型的泛化能力。
|
|
|
+
|
|
|
+### 2. 模型选择与构建
|
|
|
+- **目标检测模型选择**
|
|
|
+ - 可以选择预训练的目标检测模型,如 Faster R - CNN、YOLO(You Only Look Once)、SSD(Single Shot MultiBox Detector)等。这些模型在 TensorFlow 中都有相应的实现或开源代码。
|
|
|
+ - 以使用预训练的 MobileNet - SSD 模型为例,该模型具有轻量级、速度快的特点,适合在资源有限的设备上运行。
|
|
|
+- **构建模型**
|
|
|
+ - 在 TensorFlow 中,可以使用 `tf.keras` 或 TensorFlow Object Detection API 来构建模型。如果使用 TensorFlow Object Detection API,需要下载预训练模型的配置文件和权重文件,并根据自己的数据集进行相应的修改。
|
|
|
+ - 以下是一个简单的使用 `tf.keras` 构建自定义卷积神经网络(CNN)进行目标检测的示例代码框架:
|
|
|
+```python
|
|
|
+import tensorflow as tf
|
|
|
+from tensorflow.keras import layers
|
|
|
+
|
|
|
+def build_model(input_shape):
|
|
|
+ model = tf.keras.Sequential([
|
|
|
+ layers.Conv2D(32, (3, 3), activation='relu', input_shape=input_shape),
|
|
|
+ layers.MaxPooling2D((2, 2)),
|
|
|
+ layers.Conv2D(64, (3, 3), activation='relu'),
|
|
|
+ layers.MaxPooling2D((2, 2)),
|
|
|
+ layers.Flatten(),
|
|
|
+ layers.Dense(64, activation='relu'),
|
|
|
+ layers.Dense(4, activation='linear') # 假设输出是四个坐标值(x, y, w, h)
|
|
|
+ ])
|
|
|
+ return model
|
|
|
+
|
|
|
+input_shape = (224, 224, 3)
|
|
|
+model = build_model(input_shape)
|
|
|
+```
|
|
|
+
|
|
|
+### 3. 模型训练
|
|
|
+- **定义损失函数和优化器**
|
|
|
+ - 对于目标检测任务,常用的损失函数包括回归损失(如均方误差 MSE 用于预测框的坐标)和分类损失(如交叉熵损失用于类别预测)。
|
|
|
+ - 选择合适的优化器,如 Adam、SGD 等,并设置学习率等超参数。
|
|
|
+```python
|
|
|
+loss_object = tf.keras.losses.MeanSquaredError()
|
|
|
+optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
|
|
|
+```
|
|
|
+- **编译模型**
|
|
|
+ - 将损失函数、优化器和评估指标(如准确率、平均精度均值 mAP 等)编译到模型中。
|
|
|
+```python
|
|
|
+model.compile(optimizer=optimizer, loss=loss_object, metrics=['accuracy'])
|
|
|
+```
|
|
|
+- **训练模型**
|
|
|
+ - 使用准备好的训练集进行模型训练,同时在验证集上进行验证。可以设置训练的轮数(epochs)、批次大小(batch size)等参数。
|
|
|
+```python
|
|
|
+history = model.fit(train_dataset, epochs=10, validation_data=val_dataset)
|
|
|
+```
|
|
|
+
|
|
|
+### 4. 模型评估
|
|
|
+- **在测试集上评估**
|
|
|
+使用测试集对训练好的模型进行评估,计算各项评估指标,如准确率、召回率、F1 值、平均精度均值 mAP 等,以全面评估模型的性能。
|
|
|
+```python
|
|
|
+test_loss, test_acc = model.evaluate(test_dataset)
|
|
|
+print(f'Test accuracy: {test_acc}')
|
|
|
+```
|
|
|
+- **可视化评估结果**
|
|
|
+可以使用可视化工具(如 Matplotlib)绘制训练过程中的损失曲线和准确率曲线,直观地观察模型的训练情况。同时,对模型的预测结果进行可视化展示,检查模型的预测效果。
|
|
|
+
|
|
|
+### 5. 实际应用
|
|
|
+- **模型保存与加载**
|
|
|
+将训练好的模型保存为合适的格式,如 `.h5` 或 TensorFlow SavedModel 格式,以便在实际应用中加载使用。
|
|
|
+```python
|
|
|
+model.save('id_card_detection_model.h5')
|
|
|
+loaded_model = tf.keras.models.load_model('id_card_detection_model.h5')
|
|
|
+```
|
|
|
+- **图像输入与预处理**
|
|
|
+在实际应用中,将待识别的身份证图像进行预处理,使其与训练时的图像格式一致,然后输入到加载的模型中进行预测。
|
|
|
+```python
|
|
|
+import cv2
|
|
|
+import numpy as np
|
|
|
+
|
|
|
+def preprocess_image(image_path):
|
|
|
+ image = cv2.imread(image_path)
|
|
|
+ image = cv2.resize(image, (224, 224))
|
|
|
+ image = image / 255.0
|
|
|
+ image = np.expand_dims(image, axis=0)
|
|
|
+ return image
|
|
|
+
|
|
|
+image_path = 'test_id_card.jpg'
|
|
|
+input_image = preprocess_image(image_path)
|
|
|
+```
|
|
|
+- **预测与后处理**
|
|
|
+使用加载的模型对输入图像进行预测,得到预测结果后进行后处理,如解码预测框的坐标、过滤低置信度的预测框等。
|
|
|
+```python
|
|
|
+predictions = loaded_model.predict(input_image)
|
|
|
+# 后处理代码...
|
|
|
+```
|
|
|
|
|
|
## 极简实例
|
|
|
|
|
|
1)tensorflow 实现端到端的OCR:二代身份证号识别
|
|
|
[tensorflow 实现端到端的OCR:二代身份证号识别](http://www.gitpp.com/big-ai/ocr_tensorflow_cnn)
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
* **应用场景**:
|
|
|
|
|
|
* 图像识别:用于图像分类、目标检测和图像分割等任务。
|