Skip to content

Commit

Permalink
Merge pull request #22 from piximi/issue/18-dispose-tensors
Browse files Browse the repository at this point in the history
[fix] disposes tensors when reseting data
  • Loading branch information
Andrea-Papaleo authored Jul 23, 2024
2 parents 9c6eb46 + 75418b3 commit b300aea
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export const NewProjectDialog = ({ onClose, open }: NewProjectDialogProps) => {
})
);
dispatch(dataSlice.actions.resetData());

dispatch(classifierSlice.actions.resetClassifier());

closeDialog();
Expand Down
20 changes: 11 additions & 9 deletions src/store/data/dataSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ export const dataSlice = createSlice({
initialState: initialState,
reducers: {
resetData: (state) => {
Object.values(state.things.entities).forEach((entity) => {
dispose(entity.saved.data as unknown as TensorContainer);
if ("colors" in entity.saved) {
dispose(entity.saved.colors as unknown as TensorContainer);
}
});
return initialState();
},
initializeState(
Expand Down Expand Up @@ -849,7 +855,7 @@ export const dataSlice = createSlice({
) {
if (!action.payload.preparedByListener) return;
if (!("thingIds" in action.payload)) return;
const { thingIds, disposeColorTensors, isPermanent } = action.payload;
const { thingIds, isPermanent } = action.payload;
const imageChanges: Record<
string,
{
Expand Down Expand Up @@ -885,10 +891,8 @@ export const dataSlice = createSlice({
const kind = state.kinds.entities[thingKind];
const category = state.categories.entities[thingCategoryId];
if (isPermanent) {
if (disposeColorTensors) {
dispose(containedThing.saved.data as TensorContainer);
dispose(containedThing.changes as TensorContainer);
}
dispose(containedThing.saved.data as TensorContainer);
dispose(containedThing.changes as TensorContainer);

/* UPDATE KIND'S CONTAINING LIST */
mutatingFilter(
Expand Down Expand Up @@ -952,10 +956,8 @@ export const dataSlice = createSlice({
const kind = state.kinds.entities[thingKind];
const category = state.categories.entities[thingCategoryId];
if (isPermanent) {
if (disposeColorTensors) {
dispose(thingEntity.saved.data as TensorContainer);
dispose(thingEntity.changes as TensorContainer);
}
dispose(thingEntity.saved.data as TensorContainer);
dispose(thingEntity.changes as TensorContainer);

/* UPDATE KIND'S CONTAINING LIST */

Expand Down
1 change: 1 addition & 0 deletions src/utils/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ export const getPropertiesFromImageSync = (
});
const objSrc = objectImage.getCanvas().toDataURL();
const data = tfImage.cropAndResize(image.data, box, [0], [height, width]);
box.dispose();

return {
data: data,
Expand Down
24 changes: 12 additions & 12 deletions src/utils/common/tensorHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,22 +378,22 @@ export const findMinMaxs = async <T extends Tensor3D | Tensor4D>(
): Promise<[number[], number[]]> => {
let mins: number[];
let maxs: number[];
let minTensor: Tensor1D;
let maxTensor: Tensor1D;

if (imageTensor.rank === 3) {
mins = await tidy(
() => (imageTensor as Tensor3D).min([0, 1]) as Tensor1D
).array();
maxs = await tidy(
() => (imageTensor as Tensor3D).max([0, 1]) as Tensor1D
).array();
minTensor = (imageTensor as Tensor3D).min([0, 1]);
mins = minTensor.arraySync();
maxTensor = (imageTensor as Tensor3D).max([0, 1]);
maxs = maxTensor.arraySync();
} else {
mins = await tidy(
() => (imageTensor as Tensor4D).min([0, 1, 2]) as Tensor1D
).array();
maxs = await tidy(
() => (imageTensor as Tensor4D).max([0, 1, 2]) as Tensor1D
).array();
minTensor = (imageTensor as Tensor4D).min([0, 1, 2]);
mins = minTensor.arraySync();
maxTensor = (imageTensor as Tensor4D).max([0, 1, 2]);
maxs = maxTensor.arraySync();
}
minTensor.dispose();
maxTensor.dispose();

opts.disposeImageTensor && imageTensor.dispose();
return [mins, maxs];
Expand Down
1 change: 1 addition & 0 deletions src/views/Application/Application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const Application = () => {
});

dispatch(dataSlice.actions.initializeState({ data: dataState }));

dispatch(projectSlice.actions.setProjectImageChannels({ channels: 3 }));
setHasLoaded(true);
}, [dispatch]);
Expand Down

0 comments on commit b300aea

Please sign in to comment.