web.lua 17 KB

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