def enhance(sol, dataset): for scan in dataset.scans(): print(scan.id()) scan3D = scan.scan3D() sol3D = make_sol3d(sol, scan) enhance_scan(sol3D, scan3D) for idx, slice in enumerate(sol3D): cnts = findContours(slice) for cnt in cnts: coords = flatten( pixelToMM(scan.aux[idx + 1], x, y) for (x, y) in cv2tolist(cnt)) yield [scan.id(), idx + 1] + ["%.4f" % xy for xy in coords]
def compute(params): scan_id, slice, nbslices, aux, scan3D, lung = params img = scan3D[slice - 1] print(scan_id, slice) cnts = collect_contours(img, slice, nbslices) res = [] if cnts: # Keep best candidate merged = merge_and_filter(cnts, slice, scan3D, lung) corrected_cnts = findContours(merged) for cnt in corrected_cnts: # cnt = expand_blob(scan3D[slice-1], cnt) coords = flatten(pixelToMM(aux, x, y) for (x, y) in cv2tolist(cnt)) res.append([scan_id, slice] + ["%.4f" % xy for xy in coords]) return res
def compute(params): scan_id, slice, nbslices, img, aux = params print(scan_id, slice) res = [] for idx, contour_extractor in enumerate(contour_extractors): for cnt in contour_extractor(img): if (len(cnt) >= 5): # needed for fitEllipse features = [idx, slice, slice / nbslices ] + features_from_contour(cnt, img) features = [features] #single sample precision = precision_clf.predict(features) if precision > 0.01: recall = recall_clf.predict(features) if recall > 0.01: coords = flatten( pixelToMM(aux[slice], x, y) for (x, y) in cv2tolist(cnt)) res.append([scan_id, slice] + ["%.4f" % xy for xy in coords]) return res
def compute(params): scan_id, slice, nbslices, img, aux = params print(scan_id, slice) res = [] for idx, contour_extractor in enumerate(contour_extractors): for cnt in contour_extractor(img): if (len(cnt) >= 5): # needed for fitEllipse features = [idx, slice, slice / nbslices ] + features_from_contour(cnt, img) features = [features] #single sample precision = precision_clf.predict(features) if precision > 0.01: recall = recall_clf.predict(features) if recall > 0.01: coords = flatten( pixelToMM(aux[slice], x, y) for (x, y) in cv2tolist(cnt)) res.append([scan_id, slice] + ["%.4f" % xy for xy in coords]) return res pool = multiprocessing.Pool(40) solutions = flatten(pool.imap(compute, dataset.images_and_aux())) with open("/home/gerey/hms_lung/example_predictions_valid12.csv", "w") as f: # with open("/home/gerey/hms_lung/example_predictions8.csv","w") as f: for s in solutions: f.write(",".join(map(str, s))) f.write("\n")
if __name__ == '__main__': #dataset = Dataset("/home/gerey/hms_lung/data/provisional_extracted_no_gt", withGT=False) dataset = Dataset("/home/gerey/hms_lung/data/example_extracted_valid", withGT=False) #dataset = Dataset("/home/gerey/hms_lung/data/extract", withGT=False) scan_index = 1 solutions = [] for scan in dataset.scans(): for id, img, aux in scan.images_aux(): scan_id = scan.id() # only process middle slices # TODO: expend once false negatives are cleared if scan.nb_slices() / 4 < id < scan.nb_slices() * 1.7 / 3: print(scan_id, id, len(slices)) contours = contouring_binsym(img) for contour in contours: coords = flatten( pixelToMM(aux[id], x, y) for (x, y) in contour) solutions.append([scan_id, id] + ["%.4f" % xy for xy in coords]) with open("/home/gerey/hms_lung/predictions_valid8.csv", "w") as f: # with open("/home/gerey/hms_lung/example_predictions8.csv","w") as f: for s in solutions: f.write(",".join(map(str, s))) f.write("\n")
def generate_features(iter): pool = multiprocessing.Pool(10) return flatten(pool.imap(compute_features, iter))
def gen_params(): for scan in dataset.scans(): scan3D = scan.scan3D() if USE_LUNG: lung = segment_lung_mask(scan3D, fill_lung_structures=True) if MAX_MASK: lung = np.any(lung, axis=0) else: lung = None for scan_id, slice_idx, aux in scan.gen_aux(): yield scan_id, slice_idx, scan.nb_slices(), aux, scan3D, lung if __name__ == '__main__': # dataset = Dataset("/home/gerey/hms_lung/data/provisional_extracted_no_gt", withGT=False) dataset = Dataset( "/home/gerey/hms_lung/data/example_extracted_valid_small2", withGT=False) pool = multiprocessing.Pool(30) solutions = flatten(pool.imap(compute, gen_params())) # solutions = flatten(map( compute, gen_params() )) with open("/home/gerey/hms_lung/exemple_predictions_sample2_33.csv", "w") as f: # with open("/home/gerey/hms_lung/predictions31.csv","w") as f: for s in solutions: f.write(",".join(map(str, s))) f.write("\n")