Last active
December 26, 2022 15:42
-
-
Save AlessandroMondin/f39f0f1cf1777ecf58854ca87a7e21f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def adaptive_shape(self, annotations): | |
name = "train" if self.train else "val" | |
path = os.path.join( | |
self.root_directory, "labels", | |
"adaptive_ann_{}_{}_br_{}.csv".format(name, self.len_ann, int(self.batch_range)) | |
) | |
if os.path.isfile(path): | |
print(f"==> Loading cached annotations for rectangular training on {self.annot_folder}") | |
parsed_annot = pd.read_csv(path, index_col=0) | |
else: | |
print("...Running adaptive_shape for 'rectangular training' on training set...") | |
annotations["w_h_ratio"] = annotations.iloc[:, 2] / annotations.iloc[:, 1] | |
annotations.sort_values(["w_h_ratio"], ascending=True, inplace=True) | |
for i in range(0, len(annotations), self.batch_range): | |
size = [annotations.iloc[i, 2], annotations.iloc[i, 1]] # [width, height] | |
max_dim = max(size) | |
max_idx = size.index(max_dim) | |
size[~max_idx] += 32 | |
sz = random.randrange(int(self.default_size * 0.9), int(self.default_size * 1.1)) // 32 * 32 | |
size[~max_idx] = ((sz/size[max_idx])*(size[~max_idx]) // 32) * 32 | |
size[max_idx] = sz | |
if i + self.batch_range <= len(annotations): | |
bs = self.batch_range | |
else: | |
bs = len(annotations) - i | |
annotations.iloc[i:bs, 2] = size[0] | |
annotations.iloc[i:bs, 1] = size[1] | |
# sample annotation to avoid having pseudo-equal images in the same batch | |
annotations.iloc[i:i+bs, :] = annotations.iloc[i:i+bs, :].sample(frac=1, axis=0) | |
parsed_annot = pd.DataFrame(annotations.iloc[:,:3]) | |
parsed_annot.to_csv(path) | |
return parsed_annot |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment