Class: DeepdetectRuby::Service
- Inherits:
-
Object
- Object
- DeepdetectRuby::Service
- Defined in:
- lib/service.rb
Class Method Summary collapse
- .create(options = {}) ⇒ Object
- .delete(service_name = "", server_index = 1) ⇒ Object
- .finetuning(options = {}) ⇒ Object
- .get_info(service_name = "", server_index = 1) ⇒ Object
- .list_models ⇒ Object
- .list_models_running(options = {}) ⇒ Object
- .load_all_models(options = {}) ⇒ Object
- .reload_models(options = {}) ⇒ Object
Class Method Details
.create(options = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/service.rb', line 3 def self.create( = {}) begin debug = DeepdetectRuby.[:debug] server_index = [:server_index] || 1 dede_host = DeepdetectRuby::DedeServer.get_server(server_index) dede_api_url = "#{dede_host}/services/#{[:name]}" puts "\n-------------> Starting to create DeepDetect service #{dede_api_url}....\n" if debug description = "#{[:name]} classification service" [:mllib] = ![:mllib].nil? ? [:mllib] : "caffe" [:description] = ![:description].nil? ? [:description] : description [:type] = ![:type].nil? ? [:type] : "supervised" [:connector] = ![:connector].nil? ? [:connector] : "image" [:width] = ![:width].nil? ? [:width] : 224 [:height] = ![:height].nil? ? [:height] : 224 [:nclasses] = ![:nclasses].nil? ? [:nclasses] : 2 [:gpu] = ![:gpu].nil? ? [:gpu] : true # for finetuning [:finetuning] = ![:finetuning].nil? ? [:finetuning] : false [:template] = ![:template].nil? ? [:template] : "googlenet" [:weights] = ![:weights].nil? ? [:weights] : nil [:rotate] = ![:rotate].nil? ? [:rotate] : false [:mirror] = ![:mirror].nil? ? [:mirror] : false [:mirror] = [:finetuning] if !DeepdetectRuby.[:model_path].nil? && !DeepdetectRuby.[:model_path].empty? [:repository] = "#{DeepdetectRuby.[:model_path]}/#{[:name]}" end # for different model name if ![:model_name].nil? && ![:model_name].empty? [:repository] = "#{DeepdetectRuby.[:model_path]}/#{[:model_name]}" end data = { "mllib" => "#{[:mllib]}", "description" => "#{[:description]}", "type" => "#{[:type]}", "parameters" => { "input" => { "connector" => "#{[:connector]}", "height" => [:height], "width" => [:width] }, "mllib" => { "nclasses" => [:nclasses], "gpu" => [:gpu] } }, "model" => { "repository" => "#{[:repository]}" } } if [:finetuning] && ![:weights].nil? data = { "mllib" => "#{[:mllib]}", "description" => "#{[:description]}", "type" => "#{[:type]}", "parameters" => { "input" => { "connector" => "#{[:connector]}", "height" => [:height], "width" => [:width] }, "mllib" => { "nclasses" => [:nclasses], "gpu" => [:gpu], "weights" => [:weights], "finetuning" => [:finetuning], "rotate" => [:rotate], "mirror" => [:mirror] } }, "model" => { "repository" => "#{[:repository]}" } } end puts "\nparams data: #{data.to_json} \n" if debug dede_body = { :body => data.to_json, :headers => { "Content-Type" => 'application/json' } } object = HTTParty.put(dede_api_url, dede_body) object_info = JSON.parse(object.body) object_info.extend DeepSymbolizable return object_info.deep_symbolize rescue Exception => e puts "\n[DeepdetectRuby Service]. #{e.to_s} \n" end end |
.delete(service_name = "", server_index = 1) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/service.rb', line 114 def self.delete(service_name = "", server_index = 1) begin debug = DeepdetectRuby.[:debug] dede_host = DeepdetectRuby::DedeServer.get_server(server_index) dede_api_url = "#{dede_host}/services/#{service_name}" puts "\n-------------> Starting to delete DeepDetect service #{dede_api_url}....\n" if debug object = HTTParty.delete(dede_api_url) object_info = JSON.parse(object.body) object_info.extend DeepSymbolizable return object_info.deep_symbolize rescue Exception => e puts "\n[DeepdetectRuby delete service]. #{e.to_s} \n" end end |
.finetuning(options = {}) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/service.rb', line 180 def self.finetuning( = {}) puts "\n-----> Starting to create service and train for finetuning \n" server_index = [:server_index] || 1 dede_host = DeepdetectRuby::DedeServer.get_server(server_index) [:name] = ![:name].nil? ? [:name] : "" # weights is called the caffe filename model, example: model_iter_16368.caffemodel [:weights] = ![:weights].nil? ? [:weights] : "" [:repository] = ![:repository].nil? ? [:repository] : "" [:gpu] = ![:gpu].nil? ? [:gpu] : true [:iterations] = ![:iterations].nil? ? [:iterations] : 10000 [:test_interval] = ![:test_interval].nil? ? [:test_interval] : 500 [:nclasses] = ![:nclasses].nil? ? [:nclasses] : 2 [:measure_index] = ![:measure_index].nil? ? [:measure_index] : 1 [:batch_size] = ![:batch_size].nil? ? [:batch_size] : 32 [:test_batch_size] = ![:test_batch_size].nil? ? [:test_batch_size] : 32 if ![:name].empty? && ![:weights].empty? && ![:repository].empty? # Create a service for finetuning = { name: "#{[:name]}", finetuning: true, weights: "#{[:weights]}", nclasses: [:nclasses], server_index: server_index } DeepdetectRuby::Service.create() puts "\nWaiting to start function fine tune..." sleep(3) # Train a new updated service for finetuning = { service: "#{[:name]}", repository: "#{[:repository]}", gpu: true, iterations: [:iterations], test_interval: [:test_interval], finetuning: true, measure_index: [:measure_index], batch_size: [:batch_size], test_batch_size: [:test_batch_size], server_index: server_index } DeepdetectRuby::Train.launch() else puts "\nMissing options :name or :weights or :repository. Please check. :) \n" end end |
.get_info(service_name = "", server_index = 1) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/service.rb', line 99 def self.get_info(service_name = "", server_index = 1) begin debug = DeepdetectRuby.[:debug] dede_host = DeepdetectRuby::DedeServer.get_server(server_index) dede_api_url = "#{dede_host}/services/#{service_name}" puts "\n-------------> Starting to get information DeepDetect service #{dede_api_url}....\n" if debug object = HTTParty.get(dede_api_url) object_info = JSON.parse(object.body) object_info.extend DeepSymbolizable return object_info.deep_symbolize rescue Exception => e puts "\n[DeepdetectRuby Service get information]. #{e.to_s} \n" end end |
.list_models ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/service.rb', line 160 def self.list_models debug = DeepdetectRuby.[:debug] puts "\n-----------> Starting to reload models..." if debug model_path = DeepdetectRuby.[:model_path] dirs = Dir.glob("#{model_path}/*").select{ |e| File.directory? e } list = [] dirs.each_with_index do |dir, index| model_name = File.basename(dir) list << model_name end puts "\nList models: #{list.to_json}" if debug return list end |
.list_models_running(options = {}) ⇒ Object
174 175 176 177 178 |
# File 'lib/service.rb', line 174 def self.list_models_running( = {}) dede = DeepdetectRuby.info() services = dede[:head][:services] return services end |
.load_all_models(options = {}) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/service.rb', line 129 def self.load_all_models( = {}) debug = DeepdetectRuby.[:debug] server_index = [:server_index] || 1 dede_host = DeepdetectRuby::DedeServer.get_server(server_index) puts "\n-----------> Starting to load_all_models..." if debug model_path = DeepdetectRuby.[:model_path] dirs = Dir.glob("#{model_path}/*").select{ |e| File.directory? e } dirs.each_with_index do |dir, index| model_name = File.basename(dir) dede_opts = { repository: dir, name: model_name, server_index: server_index } DeepdetectRuby::Service.create(dede_opts) puts "Index: #{index} - model name: #{model_name} in #{dir}" if debug end puts "\n-----------> End to load_all_models..." if debug end |
.reload_models(options = {}) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/service.rb', line 145 def self.reload_models( = {}) debug = DeepdetectRuby.[:debug] server_index = [:server_index] || 1 dede_host = DeepdetectRuby::DedeServer.get_server(server_index) puts "\n-----------> Starting to reload models..." if debug model_path = DeepdetectRuby.[:model_path] dirs = Dir.glob("#{model_path}/*").select{ |e| File.directory? e } dirs.each_with_index do |dir, index| model_name = File.basename(dir) DeepdetectRuby::Service.delete(model_name, server_index) end DeepdetectRuby::Service.load_all_models(server_index) puts "\n-----------> End to reload models..." if debug end |