def main():
    parser = argparse.ArgumentParser(description='Semantic Segmentation')
    parser.add_argument('--train_cfg',
                        type=str,
                        default='./configs/train_config.yaml',
                        help='train config path')
    args = parser.parse_args()
    config_folder = Path(args.train_cfg.strip("/"))
    config = load_yaml(config_folder)
    init_seed(config['SEED'])

    df, train_ids, valid_ids = split_dataset(config['DATA_TRAIN'])
    train_dataset = getattribute(config=config,
                                 name_package='TRAIN_DATASET',
                                 df=df,
                                 img_ids=train_ids)
    valid_dataset = getattribute(config=config,
                                 name_package='VALID_DATASET',
                                 df=df,
                                 img_ids=valid_ids)
    train_dataloader = getattribute(config=config,
                                    name_package='TRAIN_DATALOADER',
                                    dataset=train_dataset)
    valid_dataloader = getattribute(config=config,
                                    name_package='VALID_DATALOADER',
                                    dataset=valid_dataset)
    model = getattribute(config=config, name_package='MODEL')
    criterion = getattribute(config=config, name_package='CRITERION')
    optimizer = getattribute(config=config,
                             name_package='OPTIMIZER',
                             params=model.parameters())
    scheduler = getattribute(config=config,
                             name_package='SCHEDULER',
                             optimizer=optimizer)
    device = config['DEVICE']
    metric_ftns = [accuracy_dice_score]
    num_epoch = config['NUM_EPOCH']
    gradient_clipping = config['GRADIENT_CLIPPING']
    gradient_accumulation_steps = config['GRADIENT_ACCUMULATION_STEPS']
    early_stopping = config['EARLY_STOPPING']
    validation_frequency = config['VALIDATION_FREQUENCY']
    saved_period = config['SAVED_PERIOD']
    checkpoint_dir = Path(config['CHECKPOINT_DIR'], type(model).__name__)
    checkpoint_dir.mkdir(exist_ok=True, parents=True)
    resume_path = config['RESUME_PATH']
    learning = Learning(model=model,
                        optimizer=optimizer,
                        criterion=criterion,
                        device=device,
                        metric_ftns=metric_ftns,
                        num_epoch=num_epoch,
                        scheduler=scheduler,
                        grad_clipping=gradient_clipping,
                        grad_accumulation_steps=gradient_accumulation_steps,
                        early_stopping=early_stopping,
                        validation_frequency=validation_frequency,
                        save_period=saved_period,
                        checkpoint_dir=checkpoint_dir,
                        resume_path=resume_path)
    learning.train(tqdm(train_dataloader), tqdm(valid_dataloader))
Exemple #2
0
 def __init__(self, total_episodes, time_training_step, time_testing_step):
     self.timestamp = datetime.now().strftime('%Y%m%d_%H_%M_%S')
     # Ínitialize a track instance
     self.Track = Track(self.timestamp)
     # Return the shape of the track
     self.way, self.dims = self.Track.retrieve_way(
     ), self.Track.retrieve_way().shape
     # Initiate the Learning module using the dimensions of the track
     self.Learning = Learning(self.timestamp, self.dims)
     self.total_episodes = total_episodes
     # setup
     self.setup()
Exemple #3
0
    def __init__(self, pomdpfile='program.pomdp'):

        self.time = ['morning', 'afternoon', 'evening']
        self.location = ['classroom', 'library']
        self.identity = ['student', 'professor', 'visitor']
        self.intention = ['interested', 'not_interested']
        self.reason = Reason('reason0.plog')
        self.model = Model(filename='program.pomdp', parsing_print_flag=False)
        self.policy = Policy(5, 4, output='program.policy')
        self.instance = []
        self.results = {}
        self.learning = Learning('./', 'interposx.csv', 'interposy.csv')
        self.trajectory_label = 0
def main():

    codecool_msc = CodecoolClass.create_local()
    late_students = MorningRoutine.random_things(codecool_msc.students)
    JusticeDepartment.random_punishment(late_students)
    print(codecool_msc.avg_energy())
    print(codecool_msc.avg_knowledge())
    Learning.morning_dojo(codecool_msc.students)
    Breaks.random_breaks(codecool_msc.students)
    coder_pairs = Pair.create_pairs(codecool_msc)
    Learning.pair_programming(coder_pairs)
    print(codecool_msc.avg_knowledge())
    Breaks.mass_lunchbreak(codecool_msc)
    NaturalDiasaster.random_disasters(codecool_msc.students)
    print("The day is over and everyone went home, hopefully with more knowledge than at the start of the day.")
Exemple #5
0
 def __init__(self, scope, q, alpha, reward, discount, quotes, bankroll, 
                                                                   log=None):
     self.logger = log
     self.actions = ACTIONS # BUY, SELL, DO_NOTHING
     Indicators.__init__(self, log)
     Learning.__init__(self, q, alpha, reward, discount, self.state, \
                                                                self.actions)
     Order.__init__(self, scope, bankroll, log)
     self.num_trades = 0
     self.performance = 1
     self.volume = max(self.performance, 1)
     self.logger = log
     self.status = {'status':'','action':''}
     self.quotes = quotes
     self.states = None
