def main():
    parser = argparse.ArgumentParser(description='Loads a pickled NetMaxTracker and outputs one or more of {the patches of the image, a deconv patch, a backprop patch} associated with the maxes.')
    parser.add_argument('--N',           type = int, default = 9, help = 'Note and save top N activations.')
    parser.add_argument('--gpu',         action = 'store_true', help = 'Use gpu.')
    parser.add_argument('--do-maxes',    action = 'store_true', help = 'Output max patches.')
    parser.add_argument('--do-deconv',   action = 'store_true', help = 'Output deconv patches.')
    parser.add_argument('--do-deconv-norm', action = 'store_true', help = 'Output deconv-norm patches.')
    parser.add_argument('--do-backprop', action = 'store_true', help = 'Output backprop patches.')
    parser.add_argument('--do-backprop-norm', action = 'store_true', help = 'Output backprop-norm patches.')
    parser.add_argument('--do-info',     action = 'store_true', help = 'Output info file containing max filenames and labels.')
    parser.add_argument('--idx-begin',   type = int, default = None, help = 'Start at this unit (default: all units).')
    parser.add_argument('--idx-end',     type = int, default = None, help = 'End at this unit (default: all units).')
    
    parser.add_argument('nmt_pkl',       type = str, help = 'Which pickled NetMaxTracker to load.')
    parser.add_argument('net_prototxt',  type = str, help = 'Network prototxt to load')
    parser.add_argument('net_weights',   type = str, help = 'Network weights to load')
    parser.add_argument('datadir',       type = str, help = 'Directory to look for files in')
    parser.add_argument('filelist',      type = str, help = 'List of image files to consider, one per line. Must be the same filelist used to produce the NetMaxTracker!')
    parser.add_argument('outdir',        type = str, help = r'Which output directory to use. Files are output into outdir/layer/unit_%%04d/{maxes,deconv,backprop}_%%03d.png')
    parser.add_argument('layer',         type = str, help = 'Which layer to output')
    #parser.add_argument('--mean', type = str, default = '', help = 'data mean to load')
    args = parser.parse_args()

    if args.gpu:
        caffe.set_mode_gpu()
    else:
        caffe.set_mode_cpu()

    imagenet_mean = load_imagenet_mean()
    net = caffe.Classifier(args.net_prototxt, args.net_weights,
                           mean=imagenet_mean,
                           channel_swap=(2,1,0),
                           raw_scale=255,
                           image_dims=(256, 256))

    assert args.do_maxes or args.do_deconv or args.do_deconv_norm or args.do_backprop or args.do_backprop_norm or args.do_info, 'Specify at least one do_* option to output.'

    with open(args.nmt_pkl, 'rb') as ff:
        nmt = pickle.load(ff)
    mt = nmt.max_trackers[args.layer]

    if args.idx_begin is None:
        args.idx_begin = 0
    if args.idx_end is None:
        args.idx_end = mt.max_vals.shape[0]
    
    with WithTimer('Saved %d images per unit for %s units %d:%d.' % (args.N, args.layer, args.idx_begin, args.idx_end)):
        output_max_patches(mt, net, args.layer, args.idx_begin, args.idx_end,
                           args.N, args.datadir, args.filelist, args.outdir,
                           (args.do_maxes, args.do_deconv, args.do_deconv_norm, args.do_backprop, args.do_backprop_norm, args.do_info))
