Пример #1
0
    def __init__(
        self,
        heads: List[str],
        lambs: Dict[str, float],
        use_uncollapsed: bool,
        half_T_side_dense: int,
        half_T_side_sparse_min: int,
        half_T_side_sparse_max: int,
        output_files: Optional[utils.OutputFiles] = None,
        do_render: Optional[bool] = None,
        render_limit: int = 1,
    ):
        super(IIDLoss, self).__init__(heads=heads)
        for head in heads:
            if head not in lambs:
                lambs[head] = 1.0

        if do_render is None:
            do_render = False
        if do_render:
            assert output_files is not None

        self._lambs = lambs
        self._use_uncollapsed = use_uncollapsed
        self._hts_dense = half_T_side_dense
        self._hts_sparse_min = half_T_side_sparse_min
        self._hts_sparse_max = half_T_side_sparse_max
        self._output_files = output_files
        self._do_render = do_render
        self._counter = utils.Counter(render_limit)
Пример #2
0
 def __init__(self):
     self.s = requests.session()
     self.poll_s = requests.session()
     self.params = {
         "daid": "164",
         "appid": "1003903",
         "js_ver": "10052",
         "js_type": "0",
         "enable_qlogin": "******",
         "aid": "1003903",
         "h": "1",
         "webqq_type": "10",
         "remember_uin": "1",
         "login2qq": "1",
         "s_url": self.WEBQQ_URL,
         "u1": self.WEBQQ_URL + "/loginproxy.html",
         "ptredirect": "0",
         "ptlang": "2052",
         "from_ui": "1",
         "pttype": "1",
         "dumy": "",
         "fp": "loginerroralert",
         "t": "1",
         "g": "1",
         "action": "0-0-4400",
         "mibao_css": "m_webqq",
         "clientid": str(random.randint(1, 10000000))
     }
     self.msg_id = utils.Counter()
     self.msg_handler = MessageHandler()
     self.groups = {}
     pass
Пример #3
0
 def __init__(self, conn):
     threading.Thread.__init__(self, name="ConnectionCheckAlive")
     self.daemon = True
     self._connection = conn
     self._counter = utils.Counter()
     self._connection_keepalive_send = False
     self._semaphore = threading.Condition()
Пример #4
0
    def train(self, training_data, training_labels):
        """
        The training loop for the perceptron passes through the training data several
        times and updates the weight vector for each label based on classification errors.
        See the project description for details.

        Use the provided self.weights[label] data structure so that
        the classify method works correctly. Also, recall that a
        datum is a counter from features to values for those features
        (and thus represents a vector a values).
        """

        errors = []
        for iteration in range(self.max_iterations):
            print('Starting iteration ', iteration)
            err = 0
            for i in range(len(training_data)):
                vectors = utils.Counter()
                for l in self.legal_labels:
                    vectors[l] = self.weights[l] * training_data[i]
                    # print('vector', vectors[l])
                guess = vectors.argMax()

                actual = training_labels[i]
                if guess != actual:
                    self.weights[
                        guess] = self.weights[guess] - training_data[i]
                    self.weights[
                        actual] = self.weights[actual] + training_data[i]
                    err += 1
            errors.append(err)
        # utils.raiseNotDefined()
        return errors
Пример #5
0
 def __init__(self, legal_labels, max_iterations):
     self.legal_labels = legal_labels
     self.type = "perceptron"
     self.max_iterations = max_iterations
     self.weights = {}
     for label in legal_labels:
         self.weights[label] = utils.Counter(
         )  # this is the data-structure you should use
def fit_pq(feats_base_init, labels_base_init, item_ix_base_init, num_channels, spatial_feat_dim, num_codebooks,
           codebook_size, batch_size=128, counter=utils.Counter()):
    """
    Fit the PQ model and then quantize and store the latent codes of the data used to train the PQ in a dictionary to 
    be used later as a replay buffer.
    :param feats_base_init: numpy array of base init features that will be used to train the PQ
    :param labels_base_init: numpy array of the base init labels used to train the PQ
    :param item_ix_base_init: numpy array of the item_ixs used to train the PQ
    :param num_channels: number of channels in desired features
    :param spatial_feat_dim: spatial dimension of desired features
    :param num_codebooks: number of codebooks for PQ
    :param codebook_size: size of each codebook for PQ
    :param batch_size: batch size used to extract PQ features
    :param counter: object to count how many latent codes are in the replay buffer/dict
    :return: (trained PQ object, dictionary of latent codes, list of item_ixs for latent codes, dict of visited classes
     and associated item_ixs)
    """

    train_data_base_init = np.transpose(feats_base_init, (0, 2, 3, 1))
    train_data_base_init = np.reshape(train_data_base_init, (-1, num_channels))
    num_samples = len(train_data_base_init)

    print('\nTraining Product Quantizer')
    start = time.time()
    nbits = int(np.log2(codebook_size))
    pq = faiss.ProductQuantizer(num_channels, num_codebooks, nbits)
    pq.train(train_data_base_init)
    print("Completed in {} secs".format(time.time() - start))
    del train_data_base_init

    print('\nEncoding and Storing Base Init Codes')
    start_time = time.time()
    latent_dict = {}
    class_id_to_item_ix_dict = defaultdict(list)
    rehearsal_ixs = []
    mb = min(batch_size, num_samples)
    for i in range(0, num_samples, mb):
        start = i
        end = min(start + mb, num_samples)
        data_batch = feats_base_init[start:end]
        batch_labels = labels_base_init[start:end]
        batch_item_ixs = item_ix_base_init[start:end]

        data_batch = np.transpose(data_batch, (0, 2, 3, 1))
        data_batch = np.reshape(data_batch, (-1, num_channels))
        codes = pq.compute_codes(data_batch)
        codes = np.reshape(codes, (-1, spatial_feat_dim, spatial_feat_dim, num_codebooks))

        # put codes and labels into buffer (dictionary)
        for j in range(len(batch_labels)):
            ix = int(batch_item_ixs[j])
            latent_dict[ix] = [codes[j], batch_labels[j]]
            rehearsal_ixs.append(ix)
            class_id_to_item_ix_dict[int(batch_labels[j])].append(ix)
            counter.update()

    print("Completed in {} secs".format(time.time() - start_time))
    return pq, latent_dict, rehearsal_ixs, class_id_to_item_ix_dict
Пример #7
0
def messageObjectToKind(view, messageObject, messageText=None):
    """
    This method converts a email message string to
    a Chandler C{Mail.MailMessage} object

    @param messageObject: A C{email.Message} object representation of a mail message
    @type messageObject: C{email.Message}
    @return: C{Mail.MailMessage}
    """

    assert isinstance(messageObject, Message.Message), \
           "messageObject must be a Python email.Message.Message instance"

    assert len(messageObject.keys()) > 0, \
           "messageObject data is not a valid RFC2882 message"

    assert messageText is None or isinstance(messageText, str), \
           "messageText can either be a string or None"

    m = Mail.MailMessage(view=view)

    """Save the original message text in a text blob"""
    if messageText is None:
        messageText = messageObject.as_string()

    m.rfc2822Message = utils.dataToBinary(m, "rfc2822Message", messageText, \
                                          'message/rfc822', 'bz2')

    counter = utils.Counter()
    bodyBuffer = []
    buf = None

    if verbose():
        if messageObject.has_key("Message-ID"):
            messageId = messageObject["Message-ID"]
        else:
            messageId = "<Unknown Message>"

        buf = ["Message: %s\n-------------------------------" % messageId]

    __parsePart(view, messageObject, m, bodyBuffer, counter, buf)

    """If the message has attachments set hasMimeParts to True"""
    if len(m.mimeParts) > 0:
        m.hasMimeParts = True

    body = (constants.LF.join(bodyBuffer)).replace(constants.CR, constants.EMPTY)

    m.body = utils.unicodeToText(m, "body", body, indexText=False)

    __parseHeaders(view, messageObject, m)

    if verbose():
        logging.warn("\n\n%s\n\n" % '\n'.join(buf))

    return m
