예제 #1
0
    def __init__(self):
        log_dir = os.path.join(util.default_root_dir(), self.LOG_DIR)
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)
        LOG_FILE = os.path.join(log_dir, '{}.log'.format(__file__))
        self.log = logger.get_logger(__file__, LOG_FILE)

        Frame.__init__(self, name='camaro_gen_tool')
        self.master.title('Camaro Generation File Tool')
        self.pack(expand=Y, fill=BOTH)

        self.root_dir = os.path.abspath(
            os.path.join(os.getcwd(), self.ROOT_DIR))
        self.ftp_popup = None

        # Visual separation in the log
        self.log.info('Log Start')

        # File dialogs
        self.ifile_var = StringVar()
        self.pfile_var = StringVar()
        self.do_erase = IntVar()
        self.do_copy_original = IntVar()
        self.do_copy_pdf = IntVar()
        self.size0 = IntVar()
        self.size1 = IntVar()
        self.size2t = IntVar()
        self.size2 = IntVar()

        self.size0.set(50)
        self.size1.set(100)
        self.size2t.set(150)
        self.size2.set(500)

        self.session = None
        self.server = StringVar()
        self.user = StringVar()
        self.pwd = StringVar()
        self.server.set('')
        self.user.set('')
        self.pwd.set('')
        self.thumbnail_listbox = None

        self.err_count = 0

        self.img_list_file = ''
        self.product_list_file = ''

        # Create input folders if not already there
        dirs = []
        root_dir = os.path.abspath(
            os.path.join(os.getcwd(), ScrapeImages.ROOT_DIR))
        dirs.append(os.path.join(root_dir, ScrapeImages.INPUT_DIR))
        dirs.append(os.path.join(root_dir, ReorderCsv.INPUT_DIR))
        dirs.append(os.path.join(root_dir, ScrapeImages.IMG_LIST_DIR))
        for dir in dirs:
            util.make_sure_path_exists(dir)

        # Create UI
        self.create_panel()
예제 #2
0
파일: bgmm.py 프로젝트: bbaldino/BGMM
def oauth_submit():
    # Get the oauth_key from the page form
    oauth_key = request.forms.get('oauth_key')
    try:
        credentials = oauth2_flow.step2_exchange(oauth_key)
    except Exception as e:
        return "Error with login: %s" % e
    else:
        # Get the user's email and add it to the session
        email = get_email(credentials.access_token)
        # Store the oauth credentials
        oauth_path = DirInfo.get_oauth_file_path(email)
        util.make_sure_path_exists(os.path.dirname(oauth_path))
        storage = oauth2client.file.Storage(oauth_path)
        storage.put(credentials)

        # Create the user instance and have it login/initialize
        user = User(email, DirInfo.BaseAppDataDir)
        users[email] = user
        if not user.init(credentials):
            return "Error with login, incorrect code?"
        else:
            global logged_in
            logged_in = True
            session = get_session()
            session["email"] = email
            redirect("/main")
예제 #3
0
파일: bgmm.py 프로젝트: bbaldino/BGMM
def main():
    logger.info("Starting google music uploader")
    pidfile = None
    if len(sys.argv) > 1:
        if sys.argv[1] == "--pidfile":
            if len(sys.argv) < 3:
                logger.error("Missing pidfile path")
                return
            pidfile = sys.argv[2]

    if pidfile:
        if not util.make_sure_path_exists(os.path.dirname(pidfile)):
            logger.warning("Error creating pidfile directory %s" % os.path.dirname(pidfile))
            return
        with open(pidfile, "w+") as f:
            logger.debug("Writing pidfile to %s" % pidfile)
            f.write(str(os.getpid()))
    config = util.read_config(DirInfo.AppConfig)

    session_opts = {
        'session.type': 'memory',
        'session.auto': 'true'
    }
    app = SessionMiddleware(bottle.app(), session_opts)

    run(app=app, host='0.0.0.0', port=config['PORT'], debug=True)
예제 #4
0
파일: main.py 프로젝트: dwysocki/GravLens
def get_args():
    parser = ArgumentParser(prog="GravLens")

    io = parser.add_argument_group("IO")
    val = parser.add_argument_group("Values")

    io.add_argument("-o", "--output",
        required=True,
        help="Output directory.")

    val.add_argument("--radius", type=float,
        default=2.2,
        help="Radius of galaxy cluster in Mpc (default 2.2)")
    val.add_argument("--dist-lens", type=float,
        default=54.0,
        help="Distance to galaxy cluster in Mpc (default 54.0)")
    val.add_argument("--dist-source", type=float,
        default=10.0,
        help="Distance to source in units of distance to lens (default 10.0)")
    val.add_argument("--angle-source", type=float,
        default=100.0,
        help="Angle between source and lens in units of inverse angular "
             "radius of lens (default 10.0)")
    val.add_argument("--mass", type=float,
        default=1.2e15,
        help="Mass of galaxy cluster in Msun (default 1.2e15)")
    val.add_argument("--profile-index", type=float,
        default=0.1,
        help="Index on mass profile, i.e. 'γ' (default 1.0)")
    val.add_argument("--scaling-radius", type=float,
        default=1e-2,
        help="Scaling radius on mass profile, i.e. 'a' (default 1e-10)")
    val.add_argument("--frequencies", type=float, nargs="+",
        default=[0.9, 1.0, 1.1],
        help="List of photon frequencies in GHz (default 0.9, 1.0, 1.1)")
    val.add_argument("--fraction-plasma", type=float,
        default=0.10,
        help="Fraction of total mass in plasma (default 0.10)")

    args = parser.parse_args()

    args.dist_source *= args.dist_lens
    args.frequencies = np.multiply(1e9, args.frequencies)

    make_sure_path_exists(args.output)

    return args
예제 #5
0
def extract(datalist, like, bbox, dst_crs, out_path):
    """Extract/reproject data within lat/lon bbox
    """
    # check that enough options are provided
    if not like and not bbox:
        util.error("Provide bounds as either a 'like' dataset or a bbox")
    # read input csv listing layers
    layers = [s for s in csv.DictReader(open(datalist, 'rb'))]
    # parse bbox
    if bbox:
        bbox = tuple(map(float, bbox.split(',')))
    # derive bbox/crs from aoi/like layer (ignoring any provided bbox)
    if like:
        bbox = util.get_bbox(like)
        dst_crs = util.get_crs(like)
    # parse provided epsg code
    elif dst_crs:
        dst_crs = from_epsg(dst_crs.split(':')[1])
    # name is derived from config file name
    if not out_path:
        b = os.path.basename(datalist)
        out_path = os.path.join(os.getcwd(), os.path.splitext(b)[0])
    util.make_sure_path_exists(out_path)
    # process each layer
    for layer in layers:
        click.echo('Extracting %s' % layer['name'])
        if util.describe(layer['path'])['type'] == 'VECTOR':
            util.bbox_copy(layer['path'],
                           os.path.join(out_path, layer['name'] + ".shp"),
                           bbox,
                           in_layer=layer['layer'],
                           dst_crs=dst_crs)
        elif util.describe(layer['path'])['type'] == 'RASTER':
            util.bbox_copyraster(layer['path'],
                                 os.path.join(out_path,
                                              layer['name'] + '.tif'),
                                 bbox,
                                 dst_crs=dst_crs)
예제 #6
0
def close_current_assignment(config, current_station):
	"""
	Zatvara trenutno otvoreni studentski zadatak

	config - globalna konfiguracija alata za pregled
	current_station - oznaka trenutno selektovanog računara
	"""
	print 'Zatvaranje studentskog zadatka {0}, posto se prelazi na drugi...'.format(current_station)

	onlyfiles = [f for f in os.listdir(config.CURRENT_ALT_ASSIGNMENT_PATH)
                 if path.isfile(join(config.CURRENT_ALT_ASSIGNMENT_PATH, f))]
	if len(onlyfiles) > 0:
		print('Postoji alternativna verzija zadatka, pa se ona kopira u {0}'.format(config.ALTERED_ASSIGNMENTS_PATH))

		altered = join(config.ALTERED_ASSIGNMENTS_PATH, current_station)
		util.make_sure_path_exists(altered)

		logging.info('Detektovano je da postoji alternativna varijanta zadatka, koja je kopirana u: {0}'
                     .format(altered))

		print('Kopiranje fajlova iz "{0}" u "{1}"'.format(config.CURRENT_ALT_ASSIGNMENT_PATH, altered))
		for f in onlyfiles:
			copyfile(join(config.CURRENT_ALT_ASSIGNMENT_PATH, f), join(altered, f))
