1 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個贊
在每行上方添加注釋以解釋其目的:
#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
要處理彩色圖像,數(shù)組中的第四個維度的大小應(yīng)為 3,表示RGB 值。
添加回答
舉報