【发布时间】:2022-12-23 03:09:54
【问题描述】:
我正在使用 Tensorflow 对象检测 API 训练对象检测模型。我要检测的物体非常小。我是否应该裁剪图像以删除图像中不相关的对象。当我的模型被部署时,实时图像也会包含不需要的项目。为训练目的裁剪图像是一种好习惯吗?
【问题讨论】:
标签: tensorflow machine-learning tensorflow2.0 object-detection-api
我正在使用 Tensorflow 对象检测 API 训练对象检测模型。我要检测的物体非常小。我是否应该裁剪图像以删除图像中不相关的对象。当我的模型被部署时,实时图像也会包含不需要的项目。为训练目的裁剪图像是一种好习惯吗?
【问题讨论】:
标签: tensorflow machine-learning tensorflow2.0 object-detection-api
你的想法是正确的裁剪图像是注意神经元网络通过手动或自动重新排列裁剪图像的位置或相关信息。您可能会阅读许多注意力神经元网络或图像识别的作品。
有时没有背景识别会提供更好的结果,但那是当很少有物体被识别时,你可能会从许多作品中读到,但在没有太多干扰的背景下,它们表现得更好,因为鞋子粘在架子上或枕头在床上,那是什么物体- 导向项目识别。
[ 样本 ]:从输入图像手动裁剪
y1 = tf.keras.layers.Cropping2D(cropping=((start_y, pic_height - box_height - start_y), (start, pic_width - box_width - start)))(input)
y1 = tf.keras.preprocessing.image.array_to_img( 255 * np.reshape(y1, (26, 15, 3)), data_format=None, scale=True )
target_1 = tf.keras.layers.Cropping2D(cropping=((previous_start_y, pic_height - box_height - previous_start_y), (previous_start, pic_width - box_width - previous_start)))(char_1)
temp_3 = tf.where(tf.math.greater_equal( np.asarray(y1, dtype=np.float32), np.asarray(target_1, dtype=np.float32)), [1.0], [0.0]).numpy()
temp_3 = tf.math.multiply( temp_3, y1, name=None )
temp_3 = tf.keras.preprocessing.image.array_to_img( 255 * np.reshape(temp_3, (26, 15, 3)), data_format=None, scale=True )
[ 样本 ]:从输入图像自动裁剪
def f1( picture ):
global stacks_frames
image = tf.constant( picture ).numpy()
stacks_frames.append( image )
stacks_frames = stacks_frames[-4:]
image_1 = stacks_frames[0][:,:,0:1] + ( 255 - stacks_frames[3][:,:,0:1] )
image_2 = stacks_frames[0][:,:,1:2] + ( 255 - stacks_frames[3][:,:,1:2] )
image_3 = stacks_frames[0][:,:,2:3] + ( 255 - stacks_frames[3][:,:,2:3] )
image = tf.keras.layers.Concatenate(axis=2)([image_1, image_2])
image = tf.keras.layers.Concatenate(axis=2)([image, image_3])
image = tf.where( tf.greater_equal( image[:,:,0:1], tf.ones( [ 29, 39, 1 ] ) * 100 ), [255] , [0], name=None ).numpy()
list_cells_values = [ ]
list_cells_pos_values = [ ]
for i in range(4):
for j in range(4):
cropped_image = tf.image.crop_to_bounding_box(image, 2 * i, 2 * j, 15, 16)
list_cells_values.append([ tf.math.count_nonzero(cropped_image) ])
list_cells_pos_values.append([ 2 * i, 2 * j, 15, 16 ])
width = list_cells_pos_values[ int(tf.math.argmax( list_cells_values ).numpy()) ][0]
height = list_cells_pos_values[ int(tf.math.argmax( list_cells_values ).numpy()) ][1]
print( 'width: ' + str( width ) )
print( 'height: ' + str( height ) )
img = tf.constant( picture, shape=( 1, 29, 39, 3 ) ).numpy()
box = np.array([0.0625 * width, 0.0625 * height, 1 - ( 0.0625 * width ), 1 - ( 0.0625 * height )])
boxes = box.reshape([1, 1, 4])
colors = np.array([[1.0, 0.0, 0.0]])
image = tf.image.draw_bounding_boxes(img, boxes, colors).numpy()
image = tf.constant( image, shape=( 29, 39, 3 ) ).numpy()
image = tf.keras.preprocessing.image.array_to_img(
image,
data_format=None,
scale=True
)
return image
【讨论】: