예제 #1
0
def ws_message(message):
    print "Message recieved from client side and the content is ", message.content['text']
    # prefix, label = message['path'].strip('/').split('/')
    socketid = message.content['text']
    
    Group(socketid).add(message.reply_channel)
    log_to_terminal(socketid, {"info": "User added to the Channel Group"})
예제 #2
0
def callback(ch, method, properties, body):
    try:
        print(" [x] Received %r" % body)
        body = yaml.safe_load(body) # using yaml instead of json.loads since that unicodes the string in value

        result = ClassificationTorchModel.predict(body['image_path'], body['label'], body['output_dir'])

        ClassificationJob.objects.create(job_id=body['socketid'], input_label=body['label'], image=str(result['input_image']).replace(settings.BASE_DIR, '')[1:], predicted_label = result['pred_label'], gcam_image=str(result['classify_gcam']).replace(settings.BASE_DIR, '')[1:])

        # Close the database connection in order to make sure that MYSQL Timeout doesn't occur
        django.db.close_old_connections()

        result['input_image'] = str(result['input_image']).replace(settings.BASE_DIR, '')
        result['classify_gcam'] = str(result['classify_gcam']).replace(settings.BASE_DIR, '')
        result['classify_gcam_raw'] = str(result['classify_gcam_raw']).replace(settings.BASE_DIR, '')
        result['classify_gb'] = str(result['classify_gb']).replace(settings.BASE_DIR, '')
        result['classify_gb_gcam'] = str(result['classify_gb_gcam']).replace(settings.BASE_DIR, '')

        print result

        log_to_terminal("Hello", {"terminal": "Completed the Classification Task"})
        log_to_terminal(body['socketid'], {"terminal": json.dumps(result)})
        log_to_terminal(body['socketid'], {"result": json.dumps(result)})
        log_to_terminal(body['socketid'], {"terminal": "Completed the Classification Task"})

        ch.basic_ack(delivery_tag = method.delivery_tag)
    except Exception, err:
        log_to_terminal(body['socketid'], {"terminal": json.dumps({"Traceback": str(traceback.print_exc())})})
예제 #3
0
def ws_message(message):
    print("Message recieved from client side and the content is ",
          message.content['text'])
    # prefix, label = message['path'].strip('/').split('/')
    socketid = message.content['text']

    Group(socketid).add(message.reply_channel)
    log_to_terminal(socketid, {"info": "User added to the Channel Group"})
예제 #4
0
def fever(request, template_name="fever/fever.html"):
    socketid = uuid.uuid4()
    if request.method == "POST":
        try:
            # from IPython import embed; embed(); import os; os._exit(1)
            socketid = request.POST.get('socketid')
            claim_text = request.POST.get('src')
            log_to_terminal(socketid, {"terminal": "Starting FEVER job..."})
            response = grad_cam_fever(str(claim_text), socketid)
        except Exception, err:
            log_to_terminal(socketid, {"terminal": traceback.print_exc()})
예제 #5
0
def captioning_api(request):
    if request.method == "POST":
        try:
            image = request.FILES['image']
            caption = request.POST.get('caption', '')

            abs_image_path = os.path.join(settings.BASE_DIR, str(img_path[1:]))
            out_dir = os.path.dirname(abs_image_path)

            # Run the captioning wrapper
            log_to_terminal(socketid, {"terminal": "Starting Captioning job..."})
            response = grad_cam_captioning(str(abs_image_path), str(caption), str(out_dir+"/"), socketid)
        except Exception, err:
            log_to_terminal(socketid, {"terminal": traceback.print_exc()})
예제 #6
0
def callback(ch, method, properties, body):
    try:
        print(" [x] Received %r" % body)
        body = yaml.safe_load(body) # using yaml instead of json.loads since that unicodes the string in value

        result = VqaTorchModel.predict(body['image_path'], constants.VQA_CONFIG['input_sz'], constants.VQA_CONFIG['input_sz'], body['input_question'], body['input_answer'], body['output_dir'])

        VqaJob.objects.create(job_id=body['socketid'], question=body['input_question'], input_answer=body['input_answer'], image=str(result['input_image']).replace(settings.BASE_DIR, '')[1:], predicted_answer = result['answer'], gcam_image=str(result['vqa_gcam']).replace(settings.BASE_DIR, '')[1:])

        # Close the database connection in order to make sure that MYSQL Timeout doesn't occur
        django.db.close_old_connections()

        result['input_image'] = str(result['input_image']).replace(settings.BASE_DIR, '')
        result['vqa_gcam'] = str(result['vqa_gcam']).replace(settings.BASE_DIR, '')
        result['vqa_gcam_raw'] = str(result['vqa_gcam_raw']).replace(settings.BASE_DIR, '')
        result['vqa_gb'] = str(result['vqa_gb']).replace(settings.BASE_DIR, '')
        result['vqa_gb_gcam'] = str(result['vqa_gb_gcam']).replace(settings.BASE_DIR, '')

        log_to_terminal(body['socketid'], {"terminal": json.dumps(result)})
        log_to_terminal(body['socketid'], {"result": json.dumps(result)})
        log_to_terminal(body['socketid'], {"terminal": "Completed the Grad-CAM VQA task"})

        ch.basic_ack(delivery_tag = method.delivery_tag)
    except Exception, err:
        log_to_terminal(body['socketid'], {"terminal": json.dumps({"Traceback": str(traceback.print_exc())})})
예제 #7
0
def callback(ch, method, properties, body):
    try:
        print(" [x] Received %r" % body)
        body = yaml.safe_load(
            body
        )  # using yaml instead of json.loads since that unicodes the string in value

        result = ClassificationTorchModel.predict(body['image_path'],
                                                  body['label'],
                                                  body['output_dir'])

        ClassificationJob.objects.create(
            job_id=body['socketid'],
            input_label=body['label'],
            image=str(result['input_image']).replace(settings.BASE_DIR,
                                                     '')[1:],
            predicted_label=result['pred_label'],
            gcam_image=str(result['classify_gcam']).replace(
                settings.BASE_DIR, '')[1:])

        # Close the database connection in order to make sure that MYSQL Timeout doesn't occur
        django.db.close_old_connections()

        result['input_image'] = str(result['input_image']).replace(
            settings.BASE_DIR, '')
        result['classify_gcam'] = str(result['classify_gcam']).replace(
            settings.BASE_DIR, '')
        result['classify_gcam_raw'] = str(result['classify_gcam_raw']).replace(
            settings.BASE_DIR, '')
        result['classify_gb'] = str(result['classify_gb']).replace(
            settings.BASE_DIR, '')
        result['classify_gb_gcam'] = str(result['classify_gb_gcam']).replace(
            settings.BASE_DIR, '')

        print result

        log_to_terminal("Hello",
                        {"terminal": "Completed the Classification Task"})
        log_to_terminal(body['socketid'], {"terminal": json.dumps(result)})
        log_to_terminal(body['socketid'], {"result": json.dumps(result)})
        log_to_terminal(body['socketid'],
                        {"terminal": "Completed the Classification Task"})

        ch.basic_ack(delivery_tag=method.delivery_tag)
    except Exception, err:
        log_to_terminal(body['socketid'], {
            "terminal":
            json.dumps({"Traceback": str(traceback.print_exc())})
        })
예제 #8
0
def captioning_api(request):
    if request.method == "POST":
        try:
            image = request.FILES['image']
            caption = request.POST.get('caption', '')

            abs_image_path = os.path.join(settings.BASE_DIR, str(img_path[1:]))
            out_dir = os.path.dirname(abs_image_path)

            # Run the captioning wrapper
            log_to_terminal(socketid,
                            {"terminal": "Starting Captioning job..."})
            response = grad_cam_captioning(str(abs_image_path), str(caption),
                                           str(out_dir + "/"), socketid)
        except Exception, err:
            log_to_terminal(socketid, {"terminal": traceback.print_exc()})
예제 #9
0
def captioning(request, template_name="captioning/captioning.html"):
    socketid = uuid.uuid4()
    if request.method == "POST":
        try:
            img_path = request.POST.get('img_path')
            img_path = urllib2.unquote(img_path)
            caption = request.POST.get('caption', '')
            socketid = request.POST.get('socketid')

            abs_image_path = os.path.join(settings.BASE_DIR, str(img_path[1:]))
            out_dir = os.path.dirname(abs_image_path)

            # Run the captioning wrapper
            log_to_terminal(socketid, {"terminal": "Starting Captioning job..."})
            response = grad_cam_captioning(str(abs_image_path), str(caption), str(out_dir+"/"), socketid)
        except Exception, err:
            log_to_terminal(socketid, {"terminal": traceback.print_exc()})