Exemple #2
0
def main():
    parser = argparse.ArgumentParser(
        description='Loads a pickled NetMaxTracker and outputs one or more of {the patches of the image, a deconv patch, a backprop patch} associated with the maxes.')
    parser.add_argument('--model')
    parser.add_argument('--idx-begin', type=int, default=None, help='Start at this unit (default: all units).')
    parser.add_argument('--idx-end', type=int, default=None, help='End at this unit (default: all units).')
    args = parser.parse_args()
    settings = Settings.Settings()
    settings.load_settings(args.model)

    net = load_network(settings)

    # set network batch size
    current_input_shape = net.blobs[net.inputs[0]].shape
    current_input_shape[0] = settings.max_tracker_batch_size
    net.blobs[net.inputs[0]].reshape(*current_input_shape)
    net.reshape()

    assert settings.max_tracker_do_maxes or settings.max_tracker_do_deconv or settings.max_tracker_do_deconv_norm or settings.max_tracker_do_backprop or settings.max_tracker_do_backprop_norm, 'Specify at least one do_* option to output.'

    nmt = load_max_tracker_from_file(settings.find_maxes_output_file)

    for layer_name in settings.layers_to_output_in_offline_scripts:

        print 'Started work on layer %s' % (layer_name)

        mt = nmt.max_trackers[layer_name]

        if args.idx_begin is None:
            idx_begin = 0
        if args.idx_end is None:
            idx_end = mt.max_vals.shape[0]

        with WithTimer('Saved %d images per unit for %s units %d:%d.' % (settings.N, layer_name, idx_begin, idx_end)):

            output_max_patches(settings, mt, net, layer_name, idx_begin, idx_end,
                               settings.N, settings.data_dir, settings.deepvis_outputs_path, False,
                               (settings.max_tracker_do_maxes, settings.max_tracker_do_deconv,
                                settings.max_tracker_do_deconv_norm, settings.max_tracker_do_backprop,
                                settings.max_tracker_do_backprop_norm, True))

            if settings.search_min:
                output_max_patches(settings, mt, net, layer_name, idx_begin, idx_end,
                                   settings.N, settings.data_dir, settings.deepvis_outputs_path, True,
                                   (settings.max_tracker_do_maxes, settings.max_tracker_do_deconv,
                                    settings.max_tracker_do_deconv_norm, settings.max_tracker_do_backprop,
                                    settings.max_tracker_do_backprop_norm, True))
def main():
    parser = argparse.ArgumentParser(description='Loads a pickled NetMaxTracker and outputs one or more of {the patches of the image, a deconv patch, a backprop patch} associated with the maxes.')
    parser.add_argument('--N',            type = int, default = 9, help = 'Note and save top N activations.')
    parser.add_argument('--gpu',          action = 'store_true', default=settings.caffevis_mode_gpu, help = 'Use gpu.')
    parser.add_argument('--do-maxes',     action = 'store_true', default=settings.max_tracker_do_maxes, help = 'Output max patches.')
    parser.add_argument('--do-deconv',    action = 'store_true', default=settings.max_tracker_do_deconv, help = 'Output deconv patches.')
    parser.add_argument('--do-deconv-norm', action = 'store_true', default=settings.max_tracker_do_deconv_norm, help = 'Output deconv-norm patches.')
    parser.add_argument('--do-backprop',  action = 'store_true', default=settings.max_tracker_do_backprop, help = 'Output backprop patches.')
    parser.add_argument('--do-backprop-norm', action = 'store_true', default=settings.max_tracker_do_backprop_norm, help = 'Output backprop-norm patches.')
    parser.add_argument('--do-info',      action = 'store_true', default=settings.max_tracker_do_info, help = 'Output info file containing max filenames and labels.')
    parser.add_argument('--idx-begin',    type = int, default = None, help = 'Start at this unit (default: all units).')
    parser.add_argument('--idx-end',      type = int, default = None, help = 'End at this unit (default: all units).')
    
    parser.add_argument('--nmt_pkl',      type = str, default = os.path.join(settings.caffevis_outputs_dir, 'find_max_acts_output.pickled'), help = 'Which pickled NetMaxTracker to load.')
    parser.add_argument('--net_prototxt', type = str, default = settings.caffevis_deploy_prototxt, help = 'network prototxt to load')
    parser.add_argument('--net_weights',  type = str, default = settings.caffevis_network_weights, help = 'network weights to load')
    parser.add_argument('--datadir',      type = str, default = settings.static_files_dir, help = 'directory to look for files in')
    parser.add_argument('--filelist',     type = str, default = settings.static_files_input_file, help = 'List of image files to consider, one per line. Must be the same filelist used to produce the NetMaxTracker!')
    parser.add_argument('--outdir',       type = str, default = settings.caffevis_outputs_dir, help = 'Which output directory to use. Files are output into outdir/layer/unit_%%04d/{maxes,deconv,backprop}_%%03d.png')
    parser.add_argument('--search-min',    action='store_true', default=False, help='Should we also search for minimal activations?')
    args = parser.parse_args()

    settings.caffevis_deploy_prototxt = args.net_prototxt
    settings.caffevis_network_weights = args.net_weights

    net, data_mean = load_network(settings)

    # validate batch size
    if settings.is_siamese and settings._calculated_siamese_network_format == 'siamese_batch_pair':
        # currently, no batch support for siamese_batch_pair networks
        # it can be added by simply handle the batch indexes properly, but it should be thoroughly tested
        assert (settings.max_tracker_batch_size == 1)

    # set network batch size
    current_input_shape = net.blobs[net.inputs[0]].shape
    current_input_shape[0] = settings.max_tracker_batch_size
    net.blobs[net.inputs[0]].reshape(*current_input_shape)
    net.reshape()

    assert args.do_maxes or args.do_deconv or args.do_deconv_norm or args.do_backprop or args.do_backprop_norm or args.do_info, 'Specify at least one do_* option to output.'

    siamese_helper = SiameseHelper(settings.layers_list)

    nmt = load_max_tracker_from_file(args.nmt_pkl)

    for layer_name in settings.layers_to_output_in_offline_scripts:

        print 'Started work on layer %s' % (layer_name)

        normalized_layer_name = siamese_helper.normalize_layer_name_for_max_tracker(layer_name)

        mt = nmt.max_trackers[normalized_layer_name]

        if args.idx_begin is None:
            idx_begin = 0
        if args.idx_end is None:
            idx_end = mt.max_vals.shape[0]

        with WithTimer('Saved %d images per unit for %s units %d:%d.' % (args.N, normalized_layer_name, idx_begin, idx_end)):

            output_max_patches(settings, mt, net, normalized_layer_name, idx_begin, idx_end,
                               args.N, args.datadir, args.filelist, args.outdir, False,
                               (args.do_maxes, args.do_deconv, args.do_deconv_norm, args.do_backprop, args.do_backprop_norm, args.do_info))

            if args.search_min:
                output_max_patches(settings, mt, net, normalized_layer_name, idx_begin, idx_end,
                                   args.N, args.datadir, args.filelist, args.outdir, True,
                                   (args.do_maxes, args.do_deconv, args.do_deconv_norm, args.do_backprop, args.do_backprop_norm, args.do_info))
