def view_local(): global installed_dict global repo_dict init_dict() list_text = [] print() if len(installed_dict) != 0: list_text.append(colorprint("{:20}{:50}{:12}".format("Plugin", "Description", "Enabled"), "bold")) else: print(colorprint("You have no installed plugins!\n", "red")) return for key in installed_dict: plugin = installed_dict[key] enabled_text = "YES" if plugin['enabled'] == True else "NO" description_arr = textwrap.wrap(repo_dict[key]['description'], 45) list_text.append("{:20}{:50}{:12}".format(key, description_arr[0], enabled_text)) for i in range(1, len(description_arr)): list_text.append("{:20}{:50}{:12}".format("", description_arr[i], "")) list_text.append("\n") print("\n".join(list_text))
def add(theme): global repo_dict global installed_dict init_dict() if theme in installed_dict['installed']: print(colorprint(f"Theme '{theme}' has already been added.", "red")) return False if theme not in repo_dict: print( colorprint(f"Theme '{theme}' is not available for installation.", "red")) return False if os.system(f"zsh {globals.repo_dir}/themes/{theme}/add.zsh") != 0: print(colorprint(f"Failed to add theme '{theme}'.", "red")) return False print(colorprint(f"Successfully added theme '{theme}'!", "green")) installed_dict['installed'][theme] = {"entry": repo_dict[theme]['entry']} dump_dict() generate() return True
def view_remote(): global installed_dict global repo_dict init_dict() list_text = [] print() list_text.append( colorprint("{:20}{:50}".format("Theme", "Description"), "bold")) for key in repo_dict: theme = repo_dict[key] if key in installed_dict['installed']: continue description_arr = textwrap.wrap(theme['description'], 45) list_text.append("{:20}{:50}".format(key, description_arr[0])) for i in range(1, len(description_arr)): list_text.append("{:20}{:50}".format("", description_arr[i])) list_text.append("\n") if len(list_text) == 1: print( colorprint("You have already installed all available themes.\n", "red")) return print("\n".join(list_text))
def build_patch_mlp(PATCH_SIZE, phase='TRAIN'): colorprint(Color.BLUE, 'Building MLP model...\n') model = Sequential() model.add( Reshape((PATCH_SIZE * PATCH_SIZE * 3, ), input_shape=(PATCH_SIZE, PATCH_SIZE, 3))) model.add(Dense(units=2048, activation='relu')) # model.add(Dense(units=1024, activation='relu')) if phase.capitalize() == 'TEST': model.add( Dense(units=8, activation='linear') ) # In test phase we softmax the average output over the image patches else: model.add(Dense(units=8, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) model.summary() if not os.path.exists('dump/patch_models'): os.mkdir('dump/patch_models') plot_model(model, to_file='dump/patch_models/' + str(hash(str(model.get_config()))) + '.png', show_shapes=True, show_layer_names=True) colorprint(Color.BLUE, 'Done!\n') return model
def add(plugin): global repo_dict global installed_dict init_dict() if plugin in installed_dict: print(colorprint(f"Plugin '{plugin}' is already installed!", "red")) return if plugin not in repo_dict: print(colorprint(f"Plugin '{plugin}' was not found!", "red")) return if os.system(f"zsh {globals.repo_dir}/plugins/{plugin}/add.zsh") != 0: print(colorprint(f"Installation of plugin '{plugin}' was unsuccessful.", "red")) installed_dict[plugin] = { "enabled": True, "entry": repo_dict[plugin]['entry'] } dump_dict() generate() print(colorprint(f"Successfully added plugin '{plugin}'", "green"))
def build_mlp(IMG_SIZE): colorprint(Color.BLUE, 'Building MLP model...\n') # Build the Multi Layer Perceptron model model = Sequential() model.add( Reshape((IMG_SIZE * IMG_SIZE * 3, ), input_shape=(IMG_SIZE, IMG_SIZE, 3), name='first')) model.add(Dense(units=2048, activation='relu', name='second')) #model.add(Dense(units=1024, activation='relu', name='third')) model.add(Dense(units=8, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) model.summary() if not os.path.exists('dump/models'): os.mkdir('dump/models') plot_model(model, to_file='dump/models/' + str(hash(str(model.get_config()))) + '.png', show_shapes=True, show_layer_names=True) colorprint(Color.BLUE, 'Done!\n') return model
def cross_validate_BoVW(self, features, train_labels): """ cross_validate classifier with k stratified folds """ colorprint(Color.BLUE, 'Cross_validating the SVM classifier...\n') init = time.time() # Create BoVW classifier BoVW_classifier = BoVW(k=512) # rearenge feautures to a single array features = np.array(features) size_descriptors = features[0].shape[1] D = np.zeros((np.sum([len(p) for p in features]), size_descriptors), dtype=np.uint8) startingpoint = 0 for i in range(len(features)): D[startingpoint:startingpoint + len(features[i])] = \ features[i] startingpoint += len(features[i]) # Compute Codebook BoVW_classifier.compute_codebook(D) # get train visual word encoding visual_words = BoVW_classifier.get_train_encoding(features, Keypoints=[]) # Cross validate classifier BoVW_classifier.cross_validate(visual_words, train_labels) end = time.time() colorprint(Color.BLUE, 'Done in ' + str(end - init) + ' secs.\n')
def main_router_loop(): utils.colorprint("Enter the route to go directly go deep else press enter to start from default", foreground=Fore.LIGHTBLUE_EX) route_to = input("Enter Route: ") exit = False while not exit: route_to = route_parser(route_to)
def train_classifier_BoVW(self, features, train_labels): # Train an BoVW classifier colorprint(Color.BLUE, '[train_classifier_BoVW]: Training the classifier...\n') init = time.time() # Create BoVW classifier self.BoVW_classifier = BoVW(k=512) # rearrange features to a single array features = np.array(features) # print(features.shape) size_descriptors = features.shape[1] size_of_mini_batches = 64 # print(int(size_descriptors / size_of_mini_batches)) for i in range(int(size_descriptors / size_of_mini_batches)): size_of_batch_of_descriptors = features.shape[1] batch_of_features = features[size_of_mini_batches * i:size_of_mini_batches * (i + 1)] print(batch_of_features.shape) print('D will be a {}x{} matrix of uint8'.format( np.sum([len(p) for p in batch_of_features]), size_of_batch_of_descriptors)) # FIXME: fix loop to avoid this code # stop it when finish if np.sum([len(p) for p in batch_of_features]) == 0: break D = np.zeros((np.sum([len(p) for p in batch_of_features ]), size_of_batch_of_descriptors), dtype=np.uint8) startingpoint = 0 for i in range(len(batch_of_features)): D[startingpoint:startingpoint + len(batch_of_features[i])] = \ batch_of_features[i] startingpoint += len(batch_of_features[i]) # Compute Codebook self.BoVW_classifier.compute_codebook_partial(D) self.BoVW_classifier.compute_codebook_partial(None, only_save=True) # get train visual word encoding visual_words = self.BoVW_classifier.get_train_encoding(features, Keypoints=[]) # Train an SVM classifier print("Problematic code here, the next shapes should match") print(visual_words.shape) print(train_labels.shape) self.BoVW_classifier.train_classifier(visual_words, train_labels) end = time.time() colorprint(Color.BLUE, 'Done in ' + str(end - init) + ' secs.\n')
def route_parser(inp_route=""): utils.colorprint(f"Current Route: {inp_route} ", foreground=Fore.BLUE) route_parts = inp_route.strip('/').split('/') if len(route_parts) < 2: log.debug("Engine not in route", module="router") return get_engines_route_input() if route_parts[0] == "creds": return cred_route_parser(inp_route, route_parts)
def view(): global aliasdict init_aliasdict() if len(aliasdict) == 0: print(colorprint("No ZConf-created aliases available.", "red")) return print(colorprint("\n{:20}{:50}{:15}".format("Alias", "Value", "Enabled"), "bold")) for alias in aliasdict: print("{:20}{:50}{:15}".format(alias, aliasdict.get(alias)['value'], ("YES" if aliasdict[alias]['enabled'] else "NO"))) print()
def view(): global pathdict init_pathdict() if len(pathdict) == 0: print(colorprint("No ZConf-created segments in PATH", "red")) return print(colorprint("{:20}{:60}{:15}".format("Segment", "Value", "Enabled"), "bold")) for seg in pathdict: print("{:20}{:60}{:15}".format(seg, pathdict.get(seg)['value'], ("YES" if pathdict[seg]['enabled'] else "NO"))) print()
def train_classifier_SVM(self, features, train_labels): # Train an SVM classifier colorprint(Color.BLUE, 'Training the SVM classifier...\n') init = time.time() self.stdSlr = StandardScaler().fit(features) D_scaled = self.stdSlr.transform(features) # Train an SVM classifier with RBF kernel # self.clf = svm.SVC(kernel='rbf', C=10, gamma=.002).fit(D_scaled, # train_labels) self.clf = svm.SVC(kernel='linear').fit(D_scaled, train_labels) end = time.time() colorprint(Color.BLUE, 'Done in ' + str(end - init) + ' secs.\n')
def disable(plugin): global installed_dict init_dict() if plugin not in installed_dict: print(colorprint(f"Plugin {plugin} is not installed!", "red")) installed_dict[plugin]['enabled'] = False dump_dict() generate() print(colorprint(f"Successfully disabled plugin '{plugin}'", "green"))
def __init__(self, img_size=32, batch_size=16, dataset_dir='/home/datasets/scenes/MIT_split', model_fname='my_first_mlp.h5'): # initialyze model self.IMG_SIZE = img_size self.BATCH_SIZE = batch_size self.DATASET_DIR = dataset_dir self.MODEL_FNAME = model_fname colorprint(Color.BLUE, 'Creating object\n') if not os.path.exists(self.DATASET_DIR): colorprint( Color.RED, 'ERROR: dataset directory ' + self.DATASET_DIR + ' do not exists!\n')
def view_all(): global repo_dict global installed_dict init_dict() list_text = [] print() list_text.append(colorprint("{:20}{:50}{:12}{:12}".format("Plugin", "Description", "Installed", "Enabled"), "bold")) for key in repo_dict: plugin = repo_dict[key] installed_text = "YES" if key in installed_dict else "NO" enabled_text = "YES" if installed_text == "YES" and installed_dict[key]['enabled'] == True else "NO" description_arr = textwrap.wrap(plugin['description'], 45) list_text.append("{:20}{:50}{:12}{:12}".format(key, description_arr[0], installed_text, enabled_text)) for i in range(1, len(description_arr)): list_text.append("{:20}{:50}{:12}{:12}".format("", description_arr[i], "", "")) list_text.append("\n") print("\n".join(list_text))
def view_local(): global repo_dict global installed_dict init_dict() list_text = [] print() list_text.append(f"Selected theme: {installed_dict['enabled']}") list_text.append("\n") list_text.append( colorprint("{:20}{:50}".format("Theme", "Description"), "bold")) for key in installed_dict['installed']: if key in repo_dict: theme = repo_dict[key] else: continue description_arr = textwrap.wrap(theme['description'], 45) list_text.append("{:20}{:50}".format(key, description_arr[0])) for i in range(1, len(description_arr)): list_text.append("{:20}{:50}".format("", description_arr[i])) list_text.append("\n") print("\n".join(list_text))
def check_alias_exists(alias): global aliasdict init_aliasdict() if alias not in aliasdict: print(colorprint("Alias {} was not found.".format(alias), "red")) return False return True
def check_segment_exists(seg): global pathdict init_pathdict() if seg.upper() not in pathdict: print(colorprint("Segment {} was not found in PATH".format(seg), "red")) return False return True
def init(): utils.colorprint("Welcome to Gringotts CLI", foreground=Fore.LIGHTBLUE_EX) utils.colorprint("Login:"******"email:", foreground=Fore.LIGHTYELLOW_EX, end=" ") # email = input() # password = getpass.getpass(Fore.LIGHTYELLOW_EX + "password: "******"\n Thank you for visiting", foreground=Fore.GREEN)
def remove(plugin): global repo_dict global installed_dict init_dict() if plugin not in installed_dict: print(colorprint(f"Plugin '{plugin}' is not installed!", "red")) return if os.system(f"zsh {globals.repo_dir}/plugins/{plugin}/remove.zsh") != 0: print(colorprint(f"Removal of plugin '{plugin}' was unsuccessful.", "red")) del installed_dict[plugin] dump_dict() generate() print(colorprint(f"Successfully removed plugin '{plugin}'", "green"))
def disable(theme): global installed_dict init_dict() if theme not in installed_dict: print(colorprint(f"Theme {theme} is not installed!")) installed_dict[theme]['enabled'] = False dump_dict() generate()
def set(alias, value): global aliasdict init_aliasdict() aliasdict.setdefault(alias, {'value': "", 'enabled': True}) aliasdict[alias]['value'] = value dump_aliasdict() generate() print(colorprint(f"Successfully aliased {alias} to '{value}'!", "green"))
def evaluate_performance_BoVW(self, features, test_labels, do_plotting): # Test the classification accuracy colorprint( Color.BLUE, '[evaluate_performance_BOVW]: Testing the SVM classifier...\n') init = time.time() # get train visual word encoding features = np.array(features) visual_words_test = self.BoVW_classifier.get_train_encoding( features, Keypoints=[]) # Test the classification accuracy self.BoVW_classifier.evaluate_performance(visual_words_test, test_labels, do_plotting, train_data=[]) end = time.time() colorprint(Color.BLUE, 'Done in ' + str(end - init) + ' secs.\n')
def set_segment(seg, value): global pathdict init_pathdict() seg = seg.upper() pathdict.setdefault(seg, {'value': "", 'enabled': True}) pathdict[seg]['value'] = value dump_pathdict() generate() print(colorprint(f"Successfully set segment {seg} to '{value}'", "green"))
def remove(theme): global repo_dict global installed_dict init_dict() if theme not in installed_dict['installed']: print(colorprint(f"Theme '{theme}' is not installed!", "red")) return if os.system(f"zsh {globals.repo_dir}/themes/{theme}/remove.zsh") != 0: print( colorprint(f"Removal of theme '{theme}' was unsuccessful.", "red")) del installed_dict['installed'][theme] installed_dict['enabled'] = "notatheme" dump_dict() generate() print(colorprint(f"Successfully removed theme '{theme}'", "green"))
def disable(alias): global aliasdict init_aliasdict() if not check_alias_exists(alias): return aliasdict[alias]['enabled'] = False dump_aliasdict() generate() print(colorprint(f"Alias {alias} is now disabled."))
def get_layer_output(self, layer=LAYERS.LAST, image_set='test'): # get layer output init = time.time() colorprint(Color.BLUE, 'Getting layer output...\n') model_layer = Model(inputs=self.model.input, outputs=self.model.get_layer(layer).output) # this is the dataset configuration we will use for testing: # only rescaling datagen = ImageDataGenerator(rescale=1. / 255) # this is a generator that will read pictures found in # subfolers of 'data/test', and indefinitely generate # batches of augmented image data generator = datagen.flow_from_directory( self.DATASET_DIR + '/' + image_set, target_size=(self.IMG_SIZE, self.IMG_SIZE), batch_size=self.BATCH_SIZE, classes=[ 'coast', 'forest', 'highway', 'inside_city', 'mountain', 'Opencountry', 'street', 'tallbuilding' ], class_mode='categorical', shuffle=False) labels = generator.classes # get the features from images features = model_layer.predict_generator(generator, steps=2) colorprint(Color.BLUE, 'Done!\n') end = time.time() colorprint(Color.BLUE, 'Done in ' + str(end - init) + ' secs.\n') return features, labels
def build_MLP_model(self): # Build MLP model init = time.time() colorprint(Color.BLUE, 'Building MLP model...\n') # Build the Multi Layer Perceptron model self.model = Sequential() self.model.add( Reshape((self.IMG_SIZE * self.IMG_SIZE * 3, ), input_shape=(self.IMG_SIZE, self.IMG_SIZE, 3), name=self.LAYERS.FIRST)) self.model.add( Dense(units=2048, activation='relu', name=self.LAYERS.SECOND)) self.model.add( Dense(units=1024, activation='relu', name=self.LAYERS.THIRD)) self.model.add( Dense(units=1024, activation='relu', name=self.LAYERS.LAST)) self.model.add( Dense(units=8, activation='softmax', name=self.LAYERS.LABELS)) self.model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) print(self.model.summary()) plot_model(self.model, to_file='modelMLP.png', show_shapes=True, show_layer_names=True) colorprint(Color.BLUE, 'Done!\n') end = time.time() colorprint(Color.BLUE, 'Done in ' + str(end - init) + ' secs.\n')
def remove(alias): global aliasdict init_aliasdict() if not check_alias_exists(alias): return del aliasdict[alias] dump_aliasdict() generate() print(colorprint(f"Removed alias {alias}", "green"))