Exemple #6
0
 def __init__(self,
              scope,
              q,
              alpha,
              reward,
              discount,
              quotes,
              bankroll,
              log=None):
     self.logger = log
     self.scope = scope
     self.actions = ACTIONS
     Indicators.__init__(self, log)
     Order.__init__(self, scope, bankroll, log)
     Learning.__init__(self, q, alpha, reward, discount, self.state, \
                                                                self.actions)
     self.num_trades = 0
     self.performance = 1
     self.volume = max(self.performance, 1)
     self.logger = log
     self.status = {'status': 'idle', 'action': ''}
     self.quotes = quotes
     self.states = None
Exemple #7
0
  def __init__(self,args):

    self.checkpoint_dir = '/data/tf/ckpts'
    self.log_dir = '/data/tf/logs'
    self.result_dir = 'results'

    self._attrs = ['generator','rule','rule_apply', 'order', 'subsample', 'ratio'] #'gan', ,'parse'
    Learning.__init__(self,
        model=args.model, dataset=args.dataset,
        model_path=args.model_path,data_path=args.data_path,
        generator=args.generator, rule=args.rule,
        gan=args.gan, parse=args.parse,
        rule_apply=args.rule_apply,order=args.order,
        subsample=args.subsample, ratio=args.ratio,
        reload=args.reload, beam=args.beam)



    tf.reset_default_graph()
    tf_config = tf.ConfigProto()
    tf_config.gpu_options.allow_growth=True
    tf_config.intra_op_parallelism_threads=15
    tf_config.inter_op_parallelism_threads=15
    self.sess = tf.InteractiveSession(config=tf_config)
Exemple #8
0
class Game:
    """ Loop over multiple episodes to
	train the reinforcement model
	"""
    def __init__(self, total_episodes, time_training_step, time_testing_step):
        self.timestamp = datetime.now().strftime('%Y%m%d_%H_%M_%S')
        # Ínitialize a track instance
        self.Track = Track(self.timestamp)
        # Return the shape of the track
        self.way, self.dims = self.Track.retrieve_way(
        ), self.Track.retrieve_way().shape
        # Initiate the Learning module using the dimensions of the track
        self.Learning = Learning(self.timestamp, self.dims)
        self.total_episodes = total_episodes
        # setup
        self.setup()

    def setup(self):
        """ Setup the logs """
        self.all_episodes_moves = []
        # setup the folders for the logs
        if not os.path.isdir(os.path.join(os.getcwd(), "logs")):
            os.path.mkdir(os.path.join(os.getcwd(), "logs"))
        # Prepare the logging
        log_num_name = "{timestamp}_log_numerics.csv".format(
            timestamp=self.timestamp)
        self.log_num_path = os.path.join(os.getcwd(), "logs", log_num_name)
        plot_name = "{timestamp}_plot.png".format(timestamp=self.timestamp)
        self.plot_path = os.path.join(os.getcwd(), "logs", plot_name)
        moves_name = "all_moves.obj".format(timestamp=self.timestamp)
        self.moves_path = os.path.join(os.getcwd(), "logs", moves_name)
        topology_name = "track_topology.npy".format(timestamp=self.timestamp)
        self.topology_path = os.path.join(os.getcwd(), "logs", topology_name)

        # Save the track condition
        np.save(self.topology_path, np.asarray(self.way))

    def train_model(self):
        """ Run the model a multiple number of times
		to fill the Q matrix
		"""
        for episode in range(self.total_episodes):
            Episode = Game_Episode(self.Learning, self.Track, episode,
                                   self.timestamp, time_training_step)
            episode_moves = Episode.run_train_episode()
            self.all_episodes_moves.append(episode_moves)

    def test_prediction(self):
        """ Using the filled Q matrix, test the learned parameters """
        for episode in range(self.total_episodes, self.total_episodes + 1):
            Episode = Game_Episode(self.Learning, self.Track, episode,
                                   self.timestamp, time_testing_step)
            episode_moves = Episode.run_test_episode()
            self.all_episodes_moves.append(episode_moves)

    def clean_up(self):
        """ Visualize the learning parameters.
		Save the parameters in a separate file
		"""

        # Save the moves for every episode to a file
        with open(self.moves_path, 'wb') as log:
            pickle.dump(self.all_episodes_moves, log)

        # Retrieve the learning parameters from each step performed
        parameters = np.round(np.array(self.Learning.retrieve_l_parameters()),
                              2)
        plt.plot(parameters[:, 0],
                 parameters[:, 2],
                 color='lightblue',
                 linewidth=3)
        plt.title("Q Learning process")
        plt.xlabel("Episode")
        plt.ylabel("Sum of squared Q matrix elements")
        plt.savefig(self.plot_path)
        plt.show()
        # Save the learning parameters explicitly to a file
        with open(self.log_num_path, 'w') as log:
            log.write(str(parameters) + "\n")
                                audio = 5
                            else:
                                audio = 1
                            video = 0
                            camera = 0
                            checkagain = 1
                            counter_silence = 0
                ##############################################################
                elif info == 1:
                    data = ast.literal_eval(data.decode('utf-8'))
                    if menuvariable == 1:
                        machinelearningtext = data
                        conn.sendall(b"FinishLearning.endmes")
                    else:

                        learnpepper = Learning(data)
                        pas, law, saving, swerve = learnpepper.learn()

                        check = checklearning(pas, law, saving, swerve)

                        if check != 'ok':
                            mystring = "LearnMore.endmes" + check
                            string = mystring.encode('utf-8')
                            conn.sendall(string)
                            learn = 2
                            if interactionvariable == 1:
                                audio = 5
                            else:
                                audio = 1
                            info = 0
                        else:
import numpy as np
from environment_explauto.environment import TestEnvironment
from learning import Learning