Пример #8
0
class Unit(dict):
    """Class used for all Game Elements (With the exception of heroes, who subclass it).
       It's implemented following a Entity-Component Model."""

    # Create a counter for each type of game element.  Used to assign IDs.
    counters = defaultdict(lambda: utils.Counter())

    def __init__(self, **kwargs):
        super(Unit, self).__init__(**kwargs)
        type = kwargs['type']  # Grab type from kwargs, for cleanliness
        initial_data = INITIAL_DATA[type]  # Get the Unit's INITIAL_DATA

        self.update(initial_data)  # Set the initial data for the unit
        self.update(
            kwargs)  # Override any data that was in different in the save.
        if 'id' not in self:  # Define an ID using the next number in the counter if not defined.
            self['id'] = "%s-%s" % (type, self.counters[type].next())

    def __repr__(self):
        """Represent the unit clearly in debug messages"""
        return "<" + self['id'] + ">"

    def __eq__(self, other):
        """Units are equal if their IDs are equal regardless of their other keys"""
        return self['id'] == other['id']

    @property
    def abilities(self):
        """Helper method to get the actual ability object for every unit's abilities"""
        return [getattr(abilities, ability) for ability in self['abilities']]

    def get_action(self, state):
        """For AI controlled actors, find an action by looking through each
           ability and seeing if it suggests and action at this time."""
        for ability in self.abilities:
            action = ability.get_action(self, state)
            if action:
                return action
        return None

    def threatened_cells(self, state):
        """Helper method for determining all cells from which this Unit can attack opponents.
           This is particularly helpful for the Move action which uses this to find a cells to move towards."""
        results = set()
        for ability in self.abilities:
            if hasattr(ability, "threatened_cells"):
                results |= ability.threatened_cells(self, state)
        return results

    def targets(self, state):
        """Helper method for determining all cells this Unit can Attack at it's next turn."""
        results = set()
        for ability in self.abilities:
            if hasattr(ability, "targets"):
                results |= ability.targets(self, state)
        return results
Пример #9
0
 def __init__(self, env, training_iterations, testing_iterations, epsilon,
              gamma, alpha):
     self.env = env
     self.training = training_iterations
     self.testing = testing_iterations
     self.epsilon = epsilon
     self.gamma = gamma
     self.alpha = alpha
     self.QValues = utils.Counter()
     self.actions = [i for i in range(self.env.action_space.n)]
Пример #10
0
 def basic_feature2_extractor(self, datum):
     """
     Returns a set of pixel features indicating whether
     each pixel in the provided datum is white (0) or gray/black (1)
     """
     features = utils.Counter()
     for x in range(datum.width):
         for y in range(datum.height):
             features[(x, y)] = datum.get_pixel(x, y)
     return features
Пример #11
0
    def register_process(self, req, msgid, message, data):
        # When a new process connects they need to acquire new process id
        p_id = "P%s" % (guid.generate(), )
        processes[p_id] = req
        req.type = "CLIENT"
        req.name = p_id

        pthreads[p_id] = []
        pthread_count[p_id] = utils.Counter()

        req.send(msgid, ("RESULT_STRING", p_id))
Пример #12
0
    def __init__(self, validation_config):
        self._nondet_var_map = None
        self.machine_model = validation_config.machine_model
        self.config = validation_config
        self.witness_creator = wit_gen.WitnessCreator()
        self.harness_creator = harness_gen.HarnessCreator()

        self.naive_verification = validation_config.naive_verification

        # If a void appears in a line, there must be something between
        # the void and the __VERIFIER_error() symbol - otherwise
        # it is a function definition/declaration.
        self.error_method_pattern = re.compile(
            '((?!void).)*(void.*\S.*)?__VERIFIER_error\(\) *;.*')

        self.statistics = utils.Statistics('Test Validator ' + self.get_name())
        self.timer_validation = utils.Stopwatch()
        self.statistics.add_value('Time for validation', self.timer_validation)
        self.timer_witness_validation = utils.Stopwatch()
        self.statistics.add_value('Time for witness validation',
                                  self.timer_witness_validation)
        self.counter_size_witnesses = utils.Counter()
        self.statistics.add_value('Total size of witnesses',
                                  self.counter_size_witnesses)
        self.timer_execution_validation = utils.Stopwatch()
        self.statistics.add_value('Time for execution validation',
                                  self.timer_execution_validation)
        self.counter_size_harnesses = utils.Counter()
        self.statistics.add_value('Total size of harnesses',
                                  self.counter_size_harnesses)

        self.timer_vector_gen = utils.Stopwatch()
        self.statistics.add_value("Time for test vector generation",
                                  self.timer_vector_gen)
        self.counter_handled_test_cases = utils.Counter()
        self.statistics.add_value('Number of looked-at test cases',
                                  self.counter_handled_test_cases)

        self.final_test_vector_size = utils.Constant()
        self.statistics.add_value("Size of successful test vector",
                                  self.final_test_vector_size)
Пример #13
0
 def predict(self, image):
     img_a = utils.Counter()
     img_a = image
     self.distance = list(map(lambda x: (self.face_data.face_train_labels[x[0]], img_a.cosine_distance(x[1])),
                              enumerate(self.trainingData)))
     # sort the list of tuples by distances in increasing order
     sorted_dist = (sorted(self.distance, key=lambda x: x[1]))
     k_neighbors = sorted_dist[:self.k]
     # select k labels
     klabels = [label for (label, _) in k_neighbors]
     # find the mode of the list
     return mode(klabels)
Пример #14
0
 def basic_feature_extractor_digit(self, datum):
     """
     Returns a set of pixel features indicating whether
     each pixel in the provided datum is white (0) or gray/black (1)
     """
     features = utils.Counter()
     for x in range(self.DIGIT_DATUM_WIDTH):
         for y in range(self.DIGIT_DATUM_HEIGHT):
             if datum.get_pixel(x, y) > 0:
                 features[(x, y)] = 1
             else:
                 features[(x, y)] = 0
     return features
Пример #15
0
    def classify(self, data):
        """
        Classifies each datum as the label that most closely matches the prototype vector
        for that label.  See the project description for details.

        Recall that a datum is a util.counter...
        """
        guesses = []
        for datum in data:
            vectors = utils.Counter()
            for l in self.legal_labels:
                vectors[l] = self.weights[l] * datum
            guesses.append(vectors.argMax())
        return guesses
    def __init__(
        self,
        image_info: utils.ImageInfo,
        output_files: Optional[utils.OutputFiles] = None,
        do_render: bool = False,
        render_limit: int = 1,
    ):
        if do_render:
            assert output_files is not None

        self._image_info = image_info
        self._output = output_files
        self._render = do_render
        self._counter = utils.Counter(render_limit)
Пример #17
0
    def getDistribution(self, state):
        # Read variables from state
        ghostState = state.getGhostState(self.index)
        legalActions = state.getLegalActions(self.index)
        pos = state.getGhostPosition(self.index)
        isScared = ghostState.scaredTimer > 0

        speed = 1
        if isScared: speed = 0.5

        actionVectors = [
            Actions.directionToVector(a, speed) for a in legalActions
        ]
        newPositions = [(pos[0] + a[0], pos[1] + a[1]) for a in actionVectors]
        pacmanPosition = state.getPacmanPosition()

        # Select best actions given the state
        distancesToPacman = [
            manhattanDistance(pos, pacmanPosition) for pos in newPositions
        ]
        if isScared:
            bestScore = max(distancesToPacman)
            bestProb = self.prob_scaredFlee
        else:
            bestScore = min(distancesToPacman)
            bestProb = self.prob_attack
        bestActions = [
            action for action, distance in zip(legalActions, distancesToPacman)
            if distance == bestScore
        ]

        # Construct distribution
        dist = utils.Counter()
        for a in bestActions:
            dist[a] = bestProb / len(bestActions)
        for a in legalActions:
            dist[a] += (1 - bestProb) / len(legalActions)
        dist.normalize()
        return dist
