platypus icon indicating copy to clipboard operation
platypus copied to clipboard

Error in yolo3_generator() function

Open apadlewski opened this issue 3 years ago • 1 comments

Describe the bug I have a comment about the yolo3_generator() function. This function works badly (set shuffle = FALSE) when the number of input images length(annot_paths) is not divisible without remainder by batch_size. Then the size of the returned list of images is smaller than batch_size, which causes an error when yolo3_fit_generator() runs (the train_on_batch() function gets an x of too small a size).

This is a part of code in yolo3_generator(), which works incorrectly:

indices <- c(i:min(i + batch_size - 1, length(annot_paths)))
      i <<- if (i + batch_size > length(annot_paths)) 1 else i + length(indices)

To Reproduce Below is my code in R (to test it):

batch_size <- 32
length_annot_paths <- 291
i <- 9 * batch_size
shuffle <- FALSE
i * batch_size
length_annot_paths / batch_size

if (shuffle) {
  indices <- sample(1:length_annot_paths, size = batch_size)
} else {
  indices <- c(i:min(i + batch_size - 1, length_annot_paths))
  i <- if (i + batch_size > length_annot_paths) 1 else i + length(indices)
}
indices
length(indices)
i

Expected behavior The yolo3_generator() function should always return a list of images with the full batch_size.

Session information (please complete the following information): R version 4.1.1 (2021-08-10) Python 3.7.9 64-bit' kernel Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 8.1 x64 (build 9600)

apadlewski avatar Sep 02 '21 08:09 apadlewski

It is my suggestion for a code fix. I think, this code will work good:

if (shuffle) {
  indices <- sample(1:length_annot_paths, size = batch_size)
} else {
  i <- ifelse(i + batch_size > length_annot_paths, 1, i)
  indices <- c(i:min(i + batch_size - 1, length_annot_paths))
  i <-  i + length(indices)
}

The last batch should be dropped in the case it has fewer than batch_size elements.

apadlewski avatar Sep 02 '21 16:09 apadlewski