Example #1
0
    def __init__(self):

        # When inferences only return new knowledge on the world
        normal_case = Inference(
            [InferenceEngine.has_safe_unexplored_neighbour],
            [InferenceEngine.explore_safe_neighbors])
        fell_case = Inference(
            [InferenceEngine.is_windy, InferenceEngine.agent_fell_in_neighbor],
            [InferenceEngine.agent_knows_hole_neighbor])
        wind_case = Inference([InferenceEngine.is_windy],
                              [InferenceEngine.mark_neighbors_risky_of_hole])
        poop_case = Inference(
            [InferenceEngine.is_pooped],
            [InferenceEngine.mark_neighbors_risky_of_monster])
        area_cleaned = Inference([
            InferenceEngine.is_pooped, InferenceEngine.threw_stone_on_neighbor
        ], [InferenceEngine.mark_neighbor_safe_of_monster])
        # gate_case = Inference([InferenceEngine.has_portal], [InferenceEngine.take_portal])

        rule_set = [normal_case, fell_case, poop_case, wind_case, area_cleaned]
        inference_engine = InferenceEngine(rule_set)

        self.size = 3

        self.wood = Wood(self.size)
        self.agent = Agent(inference_engine, self.wood)
        self.display = Display(self.agent, self.wood)
 def __init__(self, weights_path="/user/vlongobardi/checkpoint_best.hdf5", time_step=16, overlap=.5):
     self.time_step = time_step
     self.model = SharmaNet((self.time_step, 224, 224, 3), classification=True, weights='afew')  # , dim=-1)
     self.model.load_weights(weights_path)
     self.feature_generator = None
     self.inference = Inference(model=self.model, custom_inference=True, time_step=self.time_step)
     self.overlap = overlap
def inference():
    inf = Inference(model_path=f'models/{args.predict_type}.h5',
                    sample_dir='samples',
                    inference_type=args.predict_type,
                    inference_csv=f'data/{args.predict_type}.csv')

    inf.predict(save_result=True)
def test_model(configs):
    """测试模型
    """
    path_test = configs['data_params']['path_test'] if 'path_test' in configs[
        'data_params'] else None
    # init model
    encoder, ptr_model, decoder = load_model(configs)

    # init test data
    data_test = load_data(path_test)
    data_iter_test = init_test_data(configs)

    # init infer
    if 'path_test_result' not in configs['data_params'] or \
            not configs['data_params']['path_test_result']:
        path_result = configs['data_params']['path_test'] + '.result'
    else:
        path_result = configs['data_params']['path_test_result']
    # label to id dict
    path_pkl = os.path.join(configs['data_params']['alphabet_params']['path'],
                            'label.pkl')
    label2id_dict = read_bin(path_pkl)
    infer = Inference(encoder=encoder,
                      ptr_model=ptr_model,
                      decoder=decoder,
                      data_iter=data_iter_test,
                      data_raw=data_test,
                      path_result=path_result,
                      label2id_dict=label2id_dict)

    # do infer
    infer.infer2file()
Example #5
0
    def __init__(self, representation="pool", L=12, shared_core=False):
        super(GQN, self).__init__()

        # Number of generative layers
        self.L = L

        self.shared_core = shared_core

        # Representation network
        self.representation = representation
        if representation == "pyramid":
            self.phi = Pyramid()
        elif representation == "tower":
            self.phi = Tower()
        elif representation == "pool":
            self.phi = Pool()

        # Generation network
        if shared_core:
            self.inference_core = InferenceCore()
            self.generation_core = GenerationCore()
        else:
            self.inference_core = nn.ModuleList(
                [InferenceCore() for _ in range(L)])
            self.generation_core = nn.ModuleList(
                [GenerationCore() for _ in range(L)])

        # Distribution
        self.pi = Prior()
        self.q = Inference()
        self.g = Generation()
Example #6
0
def test_detect_anomalies(make_stubber, monkeypatch, error_code):
    lookoutvision_client = boto3.client('lookoutvision')
    lookoutvision_stubber = make_stubber(lookoutvision_client)
    project_name = 'test-project_name'
    model_version = 'test-model'
    photo = 'test.jpeg'
    content_type = f'image/jpeg'
    image_contents = b'test-contents'
    anomalous = True
    confidence = .5

    monkeypatch.setattr(imghdr, 'what', lambda f: 'jpeg')

    lookoutvision_stubber.stub_detect_anomalies(project_name,
                                                model_version,
                                                content_type,
                                                image_contents,
                                                anomalous,
                                                confidence,
                                                error_code=error_code)

    if error_code is None:
        with patch('builtins.open',
                   mock_open(read_data=image_contents)) as mock_file:
            got_anom, got_con = Inference.detect_anomalies(
                lookoutvision_client, project_name, model_version, photo)
            mock_file.assert_called_with(photo, 'rb')
            assert got_anom == anomalous
            assert got_con == confidence
    else:
        with pytest.raises(ClientError) as exc_info:
            with patch('builtins.open', mock_open(read_data=image_contents)):
                Inference.detect_anomalies(lookoutvision_client, project_name,
                                           model_version, photo)
            assert exc_info.value.response['Error']['Code'] == error_code
def objective(trial):
    start = time.time()
    # pagerank para
    mu = trial.suggest_uniform('mu', 0, 1)
    alpha = trial.suggest_uniform('beta', 0, 0.5)
    kappa1 = trial.suggest_uniform('kappa1', 0, 1)
    kappa2 = trial.suggest_uniform('kappa2', 0, 1)
    kappa3 = trial.suggest_uniform('kappa3', 0, 1)
    kappa = [kappa1, kappa2, kappa3]

    # model para
    embedding_dim = int(
        trial.suggest_discrete_uniform('embedding_dim', 16, 128, 16))
    #alpha = trial.suggest_loguniform('alpha', 1e-6, 1e-2) #SparseTransEの時だけ

    # training para
    lambda_ = trial.suggest_uniform('lambada_', 0, 1)
    batch_size = trial.suggest_int('batch_size', 256, 512, 128)
    lr = trial.suggest_loguniform('lr', 1e-4, 1e-2)
    weight_decay = trial.suggest_loguniform('weight_decay', 1e-6, 1e-2)
    warmup = trial.suggest_int('warmup', 10, 100)
    #lr_decay_every = trial.suggest_int('lr_decay_every', 1, 10)
    lr_decay_every = 2
    lr_decay_rate = trial.suggest_uniform('lr_decay_rate', 0.5, 1)

    data_dir = ['../data_luxury_5core/valid1', '../data_luxury_5core/valid2']
    score_sum = 0
    for i in range(len(data_dir)):

        dataset = AmazonDataset(data_dir[i], model_name='TransE')
        relation_size = len(set(list(dataset.triplet_df['relation'].values)))
        entity_size = len(dataset.entity_list)

        ppr_transe = PPR_TransE(embedding_dim, relation_size, entity_size,
                                data_dir[i], alpha, mu, kappa).to(device)

        iterater = TrainIterater(batch_size=int(batch_size),
                                 data_dir=data_dir[i],
                                 model_name=model_name)

        iterater.iterate_epoch(ppr_transe,
                               lr=lr,
                               epoch=2000,
                               weight_decay=weight_decay,
                               lambda_=lambda_,
                               warmup=warmup,
                               lr_decay_rate=lr_decay_rate,
                               lr_decay_every=lr_decay_every,
                               eval_every=1e+5)

        # inference
        inf = Inference(data_dir[i])
        score = inf.get_score(ppr_transe, kappa, mu, alpha)
        score_sum += score

    mi, sec = time_since(time.time() - start)
    print('{}m{}sec'.format(mi, sec))

    return -1 * score_sum / 2
Example #8
0
def inference():
    global actions
    if selected_filename == 'file_not_selected':
        st.write("### Customer Data")
        return
    inference = Inference(train_data, model, customer)
    actions = inference.churn_prevention(values, percentile)
    actions = inference.financial_impact(actions)
Example #9
0
def start_test(train_iter, dev_iter, test_iter, model, alphabet, config):
    config.logger.info("\nTesting Start......")
    data, path_source, path_result = load_test_data(train_iter, dev_iter,
                                                    test_iter, config)
    infer = Inference(model=model,
                      data=data,
                      path_source=path_source,
                      path_result=path_result,
                      alphabet=alphabet,
                      use_crf=config.use_crf,
                      config=config)
    infer.infer2file()
    config.logger.info("Finished Test.")
Example #10
0
def manual_process(args, net, device: str = 'cpu'):
    # Instatiate inferencing object
    infer = Inference(net, output_dir=args.save, device=device)

    # Performing PIV for left and right images
    stereo_inputs = [
        x[0] for x in os.walk(args.root)
        if os.path.basename(x[0]) != os.path.basename(args.root)
    ]
    for path in stereo_inputs:
        infer.dataloader_parsing(path, pair=False, write=True)

    # Iterate for calculating stereo results
    _flo_process(args)
Example #11
0
def post_behavior():
    actions = request.get_json()
    infer_obj = Inference(actions)
    item_list = infer_obj.inference(actions)
    spark = SparkSession\
            .builder\
            .appName('writeBehavior')\
            .master('local')\
            .getOrCreate()
    sc = SparkContext()
    data = spark.read.json(sc.parallelize(actions))
    data.write.format('com.mongodb.spark.sql.DefaultSource').mode(
        'append').save()
    return json.dumps({"recommendation": str(item_list)})
Example #12
0
def defaultInferenceParser(string, sentenceParser=None):
    '''
    Parses an inference into an Inference object

    @param string - A string representation of an inference rule
    @param sentenceParser - A function that parses the sentences in the inference rule (Defaults to prefixSentenceParser)

    @return - An inference parsed from the string
    '''

    if sentenceParser is None:
        sentenceParser = prefixSentenceParser

    from inference import Inference

    # Split the sting into lines
    lines = string.split('\n')

    # Strip all the lines
    lines = map(lambda a: a.strip(), lines)

    # Remove all blank lines
    lines = filter(lambda a: not len(a) == 0, lines)

    # The name is the first line
    name = lines.pop(0)

    # The conclusion is the last line
    conclusion = sentenceParser(lines.pop())

    # Each other line is a sentence of the premises
    premises = [sentenceParser(i) for i in lines]

    return Inference(name, conclusion, premises)
Example #13
0
    def get_keywords_for_word(self, word):
        infer = Inference(self.word_vectors)

        words_with_distances = []
        for vocab_word, freq in self.skipGram.tokenizer.word_index.items():
            dist = infer.get_cosine_distance(word, vocab_word)
            words_with_distances.append((word, vocab_word, dist))

        words_with_distances = sorted(words_with_distances,
                                      key=lambda tup: tup[2],
                                      reverse=True)

        if len(words_with_distances) >= 20:
            return words_with_distances[:20]
        else:
            return words_with_distances
