【问题标题】:Deep Learning CNN image preprocessing深度学习 CNN 图像预处理
【发布时间】:2020-07-21 08:11:14
【问题描述】:

这是我用来加载和预处理图像的几行代码

# Loading the images and their labels
# Lists to load data into
x = [] # images
y = [] # labels

# Path to folder with training images
base = "/content/flower_tpu/flower_tpu/flowers_google/flowers_google//"


# Iterating to store images and labels in their respective lists
for idx in range(len(df_flowers)):
  # get the flower row
  flower = df_flowers.iloc[idx]
  # create flower path
  path = f"{base}{flower.id}.jpeg"
  #load image
  img = Image.open(path)
  # convert to numpy
  img = np.array(img)
  # Remove noise using Gaussian Blur
  blur = cv2.GaussianBlur(img, (5, 5), 0)
  # Segmentation
  gray = cv2.cvtColor(blur, cv2.COLOR_RGB2GRAY)
  ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
  # Further noise removal (Morphology)
  kernel = np.ones((3, 3), np.uint8)
  opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)
  # sure background area
  sure_bg = cv2.dilate(opening, kernel, iterations=3)
  # Finding sure foreground area
  dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5)
  ret, sure_fg = cv2.threshold(dist_transform, 0.7 * dist_transform.max(), 255, 0)
  # Finding unknown region
  sure_fg = np.uint8(sure_fg)
  unknown = cv2.subtract(sure_bg, sure_fg)
  # Marker labelling
  ret, markers = cv2.connectedComponents(sure_fg)
  # Add one to all labels so that sure background is not 0, but 1
  markers = markers + 1
  # Now, mark the region of unknown with zero
  markers[unknown == 255] = 0
  markers = cv2.watershed(img, markers)
  img[markers == -1] = [255, 0, 0]
  #save to X
  x.append(markers)
  # get label
  label = df_labels[df_labels['flower_class'] == flower.flower_cls].label.values[0]
  # save to y
  y.append(label)

代码有效,但它将图像的形状从 (224,224,3) 更改为 (224,224)

因此,当我尝试使用 VGG16 模型来训练这个模型时,我得到了这个错误:

Input 0 of layer block1_conv1 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 224, 224]

我该如何解决这个问题?

【问题讨论】:

    标签: python machine-learning image-processing image-recognition vgg-net


    【解决方案1】:
    from PIL import Image
    import numpy as np
    x = []
    path = "data/25_12024_010.jpg"
    #load image
    img = Image.open(path)
    # convert to numpy
    img = np.array(img)
    # Remove noise using Gaussian Blur
    blur = cv2.GaussianBlur(img, (5, 5), 0)
    # Segmentation
    gray = cv2.cvtColor(blur, cv2.COLOR_RGB2GRAY)
    ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    # Further noise removal (Morphology)
    kernel = np.ones((3, 3), np.uint8)
    opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)
    # sure background area
    sure_bg = cv2.dilate(opening, kernel, iterations=3)
    # Finding sure foreground area
    dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5)
    ret, sure_fg = cv2.threshold(dist_transform, 0.7 * dist_transform.max(), 255, 0)
    # Finding unknown region
    sure_fg = np.uint8(sure_fg)
    unknown = cv2.subtract(sure_bg, sure_fg)
    # Marker labelling
    ret, markers = cv2.connectedComponents(sure_fg)
    # Add one to all labels so that sure background is not 0, but 1
    markers = markers + 1
    # Now, mark the region of unknown with zero
    markers[unknown == 255] = 0
    markers = cv2.watershed(img, markers)
    img[markers == -1] = [255, 0, 0]
    #save to X
    x.append(markers)
    
    print(x[0].shape) # (120,120)
    
    markers = np.stack((markers,)*3, axis=-1)
    
    x.append(markers)
    
    print(x[1].shape) # (120,120,3)
    

    刚刚测试了您的代码,markers 为您提供了一个二维数组,因此您只需将其转换为一个 3 维数组(3 通道图像)。

    只需在x.append(markers)之前添加以下行

    markers = np.stack((markers,)*3, axis=-1)

    【讨论】:

    • 这会将图像变为黑屏! :(
    • 因为它是一个面具!我刚刚修正了你的错误,但你做的不对。
    • 尝试乘以 255,markers = np.stack((markers,)*3, axis=-1)*255
    • @JosephJacob 如果有帮助,请接受/支持我的回答 :)
    • 我的声望还不到 15 岁:P。所以赞成票不会公开显示:/.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    相关资源
    最近更新 更多