我是深度学习和计算机视觉的新手。我最近做了一个关于yolov1实现的教程。现在我正在尝试使用不同的数据集。
这是我的代码-
def get_bboxes(filename):
name = filename[:-4]
indices = meta_df[meta_df['new_img_id'] == float(name)]
bounding_boxes = []
for index, row in indices.iterrows():
x = float(row['x'])
y = float(row['y'])
w = float(row['width'])
h = float(row['height'])
im_w = float(row['img_width'])
im_h = float(row['img_height'])
class_id = int(row['cat_id'])
bounding_box = [(x+w/2)/(im_w), (y+h/2)/(im_h), w/im_w, h/im_h, class_id]
bounding_boxes.append(bounding_box)
return tf.convert_to_tensor(bounding_boxes, dtype=tf.float32)
def generate_output(bounding_boxes):
output_label = np.zeros([int(SPLIT_SIZE), int(SPLIT_SIZE), int(N_CLASSES+5)])
for b in range(len(bounding_boxes)):
grid_x = bounding_boxes[...,b,0]*SPLIT_SIZE
grid_y = bounding_boxes[...,b,1]*SPLIT_SIZE
i = int(grid_x)
j = int(grid_y)
output_label[i, j, 0:5] = [1., grid_x%1, grid_y%1, bounding_boxes[...,b,2], bounding_boxes[...,b,3]]
output_label[i, j, 5+int(bounding_boxes[...,b,4])] = 1.
return tf.convert_to_tensor(output_label, tf.float32)
def get_imboxes(im_path, map):
img = tf.io.decode_jpeg(tf.io.read_file('./new_dataset/'+im_path))
img = tf.cast(tf.image.resize(img, [H,W]), dtype=tf.float32)
bboxes = tf.numpy_function(func=get_bboxes, inp=[im_path], Tout=tf.float32)
return img, bboxes
train_ds2 = train_ds1.map(get_imboxes)
val_ds2 = val_ds1.map(get_imboxes)
transforms = A.Compose([
A.Resize(H,W),
A.RandomCrop(
width = np.random.randint(int(0.9*W), W),
height = np.random.randint(int(0.9*H), H), p=0.5),
A.RandomScale(scale_limit=0.1, interpolation=cv.INTER_LANCZOS4, p=0.5),
A.HorizontalFlip(p=0.5),
A.Resize(H,W)
], bbox_params =A.BboxParams(format='yolo'))
def aug_albument(image, bboxes):
augmented = transforms(image = image, bboxes = bboxes)
return [tf.convert_to_tensor(augmented['image'], dtype=tf.float32), tf.convert_to_tensor(augmented['bboxes'], dtype=tf.float32)]
def process_data(image, bboxes):
aug = tf.numpy_function(func=aug_albument, inp=[image, bboxes], Tout=(tf.float32, tf.float32))
return aug[0], aug[1]
train_ds3 = train_ds2.map(process_data)
我在专辑中遇到错误。
当我写下这些行时-
for i, j in train_ds3:
print(j)
我收到此错误-
InvalidArgumentError: {{function_node __wrapped__IteratorGetNext_output_types_2_device_/job:localhost/replica:0/task:0/device:CPU:0}} ValueError: Expected y_min for bbox (0.203125, -0.002777785062789917, 0.8187500238418579, 0.8694444596767426, 5.0) to be in the range [0.0, 1.0], got -0.002777785062789917.
我检查了train_ds2的所有标签。但我找不到任何负值。我在这里缺少什么?
我尝试评论图像上的所有白化操作,但仍然存在同样的问题
在你的 get_bboxes() 函数中,你将得到如下值:
x = float(row['x']) y = float(row['y']) w = float(row['width']) h = float(row['height'])
。当您以 x、y、w、h 形式检索数据时,这意味着它已经是 x_center 和 y_center。因此,当您执行 (x+w/2)/(im_w)
甚至 (y+h/2)/(im_h)
时,它会计算 x_max 和 y_max。格式将不正确,在此之前您将不会遇到任何问题 train_ds2
。当您使用带有 format='yolo'
的专辑时,就会出现此问题。它将收到不正确的格式,这可能是负值的原因。
我认为的解决方案是修改您的
get_bboxes()
函数,如下所示:
bounding_box = [x/im_w, y/im_h, w/im_w, h/im_h, class_id]
我不知道您的数据格式,因此如果我错了,请提供有关所用格式的更多详细信息。