Example #14
0
def run_tests():
    """Runs a suite of tests for EULA classifier and summarizer models."""

    # Create 5 tests using the test template.
    eulas = [
        '18. Governing Law: This Agreement shall be governed by and interpreted in accordance with the Federal laws of theUnited States, without reference to conflict-of-laws principles. If for any reason a court of competent jurisdiction finds any provision of this Agreement to be unenforceable, that provision will be enforced to the maximum extent possible to effectuate the intent of the parties, and the remainder of this Agreement will continue in full force and effect. This Agreement shall not be governed by the United Nations Convention on Contracts for the International Sale of Goods. Buyer agrees that exclusive jurisdiction for any dispute arising out of or relating to this Agreement lies within the venue mandated by applicable Federal law.\n1.8 Modification. We may modify, update, or discontinue the Services, Software (including any of their portions or features) at any time without liability to you or anyone else. However, we will make reasonable effort to notify you before we make the change. We will also allow you a reasonable time to download your content. If we discontinue a Service in its entirety, then we will provide you with a pro rata refund for any unused fees for that Service that you may have prepaid.',
        'This Agreement and any dispute or claim arising out of or in connection with it or its subject matter, formation, validity, construction, interpretation and performance (including non-contractual disputes or claims) shall be governed by and construed in accordance with the Federal laws of the United States.\nRecourse against the United States for any alleged breach of this agreement must be made under the terms of the Federal Tort Claims Act or as a dispute under the contract disputes clause (Contract Disputes Act) as applicable. The Contractor shall proceed diligently with performance of this contract, pending final resolution of any request for relief, claim, appeal, or action arising under the contract, and comply with any decision of the Contracting Officer.',
        'Successors and Assigns. This Agreement is binding upon and inures to the benefit of the parties hereto and their respective successors and assigns. Neither party may assign or otherwise transfer this Agreement or its rights and duties without the prior written consent of the other party, except that no such consent will\nIf the Customer re-purchases the Services following a Downgrade, accounts that were converted to personal accounts as a result of the Downgrade will need to be re-invited to join the Group, if the Customer desires. However, there is no guarantee that each such user will accept that invitation.',
        'Indemnity. Except to the extent prohibited under applicable law, you agree to defend, indemnify and hold harmless COMPANY and the COMPANY Parties, and their respective successors and assigns, from and against all claims, liabilities, damages, judgments, awards, losses, costs, expenses and fees (including attorneys’ fees) arising out of or relating to (a) your use of, or activities in connection with, the COMANY Services (including Your Submissions); and (b) any violation or alleged violation of this Agreement by you.\nThe Product is not intended for use by persons under the age of eighteen (18). If you are under 18 years old, you may not use this application or provide COMPANY with any personal information.',
        '4.2 Company shall at all times keep confidential and not divulge to anyone, other than authorized employees and representatives of Company and Customer, any confidential information received by Company from Customer in pursuance of this Agreement. Furthermore, Company undertakes to keep confidential any and all vulnerabilities discovered in Customer’s systems and not, without prior written consent of Customer, disclose such vulnerabilities to a third party.\nMarketing. Customer hereby agrees that COMPANY may use, publish, disclose and incorporate in promotional material Customer\'s name and status as a customer of COMPANY, and any portion of any feedback, in any form, including, but not limited to, quotations of written and/or oral commentary relating to the Service and Customer\'s name and/or the name(s) of the individuals quoted, submitted by Customer to COMPANY.'
    ]

    true_ethicality_classifications = [
        'unethical', 'ethical', 'ethical', 'unethical', 'ethical'
    ]

    inference_engine = Inference()

    # Execute all 5 tests.
    for eula, classification in zip(eulas, true_ethicality_classifications):
        test = EULATest(inference_engine, eula, classification)
        try:
            test.run()
        except:
            print('TEST ERROR.')
Example #15
0
def main():
    test_args = arglib.TestArgs()
    args, str_args = test_args.args, test_args.str_args
    os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu

    Writer.set_writer(args.results_dir)

    id_model_path = args.pretrained_models_path.joinpath('vggface2.h5')
    stylegan_G_synthesis_path = str(
        args.pretrained_models_path.joinpath(
            f'stylegan_G_{args.resolution}x{args.resolution}_synthesis'))

    utils.landmarks_model_path = str(
        args.pretrained_models_path.joinpath(
            'shape_predictor_68_face_landmarks.dat'))

    stylegan_G_synthesis = StyleGAN_G_synthesis(
        resolution=args.resolution, is_const_noise=args.const_noise)
    stylegan_G_synthesis.load_weights(stylegan_G_synthesis_path)

    network = Network(args, id_model_path, stylegan_G_synthesis)

    network.test()
    inference = Inference(args, network)
    test_func = getattr(inference, args.test_func)
    test_func()
Example #16
0
 def set_coder(self, filename):
     try:
         self.coder = Autoencoder(settings.BOARD_SHAPE, settings.LATENT_SIZE).to(
             settings.DEVICE
         )
         self.coder.load_state_dict(torch.load(filename, map_location=settings.DEVICE))
         self.coder = self.coder.coder
         self.coder.eval()
         self.coder_launcher = Inference(
             settings.DEVICE,
             self.coder,
         )
         self.coder_set = True
         self.header.coder_label["text"]="Coder Set"
         return True
     except:
         return False