예제 #10
0
def classification(request, template_name="classification/classification.html"):
    socketid = uuid.uuid4()
    if request.method == "POST":
        try:
            img_path = request.POST.get('img_path')
            img_path = urllib2.unquote(img_path)
            label = request.POST.get('label')
            socketid = request.POST.get('socketid')

            abs_image_path = os.path.join(settings.BASE_DIR, str(img_path[1:]))
            out_dir = os.path.dirname(abs_image_path)

            # Run the classification wrapper
            log_to_terminal(socketid, {"terminal": "Starting classification job on VGG_ILSVRC_16_layers.caffemodel"})
            response = grad_cam_classification(str(abs_image_path), int(label), str(out_dir+"/"), socketid)
        except Exception, err:
            log_to_terminal(socketid, {"terminal": traceback.print_exc()})
예제 #11
0
def callback(ch, method, properties, body):
    try:
        print(" [x] Received %r" % body)
        body = yaml.safe_load(
            body
        )  # using yaml instead of json.loads since that unicodes the string in value

        log_to_terminal(body['socketid'],
                        {"terminal": "Running... Please wait."})

        result = fever_predictor.predict(body['claim_text'])
        # from IPython import embed; embed(); import os; os._exit(1)
        FEVERJob.objects.create(
            job_id=body['socketid'],
            claim_text=body['claim_text'],
            docids=str(result['docids']),
            predicted_evidence=str(
                result['predicted_evidence'].encode('utf-8')),
            predicted_label=result['predicted_label'])

        # Close the database connection in order to make sure that MYSQL Timeout doesn't occur
        django.db.close_old_connections()

        result['predicted_evidence'] = result['predicted_evidence'].split(
            '@_@')
        result['docids'] = result['docids'].split('@_@')
        # assert len(result['predicted_evidence']) == len(result['docids'])
        result['docids'] = [
            'https://en.wikipedia.org/wiki/{}'.format(d)
            for d in result['docids']
        ]

        log_to_terminal(body['socketid'], {"result": json.dumps(result)})
        log_to_terminal(body['socketid'], {"terminal": json.dumps(result)})
        log_to_terminal(body['socketid'],
                        {"terminal": "Completed the FEVER job"})

        ch.basic_ack(delivery_tag=method.delivery_tag)

    except Exception, err:
        log_to_terminal(body['socketid'], {
            "terminal":
            json.dumps({"Traceback": str(traceback.print_exc())})
        })
예제 #12
0
def captioning(request, template_name="captioning/captioning.html"):
    socketid = uuid.uuid4()
    if request.method == "POST":
        try:
            img_path = request.POST.get('img_path')
            img_path = urllib2.unquote(img_path)
            caption = request.POST.get('caption', '')
            socketid = request.POST.get('socketid')

            abs_image_path = os.path.join(settings.BASE_DIR, str(img_path[1:]))
            out_dir = os.path.dirname(abs_image_path)

            # Run the captioning wrapper
            log_to_terminal(socketid,
                            {"terminal": "Starting Captioning job..."})
            response = grad_cam_captioning(str(abs_image_path), str(caption),
                                           str(out_dir + "/"), socketid)
        except Exception, err:
            log_to_terminal(socketid, {"terminal": traceback.print_exc()})
예제 #13
0
def vqa(request, template_name="vqa/vqa.html"):
    socketid = uuid.uuid4()
    if request.method == "POST":
        # get the parameters from client side
        try:
            socketid = request.POST.get('socketid')
            input_question = request.POST.get('question', '')
            input_answer = request.POST.get('answer', None)
            img_path = request.POST.get('img_path')
            img_path = urllib2.unquote(img_path)

            abs_image_path = settings.BASE_DIR + str(img_path)
            # abs_image_path = os.path.join(settings.BASE_DIR, str(img_path[1:]))
            out_dir = os.path.dirname(abs_image_path)
            # Run the VQA wrapper
            log_to_terminal(socketid, {"terminal": "Starting Visual Question Answering job..."})
            response = grad_cam_vqa(str(input_question), str(input_answer), str(abs_image_path), str(out_dir+"/"), socketid)
        except Exception, err:
            log_to_terminal(socketid, {"terminal": traceback.print_exc()})