예제 #7
0
    def post(self):

        # validate data first
        if not self.validate_post_data():
            return

        gopca_config = self.get_gopca_config_from_post_data()
        #gopca_config.validate()
        # write config to a ini file

        # generate job ID
        job_id = ''
        while not job_id:
            job_id = self.generate_session_id()

            if job_id in self.jobs:
                # very unlikely
                job_id = ''

        #self.set_secure_cookie('session_id',session_id)
        #self.write(session_id)

        #self.logger.debug('Value of disable_local: %s', self.get_body_argument('disable_local',default=0))
        #self.logger.debug('Value of disable_global: %s', self.get_body_argument('disable_global',default=0))

        species = self.get_body_argument('species')
        description = self.get_body_argument('description',default='')

        job = GSJob(session_id, GSJob.get_current_time(),self.run_dir,species,description)
        self.logger.debug('Created job %s', str(job))

        # create the directory
        run_dir = self.run_dir + os.sep + session_id
        self.logger.debug('GOPCAHandler: Creating directory "%s"...', run_dir)
        util.make_sure_path_exists(run_dir)

        # store run data
        r.store_data()

        # create bash file
        species_name = self.species_names[species]
        gene_annotation_file = self.data_dir + os.sep + self.get_body_argument('gene_annotation') + '.gtf.gz'

        expression_url = self.get_body_argument('expression')

        go_annotation = self.get_body_argument('go_annotation')
        gene_ontology_file = self.data_dir + os.sep + go_annotation + '.obo'
        go_association_file = self.data_dir + os.sep + go_annotation + '.gaf.gz'

        evidence = self.get_body_arguments('go_evidence')
        min_genes = str(int(self.get_body_argument('go_min_genes')))
        max_genes = str(int(self.get_body_argument('go_max_genes')))
        self.logger.debug('Evidence: %s', str(evidence))
        #evidence_str = ' '.join(['"%s"' %(str(e)) for e in evidence])

        #def esc(fn):
        #    return fn.replace(' ',r'\ ')

        max_file_size = int(self.max_file_size * 1e6)
        template = self.get_template('gopca.sh')
        script = template.render(
                species_name = species_name,
                # expression data
                expression_url = expression_url,
                max_file_size = max_file_size,
                # Gene annotations
                gene_annotation_file = gene_annotation_file,
                # GO annotations
                gene_ontology_file = gene_ontology_file,
                go_association_file = go_association_file,
                go_evidence = evidence,
                go_min_genes = min_genes,
                go_max_genes = max_genes,
                # GO-PCA
                gopca_config = gopca_config
                )
        assert isinstance(script, unicode)
        output_file = run_dir + os.sep + 'gopca.sh'
        with codecs.open(output_file, 'wb', encoding = 'utf-8') as ofh:
            ofh.write(script)
        self.logger.debug('Wrote file "%s".',output_file)

        # run the script
        st = os.stat(output_file)
        os.chmod(output_file, st.st_mode | stat.S_IXUSR)
        log_file = run_dir + os.sep + 'gopca_pipeline_log.txt'
        cmd = '"%s" > "%s" 2>&1' %(output_file, log_file)
        logger.debug('Command: %s', cmd)
        subproc.Popen(cmd, shell = True, executable = '/bin/bash')

        # update run status
        r.update_status()
        r.store_data()

        # write html
        template = self.get_template('submit.html')
        html_output = template.render(timestamp=self.ts,title='Submission',run_id=session_id)
        self.write(html_output)

        self.runs[session_id] = r
예제 #8
0
def duel_rank_train(env, exploreScheduler, betaScheduler, optimizer_constructor, model_type, batch_size, rp_start, rp_size, 
	exp_frame, exp_initial, exp_final, prob_alpha, gamma, target_update_steps, frames_per_epoch, 
	frames_per_state, output_directory, last_checkpoint, max_frames, envo):

	"""
	Implementation of the training algorithm for Dueling Network Architecture using Rank-based prioritization.
	Information with regards to the algorithm can be found in the paper, 
	"Dueling Network Architectures for Deep Reinforcement Learning" by Ziyu Wang et al. 
	Refer to section 4.2 in the paper for more implementation info. 
	"""
	
	gym.undo_logger_setup()
	logging.basicConfig(filename=envo+'_'+'duel_rank_training.log',level=logging.INFO)
	num_actions = env.action_space.n
	env.reset()
	
	print('No. of actions: ', num_actions)
	print(env.unwrapped.get_action_meanings())

	# initialize action value and target network with the same weights
	model = DUEL(num_actions)
	target = DUEL(num_actions)

	if use_cuda:
		model.cuda()
		target.cuda()

	frames_count = 1

	if last_checkpoint:
		model.load_state_dict(torch.load(last_checkpoint))
		print(last_checkpoint)
		print('weights loaded...')

		exp_replay = util.initialize_rank_replay_resume(env, rp_start, rp_size, frames_per_state, 
			model, target, gamma, batch_size)
		frames_count = get_index_from_checkpoint_path(last_checkpoint)

	else:
		exp_replay = util.initialize_rank_replay(env, rp_start, rp_size, frames_per_state, 
			model, target, gamma, prob_alpha)

	target.load_state_dict(model.state_dict())

	optimizer = optimizer_constructor.type(model.parameters(), lr=optimizer_constructor.kwargs['lr'],
		alpha=optimizer_constructor.kwargs['alpha'], eps=optimizer_constructor.kwargs['eps'] )

	episodes_count = 1
	frames_per_episode = 1
	epsiodes_durations = []
	rewards_per_episode = 0
	rewards_duration = []
	loss_per_epoch = []
	wLoss_func = Weighted_Loss()

	
	current_state, _, _, _ = util.play_game(env, frames_per_state)
	print('Starting training...')

	for frames_count in range(1, max_frames):

		epsilon=exploreScheduler.anneal_linear(frames_count)
		beta = betaScheduler.anneal_linear(frames_count)
		choice = random.uniform(0,1)

		# epsilon greedy algorithm
		if choice <= epsilon:
			action = LongTensor([[random.randrange(num_actions)]])

		else:
			action = util.get_greedy_action(model, current_state)

		curr_obs, reward, done, _ = util.play_game(env, frames_per_state, action[0][0])

		rewards_per_episode += reward
		reward = Tensor([[reward]])
		current_state_ex = Variable(current_state, volatile=True)
		curr_obs_ex = Variable(curr_obs, volatile=True)
		action_ex = Variable(action, volatile=True)
		reward_ex = Variable(reward, volatile=True)

		#compute td-error for one sample
		td_error = duel_compute_td_error(batch_size=1, state_batch=current_state_ex, reward_batch=reward_ex, action_batch=action_ex, 
			next_state_batch=curr_obs_ex, model=model, target=target, gamma=gamma)

		td_error = torch.pow(torch.abs(td_error)+1e-6, prob_alpha)
		exp_replay.push(current_state, action, reward, curr_obs, td_error)
		current_state = curr_obs

		# compute y 
		if len(exp_replay) >= batch_size:
			# Get batch samples
			obs_samples, obs_ranks, obs_priorityVals = exp_replay.sample(batch_size)
			num_samples_per_batch = len(obs_samples)
			obs_priorityTensor = torch.from_numpy(np.array(obs_priorityVals))
			p_batch = 1/ obs_priorityTensor
			w_batch = (1/len(exp_replay) * p_batch)**beta
			max_weight = exp_replay.get_max_weight(beta)
			w_batch /= max_weight
			w_batch = w_batch.type(Tensor)

			batch = Experience(*zip(*obs_samples))

			loss, new_weights = duel_compute_y(batch, num_samples_per_batch, model, target, gamma, w_batch, wLoss_func)
			loss_abs = torch.abs(new_weights)
			exp_replay.update(obs_ranks, loss_abs)

			currentLOSS = loss.data.cpu().numpy()[0]

			optimizer.zero_grad()
			loss.backward()

			
			grad_index = 0
			for param in model.parameters():

				#Clip the combined gradient entering the last conv layer by 1/sqrt(2)
				if grad_index == 4:
					param.grad.data.mul_(1/math.sqrt(2))

				#Clip gradients to have their norm less than or equal to 10 
				grad_norm = torch.norm(param.grad.data)
				if grad_norm > 10: 
					param.grad.data.div_(grad_norm).mul_(10)

				grad_index += 1



			optimizer.step()
			loss_per_epoch.append(loss.data.cpu().numpy()[0])
		
		frames_per_episode+= frames_per_state

		if done:
			rewards_duration.append(rewards_per_episode)
			rewards_per_episode = 0
			frames_per_episode=1
			episodes_count+=1
			env.reset()
			current_state, _, _, _ = util.play_game(env, frames_per_state)

			if episodes_count % 100 == 0:
				avg_episode_reward = sum(rewards_duration)/100.0
				avg_reward_content = 'Episode from', episodes_count-99, ' to ', episodes_count, ' has an average of ', avg_episode_reward, ' reward and loss of ', sum(loss_per_epoch)
				print(avg_reward_content)
				logging.info(avg_reward_content)
				rewards_duration = []
				loss_per_epoch = []

		# update weights of target network for every TARGET_UPDATE_FREQ steps
		if frames_count % target_update_steps == 0:
			target.load_state_dict(model.state_dict())
			# print('weights updated at frame no. ', frames_count)

		# sort memory replay every half of it's capacity iterations 
		if frames_count % int(rp_size/2) == 0:
			exp_replay.sort()
	

		#Save weights every 250k frames
		if frames_count % 250000 == 0:
			util.make_sure_path_exists(output_directory+'/'+envo+'/')
			torch.save(model.state_dict(), output_directory+envo+'/rank_duel_'+ str(frames_count)+'.pth')


		#Print frame count and sort experience replay for every 1000000 (one million) frames:
		if frames_count % 1000000 == 0:
			training_update = 'frame count: ', frames_count, 'episode count: ', episodes_count, 'epsilon: ', epsilon
			print(training_update)
			logging.info(training_update)
