def test_calculate_fingerprint(): SCRIPTLOC = os.path.dirname(__file__) data = load_jpg('{}/data/j8za09050_drz_small.jpg'.format(SCRIPTLOC)) fresnet = FingerprintCalculatorResnet() predictions = fresnet.calculate(data[:224, :224]) assert predictions[0][:2] == ('n03223299', 'doormat') assert np.allclose(predictions[0][2], 0.40468791, atol=0.1) assert predictions[1][:2] == ('n04589890', 'window_screen') assert np.allclose(predictions[1][2], 0.14722134, atol=0.1) assert predictions[2][:2] == ('n03857828', 'oscilloscope') assert np.allclose(predictions[2][2], 0.051630393, atol=0.1)
def test_carina(): # Load the data. carina_location = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data/carina.tiff') image_data = Data(location=carina_location, radec=(10.7502222, -59.8677778), meta={}, processing=[]) image_data.get_data() # Add to the data collection dc = DataCollection() dc.add(image_data) assert len(dc) == 1 # # Create the cutouts with a processing step applied # sliding_window_cutouts = BasicCutoutGenerator(output_size=224, step_size=550) cc = CutoutCollection() for cutout in sliding_window_cutouts.create_cutouts(image_data): cc.add(cutout) assert len(cc) == 35 cmp_arr = np.array( [[[51, 66, 69], [50, 70, 78]], [[48, 66, 72], [49, 65, 72]]], dtype=np.uint8) assert np.allclose(cc[0].get_data()[:2, :2], cmp_arr) # # Compute the fingerprints for each cutout # fc = FingerprintCollection() fc_save = FingerprintCalculatorResnet().save() for fingerprint in fingerprint_calculate(cc, fc_save): fc.add(fingerprint) assert [x[1] for x in fc[0].predictions[:3] ] == ['hammerhead', 'stingray', 'binder'] # # Compute the similarity metrics # similarity_tsne = similarity_calculate(fc, 'tsne') assert True
def test_end2end(): # Load the data. carina_location = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data/carina.tiff') image_data = Data(location=carina_location, radec=(10.7502222, -59.8677778), meta={}, processing=[]) image_data.get_data() # Add to the data collection dc = DataCollection() dc.add(image_data) # # Create the cutouts with a processing step applied # sliding_window_cutouts = BasicCutoutGenerator(output_size=224, step_size=550) cc = CutoutCollection() for cutout in sliding_window_cutouts.create_cutouts(image_data): cc.add(cutout) # # Compute the fingerprints for each cutout # fc = FingerprintCollection() fc_save = FingerprintCalculatorResnet().save() for fingerprint in fingerprint_calculate(cc, fc_save): fc.add(fingerprint) # # Compute the similarity metrics # similarity_tsne = similarity_calculate(fc, 'tsne') new_similarity_tsne = Similarity.factory(similarity_tsne.save()) assert json.dumps(new_similarity_tsne.save(), sort_keys=True) == json.dumps(similarity_tsne.save(), sort_keys=True)
# print('Going to calculate the sliding window cutouts') sliding_window_cutouts = BasicCutoutGenerator(output_size=224, step_size=112) # # Create the cutouts # cutouts = sliding_window_cutouts.create_cutouts(data) # # Compute the fingerprints for each cutout # print('Calculate the fingerprint for each cutout') fc_save = FingerprintCalculatorResnet().save() fingerprints = fingerprint_calculate(cutouts, fc_save) # # Compute the similarity metrics # print('Calculating the tSNE similarity') similarity_tsne = similarity_calculate(fingerprints, 'tsne') with open('similarity_tsne.pck', 'wb') as fp: pickle.dump(similarity_tsne.save(), fp) print('Calculating the Jaccard similarity') similarity_jaccard = similarity_calculate(fingerprints, 'jaccard') with open('similarity_jaccard.pck', 'wb') as fp: pickle.dump(similarity_jaccard.save(), fp)
full_cutout = FullImageCutoutGenerator(output_size=(224, 224)) # # Now create the cutouts based on the generator and the data. # cutout_processing = [cutout_crop, cutout_resize] cutouts = full_cutout.create_cutouts(data_collection, cutout_processing=cutout_processing) # # Create the fingerprint calculator... fingerprint # print('Creating the info for the fingerprint calculator') fresnet = FingerprintCalculatorResnet() fc_save = fresnet.save() # # Calcualte the fingerprint for each of the cutouts in # the cutout collection. # print('Calculating the fingerprints') fingerprints = fingerprint_calculate(cutouts, fc_save) # # Calcualte the similarity from the fingerprints based # on three differnt methods. #