def test_load_data_single_column(): """Test that the featurizer saves attributes correctly after loading data""" f = ImageFeaturizer() f.load_data(**LOAD_DATA_ARGS) compare_featurizer_class(f, (227, 227), np.zeros((1)), **COMPARE_ARGS) # Remove path to the generated csv at end of test remove_generated_paths()
def test_load_data_single_column(): """Test that the featurizer saves attributes correctly after loading data""" f = ImageFeaturizer() f.load_data(**LOAD_DATA_ARGS) compare_featurizer_class(f, (227, 227), np.zeros((1)), **COMPARE_ARGS) # Remove path to the generated csv at end of test if os.path.isdir('tests/ImageFeaturizer_testing/csv_tests'): shutil.rmtree('tests/ImageFeaturizer_testing/csv_tests')
def test_load_then_featurize_data_single_column(): """Test featurizations and attributes for each model are correct with multiple image columns""" feat = ImageFeaturizer() feat.load_data(**LOAD_DATA_ARGS) feat.featurize_preloaded_data(save_features=True) check_array = np.load(CHECK_ARRAY.format('squeezenet')) try: compare_featurizer_class(feat, (227, 227), check_array, featurized=True, check_csv=CHECK_CSV.format('squeezenet'), **COMPARE_ARGS) finally: # Remove path to the generated csv at the end of the test remove_generated_paths() del feat
def test_load_data_multiple_columns(): """Test featurizer loads data correctly with multiple image columns""" f = ImageFeaturizer(auto_sample=True) f.load_data(**LOAD_DATA_ARGS_MULT) compare_featurizer_class(f, (227, 227), np.zeros((1)), **COMPARE_ARGS_MULT)
def test_load_data_multiple_columns_no_csv(): """Test featurizer raises error if multiple columns passed with only a directory""" f = ImageFeaturizer() with pytest.raises(ValueError): f.load_data(**LOAD_DATA_ARGS_MULT_ERROR)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--infolder', default='../../original/Suomi/Kunta/', help='folder containing the source images') parser.add_argument('--coatfolder', default=None, help='folder containing the output images') parser.add_argument('--outimage', default='embedding.png', help='Name of the output image') parser.add_argument('--outcsv', default='vectors.csv', help='Name of the csv output') parser.add_argument('--dpi', default=300, type=int, help='Resolution of the output image') parser.add_argument('--zoom', default=0.1, type=float, help='size of a coat in output image') parser.add_argument('--depth', default=1, type=int, help='Network depth') parser.add_argument('--model', default='squeezenet', help='Network type') args = parser.parse_args() image_path = args.infolder outimage = args.outimage if args.coatfolder is None: coatfolder = image_path else: coatfolder = args.coatfolder # Extract vectors featurizer = ImageFeaturizer(depth=args.depth, autosample = False, model=args.model) featurizer.load_data('images', image_path=image_path) featurize_preloaded_df = featurizer.featurize_preloaded_data(save_features=True) # Output to csv file featurize_preloaded_df.to_csv(path_or_buf = args.outcsv, sep = '\t') print('Vector data saved to {}'.format(args.outcsv)) images = featurize_preloaded_df.values[:,0] paths = [coatfolder+x for x in images] # extract vectors: X = featurize_preloaded_df.values[:,2:] # First dimensionality reduction to 32 dimensions using truncated singular value decomposition Y = TruncatedSVD(32).fit_transform(X) # For plotting dimensionality reduction to 2D with TSNE Y = TSNE(n_components=2, perplexity=30.0).fit_transform(Y) # Plot the output image plt.tight_layout() fig, ax = plt.subplots() ax.set_axis_off() ax.scatter(Y[:,0], Y[:,1]) arr = [] for x0, y0, path in zip(Y[:,0], Y[:,1], paths): try: ab = AnnotationBbox(getImage(path, zoom=args.zoom), (x0, y0), frameon=False) arr.append(ax.add_artist(ab)) except FileNotFoundError: pass fig.savefig(outimage, dpi=args.dpi, bbox_inches='tight') print("{} saved.".format(outimage))