aicrowd_helper.py 6.05 KB
Newer Older
pfrl_rainbow's avatar
pfrl_rainbow committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
#!/usr/bin/env python
import crowdai_api
import os
import logging

########################################################################
# Instatiate Event Notifier
########################################################################
crowdai_events = crowdai_api.events.CrowdAIEvents()
current_phase = None
training_progress = 0.0

def inference_start():
    ########################################################################
    # Register Inference Start event
    ########################################################################
    logging.info("Inference Start...")
    global current_phase
    current_phase = "inference"
    crowdai_events.register_event(
                event_type=crowdai_events.CROWDAI_EVENT_INFO,
                message="inference_started",
                payload={ #Arbitrary Payload
                    "event_type": "minerl_challenge:inference_started"
                    }
                )

def inference_end():
    ########################################################################
    # Register Inference End event
    ########################################################################
    logging.info("Inference End...")
    global current_phase
    current_phase = None
    crowdai_events.register_event(
                event_type=crowdai_events.CROWDAI_EVENT_INFO,
                message="inference_ended",
                payload={ #Arbitrary Payload
                    "event_type": "minerl_challenge:inference_ended"
                    }
                )

def inference_error():
    ########################################################################
    # Register Inference Error event
    ########################################################################
    logging.error("Inference Failed...")
    crowdai_events.register_event(
                event_type=crowdai_events.CROWDAI_EVENT_INFO,
                message="inference_error",
                payload={ #Arbitrary Payload
                    "event_type": "minerl_challenge:inference_error"
                    }
                )

def training_start():
    ########################################################################
    # Register Training Start event
    ########################################################################
    logging.info("Training Start...")
    global current_phase
    current_phase = "training"
    crowdai_events.register_event(
                event_type=crowdai_events.CROWDAI_EVENT_INFO,
                message="training_started",
                payload={ #Arbitrary Payload
                    "event_type": "minerl_challenge:training_started"
                    }
                )

def training_end():
    ########################################################################
    # Register Training End event
    ########################################################################
    logging.info("Training End...")
    register_progress(1.0)
    global current_phase
    current_phase = None
    crowdai_events.register_event(
                event_type=crowdai_events.CROWDAI_EVENT_INFO,
                message="training_ended",
                payload={ #Arbitrary Payload
                    "event_type": "minerl_challenge:training_ended"
                    }
                )

def training_error():
    ########################################################################
    # Register Training Error event
    ########################################################################
    logging.error("Training Failed...")
    crowdai_events.register_event(
                event_type=crowdai_events.CROWDAI_EVENT_INFO,
                message="training_error",
                payload={ #Arbitrary Payload
                    "event_type": "minerl_challenge:training_error"
                    }
                )


def register_progress(progress):
    ########################################################################
    # Register Evaluation Progress event
    # progress : float [0, 1]
    ########################################################################
    logging.info("Progress : {}".format(progress))
    global training_progress, current_phase
    if current_phase is None:
        raise Exception('Please register current phase by calling `training_start` \
                         or `inference_start` before sending progress.')
    if current_phase == "training":
        if progress < training_progress:
            logging.warn('Invalid progress update to %f while you are already \
                          at %f. Skipping it...', progress, training_progress)
            return
        training_progress = progress

    crowdai_events.register_event(
                event_type=crowdai_events.CROWDAI_EVENT_INFO,
                message="register_progress",
                payload={ #Arbitrary Payload
                    "event_type": "minerl_challenge:register_progress",
                    "training_progress" : training_progress
                    }
                )

def submit(payload={}):
    ########################################################################
    # Register Evaluation Complete event
    ########################################################################
    logging.info("AIcrowd Submit")
    crowdai_events.register_event(
                event_type=crowdai_events.CROWDAI_EVENT_SUCCESS,
                message="submit",
                payload={ #Arbitrary Payload
                    "event_type": "minerl_challenge:submit",
                    },
                blocking=True
                )

def execution_error(error):
    ########################################################################
    # Register Evaluation Complete event
    ########################################################################
    crowdai_events.register_event(
                event_type=crowdai_events.CROWDAI_EVENT_ERROR,
                message="execution_error",
                payload={ #Arbitrary Payload
                    "event_type": "minerl_challenge:execution_error",
                    "error" : error
                    },
                blocking=True
                )