有没有办法确保我的 ImageDataGenerator 的 preprocessing_function 正常工作?
Is there a way to ensure that my ImageDataGenerator's preprocessing_function works properly?
我的 preprocessing_function 检测人脸并对其进行模糊处理。如何绘制来自 ImageDataGenerator 的图像以确保它有效?代码如下:
haarcascades_loc = "libopencv-4.0.1-hbb9e17c_0/Library/etc/haarcascades/haarcascade_profileface.xml"
pface = cv2.CascadeClassifier(haarcascades_loc)
def BlurFaces(image):
gray_fr = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray_fr = np.array(gray_fr, dtype='uint8')
faces = pface.detectMultiScale(gray_fr, 1.3, 5)
for (x, y, w, h) in faces:
blur_face = image[y:y+h, x:x+w]
blur_face = cv2.GaussianBlur(blur_face,(23, 23), 30)
image[y:y+blur_face.shape[0], x:x+blur_face.shape[1]] = blur_face
return image
datagen = ImageDataGenerator(validation_split=0.20, preprocessing_function=BlurFaces)
train_generator = datagen.flow_from_directory(
directory=r"State Farm Distracted Driver Detection\imgs\train",
target_size=(224, 224),
color_mode="rgb",
batch_size=128, #32, 64, 128, 256 or 512
class_mode="categorical",
shuffle=True,
seed=42,
subset="training",
)
valid_generator = datagen.flow_from_directory(
directory=r"State Farm Distracted Driver Detection\imgs\train",
target_size=(224, 224),
color_mode="rgb",
batch_size=128,
class_mode="categorical",
shuffle=True,
seed=42,
subset="validation",
)
编辑:我用这段代码检查图像
images, labels=next(train_generator)
print(batch[0].shape)
images=batch[0][0]
print (images.shape)
plt.imshow(image.astype(np.uint8))
好吧,您需要使用 .flow or.flow_from_directory 或 .flow_from_dataframe 向生成器提供一些图像。例如
train_gen=datagen.flow_from_directory(等)然后尝试
images, labels=next(train_gen)
图像的形状为(batch_size、高度、宽度、通道)
绘制图像以查看是否得到您期望的结果。请注意 preprocessing_function 必须 return 与您尺寸相同的图像
在 target_size 中指定并且必须具有与指定相同的通道数
通过 color_mode.
我的 preprocessing_function 检测人脸并对其进行模糊处理。如何绘制来自 ImageDataGenerator 的图像以确保它有效?代码如下:
haarcascades_loc = "libopencv-4.0.1-hbb9e17c_0/Library/etc/haarcascades/haarcascade_profileface.xml"
pface = cv2.CascadeClassifier(haarcascades_loc)
def BlurFaces(image):
gray_fr = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray_fr = np.array(gray_fr, dtype='uint8')
faces = pface.detectMultiScale(gray_fr, 1.3, 5)
for (x, y, w, h) in faces:
blur_face = image[y:y+h, x:x+w]
blur_face = cv2.GaussianBlur(blur_face,(23, 23), 30)
image[y:y+blur_face.shape[0], x:x+blur_face.shape[1]] = blur_face
return image
datagen = ImageDataGenerator(validation_split=0.20, preprocessing_function=BlurFaces)
train_generator = datagen.flow_from_directory(
directory=r"State Farm Distracted Driver Detection\imgs\train",
target_size=(224, 224),
color_mode="rgb",
batch_size=128, #32, 64, 128, 256 or 512
class_mode="categorical",
shuffle=True,
seed=42,
subset="training",
)
valid_generator = datagen.flow_from_directory(
directory=r"State Farm Distracted Driver Detection\imgs\train",
target_size=(224, 224),
color_mode="rgb",
batch_size=128,
class_mode="categorical",
shuffle=True,
seed=42,
subset="validation",
)
编辑:我用这段代码检查图像
images, labels=next(train_generator)
print(batch[0].shape)
images=batch[0][0]
print (images.shape)
plt.imshow(image.astype(np.uint8))
好吧,您需要使用 .flow or.flow_from_directory 或 .flow_from_dataframe 向生成器提供一些图像。例如 train_gen=datagen.flow_from_directory(等)然后尝试
images, labels=next(train_gen)
图像的形状为(batch_size、高度、宽度、通道) 绘制图像以查看是否得到您期望的结果。请注意 preprocessing_function 必须 return 与您尺寸相同的图像 在 target_size 中指定并且必须具有与指定相同的通道数 通过 color_mode.