def estimation(dataset,
               boot_index,
               model_params,
               model_params_grad,
               num_obs_samples,
               num_future_steps,
               category_tt_split,
               num_mc_samples,
               output_file,
               true_model_params=None):
    y, x = dataset
    y_complete = y.clone().detach()
    y_complete = y_complete[0:-num_future_steps]
    category_tt_split = 'session'
    y, x, y_future, x_future = train_future_split(y, x, num_future_steps)
    y_train, y_test, test_inds = train_test_split(y.cpu(),
                                                  x.cpu(),
                                                  cat=category_tt_split)
    x = x.clone().detach()  #torch.tensor(x, dtype=dtype, device=device)
    y_train = y_train.clone().detach(
    )  #torch.tensor(y_train, dtype=dtype, device=device)
    y_test = torch.tensor(y_test, dtype=dtype, device=device)
    test_inds = torch.tensor(test_inds, dtype=torch.long, device=device)
    y_future = y_future.clone().detach(
    )  #torch.tensor(y_future, dtype=dtype, device=device)
    x_future = x_future.clone().detach(
    )  #torch.tensor(x_future, dtype=dtype, device=device)

    y_train = torch.tensor(y, device=device)
    data = [y_train, x, y_test, test_inds, y_future, x_future, y_complete]

    model = LearningDynamicsModel(dim=dim)

    boot_output_file = output_file + '/' + str(boot_index)
    os.makedirs(boot_output_file)
    os.makedirs(boot_output_file + '/model_structs')
    os.makedirs(boot_output_file + '/data')
    os.makedirs(boot_output_file + '/plots')

    inference = Inference(
        data=data,
        model=model,
        model_params=model_params,
        model_params_grad=model_params_grad,
        savedir=boot_output_file,
        num_obs_samples=num_obs_samples,
        num_future_steps=num_future_steps,
        num_mc_samples=num_mc_samples,
        ppc_window=50,
        z_true=z_true,
        true_model_params=true_model_params)  # pass in just for figures

    opt_params = inference.run()
    torch.save(opt_params, boot_output_file + '/model_structs/opt_params.npy')
    torch.save(dataset, boot_output_file + '/data/dataset.npy')
    torch.save(model_params,
               boot_output_file + '/model_structs/model_params.npy')
    return opt_params
    def rankConfOnTestSets(self, batch_size):
        inf = Inference(self.AE_model, self.charmap, self.max_len, batch_size)

        scores = []
        paths = glob(self.home_tests)  # 获得路径home_tests('./HOME/TESTs/*.txt')
        for path in paths:
            name = os.path.basename(path).split('-')[0]
            print(name)
            X, F = readPC(path, self.max_len - 1, encoding='ascii')
            rank_result = rank(F)  # 去重并获得排序序号
            rank_result = np.array(rank_result)

            # apply model
            UP = inf.applyBatch(X,
                                INCLUDE_END_SYMBOL=self.hparams['append_end'])
            UP = np.array(UP)
            score = getScore(UP, rank_result)
            scores += [(name, score)]

        return scores
Example #19
0
    def rankConfOnTestSets(self, batch_size):
        inf = Inference(self.f, self.cm, self.max_len, batch_size)

        scores = []
        paths = glob(self.home_tests)
        for path in paths:
            name = os.path.basename(path).split('-')[0]
            print(name)
            X, F = readPC(path, self.max_len - 1, encoding='ascii')
            R = rank(F)
            R = np.array(R)

            # apply model
            UP = inf.applyBatch(X,
                                INCLUDE_END_SYMBOL=self.hparams['append_end'])
            UP = np.array(UP)
            score = getScore(UP, R)
            scores += [(name, score)]

        return scores
Example #20
0
def test_download_from_s3(make_stubber, monkeypatch):
    s3_resource = boto3.resource('s3')
    photo = 's3://doc-example-bucket/test-photo.jpeg'
    file = 'test-photo.jpeg'

    monkeypatch.setattr(
        s3_resource.meta.client, 'download_file',
        lambda Filename, Bucket, Key, ExtraArgs, Callback, Config: None)

    got_file = Inference.download_from_s3(s3_resource, photo)
    assert got_file == file
Example #21
0
    def __init__(self, is_train=True):
        super(retinanet, self).__init__()
        self.is_train = is_train

        self.resnet = resNet()
        self.fpn = PyramidFeatures()
        self.classier = classier()
        self.regressier = regressier()

        self.loss = Loss()
        self.inference = Inference()
Example #22
0
def main():
    args = get_config(opts.config)
    if args is None:
        exit()
    os.environ["CUDA_VISIBLE_DEVICES"] = str(args['GPU_ID'])
    style_dir = args['data']['dir_style']
    content_dir = args['data']['dir_content']
    result_dir = mkdir_output_test(args)

    inference = Inference(args)

    predict_test(inference, result_dir, style_dir, content_dir)