예제 #9
0
def execute_run_tests_command(backend, config, criteria, stations, run_path,
                              comp):
    """
	Izvršenje komande koja obavlja pokretanje svih testova za zadatak

	backend - back-end koji se trenutno koristi
	config - globalna konfiguracija alata za pregled
	criteria - kriterijum pregleda zadatka (bodovanje, način izvršavanja itd.)
	stations - kolekcija računara i studenata koji su radili zadatak (ključ - oznaka računara, podatak - lista - broj
	           indeksa i ime/prezime studenta)
	run_path - putanja na kojoj se nalazi studentski zadatak čiji se testovi izvršavaju
	comp - oznaka računara na kojem je urađen zadatak koji se pregleda
	"""
    if not path.isdir(config.AUTOTEST_PATH):
        util.fatal_error(
            '''Prilikom postavljanja zadatka nije postavljena autotest varijanta zadatka.\n'
Ako zelite da koristite automatsko testiranje, kreirajte direktorijum "{0}" i postavite autotest varijantu u njega.'''
            .format(config.AUTOTEST_PATH))

    if not path.isdir(run_path):
        util.fatal_error(
            'Ne mogu se pokrenuti testovi jer projekat nije prethodno kompajliran.\n'
            + 'Upotrebite komandu build za kompajliranje projekta.')

    util.make_sure_path_exists(config.REPORTS_PATH)
    current_reports_path = join(config.REPORTS_PATH, comp)
    util.make_sure_path_exists(current_reports_path)
    util.clear_directory(current_reports_path)

    logging.info(
        'Pokrenuti su automatski testovi tekuceg projekta u direktorijumu: {0}'
        .format(run_path))

    proj = util.identify_project_file(backend, run_path)
    executable = backend.identify_project_executable(proj)

    all_results = []
    tests = []
    for s in criteria.score_distribution:
        tests.append(s.keys()[0])

    # Zakazivanje izvršenja testova od strane (potencijalno) više niti za izvršenje testova:

    print('Sledeci testovi ce biti izvrseni: {0}'.format(', '.join(tests)))
    print('Svaki test se izvrsava {0} put(a)'.format(
        criteria.runs_spec['total']))

    for t in tests:
        execution_results[t] = [None] * criteria.runs_spec['total']

    for i in range(backend.get_parallel_testing_threads_count()):
        t = Thread(target=execution_worker_thread,
                   args=(backend, criteria, i, run_path, executable))
        t.daemon = True
        t.start()

    for t in tests:
        test_index = 0
        for run in range(1, criteria.runs_spec['total'] + 1):
            execution_queue.put([
                test_index, t,
                get_console_report_path(config, comp, t, test_index)
            ])
            test_index += 1

    execution_queue.join()

    # Grupisanje dobijenih rezultata - konsolidacija rezultata po svakom pojedinačnom testu (po nazivu testa):

    for t in tests:
        results = execution_results[t]

        # Određivanje najdužeg vremena izvršenja ovog testa:
        max_duration = results[0].duration
        executions = []
        for r in results:
            if r.duration > max_duration:
                max_duration = r.duration
            executions.append(r.result == 'passed')

        passes = sum(1 for x in results if x.result == 'passed')

        entry = TestResults(
            name=t,
            runs=criteria.runs_spec['total'],
            passes=passes,
            failures=sum(1 for x in results if x.result != 'passed'),
            test_fails=sum(1 for x in results if x.result == 'failed'),
            crashes=sum(1 for x in results if x.result == 'crashed'),
            time_outs=sum(1 for x in results if x.result == 'timed-out'),
            total_duration=sum(x.duration for x in results),
            max_duration=max_duration,
            score=get_score(criteria, t)["percent"],
            factor=get_score(criteria, t)["factor"],
            success=(passes / float(criteria.runs_spec['total'])) >=
            criteria.runs_spec['pass_limit'],
            executions=executions)

        all_results.append(entry)

    # Ispis rezimea pokretanja testova na konzolu:

    total = len(criteria.score_distribution)
    passed = 0
    score = 0
    blockers = False
    for t in all_results:
        print ''
        header_line = 'TEST: {0}, ukupno izvrsenja: {1}'.format(t.name, t.runs)
        print '*' * len(header_line)
        print bcolors.BOLD + header_line + bcolors.ENDC
        print '*' * len(header_line)

        if t.runs < criteria.runs_spec['total']:
            print bcolors.FAIL \
               + 'Detektovano je dovoljno negativnih ishoda pa nije obavljeno svih {0} zahtevanih pokretanja'\
                .format(criteria.runs_spec['total']) + bcolors.ENDC

        if t.passes > 0:
            print bcolors.OKGREEN + 'PROSAO: {0} put(a)'.format(
                t.passes) + bcolors.ENDC
        if t.failures > 0:
            print bcolors.FAIL + 'PAO: {0} put(a), od toga:'.format(
                t.failures) + bcolors.ENDC
            if t.test_fails > 0:
                print bcolors.FAIL + '    Formirao los rezultat:       {0} put(a)'.format(
                    t.test_fails) + bcolors.ENDC
            if t.crashes > 0:
                print bcolors.FAIL + '    Nasilno prekinuo izvrsenje:  {0} put(a)'.format(
                    t.crashes) + bcolors.ENDC
            if t.time_outs > 0:
                print bcolors.FAIL + '    Prekoracio dozvoljeno vreme: {0} put(a)'.format(
                    t.time_outs) + bcolors.ENDC

        print 'Ukupno vreme izvrsenja: {0}, najduze pokretanje: {1}'.format(
            t.total_duration, t.max_duration)

        if t.success:
            print bcolors.OKGREEN + 'Test se smatra uspesnim, tezina: {0} (od ukupno {1}), procentualno: {2:.2f}%'\
             .format(t.factor, criteria.total_weight, t.score) + bcolors.ENDC
            passed += 1
            score += t.score
        else:
            print bcolors.FAIL + 'Test se smatra neuspesnim' + bcolors.ENDC
            if t in criteria.blocking_tests:
                blockers = True
                print bcolors.FAIL + 'Ovo je blokirajuci test!' + bcolors.ENDC

    print ''
    if passed == total:
        print bcolors.OKGREEN \
           + '''Uspesno su izvrseni svi testovi (ukupno je: {0} testova).\nUkupno ostvareno procenata: {1:.2f}%'''.format(total, score) + bcolors.ENDC
    else:
        failed = total - passed
        print bcolors.FAIL + '''Palo je {0} testova, od ukupno {1}!
Procenat testova koji prolaze: {2:.2f}%, procentualni ucinak: {3:.2f}%'''.format(
            failed, total,
            float(passed) / total * 100.0, score) + bcolors.ENDC

    status = ASSIGNMENT_STATUS_OK
    if blockers:
        print bcolors.FAIL + bcolors.BOLD \
           + 'Pao je makar jedan blokirajuci test! U izvestaju je naznaceno da u ovom radu postoje takvi testovi.' \
           + bcolors.ENDC
        status = ASSIGNMENT_STATUS_BLOCKED

    final_report.update_final_report(config,
                                     criteria,
                                     stations,
                                     comp,
                                     datetime.datetime.now().isoformat(),
                                     status=status,
                                     results=all_results)
예제 #10
0
def internal_build_project(backend, config, criteria, current_assignment_path,
                           autobuild_path):
    """
	Interna pomoćna metoda koja vrši kompajliranje projekta (studentskog zadatka)

	backend - back-end koji se trenutno koristi
	config - globalna konfiguracija alata za pregled
	criteria - kriterijum pregleda zadatka (bodovanje, način izvršavanja itd.)
	current_assignment_path - putanja do zadatka koji se kompajlira
	autobuild_path - putanja na kojoj se nalazi autobuild varijanta tekućeg zadatka

	Vraća indikaciju da li je kompajliranje uspešno obavljeno
	0 - kompajliranje je uspešno
	1 - produkovan je izvršni fajl ali je kompajliranje vratilo upozorenja
	2 - kompajliranje je neuspešno
	"""
    logging.info(
        'Pokrenuto je kompajliranje projekta u direktorijumu: {0}'.format(
            current_assignment_path))

    # Brisanje trenutnog sadrzaja autobuild direktorijuma:
    print('Brisanje sadrzaja direktorijuma "{0}"'.format(autobuild_path))
    util.make_sure_path_exists(autobuild_path)
    util.clear_directory(autobuild_path)

    # Kopiranje svih fajlova iz osnovnog direktorijuma u autobuild poddirektorijum:
    print('Kopiranje izvornih fajlova iz "{0}" u "{1}"'.format(
        current_assignment_path, autobuild_path))
    onlyfiles = [
        f for f in os.listdir(current_assignment_path)
        if path.isfile(join(current_assignment_path, f))
    ]
    autotestfiles = [
        f for f in os.listdir(config.AUTOTEST_PATH)
        if path.isfile(join(config.AUTOTEST_PATH, f))
    ]

    for f in onlyfiles:
        if (f in criteria.assignment_files) or (
                f not in autotestfiles
                and filename_matches_assignment_pattern(backend, f)):
            copyfile(join(current_assignment_path, f), join(autobuild_path, f))

    # Obrada događaja koji se inicira pre nego što se obavi kompajliranje zadatka:
    try:
        backend.before_build(autobuild_path)
    except RuntimeError as err:
        util.fatal_error(err.message)

    # Kopiranje dodatnih fajlova iz autotest direktorijuma u autobuild poddirektorijum:
    print('Kopiranje autotest fajlova iz "{0}" u "{1}"'.format(
        config.AUTOTEST_PATH, autobuild_path))

    if len(autotestfiles) == 0:
        util.fatal_error(
            'Projekat se ne moze kompajlirati jer autotest varijanta zadatka nije postavljena u direktorijum: "{0}"!'
            .format(config.AUTOTEST_PATH))

    for f in autotestfiles:
        # Proverava se da li je fajl naveden u listi fajlova u kojima studenti unose resenje zadatka.
        # Ako je tako, onda taj fajl ne bi smeo da bude postavljen u autotest folder.
        if f in criteria.assignment_files:
            util.fatal_error(
                '''Fajl "{0}" je postavljen u "{1}" direktorijum a ocekuje se da studenti unose svoje resenje u taj fajl.
Fajlovi iz "{1}" direktorijuma kopiraju se preko studentskog resenja, tako da bi kopiranjem ovog fajla unistili kljucni deo resenja.
Molim da procitate deo uputstva za koriscenje alata za pregled koji se odnosi na postavljanje fajlova u ovaj direktorijum.'''
                .format(f, config.AUTOTEST_PATH))

        copyfile(join(config.AUTOTEST_PATH, f), join(autobuild_path, f))

    # Potom, sledi kompajliranje projekta:
    ret = backend.build_project(
        util.identify_project_file(backend, autobuild_path))

    # Poslednja linija izvešaja o kompajliranju treba da sadrži informaciju o potencijalnim upozorenjima i greškama tokom kompajliranja:

    f = open(join(autobuild_path, backend.get_build_report_filename()), 'r')
    lines = f.readlines()
    last_line = lines[len(lines) - 1]
    regex = re.compile(
        '(?P<errors>\d+)\serror\(s\),\s(?P<warnings>\d+)\swarning\(s\)',
        re.IGNORECASE)
    m = regex.match(last_line)

    if m:
        errors = int(m.group('errors'))
        warnings = int(m.group('warnings'))

        if (errors == 0 and warnings == 0):
            return 0

        if (errors == 0):
            return 1

        return 2
    else:
        util.fatal_error(
            '''Interna greska: izvestaj o kompajliranju ne sadrzi poslednju liniju sa brojem gresaka i upozorenja.
Nije moguce utvrditi ishod kompajliranja. Potrebno je kontaktirati autora alata.'''
        )
예제 #11
0
파일: initialize.py 프로젝트: mgornik/PEAN
def execute_init_command(config, stations, all_backends, log_filename):
    """
	Izvršavanje komande za inicijalizaciju pregleda (postavljanje materijala i određivanje kriterijuma pregleda)

	config - globalna konfiguracija alata za pregled
	stations - kolekcija računara i studenata koji su radili zadatak (ključ - oznaka računara, podatak - lista - broj
	           indeksa i ime/prezime studenta)
	all_backends - lista instanci klase Backend - sve raspoložive implementacije back-end-a
	"""

    # Pribavljanje sadržaja tekućeg direktorijuma kako bi se proverilo da li je on prazan:
    lista = os.listdir('.')
    # Ako u tekućem direktorijumu postoji log fajl, on se ignoriše:
    lista.remove(log_filename)

    # Tekući direktorijum mora biti prazan da bi se dozvolila inicijalizacija projekta:
    if lista != []:
        print bcolors.FAIL + 'Tekuci direktorijum nije prazan!' + bcolors.ENDC
        print 'Ne moze se inicirati pregled zadataka u ovom direktorijumu.'
        sys.exit(1)

    print 'Inicijalizacija pregleda je pokrenuta, molim pratite instrukcije na ekranu.'

    print 'Izbor predmeta/backend-a za kompajliranje i pokretanje zadataka'
    print 'Alat je trenutno konfigurisan da podrzava sledece varijante backend-a:'

    option = 1
    for b in all_backends:
        print '{0}) Naziv: {1}, Ver: {2}, Opis: {3}'.format(
            option, b.name(), b.version(), b.description())
        option += 1

    izbor = util.get_option_answer(1, option - 1)

    backend = all_backends[izbor - 1]

    grupe = util.get_yesno_answer('Da li u ovoj proveri postoje grupe A i B?')
    autotest = util.get_yesno_answer(
        'Da li ce pregled biti obavljen automatizovano?')

    # Kreiranje dva poddirektorijuma za pregled po grupama:
    if grupe:
        util.make_sure_path_exists(config.GROUP1_DIR)
        util.make_sure_path_exists(config.GROUP2_DIR)
        print ''
        print bcolors.OKBLUE + 'Zadaci grupa A i B tretirace se kao dva odvojena zadatka koji se pregledaju.' \
           + bcolors.ENDC
        print 'Za zadatke grupe A kreiran je direktorijum "{0}" u kojem ce se postaviti fajlovi za pregled.'\
         .format(config.GROUP1_DIR)
        print 'Za zadatke grupe B kreiran je direktorijum "{0}" sa istom svrhom.'.format(
            config.GROUP2_DIR)
        print ''

    print 'Kopiranje neophodnih fajlova u tekuci direktorijum...'

    # Kopiranje XSLT fajla:
    dir = os.path.dirname(__file__)
    xslt_path = join(dir, config.FINAL_REPORT_XSLT_FILENAME)
    if grupe:
        copyfile(xslt_path,
                 join(config.GROUP1_DIR, config.FINAL_REPORT_XSLT_FILENAME))
        copyfile(xslt_path,
                 join(config.GROUP2_DIR, config.FINAL_REPORT_XSLT_FILENAME))
    else:
        copyfile(xslt_path, join('.', config.FINAL_REPORT_XSLT_FILENAME))

    # Priprema direktorijuma u koje će se raspakovati zadaci:
    if grupe:
        util.make_sure_path_exists(
            join(config.GROUP1_DIR, config.ASSIGNMENTS_PATH))
        util.make_sure_path_exists(
            join(config.GROUP2_DIR, config.ASSIGNMENTS_PATH))
    else:
        util.make_sure_path_exists(config.ASSIGNMENTS_PATH)

    util.make_sure_path_exists(config.ARCHIVE_PATH)
    print 'Postaviti sledeci sadrzaj u poddirektorijume:'
    print '\n1) Arhivu sa svim uradjenim zadacima u direktorijum "{0}"'.format(
        config.ARCHIVE_PATH)
    print 'Naziv arhive treba da bude u formatu: "{0}"'.format(
        config.ASSIGNMENTS_ARCHIVE_PATTERN)
    wait_until_files_are_set(config.ARCHIVE_PATH)
    unpack_assignments(config, stations, grupe)
    os.rename(config.ARCHIVE_PATH, config.BACKUP_PATH)
    print ''

    # Kopiranje postavke zadatka u odgovarajući direktorijum:
    if grupe:
        group1_initial_path = join(config.GROUP1_DIR,
                                   config.INITIAL_PROJECT_PATH)
        util.make_sure_path_exists(group1_initial_path)
        group2_initial_path = join(config.GROUP2_DIR,
                                   config.INITIAL_PROJECT_PATH)
        util.make_sure_path_exists(group2_initial_path)

        print '2A) Postavku zadatka grupe A koja je data studentima u direktorijum "{0}"'.format(
            group1_initial_path)
        wait_until_files_are_set(group1_initial_path)

        print '2B) Postavku zadatka grupe B koja je data studentima u direktorijum "{0}"'.format(
            group2_initial_path)
        wait_until_files_are_set(group2_initial_path)
    else:
        util.make_sure_path_exists(config.INITIAL_PROJECT_PATH)

        print '2) Postavku zadatka koja je data studentima u direktorijum "{0}"'.format(
            config.INITIAL_PROJECT_PATH)
        wait_until_files_are_set(config.INITIAL_PROJECT_PATH)
    print ''

    # Kopiranje autotest varijante zadatka u odgovarajući direktorijum:
    if autotest:
        if grupe:
            group1_autotest_path = join(config.GROUP1_DIR,
                                        config.AUTOTEST_PATH)
            util.make_sure_path_exists(group1_autotest_path)
            group2_autotest_path = join(config.GROUP2_DIR,
                                        config.AUTOTEST_PATH)
            util.make_sure_path_exists(group2_autotest_path)

            print '\n3A) Autotest varijantu zadatka grupe A u direktorijum "{0}"'.format(
                group1_autotest_path)
            wait_until_files_are_set(group1_autotest_path)

            print '\n3B) Autotest varijantu zadatka grupe B u direktorijum "{0}"'.format(
                group2_autotest_path)
            wait_until_files_are_set(group2_autotest_path)
        else:
            util.make_sure_path_exists(config.AUTOTEST_PATH)
            print '\n3) Autotest varijantu zadatka u direktorijum "{0}"'.format(
                config.AUTOTEST_PATH)
            wait_until_files_are_set(config.AUTOTEST_PATH)
        print ''

    # Kreiranje dodatnih poddirektorijuma koji se koriste u pregledu:
    if grupe:
        util.make_sure_path_exists(
            join(config.GROUP1_DIR, config.ALTERED_ASSIGNMENTS_PATH))
        util.make_sure_path_exists(
            join(config.GROUP1_DIR, config.CURRENT_ASSIGNMENT_PATH))
        util.make_sure_path_exists(
            join(config.GROUP1_DIR, config.CURRENT_ALT_ASSIGNMENT_PATH))
        util.make_sure_path_exists(join(config.GROUP1_DIR,
                                        config.REPORTS_PATH))

        util.make_sure_path_exists(
            join(config.GROUP2_DIR, config.ALTERED_ASSIGNMENTS_PATH))
        util.make_sure_path_exists(
            join(config.GROUP2_DIR, config.CURRENT_ASSIGNMENT_PATH))
        util.make_sure_path_exists(
            join(config.GROUP2_DIR, config.CURRENT_ALT_ASSIGNMENT_PATH))
        util.make_sure_path_exists(join(config.GROUP2_DIR,
                                        config.REPORTS_PATH))
    else:
        util.make_sure_path_exists(config.ALTERED_ASSIGNMENTS_PATH)
        util.make_sure_path_exists(config.CURRENT_ASSIGNMENT_PATH)
        util.make_sure_path_exists(config.CURRENT_ALT_ASSIGNMENT_PATH)
        util.make_sure_path_exists(config.REPORTS_PATH)

    # Definisanje kriterijuma za ocenjivanje zadatka.
    # Mora se izvršavati posle postavljanja inicijalne verzije zadatka, jer koristi putanje koje ona definiše.
    if autotest:
        if grupe:
            group1_criteria_path = join(config.GROUP1_DIR,
                                        config.RATING_CRITERIA_FILENAME)
            group2_criteria_path = join(config.GROUP2_DIR,
                                        config.RATING_CRITERIA_FILENAME)

            raw_input(
                '''U tekst editoru bice otvoren fajl "{0}" u kojem se definise kriterijum za zadatak grupe A.
Pritisnite <ENTER> za nastavak.'''.format(group1_criteria_path))
            generate_empty_criteria_file(backend, group1_criteria_path,
                                         group1_initial_path,
                                         group1_autotest_path)
            util.show_text_edit(config, group1_criteria_path)
            raw_input(
                'Pritisnite <ENTER> kada ste zavrsili sa izmenama u fajlu...')

            raw_input(
                '''U tekst editoru bice otvoren fajl "{0}" u kojem se definise kriterijum za zadatak grupe A.
Pritisnite <ENTER> za nastavak.'''.format(group2_criteria_path))
            generate_empty_criteria_file(backend, group2_criteria_path,
                                         group2_initial_path,
                                         group2_autotest_path)
            util.show_text_edit(config, group2_criteria_path)
            raw_input(
                'Pritisnite <ENTER> kada ste zavrsili sa izmenama u fajlu...')
        else:
            raw_input(
                '''U tekst editoru bice otvoren fajl "{0}" u kojem se definise kriterijum (nacin bodovanja).
Pritisnite <ENTER> za nastavak.'''.format(config.RATING_CRITERIA_FILENAME))
            generate_empty_criteria_file(backend,
                                         config.RATING_CRITERIA_FILENAME,
                                         config.INITIAL_PROJECT_PATH,
                                         config.AUTOTEST_PATH)
            util.show_text_edit(config, config.RATING_CRITERIA_FILENAME)
            raw_input(
                'Pritisnite <ENTER> kada ste zavrsili sa izmenama u fajlu...')
        print ''

    print 'Svi materijali su postavljeni na odgovarajuce pozicije, mozete poceti sa pregledom zadataka.'
    sys.exit(0)
예제 #12
0
def ddqn_rank_train(env, exploreScheduler, betaScheduler,
                    optimizer_constructor, model_type, batch_size, rp_start,
                    rp_size, exp_frame, exp_initial, exp_final, prob_alpha,
                    gamma, target_update_steps, frames_per_epoch,
                    frames_per_state, output_directory, last_checkpoint,
                    max_frames, envo):
    """
	Implementation of the training algorithm for DDQN using Rank-based prioritization.
	Information with regards to the algorithm can be found in the paper, 
	"Prioritized Experience Replay" by Tom Schaul, John Quan, Ioannis Antonoglou and
	David Silver. Refer to section 3.3 in the paper for more info. 
	"""

    gym.undo_logger_setup()
    logging.basicConfig(filename=envo + '_' +
                        'ddqn_rank_weighted_training.log',
                        level=logging.INFO)
    num_actions = env.action_space.n
    env.reset()

    print('No. of actions: ', num_actions)
    print(env.unwrapped.get_action_meanings())

    # initialize action value and target network with the same weights
    model = DQN(num_actions)
    target = DQN(num_actions)

    if use_cuda:
        model.cuda()
        target.cuda()

    frames_count = 1

    if last_checkpoint:
        model.load_state_dict(torch.load(last_checkpoint))
        print(last_checkpoint)
        print('weights loaded...')

        #TODO: Implementation of resume
        # exp_replay = util.initialize_rank_replay_resume(env, rp_start, rp_size, frames_per_state,
        # 	model, target, gamma, batch_size)
        # frames_count = get_index_from_checkpoint_path(last_checkpoint)

    else:
        exp_replay = util.initialize_rank_replay(env, rp_start, rp_size,
                                                 frames_per_state, model,
                                                 target, gamma, prob_alpha)

    target.load_state_dict(model.state_dict())

    optimizer = optimizer_constructor.type(
        model.parameters(),
        lr=optimizer_constructor.kwargs['lr'],
        alpha=optimizer_constructor.kwargs['alpha'],
        eps=optimizer_constructor.kwargs['eps'])

    episodes_count = 1
    epsiodes_durations = []
    rewards_per_episode = 0
    rewards_duration = []
    loss_per_epoch = []
    current_state, _, _, _ = util.play_game(env, frames_per_state)
    wLoss_func = Weighted_Loss()

    print('Starting training...')

    for frames_count in range(1, max_frames):

        epsilon = exploreScheduler.anneal_linear(frames_count)
        beta = betaScheduler.anneal_linear(frames_count)
        choice = random.uniform(0, 1)

        # epsilon greedy algorithm
        if choice <= epsilon:
            action = LongTensor([[random.randrange(num_actions)]])

        else:
            action = util.get_greedy_action(model, current_state)

        curr_obs, reward, done, _ = util.play_game(env, frames_per_state,
                                                   action[0][0])

        rewards_per_episode += reward
        reward = Tensor([[reward]])
        td_error = 1

        temp_exp = Experience(current_state, action, reward, curr_obs,
                              td_error)
        current_state = curr_obs

        # compute y
        if len(exp_replay) >= batch_size:
            # Get batch samples

            # start = time.time()

            if frames_count % rp_size == 0:
                obs_samples, obs_priorityVals = exp_replay.sample(batch_size -
                                                                  1,
                                                                  prob_alpha,
                                                                  sort=True)
            else:
                obs_samples, obs_priorityVals = exp_replay.sample(batch_size -
                                                                  1,
                                                                  prob_alpha,
                                                                  sort=False)

            obs_samples.append(temp_exp)
            obs_priorityVals.append(td_error)

            obs_pVals_tensor = torch.from_numpy(np.array(obs_priorityVals))
            # print("P(i): ", obs_pVals_tensor)
            IS_weights = torch.pow((obs_pVals_tensor * rp_size), -beta)
            max_weight = torch.max(IS_weights)

            IS_weights_norm = torch.div(IS_weights, max_weight).type(Tensor)
            IS_weights_norm[-1] = torch.max(IS_weights_norm)

            # print("Norm W(i): ", IS_weights_norm)

            batch = Experience(*zip(*obs_samples))
            loss, new_weights = ddqn_compute_y(batch, batch_size, model,
                                               target, gamma, IS_weights_norm,
                                               wLoss_func)
            new_weights = torch.pow(new_weights, prob_alpha)
            new_exp = Experience(temp_exp.state, temp_exp.action,
                                 temp_exp.reward, temp_exp.next_state,
                                 new_weights[batch_size - 1])
            exp_replay.update(obs_samples, new_weights, new_exp)
            optimizer.zero_grad()
            loss.backward()
            # print("loss: ", loss.data)
            optimizer.step()
            loss_per_epoch.append(loss.data.cpu().numpy()[0])

        else:
            exp_replay.push(new_exp.state, new_exp.action, new_exp.reward,
                            new_exp.next_state, td_error)

        # end = time.time()

        # duration = end-start

        # print('duration : ', duration)

        if done:
            # print('Game: ', rewards_per_episode)
            rewards_duration.append(rewards_per_episode)
            rewards_per_episode = 0
            episodes_count += 1
            env.reset()
            current_state, _, _, _ = util.play_game(env, frames_per_state)

            if episodes_count % 100 == 0:
                avg_episode_reward = sum(rewards_duration) / 100.0
                avg_reward_content = 'Episode from', episodes_count - 99, ' to ', episodes_count, ' has an average of ', avg_episode_reward, ' reward and loss of ', sum(
                    loss_per_epoch)
                print(avg_reward_content)
                logging.info(avg_reward_content)
                rewards_duration = []
                loss_per_epoch = []

        # update weights of target network for every TARGET_UPDATE_FREQ steps
        if frames_count % target_update_steps == 0:
            target.load_state_dict(model.state_dict())

        #Save weights every 250k frames
        if frames_count % 250000 == 0:
            util.make_sure_path_exists(output_directory + '/' + envo + '/')
            torch.save(
                model.state_dict(), output_directory + '/' + envo +
                '/rank_uniform' + str(frames_count) + '.pth')

        #Print frame count and sort experience replay for every 1000000 (one million) frames:
        if frames_count % 1000000 == 0:
            training_update = 'frame count: ', frames_count, 'episode count: ', episodes_count, 'epsilon: ', epsilon
            print(training_update)
            logging.info(training_update)
예제 #13
0
def dqn_train(env, scheduler, optimizer_constructor, model_type, batch_size,
              rp_start, rp_size, exp_frame, exp_initial, exp_final, gamma,
              target_update_steps, frames_per_epoch, frames_per_state,
              output_directory, last_checkpoint, envo):

    gym.undo_logger_setup()
    logging.basicConfig(filename=envo + '_' + model_type + '_training.log',
                        level=logging.INFO)
    num_actions = env.action_space.n

    print('No. of actions: ', num_actions)
    print(env.unwrapped.get_action_meanings())

    # initialize action value and target network with the same weights
    model = DQN(num_actions, use_bn=False)
    target = DQN(num_actions, use_bn=False)

    if use_cuda:
        model.cuda()
        target.cuda()

    exp_replay = None
    episodes_count = 1

    if last_checkpoint != '':
        model.load_state_dict(torch.load(last_checkpoint))
        exp_replay = util.initialize_replay_resume(env, rp_start, rp_size,
                                                   frames_per_state, model)
        episodes_count = get_index_from_checkpoint_path(last_checkpoint)

    else:
        exp_replay = util.initialize_replay(env, rp_start, rp_size,
                                            frames_per_state)

    target.load_state_dict(model.state_dict())
    print('weights loaded...')

    optimizer = optimizer_constructor.type(
        model.parameters(),
        lr=optimizer_constructor.kwargs['lr'],
        alpha=optimizer_constructor.kwargs['alpha'],
        eps=optimizer_constructor.kwargs['eps'])

    frames_count = 1
    frames_per_episode = 1
    epsiodes_durations = []
    rewards_per_episode = 0
    rewards_duration = []
    loss_per_epoch = []

    env.reset()

    current_state, _, _, _ = util.play_game(env, frames_per_state)

    print('Starting training...')

    count = 0

    while True:

        epsilon = scheduler.anneal_linear(frames_count)
        choice = random.uniform(0, 1)

        # epsilon greedy algorithm
        if choice <= epsilon:
            action = LongTensor([[random.randrange(num_actions)]])

        else:
            action = util.get_greedy_action(model, current_state)

        curr_obs, reward, done, _ = util.play_game(env, frames_per_state,
                                                   action[0][0])

        rewards_per_episode += reward
        reward = Tensor([reward])

        exp_replay.push(current_state, action, reward, curr_obs)

        current_state = curr_obs

        #sample random mini-batch
        obs_sample = exp_replay.sample(batch_size)

        batch = Experience(
            *zip(*obs_sample)
        )  #unpack the batch into states, actions, rewards and next_states

        #compute y
        if len(exp_replay) >= batch_size:

            loss = dqn_compute_y(batch, batch_size, model, target, gamma)
            optimizer.zero_grad()
            loss.backward()

            for param in model.parameters():
                param.grad.data.clamp_(-1, 1)

            optimizer.step()

            loss_per_epoch.append(loss.data.cpu().numpy()[0])

        frames_count += 1
        frames_per_episode += frames_per_state

        if done:
            rewards_duration.append(rewards_per_episode)
            rewards_per_episode = 0
            frames_per_episode = 1
            episodes_count += 1
            env.reset()
            current_state, _, _, _ = util.play_game(env, frames_per_state)

            if episodes_count % 100 == 0:
                avg_episode_reward = sum(rewards_duration) / 100.0
                avg_reward_content = 'Episode from', episodes_count - 99, ' to ', episodes_count, ' has an average of ', avg_episode_reward, ' reward and loss of ', sum(
                    loss_per_epoch)
                print(avg_reward_content)
                logging.info(avg_reward_content)
                rewards_duration = []
                loss_per_epoch = []

        # update weights of target network for every TARGET_UPDATE_FREQ steps
        if frames_count % target_update_steps == 0:
            target.load_state_dict(model.state_dict())
            # print('weights updated at frame no. ', frames_count)

        #Save weights every 250k frames
        if frames_count % 250000 == 0:
            util.make_sure_path_exists(output_directory + envo + '/' +
                                       model_type + '/')
            torch.save(
                model.state_dict(), output_directory + envo + '/' +
                model_type + '/weights_' + str(frames_count) + '.pth')

        #Print frame count for every 1000000 (one million) frames:
        if frames_count % 1000000 == 0:
            training_update = 'frame count: ', frames_count, 'episode count: ', episodes_count, 'epsilon: ', epsilon
            print(training_update)
            logging.info(training_update)
예제 #14
0
            print("[***] {0} TEST: One vs.".format(dataset),
                  "Rest" if one_vs_rest else "Other")

            data_all = run_test(
                dataset=dataset,
                clf_type=clf_type,
                epochs=epochs,
                true_rh1=true_rh1,
                downsample_ratio=downsample_ratio,
                ordered_models_keys=[model],
                list_of_images=[image],
                suppress_error=suppress_error,
                verbose=verbose,
                pi1=pi1,
                one_vs_rest=one_vs_rest,
                cv_n_folds=cv_n_folds,
                early_stopping=early_stopping,
                pulearning=pulearning,
            )

            print("Completed: model", model, "and image", image)

            # Before we store results, create folder if needed.
            make_sure_path_exists("data/")
            pickle.dump(
                data_all,
                open(
                    "data/metrics_{0}_{1}_{2}_epochs_rh1_{3}_downsample_{4}_model_{5}_image_{6}_pi1_{7}.p"
                    .format(dataset, clf_type, epochs, true_rh1,
                            downsample_ratio, model, image, pi1), "wb"))