Пример #18
0
    def __init__(self, mdp, testing_iterations, discount=0.99, iterations=100):
        self.mdp = mdp
        self.testing_iterations = testing_iterations
        self.discount = discount
        self.iterations = iterations
        self.values = util.Counter()  # A Counter is a dict with default 0

        cache = {}  # caches values corresponding to a (state, iteration)

        # calculates value of a state at an iteration level
        def valueIteration(state, iteration=0):

            try:
                return cache[state, iteration]

            except:  # value has not already been computed
                actions = self.mdp.getActions()
                if actions == [] or self.mdp.isTerminal(state) \
                    or iteration >= self.iterations:
                    return 0

                maxValue = None
                for action in actions:
                    summation = 0
                    for nextState, prob in self.mdp.getTransitionStatesAndProbs(
                            state, action):
                        summation += prob * (self.mdp.getReward(state, action, nextState) + \
                            self.discount * valueIteration(nextState, iteration + 1))
                    if maxValue < summation:
                        maxValue = summation

                cache[state, iteration] = maxValue
                return maxValue

        for state in self.mdp.getStates():
            self.values[state] = valueIteration(state)
Пример #19
0
 def close(self):
     bsendclose = False
     try:
         self._semaphore.acquire()
         try:
             if not self._close:
                 self._close = True
                 bsendclose = True
                 self._on_data = None
                 self._on_close = None
                 self._on_except = None
                 #print "session send stream close."
         finally:
             self._semaphore.release()
         if bsendclose:
             self._send_ws_close()
             #Attende lo shutdown
             cnt = utils.Counter()
             while not self.is_shutdown():
                 time.sleep(0.2)
                 if cnt.is_elapsed(10):
                     break
     except:
         None
Пример #20
0
from collections import namedtuple
import yaml

import ids
import utils

InstInfo = namedtuple("InstInfo", [
    "name",
    "opcode",
    "operandCount",
    "pushCount",
    "popCount",
    "isTerminator",
])

_c = utils.Counter()
instInfoByName = {}
instInfoByCode = []

with utils.openCommonFile("opcodes.yaml") as _opcodesFile:
    for _opc in yaml.load(_opcodesFile.read()):
        _info = InstInfo(_opc["name"], _c(), _opc["iops"], _opc["push"],
                         _opc["pop"], _opc["term"])
        instInfoByName[_opc["name"]] = _info
        instInfoByCode.append(_info)

# Instructions and types may have one of the widths below. This number can be added to a base
# instruction like "addi8" to get the appropriate variant.
W8 = 0
W16 = 1
W32 = 2
Пример #21
0
def main():
    # ==========
    # Load pre-trained model
    # ==========
    opts_dict = {
        'radius': 3, 
        'stdf': {
            'in_nc': 1, 
            'out_nc': 64, 
            'nf': 32, 
            'nb': 3, 
            'base_ks': 3, 
            'deform_ks': 3, 
            },
        'qenet': {
            'in_nc': 64, 
            'out_nc': 1, 
            'nf': 48, 
            'nb': 8, 
            'base_ks': 3, 
            },
        }   
    model = MFVQE(opts_dict=opts_dict)
    msg = f'loading model {ckp_path}...'
    print(msg)
    checkpoint = torch.load(ckp_path)
    if 'module.' in list(checkpoint['state_dict'].keys())[0]:  # multi-gpu training
        new_state_dict = OrderedDict()
        for k, v in checkpoint['state_dict'].items():
            name = k[7:]  # remove module
            new_state_dict[name] = v
        model.load_state_dict(new_state_dict)
    else:  # single-gpu training
        model.load_state_dict(checkpoint['state_dict'])

    msg = f'> model {ckp_path} loaded.'
    print(msg)
    model = model.cuda()
    model.eval()

    # ==========
    # Load entire video
    # ==========
    msg = f'loading raw and low-quality yuv...'
    print(msg)
    raw_y = utils.import_yuv(
        seq_path=raw_yuv_path, h=h, w=w, tot_frm=nfs, start_frm=0, only_y=True
        )
    lq_y, lq_u, lq_v = utils.import_yuv(
        seq_path=lq_yuv_path, h=h, w=w, tot_frm=nfs, start_frm=0, only_y=False
        )
    raw_y = raw_y.astype(np.float32) / 255.
    lq_y = lq_y.astype(np.float32) / 255.
    msg = '> yuv loaded.'
    print(msg)

    # ==========
    # Define criterion
    # ==========
    criterion = utils.PSNR()
    unit = 'dB'

    # ==========
    # Test
    # ==========
    pbar = tqdm(total=nfs, ncols=80)
    ori_psnr_counter = utils.Counter()
    enh_psnr_counter = utils.Counter()
    for idx in range(nfs):
        # load lq
        idx_list = list(range(idx-3,idx+4))
        idx_list = np.clip(idx_list, 0, nfs-1)
        input_data = []
        for idx_ in idx_list:
            input_data.append(lq_y[idx_])
        input_data = torch.from_numpy(np.array(input_data))
        input_data = torch.unsqueeze(input_data, 0).cuda()

        # enhance
        enhanced_frm = model(input_data)

        # eval
        gt_frm = torch.from_numpy(raw_y[idx]).cuda()
        batch_ori = criterion(input_data[0, 3, ...], gt_frm)
        batch_perf = criterion(enhanced_frm[0, 0, ...], gt_frm)
        ori_psnr_counter.accum(volume=batch_ori)
        enh_psnr_counter.accum(volume=batch_perf)

        # display
        pbar.set_description(
            "[{:.3f}] {:s} -> [{:.3f}] {:s}"
            .format(batch_ori, unit, batch_perf, unit)
            )
        pbar.update()
    
    pbar.close()
    ori_ = ori_psnr_counter.get_ave()
    enh_ = enh_psnr_counter.get_ave()
    print('ave ori [{:.3f}] {:s}, enh [{:.3f}] {:s}, delta [{:.3f}] {:s}'.format(
        ori_, unit, enh_, unit, (enh_ - ori_) , unit
        ))
    print('> done.')
Пример #22
0
def main():
    # get arguments
    args = arguments.args()

    # build environment
    env = gym.make(args.env_name)
    num_observations = env.observation_space.shape[0]
    num_actions = env.action_space.shape[0]

    # define the global network...
    critic_shared_model = models.Critic_Network(num_observations)
    critic_shared_model.share_memory()

    actor_shared_model = models.Actor_Network(num_observations, num_actions)
    actor_shared_model.share_memory()

    # define the traffic signal...
    traffic_signal = utils.TrafficLight()
    # define the counter
    critic_counter = utils.Counter()
    actor_counter = utils.Counter()
    # define the shared gradient buffer...
    critic_shared_grad_buffer = utils.Shared_grad_buffers(critic_shared_model)
    actor_shared_grad_buffer = utils.Shared_grad_buffers(actor_shared_model)
    # define shared observation state...
    shared_obs_state = utils.Running_mean_filter(num_observations)
    # define shared reward...
    shared_reward = utils.RewardCounter()
    # define the optimizer...
    critic_optimizer = torch.optim.Adam(critic_shared_model.parameters(),
                                        lr=args.value_lr)
    actor_optimizer = torch.optim.Adam(actor_shared_model.parameters(),
                                       lr=args.policy_lr)

    # prepare multiprocessing
    # find how many are available
    total_works = mp.cpu_count() - 1
    print(f'.....total available process  is {total_works}')
    num_of_workers = total_works
    print(f'.....we set num_of_processes to {num_of_workers}')

    processors = []
    workers = []

    # load model from check point
    pass
    #

    p = mp.Process(target=chief_worker,
                   args=(num_of_workers, traffic_signal, critic_counter,
                         actor_counter, critic_shared_model,
                         actor_shared_model, critic_shared_grad_buffer,
                         actor_shared_grad_buffer, critic_optimizer,
                         actor_optimizer, shared_reward, shared_obs_state,
                         args.policy_update_step, args.env_name))

    processors.append(p)

    for idx in range(num_of_workers):
        workers.append(dppo_workers(args))

    for worker in workers:
        p = mp.Process(target=worker.train_network,
                       args=(traffic_signal, critic_counter, actor_counter,
                             critic_shared_model, actor_shared_model,
                             shared_obs_state, critic_shared_grad_buffer,
                             actor_shared_grad_buffer, shared_reward))
        processors.append(p)

    for p in processors:
        p.start()

    for p in processors:
        p.join()
