def save(model, model_name): print("\nSaving model...") path = common.MODEL_DIR + model_name model.save(path) print("Model saved to " + cf.skyBlue(path))
def show_summary(): print("\n" + cf.skyBlue("Training set")) print(get_summary_matrix(common.TRAINING_DIR)) print("\n" + cf.salmon("Validation set")) print(get_summary_matrix(common.VALIDATION_DIR)) print("\n" + cf.lightGreen("Testing set")) print(get_summary_matrix(common.TESTING_DIR))
def show_settings(batch_size): print("Classification model training application started.\n") settings = pd.DataFrame(index=('max_epochs', 'patience', 'batch_size'), columns=('value', )) settings['value']['max_epochs'] = MAX_EPOCHS settings['value']['patience'] = PATIENCE settings['value']['batch_size'] = batch_size print(cf.skyBlue("Settings")) print(settings)
def labeled_value(self, key: str, msg: str, *args: Any, **kwargs: Any): """Displays a key-value pair with special formatting. Args: key (str): Label that is prepended to the message. For other arguments, see `_format_msg`. """ if self.old_style: return self._print( cf.skyBlue(key) + ": " + _format_msg(cf.bold(msg), *args, **kwargs))
def main(batch_size, model_name): show_settings(batch_size) model = make_model() training, validation = make_generators(batch_size) fit(model, training, validation, batch_size) save(model, model_name) print("\nFinished!") print("Run " + cf.skyBlue("classify.py") + " to test the model and get information about its performance.") print("More information available with " + cf.orange("Tensorboard") + ".")
def main(src_subdir, verbose, model_name): common.create_directories() src_dir = common.DATA_DIR + src_subdir + "/" model = tensorflow.keras.models.load_model(common.MODEL_DIR + model_name) # real (index) x predicted (column) confusion_matrix = pd.DataFrame(np.zeros((4, 4), dtype=np.int32), index=('down', 'left', 'right', 'up'), columns=('down', 'left', 'right', 'up')) classification_matrix = pd.DataFrame(np.zeros((4, 3)), index=('down', 'left', 'right', 'up'), columns=('precision', 'recall', 'f1')) type_matrix = pd.DataFrame(np.zeros((4, 2), dtype=np.int32), index=('round', 'wide', 'narrow', 'total'), columns=('correct', 'incorrect')) images = common.get_files(src_dir) print("Processing {} file(s) in {}/...\n".format(len(images), src_subdir)) for path, filename in images: img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) data = np.reshape(img, (1, ) + common.INPUT_SHAPE) prediction = model.predict(data) class_index = np.argmax(prediction) predicted_class = common.CLASSES[class_index] real_class, arrow_type = common.arrow_labels(filename) if verbose and real_class != predicted_class: print(path) print("Expected {} but got {}: {}\n".format( cf.lightGreen(real_class), cf.lightCoral(predicted_class), str(prediction[0]))) confusion_matrix[predicted_class][real_class] += 1 if real_class == predicted_class: type_matrix['correct'][arrow_type] += 1 type_matrix['correct']['total'] += 1 else: type_matrix['incorrect'][arrow_type] += 1 type_matrix['incorrect']['total'] += 1 print("\n" + cf.sandyBrown("Confusion matrix")) print(confusion_matrix) classification_matrix['precision'] = confusion_matrix.apply(precision) classification_matrix['recall'] = confusion_matrix.apply(recall, axis=1) classification_matrix['f1'] = classification_matrix.apply(f1, axis=1) print("\n" + cf.skyBlue("Classification summary")) print(classification_matrix) type_matrix['accuracy'] = type_matrix.apply(type_accuracy, axis=1) print("\n" + cf.plum("Accuracy by type")) print(type_matrix) print("\nFinished!")
def main(inspection, mode, automatic): print(" SPACE = approve") print("OTHER KEYS = skip") print(" Q = quit\n") labeled_imgs = common.get_files(common.LABELED_DIR) approved = 0 for path, filename in labeled_imgs: arrows = [] display = cv2.imread(path) height, width, _ = display.shape # Manually tuned values search_x, search_y = width // 5 + 35, height // 4 search_width, search_height = SEARCH_REGION_WIDTH, height // 2 - search_y for _ in range(4): x0 = search_x x1 = x0 + search_width y0 = search_y y1 = y0 + search_height img = display[y0:y1, x0:x1] (cx, cy), arrow_box = process_arrow(img, mode) search_x += int(cx + ARROW_BOX_DIST - SEARCH_REGION_WIDTH / 2) search_y += int(cy - SEARCH_REGION_HEIGHT / 2) search_width = SEARCH_REGION_WIDTH search_height = SEARCH_REGION_HEIGHT arrows.append(arrow_box) if not automatic: arrow_type, directions, _ = re.split('_', filename) reference = get_reference_arrows(directions, arrows[0].shape) cv2.imshow(arrow_type, np.vstack([np.hstack(arrows), reference])) key = cv2.waitKey() cv2.destroyAllWindows() else: key = APPROVE_KEY if key == APPROVE_KEY: if not inspection: save_arrow_imgs(arrows, filename) approved += 1 elif key == EXIT_KEY: break else: print("Skipped " + cf.skyBlue(path)) if len(labeled_imgs) > 0: print("\nApproved {} out of {} images ({}%).\n".format( approved, len(labeled_imgs), 100 * approved // len(labeled_imgs))) else: print("There are no images to preprocess.\n") show_summary() print("Finished!")