def visualize_activations(self, stacked_shreds): # TODO: hard-coded left and right indexes. change if you like. don't want to spend time. if self._comparator.t > 2: left, right = self._comparator.t + 1, self._comparator.t + 2 else: left, right = 0, 1 tensor = ComparatorCNN._prepare_left_right_check( stacked_shreds[0][left], stacked_shreds[0][right]) root = os.path.join(os.path.dirname(__file__), 'class_activation_maps') os.makedirs('class_activation_maps', exist_ok=True) for i, layer in enumerate(self._comparator._model.layers[1:]): try: layer_name = layer.name if 'conv' not in layer_name: continue cam = visualize_cam(self._comparator._model, i, None, tensor) plt.imshow(stacked_shreds[0][left], cmap='gray') plt.imshow(cam, cmap='hot', interpolation='nearest', alpha=0.15) plt.title('#{}:{}'.format(i, layer_name)) current_milli_time = lambda: int(round(time.time() * 1000)) plt.savefig( os.path.join( root, '{}_{}_{}.png'.format(i, layer_name, current_milli_time()))) plt.clf() print(cam) except: print("Exception!")
def main(): if 'large' in sys.argv: number_of_samples = sys.maxsize else: number_of_samples = 20 ts = list() if '2' in sys.argv: ts.append(2) if '4' in sys.argv: ts.append(4) if '5' in sys.argv: ts = [ 5, ] if 0 == len(ts): ts = (2, 4, 5) image_types = list() if 'image' in sys.argv: image_types.append(ImageType.IMAGES) if 'document' in sys.argv: image_types.append(ImageType.DOCUMENTS) if 0 == len(image_types): image_types = ImageType np.random.seed(42) for t in ts: for image_type in image_types: print('t={}. image type is {}'.format(t, image_type.value)) if image_type == ImageType.IMAGES: get_images = DataProvider().get_fish_images else: get_images = DataProvider().get_docs_images images = get_images(num_samples=number_of_samples) images_train, images_validation = train_test_split(images, random_state=42) clf = ComparatorCNN(t, IMAGE_TYPE_TO_T_TO_COMPARATOR_CNN_WEIGHT_FILE_ID_AND_FILE_PATH[image_type][t].width, IMAGE_TYPE_TO_T_TO_COMPARATOR_CNN_WEIGHT_FILE_ID_AND_FILE_PATH[image_type][t].height, image_type) \ .load_weights(IMAGE_TYPE_TO_T_TO_COMPARATOR_CNN_WEIGHT_FILE_ID_AND_FILE_PATH[image_type][t].model_path) cam = ComparatorActivationMap(clf) for image in images_train: cam.visualize_activations( shred_and_resize_to([image], t, (clf.width, clf.height)))
def load_models(): global image_type_to_t_to_comparator image_type_to_t_to_comparator = { image_type: { t: ComparatorCNN( t, IMAGE_TYPE_TO_T_TO_COMPARATOR_CNN_WEIGHT_FILE_ID_AND_FILE_PATH[ image_type][t].width, IMAGE_TYPE_TO_T_TO_COMPARATOR_CNN_WEIGHT_FILE_ID_AND_FILE_PATH[ image_type][t].height, image_type, IMAGE_TYPE_TO_MEAN[image_type], IMAGE_TYPE_TO_STD[image_type]). load_weights( IMAGE_TYPE_TO_T_TO_COMPARATOR_CNN_WEIGHT_FILE_ID_AND_FILE_PATH[ image_type][t].model_path) for t in TS } for image_type in ImageType }
def debug(): names_train = ['n01440764_11593.JPEG', 'n01440764_11602.JPEG', 'n01440764_4562.JPEG', 'n01440764_5148.JPEG', 'n01440764_11897.JPEG', 'n01440764_29057.JPEG', 'n01440764_22135.JPEG', 'n01440764_8003.JPEG', 'n01440764_3566.JPEG', 'n01440764_44.JPEG', 'n01440764_10910.JPEG', 'n01440764_10382.JPEG', 'n01440764_6508.JPEG', 'n01440764_10290.JPEG', 'n01440764_910.JPEG'] images_train, names_train = DataProvider.read_images('../images', names_train) indices = [4, 5, 13] images_validation = [images_train[index] for index in indices] index = 4 t = 4 permutation = [2, 8, 4, 12, 0, 10, 6, 5, 7, 3, 13, 15, 11, 9, 1, 14] width = 224 height = 224 image_type = ImageType.IMAGES cmp = ComparatorCNN(t, width, height, image_type) \ ._fit_standardisation(images_train) \ .load_weights(IMAGE_TYPE_TO_T_TO_COMPARATOR_CNN_WEIGHT_FILE_ID_AND_FILE_PATH[image_type][t].model_path) slv = SolverGreedy({t: cmp}) score = slv.evaluate_image_for_permutation(images_validation[0], permutation, sample_index=index) print('done with ', score)
def main(): if 'debug' in sys.argv: print('Debug') number_of_samples = 20 epochs = 1 else: print('Release') number_of_samples = sys.maxsize epochs = 1 ts = list() if '2' in sys.argv: ts.append(2) if '4' in sys.argv: ts.append(4) if '5' in sys.argv: ts = [ 5, ] if 0 == len(ts): ts = (2, 4, 5) image_types = list() if 'image' in sys.argv: image_types.append(ImageType.IMAGES) if 'document' in sys.argv: image_types.append(ImageType.DOCUMENTS) if 0 == len(image_types): image_types = ImageType np.random.seed(42) for image_type in image_types: print(image_type.value) if image_type == ImageType.IMAGES: get_images = DataProvider().get_fish_images mean = 100.52933494138787 std = 65.69793156777682 else: get_images = DataProvider().get_docs_images mean = 241.46115784237548 std = 49.512839464023564 images, names = get_images(num_samples=number_of_samples, return_names=True) images_train, images_validation, names_train, names_validation = train_test_split( images, names, random_state=42) t_to_comparator = { t: ComparatorCNN( t, IMAGE_TYPE_TO_T_TO_COMPARATOR_CNN_WEIGHT_FILE_ID_AND_FILE_PATH[ image_type][t].width, IMAGE_TYPE_TO_T_TO_COMPARATOR_CNN_WEIGHT_FILE_ID_AND_FILE_PATH[ image_type][t].height, image_type, mean=mean, std=std). load_weights( IMAGE_TYPE_TO_T_TO_COMPARATOR_CNN_WEIGHT_FILE_ID_AND_FILE_PATH[ image_type][t].model_path) for t in ts } clf = SolverLP(t_to_comparator, image_type=image_type) print('Train: ', names_train) accuracy = clf.evaluate(images_train, epochs=epochs, ts=ts) print('Train 0-1 accuracy on {}: {}'.format(image_type.value, accuracy)) print('Validation: ', names_validation) accuracy = clf.evaluate(images_validation, epochs=epochs, ts=ts) print('Validation 0-1 accuracy on {}: {}'.format( image_type.value, accuracy))
from solvers.solver_greedy import SolverGreedy from utils.shredder import Shredder from utils.image_type import ImageType from models.fish_or_doc_classifier import FishOrDocClassifier from models.comparator_cnn import ComparatorCNN fish_or_doc_classifier = FishOrDocClassifier( weights_file=IMAGE_OR_DOCUMENT_WEIGHT_FILE_ID_AND_FILE_PATH.model_path) image_type_to_solver_with_comparator = { image_type: SolverGreedy( { t: ComparatorCNN( t, IMAGE_TYPE_TO_T_TO_COMPARATOR_CNN_WEIGHT_FILE_ID_AND_FILE_PATH[ image_type][t].width, IMAGE_TYPE_TO_T_TO_COMPARATOR_CNN_WEIGHT_FILE_ID_AND_FILE_PATH[ image_type][t].height, image_type, IMAGE_TYPE_TO_MEAN[image_type], IMAGE_TYPE_TO_STD[image_type]). load_weights( IMAGE_TYPE_TO_T_TO_COMPARATOR_CNN_WEIGHT_FILE_ID_AND_FILE_PATH[ image_type][t].model_path) for t in TS }, image_type=image_type) for image_type in ImageType } def predict(images): reconstructed_image = Shredder.reconstruct(images) print('It is ', end='')
def main(): if 'debug' in sys.argv: print('Debug') number_of_samples = 20 epochs = 1 else: print('Release') number_of_samples = sys.maxsize epochs = 1 ts = list() if '2' in sys.argv: ts.append(2) if '4' in sys.argv: ts.append(4) if '5' in sys.argv: ts = [5, ] if 0 == len(ts): ts = (2, 4, 5) image_types = list() if 'image' in sys.argv: image_types.append(ImageType.IMAGES) if 'document' in sys.argv: image_types.append(ImageType.DOCUMENTS) if 'version' in sys.argv: version = int(sys.argv[sys.argv.index('version') + 1]) else: version = 2 if 0 == version: iterate_on_bottom_values = (False, ) iterate_on_right_values = (False, ) column_then_row_values = (False, ) iterate_first_shred = False try_to_improve_with_row_permutation = False width = 224 height = 224 elif 1 == version: iterate_on_bottom_values = (False, ) iterate_on_right_values = (False, ) column_then_row_values = (False, ) iterate_first_shred = True try_to_improve_with_row_permutation = False width = 224 height = 224 elif 2 == version: iterate_on_bottom_values = (False, True) iterate_on_right_values = (False, True) column_then_row_values = (False, True) iterate_first_shred = True try_to_improve_with_row_permutation = False width = 224 height = 224 else: # if 3 <= version iterate_on_bottom_values = (False, True) iterate_on_right_values = (False, True) column_then_row_values = (False, True) iterate_first_shred = True try_to_improve_with_row_permutation = True width = 2200 // 5 height = 2200 // 5 if 0 == len(image_types): image_types = ImageType np.random.seed(42) for image_type in image_types: print(image_type.value) if image_type == ImageType.IMAGES: get_images = DataProvider().get_fish_images mean = 100.52933494138787 std = 65.69793156777682 else: get_images = DataProvider().get_docs_images mean = 241.46115784237548 std = 49.512839464023564 images, names = get_images(num_samples=number_of_samples, return_names=True) images_train, images_validation, names_train, names_validation = train_test_split(images, names, random_state=42) t_to_comparator = { t: ComparatorCNN(t, width, height, image_type, mean=mean, std=std) .load_weights() for t in ts } clf = SolverGreedy(t_to_comparator, image_type=image_type, iterate_on_bottom_values=iterate_on_bottom_values, iterate_on_right_values=iterate_on_right_values, column_then_row_values=column_then_row_values, iterate_first_shred=iterate_first_shred, try_to_improve_with_row_permutation=try_to_improve_with_row_permutation) print('Train: ', names_train) accuracy = clf.evaluate(images_train, epochs=epochs, ts=ts) print('Train 0-1 accuracy on {}: {}'.format(image_type.value, accuracy)) print('Validation: ', names_validation) accuracy = clf.evaluate(images_validation, epochs=epochs, ts=ts) print('Validation 0-1 accuracy on {}: {}'.format(image_type.value, accuracy))
def main(): if 'debug' in sys.argv: print('Debug') number_of_samples = 20 epochs = 1 else: print('Release') number_of_samples = sys.maxsize epochs = 5 ts = list() if '2' in sys.argv: ts.append(2) if '4' in sys.argv: ts.append(4) if '5' in sys.argv: ts = [ 5, ] if 0 == len(ts): ts = (2, 4, 5) image_types = list() if 'image' in sys.argv: image_types.append(ImageType.IMAGES) if 'document' in sys.argv: image_types.append(ImageType.DOCUMENTS) if 0 == len(image_types): image_types = ImageType np.random.seed(42) for image_type in image_types: print(image_type.value) if image_type == ImageType.IMAGES: get_images = DataProvider().get_fish_images mean = IMAGE_TYPE_TO_MEAN[image_type] std = IMAGE_TYPE_TO_STD[image_type] else: get_images = DataProvider().get_docs_images mean = IMAGE_TYPE_TO_MEAN[image_type] std = IMAGE_TYPE_TO_STD[image_type] images, names = get_images(num_samples=number_of_samples, return_names=True) images_train, images_validation, names_train, names_validation = train_test_split( images, names, random_state=42) t_to_comparator = { t: ComparatorCNN( t, IMAGE_TYPE_TO_T_TO_COMPARATOR_CNN_WEIGHT_FILE_ID_AND_FILE_PATH[ image_type][t].width, IMAGE_TYPE_TO_T_TO_COMPARATOR_CNN_WEIGHT_FILE_ID_AND_FILE_PATH[ image_type][t].height, image_type, mean=mean, std=std). load_weights( IMAGE_TYPE_TO_T_TO_COMPARATOR_CNN_WEIGHT_FILE_ID_AND_FILE_PATH[ image_type][t].model_path) for t in ts } t_to_backup_solver = { t: SolverGreedy(t_to_comparator, image_type=image_type) for t in ts } clf = SolverPairwiseMerge(t_to_comparator, t_to_backup_solver=t_to_backup_solver, image_type=image_type) print('Train: ', names_train) accuracy = clf.evaluate(images_train, epochs=epochs, ts=ts) print('Train 0-1 accuracy on {}: {}'.format(image_type.value, accuracy)) print('Validation: ', names_validation) accuracy = clf.evaluate(images_validation, epochs=epochs, ts=ts) print('Validation 0-1 accuracy on {}: {}'.format( image_type.value, accuracy))