def main():
    # arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('--model_dir', type=str, required=True)
    parser.add_argument('--n_gpus', type=int, default=1)
    parser.add_argument('--checkpoint', type=str, default=None)
    parser.add_argument('--batch_capacity', type=int, default=None)
    parser.add_argument('--beam_size', type=int, default=1)
    args = parser.parse_args()

    sys.path.insert(0, args.model_dir)
    import model_config
    params = model_config.params

    # checkpoint
    checkpoint = args.checkpoint or tf.train.latest_checkpoint(
        args.model_dir + '/log/sup_checkpoint')
    assert checkpoint is not None
    logger.debug('checkpoint', checkpoint)

    # batch_capacity (specified or decided according to n_gpus)
    batch_capacity = args.batch_capacity or 64 * 128 * args.n_gpus

    inference = Inference(model_config,
                          checkpoint=checkpoint,
                          n_gpus=args.n_gpus,
                          batch_capacity=args.batch_capacity)
    inference.make_session()

    sources = sys.stdin.readlines()

    # CONC symbol
    with codecs.open(ROOTP + '/global_config.json') as f:
        gconf = json.load(f)
        CONC = gconf["spm"]["CONC"]

    translations = translate_with_context(sources, inference, CONC,
                                          args.beam_size)
    for line in translations:
        print(line)
Example #24
0
def main(args):
    inference = Inference(args.model)
    inference.load_model()

    input = args.input

    if input == 0:
        input_feeder = InputFeeder('cam', input)
    elif input.endswith('.jpg') or input.endswith('.jpeg') or input.endswith(
            '.bmp'):
        input_feeder = InputFeeder('image', input)
        is_image = True
    else:
        input_feeder = InputFeeder('video', input)

    input_feeder.load_data()

    if is_image:
        outputs = inference.predict(input_feeder.cap)

        inference.preprocess_output(outputs)
        return 0

    frames = 0
    for ret, frame in input_feeder.next_batch():

        if not ret:
            break

        frames += 1

        key = cv2.waitKey(60)
        if key == 27:
            break

        outputs = inference.predict(frame)

        inference.preprocess_output(outputs)

    input_feeder.close()
Example #25
0
    def test_pre_processed_values(self):
        # Initialize y_pred_loc, conf and y_probs
        y_pred_loc = np.zeros([1, 3200 * 4], dtype=np.float32)
        y_pred_conf = np.zeros([1, 3200], dtype=np.float32)
        y_probs = list(y_pred_conf)

        # Read the values from the file
        p = pickle.load(open("t1/train.pkl", "rb"))
        image_name = p[0]["img_name"]
        y_pred_loc = p[0]["y_loc"]
        y_pred_conf = p[0]["y_conf"]

        img = mpimg.imread("/home/ubuntu/tensorflow_ssd/data/images/" +
                           image_name)

        inf = Inference("/home/ubuntu/tensorflow_ssd/data/")
        boxes, confs = inf.convert_coordinates_to_boxes(
            y_pred_loc, y_pred_conf, y_pred_conf)
        for box in boxes:
            debug_draw_boxes(img, box, (255, 0, 0), 1)
        plt.imshow(img)
        plt.show()
Example #26
0
    def test(self, ):
        """
        Test the generator.
        """
        print("\nTesting...\n")

        num_classes = len(self.class_encoding)

        # We are going to use the CrossEntropyLoss loss function as it's most
        # frequently used in classification problems with multiple classes
        # which fits the problem. This criterion  combines LogSoftMax and
        # NLLLoss.
        criterion = nn.CrossEntropyLoss(weight=self.class_weights)

        # Evaluation metric
        ignore_index = list(self.class_encoding).index('unlabeled')
        metric = IoU(num_classes, ignore_index=ignore_index)

        # Test the trained model on the test set
        infer = Inference(self.generator,
                          self.test_loader,
                          criterion,
                          metric,
                          self.device,
                          self.count_save,
                          generate_images=True,
                          color_palette=self.class_encoding)

        print(">>>> Running test dataset")

        loss, (iou, miou) = infer.run_epoch(iteration_loss=True)
        class_iou = dict(zip(self.class_encoding.keys(), iou))

        print(">>>> Avg. loss: {0:.4f} | Mean IoU: {1:.4f}".format(loss, miou))

        # Print per class IoU
        for key, class_iou in zip(self.class_encoding.keys(), iou):
            print("{0}: {1:.4f}".format(key, class_iou))
