async def finish_config_callback(self, callback_config): print("callback_config:", callback_config) if self.config_win is not None: self.config_win.close() api.showMessage('network configured.') with open(os.path.join(self.work_dir, "anet-config.json"), "w") as f: f.write(json.dumps(callback_config)) self.config_json.update(callback_config) # await self.get_data_by_config(config=self.config_json) api.showStatus("generating mask from the annotation file ...") self.get_mask_by_json(config=self.config_json) api.showStatus("Masks generated, now start training...") self._opt = self.get_opt(self.config_json, work_dir=self.work_dir) self.initialize(self._opt) api.log("self._opt.work_dir:" + self._opt.work_dir) api.log("self._opt.input_channels:" + str(self._opt.input_channels)) api.log("self._opt.target_channels:" + str(self._opt.target_channels)) api.log("self._opt.input_nc:" + str(self._opt.input_nc)) api.log("self._opt.target_nc:" + str(self._opt.target_nc)) config = my_config() api.log("config.name:" + config.name) api.log("config.epochs:" + str(config.epochs)) api.log("config.steps:" + str(config.steps)) api.log("config.batchsize:" + str(config.batchsize)) await self.train_2(config)
async def predict(self): if not self.image_layer: api.showMessage("No selected image for prediction") return self.viewer.set_loader(True) if self.mask_layer: self.viewer.remove_layer(self.mask_layer) if self.geojson_layer: self.viewer.remove_layer(self.geojson_layer) self._trainer.predict_async(self.current_image) async def check_prediction(): self.viewer.set_loader(True) result = self._trainer.get_prediction_result() if result is None: self.viewer.set_timeout(check_prediction, 500) return api.showMessage("prediction done") try: self.viewer.set_loader(True) polygons, mask = result mask = ((mask > 0) * 255).astype("uint8") # imwrite( # os.path.join(self.current_sample_info["path"], "prediction.png"), # mask, # ) self._mask_prediction = mask self.current_annotation = polygons self.mask_layer = await self.viewer.view_image( mask, type="itk-vtk", name=self._trainer.object_name + "_mask", opacity=0.5, visible=False, ) self.viewer.set_loader(False) if len(polygons) > 0: if len(polygons) > 2000: polygons = polygons[:2000] api.showMessage( f"WARNING: Too many object detected ({len(polygons)}), only displaying the first 2000 objects." ) else: polygons = [] api.showMessage("No object detected.") self.geojson_layer = await self.viewer.add_shapes( polygons, shape_type="polygon", edge_color="red", name=self._trainer.object_name, ) except Exception as e: api.error(traceback.format_exc()) api.showMessage(str(e)) finally: self.viewer.set_loader(False) self.viewer.set_timeout(check_prediction, 1000)
async def fetch_mask(self): if self.mask_layer: self.viewer.remove_layer(self.mask_layer) if self.geojson_layer: self.viewer.remove_layer(self.geojson_layer) annotation_file = os.path.join( self._trainer.data_dir, self.current_sample_info["folder"], self.current_sample_info["name"], "annotation.json", ) with io.open( annotation_file, "r", encoding="utf-8-sig", ) as myfile: polygons = json.load(myfile) polygons = self.flipud_annotation(polygons) polygons = list( map( lambda feature: np.array( feature["geometry"]["coordinates"][0], dtype=np.uint16 ), polygons["features"], ) ) mask_dict = geojson_to_masks(annotation_file, mask_types=["labels"]) labels = mask_dict["labels"] mask = ((labels > 0) * 255).astype("uint8") self.current_annotation = polygons if len(polygons) > 0: self.mask_layer = await self.viewer.view_image( mask, type="itk-vtk", name=self._trainer.object_name + "_mask", opacity=0.5, ) if len(polygons) < 2000: self.geojson_layer = await self.viewer.add_shapes( polygons, shape_type="polygon", edge_color="red", name=self._trainer.object_name, ) else: api.showMessage(f"Too many object detected ({len(polygons)}).") else: api.showMessage("No object detected.")
async def check_augmentations(): self.viewer.set_loader(True) figure = self._trainer._plot_augmentations_result if figure is None: self.viewer.set_timeout(check_augmentations, 1500) return api.showMessage("Testing augmentation done") try: self.image_layer = await self.viewer.view_image( figure, type="itk-vtk", name="Augmented images") except Exception as e: api.showMessage(str(e)) finally: self.viewer.set_loader(False)
async def save_annotation(self): self.current_annotation = await self.geojson_layer.get_features() if len(self.current_annotation["features"]) < 1: api.showMessage("no annotation available") return img = imread( os.path.join( self._trainer.data_dir, self.current_sample_info["folder"], self.current_sample_info["name"], self._trainer.input_channels[0], ) ) size = img.shape[1] self.current_annotation = self.flipud_annotation(self.current_annotation, size) self._trainer.save_annotation_async( self.current_sample_info["folder"], self.current_sample_info["name"], self.current_annotation, ) if self.current_sample_info["folder"] == "train": self.reload_samples() await api.showMessage("Training sample pool reloaded.")
async def send_for_training(self): if not self.geojson_layer: api.showMessage("no annotation available") return await self.save_annotation() self._trainer.push_sample_async( self.current_sample_info["name"], self.current_annotation, target_folder="train", prediction=self._mask_prediction, ) self._mask_prediction = None if self.image_layer: self.viewer.remove_layer(self.image_layer) if self.geojson_layer: self.viewer.remove_layer(self.geojson_layer) if self.mask_layer: self.viewer.remove_layer(self.mask_layer) await self.update_file_tree() await api.showMessage("Sample moved to the training set") if self.auto_get_sample: await self.get_next_sample()
async def check_prediction(): self.viewer.set_loader(True) result = self._trainer.get_prediction_result() if result is None: self.viewer.set_timeout(check_prediction, 500) return api.showMessage("prediction done") try: self.viewer.set_loader(True) polygons, mask = result mask = np.clip(mask * 255, 0, 255).astype("uint8") # imwrite( # os.path.join(self.current_sample_info["path"], "prediction.png"), # mask, # ) self._mask_prediction = mask self.current_annotation = polygons self.mask_layer = await self.viewer.view_image( mask, type="itk-vtk", name=self._trainer.object_name + "_mask", opacity=0.5, ) self.viewer.set_loader(False) if len(polygons) > 0: if len(polygons) < 2000: self.geojson_layer = await self.viewer.add_shapes( polygons, shape_type="polygon", edge_color="red", name=self._trainer.object_name, ) else: api.showMessage( f"Too many object detected ({len(polygons)}).") else: api.showMessage("No object detected.") except Exception as e: api.showMessage(str(e)) finally: self.viewer.set_loader(False)
async def send_for_training(self): if self._mask_prediction is None: api.showMessage("please predict first") return if not self.geojson_layer: api.showMessage("no annotation available") return self.current_annotation = await self.geojson_layer.get_features() if len(self.current_annotation["features"]) < 1: api.showMessage("no annotation available") return img = imread( os.path.join( self._trainer.data_dir, "test", self.current_sample_info["name"], self._trainer.input_channels[0], )) size = img.shape[1] for i, feature in enumerate(self.current_annotation["features"]): coordinates = feature["geometry"]["coordinates"][0] new_coordinates = [] for j, coordinate in enumerate(coordinates): x, y = coordinate if x < 0: x = 0 if x > size: x = size if y < 0: y = 0 if y > size: y = size y = size - y new_coordinates.append([x, y]) self.current_annotation["features"][i]["geometry"]["coordinates"][ 0] = new_coordinates self._trainer.push_sample_async( self.current_sample_info["name"], self.current_annotation, target_folder="train", prediction=self._mask_prediction, ) self._mask_prediction = None # api.showMessage("Sample moved to the training set") if self.geojson_layer: self.viewer.remove_layer(self.geojson_layer) if self.mask_layer: self.viewer.remove_layer(self.mask_layer) await self.update_file_tree()
async def fetch_mask(self): if self.mask_layer: self.viewer.remove_layer(self.mask_layer) if self.geojson_layer: self.viewer.remove_layer(self.geojson_layer) annotation_file = os.path.join( self._trainer.data_dir, "test", self.current_sample_info["name"], "annotation.json", ) with io.open( annotation_file, "r", encoding="utf-8-sig", ) as myfile: polygons = json.load(myfile) size = polygons["bbox"][3] for i, feature in enumerate(polygons["features"]): coordinates = feature["geometry"]["coordinates"][0] new_coordinates = [] for j, coordinate in enumerate(coordinates): x, y = coordinate if x < 0: x = 0 if x > size: x = size if y < 0: y = 0 if y > size: y = size y = size - y new_coordinates.append([x, y]) polygons["features"][i]["geometry"]["coordinates"][ 0] = new_coordinates polygons = list( map( lambda feature: np.array(feature["geometry"]["coordinates"][0], dtype=np.uint16), polygons["features"], )) mask_dict = geojson_to_masks(annotation_file, mask_types=["labels"]) labels = mask_dict["labels"] mask = ((labels > 0) * 255).astype("uint8") self.current_annotation = polygons if len(polygons) > 0: self.mask_layer = await self.viewer.view_image( mask, type="itk-vtk", name=self._trainer.object_name + "_mask", opacity=0.5, ) if len(polygons) < 2000: self.geojson_layer = await self.viewer.add_shapes( polygons, shape_type="polygon", edge_color="red", name=self._trainer.object_name, ) else: api.showMessage(f"Too many object detected ({len(polygons)}).") else: api.showMessage("No object detected.")