Dau*_*hev 4 object-detection depth tensorflow
有没有办法在 Tensorflow 的对象检测 API 中使用针对 RGB 图像进行训练的预训练模型,用于单通道灰度图像(深度)?
我尝试使用以下方法在 Tensorflow 中使用预训练模型 (faster_rcnn_resnet101_coco_11_06_2017) 对灰度(1 通道图像)执行对象检测。它确实对我有用。
该模型是在 RGB 图像上进行训练的,因此我只需修改object_detection_tutorial.ipynb中的某些代码(可在 Tensorflow Repo 中找到)。
第一个更改: 请注意 ipynb 中的现有代码是为 3 通道图像编写的,因此更改 load_image_into_numpy 数组函数,如下所示
从
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
Run Code Online (Sandbox Code Playgroud)
到
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
channel_dict = {'L':1, 'RGB':3} # 'L' for Grayscale, 'RGB' : for 3 channel images
return np.array(image.getdata()).reshape(
(im_height, im_width, channel_dict[image.mode])).astype(np.uint8)
Run Code Online (Sandbox Code Playgroud)
第二个变化:灰度图像只有 1 个通道的数据。为了执行目标检测,我们需要 3 个通道(推理代码是为 3 个通道编写的)
这可以通过两种方式实现。a) 将单通道数据复制到另外两个通道中 b) 用零填充其他两个通道。两种方法都可以,我用的是第一种方法
在 ipynb 中,转到读取图像的部分并将它们转换为 numpy 数组(ipynb 末尾的 forloop)。
更改代码来自:
for image_path in TEST_IMAGE_PATHS:
image = Image.open(image_path)
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = load_image_into_numpy_array(image)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
Run Code Online (Sandbox Code Playgroud)
对此:
for image_path in TEST_IMAGE_PATHS:
image = Image.open(image_path)
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = load_image_into_numpy_array(image)
if image_np.shape[2] != 3:
image_np = np.broadcast_to(image_np, (image_np.shape[0], image_np.shape[1], 3)).copy() # Duplicating the Content
## adding Zeros to other Channels
## This adds Red Color stuff in background -- not recommended
# z = np.zeros(image_np.shape[:-1] + (2,), dtype=image_np.dtype)
# image_np = np.concatenate((image_np, z), axis=-1)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
Run Code Online (Sandbox Code Playgroud)
就是这样,运行该文件,您应该会看到结果。这些是我的结果
| 归档时间: |
|
| 查看次数: |
3961 次 |
| 最近记录: |