def testSquareIncluded(self): # generate the image to be processed w, h = 2000, 2000 image = np.zeros((h, w), dtype=np.uint8) # locations of the 9 multi-squares positions = [(w // 7, h // 7), (3 * w // 7, h // 7), (5 * w // 7, h // 7), (w // 7, 3 * h // 7), (3 * w // 7, 3 * h // 7), (5 * w // 7, 3 * h // 7), (w // 7, 5 * h // 7), (3 * w // 7, 5 * h // 7), (5 * w // 7, 5 * h // 7)] for position in positions: image = draw_multisquare(image, position, w // 7, color_in=127) # Build workflow builder = SLDCWorkflowBuilder() # Build workflow 1 builder.set_segmenter(BigShapeSegmenter()) builder.add_catchall_classifier(DumbClassifier()) builder.set_tile_size(512, 512) workflow1 = builder.get() # Build workflow 2 builder.set_segmenter(SmallSquareSegmenter()) builder.add_catchall_classifier(DumbClassifier()) workflow2 = builder.get() # Build chaining chain_builder = WorkflowChainBuilder() chain_builder.set_first_workflow(workflow1, label="big_squares") chain_builder.add_executor(workflow2, label="small_squares") chain = chain_builder.get() # Launch chain_info = chain.process(NumpyImage(image)) # check results big_area = (w // 7)**2 small_area = (w / 35)**2 info1 = chain_info["big_squares"] self.assertEqual(9, len(info1)) for object_info in info1: self.assertTrue( relative_error(object_info.polygon.area, big_area) < 0.005) self.assertEqual("catchall", object_info.dispatch) self.assertEqual(1, object_info.label) self.assertAlmostEqual(1.0, object_info.proba) info2 = chain_info["small_squares"] self.assertEqual(36, len(info2)) for object_info in info2: self.assertTrue( relative_error(object_info.polygon.area, small_area) < 0.005) self.assertEqual("catchall", object_info.dispatch) self.assertEqual(1, object_info.label) self.assertAlmostEqual(1.0, object_info.proba)
def main(argv): with CytomineJob.from_cli(argv) as job: if not os.path.exists(job.parameters.working_path): os.makedirs(job.parameters.working_path) # create workflow component logger = StandardOutputLogger(Logger.INFO) random_state = check_random_state(int(job.parameters.rseed)) tile_builder = CytomineTileBuilder( working_path=job.parameters.working_path) segmenter = DemoSegmenter(job.parameters.threshold) area_rule = ValidAreaRule(job.parameters.min_area) classifier = PyxitClassifierAdapter.build_from_pickle( job.parameters.pyxit_model_path, tile_builder, logger, random_state=random_state, n_jobs=job.parameters.n_jobs, working_path=job.parameters.working_path) builder = SLDCWorkflowBuilder() builder.set_n_jobs(job.parameters.n_jobs) builder.set_logger(logger) builder.set_overlap(job.parameters.sldc_tile_overlap) builder.set_tile_size(job.parameters.sldc_tile_width, job.parameters.sldc_tile_height) builder.set_tile_builder(tile_builder) builder.set_segmenter(segmenter) builder.add_classifier(area_rule, classifier, dispatching_label="valid") workflow = builder.get() slide = CytomineSlide(job.parameters.cytomine_image_id) results = workflow.process(slide) # Upload results for polygon, label, proba, dispatch in results: if label is not None: # if image is a window, the polygon must be translated if isinstance(slide, ImageWindow): polygon = translate(polygon, slide.abs_offset_x, slide.abs_offset_y) # upload the annotation polygon = affine_transform( polygon, [1, 0, 0, -1, 0, slide.image_instance.height]) annotation = Annotation( location=polygon.wkt, id_image=slide.image_instance.id).save() AlgoAnnotationTerm(id_annotation=annotation.id, id_term=label, rate=float(proba)).save()
def testSLDCWorkflowWithOneShotDispatcher(self): # pre build components segmenter = DumbSegmenter() dispatcher = DumbDispatcher() classifier = DumbClassifier() rule = DumbRule() logger = StandardOutputLogger(Logger.DEBUG) builder = SLDCWorkflowBuilder() builder.set_tile_size(512, 768) builder.set_overlap(3) builder.set_distance_tolerance(5) builder.set_n_jobs(5) builder.set_logger(logger) builder.set_parallel_dc(True) builder.set_tile_builder(None) with self.assertRaises(MissingComponentException): builder.get() builder.set_segmenter(segmenter) with self.assertRaises(MissingComponentException): builder.get() builder.set_default_tile_builder() with self.assertRaises(MissingComponentException): builder.get() builder.set_one_shot_dispatcher(dispatcher, {"default": classifier}) with self.assertRaises(InvalidBuildingException): builder.add_classifier(rule, classifier, dispatching_label="default") with self.assertRaises(InvalidBuildingException): builder.add_catchall_classifier(classifier, dispatching_label="default") workflow = builder.get() self.assertIsInstance(workflow, SLDCWorkflow) self.assertEqual(workflow._segmenter, segmenter) self.assertEqual(workflow._n_jobs, 5) self.assertEqual(workflow._tile_overlap, 3) self.assertEqual(workflow._tile_max_height, 512) self.assertEqual(workflow._tile_max_width, 768) self.assertEqual(workflow.logger, logger) self.assertIsInstance(workflow._tile_builder, DefaultTileBuilder) self.assertEqual(workflow._dispatch_classifier._dispatcher, dispatcher) self.assertEqual(len(workflow._dispatch_classifier._classifiers), 1) self.assertEqual(workflow._dispatch_classifier._classifiers[0], classifier)
def testSquareAndCircleIncluded(self): w, h = 2000, 2000 image = np.zeros((h, w), dtype=np.uint8) # locations of the 9 multi-squares shapes = [("c", (w // 7, h // 7)), ("s", (3 * w // 7, h // 7)), ("s", (5 * w // 7, h // 7)), ("s", (w // 7, 3 * h // 7)), ("c", (3 * w // 7, 3 * h // 7)), ("s", (5 * w // 7, 3 * h // 7)), ("c", (w // 7, 5 * h // 7)), ("s", (3 * w // 7, 5 * h // 7)), ("c", (5 * w // 7, 5 * h // 7))] for shape, position in shapes: if shape == "c": image = draw_multicircle(image, position, w // 7, color_in=87) elif shape == "s": image = draw_multisquare(image, position, w // 7, color_in=187) # Build workflows # 1st: find big shapes and dispatch them as circle or square # 2nd: find small circles in found circle shapes # 3rd: find small squares in found square shape builder = SLDCWorkflowBuilder() builder.set_segmenter(BigShapeSegmenter()) builder.add_classifier(CircleDispatch(), DumbClassifier(), dispatching_label="circle") builder.add_classifier(SquareDispatch(), DumbClassifier(), dispatching_label="square") builder.set_tile_size(512, 512) workflow1 = builder.get() builder.set_segmenter(SmallCircleSegmenter()) builder.add_catchall_classifier(DumbClassifier()) workflow2 = builder.get() builder.set_segmenter(SmallSquareSegmenter()) builder.add_catchall_classifier(DumbClassifier()) workflow3 = builder.get() # Build chain chain_builder = WorkflowChainBuilder() chain_builder.set_first_workflow(workflow1) chain_builder.add_executor(workflow2, filter=CircleShapeFilter()) chain_builder.add_executor(workflow3, filter=SquareShapeFilter(), n_jobs=2) chain = chain_builder.get() chain_info = chain.process(NumpyImage(image)) info1 = chain_info[0] self.assertEqual(9, len(info1)) self.assertEqual(4, len([d for d in info1.dispatches if d == "circle"])) self.assertEqual(5, len([d for d in info1.dispatches if d == "square"])) info2 = chain_info[1] self.assertEqual(16, len(info2)) info3 = chain_info[2] self.assertEqual(20, len(info3))
def testSquareIncluded(self): # generate the image to be processed w, h = 2000, 2000 image = np.zeros((h, w), dtype=np.uint8) # locations of the 9 multi-squares positions = [ (w // 7, h // 7), (3 * w // 7, h // 7), (5 * w // 7, h // 7), (w // 7, 3 * h // 7), (3 * w // 7, 3 * h // 7), (5 * w // 7, 3 * h // 7), (w // 7, 5 * h // 7), (3 * w // 7, 5 * h // 7), (5 * w // 7, 5 * h // 7) ] for position in positions: image = draw_multisquare(image, position, w // 7, color_in=127) # Build workflow builder = SLDCWorkflowBuilder() # Build workflow 1 builder.set_segmenter(BigShapeSegmenter()) builder.add_catchall_classifier(DumbClassifier()) builder.set_tile_size(512, 512) workflow1 = builder.get() # Build workflow 2 builder.set_segmenter(SmallSquareSegmenter()) builder.add_catchall_classifier(DumbClassifier()) workflow2 = builder.get() # Build chaining chain_builder = WorkflowChainBuilder() chain_builder.set_first_workflow(workflow1, label="big_squares") chain_builder.add_executor(workflow2, label="small_squares") chain = chain_builder.get() # Launch chain_info = chain.process(NumpyImage(image)) # check results big_area = (w // 7) ** 2 small_area = (w / 35) ** 2 info1 = chain_info["big_squares"] self.assertEqual(9, len(info1)) for object_info in info1: self.assertTrue(relative_error(object_info.polygon.area, big_area) < 0.005) self.assertEqual("catchall", object_info.dispatch) self.assertEqual(1, object_info.label) self.assertAlmostEqual(1.0, object_info.proba) info2 = chain_info["small_squares"] self.assertEqual(36, len(info2)) for object_info in info2: self.assertTrue(relative_error(object_info.polygon.area, small_area) < 0.005) self.assertEqual("catchall", object_info.dispatch) self.assertEqual(1, object_info.label) self.assertAlmostEqual(1.0, object_info.proba)
def testSquareAndCircleIncluded(self): w, h = 2000, 2000 image = np.zeros((h, w), dtype=np.uint8) # locations of the 9 multi-squares shapes = [ ("c", (w // 7, h // 7)), ("s", (3 * w // 7, h // 7)), ("s", (5 * w // 7, h // 7)), ("s", (w // 7, 3 * h // 7)), ("c", (3 * w // 7, 3 * h // 7)), ("s", (5 * w // 7, 3 * h // 7)), ("c", (w // 7, 5 * h // 7)), ("s", (3 * w // 7, 5 * h // 7)), ("c", (5 * w // 7, 5 * h // 7)) ] for shape, position in shapes: if shape == "c": image = draw_multicircle(image, position, w // 7, color_in=87) elif shape == "s": image = draw_multisquare(image, position, w // 7, color_in=187) # Build workflows # 1st: find big shapes and dispatch them as circle or square # 2nd: find small circles in found circle shapes # 3rd: find small squares in found square shape builder = SLDCWorkflowBuilder() builder.set_segmenter(BigShapeSegmenter()) builder.add_classifier(CircleDispatch(), DumbClassifier(), dispatching_label="circle") builder.add_classifier(SquareDispatch(), DumbClassifier(), dispatching_label="square") builder.set_tile_size(512, 512) workflow1 = builder.get() builder.set_segmenter(SmallCircleSegmenter()) builder.add_catchall_classifier(DumbClassifier()) workflow2 = builder.get() builder.set_segmenter(SmallSquareSegmenter()) builder.add_catchall_classifier(DumbClassifier()) workflow3 = builder.get() # Build chain chain_builder = WorkflowChainBuilder() chain_builder.set_first_workflow(workflow1) chain_builder.add_executor(workflow2, filter=CircleShapeFilter()) chain_builder.add_executor(workflow3, filter=SquareShapeFilter(), n_jobs=2) chain = chain_builder.get() chain_info = chain.process(NumpyImage(image)) info1 = chain_info[0] self.assertEqual(9, len(info1)) self.assertEqual(4, len([d for d in info1.dispatches if d == "circle"])) self.assertEqual(5, len([d for d in info1.dispatches if d == "square"])) info2 = chain_info[1] self.assertEqual(16, len(info2)) info3 = chain_info[2] self.assertEqual(20, len(info3))