def main():
    parser = argparse.ArgumentParser(
        description=
        'Loads a pickled NetMaxTracker and outputs one or more of {the patches of the image, a deconv patch, a backprop patch} associated with the maxes.'
    )
    parser.add_argument('--N',
                        type=int,
                        default=9,
                        help='Note and save top N activations.')
    parser.add_argument('--gpu', action='store_true', help='Use gpu.')
    parser.add_argument('--do-maxes',
                        action='store_true',
                        help='Output max patches.')
    parser.add_argument('--do-deconv',
                        action='store_true',
                        help='Output deconv patches.')
    parser.add_argument('--do-deconv-norm',
                        action='store_true',
                        help='Output deconv-norm patches.')
    parser.add_argument('--do-backprop',
                        action='store_true',
                        help='Output backprop patches.')
    parser.add_argument('--do-backprop-norm',
                        action='store_true',
                        help='Output backprop-norm patches.')
    parser.add_argument(
        '--do-info',
        action='store_true',
        help='Output info file containing max filenames and labels.')
    parser.add_argument('--idx-begin',
                        type=int,
                        default=None,
                        help='Start at this unit (default: all units).')
    parser.add_argument('--idx-end',
                        type=int,
                        default=None,
                        help='End at this unit (default: all units).')

    parser.add_argument('nmt_pkl',
                        type=str,
                        help='Which pickled NetMaxTracker to load.')
    parser.add_argument('net_prototxt',
                        type=str,
                        help='Network prototxt to load')
    parser.add_argument('net_weights',
                        type=str,
                        help='Network weights to load')
    parser.add_argument('datadir',
                        type=str,
                        help='Directory to look for files in')
    parser.add_argument(
        'filelist',
        type=str,
        help=
        'List of image files to consider, one per line. Must be the same filelist used to produce the NetMaxTracker!'
    )
    parser.add_argument(
        'outdir',
        type=str,
        help=
        r'Which output directory to use. Files are output into outdir/layer/unit_%%04d/{maxes,deconv,backprop}_%%03d.png'
    )
    parser.add_argument('layer', type=str, help='Which layer to output')
    #parser.add_argument('--mean', type = str, default = '', help = 'data mean to load')
    args = parser.parse_args()

    if args.gpu:
        caffe.set_mode_gpu()
    else:
        caffe.set_mode_cpu()

    imagenet_mean = load_imagenet_mean()
    net = caffe.Classifier(args.net_prototxt,
                           args.net_weights,
                           mean=imagenet_mean,
                           channel_swap=(2, 1, 0),
                           raw_scale=255,
                           image_dims=(256, 256))

    assert args.do_maxes or args.do_deconv or args.do_deconv_norm or args.do_backprop or args.do_backprop_norm or args.do_info, 'Specify at least one do_* option to output.'

    with open(args.nmt_pkl, 'rb') as ff:
        nmt = pickle.load(ff)
    mt = nmt.max_trackers[args.layer]

    if args.idx_begin is None:
        args.idx_begin = 0
    if args.idx_end is None:
        args.idx_end = mt.max_vals.shape[0]

    with WithTimer('Saved %d images per unit for %s units %d:%d.' %
                   (args.N, args.layer, args.idx_begin, args.idx_end)):
        output_max_patches(
            mt, net, args.layer, args.idx_begin, args.idx_end, args.N,
            args.datadir, args.filelist, args.outdir,
            (args.do_maxes, args.do_deconv, args.do_deconv_norm,
             args.do_backprop, args.do_backprop_norm, args.do_info))
def main():
    parser = argparse.ArgumentParser(description='Loads a pickled NetMaxTracker and outputs one or more of {the patches of the image, a deconv patch, a backprop patch} associated with the maxes.')
    parser.add_argument('--N',           type=int, default=9, help='Note and save top N activations.')
    parser.add_argument('--gpu-id',      type=int, default=0, help='GPU id. Set -1 to use CPU')
    parser.add_argument('--do-maxes',    action = 'store_true', help = 'Output max patches.')
    parser.add_argument('--do-deconv',   action = 'store_true', help = 'Output deconv patches.')
    parser.add_argument('--do-deconv-norm', action = 'store_true', help = 'Output deconv-norm patches.')
    parser.add_argument('--do-backprop', action = 'store_true', help = 'Output backprop patches.')
    parser.add_argument('--do-backprop-norm', action = 'store_true', help = 'Output backprop-norm patches.')
    parser.add_argument('--do-info',     action = 'store_true', help = 'Output info file containing max filenames and labels.')
    parser.add_argument('--idx-begin',   type = int, default = None, help = 'Start at this unit (default: all units).')
    parser.add_argument('--idx-end',     type = int, default = None, help = 'End at this unit (default: all units).')
    
    parser.add_argument('nmt_pkl',       type = str, help = 'Which pickled NetMaxTracker to load.')
    parser.add_argument('net_prototxt',  type = str, help = 'Network prototxt to load')
    parser.add_argument('net_weights',   type = str, help = 'Network weights to load')
    parser.add_argument('--mean-path', type=str, default=None, help='path to mean file')
    parser.add_argument('--images-dir', type=str, default=None,
                        help='directory to look for files in. Used only together with --filelist-path')
    parser.add_argument('--output-dir',        type = str, help = r'Which output directory to use. Files are output into outdir/layer/unit_%%04d/{maxes,deconv,backprop}_%%03d.png')
    parser.add_argument('--layer',         type = str, help = 'Which layer to output')
    group = parser.add_mutually_exclusive_group()
    group.add_argument('--images-csv-path', type=str, default=None,
                       help='path to csv file containing image pathes and labels')
    group.add_argument('--filelist-path', type=str,
                       help='List of image files to consider, one per line. Must be the same filelist used to produce the NetMaxTracker!')
    args = parser.parse_args()

    print 'Caffe file path:', caffe.__file__

    if args.gpu_id >= 0:
        print 'Using GPU: {}'.format(args.gpu_id)
        caffe.set_device(args.gpu_id)
        caffe.set_mode_gpu()
    else:
        caffe.set_mode_cpu()

    if args.mean_path is not None:
        print 'Loading RGB HWC mean and convert it to BRG CWH from', args.mean_path
        mean = load_rgb_hwc_mean_and_convert(args.mean_path)
    else:
        print 'Loading Imagenet mean'
        mean = load_imagenet_mean()
    net = caffe.Classifier(args.net_prototxt, args.net_weights,
                           mean=mean,
                           channel_swap=(2, 1, 0),
                           raw_scale=255,
                           image_dims=(256, 256))

    assert args.do_maxes or args.do_deconv or args.do_deconv_norm or args.do_backprop or args.do_backprop_norm or args.do_info, 'Specify at least one do_* option to output.'

    with open(args.nmt_pkl, 'rb') as ff:
        nmt = pkl.load(ff)
    mt = nmt.max_trackers[args.layer]

    if args.idx_begin is None:
        args.idx_begin = 0
    if args.idx_end is None:
        args.idx_end = mt.max_vals.shape[0]

    images_df = None
    if args.images_csv_path is not None:
        images_df = pd.read_csv(args.images_csv_path, index_col='index')

    with WithTimer('Saved %d images per unit for %s units %d:%d.' % (args.N, args.layer, args.idx_begin, args.idx_end)):
        output_max_patches(mt, net, args.layer, args.idx_begin, args.idx_end,
                           args.N, args.output_dir,
                           (args.do_maxes, args.do_deconv, args.do_deconv_norm,
                            args.do_backprop, args.do_backprop_norm, args.do_info),
                           filelist_path=args.filelist_path,
                           images_dir=args.images_dir,
                           images_df=images_df)