if __name__ == "__main__":

    print "Create environment"
    environment = TestEnvironment()

    print "Create agent"
    learning = Learning(dict(m_mins=environment.conf.m_mins,
                             m_maxs=environment.conf.m_maxs,
                             s_mins=environment.conf.s_mins,
                             s_maxs=environment.conf.s_maxs),
                        condition="AMB")
    learning.start()

    print
    print "Do 100 autonomous steps:"
    for i in range(100):
        context = environment.get_current_context()
        m = learning.produce(context)
        s = environment.update(m)
        learning.perceive(s)

    print "Do 1 arm demonstration"
    m_demo_traj = np.zeros((25, 4)) + 0.001
    m_demo = environment.torsodemo2m(m_demo_traj)
    print "m_demo", m_demo
    s = environment.update(m_demo)
    learning.perceive(s, m_demo=m_demo)
Exemple #11
0
    spec = prep.pre_transportation(spec)
    spec = prep.pre_land(spec)
    spec = prep.pre_area(spec)
    spec = prep.pre_structure(spec)
    spec = prep.pre_age(spec)
    spec = prep.pre_floor(spec)
    spec = prep.pre_direction(spec)
    spec = prep.pre_mcost(spec)
    spec = prep.pre_rcost(spec)
    spec = prep.encode_cat_to_label(spec)
    spec = prep.pre_outlier(spec)
    spec = prep.del_bid(spec)
    prep.save_spec(spec)

    #learning
    learning = Learning(data_version)
    gs = learning.xgb_learning(spec)
    learning.show_results(gs)
    learning.save_model(gs)
#     spec_all,spec_bad=learning.check_prediction(spec)
#     learning.save_prediction(spec_all)

elif mode == 'inference':
    data_version = datetime.now().strftime("%Y%m%d")
    model_version = input('which model do you use? ex.original...')
    #spec取得
    get_spec = GetSpec(data_version)
    page_num = get_spec.get_page_num()
    urls = get_spec.get_urls(page_num)
    pages = get_spec.get_pages(urls)
    get_spec.save_pages(pages)
def main():
    parser = argparse.ArgumentParser(description='Semantic Segmentation')
    parser.add_argument('--train_cfg',
                        type=str,
                        default='./configs/train.yaml',
                        help='train config path')
    args = parser.parse_args()
    config_folder = Path(args.train_cfg.strip("/"))
    config = load_yaml(config_folder)
    init_seed(config['SEED'])

    image_datasets = {
        x: vinDataset(root_dir=config['ROOT_DIR'],
                      file_name=config['FILE_NAME'],
                      num_triplet=config['NUM_TRIPLET'],
                      phase=x)
        for x in ['train', 'valid']
    }
    dataloaders = {
        x: torch.utils.data.DataLoader(image_datasets[x],
                                       batch_size=config['BATCH_SIZE'],
                                       shuffle=True,
                                       num_workers=4,
                                       pin_memory=True)
        for x in ['train', 'valid']
    }

    model = getattribute(config=config, name_package='MODEL')
    criterion = getattribute(config=config, name_package='CRITERION')
    metric_ftns = [accuracy_score]
    optimizer = getattribute(config=config,
                             name_package='OPTIMIZER',
                             params=model.parameters())
    scheduler = getattribute(config=config,
                             name_package='SCHEDULER',
                             optimizer=optimizer)
    device = config['DEVICE']
    num_epoch = config['NUM_EPOCH']
    gradient_clipping = config['GRADIENT_CLIPPING']
    gradient_accumulation_steps = config['GRADIENT_ACCUMULATION_STEPS']
    early_stopping = config['EARLY_STOPPING']
    validation_frequency = config['VALIDATION_FREQUENCY']
    saved_period = config['SAVED_PERIOD']
    checkpoint_dir = Path(config['CHECKPOINT_DIR'], type(model).__name__)
    checkpoint_dir.mkdir(exist_ok=True, parents=True)
    resume_path = config['RESUME_PATH']
    learning = Learning(model=model,
                        criterion=criterion,
                        metric_ftns=metric_ftns,
                        optimizer=optimizer,
                        device=device,
                        num_epoch=num_epoch,
                        scheduler=scheduler,
                        grad_clipping=gradient_clipping,
                        grad_accumulation_steps=gradient_accumulation_steps,
                        early_stopping=early_stopping,
                        validation_frequency=validation_frequency,
                        save_period=saved_period,
                        checkpoint_dir=checkpoint_dir,
                        resume_path=resume_path)

    learning.train(tqdm(dataloaders['train']), tqdm(dataloaders['valid']))
    ITERATIVE_LEARNING = 1  # Evaluate as iterative learning?
    IMAGE_NAME_COLUMN = 256
    SESSION_COLUMN = 257
    CATEGORY_COLUMN = 258
    INSTANCE_COLUMN = 259
    TRAIN_SESSIONS = [1, 2, 4, 5, 6, 8, 9, 11]
    TEST_SESSIONS = [3, 7, 10]
    DATA_DIMENSION = 256
    FACTOR_FRAMES = 4  # Every Nth frame will be selected. Only 2 and 4 are reasonable values. Original number of
    # frames is 8. In this case it will be reduced to 4 and 2, respectively.

    # ------------------------------------ Initialization --------------------------------------------------------------

    rgwr = GammaGWR()
    utils = Utilities()
    learning = Learning()
    args = utils.parse_arguments()

    # Get data.
    original_data = utils.load_data(args.dataset).values
    original_data_normalized = utils.normalize_data(original_data,
                                                    DATA_DIMENSION)

    train_data = original_data_normalized[np.in1d(
        original_data_normalized[:, SESSION_COLUMN], TRAIN_SESSIONS)]
    train_data = utils.reduce_number_of_frames(train_data, FACTOR_FRAMES)

    test_data = original_data_normalized[np.in1d(
        original_data_normalized[:, SESSION_COLUMN], TEST_SESSIONS)]

    # ------------------------------------ Batch learning---------------------------------------------------------------
