Class: TorchVision::Datasets::DatasetFolder

Inherits:
VisionDataset
  • Object
show all
Defined in:
lib/torchvision/datasets/dataset_folder.rb

Direct Known Subclasses

ImageFolder

Instance Attribute Summary collapse

Attributes inherited from VisionDataset

#data, #targets

Instance Method Summary collapse

Constructor Details

#initialize(root, extensions: nil, transform: nil, target_transform: nil, is_valid_file: nil) ⇒ DatasetFolder

Returns a new instance of DatasetFolder.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/torchvision/datasets/dataset_folder.rb', line 6

def initialize(root, extensions: nil, transform: nil, target_transform: nil, is_valid_file: nil)
  super(root, transform: transform, target_transform: target_transform)
  classes, class_to_idx = find_classes(@root)
  samples = make_dataset(@root, class_to_idx, extensions, is_valid_file)
  if samples.empty?
    msg = "Found 0 files in subfolders of: #{@root}\n"
    unless extensions.nil?
      msg += "Supported extensions are: #{extensions.join(",")}"
    end
    raise RuntimeError, msg
  end

  @loader = lambda do |path|
    Vips::Image.new_from_file(path)
  end
  @extensions = extensions

  @classes = classes
  @class_to_idx = class_to_idx
  @samples = samples
  @targets = samples.map { |s| s[1] }
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



4
5
6
# File 'lib/torchvision/datasets/dataset_folder.rb', line 4

def classes
  @classes
end

Instance Method Details

#[](index) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/torchvision/datasets/dataset_folder.rb', line 29

def [](index)
  path, target = @samples[index]
  sample = @loader.call(path)
  if @transform
    sample = @transform.call(sample)
  end
  if @target_transform
    target = @target_transform.call(target)
  end

  [sample, target]
end

#sizeObject Also known as: count, length



42
43
44
# File 'lib/torchvision/datasets/dataset_folder.rb', line 42

def size
  @samples.size
end