python深度学习tensorflow和fme结合,实现档案扫描件数据自动分类

文章目录

前言

一、深度学习基础知识简介

1、什么是深度学习

2、深度学习的原理

3、深度学习应用场景

二、深度学习环境搭建

1.深度学习库的安装

2.CUDA和对应版本的cudnn下载

三、实战教学

1.基础数据集的准备

2、图片数据集转换为可训练数据集

3、模型搭建

4、模型初始化和模型训练以及模型保存

5、模型再优化

6、使用模型进行预测

总结

前言

之前我发过一遍用fme调用谷歌的汉字识别库来实现扫描件分类的文章,虽然能减少大量的人力物力,但是缺点依旧有很多,比如很多扫描件资料文字不清晰,房屋照片图像资料无法识别,文字识别率低,环境配置复杂,同时分类效率低下,大概需要花费1秒才能完成一张照片的分类。于是悟空我开始从深度学习上探索如何实现扫描件的0误差分类。学习了接近一个月的机器学习基础,深度学习算法,终于是实现了这个案例的接近于99的正确率分类。

浅谈深度学习的基本知识

[En]

A brief introduction to the basic knowledge of in-depth learning

1、什么是深度学习

深度学习是机器学习的一个分支,主要通过学习样本数据的内在规律和表示层次来实现。学习过程中获得的信息对文本、图像、声音等数据的解读有很大帮助。它的最终目标是使机器具有与人类一样的分析和学习能力,并能够识别文字、图像和声音等数据。深度学习是一种复杂的机器学习算法,与以往的相关技术相比,它在语音和图像识别方面取得了更多的结果。

[En]

Deep learning is a branch of machine learning, mainly by learning the inherent rules and representation levels of sample data. The information obtained in the learning process is of great help to the interpretation of data such as text, image, sound and so on. Its ultimate goal is to enable machines to have the same analytical and learning ability as human beings, and to be able to recognize data such as words, images and sounds. Deep learning is a complex machine learning algorithm, which has achieved far more results in speech and image recognition than previous related technologies.

2、深度学习的原理

为什么深度学习叫深度学习,所以名字叫四一,其实就是通过庞大而复杂的卷积层、池化层、隐藏层、全连接层、输出层来提取目标的特征,对特征进行降维,并通过线性回归的方式,找出最适合预测输出的权重。深度学习不同于机器学习,它最大的特点是模拟人脑的神经网络结构,通过激活函数实现快速高效的目标计算。

[En]

Why deep learning is called deep learning, so the name Siyi is actually through the huge and complex convolution layer, pooling layer, hidden layer, full connection layer, output layer to extract the features of the target, reduce the dimension of features, and through the way of linear regression, find out the most appropriate weight that can predict the output. Deep learning is different from machine learning, its biggest feature is to simulate the neural network structure of the human brain, through the activation function to achieve fast and efficient calculation of goals.

3、深度学习应用场景

深度学习并不局限于图像语音识别。事实上,深度学习是一种模拟人脑的算法。为什么与计算机相比,人们的计算能力这么低,但他们大部分时间都能区分物体和神经反射?事实上,它的性能比计算机更强大,计算机受益于神经元和神经元的完全连接结构,这种结构通常不做功,但一旦收到传入的信号。相应的神经元立即被激活,并通过一个巨大的神经网络迅速做出反应。因此,深度学习是一项很有潜力的技术,可以部署到系统中完成各种复杂的数据计算。

[En]

Deep learning is not limited to image speech recognition. In fact, deep learning is an algorithm that simulates the human brain. Why is it that people have such low computing power compared with computers, but they can distinguish objects and nerve reflexes most of the time? in fact, it has a more powerful performance than the computer, which benefits from the fully connected structure of neurons and neurons, which usually does not do work, but once the incoming signal is received. The corresponding neurons are activated immediately and react quickly through a huge neural network. Therefore, deep learning is a very potential technology, which can be deployed to the system to complete a variety of complex data calculations.

二、深度学习环境搭建

1.深度学习库的安装

我这里选取了谷歌开发的tensorflow2,当然也可以选择pytorch,这个看个人喜好,两者功能几乎没什么区别,只是如果考虑后期部署到服务器给平台后台计算使用,一般我会推荐使用tensorflow,如果是做学术研究,都推荐使用pytorch。安装很简单,只需要pip install tensorflow 就可以了。这里由于我的开发环境是fme,所以我选择fme.exe python -m pip install tensorflow –target “C:\Program Files\FME\python” 进行安装。

2.CUDA和对应版本的cudnn下载

这里我不做赘述,只需要进入官网下载,但是需要注意一点就是,必须要注意版本的对应,我这里用的是11.2CUDA,8.1的CUDNN,2.7的tensorflow,以及python3.7的编译环境。

三、实战教学

1.基础数据集的准备

python深度学习tensorflow和fme结合,实现档案扫描件数据自动分类

python深度学习tensorflow和fme结合,实现档案扫描件数据自动分类

其中每个数字都包含了一种类型的文档扫描件。我一共准备了2300张图片,其实为了满足精度,一般来说图片数量是越多越好,这里的准备我使用了我以前博客提过的调用谷歌的文字识别库的fme模板来处理的基础数据。

2、图片数据集转换为可训练数据集

首先我们需要了解最终我们要输出给神经网络进行训练的数据格式什么样子的,因为神经网络模型不能直接识别图像,所以我们要先将图像转换为一个np.array格式的4维数组如下图所示。

python深度学习tensorflow和fme结合,实现档案扫描件数据自动分类

这里我首先使用了fme的路径读模块,然后筛选出图像,将结果打入字段,同时对要素进行了打乱,因为最终我们需要将数据划分为训练集和测试集,所以这里提前打乱排序,切割样本的时候就会达到均匀的分割。

python深度学习tensorflow和fme结合,实现档案扫描件数据自动分类

这里是我需要导入的模块

import fme
import fmeobjects
import pandas as pd
import tensorflow.keras as keras
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras import layers

通过init方法实例化列表对象

    def __init__(self):
        self.all_image=[]
        self.label=[]
        pass

在input方法中将要素迭代,添加进列表

    def input(self,feature):
        self.all_image.append(feature.getAttribute('path_windows'))
        self.label.append(int(feature.getAttribute('label')))
        self.pyoutput(feature)

将数据分为训练集和测试集

[En]

Divide the data into a training set and a test set

        split=int(len(self.all_image)*0.8)
        train_dir=self.all_image[ :split]
        test_dir=self.all_image[split: ]
        train_label=np.array(self.label[:split])
        test_label=np.array(self.label[split:])

将训练集列表数据转换为4维数组

        train_image=[]
        for path in train_dir:
            image=keras.preprocessing.image.load_img(path,target_size=(224,224))
            image_array=keras.preprocessing.image.img_to_array(image)
            image_array=image_array/255#数据归一化
            train_image.append(image_array)
        train_image_array=np.stack(train_image)

将测试集列表数据转换为4维数组

        test_image=[]
        for path1 in test_dir:
            image1=keras.preprocessing.image.load_img(path1,target_size=(224,224))
            image1_array=keras.preprocessing.image.img_to_array(image1)
            image1_array=image1_array/255#数据归一化
            test_image.append(image1_array)#生成图片测试集
        test_image_array=np.stack(test_image)

以上基础数据就准备好了,(1867, 224, 224, 3)我在这解释一下这4个维度的数据分辨代表什么,第一个是图片张数,第二个是x轴像素,第三个是y轴像素,最后一个是颜色的rgb通道,如果是黑白就是1,彩色就是3,当然处理过的图片也会出现2的情况,这个结合实际情况而定。

3、模型搭建

这里我的卷积层直接采用了预训练模型 MobileNetV2,当然我们也可以自己搭建神经网络结构。这里我主要讲解一下卷积和池化的概念:卷积按我的理解其实就是对图片特征值提取的一个过程,通过定义的卷积核(在卷积核上定义好权重)然后把图片从左到右从上到下全部摸一遍,最终得到一个卷积核认识过后的初始图像数据,池化层就是在卷积后将图像数据进一步缩小,把厚度进一步加深。这里激活函数我选择的relu,使用的是函数api格式搭建的模型,用了一次Droput防止过拟合

        mobile_net=keras.applications.MobileNetV2(input_shape=
        (224,224,3),include_top=False)#使用预训练模型,不包括头部
        inputs=keras.Input(shape=(224,224,3))
        x=mobile_net(inputs)

        x=keras.layers.GlobalAveragePooling2D()(x)#将卷积后的数据 展平
        x1=keras.layers.Dense(1024,activation='relu')(x)#构建全链接层
        x2=keras.layers.Dropout(0.5)(x1)
        out_type=keras.layers.Dense(17,activation='softmax')(x2)#构建输出层
        model=keras.Model(inputs=inputs,outputs=out_type)
        model.summary()