def main():
    parser = argparse.ArgumentParser(
        description=
        'Loads a pickled NetMaxTracker and outputs one or more of {the patches of the image, a deconv patch, a backprop patch} associated with the maxes.'
    )
    parser.add_argument('--N',
                        type=int,
                        default=9,
                        help='Note and save top N activations.')
    parser.add_argument('--gpu-id',
                        type=int,
                        default=0,
                        help='GPU id. Set -1 to use CPU')
    parser.add_argument('--do-maxes',
                        action='store_true',
                        help='Output max patches.')
    parser.add_argument('--do-deconv',
                        action='store_true',
                        help='Output deconv patches.')
    parser.add_argument('--do-deconv-norm',
                        action='store_true',
                        help='Output deconv-norm patches.')
    parser.add_argument('--do-backprop',
                        action='store_true',
                        help='Output backprop patches.')
    parser.add_argument('--do-backprop-norm',
                        action='store_true',
                        help='Output backprop-norm patches.')
    parser.add_argument(
        '--do-info',
        action='store_true',
        help='Output info file containing max filenames and labels.')
    parser.add_argument('--idx-begin',
                        type=int,
                        default=None,
                        help='Start at this unit (default: all units).')
    parser.add_argument('--idx-end',
                        type=int,
                        default=None,
                        help='End at this unit (default: all units).')

    parser.add_argument('nmt_pkl',
                        type=str,
                        help='Which pickled NetMaxTracker to load.')
    parser.add_argument('net_prototxt',
                        type=str,
                        help='Network prototxt to load')
    parser.add_argument('net_weights',
                        type=str,
                        help='Network weights to load')
    parser.add_argument('--mean-path',
                        type=str,
                        default=None,
                        help='path to mean file')
    parser.add_argument(
        '--images-dir',
        type=str,
        default=None,
        help=
        'directory to look for files in. Used only together with --filelist-path'
    )
    parser.add_argument(
        '--output-dir',
        type=str,
        help=
        r'Which output directory to use. Files are output into outdir/layer/unit_%%04d/{maxes,deconv,backprop}_%%03d.png'
    )
    parser.add_argument('--layer', type=str, help='Which layer to output')
    group = parser.add_mutually_exclusive_group()
    group.add_argument(
        '--images-csv-path',
        type=str,
        default=None,
        help='path to csv file containing image pathes and labels')
    group.add_argument(
        '--filelist-path',
        type=str,
        help=
        'List of image files to consider, one per line. Must be the same filelist used to produce the NetMaxTracker!'
    )
    args = parser.parse_args()

    print 'Caffe file path:', caffe.__file__

    if args.gpu_id >= 0:
        print 'Using GPU: {}'.format(args.gpu_id)
        caffe.set_device(args.gpu_id)
        caffe.set_mode_gpu()
    else:
        caffe.set_mode_cpu()

    if args.mean_path is not None:
        print 'Loading RGB HWC mean and convert it to BRG CWH from', args.mean_path
        mean = load_rgb_hwc_mean_and_convert(args.mean_path)
    else:
        print 'Loading Imagenet mean'
        mean = load_imagenet_mean()
    net = caffe.Classifier(args.net_prototxt,
                           args.net_weights,
                           mean=mean,
                           channel_swap=(2, 1, 0),
                           raw_scale=255,
                           image_dims=(256, 256))

    assert args.do_maxes or args.do_deconv or args.do_deconv_norm or args.do_backprop or args.do_backprop_norm or args.do_info, 'Specify at least one do_* option to output.'

    with open(args.nmt_pkl, 'rb') as ff:
        nmt = pkl.load(ff)
    mt = nmt.max_trackers[args.layer]

    if args.idx_begin is None:
        args.idx_begin = 0
    if args.idx_end is None:
        args.idx_end = mt.max_vals.shape[0]

    images_df = None
    if args.images_csv_path is not None:
        images_df = pd.read_csv(args.images_csv_path, index_col='index')

    with WithTimer('Saved %d images per unit for %s units %d:%d.' %
                   (args.N, args.layer, args.idx_begin, args.idx_end)):
        output_max_patches(
            mt,
            net,
            args.layer,
            args.idx_begin,
            args.idx_end,
            args.N,
            args.output_dir,
            (args.do_maxes, args.do_deconv, args.do_deconv_norm,
             args.do_backprop, args.do_backprop_norm, args.do_info),
            filelist_path=args.filelist_path,
            images_dir=args.images_dir,
            images_df=images_df)