使用MVC设计模式时如何加载重型模型

2024-03-07

我正在使用 wxPython 和使用 Tensorflow 创建的深度学习模型构建一个应用程序。我使用的设计模式是MVC。 我的问题是深度学习模型非常重,需要很长时间才能加载(大约 2 分钟),同时 GUI 会挂起。 我创建了一个描述该过程的示例代码。 这是加载时 GUI 的样子:

在此输入图像描述 https://i.stack.imgur.com/qdfmn.png

这是加载后 GUI 的样子:在此输入图像描述 https://i.stack.imgur.com/nFAag.png

问题是如何在模型加载时运行应用程序? 我还想在 GUI 中添加一个状态行,指示模型正在加载或已经加载。

我添加了示例代码来展示我的应用程序是如何构建的。

import wx
import time

class Model:
    def __init__(self):
        '''This part is simulating the loading of tensorflow'''
        x = 0
        while x < 15:
            time.sleep(1)
            print(x)
            x += 1

class View(wx.Frame):
    def __init__(self, parent, title):
        super(View, self).__init__(parent, title=title, size=(400, 400))
        self.InitUI()

    def InitUI(self):
        # Defines the GUI controls
        masterPanel = wx.Panel(self)
        masterPanel.SetBackgroundColour("gold")
        self.vbox = wx.BoxSizer(wx.VERTICAL)
        self.fgs = wx.FlexGridSizer(6, 2, 10, 25)
        id = wx.StaticText(self, label="ID:")
        firstName = wx.StaticText(self, label="First name:")
        lastName = wx.StaticText(self, label="Last name:")
        self.idTc = wx.TextCtrl(self)
        self.firstNameTc = wx.TextCtrl(self)
        self.lastNameTc = wx.TextCtrl(self)
        self.fgs.AddMany([id, (self.idTc, 1, wx.EXPAND),
                         firstName, (self.firstNameTc, 1, wx.EXPAND),
                         lastName, (self.lastNameTc, 1, wx.EXPAND)])
        self.vbox.Add(self.fgs, proportion=1, flag=wx.ALL | wx.EXPAND,
   border=15)
        self.SetSizer(self.vbox)
        self.vbox.Fit(self)
        self.Layout()

class Controller:
    def __init__(self):
        self.view = View(None, title='Test')
        self.view.Show()
        self.model = Model()

def main():
    app = wx.App()
    controller = Controller()
    app.MainLoop()

if __name__ == '__main__':
    main()

你可以使用_thread模块和PyPubSub模块以在模型加载期间保持主线程完全正常运行。

但是,请记住,如果您有wx.Button在 GUI 中绑定到method A and method A需要加载完整模型才能正确运行,然后用户将能够单击wx.Button并且程序可能会挂起,因为模型尚未完全加载。如果是这种情况,您可以使用以下方法Disable()(在加载模型时)和Enable()(加载模型后)以防止这种情况发生。