Пример #23
0
def main():
    # ==========
    # parameters
    # ==========

    opts_dict = receive_arg()
    rank = opts_dict['train']['rank']
    unit = opts_dict['train']['criterion']['unit']
    num_iter = int(opts_dict['train']['num_iter'])
    interval_print = int(opts_dict['train']['interval_print'])
    interval_val = int(opts_dict['train']['interval_val'])
    
    # ==========
    # init distributed training
    # ==========

    if opts_dict['train']['is_dist']:
        utils.init_dist(
            local_rank=rank, 
            backend='nccl'
            )

    # TO-DO: load resume states if exists
    pass

    # ==========
    # create logger
    # ==========

    if rank == 0:
        log_dir = op.join("exp", opts_dict['train']['exp_name'])
        utils.mkdir(log_dir)
        log_fp = open(opts_dict['train']['log_path'], 'w')

        # log all parameters
        msg = (
            f"{'<' * 10} Hello {'>' * 10}\n"
            f"Timestamp: [{utils.get_timestr()}]\n"
            f"\n{'<' * 10} Options {'>' * 10}\n"
            f"{utils.dict2str(opts_dict)}"
            )
        print(msg)
        log_fp.write(msg + '\n')
        log_fp.flush()

    # ==========
    # TO-DO: init tensorboard
    # ==========

    pass
    
    # ==========
    # fix random seed
    # ==========

    seed = opts_dict['train']['random_seed']
    # >I don't know why should rs + rank
    utils.set_random_seed(seed + rank)

    # ========== 
    # Ensure reproducibility or Speed up
    # ==========

    #torch.backends.cudnn.benchmark = False  # if reproduce
    #torch.backends.cudnn.deterministic = True  # if reproduce
    torch.backends.cudnn.benchmark = True  # speed up

    # ==========
    # create train and val data prefetchers
    # ==========
    
    # create datasets
    train_ds_type = opts_dict['dataset']['train']['type']
    val_ds_type = opts_dict['dataset']['val']['type']
    radius = opts_dict['network']['radius']
    assert train_ds_type in dataset.__all__, \
        "Not implemented!"
    assert val_ds_type in dataset.__all__, \
        "Not implemented!"
    train_ds_cls = getattr(dataset, train_ds_type)
    val_ds_cls = getattr(dataset, val_ds_type)
    train_ds = train_ds_cls(
        opts_dict=opts_dict['dataset']['train'], 
        radius=radius
        )
    val_ds = val_ds_cls(
        opts_dict=opts_dict['dataset']['val'], 
        radius=radius
        )

    # create datasamplers
    train_sampler = utils.DistSampler(
        dataset=train_ds, 
        num_replicas=opts_dict['train']['num_gpu'], 
        rank=rank, 
        ratio=opts_dict['dataset']['train']['enlarge_ratio']
        )
    val_sampler = None  # no need to sample val data

    # create dataloaders
    train_loader = utils.create_dataloader(
        dataset=train_ds, 
        opts_dict=opts_dict, 
        sampler=train_sampler, 
        phase='train',
        seed=opts_dict['train']['random_seed']
        )
    val_loader = utils.create_dataloader(
        dataset=val_ds, 
        opts_dict=opts_dict, 
        sampler=val_sampler, 
        phase='val'
        )
    assert train_loader is not None

    batch_size = opts_dict['dataset']['train']['batch_size_per_gpu'] * \
        opts_dict['train']['num_gpu']  # divided by all GPUs
    num_iter_per_epoch = math.ceil(len(train_ds) * \
        opts_dict['dataset']['train']['enlarge_ratio'] / batch_size)
    num_epoch = math.ceil(num_iter / num_iter_per_epoch)
    val_num = len(val_ds)
    
    # create dataloader prefetchers
    tra_prefetcher = utils.CPUPrefetcher(train_loader)
    val_prefetcher = utils.CPUPrefetcher(val_loader)

    # ==========
    # create model
    # ==========

    model = MFVQE(opts_dict=opts_dict['network'])

    model = model.to(rank)
    if opts_dict['train']['is_dist']:
        model = DDP(model, device_ids=[rank])

    """
    # load pre-trained generator
    ckp_path = opts_dict['network']['stdf']['load_path']
    checkpoint = torch.load(ckp_path)
    state_dict = checkpoint['state_dict']
    if ('module.' in list(state_dict.keys())[0]) and (not opts_dict['train']['is_dist']):  # multi-gpu pre-trained -> single-gpu training
        new_state_dict = OrderedDict()
        for k, v in state_dict.items():
            name = k[7:]  # remove module
            new_state_dict[name] = v
        model.load_state_dict(new_state_dict)
        print(f'loaded from {ckp_path}')
    elif ('module.' not in list(state_dict.keys())[0]) and (opts_dict['train']['is_dist']):  # single-gpu pre-trained -> multi-gpu training
        new_state_dict = OrderedDict()
        for k, v in state_dict.items():
            name = 'module.' + k  # add module
            new_state_dict[name] = v
        model.load_state_dict(new_state_dict)
        print(f'loaded from {ckp_path}')
    else:  # the same way of training
        model.load_state_dict(state_dict)
        print(f'loaded from {ckp_path}')
    """

    # ==========
    # define loss func & optimizer & scheduler & scheduler & criterion
    # ==========

    # define loss func
    assert opts_dict['train']['loss'].pop('type') == 'CharbonnierLoss', \
        "Not implemented."
    loss_func = utils.CharbonnierLoss(**opts_dict['train']['loss'])

    # define optimizer
    assert opts_dict['train']['optim'].pop('type') == 'Adam', \
        "Not implemented."
    optimizer = optim.Adam(
        model.parameters(), 
        **opts_dict['train']['optim']
        )

    # define scheduler
    if opts_dict['train']['scheduler']['is_on']:
        assert opts_dict['train']['scheduler'].pop('type') == \
            'CosineAnnealingRestartLR', "Not implemented."
        del opts_dict['train']['scheduler']['is_on']
        scheduler = utils.CosineAnnealingRestartLR(
            optimizer, 
            **opts_dict['train']['scheduler']
            )
        opts_dict['train']['scheduler']['is_on'] = True

    # define criterion
    assert opts_dict['train']['criterion'].pop('type') == \
        'PSNR', "Not implemented."
    criterion = utils.PSNR()

    #

    start_iter = 0  # should be restored
    start_epoch = start_iter // num_iter_per_epoch

    # display and log
    if rank == 0:
        msg = (
            f"\n{'<' * 10} Dataloader {'>' * 10}\n"
            f"total iters: [{num_iter}]\n"
            f"total epochs: [{num_epoch}]\n"
            f"iter per epoch: [{num_iter_per_epoch}]\n"
            f"val sequence: [{val_num}]\n"
            f"start from iter: [{start_iter}]\n"
            f"start from epoch: [{start_epoch}]"
            )
        print(msg)
        log_fp.write(msg + '\n')
        log_fp.flush()

    # ==========
    # evaluate original performance, e.g., PSNR before enhancement
    # ==========

    vid_num = val_ds.get_vid_num()
    if opts_dict['train']['pre-val'] and rank == 0:
        msg = f"\n{'<' * 10} Pre-evaluation {'>' * 10}"
        print(msg)
        log_fp.write(msg + '\n')

        per_aver_dict = {}
        for i in range(vid_num):
            per_aver_dict[i] = utils.Counter()
        pbar = tqdm(
                total=val_num, 
                ncols=opts_dict['train']['pbar_len']
                )

        # fetch the first batch
        val_prefetcher.reset()
        val_data = val_prefetcher.next()

        while val_data is not None:
            # get data
            gt_data = val_data['gt'].to(rank)  # (B [RGB] H W)
            lq_data = val_data['lq'].to(rank)  # (B T [RGB] H W)
            index_vid = val_data['index_vid'].item()
            name_vid = val_data['name_vid'][0]  # bs must be 1!
            b, _, _, _, _  = lq_data.shape
            
            # eval
            batch_perf = np.mean(
                [criterion(lq_data[i,radius,...], gt_data[i]) for i in range(b)]
                )  # bs must be 1!
            
            # log
            per_aver_dict[index_vid].accum(volume=batch_perf)

            # display
            pbar.set_description(
                "{:s}: [{:.3f}] {:s}".format(name_vid, batch_perf, unit)
                )
            pbar.update()

            # fetch next batch
            val_data = val_prefetcher.next()

        pbar.close()

        # log
        ave_performance = np.mean([
            per_aver_dict[index_vid].get_ave() for index_vid in range(vid_num)
            ])
        msg = "> ori performance: [{:.3f}] {:s}".format(ave_performance, unit)
        print(msg)
        log_fp.write(msg + '\n')
        log_fp.flush()

    if opts_dict['train']['is_dist']:
        torch.distributed.barrier()  # all processes wait for ending

    if rank == 0:
        msg = f"\n{'<' * 10} Training {'>' * 10}"
        print(msg)
        log_fp.write(msg + '\n')

        # create timer
        total_timer = utils.Timer()  # total tra + val time of each epoch

    # ==========
    # start training + validation (test)
    # ==========

    model.train()
    num_iter_accum = start_iter
    for current_epoch in range(start_epoch, num_epoch + 1):
        # shuffle distributed subsamplers before each epoch
        if opts_dict['train']['is_dist']:
            train_sampler.set_epoch(current_epoch)

        # fetch the first batch
        tra_prefetcher.reset()
        train_data = tra_prefetcher.next()

        # train this epoch
        while train_data is not None:

            # over sign
            num_iter_accum += 1
            if num_iter_accum > num_iter:
                break

            # get data
            gt_data = train_data['gt'].to(rank)  # (B [RGB] H W)
            lq_data = train_data['lq'].to(rank)  # (B T [RGB] H W)
            b, _, c, _, _  = lq_data.shape
            input_data = torch.cat(
                [lq_data[:,:,i,...] for i in range(c)], 
                dim=1
                )  # B [R1 ... R7 G1 ... G7 B1 ... B7] H W
            enhanced_data = model(input_data)

            # get loss
            optimizer.zero_grad()  # zero grad
            loss = torch.mean(torch.stack(
                [loss_func(enhanced_data[i], gt_data[i]) for i in range(b)]
                ))  # cal loss
            loss.backward()  # cal grad
            optimizer.step()  # update parameters

            # update learning rate
            if opts_dict['train']['scheduler']['is_on']:
                scheduler.step()  # should after optimizer.step()

            if (num_iter_accum % interval_print == 0) and (rank == 0):
                # display & log
                lr = optimizer.param_groups[0]['lr']
                loss_item = loss.item()
                msg = (
                    f"iter: [{num_iter_accum}]/{num_iter}, "
                    f"epoch: [{current_epoch}]/{num_epoch - 1}, "
                    "lr: [{:.3f}]x1e-4, loss: [{:.4f}]".format(
                        lr*1e4, loss_item
                        )
                    )
                print(msg)
                log_fp.write(msg + '\n')

            if ((num_iter_accum % interval_val == 0) or \
                (num_iter_accum == num_iter)) and (rank == 0):
                # save model
                checkpoint_save_path = (
                    f"{opts_dict['train']['checkpoint_save_path_pre']}"
                    f"{num_iter_accum}"
                    ".pt"
                    )
                state = {
                    'num_iter_accum': num_iter_accum, 
                    'state_dict': model.state_dict(),
                    'optimizer': optimizer.state_dict(), 
                    }
                if opts_dict['train']['scheduler']['is_on']:
                    state['scheduler'] = scheduler.state_dict()
                torch.save(state, checkpoint_save_path)
                
                # validation
                with torch.no_grad():
                    per_aver_dict = {}
                    for index_vid in range(vid_num):
                        per_aver_dict[index_vid] = utils.Counter()
                    pbar = tqdm(
                            total=val_num, 
                            ncols=opts_dict['train']['pbar_len']
                            )
                
                    # train -> eval
                    model.eval()

                    # fetch the first batch
                    val_prefetcher.reset()
                    val_data = val_prefetcher.next()
                    
                    while val_data is not None:
                        # get data
                        gt_data = val_data['gt'].to(rank)  # (B [RGB] H W)
                        lq_data = val_data['lq'].to(rank)  # (B T [RGB] H W)
                        index_vid = val_data['index_vid'].item()
                        name_vid = val_data['name_vid'][0]  # bs must be 1!
                        b, _, c, _, _  = lq_data.shape
                        input_data = torch.cat(
                            [lq_data[:,:,i,...] for i in range(c)], 
                            dim=1
                            )  # B [R1 ... R7 G1 ... G7 B1 ... B7] H W
                        enhanced_data = model(input_data)  # (B [RGB] H W)

                        # eval
                        batch_perf = np.mean(
                            [criterion(enhanced_data[i], gt_data[i]) for i in range(b)]
                            ) # bs must be 1!

                        # display
                        pbar.set_description(
                            "{:s}: [{:.3f}] {:s}"
                            .format(name_vid, batch_perf, unit)
                            )
                        pbar.update()

                        # log
                        per_aver_dict[index_vid].accum(volume=batch_perf)

                        # fetch next batch
                        val_data = val_prefetcher.next()
                    
                    # end of val
                    pbar.close()

                    # eval -> train
                    model.train()

                # log
                ave_per = np.mean([
                    per_aver_dict[index_vid].get_ave() for index_vid in range(vid_num)
                    ])
                msg = (
                    "> model saved at {:s}\n"
                    "> ave val per: [{:.3f}] {:s}"
                    ).format(
                        checkpoint_save_path, ave_per, unit
                        )
                print(msg)
                log_fp.write(msg + '\n')
                log_fp.flush()

            if opts_dict['train']['is_dist']:
                torch.distributed.barrier()  # all processes wait for ending

            # fetch next batch
            train_data = tra_prefetcher.next()

        # end of this epoch (training dataloader exhausted)

    # end of all epochs

    # ==========
    # final log & close logger
    # ==========

    if rank == 0:
        total_time = total_timer.get_interval() / 3600
        msg = "TOTAL TIME: [{:.1f}] h".format(total_time)
        print(msg)
        log_fp.write(msg + '\n')
        
        msg = (
            f"\n{'<' * 10} Goodbye {'>' * 10}\n"
            f"Timestamp: [{utils.get_timestr()}]"
            )
        print(msg)
        log_fp.write(msg + '\n')
        
        log_fp.close()
