示例#1
0
    def load_file(self, name="mnist"):
        # print len(self.layers)
        for i in range(len(self.layers)):
            if isinstance(self.layers[i], ParamMixin):
                if False:  ## convert old pickle files to new format...
                    path1 = str(self.nn_dir + name + "-weights" + str(i + 1) + ".save")
                    if os.path.exists(path1):
                        f1 = file(path1, "rb")
                        loaded_obj1 = cPickle.load(f1)
                        f1.close()
                        self.layers[i].W = loaded_obj1
                        print ("load " + path1)
                    path2 = str(self.nn_dir + name + "-bias" + str(i + 1) + ".save")
                    if os.path.exists(path2):
                        f2 = file(path2, "rb")
                        loaded_obj2 = cPickle.load(f2)
                        f2.close()
                        self.layers[i].b = loaded_obj2
                        print ("load " + path2)

                if not self.android_load:
                    ## load text files...
                    textw1 = str(self.nn_dir + name + "_w" + str(i + 1) + ".txt")
                    shapew1 = str(self.nn_dir + name + "_shape_w" + str(i + 1) + ".txt")
                    if os.path.exists(textw1) and os.path.exists(shapew1):
                        wshape = np.loadtxt(shapew1)
                        wtext = np.loadtxt(textw1)
                        self.layers[i].W = store.unstore_w(wtext, wshape)
                        print "w" + str(i + 1)
                    textb1 = str(self.nn_dir + name + "_b" + str(i + 1) + ".txt")
                    shapeb1 = str(self.nn_dir + name + "_shape_b" + str(i + 1) + ".txt")
                    if os.path.exists(textb1) and os.path.exists(shapeb1):
                        bshape = np.loadtxt(shapeb1)
                        btext = np.loadtxt(textb1)
                        self.layers[i].b = store.unstore_b(btext, bshape)
                        print "b" + str(i + 1)
                elif self.android_load:
                    pass
示例#2
0
    def load_file(self, name="mnist"):
        print(name)
        for i in range(len(self.layers)):
            if isinstance(self.layers[i], ParamMixin):

                if not self.android_load:
                    ## load text files...
                    print(i + 1)

                    try:
                        textw1 = str(self.nn_dir + name + '_w' + str(i + 1) +
                                     '.txt')
                        shapew1 = str(self.nn_dir + name + '_shape_w' +
                                      str(i + 1) + '.txt')
                        if os.path.exists(textw1) and os.path.exists(shapew1):
                            wshape = np.loadtxt(shapew1)
                            wtext = np.loadtxt(textw1)
                            self.layers[i].W = store.unstore_w(wtext, wshape)
                            print 'w' + str(i + 1)
                        textb1 = str(self.nn_dir + name + '_b' + str(i + 1) +
                                     '.txt')
                        shapeb1 = str(self.nn_dir + name + '_shape_b' +
                                      str(i + 1) + '.txt')
                        if os.path.exists(textb1) and os.path.exists(shapeb1):
                            bshape = np.loadtxt(shapeb1)
                            btext = np.loadtxt(textb1)
                            self.layers[i].b = store.unstore_b(btext, bshape)
                            print 'b' + str(i + 1)
                    except:
                        pass

                elif self.android_load:

                    if True:

                        try:

                            GetText = jnius.autoclass(
                                "org.renpy.android.GetText")
                            PythonActivity = jnius.autoclass(
                                'org.renpy.android.PythonActivity')

                            currentActivity = jnius.cast(
                                'android.app.Activity',
                                PythonActivity.mActivity)

                            loader = GetText()
                            textw1 = str(name + '_w' + str(i + 1))
                            shapew1 = str(name + '_shape_w' + str(i + 1))
                            textb1 = str(name + '_b' + str(i + 1))
                            shapeb1 = str(name + '_shape_b' + str(i + 1))
                            activity = currentActivity
                            #activity = PythonActivity.mActivity

                            wshape = loader.getText(activity, shapew1)
                            wtext = loader.getText(activity, textw1)
                            bshape = loader.getText(activity, shapeb1)
                            btext = loader.getText(activity, textb1)

                            Win = [float(x) for x in wtext.split(' ')]
                            bin = [float(x) for x in btext.split(' ')]

                            Wshapein = [
                                float(x.strip()) for x in wshape.splitlines()
                            ]
                            bshapein = [
                                float(x.strip()) for x in bshape.splitlines()
                            ]

                            self.layers[i].W = store.unstore_w(Win, Wshapein)
                            self.layers[i].b = store.unstore_b(bin, bshapein)

                        except:
                            #exit()
                            print("not loading android weights")

                    pass
    def load_file(self, name = "mnist"):
        print(name)
        for i in range(len(self.layers)):
            if isinstance(self.layers[i], ParamMixin):
                
                
                if not self.android_load :
                    ## load text files...
                    print( i + 1)
                    
                    try: 
                        textw1 = str(self.nn_dir+name+'_w'+str(i+1)+'.txt')
                        shapew1 = str(self.nn_dir+name+'_shape_w'+str(i+1)+'.txt')
                        if os.path.exists(textw1) and os.path.exists(shapew1):
                            wshape = np.loadtxt(shapew1)
                            wtext = np.loadtxt(textw1)
                            self.layers[i].W = store.unstore_w(wtext, wshape)
                            print 'w' + str(i+1)
                        textb1 = str(self.nn_dir+name+'_b'+str(i+1)+'.txt')
                        shapeb1 = str(self.nn_dir+name+'_shape_b'+str(i+1)+'.txt')
                        if os.path.exists(textb1) and os.path.exists(shapeb1) :
                            bshape = np.loadtxt(shapeb1)
                            btext = np.loadtxt(textb1)
                            self.layers[i].b = store.unstore_b(btext, bshape)
                            print 'b' + str(i+1)
                    except:
                        pass
                    

                elif self.android_load :
                    
                    
                    if True:
                    
                        try:
                            
                            GetText = jnius.autoclass("org.renpy.android.GetText")
                            PythonActivity = jnius.autoclass('org.renpy.android.PythonActivity')

                            currentActivity = jnius.cast('android.app.Activity', PythonActivity.mActivity)
                            
                            
                            loader = GetText()
                            textw1 = str(name+'_w'+str(i+1))
                            shapew1 = str(name+'_shape_w'+str(i+1))
                            textb1 = str(name+'_b'+str(i+1))
                            shapeb1 = str(name+'_shape_b'+str(i+1))
                            activity = currentActivity
                            #activity = PythonActivity.mActivity

                            wshape = loader.getText(activity, shapew1)
                            wtext = loader.getText(activity, textw1)
                            bshape = loader.getText(activity, shapeb1)
                            btext = loader.getText(activity, textb1)


                            Win = [float(x) for x in wtext.split(' ')]
                            bin = [float(x) for x in btext.split(' ')]

                            Wshapein = [float(x.strip()) for x in wshape.splitlines()]
                            bshapein = [float(x.strip()) for x in bshape.splitlines()]

                            self.layers[i].W = store.unstore_w(Win,Wshapein)
                            self.layers[i].b = store.unstore_b(bin,bshapein)
                            
                            
                        except:
                            #exit()
                            print("not loading android weights")
                        
                    pass