项目实战解析:基于深度学习搭建卷积神经网络模型算法,实现图像识别分类

这篇具有很好参考价值的文章主要介绍了项目实战解析:基于深度学习搭建卷积神经网络模型算法,实现图像识别分类。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


前言

随着人工智能的不断发展,深度学习这门技术也越来越重要,很多人都开启了学习机器学习,本文将通过项目开发实例,带领大家从零开始设计实现一款基于深度学习的图像识别算法。
学习本章内容, 你需要掌握以下基础知识:

  1. Python 基础语法
  2. 计算机视觉库(OpenCV)
  3. 深度学习框架(TensorFlow)
  4. 卷积神经网络(CNN)

一、基础知识介绍

  1. Python
    Python 是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。学习链接:Python学习

  2. OpenCV
    OpenCV 是一个开源的跨平台计算机视觉库。实现了图像处理和计算机视觉 方面的很多通用算法。在本设计中它主要集中在图像的采集以及图像预处理等功能。学习链接:OpenCV学习

  3. TensorFlow
    TensorFlow 是谷歌开源的计算框架,TensorFlow 框架可以很好地支持深度学 习的各种算法。本课设图像识别部分采用的深度学习(deep learning)算法就是 用 TensorFlow 框架实现的。学习链接:TensorFlow学习

  4. CNN
    卷积神经网络(Convolutional Neural Networks, CNN)是一类包含卷积计算 且具有深度结构的前馈神经网络(Feedforward Neural Networks),是深度学习(deep learning)的代表算法之一。学习链接:CNN学习

二、数据集收集

在进行图像识别前,首先需要收集数据集(数据集下载),其次对于数据集做预处理,然后才能通过深度卷积神经网络来进行特征学习,得到估计分类模型。对于数据集的要求,在卷积神经网络(CNN)中,由于对输入图像向量的权值参数的数量是固定的,所以在用卷积网络(CNN)对数据集进行模型训练前需要进行图像预处理,保证输入的图像尺寸是固定一致的。
基于深度学习中图像分类和物体检测项目实战,人工智能,深度学习,神经网络,图像处理,计算机视觉,图像分类

图一 分类网络模型流程图