带注释的代码 (####)。

import wx
import time
import _thread
from pubsub import pub

class Model:
    def __init__(self):
        '''This part is simulating the loading of tensorflow'''
        x = 0
        while x < 15:
            time.sleep(1)
            print(x)
            #### This is how you broadcast the 'Loading' message 
            #### from a different thread.
            wx.CallAfter(pub.sendMessage, 'Loading', x=x)
            x += 1
        #### The same as before
        wx.CallAfter(pub.sendMessage, 'Loading', x=x)

class View(wx.Frame):
    def __init__(self, parent, title):
        super(View, self).__init__(parent, title=title, size=(400, 400))
        self.InitUI()

    def InitUI(self):
        # Defines the GUI controls
        #### It needed to set the size of the panel to cover the frame
        #### because it was not covering the entire frame before
        masterPanel = wx.Panel(self, size=(400, 400))
        masterPanel.SetBackgroundColour("gold")
        self.vbox = wx.BoxSizer(wx.VERTICAL)
        self.fgs = wx.FlexGridSizer(6, 2, 10, 25)
        id = wx.StaticText(self, label="ID:")
        firstName = wx.StaticText(self, label="First name:")
        lastName = wx.StaticText(self, label="Last name:")
        self.idTc = wx.TextCtrl(self)
        self.firstNameTc = wx.TextCtrl(self)
        self.lastNameTc = wx.TextCtrl(self)
        self.fgs.AddMany([id, (self.idTc, 1, wx.EXPAND),
                         firstName, (self.firstNameTc, 1, wx.EXPAND),
                         lastName, (self.lastNameTc, 1, wx.EXPAND)])
        self.vbox.Add(self.fgs, proportion=1, flag=wx.ALL | wx.EXPAND,
   border=15)
        self.SetSizer(self.vbox)
        self.vbox.Fit(self)
        self.Layout()

        #### Create status bar to show loading progress. 
        self.statusbar = self.CreateStatusBar(1)
        self.statusbar.SetStatusText('Loading model')
        #### Set the size of the window because the status bar steals space
        #### in the height direction.
        self.SetSize(wx.DefaultCoord, 160)
        #### Subscribe the class to the message 'Loading'. This means that every
        #### time the meassage 'Loading' is broadcast the method 
        #### ShowLoadProgress will be executed.
        pub.subscribe(self.ShowLoadProgress, 'Loading')
        #### Start the thread that will load the model
        _thread.start_new_thread(self.LoadModel, ('test',))

    def LoadModel(self, test):
        """
        Load the Model
        """
        #### Change depending on how exactly are you going to create/load the 
        #### model
        self.model = Model()

    def ShowLoadProgress(self, x):
        """
        Show the loading progress 
        """
        if x < 15:
            self.statusbar.SetStatusText('Loading progress: ' + str(x))
        else:
            self.statusbar.SetStatusText('All loaded')

class Controller:
    def __init__(self):
        self.view = View(None, title='Test')
        self.view.Show()
        #### The line below is not needed now because the model is 
        #### loaded now from the thread started in View.InitUI
        #self.model = Model()

def main():
    app = wx.App()
    controller = Controller()
    app.MainLoop()

if __name__ == '__main__':
    main()

如果您从内部方法加载模型class View那么你将不需要 PyPubSub 模块,因为你可以调用wx.CallAfter(self.ShowLoadProgress, x)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用MVC设计模式时如何加载重型模型 的相关文章

随机推荐

  • 网站图标不工作

    我尝试插入网页的图标无法正常工作 有人可以告诉我插入它所需的代码和样式吗
  • 以动态/编程方式向 SQL 添加 WHERE 子句

    如何以编程方式向 SQL 存储过程添加搜索条件 在我的应用程序 C 中 我使用存储过程 SQL Server 2008R2 ALTER PROCEDURE dbo PROC001 userID varchar 20 password var
  • Vue 过渡与 Tailwind css 在淡出时不可见

    我使用 Tailwind css 和 Vue js 创建模式 由于 Tailwind 不支持 Vue 2 我必须添加过渡 您可以在这里看到想要的效果 https tailwindui com components application u
  • VideoView的setVideoPath和setVideoURI有什么区别

    视频查看 http developer android com reference android widget VideoView html有两种不同的方式来指定要播放的视频 设置视频路径 http developer android c
  • 如何在仅引用数据的表中循环

    我正在使用功能模块RSAQ QUERY CALL 取回一张桌子 DATA gr data TYPE REF TO data CALL FUNCTION RSAQ QUERY CALL EXPORTING query ZXXXXXXXX us
  • 通过 docker 实现 RSelenium

    我的操作系统是windows 8 1 R的版本是3 3 3 我已经安装了 RSelenium 软件包 并尝试使用以下命令运行它 library RSelenium start RSelenium server startServer che
  • 如何在步骤 1 中单击“下一步”时让 JQuery-Steps 调用 ajax 服务

    我正在使用 jquery 步骤 尽管我需要在单击第一个 下一步 时通过 ajax 调用 c 服务 但这是否可以在显示步骤 2 之前调用并返回 尽管 ajax 事件在加载步骤 2 后返回 但以下代码仍然有效 非常感谢 感谢任何帮助 Jquer
  • Java 形式类型参数定义(泛型)

    我想定义一个泛型类型 其实际类型参数只能是 数字基元包装类之一 Long Integer Float Double String 我可以用这样的定义满足第一个要求 public final class MyClass
  • 以编程方式重新启动设备

    在我的 Android 应用程序中 我想在单击按钮时重新启动我的 Android 设备 但它不起作用 到目前为止我已经做到了 ImageButton restartmob ImageButton this findViewById R id
  • 传单图块在移动设备上加载不正确

    我遇到了传单地图在移动设备上加载不正确的问题 该地图的加载纬度 经度为 25 748503 80 286949 迈阿密市中心 但在移动设备上加载的纬度 经度约为 25 584223 80 028805 大西洋沿岸 将地图拖回到正确的位置似乎
  • 我可以将 terraform 输出设置为环境变量吗?

    因此 terraform 生成了一堆我感兴趣的输出 如何将这些值通过管道传递给环境变量或其他东西 以便我的脚本能够在有人运行后获取它们terraform apply Terraform 无法直接修改调用它的 shell 的环境 一般来说 程
  • 在谷歌地图中为每个国家提供不同的颜色

    有谁知道如何在谷歌地图中为每个国家提供不同的颜色 e g 在世界地图上 蓝色覆盖英国 然后红色中国 等 我想知道google是否提供API来为每个国家提供颜色 Thanks 使用谷歌地图这确实不容易 正如 oezi 所说 你需要为你想要着色
  • 使用以位集作为键的映射时出现问题

    我正在尝试创建一个map在 C 中bitset作为钥匙 但是编译器会生成以下错误消息 In file included from usr include c 4 6 string 50 0 from usr include c 4 6 bi
  • Presto 中的用户定义函数

    我目前正在使用 Presto 0 80 我必须编写一个用户定义的函数来在选择查询期间将摄氏度转换为华氏度 我使用 Hive QL 做了同样的事情 但想知道我们是否可以在 Facebook Presto 中复制相同的内容 任何帮助将不胜感激
  • GAE组织数据结构问题

    好的 我正在与 GAE 合作 我想创建这样的东西 我有类型 组 主题 标签 每个 组 可以有尽可能多的 根据需要 主题 每个 主题 可以有任意多个 标签 如所须 每个 组 可以有任意多个 标签 如所须 它就像一个圆圈 现在我有这样的事情 c
  • QuickDraw 中的 python3 递归动画

    我有一个文本文件 其中包含行星及其相应的卫星 卫星以及它们的轨道半径和周期 我想用它来创建动画quickdraw类似于下面的 文本文件如下 RootObject Sun Object Sun Satellites Mercury Venus
  • 如何通过 Golang 的 json 解组嵌套数组中的对值

    JSON 数据如下 xxx xxx asks 0 00000315 1022 53968253 0 00000328 200 0 00000329 181 70008541 bids 0 00000254 2685 36319716 0 0
  • _PFBatchFaultingArray 对象索引:

    2012 06 15 17 53 25 532 BadgerNew 3090 707 Terminating app due to uncaught exception NSRangeException reason PFBatchFaul
  • 两个不同表中任意一个的参考键[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我有以下两个表 employees id name address designation salary phone email bu
  • 使用MVC设计模式时如何加载重型模型

    我正在使用 wxPython 和使用 Tensorflow 创建的深度学习模型构建一个应用程序 我使用的设计模式是MVC 我的问题是深度学习模型非常重 需要很长时间才能加载 大约 2 分钟 同时 GUI 会挂起 我创建了一个描述该过程的示例