Class: Newral::Data::Idx

Inherits:
Base
  • Object
show all
Defined in:
lib/newral/data/idx.rb

Instance Attribute Summary

Attributes inherited from Base

#inputs, #labels, #outputs

Instance Method Summary collapse

Methods inherited from Base

#add_input, #count_outputs, #downsample_input!, #inputs_for_output, #label_for_input, #normalized_inputs, #output_as_vector, #output_for_input, #output_hash, #output_normalized, #sample, #sub_set, #values_for

Constructor Details

#initialize(file_name: nil, label_file_name: nil) ⇒ Idx

yann.lecun.com/exdb/mnist/ used for Handwritten Images



13
14
15
16
17
# File 'lib/newral/data/idx.rb', line 13

def initialize( file_name: nil, label_file_name: nil )
  @file_name = file_name
  @label_file_name = label_file_name
  super( inputs: [], outputs: [])
end

Instance Method Details

#processObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/newral/data/idx.rb', line 19

def process
  number_of_items = 0
  open( @file_name, 'rb' ) do |file|
    magic,number_of_items = file.read(8).unpack("NN")
    width,height = file.read(8).unpack("NN")
    number_of_items.times do 
      raise Errors::UnexpectedEOF if file.eof?
      image = file.read(width*height).unpack("C"*width*height)
      @inputs << image 
    end 
    raise Errors::EOFExpected unless file.eof?
  end

  open( @label_file_name, 'rb' ) do |file|
    magic,number_of_labels = file.read(8).unpack("NN")
    raise Errors::LabelsNotMatchingItems unless number_of_labels==number_of_items
    number_of_items.times do 
      raise Errors::UnexpectedEOF,"#{ @outputs.size } vs. #{ number_of_labels }" if file.eof?
      label = file.read(1).unpack("c").first
      @outputs << label 
    end 
    raise Errors::EOFExpected,"#{ @outputs.size } #{file.read.size}" unless file.eof?
  end


end