contents

foreword

preface

acknowledgments

about this book

about the authors

about the cover illustration

Part 1: Core PyTorch

1 Introducing deep learning and the PyTorch Library

1.1  The deep learning revolution

1.2  PyTorch for deep learning

1.3  Why PyTorch?

The deep learning competitive landscape

1.4  An overview of how PyTorch supports deep learning projects

1.5  Hardware and software requirements

Using Jupyter Notebooks

1.6  Exercises

1.7  Summary

2 Pretrained networks

2.1  A pretrained network that recognizes the subject of an image

Obtaining a pretrained network for image recognition 19 AlexNet

ResNet

Ready, set, almost run 22 Run! 25

2.2  A pretrained model that fakes it until it makes it

The GAN game

CycleGAN

A network that turns horses into zebras

2.3  A pretrained network that describes scenes

NeuralTalk2

2.4  Torch Hub

2.5  Conclusion

2.6  Exercises

2.7  Summary

3 It starts with a tensor

3.1  The world as floating-point numbers

3.2  Tensors: Multidimensional arrays

From Python lists to PyTorch tensors

Constructing our first tensors

The essence of tensors

3.3  Indexing tensors

3.4  Named tensors

3.5  Tensor element types

Specifying the numeric type with dtype

A dtype for every occasion

Managing a tensor’s dtype attribute

3.6  The tensor API

3.7  Tensors: Scenic views of storage

Indexing into storage

Modifying stored values: In-place operations

3.8  Tensor metadata: Size, offset, and stride

Views of another tensor’s storage

Transposing without copying

Transposing in higher dimensions 60 Contiguous tensors 60

3.9  Moving tensors to the GPU

Managing a tensor’s device attribute

3.10  NumPy interoperability

3.11  Generalized tensors are tensors, too

3.12  Serializing tensors

Serializing to HDF5 with h5py

3.13  Conclusion

3.14  Exercises

3.15  Summary

4 Real-world data representation using tensors

4.1  Working with images

Adding color channels

Loading an image file 72 Changing the layout

Normalizing the data 74

4.2  3D images: Volumetric data

Loading a specialized format

4.3  Representing tabular data

Using a real-world dataset

Loading a wine data tensor 78 Representing scores

One-hot encoding

When to categorize

Finding thresholds 84

4.4  Working with time series

Adding a time dimension

Shaping the data by time period

Ready for training

4.5  Representing text

Converting text to numbers

One-hot-encoding characters 94 One-hot encoding whole words

Text embeddings 98 Text embeddings as a blueprint 100

4.6  Conclusion

4.7  Exercises

4.8  Summary

5 The mechanics of learning

5.1  A timeless lesson in modeling

5.2  Learning is just parameter estimation

A hot problem

Gathering some data

Visualizing the data

Choosing a linear model as a first try

5.3  Less loss is what we want

From problem back to PyTorch

5.4  Down along the gradient

Decreasing loss

Getting analytical

Iterating to fit the model

Normalizing inputs

Visualizing (again)

5.5  PyTorch’s autograd: Backpropagating all things

Computing the gradient automatically

Optimizers a la carte

Training, validation, and overfitting 131 Autograd nits and switching it off 137

5.6  Conclusion

5.7  Exercise

5.8  Summary

6 Using a neural network to fit the data

6.1  Artificial neurons

Composing a multilayer network

Understanding the error function

All we need is activation

More activation functions

Choosing the best activation function 148 What learning means for a neural network 149

6.2  The PyTorch nn module

Using __call__ rather than forward

Returning to the linear model

6.3  Finally a neural network

Replacing the linear model

Inspecting the parameters 159 Comparing to the linear model 161

6.4  Conclusion

6.5  Exercises

6.6  Summary

7 Telling birds from airplanes: Learning from images

7.1  A dataset of tiny images

Downloading CIFAR-10

The Dataset class 166 Dataset transforms

Normalizing data 170

7.2  Distinguishing birds from airplanes

Building the dataset

A fully connected model 174 Output of a classifier

Representing the output as probabilities

A loss for classifying

Training the classifier

The limits of going fully connected 189

7.3  Conclusion

7.4  Exercises

7.5  Summary

8 Using convolutions to generalize

8.1  The case for convolutions

What convolutions do

8.2  Convolutions in action

Padding the boundary

Detecting features with convolutions

Looking further with depth and pooling 202 Putting it all together for our network 205

8.3  Subclassing nn.Module

Our network as an nn.Module

How PyTorch keeps track of parameters and submodules

The functional API

8.4  Training our convnet

Measuring accuracy

Saving and loading our model 214 Training on the GPU 215

8.5  Model design

Adding memory capacity: Width

Helping our model to converge and generalize: Regularization

Going deeper to learn more complex structures: Depth

Comparing the designs from this section

It’s already outdated

8.6  Conclusion

8.7  Exercises

8.8  Summary

Part 2: Learning from images in the real world: Early detection of lung cancer

9 Using PyTorch to fight cancer

9.1  Introduction to the use case

9.2  Preparing for a large-scale project