예제 #14
0
def grad_cam_fever(claim_text, socketid):
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
    channel = connection.channel()

    channel.queue_declare('fever_task_queue', durable=True)
    message = {
        'claim_text': claim_text,
        'socketid': socketid,
    }
    # from IPython import embed; embed(); import os; os._exit(1)
    log_to_terminal(socketid, {"terminal": "Publishing job to FEVER Queue"})
    channel.basic_publish(exchange='',
                          routing_key='fever_task_queue',
                          body=json.dumps(message),
                          properties=pika.BasicProperties(
                            delivery_mode=2, # make message persistent
                          ))
    print(" [x] Sent %r" % message)
    log_to_terminal(socketid, {"terminal": "Job published successfully"})
    connection.close()
예제 #15
0
def callback(ch, method, properties, body):
    try:
        print(" [x] Received %r" % body)
        body = yaml.safe_load(
            body
        )  # using yaml instead of json.loads since that unicodes the string in value

        result = VqaTorchModel.predict(body['image_path'],
                                       constants.VQA_CONFIG['input_sz'],
                                       constants.VQA_CONFIG['input_sz'],
                                       body['input_question'],
                                       body['input_answer'],
                                       body['output_dir'])

        VqaJob.objects.create(
            job_id=body['socketid'],
            question=body['input_question'],
            input_answer=body['input_answer'],
            image=str(result['input_image']).replace(settings.BASE_DIR,
                                                     '')[1:],
            predicted_answer=result['answer'],
            gcam_image=str(result['vqa_gcam']).replace(settings.BASE_DIR,
                                                       '')[1:])

        # Close the database connection in order to make sure that MYSQL Timeout doesn't occur
        django.db.close_old_connections()

        result['input_image'] = str(result['input_image']).replace(
            settings.BASE_DIR, '')
        result['vqa_gcam'] = str(result['vqa_gcam']).replace(
            settings.BASE_DIR, '')
        result['vqa_gcam_raw'] = str(result['vqa_gcam_raw']).replace(
            settings.BASE_DIR, '')
        result['vqa_gb'] = str(result['vqa_gb']).replace(settings.BASE_DIR, '')
        result['vqa_gb_gcam'] = str(result['vqa_gb_gcam']).replace(
            settings.BASE_DIR, '')

        log_to_terminal(body['socketid'], {"terminal": json.dumps(result)})
        log_to_terminal(body['socketid'], {"result": json.dumps(result)})
        log_to_terminal(body['socketid'],
                        {"terminal": "Completed the Grad-CAM VQA task"})

        ch.basic_ack(delivery_tag=method.delivery_tag)
    except Exception, err:
        log_to_terminal(body['socketid'], {
            "terminal":
            json.dumps({"Traceback": str(traceback.print_exc())})
        })
예제 #16
0
def classification(request,
                   template_name="classification/classification.html"):
    socketid = uuid.uuid4()
    if request.method == "POST":
        try:
            img_path = request.POST.get('img_path')
            img_path = urllib2.unquote(img_path)
            label = request.POST.get('label')
            socketid = request.POST.get('socketid')

            abs_image_path = os.path.join(settings.BASE_DIR, str(img_path[1:]))
            out_dir = os.path.dirname(abs_image_path)

            # Run the classification wrapper
            log_to_terminal(
                socketid, {
                    "terminal":
                    "Starting classification job on VGG_ILSVRC_16_layers.caffemodel"
                })
            response = grad_cam_classification(str(abs_image_path), int(label),
                                               str(out_dir + "/"), socketid)
        except Exception, err:
            log_to_terminal(socketid, {"terminal": traceback.print_exc()})
