Class: KerasModelConvertor

Inherits:
Object
  • Object
show all
Defined in:
lib/dnn/keras-model-convertor.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(k_model) ⇒ KerasModelConvertor

Returns a new instance of KerasModelConvertor.



37
38
39
# File 'lib/dnn/keras-model-convertor.rb', line 37

def initialize(k_model)
  @k_model = k_model
end

Class Method Details

.load(k_model_name, k_weights_name = nil) ⇒ Object



31
32
33
34
35
# File 'lib/dnn/keras-model-convertor.rb', line 31

def self.load(k_model_name, k_weights_name = nil)
  k_model = load_model(k_model_name)
  k_model.load_weights(k_weights_name) if k_weights_name
  self.new(k_model)
end

Instance Method Details

#convert(not_support_to_nil: false, debug_message: false) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/dnn/keras-model-convertor.rb', line 41

def convert(not_support_to_nil: false, debug_message: false)
  unless @k_model.__class__.__name__ == "Sequential"
    raise DNNKerasModelConvertError.new("#{@k_model.__class__.__name__} models do not support convert.")
  end
  layers = convert_layers(not_support_to_nil: not_support_to_nil, debug_message: debug_message)
  dnn_model = DNN::Models::Sequential.new(layers)
  dnn_model
end

#convert_layers(not_support_to_nil: false, debug_message: false) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dnn/keras-model-convertor.rb', line 50

def convert_layers(not_support_to_nil: false, debug_message: false)
  layers = []
  @k_model.layers.each do |k_layer|
    layer = if not_support_to_nil
      begin
        layer_convert(k_layer)
      rescue DNNKerasLayerNotConvertSupportError => e
        nil
      end
    else
      layer_convert(k_layer)
    end
    if layer.is_a?(Array)
      layer.each { |l| puts "Converted #{l.class.name} layer" } if debug_message
      layers += layer
    else
      puts "Converted #{layer.class.name} layer" if debug_message
      layers << layer
    end
  end
  layers
end