Skip to content

Commit

Permalink
[opt] Make Stardist code DRY
Browse files Browse the repository at this point in the history
- create abstract stardist class for the two concrete stardist variant
  classes, because most of the functionality is identical
  • Loading branch information
gnodar01 committed Jun 12, 2024
1 parent cceb70a commit 7cb9e66
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 498 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import { GraphModel, History, LayersModel } from "@tensorflow/tfjs";

import { Segmenter } from "../AbstractSegmenter/AbstractSegmenter";
import { loadStardist } from "./loadStardist";
import { preprocessStardist } from "./preprocessStardist";
import { predictStardist } from "./predictStardist";
import { generateUUID } from "utils/common/helpers";
import { LoadInferenceDataArgs } from "../types";
import { ModelTask } from "../enums";
import { Kind, ImageObject } from "store/data/types";

/*
* Stardist (Versatile) Fluorescence Nuclei Segmentation
* https://zenodo.org/records/6348085
* https://bioimage.io/#/?tags=stardist&id=10.5281%2Fzenodo.6348084
* https://github.com/stardist/stardist/blob/master/README.md#pretrained-models-for-2d
* Stardist: model for object detection / instance segmentation with star-convex shapes
* This pretrained model: meant to segment individual cell nuclei from single channel fluorescence data (2018 DSB)
* Abstract model for Stardist variants
*/
export class StardistFluo extends Segmenter {
export abstract class Stardist extends Segmenter {
protected _fgKind?: Kind;
protected _inferenceDataDims?: Array<{
width: number;
Expand All @@ -26,25 +19,7 @@ export class StardistFluo extends Segmenter {
padY: number;
}>;

constructor() {
super({
name: "StardistFluo",
task: ModelTask.Segmentation,
graph: true,
pretrained: true,
trainable: false,
requiredChannels: 3,
});
}

public async loadModel() {
if (this._model) return;
// inputs: [ {name: 'input', shape: [-1,-1,-1,1], dtype: 'float32'} ]
// outputs: [ {name: 'concatenate_4/concat', shape: [-1, -1, -1, 33], dtype: 'float32'} ]
// where each -1 matches on input and output of corresponding dim/axis
// 33 -> 1 probability score, followed by 32 radial equiangular distances of rays
this._model = await loadStardist();
}
public abstract loadModel(): Promise<void>;

public loadTraining(images: ImageObject[], preprocessingArgs: any): void {}

Expand Down
33 changes: 33 additions & 0 deletions src/utils/models/Stardist/StardistFluo/StardistFluo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Stardist } from "../AbstractStardist";
import { loadStardistFluo } from "./loadStardistFluo";
import { ModelTask } from "../../enums";

/*
* Stardist (Versatile) Fluorescence Nuclei Segmentation
* https://zenodo.org/records/6348085
* https://bioimage.io/#/?tags=stardist&id=10.5281%2Fzenodo.6348084
* https://github.com/stardist/stardist/blob/master/README.md#pretrained-models-for-2d
* Stardist: model for object detection / instance segmentation with star-convex shapes
* This pretrained model: meant to segment individual cell nuclei from single channel fluorescence data (2018 DSB)
*/
export class StardistFluo extends Stardist {
constructor() {
super({
name: "StardistFluo",
task: ModelTask.Segmentation,
graph: true,
pretrained: true,
trainable: false,
requiredChannels: 3,
});
}

public async loadModel() {
if (this._model) return;
// inputs: [ {name: 'input', shape: [-1,-1,-1,1], dtype: 'float32'} ]
// outputs: [ {name: 'concatenate_4/concat', shape: [-1, -1, -1, 33], dtype: 'float32'} ]
// where each -1 matches on input and output of corresponding dim/axis
// 33 -> 1 probability score, followed by 32 radial equiangular distances of rays
this._model = await loadStardistFluo();
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Stardist2DFluorescenceWeights2 from "data/model-data/stardist-fluo/group1
* from relative paths described by the paths fields in weights manifest.
*/

export async function loadStardist() {
export async function loadStardistFluo() {
let modelDescription: File;
let modelWeights1: File;
let modelWeights2: File;
Expand Down Expand Up @@ -50,7 +50,7 @@ export async function loadStardist() {
const error: Error = err as Error;
process.env.NODE_ENV !== "production" &&
process.env.REACT_APP_LOG_LEVEL === "1" &&
console.error(`error loading stardist: ${error.message}`);
console.error(`error loading stardist fluorescence: ${error.message}`);
throw err;
}

Expand All @@ -65,7 +65,7 @@ export async function loadStardist() {

process.env.NODE_ENV !== "production" &&
process.env.REACT_APP_LOG_LEVEL === "1" &&
console.error(`error loading stardist: ${error.message}`);
console.error(`error loading stardist fluorescence: ${error.message}`);

throw err;
}
Expand Down
33 changes: 33 additions & 0 deletions src/utils/models/Stardist/StardistVHE/StardistVHE.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Stardist } from "../AbstractStardist";
import { loadStardistVHE } from "./loadStardistVHE";
import { ModelTask } from "../../enums";

/*
* Stardist (Versatile) H&E Nuclei Segmentation
* https://zenodo.org/record/6338615
* https://bioimage.io/#/?tags=stardist&id=10.5281%2Fzenodo.6338614&type=model
* https://github.com/stardist/stardist/blob/master/README.md#pretrained-models-for-2d
* Stardist: model for object detection / instance segmentation with star-convex shapes
* This pretrained model: meant to segment individual cell nuclei from brightfield images with H&E staining
*/
export class StardistVHE extends Stardist {
constructor() {
super({
name: "StardistVHE",
task: ModelTask.Segmentation,
graph: true,
pretrained: true,
trainable: false,
requiredChannels: 3,
});
}

public async loadModel() {
if (this._model) return;
// inputs: [ {name: 'input', shape: [-1,-1,-1,3], dtype: 'float32'} ]
// outputs: [ {name: 'concatenate_4/concat', shape: [-1, -1, -1, 33], dtype: 'float32'} ]
// where each -1 matches on input and output of corresponding dim/axis
// 33 -> 1 probability score, followed by 32 radial equiangular distances of rays
this._model = await loadStardistVHE();
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Stardist2DBrightfieldWeights2 from "data/model-data//stardist-vhe/group1-
* from relative paths described by the paths fields in weights manifest.
*/

export async function loadStardist() {
export async function loadStardistVHE() {
let modelDescription: File;
let modelWeights1: File;
let modelWeights2: File;
Expand Down Expand Up @@ -50,7 +50,7 @@ export async function loadStardist() {
const error: Error = err as Error;
process.env.NODE_ENV !== "production" &&
process.env.REACT_APP_LOG_LEVEL === "1" &&
console.error(`error loading stardist: ${error.message}`);
console.error(`error loading stardist H&E: ${error.message}`);
throw err;
}

Expand All @@ -65,7 +65,7 @@ export async function loadStardist() {

process.env.NODE_ENV !== "production" &&
process.env.REACT_APP_LOG_LEVEL === "1" &&
console.error(`error loading stardist: ${error.message}`);
console.error(`error loading stardist H&E: ${error.message}`);

throw err;
}
Expand Down
2 changes: 2 additions & 0 deletions src/utils/models/Stardist/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { StardistFluo } from "./StardistFluo";
export { StardistVHE } from "./StardistVHE";
164 changes: 0 additions & 164 deletions src/utils/models/StardistVHE/StardistVHE.ts

This file was deleted.

Loading

0 comments on commit 7cb9e66

Please sign in to comment.