def storm_run(loader, net, epoch, num_classes, behavior): if (behavior == 0): net.SetTrainMode() else: net.SetPredictMode() batch_time = AverageMeter() data_time = AverageMeter() losses = AverageMeter() top1 = AverageMeter() top5 = AverageMeter() end = time.time() outputs = torch.zeros([args.train_batch, num_classes], dtype=torch.float) p_output = IntPtr.op_Explicit(Int64(outputs.data_ptr())) net.Init() bar = Bar('Processing', max=len(loader)) for batch_idx, (inputs, targets) in enumerate(loader): # measure data loading time data_time.update(time.time() - end) #print(inputs.shape) ## cpu data. p_input = IntPtr.op_Explicit(Int64(inputs.data_ptr())) p_target = IntPtr.op_Explicit(Int64(targets.data_ptr())) # compute output #outputs = model(inputs) #loss = criterion(outputs, targets) loss = net.Run(p_input, p_target, p_output, args.train_batch) # measure accuracy and record loss prec1, prec5 = accuracy(outputs.data, targets.data, topk=(1, 5)) losses.update(loss, inputs.size(0)) top1.update(prec1, inputs.size(0)) top5.update(prec5, inputs.size(0)) # measure elapsed time batch_time.update(time.time() - end) end = time.time() # plot progress bar.suffix = '({batch}/{size}) Data: {data:.3f}s | Batch: {bt:.3f}s | Total: {total:} | ETA: {eta:} | Loss: {loss:.4f} | top1: {top1: .4f} | top5: {top5: .4f}'.format( batch=batch_idx + 1, size=len(loader), data=data_time.avg, bt=batch_time.avg, total=bar.elapsed_td, eta=bar.eta_td, loss=losses.avg, top1=top1.avg, top5=top5.avg, ) bar.next() bar.finish() net.Complete() return (losses.avg, top1.avg)
def __init__(self, title, url, width, height, resizable, fullscreen, min_size, webview_ready): self.Text = title self.AutoScaleBaseSize = Size(5, 13) self.ClientSize = Size(width, height) self.MinimumSize = Size(min_size[0], min_size[1]) # Application icon try: # Try loading an icon embedded in the exe file. This will crash when frozen with PyInstaller handler = windll.kernel32.GetModuleHandleW(None) icon_handler = windll.user32.LoadIconW(handler, 1) self.Icon = Icon.FromHandle( IntPtr.op_Explicit(Int32(icon_handler))) except: pass self.webview_ready = webview_ready self.web_browser = WinForms.WebBrowser() self.web_browser.Dock = WinForms.DockStyle.Fill if url: self.web_browser.Navigate(url) self.Controls.Add(self.web_browser) self.is_fullscreen = False self.Shown += self.on_shown if fullscreen: self.toggle_fullscreen()
def __init__(self, window): self.uid = window.uid self.pywebview_window = window self.url = None self.Text = window.title self.Size = Size(window.initial_width, window.initial_height) self.MinimumSize = Size(window.min_size[0], window.min_size[1]) self.BackColor = ColorTranslator.FromHtml(window.background_color) if window.initial_x is not None and window.initial_y is not None: self.move(window.initial_x, window.initial_y) else: self.StartPosition = WinForms.FormStartPosition.CenterScreen self.AutoScaleDimensions = SizeF(96.0, 96.0) self.AutoScaleMode = WinForms.AutoScaleMode.Dpi if not window.resizable: self.FormBorderStyle = WinForms.FormBorderStyle.FixedSingle self.MaximizeBox = False if window.minimized: self.WindowState = WinForms.FormWindowState.Minimized # Application icon handle = windll.kernel32.GetModuleHandleW(None) icon_path = os.path.join(os.path.dirname(os.path.realpath(importlib.util.find_spec("bcml").origin)), "data", "bcml.ico") icon_handle = windll.shell32.ExtractIconW(handle, icon_path, 0) if icon_handle != 0: self.Icon = Icon.FromHandle( IntPtr.op_Explicit(Int32(icon_handle)) ).Clone() windll.user32.DestroyIcon(icon_handle) self.closed = window.closed self.closing = window.closing self.shown = window.shown self.loaded = window.loaded self.url = window.url self.text_select = window.text_select self.on_top = window.on_top self.is_fullscreen = False if window.fullscreen: self.toggle_fullscreen() if window.frameless: self.frameless = window.frameless self.FormBorderStyle = 0 CEF.create_browser(window, self.Handle.ToInt32(), BrowserView.alert) self.Shown += self.on_shown self.FormClosed += self.on_close self.FormClosing += self.on_closing self.Resize += self.on_resize
def __init__(self, window): self.uid = window.uid self.pywebview_window = window self.real_url = None self.Text = window.title self.ClientSize = Size(window.width, window.height) self.MinimumSize = Size(window.min_size[0], window.min_size[1]) self.BackColor = ColorTranslator.FromHtml(window.background_color) if window.x is not None and window.y is not None: self.move(window.x, window.y) else: self.StartPosition = WinForms.FormStartPosition.CenterScreen self.AutoScaleDimensions = SizeF(96.0, 96.0) self.AutoScaleMode = WinForms.AutoScaleMode.Dpi if not window.resizable: self.FormBorderStyle = WinForms.FormBorderStyle.FixedSingle self.MaximizeBox = False # Application icon handle = windll.kernel32.GetModuleHandleW(None) icon_handle = windll.shell32.ExtractIconW(handle, sys.executable, 0) if icon_handle != 0: self.Icon = Icon.FromHandle(IntPtr.op_Explicit(Int32(icon_handle))).Clone() windll.user32.DestroyIcon(icon_handle) self.shown = window.shown self.loaded = window.loaded self.url = window.url self.text_select = window.text_select self.is_fullscreen = False if window.fullscreen: self.toggle_fullscreen() if window.frameless: self.frameless = window.frameless self.FormBorderStyle = 0 if is_cef: CEF.create_browser(window, self.Handle.ToInt32(), BrowserView.alert) elif is_edge: self.browser = BrowserView.EdgeHTML(self, window) else: self.browser = BrowserView.MSHTML(self, window) self.Shown += self.on_shown self.FormClosed += self.on_close if is_cef: self.Resize += self.on_resize if window.confirm_close: self.FormClosing += self.on_closing
def __init__(self, title, url, width, height, resizable, fullscreen, min_size, confirm_quit, background_color, debug, webview_ready): self.Text = title self.ClientSize = Size(width, height) self.MinimumSize = Size(min_size[0], min_size[1]) self.BackColor = ColorTranslator.FromHtml(background_color) if not resizable: self.FormBorderStyle = WinForms.FormBorderStyle.FixedSingle self.MaximizeBox = False # Application icon handle = windll.kernel32.GetModuleHandleW(None) icon_handle = windll.shell32.ExtractIconW(handle, sys.executable, 0) if icon_handle != 0: self.Icon = Icon.FromHandle( IntPtr.op_Explicit(Int32(icon_handle))).Clone() windll.user32.DestroyIcon(icon_handle) self.webview_ready = webview_ready self.web_browser = WinForms.WebBrowser() self.web_browser.Dock = WinForms.DockStyle.Fill self.web_browser.ScriptErrorsSuppressed = True self.web_browser.IsWebBrowserContextMenuEnabled = False self.web_browser.WebBrowserShortcutsEnabled = False # HACK. Hiding the WebBrowser is needed in order to show a non-default background color. Tweaking the Visible property # results in showing a non-responsive control, until it is loaded fully. To avoid this, we need to disable this behaviour # for the default background color. if background_color != '#FFFFFF': self.web_browser.Visible = False self.first_load = True else: self.first_load = False self.cancel_back = False self.web_browser.PreviewKeyDown += self.on_preview_keydown self.web_browser.Navigating += self.on_navigating self.web_browser.DocumentCompleted += self.on_document_completed if url: self.web_browser.Navigate(url) self.Controls.Add(self.web_browser) self.is_fullscreen = False self.Shown += self.on_shown if confirm_quit: self.FormClosing += self.on_closing if fullscreen: self.toggle_fullscreen()
def __init__(self, uid, title, url, width, height, resizable, fullscreen, min_size, confirm_quit, background_color, debug, js_api, text_select, frameless, webview_ready): self.uid = uid self.Text = title self.ClientSize = Size(width, height) self.MinimumSize = Size(min_size[0], min_size[1]) self.BackColor = ColorTranslator.FromHtml(background_color) self.AutoScaleDimensions = SizeF(96.0, 96.0) self.AutoScaleMode = WinForms.AutoScaleMode.Dpi if not resizable: self.FormBorderStyle = WinForms.FormBorderStyle.FixedSingle self.MaximizeBox = False # Application icon handle = windll.kernel32.GetModuleHandleW(None) icon_handle = windll.shell32.ExtractIconW(handle, sys.executable, 0) if icon_handle != 0: self.Icon = Icon.FromHandle(IntPtr.op_Explicit(Int32(icon_handle))).Clone() windll.user32.DestroyIcon(icon_handle) self.webview_ready = webview_ready self.load_event = Event() self.background_color = background_color self.url = url self.is_fullscreen = False if fullscreen: self.toggle_fullscreen() if frameless: self.frameless = frameless self.FormBorderStyle = 0 if is_cef: CEF.create_browser(self.uid, self.Handle.ToInt32(), BrowserView.alert, url, js_api) else: self._create_mshtml_browser(url, js_api, debug) self.text_select = text_select self.Shown += self.on_shown self.FormClosed += self.on_close if is_cef: self.Resize += self.on_resize if confirm_quit: self.FormClosing += self.on_closing
def __init__(self, uid, title, url, width, height, resizable, fullscreen, min_size, confirm_quit, background_color, debug, js_api, text_select, frameless, webview_ready): self.uid = uid self.Text = title self.ClientSize = Size(width, height) self.MinimumSize = Size(min_size[0], min_size[1]) self.BackColor = ColorTranslator.FromHtml(background_color) self.AutoScaleDimensions = SizeF(96.0, 96.0) self.AutoScaleMode = WinForms.AutoScaleMode.Dpi if not resizable: self.FormBorderStyle = WinForms.FormBorderStyle.FixedSingle self.MaximizeBox = False # Application icon handle = windll.kernel32.GetModuleHandleW(None) icon_handle = windll.shell32.ExtractIconW(handle, sys.executable, 0) if icon_handle != 0: self.Icon = Icon.FromHandle(IntPtr.op_Explicit(Int32(icon_handle))).Clone() windll.user32.DestroyIcon(icon_handle) self.webview_ready = webview_ready self.load_event = Event() self.background_color = background_color self.url = url self.is_fullscreen = False if fullscreen: self.toggle_fullscreen() if frameless: self.frameless = frameless self.FormBorderStyle = 0 if is_cef: CEF.create_browser(self.uid, self.Handle.ToInt32(), BrowserView.alert, url, js_api) else: self._create_mshtml_browser(url, js_api, debug) self.text_select = text_select self.Shown += self.on_shown self.FormClosed += self.on_close if is_cef: self.Resize += self.on_resize if confirm_quit: self.FormClosing += self.on_closing
def _fetch_getdev(self): clist = self._clean_channel_list() if self._inbuffer == None: return None #This conversion is much faster than doing # v=array(list(buf.GetDataAsVolts())) buf = self._inbuffer fullsize = buf.BufferSizeInSamples validsize = buf.ValidSamples v=np.ndarray(validsize, dtype=float) Marshal.Copy(buf.GetDataAsVolts(), 0, IntPtr.op_Explicit(Int32(v.ctypes.data)), len(v)) num_channel = len(clist) if num_channel != 1 and self.nb_samples.getcache() != 1: v.shape = (-1, num_channel) v = v.T return v
def _fetch_getdev(self): clist = self._clean_channel_list() if self._inbuffer is None: return None #This conversion is much faster than doing # v=array(list(buf.GetDataAsVolts())) buf = self._inbuffer fullsize = buf.BufferSizeInSamples validsize = buf.ValidSamples v = np.ndarray(validsize, dtype=float) Marshal.Copy(buf.GetDataAsVolts(), 0, IntPtr.op_Explicit(Int64(v.ctypes.data)), len(v)) num_channel = len(clist) if num_channel != 1 and self.nb_samples.getcache() != 1: v.shape = (-1, num_channel) v = v.T return v
def __init__(self, title, url, width, height, resizable, fullscreen, min_size, confirm_quit, webview_ready): self.Text = title self.ClientSize = Size(width, height) self.MinimumSize = Size(min_size[0], min_size[1]) if not resizable: self.FormBorderStyle = WinForms.FormBorderStyle.FixedSingle self.MaximizeBox = False # Application icon handle = windll.kernel32.GetModuleHandleW(None) icon_handle = windll.shell32.ExtractIconW(handle, sys.executable, 0) if icon_handle != 0: self.Icon = Icon.FromHandle( IntPtr.op_Explicit(Int32(icon_handle))).Clone() windll.user32.DestroyIcon(icon_handle) self.webview_ready = webview_ready self.web_browser = WinForms.WebBrowser() self.web_browser.Dock = WinForms.DockStyle.Fill self.web_browser.ScriptErrorsSuppressed = True self.web_browser.IsWebBrowserContextMenuEnabled = False self.cancel_back = False self.web_browser.PreviewKeyDown += self.on_preview_keydown self.web_browser.Navigating += self.on_navigating if url: self.web_browser.Navigate(url) self.Controls.Add(self.web_browser) self.is_fullscreen = False self.Shown += self.on_shown if confirm_quit: self.FormClosing += self.on_closing if fullscreen: self.toggle_fullscreen()
def __init__(self, title, url, width, height, resizable, fullscreen, min_size, confirm_quit, webview_ready): self.Text = title self.ClientSize = Size(width, height) self.MinimumSize = Size(min_size[0], min_size[1]) if not resizable: self.FormBorderStyle = WinForms.FormBorderStyle.FixedSingle self.MaximizeBox = False # Application icon handle = windll.kernel32.GetModuleHandleW(None) icon_handle = windll.shell32.ExtractIconW(handle, sys.executable, 0) if icon_handle != 0: self.Icon = Icon.FromHandle(IntPtr.op_Explicit(Int32(icon_handle))).Clone() windll.user32.DestroyIcon(icon_handle) self.webview_ready = webview_ready self.web_browser = WinForms.WebBrowser() self.web_browser.Dock = WinForms.DockStyle.Fill self.web_browser.ScriptErrorsSuppressed = True self.web_browser.IsWebBrowserContextMenuEnabled = False self.cancel_back = False self.web_browser.PreviewKeyDown += self.on_preview_keydown self.web_browser.Navigating += self.on_navigating if url: self.web_browser.Navigate(url) self.Controls.Add(self.web_browser) self.is_fullscreen = False self.Shown += self.on_shown if confirm_quit: self.FormClosing += self.on_closing if fullscreen: self.toggle_fullscreen()
def __init__(self, title, url, width, height, resizable, fullscreen, min_size, confirm_quit, webview_ready): self.Text = title self.ClientSize = Size(width, height) self.MinimumSize = Size(min_size[0], min_size[1]) self.AutoScaleDimensions = SizeF(96.0, 96.0) self.AutoScaleMode = WinForms.AutoScaleMode.Dpi if not resizable: self.FormBorderStyle = WinForms.FormBorderStyle.FixedSingle self.MaximizeBox = False # Application icon try: # Try loading an icon embedded in the exe file. This will crash when frozen with PyInstaller handler = windll.kernel32.GetModuleHandleW(None) icon_handler = windll.user32.LoadIconW(handler, 1) self.Icon = Icon.FromHandle( IntPtr.op_Explicit(Int32(icon_handler))) except: pass self.webview_ready = webview_ready self.web_browser = WinForms.WebBrowser() self.web_browser.Dock = WinForms.DockStyle.Fill self.web_browser.ScriptErrorsSuppressed = True self.web_browser.IsWebBrowserContextMenuEnabled = False if url: self.web_browser.Navigate(url) self.Controls.Add(self.web_browser) self.is_fullscreen = False self.Shown += self.on_shown if confirm_quit: self.FormClosing += self.on_closing if fullscreen: self.toggle_fullscreen()
def __init__(self, uid, title, url, width, height, resizable, fullscreen, min_size, confirm_quit, background_color, debug, js_api, text_select, webview_ready): self.uid = uid self.Text = title self.ClientSize = Size(width, height) self.MinimumSize = Size(min_size[0], min_size[1]) self.BackColor = ColorTranslator.FromHtml(background_color) self.AutoScaleDimensions = SizeF(96.0, 96.0) self.AutoScaleMode = WinForms.AutoScaleMode.Dpi if not resizable: self.FormBorderStyle = WinForms.FormBorderStyle.FixedSingle self.MaximizeBox = False # Application icon handle = windll.kernel32.GetModuleHandleW(None) icon_handle = windll.shell32.ExtractIconW(handle, sys.executable, 0) if icon_handle != 0: self.Icon = Icon.FromHandle( IntPtr.op_Explicit(Int32(icon_handle))).Clone() windll.user32.DestroyIcon(icon_handle) self.webview_ready = webview_ready self.load_event = Event() self.web_browser = WebBrowserEx() self.web_browser.Dock = WinForms.DockStyle.Fill self.web_browser.ScriptErrorsSuppressed = not debug self.web_browser.IsWebBrowserContextMenuEnabled = debug self.web_browser.WebBrowserShortcutsEnabled = False self.web_browser.DpiAware = True self.web_browser.ScriptErrorsSuppressed = not debug self.web_browser.IsWebBrowserContextMenuEnabled = debug self.js_result_semaphore = Semaphore(0) self.js_bridge = BrowserView.JSBridge() self.js_bridge.parent_uid = uid self.web_browser.ObjectForScripting = self.js_bridge self.text_select = text_select if js_api: self.js_bridge.api = js_api # HACK. Hiding the WebBrowser is needed in order to show a non-default background color. Tweaking the Visible property # results in showing a non-responsive control, until it is loaded fully. To avoid this, we need to disable this behaviour # for the default background color. if background_color != '#FFFFFF': self.web_browser.Visible = False self.first_load = True else: self.first_load = False self.cancel_back = False self.web_browser.PreviewKeyDown += self.on_preview_keydown self.web_browser.Navigating += self.on_navigating self.web_browser.NewWindow3 += self.on_new_window self.web_browser.DownloadComplete += self.on_download_complete self.web_browser.DocumentCompleted += self.on_document_completed if url: self.web_browser.Navigate(url) else: self.web_browser.DocumentText = default_html self.url = url self.Controls.Add(self.web_browser) self.is_fullscreen = False self.Shown += self.on_shown self.FormClosed += self.on_close if confirm_quit: self.FormClosing += self.on_closing if fullscreen: self.toggle_fullscreen()
def __init__(self, window): self.uid = window.uid self.pywebview_window = window self.real_url = None self.Text = window.title self.Size = Size(window.initial_width, window.initial_height) self.MinimumSize = Size(window.min_size[0], window.min_size[1]) if window.transparent: # window transparency is not supported, as webviews are not transparent. self.BackColor = Color.LimeGreen self.TransparencyKey = Color.LimeGreen self.SetStyle( WinForms.ControlStyles.SupportsTransparentBackColor, True) else: self.BackColor = ColorTranslator.FromHtml( window.background_color) if window.initial_x is not None and window.initial_y is not None: self.move(window.initial_x, window.initial_y) else: self.StartPosition = WinForms.FormStartPosition.CenterScreen self.AutoScaleDimensions = SizeF(96.0, 96.0) self.AutoScaleMode = WinForms.AutoScaleMode.Dpi if not window.resizable: self.FormBorderStyle = WinForms.FormBorderStyle.FixedSingle self.MaximizeBox = False if window.minimized: self.WindowState = WinForms.FormWindowState.Minimized # Application icon handle = kernel32.GetModuleHandleW(None) icon_handle = windll.shell32.ExtractIconW(handle, sys.executable, 0) if icon_handle != 0: self.Icon = Icon.FromHandle( IntPtr.op_Explicit(Int32(icon_handle))).Clone() windll.user32.DestroyIcon(icon_handle) self.closed = window.closed self.closing = window.closing self.shown = window.shown self.loaded = window.loaded self.url = window.real_url self.text_select = window.text_select self.on_top = window.on_top self.is_fullscreen = False if window.fullscreen: self.toggle_fullscreen() if window.frameless: self.frameless = window.frameless self.FormBorderStyle = 0 if is_cef: CEF.create_browser(window, self.Handle.ToInt32(), BrowserView.alert) elif is_chromium: self.browser = Chromium.EdgeChrome(self, window) elif is_edge: self.browser = Edge.EdgeHTML(self, window) else: self.browser = IE.MSHTML(self, window) self.Shown += self.on_shown self.FormClosed += self.on_close self.FormClosing += self.on_closing if is_cef: self.Resize += self.on_resize
def ptr(offset: int): return IntPtr.op_Explicit(Int32(offset))