def validate_hvd(val_loader, model, epoch, writer, verbose, early_stopping, hvd, start): model.eval() val_loss = utils.Metric('val_loss', hvd) val_top1 = utils.Metric('val_top1', hvd) val_top5 = utils.Metric('val_top5', hvd) with tqdm(total=len(val_loader), desc='Validate Epoch #{}'.format(epoch + 1), disable=not verbose) as t: with torch.no_grad(): for data, target in val_loader: data, target = data.cuda(), target.cuda() output = model(data) val_loss.update(F.cross_entropy(output, target)) prec1, prec5 = utils.accuracy(output, target, topk=(1, 5)) val_top1.update(prec1) val_top5.update(prec5) t.set_postfix({ 'loss': val_loss.avg.item(), 'top1': 100. * val_top1.avg.item(), 'top5': 100. * val_top5.avg.item() }) t.update(1) early_stopping(val_loss.avg.item(), model, ckpt_dir=config.path) if early_stopping.early_stop: print("Early stopping") utils.time(time.time() - start) os._exit(0) writer.add_scalar('val/loss', val_loss.avg, epoch) writer.add_scalar('val/top1', val_top1.avg, epoch) writer.add_scalar('val/top5', val_top5.avg, epoch) return val_top1.avg
def import_svmlight(path, headers=""): raw = h2o.lazy_import(path) if settings.debug and len(headers) < 100: print utils.time() + "import with headers: " + str(headers) #parsesetup = h2o.parse_setup(raw,column_names=headers) parsesetup = h2o.parse_setup( raw ) # Issue: H2O 3.8 tests length of header vs. columns, but still imports the "pseudotarget" additionally parsesetup['parse_type'] = 'SVMLight' loaded_frame = h2o.parse_raw(parsesetup) if settings.debug: print "......HEader length: " + str(len(headers)) print "......Frame imported: " + str(loaded_frame.ncol) if (len(headers) > loaded_frame.ncol): n = len(headers) - loaded_frame.ncol print "Remove last " + str(n) + " header entries" del headers[-n:] loaded_frame.set_names(headers) #Workaround, Set names now print "First column: " + loaded_frame.names[ 0] #needed because lazy name setting if settings.debug and len(headers) < 100: loaded_frame.head(show=True) loaded_frame.pop(0) #remove first ('pseudotarget') columnn #if loaded_frame.ncol>len(headers)-1: #workaround: H2O reads info from svmlight into columns -> remove everything that is not in headers # delete = [] # for i in xrange(len(headers)-1,loaded_frame.ncol): # delete.append(loaded_frame.names[i]) # loaded_frame = remove_vecs(loaded_frame,delete) if settings.debug and len(headers) < 100: loaded_frame.head(show=True) return loaded_frame
def derived_key(self): kDate = utils.sign(("AWS4" + self.secret_access_key).encode("utf-8"), utils.time("%Y%m%d")) kRegion = utils.sign(kDate, self.region) kService = utils.sign(kRegion, "glacier") kSigning = utils.sign(kService, "aws4_request") return kSigning
def __init__(self): # Input shape self.channels = 3 self.img_size = 64 self.latent_dim = 100 self.time = time() self.dataset_name = 'vdsr' self.learning_rate = 1e-4 optimizer = Adam(self.learning_rate, beta_1=0.5, decay=0.00005) self.gf = 64 # filter size of generator's last layer self.df = 64 # filter size of discriminator's first layer # Configure data loader self.data_loader = DataLoader(dataset_name=self.dataset_name, img_res=(self.img_size, self.img_size), mem_load=True) self.n_data = self.data_loader.get_n_data() self.generator = self.build_generator() print( "---------------------generator summary----------------------------" ) self.generator.summary() self.generator.compile(loss='mse', optimizer=optimizer, metrics=['mse']) # Build and compile the discriminator self.discriminator = self.build_discriminator() print( "\n---------------------discriminator summary----------------------------" ) self.discriminator.summary() self.discriminator.compile(loss='mse', optimizer=optimizer, metrics=['accuracy']) make_trainable(self.discriminator, False) z = Input(shape=(self.latent_dim, )) fake_img = self.generator(z) # for the combined model, we only train ganerator self.discriminator.trainable = False validity = self.discriminator(fake_img) self.combined = Model([z], [validity]) print( "\n---------------------combined summary----------------------------" ) self.combined.summary() self.combined.compile(loss=['binary_crossentropy'], optimizer=optimizer)
def string_to_sign(self, canonical_request): return "\n".join( [ "AWS4-HMAC-SHA256", self.header["x-amz-date"], "%(time)s/%(region)s/glacier/aws4_request\n%(hashthing)s" % {"time": utils.time("%Y%m%d"), "region": self.region, "hashthing": self.hexhash(canonical_request)}, ] )
def upload_svmlight(path, headers=""): loaded_frame = h2o.upload_file(path) if settings.debug and len(headers) < 100: print utils.time() + "import with headers: " + str(headers) if settings.debug: print "......HEader length: " + str(len(headers)) print "......Frame imported: " + str(loaded_frame.ncol) if (len(headers) > loaded_frame.ncol): n = len(headers) - loaded_frame.ncol print "Remove last " + str(n) + " header entries" del headers[-n:] loaded_frame.set_names(headers) print "First column: " + loaded_frame.names[ 0] #needed because lazy name setting if settings.debug and len(headers) < 100: loaded_frame.head(show=True) loaded_frame.pop(0) #remove first ('pseudotarget') columnn if settings.debug and len(headers) < 100: loaded_frame.head(show=True) return loaded_frame
def build_authorization_header(self): cr = self.canonical_request() sts = self.string_to_sign(cr) dk = self.derived_key() sgn = self.signature(dk, sts) return "AWS4-HMAC-SHA256 Credential="+self.access_key+"/"+\ utils.time("%Y%m%d")+"/"+self.region+\ "/glacier/aws4_request,SignedHeaders="+\ ";".join(self.signed_headers)+\ ",Signature="+sgn
def build_authorization_header(self): cr = self.canonical_request() sts = self.string_to_sign(cr) dk = self.derived_key() sgn = self.signature(dk,sts) return "AWS4-HMAC-SHA256 Credential="+self.access_key+"/"+\ utils.time("%Y%m%d")+"/"+self.region+\ "/glacier/aws4_request,SignedHeaders="+\ ";".join(self.signed_headers)+\ ",Signature="+sgn
def build_header(self,additional): header = { "host": self.host, "x-amz-glacier-version": "2012-06-01", "x-amz-date": utils.time("%Y%m%dT%H%M%SZ") } for key in additional.keys(): header[key] = additional[key] return header
def get_hours(since): list = db.get_hours(since=since) if utils.time() >= since: list.append({ "timestamp": utils.time_hour(), "click_count": click_count_hour, "event_count": event_count_hour, "click_count_total": click_count_total, "event_count_total": event_count_total }) return list
def build_header(self, additional): header = { "host": self.host, "x-amz-glacier-version": "2012-06-01", "x-amz-date": utils.time("%Y%m%dT%H%M%SZ") } for key in additional.keys(): header[key] = additional[key] return header
def _query( sparql, timeout, q, cache=LRUCache(maxsize=config.CACHE_SIZE), **_): """Cached low level function to perform a single SPARQL query. :param sparql: SPARQLWrapper endpoint :param timeout: a timeout in seconds. The endpoint 'timeout' parameter will be set to 3/4 this value in ms (Virtuoso seems to treat non zero timeouts < 1000ms as 1000ms), instructing the server to give us a partial result up to this soft limit. We also set a hard timeout via the socket to really cancel the request if there's no result after timeout seconds. :param q: The SPARQL Query as string :param cache: a cache object like cachetools.LRUCache or None :return: a (t, res) pair with querytime t as float and res as dict. """ assert isinstance(sparql, SPARQLWrapper.SPARQLWrapper) assert isinstance(q, six.string_types) sparql.resetQuery() sparql.setTimeout(timeout) sparql.setReturnFormat(SPARQLWrapper.JSON) # sparql.setMethod(SPARQLWrapper.POST) # sparql.setRequestMethod(SPARQLWrapper.POSTDIRECTLY) # set query timeout parameter to half the hard timeout time sparql.addParameter('timeout', str(int(timeout * 1000 * 3 / 4))) logger.debug('performing sparql query: \n%s', q) c = cache.get(q) if cache is not None else None if c is None: logger.debug('cache miss') try: q_short = ' '.join((line.strip() for line in q.split('\n'))) sparql.setQuery(q_short) c = time(sparql.queryAndConvert) except socket.timeout: c = (timeout, {}) except ValueError: # e.g. if the endpoint gives us bad JSON for some unicode chars logger.warning( 'Could not parse result for query, assuming empty result...\n' 'Query:\n%s\nException:', q, exc_info=1, # appends exception to message ) c = (timeout, {}) if cache is not None: cache[q] = c else: logger.debug('cache hit') t, res = c if logger.isEnabledFor(logging.DEBUG): logger.debug('orig query took %.4f s, result:\n%s\n', t, pformat(res)) return t, res
def _query( sparql, timeout, q, cache=LRUCache(maxsize=config.CACHE_SIZE), **_): """Cached low level function to perform a single SPARQL query. :param sparql: SPARQLWrapper endpoint :param timeout: a timeout in seconds. The endpoint 'timeout' parameter will be set to 3/4 this value in ms (Virtuoso seems to treat non zero timeouts < 1000ms as 1000ms), instructing the server to give us a partial result up to this soft limit. We also set a hard timeout via the socket to really cancel the request if there's no result after timeout seconds. :param q: The SPARQL Query as string :param cache: a cache object like cachetools.LRUCache or None :return: a (t, res) pair with querytime t as float and res as dict. """ assert isinstance(sparql, SPARQLWrapper.SPARQLWrapper) assert isinstance(q, six.string_types) sparql.resetQuery() sparql.setTimeout(timeout) sparql.setReturnFormat(SPARQLWrapper.JSON) # sparql.setMethod(SPARQLWrapper.POST) # sparql.setRequestMethod(SPARQLWrapper.POSTDIRECTLY) # set query timeout parameter to half the hard timeout time sparql.addParameter('timeout', str(int(timeout * 1000 * 3 / 4))) logger.debug('performing sparql query: \n%s', q) c = cache.get(q) if cache is not None else None if c is None: logger.debug('cache miss') try: q_short = ' '.join((line.strip() for line in q.split('\n'))) sparql.setQuery(q_short) c = time(sparql.queryAndConvert) except socket.timeout: c = (timeout, {}) except ValueError: # e.g. if the endpoint gives us bad JSON for some unicode chars logger.warning( 'Could not parse result for query, assuming empty result...\n' 'Query:\n%s\nException:', q, exc_info=1, # appends exception to message ) c = (timeout, {}) if cache is not None: cache[q] = c else: logger.debug('cache hit') t, res = c logger.debug('orig query took %.4f s, result:\n%s\n', t, res) return t, res
async def user_event(sid, *, user, name): # cache await user_auth(sid, name=user) global event_count_total, event_count_today, event_count_hour event_count_total += 1 event_count_today += 1 event_count_hour += 1 # user users[sid]["event_count_session"] += 1 # event if name not in ["rickroll", "exmatrikulieren"]: event = db.add_event(user=user, name=name, timestamp=utils.time()) await emit_message(f"{user} triggered {name}!") await sio.emit("event", event) else: await sio.emit("event", { "user": user, "name": name, "timestamp": utils.time() })
def image(name): if name == 'logo.png': return send_file('logo.png') elif name == 'time': time = utils.time() return render_template('time.html', time=time) else: try: org_link = Url.query.filter_by(short_link=name).first() except Exception: raise Exception if org_link is None: return render_template('not_link.html') return redirect(org_link.org_link)
def _call(self, method, data): self._correlation_id = str(uuid.uuid4()) self._response = None compressed_value = self._compressor.compress(data) tries_remaining = 2 while tries_remaining: tries_remaining -= 1 try: self._channel.basic_publish( exchange="", routing_key=method, properties=pika.BasicProperties( reply_to=constants.RABBIT_REPLYTO_QUEUE, correlation_id=self._correlation_id ), body=compressed_value, ) tries_remaining = 0 except pika.exceptions.ConnectionClosed: if tries_remaining: logger.info("Connection to queue was closed, reconnecting") self._connect() else: logger.error("Reconnect failed: {}".format(traceback.format_exc(limit=10))) raise sleep_interval_seconds = 0.1 end_ts = utils.time() + self._call_timeout while not self._response: if self._call_timeout and utils.time() >= end_ts: raise CallTimeoutException() self._connection.sleep(sleep_interval_seconds) # Pull the response out and return it return self._compressor.decompress(self._response)
async def hourly_task(): while True: current_time = utils.time() current_hour = utils.time_hour(current_time=current_time) next_hour = current_hour + utils.SECS_PER_HOUR await data.start_hour(timestamp=current_hour) # sleep until next hour remaining_secs = next_hour - current_time logging.debug(f"[{datetime.datetime.now()}] hourly_task sleeping for " f"{remaining_secs} seconds") await asyncio.sleep(remaining_secs) await data.end_hour(timestamp=current_hour)
def calibrate_query_timeout( sparql, factor=config.QUERY_TIMEOUT_FACTOR, q=None, n_queries=10): assert isinstance(sparql, SPARQLWrapper.SPARQLWrapper) logger.debug('calibrating query timer') total_time = 0. if q is None: q = 'select * where { ?s ?p ?o } limit 10' for _ in range(n_queries): sparql.resetQuery() sparql.setReturnFormat(SPARQLWrapper.JSON) sparql.setQuery(q) t, r = time(sparql.queryAndConvert) total_time += t avg = total_time / n_queries timeout = avg * factor timeout = max(timeout, config.QUERY_TIMEOUT_MIN) logger.info('Query timeout calibration: %d simplistic queries took %.3fs ' '(%.3fs avg). Setting timeout to %.3fs', n_queries, total_time, avg, timeout) return timeout
async def user_click(sid, *, user, comment, style): # cache await user_auth(sid, name=user) global click_count_total, click_count_today, click_count_hour click_count_total += 1 click_count_today += 1 click_count_hour += 1 # user users[sid]["click_count_session"] += 1 # click click = db.add_click(user=user, comment=comment, style=style, timestamp=utils.time()) click_count_session = users[sid]["click_count_session"] if click_count_session != 0 and (click_count_session % 100 == 0 or click_count_session == 50): await emit_message(f"{user} erreicht {click_count_session} Klicks!") await sio.emit("click", click)
def run(config): """Run a fixed length simulation based on the inputs provided in the config.""" # Init lookup table of parameters params = parameters.LookupTable(config) # Init MST model mst = model.MST(params, config) # Init LQG controller ctrl = controller.LQG(config["control"]) # Init reference signal ref = reference.Translator(params, config["control"]) # Init output plotter (will plot on exit) with output.Plotter(config) as out: # Main loop y = [mst.lmbda0, mst.flux] for t in time(config["time"])[0][1:]: r = ref(t) u = ctrl.control(r, y) y = mst.step(t, u) out.save(t, r, u, y, mst, ctrl)
def validate(val_loader, model, epoch, criterion, config, early_stopping, writer, start): batch_time = utils.AverageMeters('Time', ':6.3f') losses = utils.AverageMeters('Loss', ':.4e') top1 = utils.AverageMeters('Acc@1', ':6.2f') top5 = utils.AverageMeters('Acc@5', ':6.2f') if 'DALIClassificationIterator' in val_loader.__class__.__name__: progress = utils.ProgressMeter(math.ceil(val_loader._size / config.batch_size), batch_time, losses, top1, top5, prefix='Test: ') else: progress = utils.ProgressMeter(len(val_loader), batch_time, losses, top1, top5, prefix='Test: ') # switch to evaluate mode model.eval() with torch.no_grad(): end = time.time() if 'DALIClassificationIterator' in val_loader.__class__.__name__: for i, data in enumerate(val_loader): images = Variable(data[0]['data']) target = Variable( data[0]['label'].squeeze().cuda().long().cuda( non_blocking=True)) # compute output output = model(images) loss = criterion(output, target) # measure accuracy and record loss acc1, acc5 = utils.accuracy(output, target, topk=(1, 5)) if config.distributed: reduced_loss = reduce_tensor(loss.data, world_size=config.world_size) acc1 = reduce_tensor(acc1, world_size=config.world_size) acc5 = reduce_tensor(acc5, world_size=config.world_size) else: reduced_loss = loss.data losses.update(to_python_float(reduced_loss), images.size(0)) top1.update(to_python_float(acc1), images.size(0)) top5.update(to_python_float(acc5), images.size(0)) # measure elapsed time batch_time.update(time.time() - end) end = time.time() if i % config.print_freq == 0: progress.print(i) else: for i, (images, target) in enumerate(val_loader): images = images.cuda(device, non_blocking=True) target = target.cuda(device, non_blocking=True) # compute output output = model(images) loss = criterion(output, target) # measure accuracy and record loss acc1, acc5 = utils.accuracy(output, target, topk=(1, 5)) losses.update(loss.item(), images.size(0)) top1.update(acc1[0], images.size(0)) top5.update(acc5[0], images.size(0)) # measure elapsed time batch_time.update(time.time() - end) end = time.time() if i % config.print_freq == 0: progress.print(i) # TODO: this should also be done with the ProgressMeter print(' * Acc@1 {top1.avg:.3f} Acc@5 {top5.avg:.3f}'.format(top1=top1, top5=top5)) early_stopping(losses.avg, model, ckpt_dir=config.path) if early_stopping.early_stop: print("Early stopping") utils.time(time.time() - start) os._exit(0) writer.add_scalar('val/loss', losses.avg, epoch) writer.add_scalar('val/top1', top1.val, epoch) writer.add_scalar('val/top5', top5.val, epoch) return top1.avg
def string_to_sign(self,canonical_request): return "\n".join(["AWS4-HMAC-SHA256",self.header["x-amz-date"], utils.time("%Y%m%d")+"/us-east-1/glacier/aws4_request", utils.sha256(canonical_request)])
if settings.debug and len(headers) < 100: loaded_frame.head(show=True) loaded_frame.pop(0) #remove first ('pseudotarget') columnn #if loaded_frame.ncol>len(headers)-1: #workaround: H2O reads info from svmlight into columns -> remove everything that is not in headers # delete = [] # for i in xrange(len(headers)-1,loaded_frame.ncol): # delete.append(loaded_frame.names[i]) # loaded_frame = remove_vecs(loaded_frame,delete) if settings.debug and len(headers) < 100: loaded_frame.head(show=True) return loaded_frame #### #### STEP 0: clean output dir #### if settings.clean_output: print utils.time( ) + "Loesche Files im Output Verzeichnis " + settings.out_directory_client filelist = [ f for f in os.listdir(settings.out_directory_client) if os.path.isfile(os.path.join(settings.out_directory_client, f)) ] for f in filelist: os.remove(os.path.join(settings.out_directory_client, f)) #### #### STEP 1: init h2o, load data #### print utils.time() + "Initialisiere H2O Server: " + settings.ip + ":" + str( settings.port) h2o.init(ip=settings.ip, port=settings.port, start_h2o=False) h2o.remove_all()
import masks as mask import convolution as cv import gaussian_pyramid as gPyr import place_pyramid as pPyr import blending as bl import fourier as ft # Python uses the BGR color scheme input = cv2.imread('input/p1-1-0.png') # 2.1 filter = [mask.g_3, mask.g_7, mask.g_15] for i in range(len(filter)): time = ut.time() output = cv.convolve(cp.copy(input), filter[i]) print("Convolution time[", i, "]: ", time.elapsed()) cv2.imwrite('output/p1-2-1-{}.png'.format(i), output) time = ut.time() output = cv2.filter2D(cp.copy(input), -1, np.flip(np.flip(filter[i], 0), 1)) print("OpenCV Convolution time[", i, "]: ", time.elapsed()) print("") # 2.2 output = gPyr.gaussianPyramid(cp.copy(input), 3) for i in range(len(output)):
import matplotlib.pyplot as plt import numpy as np from matplotlib import cm from utils import time x = np.linspace(1, 6, 1000) #aiming itme y = np.linspace(1, 20, 1000) #modified dispersion arr = np.linspace(0, 20, 21) X, Y = np.meshgrid(x, y) Z = time(X, Y) fig, ax = plt.subplots(1, 1) cp = ax.contourf(X, Y, Z, cmap=cm.coolwarm, levels=arr) cbar = fig.colorbar(cp) cbar.ax.set_ylabel('Seconds to aim') cbar.set_ticks(arr) ax.set_xlabel('aiming time') #ax.set_xlim(0, 6) ax.set_ylabel('modified dispersion') #ax.set_ylim(0,20) plt.show() #plt.savefig('contour.png', dpi=600, format='png')
from indexed import IndexedOrderedDict import math ####### SETTINGS ########### settings = settings.development() #settings = settings.production() # save stdout old_stdout = sys.stdout #sys.stdout = open(settings.out_log, 'w') #### #### STEP 0: clean output dir #### if settings.clean_output: print utils.time( ) + "Loesche Files im Output Verzeichnis " + settings.out_directory_client filelist = [ f for f in os.listdir(settings.out_directory_client) if os.path.isfile(os.path.join(settings.out_directory_client, f)) ] for f in filelist: os.remove(os.path.join(settings.out_directory_client, f)) #### #### STEP 1: load data #### Result: scikit-learn csr matrix, headers #### headerfile = settings.in_directory_client + '/' + settings.colnames_file print utils.time() + "Lade Header File " + headerfile headers = functions.load_svm_headers4scipy(headerfile) print "......Header length: " + str(len(headers))
def string_to_sign(self,canonical_request): return "\n".join(["AWS4-HMAC-SHA256",self.header["x-amz-date"], "%(time)s/%(region)s/glacier/aws4_request\n%(hashthing)s" % {"time":utils.time("%Y%m%d"),"region": self.region,"hashthing":self.hexhash(canonical_request)}])
print "Remove last " + str(n) + " header entries" del headers[-n:] loaded_frame.set_names(headers) print "First column: " + loaded_frame.names[ 0] #needed because lazy name setting if settings.debug and len(headers) < 100: loaded_frame.head(show=True) loaded_frame.pop(0) #remove first ('pseudotarget') columnn if settings.debug and len(headers) < 100: loaded_frame.head(show=True) return loaded_frame #### #### STEP 0: clean output dir #### if settings.clean_output: print utils.time( ) + "Loesche Files im Output Verzeichnis " + settings.out_directory_client filelist = [ f for f in os.listdir(settings.out_directory_client) if os.path.isfile(os.path.join(settings.out_directory_client, f)) ] for f in filelist: os.remove(os.path.join(settings.out_directory_client, f)) #### #### STEP 1: init h2o, load data #### print utils.time() + "Initialisiere H2O Server: " + settings.ip + ":" + str( settings.port) h2o.init(ip=settings.ip, port=settings.port, start_h2o=False) #h2o.remove_all()
def string_to_sign(self,canonical_request): return "\n".join(["AWS4-HMAC-SHA256",self.header["x-amz-date"], "%(time)s/%(region)s/glacier/aws4_request" % \ {"time":utils.time("%Y%m%d"),"region": self.region}, utils.sha256(canonical_request)])
def main(): start = time.time() if not torch.cuda.is_available(): logging.info('no gpu device available') sys.exit(1) np.random.seed(config.seed) torch.manual_seed(config.seed) torch.cuda.manual_seed_all(config.seed) torch.backends.cudnn.benchmark = True CLASSES = 1000 channels = SEARCH_SPACE['channel_size'] strides = SEARCH_SPACE['strides'] # Model model = Network(channels, strides, CLASSES) model = model.to(device) model = nn.DataParallel(model, device_ids=config.gpus) logger.info("param size = %fMB", utils.count_parameters_in_MB(model)) config.world_size = 0 if config.target_hardware is None: config.ref_value = None else: config.ref_value = ref_values[config.target_hardware][ '%.2f' % config.width_mult] # Loss criterion = LatencyLoss(config, channels, strides).cuda(config.gpus) normal_critersion = nn.CrossEntropyLoss() alpha_weight = model.module.arch_parameters() # weight = [param for param in model.parameters() if not utils.check_tensor_in_list(param, alpha_weight)] weight = model.module.weight_parameters() # Optimizer w_optimizer = torch.optim.SGD(weight, config.w_lr, momentum=config.w_momentum, weight_decay=config.w_weight_decay) alpha_optimizer = torch.optim.Adam(alpha_weight, lr=config.alpha_lr, betas=(config.arch_adam_beta1, config.arch_adam_beta2), eps=config.arch_adam_eps, weight_decay=config.alpha_weight_decay) train_data = get_imagenet_torch( type='train', # image_dir="/googol/atlas/public/cv/ILSVRC/Data/" # use soft link `mkdir ./data/imagenet && ln -s /googol/atlas/public/cv/ILSVRC/Data/CLS-LOC/* ./data/imagenet/` image_dir=config.data_path + "/" + config.dataset.lower(), batch_size=config.batch_size, num_threads=config.workers, world_size=config.world_size, crop=224, device_id=0, num_gpus=len(config.gpus), portion=config.train_portion) valid_data = get_imagenet_torch( type='val', # image_dir="/googol/atlas/public/cv/ILSVRC/Data/" # use soft link `mkdir ./data/imagenet && ln -s /googol/atlas/public/cv/ILSVRC/Data/CLS-LOC/* ./data/imagenet/` image_dir=config.data_path + "/" + config.dataset.lower(), batch_size=config.batch_size, num_threads=config.workers, world_size=config.world_size, crop=224, device_id=0, num_gpus=len(config.gpus), portion=config.val_portion) best_top1 = 0. best_genotype = list() lr = 0 config.start_epoch = -1 config.warmup_epoch = 0 config.warmup = True ### Resume form warmup model or train model ### if config.resume: try: model_path = config.path + '/checkpoint.pth.tar' model, w_optimizer, alpha_optimizer = load_model( model, model_fname=model_path, optimizer=w_optimizer, arch_optimizer=alpha_optimizer) except Exception: warmup_path = config.path + '/warmup.pth.tar' if os.path.exists(warmup_path): print('load warmup weights') model, w_optimizer, alpha_optimizer = load_model( model, model_fname=warmup_path, optimizer=w_optimizer, arch_optimizer=alpha_optimizer) else: print('fail to load models') w_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR( w_optimizer, float(config.epochs), eta_min=config.w_lr_min) if config.start_epoch < 0 and config.warm_up: for epoch in range(config.warmup_epoch, config.warmup_epochs): # warmup train_top1, train_loss = warm_up(train_data, valid_data, model, normal_critersion, criterion, w_optimizer, epoch, writer) config.start_epoch = epoch update_schedule = utils.get_update_schedule_grad(len(train_data), config) for epoch in range(config.start_epoch + 1, config.epochs): if epoch > config.warmup_epochs: w_scheduler.step() lr = w_scheduler.get_lr()[0] logger.info('epoch %d lr %e', epoch, lr) # training train_top1, train_loss = train(train_data, valid_data, model, normal_critersion, criterion, w_optimizer, alpha_optimizer, lr, epoch, writer, update_schedule) logger.info('Train top1 %f', train_top1) # validation top1 = train_top1 if epoch % 10 == 0: top1, loss = infer(valid_data, model, epoch, criterion, normal_critersion, writer) logger.info('valid top1 %f', top1) genotype = model.module.genotype() logger.info("genotype = {}".format(genotype)) # save if best_top1 < top1: best_top1 = top1 best_genotype = genotype is_best = True else: is_best = False save_model(model, { 'warmup': False, 'epoch': epoch, 'w_optimizer': w_optimizer.state_dict(), 'alpha_optimizer': alpha_optimizer.state_dict(), 'state_dict': model.state_dict() }, is_best=is_best) utils.time(time.time() - start) logger.info("Final best Prec@1 = {:.4%}".format(best_top1)) logger.info("Best Genotype = {}".format(best_genotype))
def main(argv=sys.argv): active = True try: while active: screen = curses.initscr() screen.border(0) screen.addstr(2, 2, " \u2022\u2022") screen.addstr(3, 2, " \u2022\u2022") screen.addstr( 4, 2, " \u2022\u2022\u2022\u2022\u2022\u2022 \ \u2022\u2022\u2022\u2022\u2022\u2022 \ \u2022\u2022\u2022\u2022\u2022\u2022\u2022", ) screen.addstr( 5, 2, " \u2022\u2022 \u2022\u2022 \ \u2022\u2022 \u2022\u2022 \u2022\u2022", ) screen.addstr( 6, 2, " \u2022\u2022 \u2022 \ \u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022 \u2022\u2022 \u2022\u2022 \ - Once a day", ) screen.addstr( 7, 2, " \u2022\u2022 \u2022\u2022 \u2022\u2022 \ \u2022\u2022 \u2022\u2022 \u2022\u2022", ) screen.addstr( 8, 2, " \u2022\u2022\u2022\u2022\u2022\u2022 \ \u2022\u2022\u2022\u2022\u2022\u2022\u2022 \ \u2022\u2022\u2022\u2022\u2022\u2022\u2022", ) screen.addstr(11, 4, "1 - Mailsettings") screen.addstr(12, 4, "2 - Time Settings") screen.addstr(13, 4, "3 - Notifications") screen.addstr(14, 4, "4 - Exit") screen.refresh() choice = screen.getch() if choice == ord("4"): active = False curses.endwin() elif choice == ord("1"): # TODO: write function utils.mail() curses.endwin() elif choice == ord("2"): # TODO: write function utils.time() curses.endwin() elif choice == ord("3"): utils.notification() curses.endwin() except Exception as e: curses.endwin() windows = True if platform.system() == "Windows" else False clear = "cls" if windows else "clear" subprocess.run(clear) print("Once-a-day exited because of an Exception:\n {}".format(e))
def string_to_sign(self, canonical_request): return "\n".join(["AWS4-HMAC-SHA256",self.header["x-amz-date"], "%(time)s/%(region)s/glacier/aws4_request" % \ {"time":utils.time("%Y%m%d"),"region": self.region}, utils.sha256(canonical_request)])
def __init__(self, params, config): # Get splines for magnetic fields, ohmic power and magnetic energy self.spl_B_phi = params.B_phi self.spl_B_theta = params.B_theta self.spl_P_ohm = params.P_ohm self.spl_U_mag = params.U_mag # Parameters self.mu0 = config["mu0"] aspect_ratio = config["aspect_ratio"] self.a = config["a"] self.R0 = aspect_ratio * self.a R_star = 0.969 * self.R0 # FIXME: use config file zneo = config["zneo"] zeff = config["zeff"] self.zohm = (0.4 + 0.6 * zeff) * zneo self.alpha = 4.0 # FIXME: using hardcoded fixed alpha for now # Time discretization t, num_t = time(config["time"]) self.tmin = t[num_t / 10] self.dt = config["time"]["dt"] # Parameters specific to simulation mode self.mode = config["mode"] if self.mode not in config: raise ValueError( "parameters for mode '%s' missing from config file" % self.mode) config_mode = config[self.mode] # if "alpha" in config_mode: # config_alpha = config_mode["alpha"] # alpha = sp.interp1d(loadVec(config_alpha["time"]), loadVec(config_alpha["value"])) if "flux" in config_mode: config_flux = config_mode["flux"] flux0 = config_flux["ref"] * config_flux["multiplier"] else: flux0 = config_mode["flux_ref"] * config_mode["flux_multiplier"] if "density" in config_mode: config_density = config_mode["density"] self.density = sp.interp1d(loadVec(config_density["time"]), loadVec(config_density["value"])) else: self.density = lambda t: config_mode["density_ref"] * config_mode[ "density_multiplier"] config_phi = config_mode["toroidal"] V_phi_wave = np.interp(t, loadVec(config_phi["time"]), loadVec(config_phi["voltage"])) self.V_phi_DC = config_phi["DC_voltage"] config_theta = config_mode["poloidal"] V_theta_wave = np.interp(t, loadVec(config_theta["time"]), loadVec(config_theta["voltage"])) config_anom = config_mode["anomalous"] self.zanom_wave = sp.interp1d(config_anom["time"], config_anom["voltage"]) if "OPCD" in config_mode: config_opcd = config_mode["OPCD"] t_start = config_opcd["t_start"] freq = config_opcd["freq"] domain = (t >= t_start) & (t <= t_start + config_opcd["cycles"] / freq) V_theta_wave -= config_opcd["max_voltage"] * np.sin( 2 * np.pi * freq * t) * domain if "OFCD" in config_mode: config_ofcd = config_mode["OFCD"] t_start = config_ofcd["t_start"] freq = config_ofcd["freq"] cycles = config_ofcd["cycles"] phase = config_ofcd["phase"] sin_wave = config_ofcd["max_voltage"] * np.sin( 2 * np.pi * freq * t) theta_domain = (t >= t_start) & (t <= t_start + cycles / freq) V_theta_wave[theta_domain] -= sin_wave[theta_domain] offset = int(phase / (freq * (t[1] - t[0]))) phi_domain = np.roll(theta_domain, -offset) V_phi_wave[phi_domain] = V_phi_wave[ theta_domain] - 10 * sin_wave[theta_domain] if "buck" in config_mode: config_buck = config_mode["buck"] V_phi_wave += np.interp(t, config_buck["time"], config_buck["voltage"]) if "Te" in config_mode: config_Te = config_mode["Te"] PPCD_Te_mult += np.interp(t, config_Te["time"], config_Te["voltage"]) self.V_theta_wave = sp.interp1d(t, V_theta_wave) self.V_phi_wave = sp.interp1d(t, V_phi_wave) I_phi_0 = config["initial"]["I_phi"] # Initialize state variables self.flux = flux0 self.lmbda0 = params.B_theta_to_lmbda0(self.mu0 * self.a * I_phi_0 / flux0) self.I_phi = I_phi_0 self.I_theta = self.spl_B_phi( self.alpha, self.lmbda0) * flux0 * self.R0 / (self.a**2 * self.mu0) self.V_phi = self.V_phi_wave(t[0]) self.V_theta = self.V_theta_wave(t[0]) self.P_ohm = 0.0 self.eta0 = 0.0 # Initialize ODE solver self.solver = si.ode(self.lfDot) self.solver.set_integrator("dopri5") self.solver.set_initial_value([self.lmbda0, self.flux], t[0])
def __init__(self, config): # Parameters self.mu0 = config["mu0"] aspect_ratio = config["aspect_ratio"] self.a = config["a"] self.R0 = aspect_ratio * self.a # Time discretization self.t, num_t = time(config["time"]) self.tf = config["time"]["tf"] # Mode-dependent parameters mode = config["mode"] if mode not in config: raise ValueError( "parameters for mode '%s' missing from config file" % mode) config_mode = config[mode] self.P_ohm_over_I_phi_shot = np.interp( self.t, loadVec(config_mode["time"]), loadVec(config_mode["P_ohm_over_I_phi"])) self.f_shot = np.interp(self.t, loadVec(config_mode["time"]), loadVec(config_mode["f"])) self.theta_shot = np.interp(self.t, loadVec(config_mode["time"]), loadVec(config_mode["theta"])) config_phi = config_mode["toroidal"] self.I_phi_shot = np.interp(self.t, loadVec(config_phi["time"]), loadVec(config_phi["current"])) self.V_phi_shot = np.interp(self.t, loadVec(config_phi["time"]), loadVec(config_phi["voltage"])) config_theta = config_mode["poloidal"] self.I_theta_shot = np.interp(self.t, loadVec(config_theta["time"]), loadVec(config_theta["current"])) self.V_theta_shot = np.interp(self.t, loadVec(config_theta["time"]), loadVec(config_theta["voltage"])) config_flux = config_mode["flux"] self.flux_shot = np.interp(self.t, loadVec(config_flux["time"]), loadVec(config_flux["value"])) # Init arrays, hopefully with their final size, otherwise expandable self.flux = np.zeros(num_t) self.lmbda0 = np.zeros(num_t) self.I_phi = np.zeros(num_t) self.I_theta = np.zeros(num_t) self.V_phi = np.zeros(num_t) self.V_theta = np.zeros(num_t) self.P_ohm = np.zeros(num_t) self.eta0 = np.zeros(num_t) self.I_phi_primary = np.zeros(num_t) self.I_theta_primary = np.zeros(num_t) # Current index self.index = 1 # Convert voltages to primary current config_primary = config["primary"] self.coeff_phi = config_primary["coeff_phi"] self.coeff_theta = config_primary["coeff_theta"] self.R_phi = config_primary["R_phi"] self.R_theta = config_primary["R_theta"] self.L_phi = config_primary["L_phi"] self.L_theta = config_primary["L_theta"] self.solver = si.ode(self.primaryCurrentDot) self.solver.set_integrator("dopri5") self.solver.set_initial_value( [self.V_phi_shot[0], self.V_theta_shot[0]], self.t[0])
def main(): start = time.time() logger.info( "Logger is set - training start, Let`s get ready to the rumble!!! ") global best_acc1 early_stopping = utils.EarlyStopping(patience=config.patience, verbose=True) if config.multiprocessing_distributed: if config.dataset.lower() != "imagenet": raise NameError( "Not Imagenet dataset, if you really need mutil node, change the code for own dataset" ) os._exit(0) import horovod.torch as hvd # horovod set hvd.init() # set default gpu device id torch.cuda.set_device(hvd.local_rank()) # set seed np.random.seed(config.seed) torch.manual_seed(config.seed) torch.cuda.manual_seed_all(config.seed) torch.backends.cudnn.benchmark = True # If set > 0, will resume training from a given checkpoint. resume_from_epoch = 0 for try_epoch in range(config.epochs, 0, -1): if os.path.exists( config.checkpoint_format.format(epoch=try_epoch)): resume_from_epoch = try_epoch break # Horovod: broadcast resume_from_epoch from rank 0 (which will have # checkpoints) to other ranks. resume_from_epoch = hvd.broadcast(torch.tensor(resume_from_epoch), root_rank=0, name='resume_from_epoch').item() # Horovod: print logs on the first worker. verbose = 1 if hvd.rank() == 0 else 0 # Horovod: write TensorBoard logs on first worker. hvd_writer = SummaryWriter(log_dir=os.path.join( config.path, "tb_hvd")) if hvd.rank() == 0 else None kwargs = { 'num_workers': config.workers, 'pin_memory': True } if len(config.gpus) > 0 else {} train_dataset = \ datasets.ImageFolder(config.data_path+config.dataset+"/train", transform=transforms.Compose([ transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ])) # Horovod: use DistributedSampler to partition data among workers. Manually specify # `num_replicas=hvd.size()` and `rank=hvd.rank()`. train_sampler = torch.utils.data.distributed.DistributedSampler( train_loader, num_replicas=hvd.size(), rank=hvd.rank()) train_loader = torch.utils.data.DataLoader( train_dataset, batch_size=config.allreduce_batch_size, sampler=train_sampler, **kwargs) if 'efficientnet' in config.arch: image_size = EfficientNet.EfficientNet.get_image_size(config.arch) val_dataset = \ datasets.ImageFolder(config.data_path+config.dataset+"/val", transform=transforms.Compose([ transforms.Resize(image_size, interpolation=PIL.Image.BICUBIC), transforms.CenterCrop(image_size), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ])) print('Using image size', image_size) else: val_dataset = \ datasets.ImageFolder(config.data_path+config.dataset+"/val", transform=transforms.Compose([ transforms.Resize(256, interpolation=PIL.Image.BICUBIC), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ])) print('Using image size', 224) val_sampler = torch.utils.data.distributed.DistributedSampler( val_dataset, num_replicas=hvd.size(), rank=hvd.rank()) valid_loader = torch.utils.data.DataLoader( val_dataset, batch_size=config.val_batch_size, sampler=val_sampler, **kwargs) model = EfficientNet.EfficientNet.from_name(config.arch) # model = EfficientNet.from_pretrained('efficientnet-b0') # online model = model.to(device) # Horovod: scale learning rate by the number of GPUs. # Gradient Accumulation: scale learning rate by batches_per_allreduce optimizer = torch.optim.SGD( model.parameters(), lr=(config.lr * config.batches_per_allreduce * hvd.size()), momentum=config.momentum, weight_decay=config.weight_decay) # Horovod: (optional) compression algorithm. compression = hvd.Compression.fp16 if config.fp16_allreduce else hvd.Compression.none # Horovod: wrap optimizer with DistributedOptimizer. optimizer = hvd.DistributedOptimizer( optimizer, named_parameters=model.named_parameters(), compression=compression, backward_passes_per_step=config.batches_per_allreduce) # Restore from a previous checkpoint, if initial_epoch is specified. # Horovod: restore on the first worker which will broadcast weights to other workers. if resume_from_epoch > 0 and hvd.rank() == 0: filepath = config.checkpoint_format.format(epoch=resume_from_epoch) checkpoint = torch.load(filepath) model.load_state_dict(checkpoint['model']) optimizer.load_state_dict(checkpoint['optimizer']) # Horovod: broadcast parameters & optimizer state. hvd.broadcast_parameters(model.state_dict(), root_rank=0) hvd.broadcast_optimizer_state(optimizer, root_rank=0) criterion = nn.CrossEntropyLoss().cuda(device) # optionally resume from a checkpoint if config.resume: if os.path.isfile(config.resume): print("=> loading checkpoint '{}'".format(config.resume)) checkpoint = torch.load(config.resume) config.start_epoch = checkpoint['epoch'] best_acc1 = checkpoint['best_acc1'] if config.gpus is not None: # best_acc1 may be from a checkpoint from a different GPU best_acc1 = best_acc1.to(config.gpus) model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) print("=> loaded checkpoint '{}' (epoch {})".format( config.resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(config.resume)) if config.evaluate: validate_hvd(valid_loader, model, 0, hvd_writer, verbose, early_stopping, hvd, start) for epoch in range(config.start_epoch, config.epochs): train_sampler.set_epoch(epoch) # train for one epoch train_hvd(train_loader, model, optimizer, epoch, config, hvd_writer, verbose, hvd) # evaluate on validation set validate_hvd(valid_loader, model, epoch, hvd_writer, verbose, early_stopping, hvd, start) utils.time(time.time() - start) logger.info("Final best Prec@1 = {:.4%}".format(best_acc1)) else: # set default gpu device id if config.fp16_allreduce or config.distributed: torch.cuda.set_device(config.local_rank % len(config.gpus)) torch.distributed.init_process_group(backend='nccl', init_method='env://') config.world_size = torch.distributed.get_world_size() else: torch.cuda.set_device(config.gpus[0]) config.world_size = 1 config.total_batch = config.world_size * config.batch_size # set seed np.random.seed(config.seed) torch.manual_seed(config.seed) torch.cuda.manual_seed_all(config.seed) torch.backends.cudnn.benchmark = True if config.static_loss_scale != 1.0: if not config.fp16_allreduce: print( "Warning: if --fp16_allreduce is not used, static_loss_scale will be ignored." ) if config.dataset.lower() == "imagenet": train_loader = get_imagenet_iter_dali( type='train', # image_dir="/googol/atlas/public/cv/ILSVRC/Data/" # use soft link `mkdir ./data/imagenet && ln -s /googol/atlas/public/cv/ILSVRC/Data/CLS-LOC/* ./data/imagenet/` image_dir=config.data_path + config.dataset.lower(), batch_size=config.batch_size, num_threads=config.workers, world_size=config.world_size, local_rank=config.local_rank, crop=224, device_id=config.local_rank, num_gpus=config.gpus, dali_cpu=config.dali_cpu) if 'efficientnet' in config.arch: image_size = EfficientNet.EfficientNet.get_image_size( config.arch) val_transforms = transforms.Compose([ transforms.Resize(image_size, interpolation=PIL.Image.BICUBIC), transforms.CenterCrop(image_size), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) print('Using image size', image_size) valid_loader = torch.utils.data.DataLoader( datasets.ImageFolder( config.data_path + config.dataset.lower() + "/val", val_transforms), batch_size=config.batch_size, shuffle=False, num_workers=config.workers, pin_memory=True) else: valid_loader = get_imagenet_iter_dali( type='val', image_dir=config.data_path + config.dataset.lower(), batch_size=config.batch_size, num_threads=config.workers, world_size=config.world_size, local_rank=config.local_rank, crop=224, device_id=config.local_rank, num_gpus=config.gpus) elif config.dataset.lower() == "cifar10": if 'efficientnet' in config.arch: raise NameError("don`t use cifar10 train efficientnet") os._exit(0) train_loader = get_cifar_iter_dali(type='train', image_dir=config.data_path + config.dataset.lower(), batch_size=config.batch_size, num_threads=config.workers, world_size=config.world_size, local_rank=config.local_rank) valid_loader = get_cifar_iter_dali(type='val', image_dir=config.data_path + config.dataset.lower(), batch_size=config.batch_size, num_threads=config.workers, world_size=config.world_size, local_rank=config.local_rank) else: raise NameError("No Support dataset config") ''' @description: we need define model here! ''' model = EfficientNet.EfficientNet.from_name(config.arch) # model = EfficientNet.from_pretrained('efficientnet-b0') # online model = model.to(device) if config.fp16_allreduce: model = network_to_half(model) if config.distributed: # shared param/delay all reduce turns off bucketing in DDP, for lower latency runs this can improve perf # for the older version of APEX please use shared_param, for newer one it is delay_allreduce model = DDP(model, delay_allreduce=True) # define loss function (criterion) and optimizer criterion = nn.CrossEntropyLoss().cuda(config.gpus) optimizer = torch.optim.SGD(model.parameters(), config.lr, momentum=config.momentum, weight_decay=config.weight_decay) if config.fp16_allreduce: optimizer = FP16_Optimizer( optimizer, static_loss_scale=config.static_loss_scale, dynamic_loss_scale=config.dynamic_loss_scale) # optionally resume from a checkpoint if config.resume: if os.path.isfile(config.resume): print("=> loading checkpoint '{}'".format(config.resume)) checkpoint = torch.load(config.resume) config.start_epoch = checkpoint['epoch'] best_acc1 = checkpoint['best_acc1'] if config.gpus is not None: # best_acc1 may be from a checkpoint from a different GPU best_acc1 = best_acc1.to(config.gpus) model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) print("=> loaded checkpoint '{}' (epoch {})".format( config.resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(config.resume)) if config.evaluate: res = validate(valid_loader, model, 0, criterion, config, early_stopping, writer, start) with open('res.txt', 'w') as f: print(res, file=f) return for epoch in range(config.start_epoch, config.epochs): # train for one epoch train(train_loader, model, criterion, optimizer, epoch, config, writer) # evaluate on validation set best_acc1 = validate(valid_loader, model, epoch, criterion, config, early_stopping, writer, start) # remember best acc@1 and save checkpoint #is_best = acc1 > best_acc1 #best_acc1 = max(acc1, best_acc1) #utils.save_checkpoint(model, config.path, is_best) train_loader.reset() if 'efficientnet' not in config.arch: valid_loader.reset() utils.time(time.time() - start) logger.info("Final best Prec@1 = {:.4%} use {} s".format(best_acc1, ))