Пример #24
0
    act_net = networks.LinearNetwork(inputs=state_dim,
                                     outputs=act_dim,
                                     n_hidden_layers=3,
                                     n_hidden_units=128)
    shared_model = networks.DiscreteActorCriticSplit(actor=act_net,
                                                     critic=value_net,
                                                     add_softmax=True)
    shared_average_model = copy.deepcopy(shared_model)
    shared_average_model.no_grads(
    )  # Set requires_grad to false for all parameters
    shared_model.share_memory()
    shared_average_model.share_memory()
    shared_opt = optimizers.SharedAdam(shared_model.parameters(), lr=args.lr)

    # Create shared variables
    shared_counter = utils.Counter()
    shared_model_lock = mp.Lock() if not args.no_lock else None
    summary_queue = mp.Queue()

    processes = []
    workers = []
    for i in range(args.num_workers):
        w = worker.Worker(worker_id=i,
                          env_name=args.env_name,
                          n_steps=args.worker_steps,
                          max_steps=args.t_max,
                          shared_model=shared_model,
                          shared_avg_model=shared_average_model,
                          shared_optimizer=shared_opt,
                          shared_counter=shared_counter,
                          df=args.discount,
Пример #25
0
 def getDistribution(self, state):
     dist = utils.Counter()
     for a in state.getLegalActions(self.index):
         dist[a] = 1.0
     dist.normalize()
     return dist
Пример #26
0
    def td_cap(self,
               tdc_params: Dict[str, Any],
               update_snap: bool = True) -> Tuple[Any]:
        """Performs a capacitive touchdown.

        Args:
            tdc_params: Dict of capacitive touchdown parameters as defined
                in measurement configuration file.
            update_snap: Whether to update the microscope snapshot. Default True.
                (You may want this to be False when getting a plane or approaching.)
        Returns:
            Tuple[qcodes.DataSet, plots.TDCPlot]: data, tdc_plot
                DataSet and plot generated by the touchdown Loop.
        """
        old_pos = self.scanner.position()
        constants = tdc_params['constants']
        daq_config = self.config['instruments']['daq']
        daq_name = daq_config['name']
        ai_channels = daq_config['channels']['analog_inputs']
        meas_channels = tdc_params['channels']
        channels = {}
        for ch in meas_channels:
            channels.update({ch: ai_channels[ch]})
        nchannels = len(channels.keys())
        daq_rate = self.Q_(daq_config['rate']).to('Hz').magnitude / nchannels
        self.set_lockins(tdc_params)
        self.snapshot(update=update_snap)
        #: z position voltage step
        dV = self.Q_(tdc_params['dV']).to('V').magnitude
        #: Start and end z position voltages
        startV, endV = sorted(
            [self.Q_(lim).to('V').magnitude for lim in tdc_params['range']])
        delay = constants['wait_factor'] * max(
            self.CAP_lockin.time_constant(), self.SUSC_lockin.time_constant())
        prefactors = self.get_prefactors(tdc_params)
        #: get channel prefactors in string form so they can be saved in metadata
        prefactor_strs = {}
        for ch, prefac in prefactors.items():
            unit = tdc_params['channels'][ch]['unit']
            pre = prefac.to('{}/V'.format(unit))
            prefactor_strs.update(
                {ch: '{} {}'.format(pre.magnitude, pre.units)})
        ai_task = nidaqmx.Task('td_cap_ai_task')
        self.remove_component('daq_ai')
        if hasattr(self, 'daq_ai'):
            #self.daq_ai.clear_instances()
            self.daq_ai.close()
        self.daq_ai = DAQAnalogInputs('daq_ai', daq_name, daq_rate, channels,
                                      ai_task)
        loop_counter = utils.Counter()
        tdc_plot = TDCPlot(tdc_params, self.ureg)
        loop = qc.Loop(self.scanner.position_z.sweep(startV, endV, dV)).each(
            qc.Task(time.sleep, delay), self.daq_ai.voltage,
            qc.Task(self.scanner.check_for_td, tdc_plot,
                    qc.loops.active_data_set, loop_counter),
            qc.Task(self.scanner.get_td_height, tdc_plot),
            qc.BreakIf(lambda scanner=self.scanner: scanner.break_loop or
                       scanner.td_has_occurred), qc.Task(loop_counter.advance)
        ).then(
            qc
            .Task(ai_task.stop),
            qc.
            Task(ai_task.close
                 ),
            #qc.Task(self.CAP_lockin.amplitude, 0.004),
            #qc.Task(self.SUSC_lockin.amplitude, 0.004),
            qc.Task(self.scanner.retract),
            qc.Task(tdc_plot.fig.show),
            qc.Task(tdc_plot.save))
        #: loop.metadata will be saved in DataSet
        loop.metadata.update(tdc_params)
        loop.metadata.update({'prefactors': prefactor_strs})
        for idx, ch in enumerate(meas_channels):
            loop.metadata['channels'][ch].update({'idx': idx})
        data = loop.get_data_set(name=tdc_params['fname'], write_period=None)
        try:
            log.info('Starting capacitive touchdown.')
            loop.run()
            if self.scanner.td_height is not None:
                data.metadata['loop']['metadata'].update(
                    {'td_height': self.scanner.td_height})
                if abs(old_pos[0]) < 0.01 and abs(old_pos[1]) < 0.01:
                    self.scanner.metadata['plane'].update(
                        {'z': self.scanner.td_height})
        except KeyboardInterrupt:
            log.warning(
                'Touchdown interrupted by user. Retracting scanner. DataSet saved to {}.'
                .format(data.location))
            #: Set break_loop = True so that get_plane() and approach() will be aborted
            self.scanner.break_loop = True
        finally:
            try:
                #: Stop 'td_cap_ai_task' so that we can read our current position
                ai_task.stop()
                ai_task.close()
                self.scanner.retract()
                #self.CAP_lockin.amplitude(0.004)
                tdc_plot.fig.show()
                tdc_plot.save()
            except:
                pass
        utils.td_to_mat_file(data, real_units=True)
        return data, tdc_plot
Пример #27
0
    def fit_incremental_batch(self,
                              curr_loader,
                              latent_dict,
                              pq,
                              rehearsal_ixs=None,
                              class_id_to_item_ix_dict=None,
                              verbose=True,
                              counter=utils.Counter()):
        """
        Fit REMIND on samples from a data loader one at a time.
        :param curr_loader: the data loader of new samples to be fit (returns (images, labels, item_ixs)
        :param latent_dict: dictionary containing latent codes for replay samples
        :param pq: trained PQ object for decoding latent codes
        :param rehearsal_ixs: list of item_ixs eligible for replay
        :param class_id_to_item_ix_dict: dictionary of visited classes with associated item_ixs visited
        :param verbose: true for printing loss to console
        :param counter: object to track how many samples are in buffer
        :return: None
        """

        ongoing_class = None

        # put classifiers on GPU and set plastic portion of network to train
        classifier_F = self.classifier_F.cuda()
        classifier_F.train()
        classifier_G = self.classifier_G.cuda()
        classifier_G.eval()

        criterion = nn.CrossEntropyLoss(reduction='none')

        msg = '\rSample %d -- train_loss=%1.6f -- elapsed_time=%d secs'

        start_time = time.time()
        total_loss = utils.CMA()
        c = 0
        for batch_images, batch_labels, batch_item_ixs in curr_loader:

            # get features from G and latent codes from PQ
            data_batch = classifier_G(batch_images.cuda()).cpu().numpy()
            data_batch = np.transpose(data_batch, (0, 2, 3, 1))
            data_batch = np.reshape(data_batch, (-1, self.num_channels))
            codes = pq.compute_codes(data_batch)
            codes = np.reshape(
                codes,
                (-1, self.num_feats, self.num_feats, self.num_codebooks))

            # train REMIND on one new sample at a time
            for x, y, item_ix in zip(codes, batch_labels, batch_item_ixs):
                if self.lr_mode == 'step_lr_per_class' and (
                        ongoing_class is None or ongoing_class != y):
                    ongoing_class = y

                if self.use_mixup:
                    # gather two batches of previous data for mixup and replay
                    data_codes = np.empty(
                        (2 * self.num_samples + 1, self.num_feats,
                         self.num_feats, self.num_codebooks),
                        dtype=np.uint8)
                    data_labels = torch.empty((2 * self.num_samples + 1),
                                              dtype=torch.int).cuda()
                    data_codes[0] = x
                    data_labels[0] = y
                    ixs = randint(len(rehearsal_ixs), 2 * self.num_samples)
                    ixs = [rehearsal_ixs[_curr_ix] for _curr_ix in ixs]
                    for ii, v in enumerate(ixs):
                        data_codes[ii + 1] = latent_dict[v][0]
                        data_labels[ii + 1] = torch.from_numpy(
                            latent_dict[v][1])

                    # reconstruct/decode samples with PQ
                    data_codes = np.reshape(
                        data_codes,
                        ((2 * self.num_samples + 1) * self.num_feats *
                         self.num_feats, self.num_codebooks))
                    data_batch_reconstructed = pq.decode(data_codes)
                    data_batch_reconstructed = np.reshape(
                        data_batch_reconstructed,
                        (-1, self.num_feats, self.num_feats,
                         self.num_channels))
                    data_batch_reconstructed = torch.from_numpy(
                        np.transpose(data_batch_reconstructed,
                                     (0, 3, 1, 2))).cuda()

                    # perform random resize crop augmentation on each tensor
                    if self.use_random_resize_crops:
                        transform_data_batch = torch.empty_like(
                            data_batch_reconstructed)
                        for tens_ix, tens in enumerate(
                                data_batch_reconstructed):
                            transform_data_batch[
                                tens_ix] = self.random_resize_crop(tens)
                        data_batch_reconstructed = transform_data_batch

                    # MIXUP: Do mixup between two batches of previous data
                    x_prev_mixed, prev_labels_a, prev_labels_b, lam = self.mixup_data(
                        data_batch_reconstructed[1:1 + self.num_samples],
                        data_labels[1:1 + self.num_samples],
                        data_batch_reconstructed[1 + self.num_samples:],
                        data_labels[1 + self.num_samples:],
                        alpha=self.mixup_alpha)

                    data = torch.empty(
                        (self.num_samples + 1, self.num_channels,
                         self.num_feats, self.num_feats))
                    data[0] = data_batch_reconstructed[0]
                    data[1:] = x_prev_mixed.clone()
                    labels_a = torch.zeros(self.num_samples + 1).long()
                    labels_b = torch.zeros(self.num_samples + 1).long()
                    labels_a[0] = y.squeeze()
                    labels_b[0] = y.squeeze()
                    labels_a[1:] = prev_labels_a
                    labels_b[1:] = prev_labels_b

                    # fit on replay mini-batch plus new sample
                    output = classifier_F(data.cuda())
                    loss = self.mixup_criterion(criterion, output,
                                                labels_a.cuda(),
                                                labels_b.cuda(), lam)
                else:
                    # gather previous data for replay
                    data_codes = np.empty(
                        (self.num_samples + 1, self.num_feats, self.num_feats,
                         self.num_codebooks),
                        dtype=np.uint8)
                    data_labels = torch.empty((self.num_samples + 1),
                                              dtype=torch.long).cuda()
                    data_codes[0] = x
                    data_labels[0] = y
                    ixs = randint(len(rehearsal_ixs), self.num_samples)
                    ixs = [rehearsal_ixs[_curr_ix] for _curr_ix in ixs]
                    for ii, v in enumerate(ixs):
                        data_codes[ii + 1] = latent_dict[v][0]
                        data_labels[ii + 1] = torch.from_numpy(
                            latent_dict[v][1])

                    # reconstruct/decode samples with PQ
                    data_codes = np.reshape(
                        data_codes, ((self.num_samples + 1) * self.num_feats *
                                     self.num_feats, self.num_codebooks))
                    data_batch_reconstructed = pq.decode(data_codes)
                    data_batch_reconstructed = np.reshape(
                        data_batch_reconstructed,
                        (-1, self.num_feats, self.num_feats,
                         self.num_channels))
                    data_batch_reconstructed = torch.from_numpy(
                        np.transpose(data_batch_reconstructed,
                                     (0, 3, 1, 2))).cuda()

                    # perform random resize crop augmentation on each tensor
                    if self.use_random_resize_crops:
                        transform_data_batch = torch.empty_like(
                            data_batch_reconstructed)
                        for tens_ix, tens in enumerate(
                                data_batch_reconstructed):
                            transform_data_batch[
                                tens_ix] = self.random_resize_crop(tens)
                        data_batch_reconstructed = transform_data_batch

                    # fit on replay mini-batch plus new sample
                    output = classifier_F(data_batch_reconstructed)
                    loss = criterion(output, data_labels)

                loss = loss.mean()
                self.optimizer.zero_grad(
                )  # zero out grads before backward pass because they are accumulated
                loss.backward()

                # if gradient clipping is desired
                if self.grad_clip is not None:
                    nn.utils.clip_grad_norm_(classifier_F.parameters(),
                                             self.grad_clip)

                self.optimizer.step()

                total_loss.update(loss.item())
                if verbose:
                    print(msg % (c, total_loss.avg, time.time() - start_time),
                          end="")
                c += 1

                # since we have visited item_ix, it is now eligible for replay
                rehearsal_ixs.append(int(item_ix.numpy()))
                latent_dict[int(item_ix.numpy())] = [x, y.numpy()]
                class_id_to_item_ix_dict[int(y.numpy())].append(
                    int(item_ix.numpy()))

                # if buffer is full, randomly replace previous example from class with most samples
                if self.max_buffer_size is not None and counter.count >= self.max_buffer_size:
                    # class with most samples and random item_ix from it
                    max_key = max(
                        class_id_to_item_ix_dict,
                        key=lambda x: len(class_id_to_item_ix_dict[x]))
                    max_class_list = class_id_to_item_ix_dict[max_key]
                    rand_item_ix = random.choice(max_class_list)

                    # remove the random_item_ix from all buffer references
                    max_class_list.remove(rand_item_ix)
                    latent_dict.pop(rand_item_ix)
                    rehearsal_ixs.remove(rand_item_ix)
                else:
                    counter.update()

                # update lr scheduler
                if self.lr_scheduler_per_class is not None:
                    self.lr_scheduler_per_class[int(y)].step()
Пример #28
0
    def scan_surface(self, scan_params: Dict[str, Any]) -> None:
        """
        Scan the current surface while acquiring data in the channels defined in
        measurement configuration file (e.g. MAG, SUSCX, SUSCY, CAP).
        
        Args:
            scan_params: Dict of scan parameters as defined
                in measuremnt configuration file.
        Returns:
            None
        """
        if not self.atto.surface_is_current:
            raise RuntimeError('Surface is not current. Aborting scan.')
        surface_type = scan_params['surface_type'].lower()
        if surface_type not in ['plane', 'surface']:
            raise ValueError('surface_type must be "plane" or "surface".')

        old_pos = self.scanner.position()

        daq_config = self.config['instruments']['daq']
        ao_channels = daq_config['channels']['analog_outputs']
        ai_channels = daq_config['channels']['analog_inputs']
        meas_channels = scan_params['channels']
        channels = {}
        for ch in meas_channels:
            channels.update({ch: ai_channels[ch]})
        nchannels = len(channels.keys())

        daq_name = daq_config['name']
        #: DAQ AI sampling rate is divided amongst all active AI channels
        daq_rate = self.Q_(daq_config['rate']).to('Hz').magnitude / nchannels

        fast_ax = scan_params['fast_ax'].lower()
        slow_ax = 'x' if fast_ax == 'y' else 'y'

        pix_per_line = scan_params['scan_size'][fast_ax]
        line_duration = pix_per_line * self.ureg('pixels') / self.Q_(
            scan_params['scan_rate'])
        pts_per_line = int(daq_rate * line_duration.to('s').magnitude)
        height = self.Q_(scan_params['height']).to('V').magnitude

        if 1 / self.Q_(scan_params['scan_rate']).to(
                'pixels/s').magnitude < self.SUSC_lockin.time_constant():
            warning = 'Averaging time per pixel is less than the SUSC_lockin time constant. '
            warning += 'For reliable susceptibility data, averaging time per pixel should be '
            warning += 'significantly greater than the SUSC_lockin time constant.'
            log.warning(warning)

        scan_vectors = utils.make_scan_vectors(scan_params, self.ureg)
        #scan_grids = utils.make_scan_grids(scan_vectors, slow_ax, fast_ax,
        #                                   pts_per_line, plane, height)
        plane = self.scanner.metadata['plane']
        if surface_type == 'plane':
            scan_grids = utils.make_scan_surface(surface_type, scan_vectors,
                                                 slow_ax, fast_ax,
                                                 pts_per_line, plane, height)
        else:
            scan_grids = utils.make_scan_surface(
                surface_type,
                scan_vectors,
                slow_ax,
                fast_ax,
                pts_per_line,
                plane,
                height,
                interpolator=self.scanner.surface_interp)
        utils.validate_scan_params(self.scanner.metadata, scan_params,
                                   scan_grids, pix_per_line, pts_per_line,
                                   self.temp, self.ureg, log)
        self.scanner.goto([scan_grids[axis][0][0] for axis in ['x', 'y', 'z']])
        self.set_lockins(scan_params)
        #: get channel prefactors in pint Quantity form
        prefactors = self.get_prefactors(scan_params)
        #: get channel prefactors in string form so they can be saved in metadata
        prefactor_strs = {}
        for ch, prefac in prefactors.items():
            unit = scan_params['channels'][ch]['unit']
            pre = prefac.to('{}/V'.format(unit))
            prefactor_strs.update(
                {ch: '{} {}'.format(pre.magnitude, pre.units)})
        ai_task = nidaqmx.Task('scan_plane_ai_task')
        self.remove_component('daq_ai')
        if hasattr(self, 'daq_ai'):
            #self.daq_ai.clear_instances()
            self.daq_ai.close()
        self.daq_ai = DAQAnalogInputs(
            'daq_ai',
            daq_name,
            daq_rate,
            channels,
            ai_task,
            samples_to_read=pts_per_line,
            target_points=pix_per_line,
            #: Very important to synchronize AOs and AIs
            clock_src='ao/SampleClock')
        self.add_component(self.daq_ai)
        slow_ax_position = getattr(self.scanner, 'position_{}'.format(slow_ax))
        slow_ax_start = scan_vectors[slow_ax][0]
        slow_ax_end = scan_vectors[slow_ax][-1]
        slow_ax_step = scan_vectors[slow_ax][1] - scan_vectors[slow_ax][0]
        #: There is probably a counter built in to qc.Loop, but I couldn't find it
        loop_counter = utils.Counter()
        scan_plot = ScanPlot(scan_params, self.ureg)
        loop = qc.Loop(
            slow_ax_position.sweep(start=slow_ax_start,
                                   stop=slow_ax_end,
                                   step=slow_ax_step),
            delay=0.1
        ).each(
            #: Create AO task and queue data to be written to AOs
            qc.Task(self.scanner.scan_line, scan_grids, ao_channels, daq_rate,
                    loop_counter),
            #: Start AI task; acquisition won't start until AO task is started
            qc.Task(ai_task.start),
            #: Start AO task
            qc.Task(self.scanner.control_ao_task, 'start'),
            #: Acquire voltage from all active AI channels
            self.daq_ai.voltage,
            qc.Task(ai_task.wait_until_done),
            qc.Task(self.scanner.control_ao_task, 'wait_until_done'),
            qc.Task(ai_task.stop),
            #: Stop and close AO task so that AOs can be used for goto
            qc.Task(self.scanner.control_ao_task, 'stop'),
            qc.Task(self.scanner.control_ao_task, 'close'),
            qc.Task(self.scanner.goto_start_of_next_line, scan_grids,
                    loop_counter),
            #: Update and save plot
            qc.Task(scan_plot.update, qc.loops.active_data_set, loop_counter),
            qc.Task(scan_plot.save),
            qc.Task(loop_counter.advance)
        ).then(
            qc.Task(ai_task.stop),
            qc.Task(ai_task.close),
            qc.Task(self.daq_ai.close),
            #qc.Task(self.daq_ai.clear_instances),
            qc.Task(self.scanner.goto, old_pos),
            #qc.Task(self.CAP_lockin.amplitude, 0.004),
            #qc.Task(self.SUSC_lockin.amplitude, 0.004)
        )
        #: loop.metadata will be saved in DataSet
        loop.metadata.update(scan_params)
        loop.metadata.update({'prefactors': prefactor_strs})
        for idx, ch in enumerate(meas_channels):
            loop.metadata['channels'][ch].update({'idx': idx})
        data = loop.get_data_set(name=scan_params['fname'])
        #: Run the loop
        try:
            loop.run()
            log.info('Scan completed. DataSet saved to {}.'.format(
                data.location))
        #: If loop is aborted by user:
        except KeyboardInterrupt:
            log.warning(
                'Scan aborted by user. Going to [0,0,0] V. DataSet saved to {}.'
                .format(data.location))
        finally:
            try:
                #: Stop 'scan_plane_ai_task' so that we can read our current position
                ai_task.stop()
                ai_task.close()
                #: If there's an active AO task, close it so that we can use goto
                self.scanner.control_ao_task('stop')
                self.scanner.control_ao_task('close')
                self.remove_component('daq_ai')
            except:
                pass
        self.scanner.goto([0, 0, 0])
        #self.CAP_lockin.amplitude(0.004)
        #self.SUSC_lockin.amplitude(0.004)
        utils.scan_to_mat_file(data,
                               real_units=True,
                               interpolator=self.scanner.surface_interp)
Пример #29
0
    # build up the environment and extract some informations...
    env = gym.make(args.env_name)
    num_inputs = env.observation_space.shape[0]
    num_actions = env.action_space.shape[0]

    # define the global network...
    critic_shared_model = models.Critic_Network(num_inputs)
    critic_shared_model.share_memory()

    actor_shared_model = models.Actor_Network(num_inputs, num_actions)
    actor_shared_model.share_memory()

    # define the traffic signal...
    traffic_signal = utils.TrafficLight()
    # define the counter
    critic_counter = utils.Counter()
    actor_counter = utils.Counter()
    # define the shared gradient buffer...
    critic_shared_grad_buffer = utils.Shared_grad_buffers(critic_shared_model)
    actor_shared_grad_buffer = utils.Shared_grad_buffers(actor_shared_model)
    # define shared observation state...
    shared_obs_state = utils.Running_mean_filter(num_inputs)
    # define shared reward...
    shared_reward = utils.RewardCounter()
    # define the optimizer...
    critic_optimizer = torch.optim.Adam(critic_shared_model.parameters(), lr=args.value_lr)
    actor_optimizer = torch.optim.Adam(actor_shared_model.parameters(), lr=args.policy_lr)

    # find how many processor is available...
    num_of_workers = mp.cpu_count() - 1
    processor = []
Пример #30
0

def broadcast_tonodes(nodes, firstreplyonly, *args):
    todo = nodes[:]
    r = []

    for n in todo:
        n = str(n)
        assert utils.isNodeId(n)
        m = sendMessageToNode(n, None, *args)

        if m is None or m[0] == dont_know:
            pass
        elif firstreplyonly:
            return m
        else:
            r.append(m)
    if firstreplyonly:
        return dont_know
    else:
        return r


import utils
getMsgId = utils.Counter()

import server

from autoregister import convertTo, convertFrom
import thread_pool