Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub
Data GenerationOur images are quite simple: They have black backgrounds and white lines drawnon top of them. The lines can be drawn either in a diagonal or in a parallel (to one ofthe edges, so they could be either horizontal or vertical) way. So, our classificationproblem can be simply stated as: Is the line diagonal?If the line is diagonal, then we assume it belongs to the positive class. If it is notdiagonal, it belongs to the negative class. We have our labels (y), which we cansummarize like this:Line Value ClassNot Diagonal 0 NegativeDiagonal 1 PositiveLet’s generate 300 random images, each one five-by-five pixels in size:Data Generation1 images, labels = generate_dataset(2 img_size=5, n_images=300, binary=True, seed=133 )And then let’s visualize the first 30 images:fig = plot_images(images, labels, n_plot=30)Classifying Images | 267
Figure 4.1 - Image datasetSince our images are quite small, there aren’t that many possibilities for drawinglines on top of them. There are actually 18 different configurations for diagonallines (nine to the left, nine to the right), and 10 different configurations forhorizontal and vertical lines (five each). That’s a total of 28 possibilities in a 300-image dataset. So, there will be lots of duplicates (like images #1 and #2, or #6 and#7, for example), but that’s fine.268 | Chapter 4: Classifying Images
- Page 242 and 243: Sigmoid Functiondef sigmoid(z):retu
- Page 244 and 245: A picture is worth a thousand words
- Page 246 and 247: OutputOrderedDict([('linear.weight'
- Page 248 and 249: The first summation adds up the err
- Page 250 and 251: IMPORTANT: Make sure to pass the pr
- Page 252 and 253: To make it clear: In this chapter,
- Page 254 and 255: argument of nn.BCEWithLogitsLoss().
- Page 256 and 257: It is not that hard, to be honest.
- Page 258 and 259: Figure 3.6 - Training and validatio
- Page 260 and 261: Outputarray([[0.5504593 ],[0.949995
- Page 262 and 263: decision boundary.Look at the expre
- Page 264 and 265: Are my data points separable?That
- Page 266 and 267: model = nn.Sequential()model.add_mo
- Page 268 and 269: It looks like this:Figure 3.10 - Sp
- Page 270 and 271: True and False Positives and Negati
- Page 272 and 273: tpr_fpr(cm_thresh50)Output(0.909090
- Page 274 and 275: The trade-off between precision and
- Page 276 and 277: Figure 3.13 - Using a low threshold
- Page 278 and 279: Figure 3.16 - Trade-offs for two di
- Page 280 and 281: thresholds do not necessarily inclu
- Page 282 and 283: actual data, it is as bad as it can
- Page 284 and 285: If you want to learn more about bot
- Page 286 and 287: Model Training1 n_epochs = 10023 sb
- Page 288 and 289: step in your journey! What’s next
- Page 290 and 291: Chapter 4Classifying ImagesSpoilers
- Page 294 and 295: Images and ChannelsIn case you’re
- Page 296 and 297: image_rgb = np.stack([image_r, imag
- Page 298 and 299: That’s fairly straightforward; we
- Page 300 and 301: • Transformations based on Tensor
- Page 302 and 303: position of an object in a picture
- Page 304 and 305: Outputtensor([[[0., 0., 0., 1., 0.]
- Page 306 and 307: Outputtensor([[[-1., -1., -1., 1.,
- Page 308 and 309: We can convert the former into the
- Page 310 and 311: composer = Compose([RandomHorizonta
- Page 312 and 313: Output<torch.utils.data.dataset.Sub
- Page 314 and 315: train_composer = Compose([RandomHor
- Page 316 and 317: The minority class should have the
- Page 318 and 319: train_loader = DataLoader(dataset=t
- Page 320 and 321: implemented in Chapter 2.1? Let’s
- Page 322 and 323: Let’s take one mini-batch of imag
- Page 324 and 325: What does our model look like? Visu
- Page 326 and 327: Model TrainingLet’s train our mod
- Page 328 and 329: preceding hidden layer to compute i
- Page 330 and 331: fig = sbs_nn.plot_losses()Figure 4.
- Page 332 and 333: Equation 4.2 - Equivalence of deep
- Page 334 and 335: w_nn_equiv = w_nn_output.mm(w_nn_hi
- Page 336 and 337: Weights as PixelsDuring data prepar
- Page 338 and 339: is only 0.25 (for z = 0) and that i
- Page 340 and 341: nn.Tanh()(dummy_z)Outputtensor([-0.
Figure 4.1 - Image dataset
Since our images are quite small, there aren’t that many possibilities for drawing
lines on top of them. There are actually 18 different configurations for diagonal
lines (nine to the left, nine to the right), and 10 different configurations for
horizontal and vertical lines (five each). That’s a total of 28 possibilities in a 300-
image dataset. So, there will be lots of duplicates (like images #1 and #2, or #6 and
#7, for example), but that’s fine.
268 | Chapter 4: Classifying Images