Skip to content

Commit

Permalink
Merge pull request #19 from piximi/issue/456-classifier_models_not_cl…
Browse files Browse the repository at this point in the history
…eared

[fix] closes #456
  • Loading branch information
Andrea-Papaleo authored Jul 23, 2024
2 parents ec5b7b8 + c37ec42 commit 0bd4edb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const ExampleProjectCard = ({
break;
case ExampleProject.HumanU2OSCells:
exampleProjectFilePath =
process.env.NODE_ENV !== "production"
process.env.NODE_ENV === "production"
? `${domain}/${rootPath}/HumanU2OSCellsExampleProject.${ext}`
: (
await import(
Expand Down Expand Up @@ -140,14 +140,12 @@ export const ExampleProjectCard = ({
);
dispatch(dataSlice.actions.initializeState({ data }));
dispatch(projectSlice.actions.setProject({ project }));

dispatch(classifierSlice.actions.setDefaults({}));
dispatch(
classifierSlice.actions.setClassifier({
classifier,
})
);

dispatch(classifierSlice.actions.setDefaults({}));
});
} catch (err) {
const error: Error = err as Error;
Expand Down
32 changes: 27 additions & 5 deletions src/hooks/useLearningModel/useClassifierModelAgain.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { useEffect, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { selectAlertState } from "store/applicationSettings/selectors";
import { classifierSlice } from "store/classifier";
import {
selectClassifierFitOptions,
selectClassifierHistory,
selectClassifierModelStatus,
selectClassifierSelectedModel,
selectClassifierTrainingPercentage,
Expand Down Expand Up @@ -48,9 +47,22 @@ export const useClassificationModelAgain = () => {
const alertState = useSelector(selectAlertState);
const fitOptions = useSelector(selectClassifierFitOptions);
const trainingPercentage = useSelector(selectClassifierTrainingPercentage);
const modelHistory = useSelector((state) => {
return selectClassifierHistory(state, historyItems);
});

const modelHistory = useMemo(() => {
const fullHistory = selectedModel.history.history;
const selectedHistory: { [key: string]: number[] } = {};
for (const k of historyItems) {
if (k === "epochs") {
selectedHistory[k] = selectedModel.history.epochs;
} else {
selectedHistory[k] = fullHistory.flatMap(
(cycleHistory) => cycleHistory[k]
);
}
}

return selectedHistory;
}, [selectedModel]);
const noLabeledThingsAlert: AlertState = {
alertType: AlertType.Info,
name: "No labeled images",
Expand Down Expand Up @@ -122,6 +134,7 @@ export const useClassificationModelAgain = () => {
setShowWarning(true);
}
}, [labeledThingsCount, selectedModel]);

useEffect(() => {
setTrainingAccuracy(
modelHistory.categoricalAccuracy.map((y, i) => ({ x: i + 0.5, y }))
Expand Down Expand Up @@ -174,6 +187,15 @@ export const useClassificationModelAgain = () => {
}
}, [fitOptions.batchSize, trainingPercentage, labeledThingsCount]);

useEffect(() => {
if (modelStatus === ModelStatus.Uninitialized) {
setTrainingAccuracy([]);
setValidationAccuracy([]);
setTrainingLoss([]);
setValidationLoss([]);
setShowPlots(false);
}
}, [modelStatus]);
return {
showWarning,
setShowWarning,
Expand Down
8 changes: 7 additions & 1 deletion src/store/classifier/classifierSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ export const classifierSlice = createSlice({
name: "classifier",
initialState: initialState,
reducers: {
resetClassifier: () => initialState,
resetClassifier: (state) => {
availableClassifierModels[state.selectedModelIdx].dispose();
return initialState;
},
setClassifier(
state,
action: PayloadAction<{ classifier: ClassifierState }>
Expand All @@ -70,6 +73,9 @@ export const classifierSlice = createSlice({
},
setDefaults(state, action: PayloadAction<{}>) {
// TODO - segmenter: dispose() and state.selectedModel = SimpleCNN(), or whatever

availableClassifierModels[state.selectedModelIdx].dispose();

state.modelStatus = ModelStatus.Uninitialized;
state.evaluationResult = {
confusionMatrix: [],
Expand Down

0 comments on commit 0bd4edb

Please sign in to comment.