1

Any idea how to achieve the same as

https://www.tensorflow.org/tutorials/images/classification

in tensorflow.js under node.js? I can't see how to load the images in subfolders, resize them and convert them to tensors.

The tutorial is about classifying an image, we have two folders/labels: cats and dogs, inside two subfolders "training" and "validation"

Any help would be appreciated.

Hamz Sider
  • 59
  • 8
  • There is not yet packages to create data pipeline in nodejs as it is in python. You can write your own to do it – edkeveked Jul 30 '20 at 07:30
  • There are a number packages @edkeveked - like my own `scramjet` for instance. The data must then be loaded to tensorflow.js. I can help with this, but we'd need to start with some testable example - if you point me to a repo - I'd be happy to check it out. – Michał Karpacki Jul 30 '20 at 09:25
  • @edkeveked Yep, I can see that, it would be great if we could achieve a similar data pipeline for node.js developers. – Hamz Sider Jul 30 '20 at 10:35
  • @MichałKapracki here's a git https://github.com/sid3r/catordog, I modified the script to store images with label and directory. – Hamz Sider Jul 30 '20 at 11:58
  • @HamzSider I'll look into this over the weekend. :) – Michał Karpacki Jul 31 '20 at 09:43

1 Answers1

0

Folder structure:

  1. data
    • train
      • cats
      • dogs
    • validation
      • cats
      • dogs

the script succeeded to scan the "data" directory and it's sub folder, resize the images, save them in the corresponding folder in "resized" directory and return "images" array with "label" and "path".

let path = require('path');
let fs = require('fs');
let sd = require('scandirectory');
let sharp = require('sharp');
// params
const IMG_HEIGHT = 150
const IMG_WIDTH = 150
const options = {}
let uniq_labels = []
let images = []
// let images_tensor = {}
const data_path = path.join(__dirname, '../data/');
const resized_path = path.join(__dirname, '../resized/');

// resize images
sd(data_path, options, scanDirCb);

function scanDirCb(err, list, tree) {
    let labels = []
    if (err) {
        console.log('Unable to scan directory: ' + err);
        return
    } else {
        // loop
        Object.entries(tree).forEach(([folder, label]) => {
            // create sub folder
            if (!fs.existsSync(resized_path + folder)) {
                fs.mkdirSync(resized_path + folder);
            }
            Object.entries(label).forEach(([label_name, content]) => {
                // new image
                let new_image = {
                    "label": label_name
                };
                // save labels
                labels.push(label_name);

                if (!fs.existsSync(resized_path + folder + '/' + label_name)) {
                    fs.mkdirSync(resized_path + folder + '/' + label_name);
                }
                // saving images
                for (let image in content) {
                    if (content.hasOwnProperty(image)) {
                        // console.log(`Resizing image: ${image}`)
                        // save image path
                        new_image.path = resized_path + folder + '/' + label_name + '/' + image;

                        sharp(path.join(data_path + folder + '/' + label_name, '/' + image))
                            .resize(IMG_HEIGHT, IMG_WIDTH)
                            .jpeg({
                                quality: 90
                            }).toFile(path.join(resized_path + folder + '/' + label_name, '/' + image));

                        images.push(new_image)
                    }
                }
            });
        });
    };
    // remove dublicate labels
    uniq_labels = [...new Set(labels)];
    // console.log(images)
    // results: 
    // { label: 'cats',
    //     path: '/Users/mac/MachineLearning/cats-dogs/resized/train/cats/cat.999.jpg'
    // }, {
    //     label: 'cats',
    //     path: '/Users/mac/MachineLearning/cats-dogs/resized/train/cats/cat.999.jpg'
    // } ...more lines
}


how do I load and and pass this data to tensorflow.js?

Hamz Sider
  • 59
  • 8
  • See this article on hold to load image datasets into tensorflow.js – https://stackoverflow.com/questions/58953399/how-to-train-a-model-in-nodejs-tensorflow-js – Josh Sep 22 '20 at 15:26