Example #27
0
def estimation(dataset, num_obs_samples, num_future_steps, num_mc_samples):
    y, x, z_true = dataset
    init_prior = ([0.0] * dim, [math.log(1.0)] * dim)
    init_transition_log_scale = [math.log(1.)]
    model = LearningDynamicsModel(init_prior, init_transition_log_scale, dim=3)
    inference = Inference(data,
                          model,
                          savedir='',
                          num_obs_samples=num_obs_samples,
                          num_future_steps=num_future_steps,
                          num_mc_samples=num_mc_samples,
                          z_true=z_true)
    opt_params = inference.optimize()
    return opt_params
Example #28
0
def get_predicted_result(predict_images, cid_array):
    """
    TODO: Give the predict here.
    :param predict_images: the images array
    :param cid_array: the oriented type, left center or right : 0,1,2
    :return:
    """
    infer = Inference()
    results = infer.predict(predict_images)
    new_results = []
    new_results1 = []
    for i in results:
        i.sort(reverse=True, key=lambda j: float(j.split(":")[1]))
        new_results.append(i[0].split(":")[0])
        new_results1.append(i[0])
    max_results = max_list(new_results)
    sum = 0
    num = new_results.count(max_results)
    for rid in new_results1:
        if rid.split(":")[0] == max_results:
            sum = sum + float(rid.split(":")[1])
    end_result = round(sum / num, 2)
    return end_result, max_results
Example #29
0
def find_similar(fen, num=1, similarity_function=nearest):
    """
    Finds x smallest values ​​in a table.
    :param fen: String with chess position in Fen notation
    :param num: Number of games to be found
    :param similarity_function: Function that measures the similarity of vectors
    :return: list of games with similar positions
    """
    coder = Autoencoder(settings.BOARD_SHAPE, settings.LATENT_SIZE).to(
        settings.DEVICE
    )
    coder.load_state_dict(torch.load(settings.CODER_PATH, map_location=settings.DEVICE))
    coder = coder.coder
    coder.eval()
    inf = Inference(settings.DEVICE, coder)
    target = inf.predict([fen]).tolist()[0]
    conn = sqlite3.connect(settings.DATABASE)
    cursor = conn.cursor()
    cursor.execute("select Embeding FROM positions_lite")
    matrix = cursor.fetchall()
    matrix = [json.loads(x[0])[0] for x in matrix]
    scores = similarity_function(matrix, target)
    idx = find_lowest(scores, num)
    return Games(find_game(idx, cursor))
class FramesClassifier:
    def __init__(self, weights_path="/user/vlongobardi/checkpoint_best.hdf5", time_step=16, overlap=.5):
        self.time_step = time_step
        self.model = SharmaNet((self.time_step, 224, 224, 3), classification=True, weights='afew')  # , dim=-1)
        self.model.load_weights(weights_path)
        self.feature_generator = None
        self.inference = Inference(model=self.model, custom_inference=True, time_step=self.time_step)
        self.overlap = overlap

    # #### LATE FUSION #### #
    def predict(self, path):
        test_gen = DataGenerator(path, '', 1, 31, NoAug(), split_video_len=1, max_invalid=12, test=True)
        self._clear_past_predictions()
        graund_truth, prediction = self.inference.predict_generator(test_gen, mode="overlap")
        if len(graund_truth) == 0:
            print(path)
            return None, None
        return graund_truth[0], prediction[0]

    def _clear_past_predictions(self):
        self.inference.true.clear()
        self.inference.predicted.clear()

    # ###################### #
    # #### EARLY FUSION #### #
    def init_feature_generator(self):
        """ Define a new model to generate features for frames. Obtained removing all layers after the RNN """
        self.feature_generator = Model(self.model.input, self.model.layers[-5].output)
        # self.feature_generator.summary()

    def get_feature(self, path):
        """ Given the CSV clip, generates a List: each entry are a frame features """
        if self.feature_generator is None:
            self.init_feature_generator()

        features = []
        generator = DataGenerator(path, '', 1, 31, NoAug(), split_video_len=1, max_invalid=12, test=True)
        item = {
            'frames': generator[0][0].reshape(generator[0][0].shape[1:]),
            'label': generator[0][1]
        }
        x = item['frames']
        for i in range(0, len(item['frames'])):
            item = x[i]
            item = item[np.newaxis, np.newaxis, ...]
            features.append(self.feature_generator.predict(item))
        return features