web.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. require 'pl'
  2. local __FILE__ = (function() return string.gsub(debug.getinfo(2, 'S').source, "^@", "") end)()
  3. local ROOT = path.dirname(__FILE__)
  4. package.path = path.join(ROOT, "lib", "?.lua;") .. package.path
  5. _G.TURBO_SSL = true
  6. require 'w2nn'
  7. local uuid = require 'uuid'
  8. local ffi = require 'ffi'
  9. local md5 = require 'md5'
  10. local iproc = require 'iproc'
  11. local reconstruct = require 'reconstruct'
  12. local image_loader = require 'image_loader'
  13. local alpha_util = require 'alpha_util'
  14. local gm = require 'graphicsmagick'
  15. -- Note: turbo and xlua has different implementation of string:split().
  16. -- Therefore, string:split() has conflict issue.
  17. -- In this script, use turbo's string:split().
  18. local turbo = require 'turbo'
  19. local cmd = torch.CmdLine()
  20. cmd:text()
  21. cmd:text("waifu2x-api")
  22. cmd:text("Options:")
  23. cmd:option("-port", 8812, 'listen port')
  24. cmd:option("-gpu", 1, 'Device ID')
  25. cmd:option("-crop_size", 128, 'patch size per process')
  26. cmd:option("-batch_size", 1, 'batch size')
  27. cmd:option("-thread", -1, 'number of CPU threads')
  28. cmd:option("-force_cudnn", 0, 'use cuDNN backend (0|1)')
  29. cmd:option("-max_pixels", 3000 * 3000, 'maximum number of output image pixels (e.g. 3000x3000=9000000)')
  30. cmd:option("-curl_request_timeout", 60, "request_timeout for curl")
  31. cmd:option("-curl_connect_timeout", 60, "connect_timeout for curl")
  32. cmd:option("-curl_max_redirects", 2, "max_redirects for curl")
  33. cmd:option("-max_body_size", 5 * 1024 * 1024, "maximum allowed size for uploaded files")
  34. local opt = cmd:parse(arg)
  35. cutorch.setDevice(opt.gpu)
  36. torch.setdefaulttensortype('torch.FloatTensor')
  37. if opt.thread > 0 then
  38. torch.setnumthreads(opt.thread)
  39. end
  40. if cudnn then
  41. cudnn.fastest = true
  42. cudnn.benchmark = true
  43. end
  44. opt.force_cudnn = opt.force_cudnn == 1
  45. local ART_MODEL_DIR = path.join(ROOT, "models", "upconv_7", "art")
  46. local PHOTO_MODEL_DIR = path.join(ROOT, "models", "upconv_7", "photo")
  47. local art_model = {
  48. scale = w2nn.load_model(path.join(ART_MODEL_DIR, "scale2.0x_model.t7"), opt.force_cudnn),
  49. noise0_scale = w2nn.load_model(path.join(ART_MODEL_DIR, "noise0_scale2.0x_model.t7"), opt.force_cudnn),
  50. noise1_scale = w2nn.load_model(path.join(ART_MODEL_DIR, "noise1_scale2.0x_model.t7"), opt.force_cudnn),
  51. noise2_scale = w2nn.load_model(path.join(ART_MODEL_DIR, "noise2_scale2.0x_model.t7"), opt.force_cudnn),
  52. noise3_scale = w2nn.load_model(path.join(ART_MODEL_DIR, "noise3_scale2.0x_model.t7"), opt.force_cudnn),
  53. noise0 = w2nn.load_model(path.join(ART_MODEL_DIR, "noise0_model.t7"), opt.force_cudnn),
  54. noise1 = w2nn.load_model(path.join(ART_MODEL_DIR, "noise1_model.t7"), opt.force_cudnn),
  55. noise2 = w2nn.load_model(path.join(ART_MODEL_DIR, "noise2_model.t7"), opt.force_cudnn),
  56. noise3 = w2nn.load_model(path.join(ART_MODEL_DIR, "noise3_model.t7"), opt.force_cudnn)
  57. }
  58. local photo_model = {
  59. scale = w2nn.load_model(path.join(PHOTO_MODEL_DIR, "scale2.0x_model.t7"), opt.force_cudnn),
  60. noise0_scale = w2nn.load_model(path.join(PHOTO_MODEL_DIR, "noise0_scale2.0x_model.t7"), opt.force_cudnn),
  61. noise1_scale = w2nn.load_model(path.join(PHOTO_MODEL_DIR, "noise1_scale2.0x_model.t7"), opt.force_cudnn),
  62. noise2_scale = w2nn.load_model(path.join(PHOTO_MODEL_DIR, "noise2_scale2.0x_model.t7"), opt.force_cudnn),
  63. noise3_scale = w2nn.load_model(path.join(PHOTO_MODEL_DIR, "noise3_scale2.0x_model.t7"), opt.force_cudnn),
  64. noise0 = w2nn.load_model(path.join(PHOTO_MODEL_DIR, "noise0_model.t7"), opt.force_cudnn),
  65. noise1 = w2nn.load_model(path.join(PHOTO_MODEL_DIR, "noise1_model.t7"), opt.force_cudnn),
  66. noise2 = w2nn.load_model(path.join(PHOTO_MODEL_DIR, "noise2_model.t7"), opt.force_cudnn),
  67. noise3 = w2nn.load_model(path.join(PHOTO_MODEL_DIR, "noise3_model.t7"), opt.force_cudnn)
  68. }
  69. collectgarbage()
  70. local CLEANUP_MODEL = false -- if you are using the low memory GPU, you could use this flag.
  71. local CACHE_DIR = path.join(ROOT, "cache")
  72. local MAX_NOISE_IMAGE = opt.max_pixels
  73. local MAX_SCALE_IMAGE = (math.sqrt(opt.max_pixels) / 2)^2
  74. local CURL_OPTIONS = {
  75. request_timeout = opt.curl_request_timeout,
  76. connect_timeout = opt.curl_connect_timeout,
  77. allow_redirects = true,
  78. max_redirects = opt.curl_max_redirects
  79. }
  80. local CURL_MAX_SIZE = opt.max_body_size
  81. local function valid_size(x, scale, tta_level)
  82. if scale <= 0 then
  83. local limit = math.pow(math.floor(math.pow(MAX_NOISE_IMAGE / tta_level, 0.5)), 2)
  84. return x:size(2) * x:size(3) <= limit
  85. else
  86. local limit = math.pow(math.floor(math.pow(MAX_SCALE_IMAGE / tta_level, 0.5)), 2)
  87. return x:size(2) * x:size(3) <= limit
  88. end
  89. end
  90. local function auto_tta_level(x, scale)
  91. local limit2, limit4, limit8
  92. if scale <= 0 then
  93. limit2 = math.pow(math.floor(math.pow(MAX_NOISE_IMAGE / 2, 0.5)), 2)
  94. limit4 = math.pow(math.floor(math.pow(MAX_NOISE_IMAGE / 4, 0.5)), 2)
  95. limit8 = math.pow(math.floor(math.pow(MAX_NOISE_IMAGE / 8, 0.5)), 2)
  96. else
  97. limit2 = math.pow(math.floor(math.pow(MAX_SCALE_IMAGE / 2, 0.5)), 2)
  98. limit4 = math.pow(math.floor(math.pow(MAX_SCALE_IMAGE / 4, 0.5)), 2)
  99. limit8 = math.pow(math.floor(math.pow(MAX_SCALE_IMAGE / 8, 0.5)), 2)
  100. end
  101. local px = x:size(2) * x:size(3)
  102. if px <= limit8 then
  103. return 8
  104. elseif px <= limit4 then
  105. return 4
  106. elseif px <= limit2 then
  107. return 2
  108. else
  109. return 1
  110. end
  111. end
  112. local function cache_url(url)
  113. local hash = md5.sumhexa(url)
  114. local cache_file = path.join(CACHE_DIR, "url_" .. hash)
  115. if path.exists(cache_file) then
  116. return image_loader.load_float(cache_file)
  117. else
  118. local res = coroutine.yield(
  119. turbo.async.HTTPClient({verify_ca=false},
  120. nil,
  121. CURL_MAX_SIZE):fetch(url, CURL_OPTIONS)
  122. )
  123. if res.code == 200 then
  124. local content_type = res.headers:get("Content-Type", true)
  125. if type(content_type) == "table" then
  126. content_type = content_type[1]
  127. end
  128. if content_type and content_type:find("image") then
  129. local fp = io.open(cache_file, "wb")
  130. local blob = res.body
  131. fp:write(blob)
  132. fp:close()
  133. return image_loader.decode_float(blob)
  134. end
  135. end
  136. end
  137. return nil, nil
  138. end
  139. local function get_image(req)
  140. local file_info = req:get_arguments("file")
  141. local url = req:get_argument("url", "")
  142. local file = nil
  143. local filename = nil
  144. if file_info and #file_info == 1 then
  145. file = file_info[1][1]
  146. local disp = file_info[1]["content-disposition"]
  147. if disp and disp["filename"] then
  148. filename = path.basename(disp["filename"])
  149. end
  150. end
  151. if file and file:len() > 0 then
  152. local x, meta = image_loader.decode_float(file)
  153. return x, meta, filename
  154. elseif url and url:len() > 0 then
  155. local x, meta = cache_url(url)
  156. return x, meta, filename
  157. end
  158. return nil, nil, nil
  159. end
  160. local function cleanup_model(model)
  161. if CLEANUP_MODEL then
  162. model:clearState() -- release GPU memory
  163. end
  164. end
  165. local function convert(x, meta, options)
  166. local cache_file = path.join(CACHE_DIR, options.prefix .. ".png")
  167. local alpha_cache_file = path.join(CACHE_DIR, options.alpha_prefix .. ".png")
  168. local alpha = meta.alpha
  169. local alpha_orig = alpha
  170. if path.exists(alpha_cache_file) then
  171. alpha = image_loader.load_float(alpha_cache_file)
  172. if alpha:dim() == 2 then
  173. alpha = alpha:reshape(1, alpha:size(1), alpha:size(2))
  174. end
  175. if alpha:size(1) == 3 then
  176. alpha = image.rgb2y(alpha)
  177. end
  178. end
  179. if path.exists(cache_file) then
  180. x = image_loader.load_float(cache_file)
  181. meta = tablex.copy(meta)
  182. meta.alpha = alpha
  183. return x, meta
  184. else
  185. local model = nil
  186. if options.style == "art" then
  187. model = art_model
  188. elseif options.style == "photo" then
  189. model = photo_model
  190. end
  191. if options.border then
  192. x = alpha_util.make_border(x, alpha_orig, reconstruct.offset_size(model.scale))
  193. end
  194. if (options.method == "scale" or
  195. options.method == "noise0_scale" or
  196. options.method == "noise1_scale" or
  197. options.method == "noise2_scale" or
  198. options.method == "noise3_scale")
  199. then
  200. x = reconstruct.scale_tta(model[options.method], options.tta_level, 2.0, x,
  201. opt.crop_size, opt.batch_size)
  202. if alpha then
  203. if not (alpha:size(2) == x:size(2) and alpha:size(3) == x:size(3)) then
  204. alpha = reconstruct.scale(model.scale, 2.0, alpha,
  205. opt.crop_size, opt.batch_size)
  206. image_loader.save_png(alpha_cache_file, alpha)
  207. cleanup_model(model.scale)
  208. end
  209. end
  210. cleanup_model(model[options.method])
  211. elseif (options.method == "noise0" or
  212. options.method == "noise1" or
  213. options.method == "noise2" or
  214. options.method == "noise3")
  215. then
  216. x = reconstruct.image_tta(model[options.method], options.tta_level,
  217. x, opt.crop_size, opt.batch_size)
  218. cleanup_model(model[options.method])
  219. end
  220. image_loader.save_png(cache_file, x)
  221. meta = tablex.copy(meta)
  222. meta.alpha = alpha
  223. return x, meta
  224. end
  225. end
  226. local function client_disconnected(handler)
  227. return not(handler.request and
  228. handler.request.connection and
  229. handler.request.connection.stream and
  230. (not handler.request.connection.stream:closed()))
  231. end
  232. local function make_output_filename(filename, mode)
  233. local e = path.extension(filename)
  234. local base = filename:sub(0, filename:len() - e:len())
  235. if mode then
  236. return base .. "_waifu2x_" .. mode .. ".png"
  237. else
  238. return base .. ".png"
  239. end
  240. end
  241. local APIHandler = class("APIHandler", turbo.web.RequestHandler)
  242. function APIHandler:post()
  243. if client_disconnected(self) then
  244. self:set_status(400)
  245. self:write("client disconnected")
  246. return
  247. end
  248. local x, meta, filename = get_image(self)
  249. local scale = tonumber(self:get_argument("scale", "-1"))
  250. local noise = tonumber(self:get_argument("noise", "-1"))
  251. local tta_level = tonumber(self:get_argument("tta_level", "1"))
  252. local style = self:get_argument("style", "art")
  253. local download = (self:get_argument("download", "")):len()
  254. if tta_level == 0 then
  255. tta_level = auto_tta_level(x, scale)
  256. end
  257. if not (tta_level == 0 or tta_level == 1 or tta_level == 2 or tta_level == 4 or tta_level == 8) then
  258. tta_level = 1
  259. end
  260. if style ~= "art" then
  261. style = "photo" -- style must be art or photo
  262. end
  263. if x and valid_size(x, scale, tta_level) then
  264. local prefix = nil
  265. if (noise >= 0 or scale > 0) then
  266. local hash = md5.sumhexa(meta.blob)
  267. local alpha_prefix = style .. "_" .. hash .. "_alpha"
  268. local border = false
  269. if scale >= 0 and meta.alpha then
  270. border = true
  271. end
  272. if (scale == 1 or scale == 2) and (noise < 0) then
  273. prefix = style .. "_scale_tta_" .. tta_level .. "_"
  274. x, meta = convert(x, meta, {method = "scale",
  275. style = style,
  276. tta_level = tta_level,
  277. prefix = prefix .. hash,
  278. alpha_prefix = alpha_prefix,
  279. border = border})
  280. if scale == 1 then
  281. x = iproc.scale(x, x:size(3) * (1.6 / 2.0), x:size(2) * (1.6 / 2.0), "Sinc")
  282. end
  283. elseif (scale == 1 or scale == 2) and (noise == 0 or noise == 1 or noise == 2 or noise == 3) then
  284. prefix = style .. string.format("_noise%d_scale_tta_", noise) .. tta_level .. "_"
  285. x, meta = convert(x, meta, {method = string.format("noise%d_scale", noise),
  286. style = style,
  287. tta_level = tta_level,
  288. prefix = prefix .. hash,
  289. alpha_prefix = alpha_prefix,
  290. border = border})
  291. if scale == 1 then
  292. x = iproc.scale(x, x:size(3) * (1.6 / 2.0), x:size(2) * (1.6 / 2.0), "Sinc")
  293. end
  294. elseif (noise == 0 or noise == 1 or noise == 2 or noise == 3) then
  295. prefix = style .. string.format("_noise%d_tta_", noise) .. tta_level .. "_"
  296. x = convert(x, meta, {method = string.format("noise%d", noise),
  297. style = style,
  298. tta_level = tta_level,
  299. prefix = prefix .. hash,
  300. alpha_prefix = alpha_prefix,
  301. border = border})
  302. border = false
  303. end
  304. end
  305. local name = nil
  306. if filename then
  307. if prefix then
  308. name = make_output_filename(filename, prefix:sub(0, prefix:len()-1))
  309. else
  310. name = make_output_filename(filename, nil)
  311. end
  312. else
  313. name = uuid() .. ".png"
  314. end
  315. local blob = image_loader.encode_png(alpha_util.composite(x, meta.alpha),
  316. tablex.update({depth = 8, inplace = true}, meta))
  317. self:set_header("Content-Length", string.format("%d", #blob))
  318. if download > 0 then
  319. self:set_header("Content-Type", "application/octet-stream")
  320. self:set_header("Content-Disposition", string.format('attachment; filename="%s"', name))
  321. else
  322. self:set_header("Content-Type", "image/png")
  323. self:set_header("Content-Disposition", string.format('inline; filename="%s"', name))
  324. end
  325. self:write(blob)
  326. else
  327. if not x then
  328. self:set_status(400)
  329. self:write("ERROR: An error occurred. (unsupported image format/connection timeout/file is too large)")
  330. else
  331. self:set_status(400)
  332. self:write("ERROR: image size exceeds maximum allowable size.")
  333. end
  334. end
  335. collectgarbage()
  336. end
  337. local FormHandler = class("FormHandler", turbo.web.RequestHandler)
  338. local index_ja = file.read(path.join(ROOT, "assets", "index.ja.html"))
  339. local index_ru = file.read(path.join(ROOT, "assets", "index.ru.html"))
  340. local index_pt = file.read(path.join(ROOT, "assets", "index.pt.html"))
  341. local index_es = file.read(path.join(ROOT, "assets", "index.es.html"))
  342. local index_fr = file.read(path.join(ROOT, "assets", "index.fr.html"))
  343. local index_de = file.read(path.join(ROOT, "assets", "index.de.html"))
  344. local index_tr = file.read(path.join(ROOT, "assets", "index.tr.html"))
  345. local index_zh_cn = file.read(path.join(ROOT, "assets", "index.zh-CN.html"))
  346. local index_zh_tw = file.read(path.join(ROOT, "assets", "index.zh-TW.html"))
  347. local index_ko = file.read(path.join(ROOT, "assets", "index.ko.html"))
  348. local index_en = file.read(path.join(ROOT, "assets", "index.html"))
  349. function FormHandler:get()
  350. local lang = self.request.headers:get("Accept-Language")
  351. if lang then
  352. local langs = utils.split(lang, ",")
  353. for i = 1, #langs do
  354. langs[i] = utils.split(langs[i], ";")[1]
  355. end
  356. if langs[1] == "ja" then
  357. self:write(index_ja)
  358. elseif langs[1] == "ru" then
  359. self:write(index_ru)
  360. elseif langs[1] == "pt" or langs[1] == "pt-BR" then
  361. self:write(index_pt)
  362. elseif langs[1] == "es" or langs[1] == "es-ES" then
  363. self:write(index_es)
  364. elseif langs[1] == "fr" then
  365. self:write(index_fr)
  366. elseif langs[1] == "de" then
  367. self:write(index_de)
  368. elseif langs[1] == "tr" then
  369. self:write(index_tr)
  370. elseif langs[1] == "zh-CN" or langs[1] == "zh" then
  371. self:write(index_zh_cn)
  372. elseif langs[1] == "zh-TW" then
  373. self:write(index_zh_tw)
  374. elseif langs[1] == "ko" then
  375. self:write(index_ko)
  376. else
  377. self:write(index_en)
  378. end
  379. else
  380. self:write(index_en)
  381. end
  382. end
  383. turbo.log.categories = {
  384. ["success"] = true,
  385. ["notice"] = false,
  386. ["warning"] = true,
  387. ["error"] = true,
  388. ["debug"] = false,
  389. ["development"] = false
  390. }
  391. local app = turbo.web.Application:new(
  392. {
  393. {"^/$", FormHandler},
  394. {"^/api$", APIHandler},
  395. {"^/([%a%d%.%-_]+)$", turbo.web.StaticFileHandler, path.join(ROOT, "assets/")},
  396. }
  397. )
  398. app:listen(opt.port, "0.0.0.0", {max_body_size = CURL_MAX_SIZE})
  399. turbo.ioloop.instance():start()