web.lua 16 KB

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