9.3  What is a CT scan, exactly?

9.4  The project: An end-to-end detector for lung cancer

Why can’t we just throw data at a neural network until it works?

What is a nodule?

Our data source: The LUNA Grand Challenge

Downloading the LUNA data

9.5  Conclusion

9.6  Summary

10 Combining data sources into a unified dataset

10.1  Raw CT data files

10.2  Parsing LUNA’s annotation data

Training and validation sets

Unifying our annotation and candidate data

10.3  Loading individual CT scans

Hounsfield Units

10.4  Locating a nodule using the patient coordinate system

The patient coordinate system

CT scan shape and voxel sizes

Converting between millimeters and voxel addresses

Extracting a nodule from a CT scan

10.5  A straightforward dataset implementation

Caching candidate arrays with the getCtRawCandidate function

Constructing our dataset in LunaDataset .__init__

A training/validation split

Rendering the data

10.6  Conclusion

10.7  Exercises

10.8  Summary

11 Training a classification model to detect suspected tumors

11.1  A foundational model and training loop

11.2  The main entry point for our application

11.3  Pretraining setup and initialization

Initializing the model and optimizer

Care and feeding of data loaders

11.4  Our first-pass neural network design

The core convolutions

The full model

11.5  Training and validating the model

The computeBatchLoss function

The validation loop is similar

11.6  Outputting performance metrics

The logMetrics function

11.7  Running the training script

Needed data for training

Interlude: The enumerateWithEstimate function

11.8  Evaluating the model: Getting 99.7% correct means we’re done, right?

11.9  Graphing training metrics with TensorBoard

Running TensorBoard

Adding TensorBoard support to the metrics logging function

11.10  Why isn’t the model learning to detect nodules?

11.11  Conclusion

11.12  Exercises

11.13  Summary

12 Improving training with metrics and augmentation

12.1  High-level plan for improvement

12.2  Good dogs vs. bad guys: False positives and false negatives

12.3  Graphing the positives and negatives

Recall is Roxie’s strength

Precision is Preston’s forte 326 Implementing precision and recall in logMetrics

Our ultimate performance metric: The F1 score

How does our model perform with our new metrics? 332

12.4  What does an ideal dataset look like?

Making the data look less like the actual and more like the “ideal” 336 Contrasting training with a balanced LunaDataset to previous runs

Recognizing the symptoms of overfitting 343

12.5  Revisiting the problem of overfitting

An overfit face-to-age prediction model

12.6  Preventing overfitting with data augmentation

Specific data augmentation techniques

Seeing the improvement from data augmentation

12.7  Conclusion

12.8  Exercises

12.9  Summary

13 Using segmentation to find suspected nodules

13.1  Adding a second model to our project

13.2  Various types of segmentation

13.3  Semantic segmentation: Per-pixel classification

The U-Net architecture

13.4  Updating the model for segmentation

Adapting an off-the-shelf model to our project

13.5  Updating the dataset for segmentation

U-Net has very specific input size requirements

U-Net trade-offs for 3D vs. 2D data

Building the ground truth data

Implementing Luna2dSegmentationDataset

Designing our training and validation data

Implementing TrainingLuna2dSegmentationDataset

Augmenting on the GPU

13.6  Updating the training script for segmentation

Initializing our segmentation and augmentation models 387 Using the Adam optimizer

Dice loss

Getting images into TensorBoard

Updating our metrics logging 396 Saving our model 397

13.7  Results

13.8  Conclusion

13.9  Exercises

13.10  Summary

14 End-to-end nodule analysis, and where to go next

14.1  Towards the finish line

14.2  Independence of the validation set

14.3  Bridging CT segmentation and nodule candidate classification

Segmentation

Grouping voxels into nodule candidates 411 Did we find a nodule? Classification to reduce false positives 412

14.4  Quantitative validation

14.5  Predicting malignancy

Getting malignancy information

An area under the curve baseline: Classifying by diameter

Reusing preexisting weights: Fine-tuning

More output in TensorBoard

14.6  What we see when we diagnose

Training, validation, and test sets

14.7  What next? Additional sources of inspiration (and data)

Preventing overfitting: Better regularization

Refined training data

Competition results and research papers

14.8  Conclusion

Behind the curtain

14.9  Exercises

14.10  Summary

Part 3: Deployment

15 Deploying to production

15.1  Serving PyTorch models

Our model behind a Flask server

What we want from deployment

Request batching

15.2  Exporting models

Interoperability beyond PyTorch with ONNX

PyTorch’s own export: Tracing

Our server with a traced model

15.3  Interacting with the PyTorch JIT

What to expect from moving beyond classic Python/PyTorch 458 The dual nature of PyTorch as interface and backend 460 TorchScript

Scripting the gaps of traceability 464

15.4  LibTorch: PyTorch in C++

Running JITed models from C++

C++ from the start: The C++ API

15.5  Going mobile

Improving efficiency: Model design and quantization

15.6  Emerging technology: Enterprise serving of PyTorch models

15.7  Conclusion

15.8  Exercises

15.9  Summary

index

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset