Linn Bedroc commited on
Commit
1e9f296
1 Parent(s): ffb9b72
Files changed (3) hide show
  1. nerf/renderer.py +4 -1
  2. obj2glb.py +11 -0
  3. server.cpp +57 -19
nerf/renderer.py CHANGED
@@ -296,8 +296,11 @@ class NeRFRenderer(nn.Module):
296
  fp.write(f'Ns 0.000000 \n')
297
  fp.write(f'map_Kd {name}albedo.png \n')
298
 
 
 
299
  _export(v, f)
300
 
 
301
  def run(self, rays_o, rays_d, num_steps=128, upsample_steps=128, light_d=None, ambient_ratio=1.0, shading='albedo', bg_color=None, perturb=False, **kwargs):
302
  # rays_o, rays_d: [B, N, 3], assumes B == 1
303
  # bg_color: [BN, 3] in range [0, 1]
@@ -642,4 +645,4 @@ class NeRFRenderer(nn.Module):
642
  else:
643
  results = _run(rays_o, rays_d, **kwargs)
644
 
645
- return results
 
296
  fp.write(f'Ns 0.000000 \n')
297
  fp.write(f'map_Kd {name}albedo.png \n')
298
 
299
+ os.system(f"blender -b -P obj2glb.py")
300
+
301
  _export(v, f)
302
 
303
+
304
  def run(self, rays_o, rays_d, num_steps=128, upsample_steps=128, light_d=None, ambient_ratio=1.0, shading='albedo', bg_color=None, perturb=False, **kwargs):
305
  # rays_o, rays_d: [B, N, 3], assumes B == 1
306
  # bg_color: [BN, 3] in range [0, 1]
 
645
  else:
646
  results = _run(rays_o, rays_d, **kwargs)
647
 
648
+ return results
obj2glb.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # using blender, open an input.obj file, and export it as output.glb
2
+ import bpy
3
+ # import input.obj file
4
+ bpy.ops.import_scene.obj(filepath='trial/mesh/mesh.obj')
5
+
6
+ # select all
7
+ bpy.ops.object.select_all(action='SELECT')
8
+
9
+ # export output.glb file
10
+ bpy.ops.export_scene.gltf(filepath="model.glb")
11
+ print('done')
server.cpp CHANGED
@@ -9,11 +9,15 @@
9
  #include <mutex>
10
  #include <vector>
11
  #include <filesystem>
12
- #include <sstream>
13
- #define CROW_MAIN
14
  #include <crow.h>
15
  #include <ranges>
16
  #include "threadpool.h"
 
 
 
 
 
 
17
  using std::string;
18
  using std::mutex;
19
  using std::lock_guard;
@@ -22,6 +26,11 @@ using std::queue;
22
  using std::vector;
23
  namespace fs = std::filesystem;
24
  namespace rv = std::ranges::views;
 
 
 
 
 
25
  static inline string exec(const char* cmd) {
26
  std::array<char, 128> buffer;
27
  std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
@@ -58,14 +67,11 @@ constexpr vector<string> splitOn(const string& s, const string& delim){
58
  return ret;
59
  }
60
 
61
- static inline string reset = "rm -rf trial/checkpoints/*";
62
 
63
  static inline string train(const string& prompt){
64
- return string("python main.py --cuda_ray --save_mesh --text \"") + strip(prompt, '\'', '\"') + "\" --workspace trial -O";
65
  }
66
 
67
- static inline string save(const string& name){ return string("zip ") + name + ".zip trial/mesh/*"; }
68
-
69
  template <typename T>
70
  constexpr auto q_to_v(queue<T> qcopy){
71
  vector<T> v;
@@ -75,6 +81,35 @@ constexpr auto q_to_v(queue<T> qcopy){
75
  return v;
76
  }
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  int main(){
79
  crow::SimpleApp app;
80
  typedef std::array<string, 2> guy;
@@ -82,6 +117,7 @@ int main(){
82
  auto queue_mutex = make_shared<mutex>()
83
  , train_mutex = make_shared<mutex>();
84
  auto pool = make_shared<threadpool<>>(avail_threads() / 2);
 
85
  auto run = [=](const string& cmd){
86
  CROW_LOG_INFO << "running \'" << cmd;
87
  return exec(cmd.c_str());
@@ -107,10 +143,13 @@ int main(){
107
  auto training_loop = [=](){
108
  lock_guard<mutex> lock(*train_mutex);
109
  while(!commissions->empty()){
110
- auto& [name, prompt] = commissions->front();
111
- CROW_LOG_INFO << "Launched training for " + name;
112
- run(reset), run(train(prompt)), run(save(name));
113
- CROW_LOG_INFO << "Finished training for " + name;
 
 
 
114
  poppe();
115
  }
116
  };
@@ -123,20 +162,19 @@ int main(){
123
  };
124
 
125
  CROW_ROUTE(app, "/create/<string>")
126
- .methods("GET"_method, "POST"_method)([=](const crow::request& req, const string& name){
127
- CROW_LOG_INFO << name << " commissioned";
128
- if(auto prompt = req.url_params.get("prompt"); prompt == nullptr){
129
  CROW_LOG_INFO << "No prompt specified";
130
- return "Error: Can't train a NeRF for " + name + " without a prompt!";
131
  } else {
132
- if(auto r = check_queue(name); r < 0){
133
- enqueue({name, prompt});
134
  pool->enqueue(training_loop);
135
  CROW_LOG_INFO << "Launched training loop";
136
- return "Scheduled training for " + name;
137
  } else
138
- return name + " is currently "
139
- + (r ? string("in line") : string("training"));
140
  }
141
  });
142
 
 
9
  #include <mutex>
10
  #include <vector>
11
  #include <filesystem>
 
 
12
  #include <crow.h>
13
  #include <ranges>
14
  #include "threadpool.h"
15
+ #include <aws/core/Aws.h>
16
+ #include <aws/s3/S3Client.h>
17
+ #include <aws/s3/model/PutObjectRequest.h>
18
+ #include <sstream>
19
+ #define CROW_MAIN
20
+
21
  using std::string;
22
  using std::mutex;
23
  using std::lock_guard;
 
26
  using std::vector;
27
  namespace fs = std::filesystem;
28
  namespace rv = std::ranges::views;
29
+
30
+ constexpr string to_st(const auto& i){ std::stringstream ss; ss << i; return ss.str(); }
31
+
32
+ static inline auto uid(const std::string& s){ std::hash<string> h; return to_st(h(s)); }
33
+
34
  static inline string exec(const char* cmd) {
35
  std::array<char, 128> buffer;
36
  std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
 
67
  return ret;
68
  }
69
 
 
70
 
71
  static inline string train(const string& prompt){
72
+ return string("python main.py --save_mesh --text \"") + strip(prompt, '\'', '\"') + "\" --workspace trial -O";
73
  }
74
 
 
 
75
  template <typename T>
76
  constexpr auto q_to_v(queue<T> qcopy){
77
  vector<T> v;
 
81
  return v;
82
  }
83
 
84
+ constexpr auto stdfopts = std::ios_base::in | std::ios_base::binary;
85
+ static inline auto awssave(){
86
+ auto options = make_shared<Aws::SDKOptions>();
87
+ Aws::InitAPI(*options);
88
+ Aws::Client::ClientConfiguration clientConfig;
89
+ std::shared_ptr<Aws::S3::S3Client> s3_client( new Aws::S3::S3Client(clientConfig)
90
+ , [=](Aws::S3::S3Client* c){
91
+ Aws::ShutdownAPI(*options);
92
+ delete c;
93
+ });
94
+ return [=](const std::string& id){
95
+ Aws::S3::Model::PutObjectRequest request;
96
+ request.SetBucket("models.webaverse.com");
97
+ request.SetKey(id);
98
+ if(auto in = Aws::MakeShared<Aws::FStream>("a", "model.glb", stdfopts); *in){
99
+ request.SetBody(in);
100
+ auto outcome = s3_client->PutObject(request);
101
+ if(outcome.IsSuccess())
102
+ std::cout << "Added '" << id << "' to bucket 'models.webaverse.com'\n";
103
+ else
104
+ std::cerr << "AWS S3 Error: " << outcome.GetError().GetMessage() << "\n";
105
+ return outcome.IsSuccess();
106
+ } else {
107
+ std::cerr << "No such file: model.glb\n";
108
+ return false;
109
+ }
110
+ };
111
+ }
112
+
113
  int main(){
114
  crow::SimpleApp app;
115
  typedef std::array<string, 2> guy;
 
117
  auto queue_mutex = make_shared<mutex>()
118
  , train_mutex = make_shared<mutex>();
119
  auto pool = make_shared<threadpool<>>(avail_threads() / 2);
120
+
121
  auto run = [=](const string& cmd){
122
  CROW_LOG_INFO << "running \'" << cmd;
123
  return exec(cmd.c_str());
 
143
  auto training_loop = [=](){
144
  lock_guard<mutex> lock(*train_mutex);
145
  while(!commissions->empty()){
146
+ auto& [id, prompt] = commissions->front();
147
+ CROW_LOG_INFO << "Launched training for prompt: " + prompt;
148
+ run("rm -rf trial/checkpoints/*");
149
+ run(train(prompt));
150
+ CROW_LOG_INFO << run("aws s3 cp model.glb s3://models.webaverse.com/" + id + ".glb");
151
+ run("rm model.glb");
152
+ CROW_LOG_INFO << "Finished training for prompt: " + prompt;
153
  poppe();
154
  }
155
  };
 
162
  };
163
 
164
  CROW_ROUTE(app, "/create/<string>")
165
+ .methods("GET"_method, "POST"_method)([=](const crow::request& req, const string& prompt) -> string {
166
+ CROW_LOG_INFO << prompt << " commissioned";
167
+ if(auto id = uid(prompt); prompt.empty()){
168
  CROW_LOG_INFO << "No prompt specified";
169
+ return "Error: Can't train a NeRF without a prompt!";
170
  } else {
171
+ if(auto r = check_queue(id); r < 0){
172
+ enqueue({id, prompt});
173
  pool->enqueue(training_loop);
174
  CROW_LOG_INFO << "Launched training loop";
175
+ return "Scheduled training for " + id;
176
  } else
177
+ return id + " is currently " + (r ? string("in line") : string("training"));
 
178
  }
179
  });
180