예제 #17
0
def vqa(request, template_name="vqa/vqa.html"):
    socketid = uuid.uuid4()
    if request.method == "POST":
        # get the parameters from client side
        try:
            socketid = request.POST.get('socketid')
            input_question = request.POST.get('question', '')
            input_answer = request.POST.get('answer', None)
            img_path = request.POST.get('img_path')
            img_path = urllib2.unquote(img_path)

            abs_image_path = settings.BASE_DIR + str(img_path)
            # abs_image_path = os.path.join(settings.BASE_DIR, str(img_path[1:]))
            out_dir = os.path.dirname(abs_image_path)
            # Run the VQA wrapper
            log_to_terminal(
                socketid,
                {"terminal": "Starting Visual Question Answering job..."})
            response = grad_cam_vqa(str(input_question), str(input_answer),
                                    str(abs_image_path), str(out_dir + "/"),
                                    socketid)
        except Exception, err:
            log_to_terminal(socketid, {"terminal": traceback.print_exc()})
예제 #18
0
파일: sender.py 프로젝트: exgile/Grad-CAM-1
def grad_cam_captioning(image_path, caption, out_dir, socketid):
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
    channel = connection.channel()

    channel.queue_declare(queue='captioning_task_queue', durable=True)
    message = {
        'image_path': image_path,
        'caption': caption,
        'output_dir': out_dir,
        'socketid': socketid,
    }
    log_to_terminal(socketid, {"terminal": "Publishing job to Captioning Queue"})
    channel.basic_publish(exchange='',
                      routing_key='captioning_task_queue',
                      body=json.dumps(message),
                      properties=pika.BasicProperties(
                         delivery_mode = 2, # make message persistent
                      ))

    print(" [x] Sent %r" % message)
    log_to_terminal(socketid, {"terminal": "Job published successfully"})
    connection.close()
예제 #19
0
def callback(ch, method, properties, body):
    try:
        print(" [x] Received %r" % body)
        body = yaml.safe_load(
            body
        )  # using yaml instead of json.loads since that unicodes the string in value

        result = ClassificationTorchModel.predict(body['image_path'],
                                                  body['label'],
                                                  body['output_dir'])

        result['input_image'] = str(result['input_image']).replace(
            settings.BASE_DIR, '')
        result['classify_gcam'] = str(result['classify_gcam']).replace(
            settings.BASE_DIR, '')
        result['classify_gcam_raw'] = str(result['classify_gcam_raw']).replace(
            settings.BASE_DIR, '')
        result['classify_gb'] = str(result['classify_gb']).replace(
            settings.BASE_DIR, '')
        result['classify_gb_gcam'] = str(result['classify_gb_gcam']).replace(
            settings.BASE_DIR, '')

        print result

        log_to_terminal("Hello",
                        {"terminal": "Completed the Classification Task"})
        log_to_terminal(body['socketid'], {"terminal": json.dumps(result)})
        log_to_terminal(body['socketid'], {"result": json.dumps(result)})
        log_to_terminal(body['socketid'],
                        {"terminal": "Completed the Classification Task"})

        ch.basic_ack(delivery_tag=method.delivery_tag)
    except Exception, err:
        log_to_terminal(body['socketid'], {
            "terminal":
            json.dumps({"Traceback": str(traceback.print_exc())})
        })
예제 #20
0
def callback(ch, method, properties, body):
    try:
        print(" [x] Received %r" % body)
        body = yaml.safe_load(body) # using yaml instead of json.loads since that unicodes the string in value

        result = VqaTorchModel.predict(body['image_path'], constants.VQA_CONFIG['input_sz'], constants.VQA_CONFIG['input_sz'], body['input_question'], body['input_answer'], body['output_dir'])
        result['input_image'] = str(result['input_image']).replace(settings.BASE_DIR, '')
        result['vqa_gcam'] = str(result['vqa_gcam']).replace(settings.BASE_DIR, '')
        result['vqa_gcam_raw'] = str(result['vqa_gcam_raw']).replace(settings.BASE_DIR, '')
        result['vqa_gb'] = str(result['vqa_gb']).replace(settings.BASE_DIR, '')
        result['vqa_gb_gcam'] = str(result['vqa_gb_gcam']).replace(settings.BASE_DIR, '')

        log_to_terminal(body['socketid'], {"terminal": json.dumps(result)})
        log_to_terminal(body['socketid'], {"result": json.dumps(result)})
        log_to_terminal(body['socketid'], {"terminal": "Completed the Grad-CAM VQA task"})

        ch.basic_ack(delivery_tag = method.delivery_tag)
    except Exception, err:
        log_to_terminal(body['socketid'], {"terminal": json.dumps({"Traceback": str(traceback.print_exc())})})