예제 #15
0
파일: initialize.py 프로젝트: mgornik/PEAN
def unpack_assignments(config, stations, two_groups):
    """
	Raspakuje arhivu sa svim studentskim radovima iz direktorijuma config.ARCHIVE_PATH

	config - globalna konfiguracija alata za pregled
	stations - kolekcija računara i studenata koji su radili zadatak (ključ - oznaka računara, podatak - lista - broj
	           indeksa i ime/prezime studenta)
	two_groups - boolean koji indikuje da li je zadatak rađen u dve grupe (A i B)
	"""
    util.make_sure_path_exists(config.TEMP_PATH)

    # Ukoliko se u prihvatnom direktorijumu nalazi arhiva, ona će biti raspakovana u direktorijum sa studentskim
    # zadacima i potom premeštena u backup direktorijum:
    archives = glob.glob(
        join(config.ARCHIVE_PATH, config.ASSIGNMENTS_ARCHIVE_PATTERN))
    found = 0
    for a in archives:
        found = found + 1
        print 'Pronadjena je nova arhiva sa zadacima koja ce biti obradjena'
        print 'Raspakivanje arhive "{0}" u direktorijum "{1}"...'.format(
            a, config.TEMP_PATH)
        command = config.EXTRACT_ASSIGNMENTS_CMD.format(a, config.TEMP_PATH)
        ret = call(command, shell=True)
        if ret != 0:
            util.fatal_error(
                'Raspakivanje arhive sa zadacima je neuspesno!\nKomanda koja je pokrenuta:\n{0}'
                .format(command))

    if found == 0:
        util.fatal_error('''Nije pronadjena arhiva sa zadacima! 
Molim proverite da li arhiva koju ste prilozili ima adekvatan naziv i adekvatnu ekstenziju! 
Ocekuje se naziv poput sledeceg: {0}'''.format(
            config.ASSIGNMENTS_ARCHIVE_PATTERN))

    # Učitavanje liste studenata iz raspakovanog sadrzaja arhive:
    matches = glob.glob(join(config.TEMP_PATH, config.STUDENTS_LIST_PATTERN))

    if len(matches) == 0:
        util.fatal_error(
            'Nije pronadjena lista studenata (pattern za trazenje: "{0}")'.
            format(config.STUDENTS_LIST_PATTERN))
    if len(matches) > 1:
        util.fatal_error(
            'Pronadjen je vise od jednog fajla koji je kandidat za listu studenata. Kandidati: {0}'
            .format(matches))

    # Ako zadatak ima samo jednu grupu (nema grupe A i B), onda se lista studenata samo kopira na destinaciju
    # (neće biti njene podele na dve podgrupe):
    if not two_groups:
        copyfile(matches[0], join(config.ASSIGNMENTS_PATH,
                                  basename(matches[0])))

    logging.debug(
        'Ucitavanje spiska studenata prilikom raspakivanja arhive...')
    ifile = open(matches[0], "rb")
    reader = csv.DictReader(ifile,
                            dialect='excel',
                            fieldnames=['station', 'id', 'name'])
    for row in reader:
        station = row['station'].strip()
        id = row['id'].strip()
        name = row['name'].strip()
        if id:
            logging.debug(
                'Ucitano: Stanica: {0}, Broj indeksa: {1}, Ime: {2}'.format(
                    station, id, name))
            stations[station] = [id, name]
    ifile.close()
    logging.debug('Ucitavanje spiska studenata zavrseno')

    if two_groups:
        group1_list = open(
            join(config.GROUP1_DIR, config.ASSIGNMENTS_PATH,
                 "spisak_stud_koji_trenutno_rade_proveru.txt"), "a+")
        group2_list = open(
            join(config.GROUP2_DIR, config.ASSIGNMENTS_PATH,
                 "spisak_stud_koji_trenutno_rade_proveru.txt"), "a+")

    # Arhive koje su pronađene u direktorijumu sa studentskim zadacima biće raspakovane i potom obrisane:
    archives = sorted(
        glob.glob(
            join(config.TEMP_PATH, config.SINGLE_ASSIGNMENT_ARCHIVE_PATTERN)))
    for a in archives:
        logging.debug(
            'Pronadjena je sledeca arhiva sa pojedinacnim zadatkom: {0}'.
            format(a))

        # Manipulacija nazivom arhive, kako bi se došlo do oznake stanice na kojoj je student radio:
        name = os.path.basename(a)[:-len(config.SINGLE_ASSIGNMENT_ARCHIVE_EXT)]
        tokens = name.split('_')
        station = tokens[len(tokens) - 1]

        # Proces kopiranja zadataka vođen je spiskom studenata koji su radili proveru.
        # Ako stanica nije na tom spisku, znači da je u pitanju blanko direktorijum i on se preskače.
        if station in stations:
            if two_groups:
                station_num = int(station[1:])
                if station_num % 2 == 0:
                    station_directory = join(config.GROUP1_DIR,
                                             config.ASSIGNMENTS_PATH, station)
                    if not station in stations:
                        util.fatal_error(
                            'Racunar sa oznakom "{0}" nije pronadjen u spisku studenata koji rade proveru!'
                            .format(station))
                    group1_list.write('{0}, {1}, {2}\n'.format(
                        station, stations[station][0], stations[station][1]))
                else:
                    station_directory = join(config.GROUP2_DIR,
                                             config.ASSIGNMENTS_PATH, station)
                    if not station in stations:
                        util.fatal_error(
                            'Racunar sa oznakom "{0}" nije pronadjen u spisku studenata koji rade proveru!'
                            .format(station))
                    group2_list.write('{0}, {1}, {2}\n'.format(
                        station, stations[station][0], stations[station][1]))
            else:
                station_directory = join(config.ASSIGNMENTS_PATH, station)

            logging.debug(
                'Raspakivanje zadatka "{0}" u direktorijum "{1}"'.format(
                    a, station_directory))
            util.make_sure_path_exists(station_directory)
            command = config.EXTRACT_SINGLE_ASSIGNMENT_CMD.format(
                a, station_directory)
            ret = call(command, shell=True)
            if ret != 0:
                util.fatal_error(
                    'Raspakivanje pojedinacnog zadatka je neuspesno!\nKomanda koja je pokrenuta:\n{0}'
                    .format(command))
            os.remove(a)

    util.clear_directory(config.TEMP_PATH)
예제 #16
0
def run_test(
    dataset,
    clf_type,
    epochs,
    true_rh1,
    downsample_ratio,
    ordered_models_keys,
    list_of_images=range(10),
    suppress_error=False,
    verbose=False,
    pi1=0.0,
    one_vs_rest=True,
    cv_n_folds=3,
    early_stopping=True,
    pulearning=None,
):

    # Cast types to ensure consistency for 1 and 1.0, 0 and 0.0
    true_rh1 = float(true_rh1)
    downsample_ratio = float(downsample_ratio)
    pi1 = float(pi1)

    # Load MNIST or CIFAR data
    (X_train_original,
     y_train_original), (X_test_original,
                         y_test_original) = get_dataset(dataset=dataset)
    X_train_original, y_train_original = downsample(X_train_original,
                                                    y_train_original,
                                                    downsample_ratio)

    # Initialize models and result storage
    metrics = {key: [] for key in ordered_models_keys}
    data_all = {"metrics": metrics, "calculated": {}, "errors": {}}
    start_time = dt.now()

    # Run through the ten images class of 0, 1, ..., 9
    for image in list_of_images:
        if one_vs_rest:
            # X_train and X_test will not be modified. All data will be used. Adjust pointers.
            X_train = X_train_original
            X_test = X_test_original

            # Relabel the image data. Make label 1 only for given image.
            y_train = np.array(y_train_original == image, dtype=int)
            y_test = np.array(y_test_original == image, dtype=int)
        else:  # one_vs_other
            # Reducing the dataset to just contain our image and image = 4
            other_image = 4 if image != 4 else 7
            X_train = X_train_original[(y_train_original == image) |
                                       (y_train_original == other_image)]
            y_train = y_train_original[(y_train_original == image) |
                                       (y_train_original == other_image)]
            X_test = X_test_original[(y_test_original == image) |
                                     (y_test_original == other_image)]
            y_test = y_test_original[(y_test_original == image) |
                                     (y_test_original == other_image)]

            # Relabel the data. Make label 1 only for given image.
            y_train = np.array(y_train == image, dtype=int)
            y_test = np.array(y_test == image, dtype=int)

        print()
        print("Evaluating image:", image)
        print("Number of positives in y:", sum(y_train))
        print()
        sys.stdout.flush()

        s = y_train * (np.cumsum(y_train) < (1 - true_rh1) * sum(y_train))
        # In the presence of mislabeled negative (negative incorrectly labeled positive):
        # pi1 is the fraction of mislabeled negative in the labeled set:
        num_mislabeled = int(sum(y_train) * (1 - true_rh1) * pi1 / (1 - pi1))
        if num_mislabeled > 0:
            negative_set = s[y_train == 0]
            mislabeled = np.random.choice(len(negative_set),
                                          num_mislabeled,
                                          replace=False)
            negative_set[mislabeled] = 1
            s[y_train == 0] = negative_set

        print("image = {0}".format(image))
        print(
            "Training set: total = {0}, positives = {1}, negatives = {2}, P_noisy = {3}, N_noisy = {4}"
            .format(len(X_train), sum(y_train),
                    len(y_train) - sum(y_train), sum(s),
                    len(s) - sum(s)))
        print("Testing set:  total = {0}, positives = {1}, negatives = {2}".
              format(len(X_test), sum(y_test),
                     len(y_test) - sum(y_test)))

        # Fit different models for PU learning
        for key in ordered_models_keys:
            fit_start_time = dt.now()
            print("\n\nFitting {0} classifier. Default classifier is {1}.".
                  format(key, clf_type))

            if clf_type == "logreg":
                clf = LogisticRegression()
            elif clf_type == "cnn":
                from classifier_cnn import CNN
                from keras import backend as K
                K.clear_session()
                clf = CNN(
                    dataset_name=dataset,
                    num_category=2,
                    epochs=epochs,
                    early_stopping=early_stopping,
                    verbose=1,
                )
            else:
                raise ValueError(
                    "clf_type must be either logreg or cnn for this testing file."
                )

            ps1 = sum(s) / float(len(s))
            py1 = sum(y_train) / float(len(y_train))
            true_rh0 = pi1 * ps1 / float(1 - py1)

            model = get_model(
                key=key,
                rh1=true_rh1,
                rh0=true_rh0,
                clf=clf,
            )

            try:
                if key == "True Classifier":
                    model.fit(X_train, y_train)
                elif key in [
                        "Rank Pruning", "Rank Pruning (noise rates given)",
                        "Liu16 (noise rates given)"
                ]:
                    model.fit(X_train,
                              s,
                              pulearning=pulearning,
                              cv_n_folds=cv_n_folds)
                elif key in ["Nat13 (noise rates given)"]:
                    model.fit(X_train, s, pulearning=pulearning)
                else:  # Elk08, Baseline
                    model.fit(X_train, s)

                pred = model.predict(X_test)
                # Produces only P(y=1|x) for pulearning models because they are binary
                pred_prob = model.predict_proba(X_test)
                pred_prob = pred_prob[:,
                                      1] if key == "True Classifier" else pred_prob

                # Compute metrics
                metrics_dict = get_metrics(pred, pred_prob, y_test)
                elapsed = (dt.now() - fit_start_time).total_seconds()

                if verbose:
                    print(
                        "\n{0} Model Performance at image {1}:\n=================\n"
                        .format(key, image))
                    print("Time Required", elapsed)
                    print("AUC:", metrics_dict["AUC"])
                    print("Error:", metrics_dict["Error"])
                    print("Precision:", metrics_dict["Precision"])
                    print("Recall:", metrics_dict["Recall"])
                    print("F1 score:", metrics_dict["F1 score"])
                    print("rh1:", model.rh1 if hasattr(model, 'rh1') else None)
                    print("rh0:", model.rh0 if hasattr(model, 'rh0') else None)
                    print()

                metrics_dict["image"] = image
                metrics_dict["time_seconds"] = elapsed
                metrics_dict["rh1"] = model.rh1 if hasattr(model,
                                                           'rh1') else None
                metrics_dict["rh0"] = model.rh0 if hasattr(model,
                                                           'rh0') else None

                # Append dictionary of error and loss metrics
                if key not in data_all["metrics"]:
                    data_all["metrics"][key] = [metrics_dict]
                else:
                    data_all["metrics"][key].append(metrics_dict)
                data_all["calculated"][(key, image)] = True

            except Exception as e:
                msg = "Error in {0}, image {1}, rh1 {2}, m {3}: {4}\n".format(
                    key, image, true_rh1, pi1, e)
                print(msg)
                make_sure_path_exists("failed_models/")
                with open("failed_models/" + key + ".txt", "ab") as f:
                    f.write(msg)
                if suppress_error:
                    continue
                else:
                    raise
    return data_all
