Skip to Content
PyTorch 中的 AI 和 ML 编码
book

PyTorch 中的 AI 和 ML 编码

by Laurence Moroney
July 2025
Beginner to intermediate
444 pages
6h 20m
Chinese
O'Reilly Media, Inc.
Content preview from PyTorch 中的 AI 和 ML 编码

第 3 章 Go beyond the Basics:检测图像中的特征

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

第 2 章中,你学习了如何 通过创建一个简单的神经网络,将时尚 MNIST 数据集的输入像素与 10 个标签进行匹配,每个标签代表一种服装类型(或类别),从而开始学习计算机视觉。虽然你创建的网络在检测服装类型方面相当出色,但也存在明显的缺陷。您的神经网络是在小型单色图像上训练的,每张图像只包含一件衣服,而且每件衣服都位于图像的中心。

为了让模型更上一层楼,您需要它能够检测图像中的特征。因此,举例来说,我们不能只看图像中的原始像素,如果我们能将图像过滤成组成元素呢?匹配这些元素而不是原始像素,将有助于模型更有效地检测图像内容。例如,请看我们在上一章中使用的时尚 MNIST 数据集。在检测鞋子时,神经网络可能会被图像底部聚集的大量深色像素激活,并将其视为鞋底。但是,如果鞋的位置不居中,而且没有填满整个画面,这种逻辑就不成立了。

检测特征的一种方法来自于摄影和图像处理方法,你可能已经对它们非常熟悉了。如果你曾经使用过 Photoshop 或 GIMP 等工具来锐化图像,那么你就使用过对图像像素进行处理的数学滤镜。这些滤波器的另一个词是卷积,通过在神经网络中使用这种滤波器,你将创建一个卷积神经网络(CNN)。

在本章中,你将首先学习如何使用卷积来检测图像中的特征。然后,你将深入了解如何根据图像中的特征对图像进行分类。我们还将探索图像增强以获取更多特征,以及迁移学习以获取由他人学习到的已有特征,然后我们将简要了解通过使用 dropout 来优化模型。

卷积

卷积简单来说就是 一个权重滤波器,用来将一个像素乘以其邻近像素,从而得到该像素的新值。例如,请看时尚 MNIST 中的踝靴图像及其像素值(见图 3-1)。

图 3-1. 使用卷积的踝靴

如果我们观察选区中间的像素,就会发现它的值为 192。(回想一下,时尚 MNIST 使用的是像素值从 0 到 255 的单色图像)。上方和左侧的像素值为 0,紧接着上方的像素值为 64,等等。

如果我们在相同的 3 × 3 网格中定义一个滤波器(如原始值下方所示),就可以通过计算一个新值来转换该像素。方法是将网格中每个像素的当前值乘以滤波器网格中相同位置的值,然后求和。这个总和就是当前像素的新值,然后我们对图像中的所有像素重复计算。

因此,在本例中,选区中心像素的当前值为 192,我们计算应用滤镜后的新值如下:

new_val = (1 * 0) + (0 * 64) + (2 * 128) + 
     (.5 * 48) + (4.5 * 192) + (1.5 * 144) + 
     (1.5 * 142) + (2 * 226) + (3 * 168)

结果等于 577,这就是该像素的新值。对图像中的每个像素重复这一过程,就能得到滤波后的图像。

现在,让我们考虑一下 滤镜对更复杂图像的影响:具体来说,就是 SciPy 内置的便于测试的上升图像。这是一张 512 × 512 的灰度图像,显示两个人在爬楼梯。

使用左侧为负值、右侧为正值、中间为零的滤波器,最终会去除图像中除垂直线以外的大部分信息(见图 3-2)。

图 3-2. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

在企业中实施 MLOps

在企业中实施 MLOps

Yaron Haviv, Noah Gift
图解大模型 : 生成式AI 原理与实战

图解大模型 : 生成式AI 原理与实战

Jay Alammar, Maarten Grootendorst

Publisher Resources

ISBN: 9798341662599