Search code examples
lualuajittorch

Read images from local disk using torch7 while those images saved in different subfolders?


I have images which have saved in the desk. The data saved as follow: 4 main folders (1,2,3 and 4) each folder has 26 subfolders ( these subfolders represent the class of images (A, B, C, D, ..,Z)). Each of these subfolder contains more than 500 images. However, I am looking for file or code in torch that can read these images. In MATLAB I could wrote a code but here I find it confuse. Could you please advise me.


Solution

  • What you can do is use Penlight (the library is installed when you install Torch).

    Penlight provides pl.dir that makes it easy to scan files in (sub-)folders. For example what you can do is:

    local pl = require('pl.import_into')()
    local t = {}
    for i,f in ipairs(pl.dir.getallfiles('/data/foo', '*.jpg')) do
        t[i] = { f, pl.path.basename(pl.path.dirname(f)) }
    end
    

    This creates a list of pairs (filename, class label = "A" or "B" ...). Of course you are free to change the file pattern (*.jpg) or to omit it (in such a case Penlight will simply list all files). You can also load the images on the fly:

    t[i] = { image.load(f), pl.path.basename(pl.path.dirname(f)) }
    

    Or do that right after when manipulating t.