def __init__(self, parent, path): # take data and use datamanager to return imageSize, and ints. # Attributes self.path = path self.imageNum = 0 # current image in the data to be viewed self.imgSize = DataManager.getImgSize(path, split=True) self.imgSizeX = self.imgSize[1] self.imgSizeY = self.imgSize[0] self.imgSize = DataManager.getImgSize(path, split=False) self.setSize = DataManager.getSetSize(path) # Number of images in set self.currentPixel = None # Tk.Frame obj self.currentPixelPos = None # int position of pixel in current image # Pixel holder self.wrapper = Tk.Frame(parent) self.wrapper.grid(row=0, column=0) self.digitDisplay = Tk.Frame(self.wrapper, width=235, height=235) self.digitDisplay.grid_propagate(False) for i in range(self.imgSizeY): self.digitDisplay.grid_rowconfigure(i, weight=1) for i in range(self.imgSizeX): self.digitDisplay.grid_columnconfigure(i, weight=1) self.digitDisplay.grid(row=0, column=0) # EntryBox self.pixelLabel = Tk.Label(self.wrapper, text='No pixel selected') self.pixelLabel.grid(row=1, column=0, sticky='nswe') # self.pixel is a 2d array containing Tk.Frames. [0] = imageNum, [1] = frames pixel = [] for i in range(self.setSize): pixel.append([]) self.data = DataManager.getImage(self.path, i) for j in range(self.imgSizeX * self.imgSizeY): val = int.from_bytes(self.data[j:j + 1], byteorder='big') pixel[i].append(self.newPixel(val, j)) self.pixel = pixel # Free up some ram self.data = None return
def newImg(self, path): # Destroy old pixels for i in range(self.setSize): self.data = DataManager.getImage(self.path, i) for j in range(self.imgSizeX * self.imgSizeY): self.pixel[i][j].destroy() # New attributes self.path = path self.imageNum = 0 # current image in the data to be viewed self.imgSize = DataManager.getImgSize(path, split=True) self.imgSizeX = self.imgSize[1] self.imgSizeY = self.imgSize[0] self.imgSize = DataManager.getImgSize(path, split=False) self.setSize = DataManager.getSetSize(path) # Number of images in set self.currentPixel = None # Tk.Frame obj self.currentPixelPos = None # int position of pixel in current image # self.pixel is a 2d array containing Tk.Frames. [0] = imageNum, [1] = frames pixel = [] for i in range(self.setSize): pixel.append([]) self.data = DataManager.getImage(self.path, i) for j in range(self.imgSizeX * self.imgSizeY): val = int.from_bytes(self.data[j:j + 1], byteorder='big') pixel[i].append(self.newPixel(val, j)) self.pixel = pixel # Free up some ram self.data = None return
def __init__(self, dataPath, labelPath): self.stop = False dataPath = 'Data/Train Set Images/' + dataPath + '.gz' labelPath = 'Data/Train Set Labels/' + labelPath + '.gz' self.window = Tk.Toplevel() self.window.title('Training') self.window.config(bg=colourScheme['background']) # window.overrideredirect(1) self.window.wm_geometry('400x120') self.window.resizable(False, False) self.window.grid_rowconfigure(0, weight=1) self.window.grid_columnconfigure(1, weight=1) self.window.grab_set() sets = DataManager.getSetSize(dataPath) self.progressLabel = Tk.Label(self.window, bg=colourScheme['background'], text='Progress:') self.progressLabel.grid(row=0, column=0, sticky='sw', padx=20, pady=(10, 0)) self.progressBar = ttk.Progressbar(self.window, orient='horizontal', maximum=sets, mode='determinate') self.progressBar.grid( row=1, column=0, columnspan=2, sticky='we', padx=10, pady=10, ) # Buttons btnFrame = Tk.Frame(self.window, bg=colourScheme['background']) btnFrame.grid(row=2, column=1, sticky='e', padx=10, pady=(0, 10)) self.closeBtn = Tk.Button(btnFrame, text='Close', state='disabled', command=lambda : \ self.closeFunc()) self.stopBtn = Tk.Button(btnFrame, text='Stop', command=lambda: self.stopFunc()) self.stopBtn.grid(row=0, column=0, sticky='w') self.closeBtn.grid(row=0, column=1, padx=(10, 0), sticky='e') # Call Network.train() if Globals.NN.structure == []: self.progressLabel['text'] = 'Progress: Network Missing' self.trainingProgress['value'] = sets self.stopFunc() return # Run Training Globals.NN.train(dataPath, labelPath, self) self.stopFunc() return
def __init__(self, dataPath, labelPath): self.stop = False dataPath = 'Data/Test Set Images/' + dataPath + '.gz' labelPath = 'Data/Test Set Labels/' + labelPath + '.gz' self.window = Tk.Toplevel() self.window.title('Testing') self.window.config(bg=colourScheme['background']) # window.overrideredirect(1) self.window.wm_geometry('400x120') self.window.resizable(False, False) self.window.grid_rowconfigure(0, weight=1) self.window.grid_columnconfigure(1, weight=1) self.window.grab_set() sets = DataManager.getSetSize(dataPath) # Setup TreeView/Table self.table = ttk.Treeview(self.window) self.table['columns'] = ('Instances', 'Correct', 'Accuracy') self.table.column('#0', minwidth=30, stretch=Tk.NO) self.table.column('Instances', minwidth=60, stretch=Tk.NO) self.table.column('Correct', minwidth=120, stretch=Tk.NO) self.table.column('Accuracy', minwidth=60, stretch=Tk.NO) self.table.heading('#0', text='Digit', anchor=Tk.W) self.table.heading('Instances', text='Instances', anchor=Tk.W) self.table.heading('Correct', text='Correct Categorisaion', anchor=Tk.W) self.table.heading('Accuracy', text='Accuracy', anchor=Tk.W) # Setup Progressbar self.progressLabel = Tk.Label(self.window, bg=colourScheme['background'], text='Progress:') self.progressLabel.grid(row=0, column=0, sticky='sw', padx=20, pady=(10, 0)) self.progressBar = ttk.Progressbar(self.window, orient='horizontal', maximum=sets, mode='determinate') self.progressBar.grid( row=1, column=0, columnspan=2, sticky='we', padx=10, pady=10, ) # Buttons btnFrame = Tk.Frame(self.window, bg=colourScheme['background']) btnFrame.grid(row=2, column=1, sticky='e', padx=10, pady=(0, 10)) self.closeBtn = Tk.Button(btnFrame, text='Close', state='disabled', command=lambda : \ self.closeFunc()) self.stopBtn = Tk.Button(btnFrame, text='Stop', command=lambda : self.stopFunc()) self.stopBtn.grid(row=0, column=0, sticky='w') self.closeBtn.grid(row=0, column=1, padx=(10, 0), sticky='e') # Check if there is a network to test if Globals.NN.structure == []: self.progressLabel['text'] = 'Progress: Network Missing' self.progressBar['value'] = sets self.stopFunc() return # Run Testing and display grid Globals.NN.test(dataPath, labelPath, self) self.stopFunc() self.progressLabel.destroy() self.progressBar.destroy() self.table.grid( row=0, column=0, columnspan=2, sticky='nswe', padx=10, pady=10, ) self.window.wm_geometry('700x350') return
def train( self, data, expecteds, window, normalise=False, ): # Get image size, and num of images from data imgCount = DataManager.getSetSize(data) imgSize = DataManager.getImgSize(data) # Get label file ready expecteds = gzip.open(expecteds, 'r') expecteds.read(8) # get past headers # Get images and train data = gzip.open(data, 'r') for i in range(0, imgCount): # print('Training on Image: ', i+1) # Build image array buffer = data.read(imgSize) image = np.frombuffer(buffer, dtype=np.uint8) # One hot encode label label = int.from_bytes(expecteds.read(1), byteorder='big') expectedOutput = np.zeros(10) expectedOutput[label] = 1 # Update progress bar window.progressBar['value'] += 1 window.window.update() # Do passes self.forwardProp(image, normalise=normalise) self.backwardProp(expectedOutput) # Check if training should be stopped if window.stop == True: break # Setup window things window.progressLabel['text'] = 'Progress: Complete!' # Free resources data.close() expecteds.close() return