defforward(self, x): x = F.relu(F.max_pool2d(self.conv1(x), 2)) x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) x = x.view(-1, 320) x = F.relu(self.fc1(x)) x = F.dropout(x, training=self.training) x = self.fc2(x) return F.log_softmax(x, dim=1)
import torch as th from celery import Celery from mnist_model import ToyNet import json import cv2 import sys
app = Celery('tasks', backend='amqp', broker='amqp://') model = ToyNet() static_dict = th.load('./model/toynet.pth.tar') model.load_state_dict(static_dict) print('hope this is done only once')
@app.task definference(json_str): task_spec = json.loads(json_str) try: img_path = task_spec['img_path'] is_cuda = task_spec['is_cuda'] is_file = task_spec['is_file'] except KeyError as err: print('Key not found in json file.') print(err) res = dict(massage='Read Image Error') json_res = json.dumps(res) return json_res except: print('Json load error.') res = dict(massage='Read Image Error') json_res = json.dumps(res) return json_res if is_file: try: img = cv2.imread(img_path, 0)[:, :, None] except: print(sys.exc_info()[0]) res = dict(massage='Read Image Error') json_res = json.dumps(res) return json_res
if __name__ == '__main__': task_t = time.time() spec = dict( img_path='./test.jpg', is_cuda=False, is_file=True ) json_str = json.dumps(spec) res_list = [] for i in range(10): res = th_task.inference.delay(json_str) res_list.append(res) print('-----------') print(time.time()-task_t) print('-' * 10) for k, t in enumerate(res_list): x = t.wait() print(time.time()-task_t)