def predict_command(model, training_cnf, features_file, images_dir, weights_from, tag, sync, ): util.check_required_program_args([model, training_cnf, features_file, images_dir, weights_from]) model_def = util.load_module(model) model = model_def.model cnf = util.load_module(training_cnf).cnf weights_from = str(weights_from) image_features = np.load(features_file) images = data.get_image_files(images_dir) predictions = predict_withf(model, cnf, weights_from, image_features) predict_dir = os.path.dirname(features_file) prediction_results_dir = os.path.abspath(os.path.join(predict_dir, 'predictions', tag)) if not os.path.exists(prediction_results_dir): os.makedirs(prediction_results_dir) names = data.get_names(images) image_prediction_probs = np.column_stack([names, predictions]) headers = ['score%d' % (i + 1) for i in range(predictions.shape[1])] title = np.array(['image'] + headers) image_prediction_probs = np.vstack([title, image_prediction_probs]) prediction_probs_file = os.path.join(prediction_results_dir, 'predictions.csv') np.savetxt(prediction_probs_file, image_prediction_probs, delimiter=",", fmt="%s") print('Predictions saved to: %s' % prediction_probs_file) if cnf['classification']: class_predictions = np.argmax(predictions, axis=1) image_class_predictions = np.column_stack([names, class_predictions]) title = np.array(['image', 'label']) image_class_predictions = np.vstack([title, image_class_predictions]) prediction_class_file = os.path.join(prediction_results_dir, 'predictions_class.csv') np.savetxt(prediction_class_file, image_class_predictions, delimiter=",", fmt="%s") print('Class predictions saved to: %s' % prediction_class_file)
def predict(model, training_cnf, predict_dir, weights_from, predict_type): model_def = util.load_module(model) model = model_def.model cnf = util.load_module(training_cnf).cnf weights_from = str(weights_from) images = data.get_image_files(predict_dir) preprocessor = None prediction_iterator = create_prediction_iter(cnf, model_def.crop_size, preprocessor, False) if predict_type == 'quasi': predictor = QuasiCropPredictor(model, cnf, weights_from, prediction_iterator, 20) elif predict_type == '1_crop': predictor = OneCropPredictor(model, cnf, weights_from, prediction_iterator) elif predict_type == '10_crop': predictor = TenCropPredictor(model, cnf, weights_from, prediction_iterator, model_def.crop_size[0], model_def.image_size[0]) else: raise ValueError('Unknown predict_type: %s' % predict_type) predictions = predictor.predict(images) predictions = predictions.reshape(-1, 1000) names = data.get_names(images) for i, name in enumerate(names): print("---Predictions for %s:" % name) preds = (np.argsort(predictions[i])[::-1])[0:5] for p in preds: print(class_names[p], predictions[i][p])
def visualize(images_dir): files = data.get_image_files(images_dir) original_images = data.load_images(files) num = len(original_images) tfs = list() tfs.append(data.build_augmentation_transform((1, 1), 0, 0, (0, 200), False)) tfs.append(data.build_augmentation_transform((1, 1), 45, 0, (0, 0), False)) tfs.append(data.build_augmentation_transform((1, 1), 45, 0, (0, 200), False)) tfs.append(data.build_augmentation_transform((1, 1), 0, 45, (0, 0), False)) num_cols = len(tfs) + 1 for j, file in enumerate(files): plt.subplot(num, num_cols, j * num_cols + 1) imshow(plt, original_images[j]) for i, tf in enumerate(tfs, start=2): plt.subplot(num, num_cols, j * num_cols + i) img = data.load_augment(file, data.image_no_preprocessing, 512, 512, False, transform=tf) imshow(plt, img) plt.show()
def predict(model, training_cnf, predict_dir, weights_from, dataset_name, convert, image_size, sync, test_type): model_def = util.load_module(model) model = model_def.model cnf = util.load_module(training_cnf).cnf weights_from = str(weights_from) images = data.get_image_files(predict_dir) standardizer = cnf.get('standardizer', None) preprocessor = convert_preprocessor(image_size) if convert else None prediction_iterator = create_prediction_iter(cnf, standardizer, model_def.crop_size, preprocessor, sync) if test_type == 'quasi': predictor = QuasiCropPredictor(model, cnf, weights_from, prediction_iterator, 20) predictions = predictor.predict(images) if not os.path.exists(os.path.join(predict_dir, '..', 'results')): os.mkdir(os.path.join(predict_dir, '..', 'results')) if not os.path.exists( os.path.join(predict_dir, '..', 'results', dataset_name)): os.mkdir(os.path.join(predict_dir, '..', 'results', dataset_name)) names = data.get_names(images) image_prediction_prob = np.column_stack([names, predictions]) headers = ['score%d' % (i + 1) for i in range(predictions.shape[1])] title = np.array(['image'] + headers) image_prediction_prob = np.vstack([title, image_prediction_prob]) labels_file_prob = os.path.abspath( os.path.join(predict_dir, '..', 'results', dataset_name, 'predictions.csv')) np.savetxt(labels_file_prob, image_prediction_prob, delimiter=",", fmt="%s")
maxr - minr, fill=False, edgecolor=colors[i % len(colors)], linewidth=1) plt.gca().add_patch(rect) return minr, minc, maxr, maxc def imshow(plt, image): show_img = image.astype(np.uint8) plt.imshow(show_img, cmap='gray') images_dir = '../experiments/images/' files = data.get_image_files(images_dir) original_images = data.load_images(files) num = len(original_images) num_cols = 4 for j, file in enumerate(files): plt.subplot(num, num_cols, j * num_cols + 1) imshow(plt, original_images[j]) plt.subplot(num, num_cols, j * num_cols + 2) img = convert(file, 512) imshow(plt, img) plt.subplot(num, num_cols, j * num_cols + 3) img = convert_new(file, 512) imshow(plt, img)
def predict(model, training_cnf, predict_dir, weights_from, dataset_name, convert, image_size, sync, predict_type): images = data.get_image_files(predict_dir) # Form now, hard coded models, cnfs, and weights # Need to take these from program inputs or an ensembling config file print('Creating predictor 1') weights_from1 = 'weights.sa/model-epoch-97.ckpt' model1 = 'examples/mnist_model_sa.py' training_cnf1 = 'examples/mnist_cnf.py' model_def1 = util.load_module(model1) model1 = model_def1.model cnf1 = util.load_module(training_cnf1).cnf standardizer = cnf1.get('standardizer', NoOpStandardizer()) preprocessor = convert_preprocessor( model_def1.image_size[0]) if convert else None prediction_iterator1 = create_prediction_iter(cnf1, standardizer, model_def1.crop_size, preprocessor, sync) predictor1 = QuasiCropPredictor(model1, cnf1, weights_from1, prediction_iterator1, 20) # predictor1 = OneCropPredictor(model1, cnf1, weights_from1, prediction_iterator1) print('Creating predictor 2') weights_from2 = 'weights.rv/model-epoch-31.ckpt' model2 = 'examples/mnist_model.py' training_cnf2 = 'examples/mnist_cnf.py' model_def2 = util.load_module(model2) model2 = model_def2.model cnf2 = util.load_module(training_cnf2).cnf standardizer = cnf2.get('standardizer', NoOpStandardizer()) preprocessor = convert_preprocessor( model_def2.image_size[0]) if convert else None prediction_iterator2 = create_prediction_iter(cnf2, standardizer, model_def2.crop_size, preprocessor, sync) predictor2 = QuasiCropPredictor(model2, cnf2, weights_from2, prediction_iterator2, 20) # predictor2 = OneCropPredictor(model2, cnf2, weights_from2, prediction_iterator2) predictor = EnsemblePredictor([predictor1, predictor2]) predictions = predictor.predict(images) if not os.path.exists(os.path.join(predict_dir, '..', 'results')): os.mkdir(os.path.join(predict_dir, '..', 'results')) if not os.path.exists( os.path.join(predict_dir, '..', 'results', dataset_name)): os.mkdir(os.path.join(predict_dir, '..', 'results', dataset_name)) names = data.get_names(images) image_prediction_probs = np.column_stack([names, predictions]) headers = ['score%d' % (i + 1) for i in range(predictions.shape[1])] title = np.array(['image'] + headers) image_prediction_probs = np.vstack([title, image_prediction_probs]) prediction_probs_file = os.path.abspath( os.path.join(predict_dir, '..', 'results', dataset_name, 'predictions.csv')) np.savetxt(prediction_probs_file, image_prediction_probs, delimiter=",", fmt="%s") print('Predictions saved to: %s' % prediction_probs_file) if cnf1['classification']: class_predictions = np.argmax(predictions, axis=1) image_class_predictions = np.column_stack([names, class_predictions]) title = np.array(['image', 'label']) image_class_predictions = np.vstack([title, image_class_predictions]) prediction_class_file = os.path.abspath( os.path.join(predict_dir, '..', 'results', dataset_name, 'predictions_class.csv')) np.savetxt(prediction_class_file, image_class_predictions, delimiter=",", fmt="%s") print('Class predictions saved to: %s' % prediction_class_file)
def predict(model, output_layer, training_cnf, predict_dir, weights_from, tag, convert, image_size, sync, predict_type): util.check_required_program_args( [model, training_cnf, predict_dir, weights_from]) model_def = util.load_module(model) model = model_def.model cnf = util.load_module(training_cnf).cnf weights_from = str(weights_from) images = data.get_image_files(predict_dir) preprocessor = convert_preprocessor(image_size) if convert else None prediction_iterator = create_prediction_iter(cnf, model_def.crop_size, preprocessor, sync) if predict_type == 'quasi': predictor = QuasiCropPredictor(model, cnf, weights_from, prediction_iterator, 20, output_layer) elif predict_type == '1_crop': predictor = OneCropPredictor(model, cnf, weights_from, prediction_iterator, output_layer) elif predict_type == '10_crop': predictor = TenCropPredictor(model, cnf, weights_from, prediction_iterator, model_def.crop_size[0], model_def.image_size[0], output_layer) else: raise ValueError('Unknown predict_type: %s' % predict_type) predictions = predictor.predict(images) prediction_results_dir = os.path.abspath( os.path.join(predict_dir, '..', 'predictions', tag)) if not os.path.exists(prediction_results_dir): os.makedirs(prediction_results_dir) if output_layer == 'predictions': names = data.get_names(images) image_prediction_probs = np.column_stack([names, predictions]) headers = ['score%d' % (i + 1) for i in range(predictions.shape[1])] title = np.array(['image'] + headers) image_prediction_probs = np.vstack([title, image_prediction_probs]) prediction_probs_file = os.path.join(prediction_results_dir, 'predictions.csv') np.savetxt(prediction_probs_file, image_prediction_probs, delimiter=",", fmt="%s") print('Predictions saved to: %s' % prediction_probs_file) if cnf['classification']: class_predictions = np.argmax(predictions, axis=1) image_class_predictions = np.column_stack( [names, class_predictions]) title = np.array(['image', 'label']) image_class_predictions = np.vstack( [title, image_class_predictions]) prediction_class_file = os.path.join(prediction_results_dir, 'predictions_class.csv') np.savetxt(prediction_class_file, image_class_predictions, delimiter=",", fmt="%s") print('Class predictions saved to: %s' % prediction_class_file) else: # feature extraction features_file = os.path.join(prediction_results_dir, 'features.npy') np.save(features_file, predictions) print('Features from layer: %s saved to: %s' % (output_layer, features_file))