Exemple #14
0
import math
import time
import torch
import torch.nn as nn
import torchvision.transforms as t
import torch.nn.functional as F
import torch.optim as optim
import random
import numpy as np
import matplotlib.pyplot as plt
from mario_q import MarioManager
from helpers import Transition
from helpers import ReplayMemory
from helpers import DQN
from learning import Learning

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
em = MarioManager(device)
memory = ReplayMemory(1000000)
policy = DQN(em.screen_height(), em.screen_width()).to(device)
target = DQN(em.screen_height(), em.screen_width()).to(device)
optimizer = optim.Adam(params=policy.parameters(), lr=0.001)
target.load_state_dict(policy.state_dict())
target.eval()

learning_agent = Learning(policy, target, em, memory, optimizer)
learning_agent.learn()
learning_agent.plot_on_figure()
def train_fold(train_config, experiment_folder, pipeline_name, log_dir,
               fold_id, train_dataloader, val_dataloader, binarizer_fn,
               eval_fn):

    fold_logger = helpers.init_logger(log_dir, f'train_log_{fold_id}.log')

    best_checkpoint_folder = Path(experiment_folder,
                                  train_config['CHECKPOINTS']['BEST_FOLDER'])
    best_checkpoint_folder.mkdir(parents=True, exist_ok=True)

    checkpoints_history_folder = Path(
        experiment_folder, train_config['CHECKPOINTS']['FULL_FOLDER'],
        f'fold_{fold_id}')
    checkpoints_history_folder.mkdir(parents=True, exist_ok=True)
    checkpoints_topk = train_config['CHECKPOINTS']['TOPK']

    calculation_name = f'{pipeline_name}_fold_{fold_id}'

    device = train_config['DEVICE']

    module = importlib.import_module(train_config['MODEL']['PY'])
    model_class = getattr(module, train_config['MODEL']['CLASS'])
    model = model_class(**train_config['MODEL']['ARGS'])

    pretrained_model_config = train_config['MODEL'].get('PRETRAINED', False)
    if pretrained_model_config:
        loaded_pipeline_name = pretrained_model_config['PIPELINE_NAME']
        pretrained_model_path = Path(
            pretrained_model_config['PIPELINE_PATH'],
            pretrained_model_config['CHECKPOINTS_FOLDER'],
            f'{loaded_pipeline_name}_fold_{fold_id}.pth')

        if pretrained_model_path.is_file():
            model.load_state_dict(torch.load(pretrained_model_path))
            fold_logger.info(f'Load model from {pretrained_model_path}')

    if len(train_config['DEVICE_LIST']) > 1:
        model = torch.nn.DataParallel(model)

    module = importlib.import_module(train_config['CRITERION']['PY'])
    loss_class = getattr(module, train_config['CRITERION']['CLASS'])
    loss_fn = loss_class(**train_config['CRITERION']['ARGS'])

    optimizer_class = getattr(torch.optim, train_config['OPTIMIZER']['CLASS'])
    optimizer = optimizer_class(model.parameters(),
                                **train_config['OPTIMIZER']['ARGS'])
    scheduler_class = getattr(torch.optim.lr_scheduler,
                              train_config['SCHEDULER']['CLASS'])
    scheduler = scheduler_class(optimizer, **train_config['SCHEDULER']['ARGS'])

    n_epochs = train_config['EPOCHS']
    grad_clip = train_config['GRADIENT_CLIPPING']
    grad_accum = train_config['GRADIENT_ACCUMULATION_STEPS']
    early_stopping = train_config['EARLY_STOPPING']
    validation_frequency = train_config.get('VALIDATION_FREQUENCY', 1)

    freeze_model = train_config['MODEL']['FREEZE']

    Learning(optimizer, binarizer_fn, loss_fn, eval_fn, device, n_epochs,
             scheduler, freeze_model, grad_clip, grad_accum, early_stopping,
             validation_frequency, calculation_name, best_checkpoint_folder,
             checkpoints_history_folder, checkpoints_topk,
             fold_logger).run_train(model, train_dataloader, val_dataloader)
