def test(mf, df, tc=None, dt="cls"): if tc is None: tc = "all" else: tc = [tc] prediction_folder = os.path.join(mf, "image_wise_classification") check_n_make_dir(prediction_folder, clean=True) ML_pipeline = EnsembleImageClassifier(mf) ML_pipeline.load() d_set = DataSet(data_set_dir=df, class_mapping=ML_pipeline.class_mapping) d_set.load_data(tag_type=dt) tag_set = d_set.get_tags(classes_to_consider=tc) tg = TagGate({"height": 0, "width": 0}) tag_set = tg.apply(tag_set) print("Analysis of Tags ...") result_dict = init_result_dict(d_set.get_class_mapping()) for tag_id in tqdm(tag_set): tag = d_set.tags[tag_id] y_pred = ML_pipeline.predict(tag) tag.write_prediction(y_pred, prediction_folder) result_dict = tag.evaluate_prediction(y_pred, result_dict) show_results(result_dict) save_results(prediction_folder, "image_classifier", result_dict)
def save(self, model_path, name="clf"): check_n_make_dir(model_path) save_dict(self.opt, os.path.join(model_path, "{}_opt.json".format(name))) if self.regressor is not None: joblib.dump(self.regressor, os.path.join(model_path, "{}.pkl".format(name)))
def save(self): check_n_make_dir(self.model_path) joblib.dump(self.k_means_clustering, self.save_file_cluster) with open(self.save_file_parameters, "w") as f: j_file = json.dumps(self.parameters) f.write(j_file)
def save(self, model_path): model_path = os.path.join(model_path, self.layer_type + "-" + self.name) check_n_make_dir(model_path) self.opt["index"] = self.index save_dict(self.opt, os.path.join(model_path, "opt.json")) for p in self.previous: p.save(model_path)
def save(self): check_n_make_dir(self.model_path, clean=False) print("machine_learning-Pipeline was saved to: {}".format(self.model_path)) save_dict(self.class_mapping, self.path_to_class_mapping) save_dict(self._pipeline_opt, self.path_to_opt) joblib.dump(self.feature_extractor, self.feature_extractor_path) joblib.dump(self.classifier, self.classifier_path) joblib.dump(self.aggregator, self.aggregator_path)
def save(self): check_n_make_dir(self.model_path, clean=True) save_dict(self.class_mapping, self.path_to_class_mapping) save_dict(self.opt, self.path_to_pipeline_opt) if self.classifier is not None: joblib.dump(self.classifier, os.path.join(self.model_path, "classifier.pkl")) print("machine_learning-Pipeline was saved to: {}".format(self.model_path))
def export_frames(self, image_folder, roi_only=True): check_n_make_dir(image_folder) print("Exporting {}".format(self)) for i in tqdm(range(len(self))): image = self.get_frame_of_index(i, roi_only) i_file = os.path.join(image_folder, "{}_{}.jpg".format(self.id, i)) if image is not None: cv2.imwrite(i_file, image)
def export_box(self, export_folder, surrounding=0, with_text=False): check_n_make_dir(export_folder) box_data = self.load_data(surrounding=surrounding) box_name = "{}.jpg".format(self.generate_file_name(export_folder)) if with_text: font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(box_data, "{}".format(self.tag_class), (10, 10), font, .5, (0, 255, 0), 2, cv2.LINE_AA) cv2.imwrite(box_name, box_data)
def _export_label(self, export_folder): check_n_make_dir(export_folder) box_name = "{}.txt".format(self.generate_file_name(export_folder)) lb_str = "" for cls in self.tag_class: lb_str += cls lb_str += " " lb_str = lb_str[:-1] with open(box_name, "w") as f: f.write("{}".format(lb_str))
def save(self, model_path, name="clf"): check_n_make_dir(model_path) save_dict(self.opt, os.path.join(model_path, "{}_opt.json".format(name))) if self.classifier is not None: joblib.dump(self.classifier, os.path.join(model_path, "{}.pkl".format(name))) if self.report is not None: with open(os.path.join(model_path, "classification_report.txt"), "w") as f: f.write(self.report)
def export_tags(self, classes_to_consider="all", surrounding=0): """ Exporting cropped images of every tag. Tags are sorted by class and stored in image folders with matching label folders Returns: """ check_n_make_dir(self.tags_folder, clean=True) print("Exporting Objects...") for tag_id in tqdm(self.tags): tag = self.tags[tag_id] if tag.has_relevant_classes(classes_to_consider) or classes_to_consider == "all": # Initialising dir where each class is stored tag.export(self.tags_folder, surrounding) print("Objects exported!")
def write_prediction(self, prediction, prediction_folder): check_n_make_dir(prediction_folder) prediction_file_folder = os.path.join(prediction_folder, "prediction") check_n_make_dir(prediction_file_folder) p_string = "" for p in prediction: if p in self.class_mapping_inv: p_string += str(self.class_mapping_inv[p]) else: p_string += p p_string += "_" p_string = p_string[:-1] box_name = "{}.txt".format( self.generate_file_name(prediction_file_folder)) with open(box_name, "w") as f: f.write(p_string) cls_folder = os.path.join(prediction_folder, p_string) check_n_make_dir(cls_folder) self.export_box(cls_folder, with_text=True)
def __init__(self, data_directory): self.data_dir = data_directory check_n_make_dir(data_directory) self.storage = dict()
def main(args_): df = args_.data_folder with open(os.path.join(df, "dataset_split_0.hjson")) as infile: split = hjson.load(infile) be = os.path.join(df, "benigne_maligne", "benigne") ma = os.path.join(df, "benigne_maligne", "maligne") tr = os.path.join(df, "train") te = os.path.join(df, "test") check_n_make_dir(tr) check_n_make_dir(te) tr_ma = os.path.join(df, "train", "maligne") tr_be = os.path.join(df, "train", "benigne") te_ma = os.path.join(df, "test", "maligne") te_be = os.path.join(df, "test", "benigne") check_n_make_dir(tr_ma) check_n_make_dir(tr_be) check_n_make_dir(te_ma) check_n_make_dir(te_be) tr_ma = os.path.join(df, "train", "maligne", "images") tr_be = os.path.join(df, "train", "benigne", "images") te_ma = os.path.join(df, "test", "maligne", "images") te_be = os.path.join(df, "test", "benigne", "images") check_n_make_dir(tr_ma) check_n_make_dir(tr_be) check_n_make_dir(te_ma) check_n_make_dir(te_be) for im_f in os.listdir(be): src = os.path.join(be, im_f) if im_f in split["train"] or im_f in split["val"]: dst = os.path.join(tr_be, im_f) copyfile(src, dst) if im_f in split["test"]: dst = os.path.join(te_be, im_f) copyfile(src, dst) for im_f in os.listdir(ma): src = os.path.join(ma, im_f) if im_f in split["train"] or im_f in split["val"]: dst = os.path.join(tr_ma, im_f) copyfile(src, dst) if im_f in split["test"]: dst = os.path.join(te_ma, im_f) copyfile(src, dst)
def export(self, export_folder, surrounding=0): check_n_make_dir(export_folder) class_img_dir = os.path.join(export_folder, "images") class_lbf_dir = os.path.join(export_folder, "labels") self.export_box(class_img_dir, surrounding=surrounding) self._export_label(class_lbf_dir)
def export_tags(self): check_n_make_dir(self.tags_folder) for tag_id in self.tags: tag = self.tags[tag_id] tag.export(self.tags_folder)
def save(self): check_n_make_dir(self.model_path) joblib.dump(self.gmm, self.save_file_gmm)
def save(self, model_path): check_n_make_dir(model_path) check_n_make_dir(os.path.join(model_path, "graph")) self.graph.save(os.path.join(model_path, "graph")) print("Model was saved to: {}".format(model_path))