Datasets:
Tasks:
Image Classification
Sub-tasks:
multi-class-image-classification
Languages:
English
Size:
1M<n<10M
ArXiv:
License:
Update ForNet.py
Browse files1125 unmodified lines112611271128112911301131113211331134113511361137113811391140114134 unmodified lines11761177117811791180118111821183118411851186118711881189309 unmodified lines14991500150115021503150485 unmodified lines15901591159215931594159515962 unmodified lines1599160016011602160316041605154 unmodified lines1760176117621125 unmodified linesbg_rat_indices = super()._getitem(0)["bg_rat_idx_file"]self.train = "train" in bg_rat_indices.split("/")[-1]with open(bg_rat_indices, "r") as f:bg_rat_indices = json.load(f)for in_cls in bg_rat_indices:if in_cls not in self.cls_to_idx:self.cls_to_idx[in_cls] = []for idx, rat in bg_rat_indices[in_cls]:if rat < self.pruning_ratio:self.cls_to_idx[in_cls].append(idx)if self.background_combination == "all":self.cls_to_idx["all"] = list(itertools.chain(*self.cls_to_idx.values()))@propertydef total_epochs(self):34 unmodified linesif self.background_combination == "orig":bg_item = fg_itemelif self.background_combination == "same":rand_idx = np.random.randint(len(self.cls_to_idx[in_cls]))rand_idx = self.cls_to_idx[in_cls][rand_idx]bg_item = super()._getitem(rand_idx)else:# allrand_idx = np.random.randint(len(self.cls_to_idx["all"]))rand_idx = self.cls_to_idx["all"][rand_idx]bg_item = super()._getitem(rand_idx)fg_img = fg_item["fg"].convert("RGBA")bg_img = bg_item["bg"].convert("RGB")309 unmodified linesdef _generate_examples(self, patch_files, split, hf_indices, cls_to_idx_loc, fg_bg_ratios):logger.info(f"Generating examples from {len(patch_files)} patch files")logger.info("Opening files")class_to_zipfile = {}for f in patch_files:85 unmodified linesfor proc in zip_procs:proc.join()# tqdm.write("Finished all processes")while not ret_queue.empty():data = ret_queue.get()in_cls = data["path"].split("/")[0]2 unmodified linescls_to_idx[in_cls].append(foraug_idx)yield foraug_idx, dataforaug_idx += 1tqdm.write("Done")with open(cls_to_idx_loc, "w") as f:json.dump(cls_to_idx, f)154 unmodified lines"fg/bg_area": fg_bg_ratios[patch_name],})1125 unmodified lines1126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116334 unmodified lines119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221309 unmodified lines153115321533153415351536153785 unmodified lines16231624162516261627162816292 unmodified lines1632163316341635163616371638154 unmodified lines17931794179517961125 unmodified linesbg_rat_indices = super()._getitem(0)["bg_rat_idx_file"]self.train = "train" in bg_rat_indices.split("/")[-1]bg_rat_idx_file = bg_rat_indicesif self.background_combination == "same":try:with open(bg_rat_indices, "r") as f:bg_rat_indices = json.load(f)for in_cls in bg_rat_indices:if in_cls not in self.cls_to_idx:self.cls_to_idx[in_cls] = []for idx, rat in bg_rat_indices[in_cls]:if rat < self.pruning_ratio:self.cls_to_idx[in_cls].append(idx)except (TypeError, KeyError, OSError):logger.warning(f"Could not load background ratio indices from {bg_rat_indices}. Will do pruning and background selection on the fly. This will take more time in the first few epochs")self.cls_to_idx = {cls: list(range(len(self))) for cls in IMAGENET2012_CLASSES.keys()}if self.background_combination == "all":try:self.cls_to_idx["all"] = []with open(bg_rat_indices, "r") as f:bg_rat_indices = json.load(f)for in_cls in bg_rat_indices:for idx, rat in bg_rat_indices[in_cls]:if rat < self.pruning_ratio:self.cls_to_idx["all"].append(idx)except (TypeError, KeyError, OSError) as e:logger.warning(f"Error {e} while extracting bg_rat_indices")logger.warning(f"Could not load background ratio indices from {bg_rat_idx_file}. Will do pruning and background selection on the fly. This will take more time in the first few epochs")self.cls_to_idx["all"] = list(range(len(self)))@propertydef total_epochs(self):34 unmodified linesif self.background_combination == "orig":bg_item = fg_itemelif self.background_combination == "same":while True:rand_idx = np.random.randint(len(self.cls_to_idx[in_cls]))rand_idx = self.cls_to_idx[in_cls][rand_idx]bg_item = super()._getitem(rand_idx)if bg_item["fg/bg_area"] < self.pruning_ratio and bg_item["label"] == fg_item["label"]:breakelse:self.cls_to_idx[in_cls].remove(rand_idx)else:# allwhile True:rand_idx = np.random.randint(len(self.cls_to_idx["all"]))rand_idx = self.cls_to_idx["all"][rand_idx]bg_item = super()._getitem(rand_idx)if bg_item["fg/bg_area"] < self.pruning_ratio:breakelse:self.cls_to_idx["all"].remove(rand_idx)fg_img = fg_item["fg"].convert("RGBA")bg_img = bg_item["bg"].convert("RGB")309 unmodified linesdef _generate_examples(self, patch_files, split, hf_indices, cls_to_idx_loc, fg_bg_ratios):logger.info(f"Generating examples from {len(patch_files)} patch files")logger.info(f"will save cls_to_idx to '{cls_to_idx_loc}'")logger.info("Opening files")class_to_zipfile = {}for f in patch_files:85 unmodified linesfor proc in zip_procs:proc.join()tqdm.write("Finished all processes")while not ret_queue.empty():data = ret_queue.get()in_cls = data["path"].split("/")[0]2 unmodified linescls_to_idx[in_cls].append(foraug_idx)yield foraug_idx, dataforaug_idx += 1tqdm.write(f"Done generating {split} examples. Saving cls_to_idx file at '{cls_to_idx_loc}'.")with open(cls_to_idx_loc, "w") as f:json.dump(cls_to_idx, f)154 unmodified lines"fg/bg_area": fg_bg_ratios[patch_name],})
ForNet.py+51-17