本案例以实现垃圾识别分类作为最终实现目标, 收集数据集包含四类图片,分别为厨余垃圾、可回收垃圾、有毒垃圾、其它垃圾,每类图片数据集规模为200张(学习者可以根据自己需求选择数据集类型及规模。数据集下载

基于深度学习中图像分类和物体检测项目实战,人工智能,深度学习,神经网络,图像处理,计算机视觉,图像分类

图二 数据集目录

数据集图片预处理代码如下:

#数据图片rename
#数据集路径:(self.image_path = "./picture/")
   def rename(self):
        listdir = os.listdir(self.image_path)
        i = 0
        while i < len(listdir):
            images_list_dir = os.listdir(os.path.join(self.image_path, listdir[i]))
            j = 0
            while j < len(images_list_dir):
                old_name = os.path.join(self.image_path, listdir[i], images_list_dir[j])
                new_name = os.path.join(self.image_path, "%d-%d" % (i, j) + ".jpg")
                os.rename(old_name, new_name)
                j += 1
            i += 1
        for p in range(len(listdir)):
            tmp_path = os.path.join(self.image_path, listdir[p])
            if os.path.exists(tmp_path):
                os.removedirs(tmp_path)
#图片resize
 def resize_img(self):
        listdir = os.listdir(self.image_path)
        for file in listdir:
            file_path = os.path.join(self.image_path, file)
            try:
                imread = cv2.imread(file_path)
                resize = cv2.resize(imread, (200, 200))
                cv2.imwrite(os.path.join(self.image_path, file), resize)
            except Exception:
                os.remove(file_path)
                continue

基于深度学习中图像分类和物体检测项目实战,人工智能,深度学习,神经网络,图像处理,计算机视觉,图像分类

图三 预处理后数据集示例
#转存图片信息到csv文件
#csv生成路径:(csv_file_saved_path = "./picture/")
def train_data_to_csv(self):
        files = os.listdir(self.image_path)
        data = []
        for file in files:
            data.append({"path": self.image_path + file, "label": file[0]})

        frame = pd.DataFrame(data, columns=['path', 'label'])
        dummies = pd.get_dummies(frame['label'], 'label')
        concat = pd.concat([frame, dummies], 1)
        concat.to_csv(csv_file_saved_path + "train.csv")

基于深度学习中图像分类和物体检测项目实战,人工智能,深度学习,神经网络,图像处理,计算机视觉,图像分类

图四 数据集转存为csv示例

三、模型训练

模型训练代码如下:

#模型训练算法
def build_model():
    with tf.name_scope("input"):
        x = tf.placeholder(tf.float32, [None, 200, 200, 3], "x")
        y = tf.placeholder(tf.float32, [None, 5], "y")

    with tf.variable_scope("conv_layer_1"):
        conv1 = tf.layers.conv2d(x, 64, [3, 3], activation=tf.nn.relu, name='conv1')
        max1 = tf.layers.max_pooling2d(conv1, [2, 2], [2, 2])
        bn1 = tf.layers.batch_normalization(max1, name='bn1')
        output1 = tf.layers.dropout(bn1, name='droput')

    with tf.variable_scope("conv_layer_2"):
        conv2 = tf.layers.conv2d(output1, 64, [3, 3], activation=tf.nn.relu, name='conv2')
        max2 = tf.layers.max_pooling2d(conv2, [2, 2], [2, 2], name='max2')
        bn2 = tf.layers.batch_normalization(max2)
        output2 = tf.layers.dropout(bn2, name='dropout')

    with tf.variable_scope("conv_layer_3"):
        conv3 = tf.layers.conv2d(output2, 64, [3, 3], activation=tf.nn.relu, name='conv3')
        max3 = tf.layers.max_pooling2d(conv3, [2, 2], [2, 2], name='max3')
        bn3 = tf.layers.batch_normalization(max3, name='bn3')
        output3 = bn3

    with tf.variable_scope("conv_layer_4"):
        conv4 = tf.layers.conv2d(output3, 32, [3, 3], activation=tf.nn.relu, name='conv4')
        max4 = tf.layers.max_pooling2d(conv4, [2, 2], [2, 2], name='max4')
        bn4 = tf.layers.batch_normalization(max4, name='bn4')
        output = bn4
        flatten = tf.layers.flatten(output, 'flatten')

    with tf.variable_scope("fc_layer1"):
        fc1 = tf.layers.dense(flatten, 256, activation=tf.nn.relu)
        fc_bn1 = tf.layers.batch_normalization(fc1, name='bn1')
        dropout1 = tf.layers.dropout(fc_bn1, 0.5)

    with tf.variable_scope("fc_layer2"):
        fc2 = tf.layers.dense(dropout1, 128, activation=tf.nn.relu)
        dropout2 = tf.layers.dropout(fc2)

    with tf.variable_scope("fc_layer3"):
        fc3 = tf.layers.dense(dropout2, 64)
        dropout3 = tf.layers.dropout(fc3)

    with tf.variable_scope("fc_layer4"):
        fc4 = tf.layers.dense(dropout3, 32)

    with tf.variable_scope("fc_layer5"):
        fc5 = tf.layers.dense(fc4, 5)

    softmax = tf.nn.softmax(fc5, name='softmax')
    predict = tf.argmax(softmax, axis=1)
    loss = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits_v2(logits=fc5, labels=y, name='loss'))
    tf.summary.scalar("loss", loss)
    accuracy = tf.reduce_mean(tf.cast(tf.equal(predict, tf.argmax(y, axis=1)), tf.float32))
    tf.summary.scalar("acc", accuracy)
    merged = tf.summary.merge_all()
    return x, y, predict, loss, accuracy, merged, softmax

基于深度学习中图像分类和物体检测项目实战,人工智能,深度学习,神经网络,图像处理,计算机视觉,图像分类

图五 神经网络结构图
#模型训练
    def train(self):
        saver = tf.train.Saver(max_to_keep=3)
        sess = tf.InteractiveSession(config=self.config)
        sess.run(tf.global_variables_initializer())
        writer = tf.summary.FileWriter("./log", graph=sess.graph)
        for i in range(100):
            j = 1
            all_loss_ = 0
            all_acc_ = 0
            while j <= self.batches and self.has_next_batch:
                train_x, train_y = self.next_batch()
                _, loss_, accuracy_, merged_ = sess.run([ self.opt, self.loss, self.accuracy, self.merged ],
                                                        feed_dict={self.x: train_x, self.y: train_y})
                all_loss_ += loss_
                all_acc_ += accuracy_
                print("\repoch %d-- batch: %d-->    " % (i, j) + "=" * j + ">" + "-" * (
                        self.batches - j) + "\t\t loss: %.4f, acc: %.4f" % (
                          loss_, accuracy_), )
                j += 1
                writer.add_summary(merged_, i * self.batches + j - 1)
            print("\n===epoch %d===    >    mean loss is : %.4f, mean acc is : %.4f" % (
                i, all_loss_ / self.batches, all_acc_ / self.batches))
            test_x, test_y = self.get_test_data()
            test_loss_, test_acc_ = sess.run([ self.loss, self.accuracy ],
                                             feed_dict={self.x: test_x[ 0:16 ], self.y: test_y[ 0:16 ]})
            print("===epoch %d===    >    test loss is : %.4f, test acc is : %.4f" % (
                i, test_loss_, test_acc_))
            self.start = 0
            self.has_next_batch = True
            if i % 5 == 0:
                saver.save(sess, "./h5_dell/mode.ckpt", i)
        sess.close()