Exemple #16
0
class Simulator:
    def __init__(self, pomdpfile='program.pomdp'):

        self.time = ['morning', 'afternoon', 'evening']
        self.location = ['classroom', 'library']
        self.identity = ['student', 'professor', 'visitor']
        self.intention = ['interested', 'not_interested']
        self.reason = Reason('reason0.plog')
        self.model = Model(filename='program.pomdp', parsing_print_flag=False)
        self.policy = Policy(5, 4, output='program.policy')
        self.instance = []
        self.results = {}
        self.learning = Learning('./', 'interposx.csv', 'interposy.csv')
        self.trajectory_label = 0

    def sample(self, alist, distribution):

        return np.random.choice(alist, p=distribution)

    def create_instance(self, i):
        random.seed(i)

        #person = random.choice(self.identity)
        person = 'visitor'
        #print ('\nIdentity (uniform sampling): [student, visitor, professor] : '), person
        #		print ('identity is:'), person
        if person == 'student':
            #place ='classroom'
            place = self.sample(self.location, [0.7, 0.3])
            #time ='evening'
            #time =self.sample(self.time,[0.15,0.15,0.7])
            #intention =self.sample(self.intention,[0.3,0.7])
            #intention = 'not_interested'
        elif person == 'professor':
            place = self.sample(self.location, [0.9, 0.1])
            time = self.sample(self.time, [0.8, 0.1, 0.1])
            intention = self.sample(self.intention, [0.1, 0.9])
        else:
            #place = self.sample(self.location,[0.2,0.8])
            place = 'classroom'
            #time =self.sample(self.time,[0.1,0.7,0.2])
            time = 'afternoon'
            intention = self.sample(self.intention, [0.8, 0.2])
            intention = 'interested'

        self.trajectory_label = self.learning.get_traj(intention)

        print('Sampling time, location and intention for the identity: ' +
              person)

        self.instance.append(person)
        self.instance.append(time)  #1
        self.instance.append(place)  #2
        self.instance.append(intention)
        self.instance.append('trajectory with label ' +
                             str(self.trajectory_label))
        print('Instance: ')
        print(self.instance[0], self.instance[1], self.instance[2],
              self.instance[3], self.instance[4])
        return self.instance

    def observe_fact(self, i):
        random.seed(i)
        #print '\nObservations:'
        time = self.instance[1]
        location = self.instance[2]
        #print ('Observed time: '),time
        #print ('Observed location: '),location
        return time, location

    def init_belief(self, int_prob):

        l = len(self.model.states)
        b = np.zeros(l)

        # initialize the beliefs of the states with index=0 evenly

        int_prob = float(int_prob)
        init_belief = [0, 0, 1.0 - int_prob, int_prob, 0]
        b = np.zeros(len(self.model.states))
        for i in range(len(self.model.states)):
            b[i] = init_belief[i] / sum(init_belief)
        print 'The normalized initial belief would be: '
        print b
        return b

    def get_state_index(self, state):

        return self.model.states.index(state)

    def init_state(self):
        state = random.choice(
            ['not_turned_not_interested', 'not_turned_interested'])
        #print '\nRandomly selected state from [not_forward_not_interested,not_forward_interested] =',state
        s_idx = self.get_state_index(state)
        #print s_idx
        return s_idx, state

    def get_obs_index(self, obs):

        return self.model.observations.index(obs)

    def observe(self, a_idx, intention, pln_obs_acc):

        p = pln_obs_acc
        if self.model.actions[
                a_idx] == 'move_forward' and intention == 'interested':
            #obs='physical'
            obs = self.sample(['pos', 'neg'], [p, 1 - p])
        elif self.model.actions[
                a_idx] == 'move_forward' and intention == 'not_interested':
            obs = obs = self.sample(['pos', 'neg'], [1 - p, p])
        elif self.model.actions[a_idx] == 'greet' and intention == 'interested':
            #obs = 'verbal'
            obs = self.sample(['pos', 'neg'], [p, 1 - p])
        elif self.model.actions[
                a_idx] == 'greet' and intention == 'not_interested':
            obs = self.sample(['pos', 'neg'], [1 - p, p])
        elif self.model.actions[a_idx] == 'turn' and intention == 'interested':
            #obs = 'verbal'
            obs = self.sample(['pos', 'neg'], [p, 1 - p])
        elif self.model.actions[
                a_idx] == 'turn' and intention == 'not_interested':
            obs = self.sample(['pos', 'neg'], [1 - p, p])
        else:
            obs = 'na'
        #l=len(self.model.observations)-1
        #o_idx=randint(0,l)
        o_idx = self.get_obs_index(obs)
        print('random observation is: ', self.model.observations[o_idx])
        return o_idx

    def update(self, a_idx, o_idx, b):
        b = np.dot(b, self.model.trans_mat[a_idx, :])

        b = [
            b[i] * self.model.obs_mat[a_idx, i, o_idx]
            for i in range(len(self.model.states))
        ]

        b = b / sum(b)
        return b

    def run(self, strategy, time, location, r_thresh, l_thresh, pln_obs_acc):
        a_cnt = 0
        success = 0
        tp = 0
        tn = 0
        fp = 0
        fn = 0
        cost = 0
        R = 0

        if strategy == 'corpp':
            #prob = self.reason.query_nolstm(time, location,'reason_nolstm.plog')
            prob = self.reason.query_nolstm(time, location, 'reason0.plog')
            print colored('\nSTRATEGY: ', 'red'), colored(strategy, 'red')
            print '\nOur POMDP Model states are: '
            print self.model.states

            s_idx, temp = self.init_state()
            b = self.init_belief(prob)

            #print ( 'b shape is,', b.shape )
            #print b

            while True:
                a_idx = self.policy.select_action(b)
                a = self.model.actions[a_idx]
                a_cnt = a_cnt + 1
                if a_cnt > 20:
                    print('POLICY IS NOT REPORTING UNDER 20 ACTIONS')
                    sys.exit()
                print('action selected', a)

                o_idx = self.observe(a_idx, self.instance[3], pln_obs_acc)
                #print ('transition matrix shape is', self.model.trans_mat.shape)
                #print self.model.trans_mat[a_idx,:,:]
                #print ('observation matrix shape is', self.model.obs_mat.shape)
                #print self.model.trans_mat[a_idx,:,:]
                #print s_idx
                R = R + self.model.reward_mat[a_idx, s_idx]
                print 'Reward is : ', cost
                #print ('Total reward is,' , cost)
                b = self.update(a_idx, o_idx, b)
                print b

                if 'report' in a:
                    if 'not_interested' in a and 'not_interested' == self.instance[
                            3]:
                        success = 1
                        tn = 1
                        print 'Trial was successfull'
                    elif 'report_interested' in a and 'interested' == self.instance[
                            3]:
                        success = 1
                        tp = 1
                        print 'Trial was successful'
                    elif 'report_interested' in a and 'not_interested' == self.instance[
                            3]:
                        fp = 1
                        print 'Trial was unsuccessful'
                    elif 'not_interested' in a and 'interested' == self.instance[
                            3]:
                        fn = 1

                    print('Finished\n ')

                    break
                cost = cost + self.model.reward_mat[a_idx, s_idx]
                print 'cost is : ', cost

        if strategy == 'planning':
            #prob = self.reason.query_nolstm(time, location,'reason_nolstm.plog')
            print colored('\nSTRATEGY', 'green'), colored(strategy, 'green')
            print '\nOur POMDP Model states are: '
            print self.model.states

            s_idx, temp = self.init_state()
            #init_belief = [0.25, 0.25, 0.25, 0.25, 0]
            #b = np.zeros(len(init_belief))
            b = np.ones(len(self.model.states))
            for i in range(len(self.model.states)):
                b[i] = b[i] / len(self.model.states)
            print 'initial belief', b
            while True:
                a_idx = self.policy.select_action(b)
                a = self.model.actions[a_idx]

                print('action selected', a)

                o_idx = self.observe(a_idx, self.instance[3], pln_obs_acc)
                R = R + self.model.reward_mat[a_idx, s_idx]
                print 'R is : ', cost
                #print ('Total reward is,' , cost)
                b = self.update(a_idx, o_idx, b)
                print b

                if 'report' in a:
                    if 'not_interested' in a and 'not_interested' == self.instance[
                            3]:
                        success = 1
                        tn = 1
                        print 'Trial was successfull'
                    elif 'report_interested' in a and 'interested' == self.instance[
                            3]:
                        success = 1
                        tp = 1
                        print 'Trial was successful'
                    elif 'report_interested' in a and 'not_interested' == self.instance[
                            3]:
                        fp = 1
                        print 'Trial was unsuccessful'
                    elif 'not_interested' in a and 'interested' == self.instance[
                            3]:
                        fn = 1

                    print('Finished\n ')

                    break

                cost = cost + self.model.reward_mat[a_idx, s_idx]
                print 'cost is : ', cost

        if strategy == 'reasoning':
            print colored('\nSTRATEGY is: ',
                          'yellow'), colored(strategy, 'yellow')
            #prob = self.reason.query_nolstm(time, location,'reason_nolstm.plog')			prob = self.reason.query_nolstm(time, location,'reason_nolstm.plog')
            prob = self.reason.query_nolstm(time, location, 'reason0.plog')

            print('P(ineterested)| observed time and location: '), prob

            prob = float(prob)
            if prob >= r_thresh and 'interested' == self.instance[3]:
                success = 1
                print('Greater than threshold =' + str(r_thresh) +
                      ', -> human IS interested')
                print 'Trial was successful'
                tp = 1
            elif prob >= r_thresh and 'not_interested' == self.instance[3]:
                success = 0
                fp = 1
                print('Greater than threshold =' + str(r_thresh) +
                      ', -> human IS interested')
                print 'Trial was unsuccessful'
            elif prob < r_thresh and 'interested' == self.instance[3]:
                success = 0
                print('Less than threshold =' + str(r_thresh) +
                      ', -> human IS NOT interested')
                fn = 1
                print 'Trial was unsuccessful'
            else:
                success = 1
                print('Less than threshold =' + str(r_thresh) +
                      ', -> human IS NOT interested')
                print 'Trial was successful'
                tn = 1

        if strategy == 'learning':

            print colored('\nStrategy is: ', 'blue'), colored(strategy, 'blue')
            res = self.learning.predict()

            if res > l_thresh and self.trajectory_label == 1.0:
                print('CASE I the trajectory shows person is interested')
                success = 1
                tp = 1
            elif res < l_thresh and self.trajectory_label == 0:

                print('CASE II the person is not interested')
                success = 1
                tn = 1
            elif res > l_thresh and self.trajectory_label == 0:
                sucess = 0
                fp = 1
                print('CASE III the trajectory shows person is interested')
            elif res < l_thresh and self.trajectory_label == 1.0:
                fn = 1
                success = 0
                ('CASE IV the person is not interested')

        if strategy == 'lstm-corpp':
            print colored('\nSTRATEGY is: ',
                          'magenta'), colored(strategy, 'magenta')
            res = self.learning.predict()
            #do not change 0.2 below
            if res > l_thresh:
                #prob = self.reason.query(time, location,'one','reason.plog')
                prob = self.reason.query(time, location, 'one', 'reason0.plog')
            else:
                prob = self.reason.query(time, location, 'zero',
                                         'reason0.plog')
            print '\nOur POMDP Model states are: '
            print self.model.states

            s_idx, temp = self.init_state()
            b = self.init_belief(prob)

            while True:
                a_idx = self.policy.select_action(b)
                a = self.model.actions[a_idx]

                print('action selected', a)

                o_idx = self.observe(a_idx, self.instance[3], pln_obs_acc)
                R = R + self.model.reward_mat[a_idx, s_idx]
                print 'R is : ', cost
                #print ('Total reward is,' , cost)
                b = self.update(a_idx, o_idx, b)
                print b

                if 'report' in a:
                    if 'not_interested' in a and 'not_interested' == self.instance[
                            3]:
                        success = 1
                        tn = 1
                        print 'Trial was successfull'
                    elif 'report_interested' in a and 'interested' == self.instance[
                            3]:
                        success = 1
                        tp = 1
                        print 'Trial was successful'
                    elif 'report_interested' in a and 'not_interested' == self.instance[
                            3]:
                        fp = 1
                        print 'Trial was unsuccessful'
                    elif 'not_interested' in a and 'interested' == self.instance[
                            3]:
                        fn = 1

                    print('Finished\n ')

                    break
                cost = cost + self.model.reward_mat[a_idx, s_idx]
                print 'cost is : ', cost

        if strategy == 'lreasoning':
            print colored('\nStrategy is: learning + reasoning ', 'cyan')
            res = self.learning.predict()
            if res > l_thresh:
                prob = self.reason.query(time, location, 'one', 'reason0.plog')
            else:
                prob = self.reason.query(time, location, 'zero',
                                         'reason0.plog')
            lr_thresh = r_thresh
            prob = float(prob)
            if prob >= r_thresh and 'interested' == self.instance[3]:
                success = 1
                print('Greater than threshold =' + str(r_thresh) +
                      ', -> human IS interested')
                print 'Trial was successful'
                tp = 1
            elif prob >= r_thresh and 'not_interested' == self.instance[3]:
                success = 0
                fp = 1
                print('Greater than threshold =' + str(r_thresh) +
                      ', -> human IS interested')
                print 'Trial was unsuccessful'
            elif prob < r_thresh and 'interested' == self.instance[3]:
                success = 0
                print('Less than threshold =' + str(r_thresh) +
                      ', -> human IS NOT interested')
                fn = 1
                print 'Trial was unsuccessful'
            else:
                success = 1
                print('Less than threshold =' + str(r_thresh) +
                      ', -> human IS NOT interested')
                print 'Trial was successful'
                tn = 1

        return cost, success, tp, tn, fp, fn, R

    def trial_num(self, num, strategylist, r_thresh, l_thresh, pln_obs_acc):
        df = pd.DataFrame()

        total_success = {}
        total_cost = {}
        total_tp = {}
        total_tn = {}
        total_fp = {}
        total_fn = {}
        prec = {}
        recall = {}
        SDcost = {}
        reward = {}
        SDreward = {}

        for strategy in strategylist:
            total_success[strategy] = 0
            total_cost[strategy] = 0
            total_tp[strategy] = 0
            total_tn[strategy] = 0
            total_fp[strategy] = 0
            total_fn[strategy] = 0
            prec[strategy] = 0
            recall[strategy] = 0
            reward[strategy] = 0
            SDreward[strategy] = []
            SDcost[strategy] = []
        for i in range(num):
            print colored('######TRIAL:',
                          'blue'), colored(i,
                                           'red'), colored('#######', 'blue')

            del self.instance[:]
            self.create_instance(i)
            time, location = self.observe_fact(i)

            for strategy in strategylist:

                c, s, tp, tn, fp, fn, R = self.run(strategy, time, location,
                                                   r_thresh, l_thresh,
                                                   pln_obs_acc)

                reward[strategy] += R
                total_cost[strategy] += c
                total_success[strategy] += s
                total_tp[strategy] += tp
                total_tn[strategy] += tn
                total_fp[strategy] += fp
                total_fn[strategy] += fn
                SDcost[strategy].append(c)
                SDreward[strategy].append(R)
                #		print ('total_tp:'), total_tp
                #		print ('total_tn:'), total_tn
                #		print ('total_fp:'), total_fp
                #		print ('total_fn:'), total_fn
                try:
                    df.at[strategy, 'Reward'] = float(reward[strategy]) / num
                    df.at[strategy,
                          'SDCost'] = statistics.stdev(SDcost[strategy])
                    df.at[strategy,
                          'SDreward'] = statistics.stdev(SDreward[strategy])
                    df.at[strategy, 'Cost'] = float(total_cost[strategy]) / num
                    df.at[strategy,
                          'Success'] = float(total_success[strategy]) / num
                    prec[strategy] = round(
                        float(total_tp[strategy]) /
                        (total_tp[strategy] + total_fp[strategy]), 2)
                    recall[strategy] = round(
                        float(total_tp[strategy]) /
                        (total_tp[strategy] + total_fn[strategy]), 2)
                    df.at[strategy, 'Precision'] = prec[strategy]
                    df.at[strategy, 'Recall'] = recall[strategy]
                    df.at[strategy, 'F1 Score'] = round(
                        2 * prec[strategy] * recall[strategy] /
                        (prec[strategy] + recall[strategy]), 2)

                except:
                    print 'Can not divide by zero'
                    df.at[strategy, 'Precision'] = 0
                    df.at[strategy, 'Recall'] = 0
                    df.at[strategy, 'F1 Score'] = 0

        #print 'fp',total_fp['learning']
        #print 'fn',total_fn['learning']

        #self.instance =[]
        return df

    def print_results(self, df):
        print '\nWRAP UP OF RESULTS:'
        print df
