[Vuejs]-Vue and Tensorflow: Save classifier examples to localstorage

2๐Ÿ‘

โœ…

So, after small research I managed to save and load data with the next methods:

    async toDatasetObject(dataset) {
      const result = await Promise.all(
        Object.entries(dataset).map(async ([classId, value]) => {
          const data = await value.data();

          return {
            label: Number(classId),
            data: Array.from(data),
            shape: value.shape
          };
        })
      );

      return result;
    },
    fromDatasetObject(datasetObject) {
      return Object.entries(datasetObject).reduce(
        (result, [indexString, { data, shape }]) => {
          const tensor = tf.tensor2d(data, shape);
          const index = Number(indexString);

          result[index] = tensor;

          return result;
        },
        {}
      );
    },

And then I just load it:

this.classifier.setClassifierDataset(
   this.fromDatasetObject(JSON.parse(localStorage.getItem("my-data")))
);

Leave a comment