基于深度学习中图像分类和物体检测项目实战,人工智能,深度学习,神经网络,图像处理,计算机视觉,图像分类

图六 训练模型输出文件

四、图像识别分类

图像识别分类代码:

#利用模型实时识别图像
    def predict_value(self, type='image', image_path=None):
        saver = tf.train.Saver()
        sess = tf.InteractiveSession()
        saver.restore(sess, tf.train.latest_checkpoint("./h5_dell1/"))
        if type == 'image':
            assert image_path is not None
            image = cv2.imread(image_path)
            image = cv2.resize(image, (200, 200))
            image = np.asarray(image, np.float32) / 255.
            image = np.reshape(image, (1, image.shape[ 0 ], image.shape[ 1 ], image.shape[ 2 ]))
            [ predict, probab ] = sess.run([ self.predict, self.probab ], feed_dict={self.x: image})
           # predict = sess.run(self.predict, feed_dict={self.x: image})
           # print("what? 1:",np.max(probab))
           # print("what? 2:",predict[0])
            return predict[0]
            if (np.max(probab)<1):
                print("recognise fail")
                predict=4
            print(predict)

        elif type == 'video':
            capture = cv2.VideoCapture(0)
            while True:
                ret, frame = capture.read()
                resize = cv2.resize(frame, (200, 200))
                x_ = np.asarray(resize, np.float32) / 255.
                x_ = np.reshape(x_, [ 1, x_.shape[ 0 ], x_.shape[ 1 ], x_.shape[ 2 ] ])
                [ predict, probab ] = sess.run([ self.predict, self.probab ], feed_dict={self.x: x_})
                if predict == 0:
                    cv2.putText(frame, "0 probab: %.3f" % np.max(probab), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,
                                (0, 0, 255), 2, cv2.LINE_AA)
                elif predict == 1:
                    cv2.putText(frame, "1 probab: %.3f" % np.max(probab), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,
                                (0, 255, 255), 2, cv2.LINE_AA)
                elif predict == 2:
                    cv2.putText(frame, "2 probab: %.3f" % np.max(probab), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,
                                (0, 255, 0), 2, cv2.LINE_AA)
                elif predict == 3:
                    cv2.putText(frame, "3 probab: %.3f" % np.max(probab), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,
                                (255, 0, 255), 2, cv2.LINE_AA)
                elif predict == 4:
                    cv2.putText(frame, "4 probab: %.3f" % np.max(probab), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,
                                (255, 0, 255), 2, cv2.LINE_AA)
                if predict==3:
                    print("1111")

                print(predict)

                cv2.imshow("recognized", frame)
                key = cv2.waitKey(1)
                if key == 27:
                    break
            cv2.destroyAllWindows()
            capture.release()

基于深度学习中图像分类和物体检测项目实战,人工智能,深度学习,神经网络,图像处理,计算机视觉,图像分类

图七 蔬菜类图像识别结果

基于深度学习中图像分类和物体检测项目实战,人工智能,深度学习,神经网络,图像处理,计算机视觉,图像分类

图八 易拉罐类图片实现效果

总结

本章节内容以实际项目案例介绍神经网络图像识别算法的搭建及使用详细步骤,介 绍卷积神经网络实现图像识别分类的详细过程,以及实现效果的展示,希望文章能够帮助到大家,有问题请点击【链接】沟通交流, 获取源码。文章来源地址https://www.toymoban.com/news/detail-779300.html