Exemple #17
0
	filename = "supporter.cfg"
	config = configparser.SafeConfigParser()
	config.read(filename)

	jid = config.get("xmpp", "jid")
	password = config.get("xmpp", "password")
	room = config.get("xmpp", "room")
	nick = config.get("xmpp", "nick")

	logging.basicConfig(level=logging.INFO,
		                        format='%(levelname)-8s %(message)s')

	words = config.get("brain", "words")
	synonyms = config.get("brain", "synonyms")
	thoughts = config.get("brain", "thoughts")
	messages = config.get("brain", "messages")
	state = config.get("brain", "state")
	brain = Learning(words, synonyms, thoughts, messages, state)

	xmpp = Client(jid, password, room, nick)
	xmpp.register_plugin('xep_0030') # Service Discovery
	xmpp.register_plugin('xep_0045') # Multi-User Chat
	xmpp.register_plugin('xep_0199') # XMPP Ping
	def do_brain(nick, msg, **keywords):
		brain(msg, nick, xmpp.muc_send)
	xmpp.add_message_listener(do_brain)
	if xmpp.connect():
		xmpp.process(block=True)
	else:
		print("Unable to connect")
Exemple #18
0
    NUMBER_FRAMES = 75  # The number of frames per session. 4 sessions make 300 (75 * 4) session for an object.
    CATEGORY_COLUMN = 256
    INSTANCE_COLUMN = 257
    SESSION_COLUMN = 258
    DAY_COLUMN = 259
    CAMERA_COLUMN = 260
    IMAGE_NAME_COLUMN = 261
    DATA_DIMENSION = 256
    FACTOR_FRAMES = 2  # Every Nth frame will be selected. Only 2 and 4 are reasonable values. Original number of
    # frames is 8. In this case it will be reduced to 4 and 2, respectively.

    # ------------------------------------ Initialization --------------------------------------------------------------

    rgwr = GammaGWR()
    utils = Utilities()
    learning = Learning()
    args = utils.parse_arguments()

    # Get data.
    original_data = utils.load_data(args.dataset).values
    original_data_normalized = utils.normalize_data(original_data,
                                                    DATA_DIMENSION)

    original_data_day_one = original_data_normalized[np.in1d(
        original_data_normalized[:, DAY_COLUMN], ONE_DAY)]
    original_data_left_camera = original_data_day_one[np.in1d(
        original_data_day_one[:, CAMERA_COLUMN], CAMERA)]
    selected_data = original_data_left_camera[np.in1d(
        original_data_left_camera[:, CATEGORY_COLUMN], CATEGORIES)]

    # Comment if categorization instead of identification to use. For the rest of the evaluation the CATEGORY column
