【问题标题】:How prepping data works for deep learning in python [closed]准备数据如何在 python 中用于深度学习 [关闭]
【发布时间】:2020-10-02 19:42:00
【问题描述】:

我已经完成了 Kaggle learn 的深度学习课程,并开始为 MNIST Digit 数据集编写模型。我喜欢理解我学习的代码,并且我遇到过这个:

def data_prep(raw):
    out_y = keras.utils.to_categorical(raw.label, num_classes)

    num_images = raw.shape[0]
    x_as_array = raw.values[:,1:]
    x_shaped_array = x_as_array.reshape(num_images, img_rows, img_cols, 1)
    out_x = x_shaped_array / 255
    return out_x, out_y

这部分真的让我很困惑。我不明白其中的大部分。有人可以逐步解释每一行代码的作用吗?如果我要在具有多种颜色的彩色图像上执行此操作,这将如何工作? 我知道这有点宽泛。稍后,我将做一些涉及彩色图像的事情,但我不确定该怎么做,因为我可以看到黑白“参数”(数组整形中的 1,除以第255章)

旁注:raw 是 pandas 数据框

【问题讨论】:

  • 这个问题太宽泛了。 Here is a great guide to debugging 那应该给你一个开始的地方。您是否尝试在每个步骤之后检查每个变量的输出以理解它?这将是迈出的第一步

标签: python keras deep-learning


【解决方案1】:

在每行上方添加 cmets 以说明其用途:

#input is a 2D dataframe of images
def data_prep(raw):
    #convert the classes in raw to a binary matrix
    #also known as one hot encoding and is typically done in ML
    out_y = keras.utils.to_categorical(raw.label, num_classes)

    #first dimension of raw is the number of images; each row in the df represents an image
    num_images = raw.shape[0]

    #remove the first column in each row which is likely a header and convert the rest into an array of values
    #ML algorithms usually do not take in a pandas dataframe 
    x_as_array = raw.values[:,1:]

    #reshape the images into 3 dimensional
    #1st dim: number of images
    #2nd dim: height of each image (i.e. rows when represented as an array)
    #3rd dim: width of each image (i.e. columns when represented as an array)
    #4th dim: the number of pixels which is 3 (RGB) for colored images and 1 for gray-scale images
    x_shaped_array = x_as_array.reshape(num_images, img_rows, img_cols, 1)

    #this normalizes (i.e. 0-1) the image pixels since they range from 1-255. 
    out_x = x_shaped_array / 255

    return out_x, out_y

要处理彩色图像,数组中的第 4 个维度的大小应为 3,代表 RGB values。查看此tutorial,了解有关 CNN 及其输入的更深入信息。

【讨论】:

  • 谢谢,现在这更有意义了!
猜你喜欢
  • 1970-01-01
  • 2017-12-25
  • 2018-03-25
  • 2015-08-17
  • 2016-07-25
  • 1970-01-01
  • 2019-02-03
  • 2015-10-09
  • 2017-03-04
相关资源
最近更新 更多