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_featurize_first(): """Test that the featurizer raises an error if featurize is called before loading data""" f = ImageFeaturizer() # Raise error if attempting to featurize before loading data with pytest.raises(IOError): f.featurize_preloaded_data()
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))