def main():
    parser = argparse.ArgumentParser(description='Pytorch parser')
    parser.add_argument('--train_cfg',
                        type=str,
                        default='./configs/efficientdet-d0.yaml',
                        help='train config path')
    parser.add_argument('-d',
                        '--device',
                        default=None,
                        type=str,
                        help='indices of GPUs to enable (default: all)')
    parser.add_argument('-r',
                        '--resume',
                        default=None,
                        type=str,
                        help='path to latest checkpoint (default: None)')

    CustomArgs = collections.namedtuple('CustomArgs', 'flags type target')
    options = [
        CustomArgs(['-lr', '--learning_rate'],
                   type=float,
                   target='OPTIMIZER,ARGS,lr'),
        CustomArgs(
            ['-bs', '--batch_size'],
            type=int,
            target=
            'TRAIN_DATALOADER,ARGS,batch_size;VALID_DATALOADER,ARGS,batch_size'
        )
    ]
    config = config_parser(parser, options)
    init_seed(config['SEED'])
    train_dataset = VOCDetection(root=VOC_ROOT,
                                 transform=SSDAugmentation(
                                     voc['min_dim'], MEANS))

    train_dataloader = getattribute(config=config,
                                    name_package='TRAIN_DATALOADER',
                                    dataset=train_dataset,
                                    collate_fn=detection_collate)
    # valid_dataloader = getattribute(config = config, name_package = 'VALID_DATALOADER', dataset = valid_dataset)
    model = getattribute(config=config, name_package='MODEL')
    criterion = getattribute(config=config, name_package='CRITERION')
    optimizer = getattribute(config=config,
                             name_package='OPTIMIZER',
                             params=model.parameters())
    scheduler = getattribute(config=config,
                             name_package='SCHEDULER',
                             optimizer=optimizer)
    device = config['DEVICE']
    metric_ftns = []
    num_epoch = config['NUM_EPOCH']
    gradient_clipping = config['GRADIENT_CLIPPING']
    gradient_accumulation_steps = config['GRADIENT_ACCUMULATION_STEPS']
    early_stopping = config['EARLY_STOPPING']
    validation_frequency = config['VALIDATION_FREQUENCY']
    tensorboard = config['TENSORBOARD']
    checkpoint_dir = Path(config['CHECKPOINT_DIR'], type(model).__name__)
    checkpoint_dir.mkdir(exist_ok=True, parents=True)
    resume_path = config['RESUME_PATH']
    learning = Learning(model=model,
                        criterion=criterion,
                        optimizer=optimizer,
                        scheduler=scheduler,
                        metric_ftns=metric_ftns,
                        device=device,
                        num_epoch=num_epoch,
                        grad_clipping=gradient_clipping,
                        grad_accumulation_steps=gradient_accumulation_steps,
                        early_stopping=early_stopping,
                        validation_frequency=validation_frequency,
                        tensorboard=tensorboard,
                        checkpoint_dir=checkpoint_dir,
                        resume_path=resume_path)

    learning.train(tqdm(train_dataloader))