到了这里,关于项目实战解析:基于深度学习搭建卷积神经网络模型算法,实现图像识别分类的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 毕设项目分享 基于机器视觉opencv的手势检测 手势识别 算法 - 深度学习 卷积神经网络 opencv python

    今天学长向大家介绍一个机器视觉项目 基于机器视觉opencv的手势检测 手势识别 算法 普通机器视觉手势检测的基本流程如下: 其中轮廓的提取,多边形拟合曲线的求法,凸包集和凹陷集的求法都是采用opencv中自带的函数。手势数字的识别是利用凸包点以及凹陷点和手部中心

    2024年02月03日
    浏览(52)
  • 深度学习入门——卷积神经网络CNN基本原理+实战

    ​ 卷积神经网络(Convolutional Neural Network,CNN)是深度学习技术中最基础的网络结构,模拟人脑工作,具备强大的特征学习能力。CNN结构主要由两部分组成:特征提取部分和分类部分color{blue}{特征提取部分和分类部分}特征提取部分和分类部分。特征提取部分网络将执行一系列

    2024年01月21日
    浏览(39)
  • 深度学习实战——卷积神经网络/CNN实践(LeNet、Resnet)

          忆如完整项目/代码详见github: https://github.com/yiru1225 (转载标明出处 勿白嫖 star for projects thanks) 本系列博客重点在深度学习相关实践(有问题欢迎在评论区讨论指出,或直接私信联系我)。 第一章  深度学习实战——不同方式的模型部署(CNN、Yolo)_如何部署cnn_

    2023年04月11日
    浏览(33)
  • Python深度学习实战-基于class类搭建BP神经网络实现分类任务(附源码和实现效果)

    实现功能 上篇文章介绍了用Squential搭建BP神经网络,Squential可以搭建出上层输出就是下层输入的顺序神经网络结构,无法搭出一些带有跳连的非顺序网络结构,这个时候我们可以选择类class搭建封装神经网络结构。 第一步:import tensorflow as tf:导入模块 第二步:制定输入网络

    2024年02月08日
    浏览(43)
  • 竞赛项目 深度学习的口罩佩戴检测 - opencv 卷积神经网络 机器视觉 深度学习

    🔥 优质竞赛项目系列,今天要分享的是 基于深度学习的口罩佩戴检测【全网最详细】 - opencv 卷积神经网络 机器视觉 深度学习 该项目较为新颖,适合作为竞赛课题方向,学长非常推荐! 🧿 更多资料, 项目分享: https://gitee.com/dancheng-senior/postgraduate 从2019年末开始,新型冠状

    2024年02月13日
    浏览(38)
  • Python深度学习实战-基于tensorflow原生代码搭建BP神经网络实现分类任务(附源码和实现效果)

            前面两篇文章分别介绍了两种搭建神经网络模型的方法,一种是基于tensorflow的keras框架,另一种是继承父类自定义class类,本篇文章将编写原生代码搭建BP神经网络。 本人读研期间发表5篇SCI数据挖掘相关论文,现在某研究院从事数据挖掘相关科研工作,对数据挖掘

    2024年02月08日
    浏览(38)
  • 鸟类识别Python,基于TensorFlow卷积神经网络【实战项目】

    鸟类识别系统,使用Python作为主要开发语言,基于深度学习TensorFlow框架,搭建卷积神经网络算法。并通过对数据集进行训练,最后得到一个识别精度较高的模型。并基于Django框架,开发网页端操作平台,实现用户上传一张图片识别其名称。 数据集选自加州理工学院200种鸟类

    2024年02月10日
    浏览(36)
  • Matlab深度学习入门实例:从0搭建卷积神经网络CNN(附完整代码)

    网上已具有大量卷积神经网络的讲解,故本文不在对此赘述,这篇文章针对已了解CNN基础结构和原理者,以一个例子搭建一个简单的卷积神经网络,作为正式迈入深度学习的第一步。 我们以深度学习最经典的案例——手写数字的识别,和一种经典的CNN——LeNet进行本次学习。

    2024年02月01日
    浏览(42)
  • 大数据深度学习:基于Tensorflow深度学习卷积神经网络CNN算法垃圾分类识别系统

    随着社会的发展和城市化进程的加速,垃圾分类已经成为了环境保护和可持续发展的重要课题。然而,传统的垃圾分类方法通常依赖于人工识别,效率低下且易出错。因此,本项目旨在利用大数据和深度学习技术,构建一个基于 TensorFlow 深度学习的神经网络 CNN(Convolutional

    2024年04月14日
    浏览(38)
  • 基于 Python中的深度学习:神经网络与卷积神经网络

    当下,深度学习已经成为人工智能研究和应用领域的关键技术之一。作为一个开源的高级编程语言,Python提供了丰富的工具和库,为深度学习的研究和开发提供了便利。本文将深入探究Python中的深度学习,重点聚焦于神经网络与卷积神经网络的原理和应用。 深度学习是机器学

    2024年02月07日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包