def main(): """The main program. """ parser = \ argparse.ArgumentParser(description='Deep-learning based classifiers') parser.add_argument('--train', action='store_true', default=False, help='train the autoencoder on the given datasource') parser.add_argument('--inference', action='store_true', default=False, help='perform inference with the autoencoder') parser.add_argument('--plot', action='store_true', default=False, help='plot codes and/or results') ToolboxArgparse.add_arguments(parser, ('network', )) args = parser.parse_args() ToolboxArgparse.process_arguments(parser, args) # # Create an autoencoder # data_shape = (28, 28, 1) # autoencoder = NetworkArgparse.network(parser, args) autoencoder = Autoencoder(shape=data_shape, code_dim=2, module='tf') # Further command line arguments display = Display() if args.plot else None # # Use the autoencoder # if args.train: datasource_train = Datasource(module='mnist', one_hot=True) demo_training(autoencoder, datasource_train, progress=tqdm, display=display) if args.inference: test_data = Datasource(module='mnist', section='test', one_hot=True) demo_inference(autoencoder, test_data)
def main(): """Main program: parse command line options and start video tools. """ parser = ArgumentParser(description='Deep learning based video processing') parser.add_argument('video', metavar='VIDEO', type=str, nargs='*', help='an image to use') parser.add_argument('--detect', action='store_true', default=True, help='run detection') parser.add_argument('--output', type=str, help='write output in the given video file') ToolboxArgparse.add_arguments(parser) args = parser.parse_args() ToolboxArgparse.process_arguments(args) if args.webcam: Datasource['Webcam'] webcam = Datasource['Webcam'] webcam.prepare() display = ImageDisplay(module='qt') try: with VideoWriter(filename='test.mp4', fps=3, size=None) as writer: # FIXME[bug]: fps seems to be ignored by ImageIO writer for i in range(100): data = webcam.get_data() print(i, data.array.shape) detector.process(data, mark=True) output_detections(detector, data, display=display, writer=writer) if display.closed: break except KeyboardInterrupt: print("stop")
def main(): """The main program. """ parser = \ argparse.ArgumentParser(description="Activation extraction from " "layers of a neural network") parser.add_argument('--gui', action='store_true', help='display activations in graphical user interface') parser.add_argument('--iterate', action='store_true', help='iterate over activation values') parser.add_argument('--top', type=int, help='obtain top n activation values') parser.add_argument('--store', action='store_true', help='store activation values') parser.add_argument('--archive', action='store_true', help='use activation values from archive') parser.add_argument('--store-top', action='store_true', help='store top activation values') parser.add_argument('image', metavar='IMAGE', nargs='*', help='input image(s)') ToolboxArgparse.add_arguments(parser) NetworkArgparse.prepare(parser, layers=True) DatasourceArgparse.prepare(parser) args = parser.parse_args() ToolboxArgparse.process_arguments(parser) network, layers = NetworkArgparse.network(parser, args, layers=True) network.summary(layers=layers) datasource = DatasourceArgparse.datasource(parser, args) # FIXME[hack]: GUI display global show_activations if args.gui: global app, window, activationview app = QApplication([]) window = QMainWindow() activationview = QActivationView() show_activations = gui_show_activations else: show_activations = console_show_activations if args.iterate: demo_iterate_activations(network, datasource) elif args.store: demo_store_activations(network, datasource) elif args.archive: demo_load_activations(network, datasource) elif args.top: demo_top_activations(network, datasource) elif args.store_top: demo_store_top_activations(network, datasource) elif datasource is not None: # # loop over the dataset # # FIXME[bug]: error in ActivationWorker extract_activations1(network, datasource, layers=layers) else: # image_file = 'images/elephant.jpg' for image_file in args.image: image = imread(image_file) # FIXME[bug]: error in ActivationWorker demo_image_activations1(network, image) demo_image_activations2(network, image) demo_image_activations3(network, image) demo_image_activations4(network, image) demo_image_activations5(network, image)
def main(): parser = \ argparse.ArgumentParser(description='Deep-learning based classifiers') parser.add_argument('--architecture', action='store_true', default=False, help="display the network's architecture") parser.add_argument('--summary', action='store_true', default=False, help="display the network's architecture") parser.add_argument('--backend-summary', action='store_true', default=False, help="display backend specific summary of the network") parser.add_argument('--layer', help='describe the specified layer') parser.add_argument('--field', help='extract receptive field') ToolboxArgparse.add_arguments(parser) NetworkArgparse.prepare(parser) args = parser.parse_args() ToolboxArgparse.process_arguments(parser, args) network = NetworkArgparse.network(parser, args) if network is None: print("No network was specified.") return if args.summary: print(f"Network: {network}") print(f" input: '{network.input_layer_id()}', " f"shape: {network.get_input_shape()}, " f"{network._get_input_shape()}") print(f" output: '{network.output_layer_id()}'," f" shape: {network.get_output_shape()}") if args.architecture: for index, layer in enumerate(network.layers()): print(f"{index:3} {str(layer):20} " f"{str(layer.input_shape):20} -> " f"{str(layer.output_shape):20}") if args.backend_summary: if 'network.keras' in sys.modules: KerasNetwork = sys.modules['network.keras'].Network if isinstance(network, KerasNetwork): network.model.summary() else: print("Don't know how to obtain a backend specific summary " "for this network, sorry!") if args.layer: layer = network[args.layer] print(f"Layer: {layer}") print(f" input: '{layer.input_shape}'") print(f" output: '{layer.output_shape}'") if args.field: unit = tuple(map(int, args.field.split(','))) print(f"Receptive field: {args.field}, {unit}, " f"{layer.receptive_field(unit)}") image = 'images/elephant.jpg' extract = network.extract_receptive_field(layer, unit, image) print(extract.shape) from dltb.util.image import imshow imshow(extract)
def main(): parser = ArgumentParser(description='Face labeling tool') parser.add_argument('--directory', type=str, default=DIRECTORY, help="path to the base directory " "(containing clean* subdirectories)") parser.add_argument('--clean0', type=str, help="path to the clean0 directory (optional)") parser.add_argument('--clean2', type=str, help="path to the clean2 directory (optional)") parser.add_argument('--clean4', type=str, help="path to the clean4 directory " "(including 'UnifiedFunneled2')") ToolboxArgparse.add_arguments(parser) args = parser.parse_args() ToolboxArgparse.process_arguments(parser, args) if args.clean0: directory_clean0 = args.clean0 elif DIRECTORY_CLEAN0 is None: directory_clean0 = os.path.join(args.directory, 'clean0') else: directory_clean0 = DIRECTORY_CLEAN0 if args.clean2: directory_clean2 = args.clean2 elif DIRECTORY_CLEAN2 is None: directory_clean2 = os.path.join(args.directory, 'clean2') else: directory_clean2 = DIRECTORY_CLEAN2 if args.clean4: directory_clean4 = args.clean4 elif DIRECTORY_CLEAN4 is None: directory_clean4 = os.path.join(args.directory, 'clean4') else: directory_clean4 = DIRECTORY_CLEAN4 directory_funneled = os.path.join(directory_clean4, 'UnifiedFunneled2') # # Some chanity checks # if not os.path.isdir(directory_clean4): logging.warning( "Clean4 directory '%s' does not exist " "- no data to label.", directory_clean4) sys.exit(1) if not os.path.isdir(directory_clean0): logging.warning( "Clean0 directory '%s' does not exist " "- some images may not be available.", directory_clean0) try: # RIFF (little-endian) data, Web/P image, VP8 encoding, 230x230 problematic_image = os.path.join(directory_clean4, 'UnifiedFunneled', 'AaronWolff', 'New', '1091847.jpg') import imageio from dltb.util.image import imread image = imread(problematic_image, module='imageio') except ValueError as ex: # imageio: ValueError: Could not find a format to read the # specified file in single-image mode logging.error("Problematic image file: '%s' (imageio version %s)", problematic_image, imageio.__version__) # error: imageio 2.9.0 [conda: pyhd3eb1b0_0 default] (Ubuntu 20.04) # error: imageio 2.9.0 [conda: py_0 conda-forge] (Ubuntu 20.04) # ok: imageio 2.6.1 [conda: py36_0 default] (Ubuntu 16.04) # print(ex, file=sys.stderr) #sys.exit(1) # # open the data set # try: datasource = ChildFaces(directory=directory_funneled, prepare=True) except Exception as ex: print(ex, file=sys.stderr) sys.exit(1) print(datasource) data = datasource[0] print(data) datasource.load_metadata(data) # FIXME[test] data.add_attribute('valid', True) datasource.write_metadata(data) labels = list(datasource.labels()) label = labels[0] faces = [ datasource.get_data(filename=filename) for filename in datasource.faces(label) ] print(faces) # # run the graphical user interface # app = QApplication([]) screensize = QApplication.desktop().screenGeometry() face_labeler = QFaceLabeler(datasource) #image_view = face_labeler.multiImageView #image_view.setImages(faces) # This will run the graphical interface gui = face_labeler gui.setMaximumSize(screensize.width() - 300, screensize.height() - 300) gui.show() rc = app.exec() logging.info(f"Main: exiting gracefully (rc=%d).", rc) sys.exit(rc)
def main(): """The main program. """ parser = \ argparse.ArgumentParser(description='Deep-learning based adversarial ' 'examples') parser.add_argument('--classify', action='store_true', help="classify given input files") parser.add_argument('--adversarial', action='store_true', help="perform adversarial attack") parser.add_argument('--target', action='store_true', default=False, help='create targeted adversarial example') parser.add_argument('--targets', type=str, help="class(es) for targeted adversarial attack") parser.add_argument('--show', action='store_true', help='show the images that are processed') parser.add_argument('--save', action='store_true', help='store adversarial examples') parser.add_argument('--reload', action='store_true', help='reload saved image') parser.add_argument('--verbose', action='store_true', help="increase output verbosity") parser.add_argument('--version', action='version', version=f"%(prog)s {__version__}") ToolboxArgparse.add_arguments(parser, components=('network', )) parser.add_argument('files', metavar='FILE', nargs='*', help="files to process") args = parser.parse_args() ToolboxArgparse.process_arguments(parser, args) # # Process the command line options # # output some information setup_logging(args) network = args.network logging.info("Network: %s", network.name) # network.range_info() if args.adversarial: attacker = \ IterativeGradientSignAttacker(max_steps=20, min_confidence=.99) logging.info("Targets=%s", args.targets) # # main loop of the program: process command line arguments # for filename in args.files: print(f"Loading image '{filename}'") # image = imread(input_filename) image_tensor = network.image_to_internal(filename) print("image_tensor:", type(image_tensor), image_tensor.shape, image_tensor.dtype, image_tensor.min(), image_tensor.max()) # Classifiy the original image if args.classify: network.classify_image(image_tensor) # Create the adversarial example if args.adversarial: # perform targeted adversarial attack for target in Selection(args.targets): output_filename = \ os.path.join(output_directory, 'adversarial_examples', f"image-{network.name}-{target}.png") original, adversarial = \ attacker(network, image_tensor, target=target, result=('original', 'adversarial')) diff = original.inputs - adversarial.inputs print("Maximal difference between image and " "adversarial example: " f"{np.abs(diff).max():.4f} " f"(real={diff.min()}/{diff.max()}, " f"levels={np.round(np.abs(diff).max()*255)})") # # Visualization # if args.show: eps = .123 # FIXME[hack] visualize(original, adversarial, eps) # # Storing the image # adversarial_uint8 = \ (adversarial.inputs*255).astype(np.uint8) if os.path.exists(output_filename): logging.warning("File '%s' already exists. " "Will not overwrite.", output_filename) else: if (filename.endswith('.jpeg') or filename.endswith('.jpg')): # imageio.imwrite(output_filename, adversarial_uint8, quality=100) else: imageio.imwrite(output_filename, adversarial_uint8) # # Reloading the image # if args.reload: relaoded_image_uint8 = imageio.imread(output_filename) relaoded_image_float = \ relaoded_image_uint8.astype(np.float32)/255 reload_diffb = \ adversarial_uint8 - relaoded_image_uint8 reload_diff = adversarial.inputs - relaoded_image_float print("Maximal image difference after reload " f"(image='{output_filename}'): " f"uint8={np.abs(reload_diffb).max()}, " f"float={np.abs(reload_diff).max():.4f}, " f"threshold={1./255:.4f}") adv_example3 = \ network.image_to_internal(relaoded_image_float) print("Predictions for the reloaded image:") network.classify_image(adv_example3, preprocess=False)
def main(): """Main program: parse command line options and start face tools. """ parser = ArgumentParser(description='Deep learning based face processing') parser.add_argument('images', metavar='IMAGE', type=str, nargs='*', help='an image to use') parser.add_argument('--webcam', action='store_true', default=False, help='run on webcam') parser.add_argument('--show', action='store_true', default=False, help='show results in a window') group_detector = parser.add_argument_group("Detector arguments") group_detector.add_argument('--detect', action='store_true', default=False, help='run face detection') group_detector.add_argument('--detector', type=str, help='the face detector to use') group_detector.add_argument('--list-detectors', action='store_true', default=False, help='list available detectors') group_detector.add_argument('--warper', type=str, default=None, help='the image warper to use') group_detector.add_argument('--list-warpers', action='store_true', default=False, help='list available image warpers') group_detector.add_argument('--size', type=str, default='112x112', help='size of the output image') group_detector.add_argument('--output-directory', type=str, default='output', help='path of the output directory') ToolboxArgparse.add_arguments(parser) DatasourceArgparse.prepare(parser) args = parser.parse_args() ToolboxArgparse.process_arguments(parser, args) if args.list_detectors: print("FaceDetector implementations:") for index, implementation in enumerate(implementations(FaceDetector)): print(f"{index+1}) {implementation}") return os.EX_OK if args.list_warpers: print("ImageWarper implementations:") for index, implementation in enumerate(ImageWarper.implementations()): print(f"{index+1}) {implementation}") return os.EX_OK # obtain the datasource if provided (otherwise None) datasource = DatasourceArgparse.datasource(parser, args) # obtain an ImageDisplay object if --show is set (otherwise None) display = get_display() if args.show else None # obtain the face detector detector = \ Detector(implementation='dltb.thirdparty.face_evolve.mtcnn.Detector') print(f"Detector: {detector} ({type(detector)})") # obtain the ImageWarper #warper = ImageWarper(implementation='dltb.thirdparty.skimage.ImageUtil') #warper = ImageWarper(implementation='dltb.thirdparty.opencv.ImageUtils') warper = ImageWarper(implementation=args.warper) # create the LandmarkAligner aligner = LandmarkAligner(detector=detector, size=args.size, warper=warper) if not datasource: for image in args.images: apply_single_hack(Image(image), detector, aligner, display=display) else: apply_multi_hack(datasource, detector, aligner, input_directory=datasource.directory, output_directory=Path(args.output_directory), progress=tqdm.tqdm, display=display) return os.EX_OK
def main(): """Main program: parse command line options and start face tools. """ parser = ArgumentParser(description='Deep learning based face processing') parser.add_argument('images', metavar='IMAGE', type=str, nargs='*', help='an image to use') parser.add_argument('--webcam', action='store_true', default=False, help='run on webcam') parser.add_argument('--show', action='store_true', default=True, help='show results in a window') parser.add_argument('--evaluate', action='store_true', default=True, help='perform evaluation') parser.add_argument('--output-directory', type=str, default='output', help='path of the output directory') group_detector = parser.add_argument_group("Detector arguments") group_detector.add_argument('--detect', action='store_true', default=False, help='run face detection') group_detector.add_argument('--detector', type=str, help='the face detector to use') group_detector.add_argument('--list-detectors', action='store_true', default=False, help='list available detectors') group_aligner = parser.add_argument_group("Alignment arguments") group_aligner.add_argument('--align', action='store_true', default=False, help='run face alignment') group_aligner.add_argument('--warper', type=str, default=None, help='the image warper to use') group_aligner.add_argument('--list-warpers', action='store_true', default=False, help='list available image warpers') group_aligner.add_argument('--size', type=str, default='112x112', help='size of the output image') group_recognizer = parser.add_argument_group("Recognition arguments") group_recognizer.add_argument('--verify', action='store_true', default=False, help='run face verification') ToolboxArgparse.add_arguments(parser) DatasourceArgparse.prepare(parser) args = parser.parse_args() ToolboxArgparse.process_arguments(parser, args) if args.list_detectors: print("FaceDetector implementations:") for index, implementation in enumerate(implementations(FaceDetector)): print(f"{index+1}) {implementation}") return if args.list_warpers: print("ImageWarper implementations:") for index, implementation in enumerate(ImageWarper.implementations()): print(f"{index+1}) {implementation}") return os.EX_OK # obtain the datasource if provided (otherwise None) datasource = DatasourceArgparse.datasource(parser, args) if args.detector: detector = FaceDetector(implementation=args.detector) elif args.detector: # FIXME[old] print(f"Detector class: {args.detector}") Detector = import_class(args.detector) detector = Detector() # 'haar', 'ssd', 'hog', 'cnn', 'mtcnn' # detector = Tool['haar'] # detector = Tool['ssd'] print(f"Detector: {detector} [prepared={detector.prepared}]") detector.prepare() print(f"Detector: {detector} [prepared={detector.prepared}]") if args.detect: if args.webcam: webcam = Webcam() display = ImageDisplay(module='qt') display.present(display_video, (webcam, detector)) for url in args.images: if os.path.isdir(url): datasource = ImageDirectory('images') datasource.prepare() for data in datasource: print(detector(data)) # detector.process(data, mark=True) # output_detections(detector, data) else: print(f"Applying detector to {url}") # print(detector(url)) result = ('detections', 'mark') # , 'extract') data = detector.process_image( url, result=result) #mark=True, extract=True data.debug() output_detections(detector, data) # , extract=True elif args.align: # # perform face alignment # # obtain the face detector detector_implementation = 'dltb.thirdparty.face_evolve.mtcnn.Detector' detector = FaceDetector(implementation=detector_implementation) print(f"Detector: {detector} ({type(detector)})") # obtain the ImageWarper warper = ImageWarper(implementation=args.warper) # create an aligner aligner = LandmarkAligner(detector=detector, size=args.size, warper=warper) # obtain an ImageDisplay object if --show is set (otherwise None) display = get_display() if args.show else None if not datasource: for image in args.images: apply_single_hack(Image(image), detector, aligner, display=display) else: apply_multi_hack(datasource, detector, aligner, input_directory=datasource.directory, output_directory=Path(args.output_directory), progress=tqdm.tqdm, display=display) elif args.evaluate: # obtain the face detector detector_implementation = 'dltb.thirdparty.face_evolve.mtcnn.Detector' detector = FaceDetector(implementation=detector_implementation) print(f"Detector: {detector} ({type(detector)})") # obtain the ImageWarper warper = ImageWarper(implementation=args.warper) # create an aligner aligner = LandmarkAligner(detector=detector, size=args.size, warper=warper) from dltb.thirdparty.arcface import ArcFace arcface = ArcFace(aligner=aligner) embedding_file_name = Path("embeddings.npz") if embedding_file_name.is_file(): content = np.load(embedding_file_name) embeddings, labels = content['embeddings'], content['labels'] else: iterable = datasource.pairs() iterable = tqdm.tqdm(iterable) embeddings, labels = arcface.embed_labeled_pairs(iterable) print(f"Writing embeddings of shape {embeddings.shape} to " f"'{embedding_file_name}'") np.savez_compressed(embedding_file_name, embeddings=embeddings, labels=labels) print("embeddings:", embeddings.shape, embeddings.dtype) print("labels:", labels.shape, labels.dtype) #for image1, image2, same in iterable: # print(image1.shape, image2.shape, same) # embedding1 = embed(image1) # embedding2 = embed(image1) # distance = distance(embedding1, embedding2) else: print("No operation specified.")
def main(): parser = \ argparse.ArgumentParser(description='GAN demonstration program') parser.add_argument('--list', action='store_true', default=False, help='list GAN implementations') parser.add_argument('--class', type=str, default='ImageGAN', help='GAN implementation') parser.add_argument('--model', type=str, default=None, help='use pretrained model') parser.add_argument('--cats', dest='model', action='store_const', const='cats', help='use the cats model') parser.add_argument('--cars', dest='model', action='store_const', const='cars', help='use the cars model') parser.add_argument('--bedrooms', dest='model', action='store_const', const='bedrooms', help='use the bedrooms model') parser.add_argument('--celebahq', dest='model', action='store_const', const='celebahq', help='use the celebahq model') parser.add_argument('--ffhq', dest='model', action='store_const', const='ffhq', help='use the ffhq model') parser.add_argument('--filename', type=str, default=None, help='filename for loading model') parser.add_argument('--seed', type=int, default=None, help='generate image for given seed') parser.add_argument('--random', action='store_true', default=False, help='choose random seed for image generation') parser.add_argument('--transition', action='store_true', default=False, help='transition between two images') parser.add_argument('--mix', type=int, default=None, help='mix style of two images') parser.add_argument('--show', action='store_true', default=False, help='show generated image(s)') parser.add_argument('--store', action='store_true', default=False, help='store generated image to disk') ToolboxArgparse.add_arguments(parser) args = parser.parse_args() ToolboxArgparse.process_arguments(parser) if args.list: for implementation in implementations('ImageGAN'): print(f" - {implementation}") return # Instantiation and initialization ImageGANImplementation = import_class(getattr(args, 'class')) gan = ImageGANImplementation(model=args.model, filename=args.filename) gan.prepare() gan.info() if args.seed is not None: image = gan.generate(seed=args.seed) if args.show: imshow(image) if args.store: filename = gan.model + '-' + str(args.seed) + '.jpg' print(f"Writing image to '{filename}'") imwrite(filename, image) elif args.transition: transition = gan.transition(400, 602, steps=200) print(transition.array.shape) with ImageDisplay() as display: for index, image in enumerate(transition): display.show(image, timeout=.1, title=f"{index}/{len(transition)}") elif args.mix: with ImageDisplay() as display: mix_max = 18 for mix in range(mix_max): image = gan.mix(mix=mix) display.show(image, timeout=1., title=f"{mix}/{mix_max}") elif False: # FIXME[hack]: add option "--slideshow" once this is finished slideshow = Slideshow(gan) with ImageDisplay() as display: while True: # not display.closed: # FIXME[todo]: context manager should show the window slideshow.step(step=2) display.show(slideshow.canvas, timeout=.01, unblock='freeze', title=f"Slideshow ({slideshow._offset})") if display.closed: break # FIXME[todo] else: # Display randomly generated images with ImageDisplay() as display: while True: # not display.closed: # FIXME[todo]: context manager should show the window seed = random.randint(0, 10000) image = gan.generate(seed=seed) display.show(image, timeout=0, unblock='freeze', title=f"Seed={seed} -> {image.array.shape}") if display.closed: break # FIXME[todo]