def ddqn_rankBatch_train(env, scheduler, optimizer_constructor, model_type,
                         batch_size, rp_start, rp_size, exp_frame, exp_initial,
                         exp_final, inital_beta, gamma, target_update_steps,
                         frames_per_epoch, frames_per_state, output_directory,
                         last_checkpoint):
    """
	Implementation of the training algorithm for DDQN using Rank-based prioritization.
	Information with regards to the algorithm can be found in the paper, 
	"Prioritized Experience Replay" by Tom Schaul, John Quan, Ioannis Antonoglou and
	David Silver. Refer to section 3.3 in the paper for more info. 
	"""

    gym.undo_logger_setup()
    logging.basicConfig(filename='ddqn_rank_training.log', level=logging.INFO)
    num_actions = env.action_space.n
    env.reset()

    print('No. of actions: ', num_actions)
    print(env.unwrapped.get_action_meanings())

    # initialize action value and target network with the same weights
    model = DQN(num_actions, use_bn=False)
    target = DQN(num_actions, use_bn=False)

    if use_cuda:
        model.cuda()
        target.cuda()

    frames_count = 1

    if last_checkpoint:
        model.load_state_dict(torch.load(last_checkpoint))
        print(last_checkpoint)
        print('weights loaded...')

        exp_replay = util.initialize_rank_replay_resume(
            env, rp_start, rp_size, frames_per_state, model, target, gamma,
            batch_size)
        frames_count = get_index_from_checkpoint_path(last_checkpoint)

    else:
        exp_replay = util.initialize_rank_replay(env, rp_start, rp_size,
                                                 frames_per_state, model,
                                                 target, gamma)

    target.load_state_dict(model.state_dict())

    optimizer = optimizer_constructor.type(
        model.parameters(),
        lr=optimizer_constructor.kwargs['lr'],
        alpha=optimizer_constructor.kwargs['alpha'],
        eps=optimizer_constructor.kwargs['eps'])

    episodes_count = 1
    frames_per_episode = 1
    epsiodes_durations = []
    rewards_per_episode = 0
    rewards_duration = []
    loss_per_epoch = []

    current_state, _, _, _ = util.play_game(env, frames_per_state)
    print('Starting training...')

    count = 0

    while True:

        epsilon = scheduler.anneal_linear(frames_count)
        choice = random.uniform(0, 1)

        # epsilon greedy algorithm
        if choice <= epsilon:
            action = LongTensor([[random.randrange(num_actions)]])

        else:
            action = util.get_greedy_action(model, current_state)

        curr_obs, reward, done, _ = util.play_game(env, frames_per_state,
                                                   action[0][0])

        rewards_per_episode += reward
        reward = Tensor([[reward]])
        current_state_ex = Variable(current_state, volatile=True)
        curr_obs_ex = Variable(curr_obs, volatile=True)
        action_ex = Variable(action, volatile=True)
        reward_ex = Variable(reward, volatile=True)

        #compute td-error for one sample
        td_error = ddqn_compute_td_error(batch_size=1,
                                         state_batch=current_state_ex,
                                         reward_batch=reward_ex,
                                         action_batch=action_ex,
                                         next_state_batch=curr_obs_ex,
                                         model=model,
                                         target=target,
                                         gamma=gamma)

        td_error = torch.abs(td_error)
        exp_replay.push(current_state_ex, action_ex, reward_ex, curr_obs_ex,
                        td_error)
        current_state = curr_obs

        # compute y
        if len(exp_replay) >= batch_size:
            # Get batch samples
            obs_samples, obs_ranks, obs_priorityVals = exp_replay.sample(
                batch_size)
            obs_priorityTensor = torch.from_numpy(np.array(obs_priorityVals))
            p_batch = 1 / obs_priorityTensor
            w_batch = (1 / len(exp_replay) * p_batch)**inital_beta
            max_weight = exp_replay.get_max_weight(inital_beta)
            params_grad = []

            for i in range(len(obs_samples)):
                sample = obs_samples[i]
                sample.state.volatile = False
                sample.next_state.volatile = False
                sample.reward.volatile = False
                sample.action.volatile = False
                loss = ddqn_compute_y(batch_size=1,
                                      state_batch=sample.state,
                                      reward_batch=sample.reward,
                                      action_batch=sample.action,
                                      next_state_batch=sample.next_state,
                                      model=model,
                                      target=target,
                                      gamma=gamma)
                loss_abs = torch.abs(loss)
                exp_replay.update(obs_ranks[i], loss_abs)

                for param in model.parameters():
                    if param.grad is not None:
                        param.grad.data.zero_()

                loss.backward()

                #accumulate weight change
                if i == 0:
                    for param in model.parameters():
                        tmp = ((w_batch[i] / max_weight) *
                               loss.data[0]) * param.grad.data
                        params_grad.append(tmp)

                else:
                    paramIndex = 0
                    for param in model.parameters():
                        tmp = ((w_batch[i] / max_weight) *
                               loss.data[0]) * param.grad.data
                        params_grad[paramIndex] = tmp + params_grad[paramIndex]
                        paramIndex += 1

            # update weights
            paramIndex = 0
            for param in model.parameters():
                param.data += params_grad[paramIndex].mul(
                    optimizer_constructor.kwargs['lr']).type(Tensor)
                paramIndex += 1

        frames_count += 1
        frames_per_episode += frames_per_state

        if done:
            rewards_duration.append(rewards_per_episode)
            rewards_per_episode = 0
            frames_per_episode = 1
            episodes_count += 1
            env.reset()
            current_state, _, _, _ = util.play_game(env, frames_per_state)

            if episodes_count % 100 == 0:
                avg_episode_reward = sum(rewards_duration) / 100.0
                avg_reward_content = 'Episode from', episodes_count - 99, ' to ', episodes_count, ' has an average of ', avg_episode_reward, ' reward and loss of ', sum(
                    loss_per_epoch)
                print(avg_reward_content)
                logging.info(avg_reward_content)
                rewards_duration = []
                loss_per_epoch = []

        # update weights of target network for every TARGET_UPDATE_FREQ steps
        if frames_count % target_update_steps == 0:
            target.load_state_dict(model.state_dict())
            # print('weights updated at frame no. ', frames_count)

        #Save weights every 250k frames
        if frames_count % 250000 == 0:
            util.make_sure_path_exists(output_directory + model_type + '/')
            torch.save(model.state_dict(),
                       'rank_weights_' + str(frames_count) + '.pth')

        #Print frame count and sort experience replay for every 1000000 (one million) frames:
        if frames_count % 1000000 == 0:
            training_update = 'frame count: ', frames_count, 'episode count: ', episodes_count, 'epsilon: ', epsilon
            print(training_update)
            logging.info(training_update)
            exp_replay.sort()