4、模型初始化和模型训练以及模型保存

这里没有什么可说的,但请注意,因为我使用定制函数,所以我只保存模型的权重。

[En]

There is nothing to say here, but notice that because I use custom functions, I only save the weights of the model.

python深度学习tensorflow和fme结合,实现档案扫描件数据自动分类

可以看到准确率已经到 了百分之98 而且拟合度非常之好。

        model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.0001),loss='sparse_categorical_crossentropy',metrics=['acc'])

model.fit(train_image_array,train_label,epochs=5,batch_size=4,validation_data=(test_image_array,test_label))
        model.save_weights('23333.h5')

5、模型再优化

首先我要说明,模型的优化最好的方法就是增大数据集,但是很多时候我们并没有这个条件,所以这里就引入了图像增强的方法,由于扫描件数据格式都比较接近,而且训练的结果已经非常理想,我并没有做图像增强,但是我还是简单介绍一下图像增强的原理。过拟合通常发生在训练样本较少时。数据增强采用的方法是从现有示例中生成额外的训练数据,方法是使用随机变换来增强它们,从而产生看起来可信的图像。这有助于将模型暴露于数据的更多方面并更好地概括。 您将使用以下Keras预处理层实现数据增强:tf.keras.layers.RandomFlip,tf.keras.layers.RandomRotation,和tf.keras.layers.RandomZoom。这些可以像其他层一样包含在您的模型中,并在 GPU 上运行。

data_augmentation = keras.Sequential(  [ layers.RandomFlip("horizontal",                                                            input_shape=(img_height,  img_width,  3)),    
                                         layers.RandomRotation(0.1),    
                                         layers.RandomZoom(0.1),  ] )

6、使用模型进行预测

python深度学习tensorflow和fme结合,实现档案扫描件数据自动分类

        pass
    def input(self,feature):

        image=keras.preprocessing.image.load_img(feature.getAttribute('path_windows'),target_size=(224,224))
        image_array=keras.preprocessing.image.img_to_array(image)
        image_array=image_array/255#数据归一化

        train_image_array=np.expand_dims(image_array,0)#拓展维度
        #模型结构
        mobile_net=keras.applications.MobileNetV2(input_shape=(224,224,3),include_top=False)#使用预训练模型,不包括头部
        inputs=keras.Input(shape=(224,224,3))
        x=mobile_net(inputs)

        x=keras.layers.GlobalAveragePooling2D()(x)#将卷积后的数据 展平
        x1=keras.layers.Dense(1024,activation='relu')(x)#构建全链接层
        x2=keras.layers.Dropout(0.5)(x1)
        out_type=keras.layers.Dense(17,activation='softmax')(x2)#构建输出层
        model=keras.Model(inputs=inputs,outputs=out_type)

        #模型数据初始化(优化函数和loss函数)
       # model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.0001),loss='sparse_categorical_crossentropy',metrics=['acc'])
        #
        model.load_weights('23333.h5')

        a=model(train_image_array)
        #print(a)
        b=np.argmax(a[0])
      #  print(b)
        feature.setAttribute("分类结果",int(b))
        self.pyoutput(feature)

然后使用filecopy将预测结果整理好转换为文本,并输出为成果

python深度学习tensorflow和fme结合,实现档案扫描件数据自动分类

总结

通过一个多月的学习,我大概看了接近30个g的各种教程,4本书,从机器学习算法到深度学习实战,也走过很多弯路,自学的过程是痛苦而快乐的,痛苦就痛苦在出错时候你不知道问题出在哪儿,记得当时因为一个显卡的vs编译器版本低导致一直无法训练,我排查了一整天,就用控制变量法,一步一步的试错。快乐就是代码成功运行的时候,那种充实感,成就感可能是任何东西都无法比拟的。还有一点就是关于深度学习,现在网上的教程鱼龙混杂,最终我得出结论,真要学东西,就得去翻官网 翻源码,只要达到这一步,你才能对代码有自己的认识,而不仅仅是一个码代码抄代码的工具。

Original: https://blog.csdn.net/weixin_57664381/article/details/121325069
Author: 努力的悟空
Title: python深度学习tensorflow和fme结合,实现档案扫描件数据自动分类

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/514254/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球