retval['halign'] = image.halign return retval def main(images): temp_dc = wx.MemoryDC() temp_dc.SelectObject(destbitmap) mimg = MultiImage(images) drawrect = wx.Rect(10, 10, 220, 440) mimg.draw(temp_dc, drawrect) temp_dc.SelectObject(wx.NullBitmap) if __name__ == '__main__': import util, syck from skins import images as imgmngr from skins import skins app = wx.PySimpleApp() # image['source'] = 'skins/default/checkerboard9.png' skins.res_path = "../../res/" destbitmap = imgmngr.get('skins/default/blue-flower.jpg') f = file("../../res/skins/skinExample") images = to_storage(syck.load(f)).Images f.close() util.profile(main, images) destbitmap.SaveFile('C:/workspace/Digsby/res/skins/default/output.png', wx.BITMAP_TYPE_PNG)
# -*- coding: utf-8 -*- """ add your model discription and calculate the computation! """ import keras from util import profile # TODO: RELUs # TODO: BatchNorms # Residual Paths model = keras.applications.resnet50.ResNet50(weights=None) # look at model model.summary() # run profile layer_name, layer_flops, inshape, weights = profile(model) # visualize results for name, flop, shape, weight in zip(layer_name, layer_flops, inshape, weights): print("layer:", name, shape, " MegaFLOPS:", flop/1e6, " MegaWeights:", weight/1e6) print("Total FLOPS[GFLOPS]:", sum(layer_flops)/1e9) # TODO: summarize results as dict
def main(): logger.info("Loading data...") tokenizer = BertTokenizer.from_pretrained('bert-base-chinese') train_loader = util.profile('train.txt', tokenizer) # test_loader = util.profile('test.txt', tokenizer) dev_loader = util.profile('dev.txt', tokenizer) logger.info("Build model...") bert = BertModel.from_pretrained('bert-base-chinese') model = fn_cls(bert) criterion = nn.BCELoss() sigmoid = nn.Sigmoid() max_epoch = args.epoch model.cuda() model.train() optim = torch.optim.Adam(model.parameters(), lr=1e-5) for epoch in range(1, max_epoch + 1): tacc = [] losslis = [] # train epoch train_time = time.time() for batch_idx, (data, target) in enumerate(train_loader): data = data.cuda() target = target.cuda() mask = [] for sample in data: mask.append([1 if i != 0 else 0 for i in sample]) mask = torch.tensor(mask).cuda() output = model(data, attention_mask=mask) pred = torch.argmax(output, 1) tacc.append( np.mean((pred == torch.argmax(target, 1)).cpu().numpy())) loss = criterion(sigmoid(output).view(-1, 10), target) losslis.append(loss.item()) loss.backward() optim.step() optim.zero_grad() logger.info('Train Epoch: {} \tLoss:{:.6f} Acc:{}'.format( epoch, np.mean(losslis), 100 * np.mean(tacc))) logger.info('time:{}'.format((time.time() - train_time))) # evaluate epoch model.eval() li = [] acc = [] for batch_idx, (data, target) in enumerate(dev_loader): data, target = torch.tensor(data).cuda(), torch.tensor( target).cuda() mask = [] for sample in data: mask.append([1 if i != 0 else 0 for i in sample]) mask = torch.tensor(mask).cuda() output = model(data, attention_mask=mask) pred = torch.argmax(output, 1) acc.append(np.mean((pred == target).cpu().numpy())) eval_loss = criterion(sigmoid(output).view(-1, 10), target) li.append(eval_loss.item()) logger.info('-' * 50) logger.info('Validate Epoch: {} acc:{:.2f}%\tLoss:{:.6f}'.format( 1, np.mean(acc) * 100, np.mean(li)))
def __init__(self, trial, x, y, num_steps, data_path, log_path, learning_rate=0.5, norm=False, radius=None, tensorboard=False, gpu=None): ''' trial: string, name of the trial data: tensorflow dataset iterator x, y: int, dimension of the map feat_nr: int, number of inpt_features num_steps: int, number of training steps norm: bool, if True then data will be normalized radius: int, if None use default radius max(x,y)/2 tensorboard: bool, if True, logs the graph and runs a performance test gpu: int, picks gpu from cluster by number ''' self.trial = trial self.data_path = data_path self.log_path = log_path self.x = x self.y = y self.num_steps = num_steps self.learning_rate = learning_rate self.radius = radius self.norm = norm self.tensorboard = tensorboard self.gpu = gpu if gpu != None: ########## ### PICK GPU ################### os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu) with tf.variable_scope("input"): with tf.variable_scope("data"): if norm is True: self.data = normalize( np.load(data_path).astype(np.float32)) else: self.data = np.load(data_path).astype(np.float32) self.feat_nr = len(self.data[0]) self.nr_inst = len(self.data) with tf.variable_scope("pipeline"): inpt = pipe(self.data, prefetch=20) # step count self.step = tf.placeholder(tf.float32) # create weight matrix self.weights = tf.get_variable( "weights", shape=[self.x * self.y, self.feat_nr], initializer=tf.random_uniform_initializer) #initializer=tf.random_normal_initializer) with tf.variable_scope("locations"): self.locations = tf.constant( np.array( list([ np.array([i, j]) for i in range(self.x) for j in range(self.y) ]))) if radius == None: self.radius = tf.constant(max(self.x, self.y) / 2, dtype=tf.float32) #Calculate current learning rate and radius # todo: different decay options with tf.variable_scope("decay"): decay = tf.cast(1 - (self.step / self.num_steps), dtype=tf.float32) with tf.variable_scope("current_lr"): current_lr = self.learning_rate * decay with tf.variable_scope("current_radius"): current_radius = self.radius * decay with tf.variable_scope("bmu"): # calculate Best Matching Unit (euclidean distances) distances = tf.sqrt(tf.reduce_sum((self.weights - inpt)**2, 1)) bmu = tf.reduce_min(distances) #bmu = tf.argmin(distances, 0) with tf.variable_scope("bmu_loc"): # get the location of the bmu #mask = tf.pad(tf.reshape(bmu, [1]), tf.constant(np.array([[0, 1]]))) #size = tf.cast(tf.constant(np.array([1, 2])), dtype=tf.int64) #bmu_loc = tf.reshape(tf.slice(self.locations, mask, size), [2]) mask = tf.equal(distances, bmu) bmu_locs = tf.boolean_mask(self.locations, mask) bmu_loc = tf.slice(bmu_locs, [0, 0], [1, 2])[0] # make sure its only one location with tf.variable_scope("neighborhood"): # calculate the influence on the neighborhood of the bmu bmu_dist = tf.sqrt( tf.cast(tf.reduce_sum((self.locations - bmu_loc)**2, 1), dtype=tf.float32)) nbr_func = tf.exp(-bmu_dist / (2 * (current_radius**2))) with tf.variable_scope("delta"): # get the delta of the weights delta = tf.expand_dims(nbr_func, -1) * current_lr * (inpt - self.weights) with tf.variable_scope("new_weights"): #update the new weights new_weights = self.weights + delta self.train_step = tf.assign(self.weights, new_weights) # initialize session self.sess = tf.InteractiveSession() self.sess.run(tf.global_variables_initializer()) if self.tensorboard is True: wrtr = tf.summary.FileWriter( os.path.expanduser(self.log_path + self.trial)) wrtr.add_graph(self.sess.graph) profile(self.sess, wrtr, new_weights, feed_dict=None, prerun=3, tag='flow') wrtr.flush()
retval['halign'] = image.halign return retval def main(images): temp_dc = wx.MemoryDC(); temp_dc.SelectObject(destbitmap); mimg = MultiImage(images) drawrect = wx.Rect(10,10,220,440) mimg.draw(temp_dc, drawrect) temp_dc.SelectObject(wx.NullBitmap) if __name__ == '__main__': import util, syck from skins import images as imgmngr from skins import skins app = wx.PySimpleApp() # image['source'] = 'skins/default/checkerboard9.png' skins.res_path = "../../res/" destbitmap = imgmngr.get('skins/default/blue-flower.jpg') f = file("../../res/skins/skinExample") images = to_storage(syck.load(f)).Images f.close() util.profile(main, images) destbitmap.SaveFile('C:/workspace/Digsby/res/skins/default/output.png', wx.BITMAP_TYPE_PNG)
if not self.sizing: self.sizing = True dx, dy = x - self.orig[0], y - self.orig[1] self.SetSize( (self.origsize[0] + dx, self.origsize[1] + dy)) self.sizing = False elif self.click_state == 'moving': fp = (x - self.delta[0], y - self.delta[1]) self.Move(fp) def method_sizing(self): pass if __name__ == '__main__': import util import windowfx app = wx.PySimpleApp() from gui import skininit skininit('../../res', 'halloween') frame = Skindow(None, 'IMWin', title='Skindow Test') from uberwidgets.UberBook import SamplePanel frame.set_content(SamplePanel(frame.content, 'orange')) windowfx.fadein(frame) app.SetTopWindow(frame) util.profile(app.MainLoop)
wx.EVT_BUTTON, lambda e: self.capbar.ShowCapabilities( not self.capbar.cbar.IsShown())) b2.Bind( wx.EVT_BUTTON, lambda e: self.capbar.ShowToFrom(not self.capbar.tfbar.IsShown())) b3.Bind( wx.EVT_BUTTON, lambda e: self.capbar.ShowComposeButton( not self.capbar.bcompose.IsShown())) self.panel.Sizer.Add(b1) self.panel.Sizer.Add(b2) self.panel.Sizer.Add(b3) self.capbar.bsms.Bind(wx.EVT_BUTTON, self.OnButton) self.capbar.binfo.Bind(wx.EVT_BUTTON, lambda e: self.capbar.bsms.SendButtonEvent()) def OnButton(self, event): print "button clicked" def Go(): f = Frame() f.Show(True) if __name__ == '__main__': a = App(Go) from util import profile profile(a.MainLoop)
self.sizing = True dx, dy = x - self.orig[0], y - self.orig[1] self.SetSize( ( self.origsize[0] + dx, self.origsize[1] + dy ) ) self.sizing = False elif self.click_state == 'moving': fp = (x - self.delta[0], y - self.delta[1]) self.Move( fp ) def method_sizing(self): pass if __name__ == '__main__': import util import windowfx app = wx.PySimpleApp() from gui import skininit skininit('../../res','halloween') frame = Skindow( None,'IMWin', title='Skindow Test' ) from uberwidgets.UberBook import SamplePanel frame.set_content(SamplePanel(frame.content, 'orange')) windowfx.fadein( frame ) app.SetTopWindow( frame ) util.profile(app.MainLoop)
self.panel.Sizer.Add(self.capbar,0,wx.EXPAND) b1=wx.Button(self.panel,-1,'Hide Capabilities') b2=wx.Button(self.panel,-1,'Hide To/From') b3=wx.Button(self.panel,-1,'Hide Compose') b1.Bind(wx.EVT_BUTTON,lambda e: self.capbar.ShowCapabilities(not self.capbar.cbar.IsShown())) b2.Bind(wx.EVT_BUTTON,lambda e: self.capbar.ShowToFrom(not self.capbar.tfbar.IsShown())) b3.Bind(wx.EVT_BUTTON, lambda e: self.capbar.ShowComposeButton(not self.capbar.bcompose.IsShown())) self.panel.Sizer.Add(b1) self.panel.Sizer.Add(b2) self.panel.Sizer.Add(b3) self.capbar.bsms.Bind(wx.EVT_BUTTON,self.OnButton) self.capbar.binfo.Bind(wx.EVT_BUTTON,lambda e: self.capbar.bsms.SendButtonEvent()) def OnButton(self,event): print "button clicked" def Go(): f=Frame() f.Show(True) if __name__=='__main__': a = App( Go ) from util import profile profile(a.MainLoop)