コード例 #1
0
 def load(self, session, model_path):
     x_input = tf.placeholder(tf.float32, shape=(None, ) + self.x_shape)
     x_input = tf.transpose(x_input, [0, 3, 1, 2])
     with TowerContext('', is_training=False):
         with tf.variable_scope('', reuse=tf.AUTO_REUSE):
             _ = self.model.get_logits(x_input)
     get_model_loader(model_path).init(session)
コード例 #2
0
ファイル: train.py プロジェクト: haelkemary/xy_net
    def run(self, nr_gpus):
        """
        Do the 2 phase training, both phases have the same nr_epochs and lr_sched
        Phase 1: finetune decoder portion (together with the first 7x7 in the encoder),
                 using the pretrained preact-resnet50 on ImageNet 
        Phase 2: unfreeze all layer, training whole, weigths taken from last epoch of
                 Phase 1
        """
        def get_last_chkpt_path(phase1_dir):
            stat_file_path = phase1_dir + '/stats.json'
            with open(stat_file_path) as stat_file:
                info = json.load(stat_file)
            chkpt_list = [epoch_stat['global_step'] for epoch_stat in info]
            last_chkpts_path = "%smodel-%d.index" % (phase1_dir,
                                                     max(chkpt_list))
            return last_chkpts_path

        save_dir = self.save_dir + '/base/'
        self.train_batch_size = self.train_phase1_batch_size
        init_weights = get_model_loader(self.pretrained_preact_resnet50_path)
        self.run_once(nr_gpus, True, sess_init=init_weights, save_dir=save_dir)

        save_dir = self.save_dir + '/tune/'
        self.train_batch_size = self.train_phase2_batch_size
        phase1_last_model_path = get_last_chkpt_path(self.save_dir + '/base/')
        init_weights = SaverRestore(phase1_last_model_path,
                                    ignore=['learning_rate'])
        self.run_once(nr_gpus,
                      False,
                      sess_init=init_weights,
                      save_dir=save_dir)
        return
コード例 #3
0
def run(model):
    instance = Model(model, model.conf.data_format)
    if not model.conf.is_train:
        batch = 64
        dataset = get_data(model.conf.data_dir, 'val', batch)
        eval_on_ILSVRC12(
            instance,
            get_model_loader(model.conf.logdir + '/' + model.conf.test_step),
            dataset)
    else:
        logger.set_logger_dir(os.path.join(model.conf.logdir))
        config = get_config(instance, model.conf)
        if model.conf.reload_step:
            config.session_init = get_model_loader(model.conf.logdir + '/' +
                                                   model.conf.reload_step)
        trainer = SyncMultiGPUTrainerParameterServer(max(get_nr_gpu(), 1))
        launch_train_with_config(config, trainer)
コード例 #4
0
ファイル: predict.py プロジェクト: zhaorui8/tensorpack
def do_visualize(model, model_path, nr_visualize=100, output_dir='output'):
    """
    Visualize some intermediate results (proposals, raw predictions) inside the pipeline.
    """
    df = get_train_dataflow()
    df.reset_state()

    pred = OfflinePredictor(
        PredictConfig(model=model,
                      session_init=get_model_loader(model_path),
                      input_names=['image', 'gt_boxes', 'gt_labels'],
                      output_names=[
                          'generate_{}_proposals/boxes'.format(
                              'fpn' if cfg.MODE_FPN else 'rpn'),
                          'generate_{}_proposals/scores'.format(
                              'fpn' if cfg.MODE_FPN else 'rpn'),
                          'fastrcnn_all_scores',
                          'output/boxes',
                          'output/scores',
                          'output/labels',
                      ]))

    if os.path.isdir(output_dir):
        shutil.rmtree(output_dir)
    fs.mkdir_p(output_dir)
    with tqdm.tqdm(total=nr_visualize) as pbar:
        for idx, dp in itertools.islice(enumerate(df), nr_visualize):
            img, gt_boxes, gt_labels = dp['image'], dp['gt_boxes'], dp[
                'gt_labels']

            rpn_boxes, rpn_scores, all_scores, \
                final_boxes, final_scores, final_labels = pred(img, gt_boxes, gt_labels)

            # draw groundtruth boxes
            gt_viz = draw_annotation(img, gt_boxes, gt_labels)
            # draw best proposals for each groundtruth, to show recall
            proposal_viz, good_proposals_ind = draw_proposal_recall(
                img, rpn_boxes, rpn_scores, gt_boxes)
            # draw the scores for the above proposals
            score_viz = draw_predictions(img, rpn_boxes[good_proposals_ind],
                                         all_scores[good_proposals_ind])

            results = [
                DetectionResult(*args)
                for args in zip(final_boxes, final_scores, final_labels,
                                [None] * len(final_labels))
            ]
            final_viz = draw_final_outputs(img, results)

            viz = tpviz.stack_patches(
                [gt_viz, proposal_viz, score_viz, final_viz], 2, 2)

            if os.environ.get('DISPLAY', None):
                tpviz.interactive_imshow(viz)
            cv2.imwrite("{}/{:03d}.png".format(output_dir, idx), viz)
            pbar.update()
コード例 #5
0
    def run(self):
        def get_last_chkpt_path(prev_phase_dir):
            stat_file_path = os.path.join(prev_phase_dir, "stats.json")
            with open(stat_file_path) as stat_file:
                info = json.load(stat_file)
            chkpt_list = [epoch_stat["global_step"] for epoch_stat in info]
            last_chkpts_path = os.path.join(
                prev_phase_dir, "model-{}.index".format(max(chkpt_list))
            )
            return last_chkpts_path

        phase_opts = self.training_phase

        if len(phase_opts) > 1:
            for idx, opt in enumerate(phase_opts):
                random.seed(self.seed)
                np.random.seed(self.seed)
                tf.random.set_random_seed(self.seed)

                log_dir = os.path.join(self.save_dir, str(idx))
                pretrained_path = opt["pretrained_path"]
                if pretrained_path == -1:
                    pretrained_path = get_last_chkpt_path(prev_log_dir)
                    init_weights = SaverRestore(
                        pretrained_path, ignore=["learning_rate"]
                    )
                elif pretrained_path is not None:
                    init_weights = get_model_loader(pretrained_path)
                self.run_once(opt, sess_init=init_weights, save_dir=log_dir)
                prev_log_dir = log_dir
        else:
            random.seed(self.seed)
            np.random.seed(self.seed)
            tf.random.set_random_seed(self.seed)

            opt = phase_opts[0]
            init_weights = None
            if "pretrained_path" in opt:
                assert opt["pretrained_path"] != -1
                init_weights = get_model_loader(opt["pretrained_path"])
            self.run_once(opt, sess_init=init_weights, save_dir=self.save_dir)

        return
コード例 #6
0
ファイル: train.py プロジェクト: simongraham/dsf-cnn
    def run(self):
        def get_last_chkpt_path(prev_phase_dir):
            stat_file_path = prev_phase_dir + "/stats.json"
            with open(stat_file_path) as stat_file:
                info = json.load(stat_file)
            chkpt_list = [epoch_stat["global_step"] for epoch_stat in info]
            last_chkpts_path = "%smodel-%d.index" % (prev_phase_dir,
                                                     max(chkpt_list))
            return last_chkpts_path

        phase_opts = self.training_phase

        if len(phase_opts) > 1:
            for idx, opt in enumerate(phase_opts):

                log_dir = "%s/%02d" % (self.save_dir, idx)
                if opt["pretrained_path"] == -1:
                    pretrained_path = get_last_chkpt_path(prev_log_dir)
                    init_weights = SaverRestore(pretrained_path,
                                                ignore=["learning_rate"])
                elif opt["pretrained_path"] is not None:
                    init_weights = get_model_loader(pretrained_path)
                self.run_once(opt,
                              sess_init=init_weights,
                              save_dir=log_dir + "/")
                prev_log_dir = log_dir
        else:

            opt = phase_opts[0]
            if "pretrained_path" in opt:
                if opt["pretrained_path"] == None:
                    init_weights = None
                elif opt["pretrained_path"] == -1:
                    log_dir_prev = "%s" % self.save_dir
                    pretrained_path = get_last_chkpt_path(log_dir_prev)
                    init_weights = SaverRestore(pretrained_path,
                                                ignore=["learning_rate"])
                else:
                    init_weights = get_model_loader(opt["pretrained_path"])
            self.run_once(opt, sess_init=init_weights, save_dir=self.save_dir)

        return
コード例 #7
0
    def load(self, **kwargs):
        session = kwargs["session"]
        assert isinstance(session, tf.Session)

        x_input = tf.placeholder(tf.float32, shape=(None, ) + self.x_shape)
        x_input = tf.transpose(x_input, [0, 3, 1, 2])
        with TowerContext('', is_training=False):
            with tf.variable_scope('', reuse=tf.AUTO_REUSE):
                logits = self.model.get_logits(x_input)

        model_path = get_model_path('R152-Denoise.npz')
        url = 'https://github.com/facebookresearch/ImageNet-Adversarial-Training/releases/download/v0.1/R152-Denoise.npz'
        if not os.path.exists(model_path):
            if not os.path.exists(os.path.dirname(model_path)):
                os.makedirs(os.path.dirname(model_path))

            from six.moves import urllib
            urllib.request.urlretrieve(url, model_path, show_progress)

        get_model_loader(model_path).init(session)
コード例 #8
0
ファイル: train.py プロジェクト: Ghulam111/DRCA_Net
    def run(self):
        def get_last_chkpt_path(prev_phase_dir):
            stat_file_path = prev_phase_dir + '/stats.json'
            with open(stat_file_path) as stat_file:
                info = json.load(stat_file)
            chkpt_list = [epoch_stat['global_step'] for epoch_stat in info]
            last_chkpts_path = "%smodel-%d.index" % (prev_phase_dir,
                                                     max(chkpt_list))
            return last_chkpts_path

        phase_opts = self.training_phase

        if len(phase_opts) > 1:
            for idx, opt in enumerate(phase_opts):
                random.seed(self.seed)
                np.random.seed(self.seed)
                tf.random.set_random_seed(self.seed)

                log_dir = '%s/%02d/' % (self.save_dir, idx)
                pretrained_path = opt['pretrained_path']
                if pretrained_path == -1:
                    pretrained_path = get_last_chkpt_path(prev_log_dir)
                    init_weights = SaverRestore(pretrained_path,
                                                ignore=['learning_rate'])
                elif pretrained_path is not None:
                    init_weights = get_model_loader(pretrained_path)
                self.run_once(opt, sess_init=init_weights, save_dir=log_dir)
                prev_log_dir = log_dir
        else:
            random.seed(self.seed)
            np.random.seed(self.seed)
            tf.random.set_random_seed(self.seed)

            opt = phase_opts[0]
            init_weights = None
            if 'pretrained_path' in opt:
                assert opt['pretrained_path'] != -1
                init_weights = get_model_loader(opt['pretrained_path'])
            self.run_once(opt, sess_init=init_weights, save_dir=self.save_dir)

        return
コード例 #9
0
def main(_):
    # Images for inception classifier are normalized to be in [-1, 1] interval,
    # eps is a difference between pixels so it should be in [0, 2] interval.
    # Renormalizing epsilon from [0, 255] to [0, 2].
    full_start = timer()
    eps = 2.0 * FLAGS.max_epsilon / 255.0

    tf.logging.set_verbosity(tf.logging.INFO)

    with tf.Graph().as_default():
        # Prepare graph
        x_raw = tf.placeholder(tf.float32, shape=batch_shape)
        x_max = tf.clip_by_value(x_raw + eps, -1.0, 1.0)
        x_min = tf.clip_by_value(x_raw - eps, -1.0, 1.0)
        x_input = tf.placeholder(tf.float32, shape=batch_shape)
        target_class_input = tf.placeholder(tf.int32, shape=[FLAGS.batch_size])
        raw_class_input = tf.placeholder(tf.int32, shape=[FLAGS.batch_size])
        i = tf.constant(0)
        grad = tf.zeros(shape=batch_shape)
        adv_images = np.zeros(shape=batch_shape)

        x_adv1, _, _, _, _, _, _, = tf.while_loop(stop, graph, [
            x_input, target_class_input, raw_class_input, i, x_max, x_min, grad
        ])

        print('Created Graph')

        # Run computation
        with tf.Session() as sess:
            processed = 0.0
            s1 = tf.train.Saver(slim.get_model_variables(scope='resnet_v2_50'))
            s1.restore(sess, FLAGS.checkpoint_path_resnet_v2)
            get_model_loader(args1.load).init(sess)
            #如果有allow_pickle的错误,请修改tensorpack的库,使得里面dict(np.load(path,allow_pickle=True))
            get_model_loader(args2.load).init(sess)
            get_model_loader(args3.load).init(sess)

            print('Initialized Models')

            for filenames, image_mix, image_raw in load_images(
                    FLAGS.input_dir, batch_shape):
                target_class_for_batch = (
                    [all_images_taget_class[n] for n in filenames] + [0] *
                    (FLAGS.batch_size - len(filenames)))
                raw_class_for_batch = (
                    [all_images_raw_class[n] for n in filenames] + [0] *
                    (FLAGS.batch_size - len(filenames)))
                adv_images = sess.run(x_adv1,
                                      feed_dict={
                                          x_input: image_mix,
                                          target_class_input:
                                          target_class_for_batch,
                                          x_raw: image_raw,
                                          raw_class_input: raw_class_for_batch
                                      })
                save_images(adv_images, filenames, FLAGS.output_dir)
                processed += FLAGS.batch_size
            full_end = timer()
            print("DONE: Processed {} images in {} sec".format(
                processed, full_end - full_start))
コード例 #10
0
def prepare_model(model_name, classes, use_pretrained,
                  pretrained_model_file_path):
    kwargs = {'pretrained': use_pretrained, 'classes': classes}

    net_lambda, net_file_path = get_model(model_name, **kwargs)
    net = ImageNetModel(model_lambda=net_lambda)

    if use_pretrained and not pretrained_model_file_path:
        pretrained_model_file_path = net_file_path

    inputs_desc = None
    if pretrained_model_file_path:
        assert (os.path.isfile(pretrained_model_file_path))
        logging.info('Loading model: {}'.format(pretrained_model_file_path))
        inputs_desc = get_model_loader(pretrained_model_file_path)

    return net, inputs_desc
コード例 #11
0
ファイル: export_model.py プロジェクト: thanhtcptit/vqvae
def export(params, model_path, export_type, output_dir):
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    model_params = params['model']
    model = BaseVQVAE.from_params(model_params)

    pred_config = PredictConfig(
        session_init=get_model_loader(model_path),
        model=model,
        input_names=['input'],
        output_names=['x_recon', 'embeddings', 'latent_zq'])
    if export_type == 'compact':
        checkpoint_name = os.path.split(model_path)[1]
        ModelExporter(pred_config).export_compact(
            os.path.join(output_dir, 'model.pb'))
    else:
        ModelExporter(pred_config).export_serving(
            os.path.join(output_dir, 'exported'))
コード例 #12
0
ファイル: utils_tp.py プロジェクト: yyfyan/imgclsmob
def prepare_model(model_name, use_pretrained, pretrained_model_file_path):
    kwargs = {'pretrained': use_pretrained}

    raw_net = get_model(model_name, **kwargs)
    input_image_size = raw_net.in_size[0] if hasattr(raw_net,
                                                     'in_size') else 224

    net = ImageNetModel(model_lambda=raw_net, image_size=input_image_size)

    if use_pretrained and not pretrained_model_file_path:
        pretrained_model_file_path = raw_net.file_path

    inputs_desc = None
    if pretrained_model_file_path:
        assert (os.path.isfile(pretrained_model_file_path))
        logging.info('Loading model: {}'.format(pretrained_model_file_path))
        inputs_desc = get_model_loader(pretrained_model_file_path)

    return net, inputs_desc
コード例 #13
0
def create():
    # fetch weights
    weights_path = zoo.fetch_weights(
        'https://github.com/facebookresearch/ImageNet-Adversarial-Training/releases/download/v0.1/R152-Denoise.npz',
        unzip=False)

    # model constructer expects an ArgumentParser as argument
    parser = argparse.ArgumentParser()
    parser.add_argument('-d',
                        '--depth',
                        help='ResNet depth',
                        type=int,
                        default=152,
                        choices=[50, 101, 152])
    parser.add_argument('--arch',
                        help='Name of architectures defined in nets.py',
                        default='ResNetDenoise')
    args = parser.parse_args([])

    model = getattr(nets, args.arch + 'Model')(args)

    image = tf.placeholder(tf.float32, shape=(None, 3, 224, 224))

    with TowerContext(tower_name='', is_training=False):
        logits = model.get_logits(image)

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    session = tf.Session(config=config)
    with session.as_default():
        model = get_model_loader(weights_path).init(session)

        fmodel = TensorFlowModel(image,
                                 logits,
                                 channel_axis=1,
                                 bounds=[0, 255.],
                                 preprocessing=(127.5, 127.5))

    return fmodel
コード例 #14
0
def prepare_model(model_name,
                  use_pretrained,
                  pretrained_model_file_path,
                  data_format="channels_last"):
    kwargs = {"pretrained": use_pretrained}

    raw_net = get_model(name=model_name, data_format=data_format, **kwargs)
    input_image_size = raw_net.in_size[0] if hasattr(raw_net,
                                                     "in_size") else 224

    net = ImageNetModel(model_lambda=raw_net,
                        image_size=input_image_size,
                        data_format=data_format)

    if use_pretrained and not pretrained_model_file_path:
        pretrained_model_file_path = raw_net.file_path

    inputs_desc = None
    if pretrained_model_file_path:
        assert (os.path.isfile(pretrained_model_file_path))
        logging.info("Loading model: {}".format(pretrained_model_file_path))
        inputs_desc = get_model_loader(pretrained_model_file_path)

    return net, inputs_desc
コード例 #15
0
ファイル: config.py プロジェクト: uit-hdl/hovernet-pipeline
    def gen_pred_config(self):
        if self.inf_auto_find_chkpt:
            self.inf_model_path = os.path.join(
                self.save_dir,
                str(
                    max([
                        int(x) for x in [
                            name for name in os.listdir(self.save_dir)
                            if os.path.isdir(os.path.join(self.save_dir, name))
                        ]
                    ])),
            )
            print(f"Inference model path: <{self.inf_model_path}>")
            print(
                '-----Auto Selecting Checkpoint Basing On "%s" Through "%s" Comparison'
                % (self.inf_auto_metric, self.inf_auto_comparator))
            model_path, stat = get_best_chkpts(self.inf_model_path,
                                               self.inf_auto_metric,
                                               self.inf_auto_comparator)
            print("Selecting: %s" % model_path)
            print("Having Following Statistics:")
            for key, value in stat.items():
                print("\t%s: %s" % (key, value))
            sess = get_model_loader(model_path)
        else:
            model_path = self.inf_model_path
            sess = SaverRestoreRelaxed(self.inf_model_path)

        model_constructor = self.get_model()
        pred_config = PredictConfig(
            model=model_constructor(),
            session_init=sess,
            input_names=self.eval_inf_input_tensor_names,
            output_names=self.eval_inf_output_tensor_names,
        )
        return pred_config
コード例 #16
0
with tf.Graph().as_default() as G:
    if args.config:
        logger.warn(
            "Using a config script is not reliable. Please use metagraph.")
        MODEL = imp.load_source('config_script', args.config).Model
        M = MODEL()
        with TowerContext('', is_training=False):
            input = PlaceholderInput()
            input.setup(M.get_inputs_desc())
            M.build_graph(input)
    else:
        tf.train.import_meta_graph(args.meta)

    # loading...
    init = get_model_loader(args.model)
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
    init.init(sess)

    # dump ...
    with sess.as_default():
        if args.output.endswith('npy') or args.output.endswith('npz'):
            varmanip.dump_session_params(args.output)
        else:
            var = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
            var.extend(tf.get_collection(tf.GraphKeys.MODEL_VARIABLES))
            gvars = set([k.name for k in tf.global_variables()])
            var = [v for v in var if v.name in gvars]
            var_dict = {}
コード例 #17
0
    """
    Sec 2.3: We keep the per-worker sample size n constant when we change the number of workers k.
    In this work, we use n = 32 which has performed well for a wide range of datasets and networks.
    """
    parser.add_argument('--batch', help='per-GPU batch size', default=32, type=int)
    args = parser.parse_args()

    model = Model(args.depth, args.norm, args.use_ws)
    model.accum_grad = args.accum_grad
    if args.weight_decay_norm:
        model.weight_decay_pattern = ".*/W|.*/gamma|.*/beta"

    if args.eval:
        batch = 128    # something that can run on one gpu
        ds = get_val_dataflow(args.data, batch, fbresnet_augmentor(False))
        eval_classification(model, get_model_loader(args.load), ds)
        sys.exit()

    logger.info("Training on {}".format(socket.gethostname()))
    # Print some information for sanity check.
    os.system("nvidia-smi")
    assert args.load is None

    hvd.init()

    if args.logdir is None:
        args.logdir = os.path.join('train_log', 'Horovod-{}GPUs-{}Batch'.format(hvd.size(), args.batch))

    if hvd.rank() == 0:
        logger.set_logger_dir(args.logdir, 'd')
    logger.info("Rank={}, Local Rank={}, Size={}".format(hvd.rank(), hvd.local_rank(), hvd.size()))
コード例 #18
0
from tensorpack import logger
from tensorpack.tfutils import varmanip, get_model_loader

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Keep only TRAINABLE and MODEL variables in a checkpoint.')
    parser.add_argument('--meta', help='metagraph file', required=True)
    parser.add_argument(dest='input', help='input model file, has to be a TF checkpoint')
    parser.add_argument(dest='output', help='output model file, can be npz or TF checkpoint')
    args = parser.parse_args()

    tf.train.import_meta_graph(args.meta, clear_devices=True)

    # loading...
    init = get_model_loader(args.input)
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
    init.init(sess)

    # dump ...
    with sess.as_default():
        if args.output.endswith('npy') or args.output.endswith('npz'):
            varmanip.dump_session_params(args.output)
        else:
            var = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
            var.extend(tf.get_collection(tf.GraphKeys.MODEL_VARIABLES))
            gvars = set([k.name for k in tf.global_variables()])
            var = [v for v in var if v.name in gvars]
            var_dict = {}
コード例 #19
0
ファイル: shufflenet.py プロジェクト: quanlzheng/tensorpack
    parser.add_argument('--eval', action='store_true')
    parser.add_argument('--flops', action='store_true', help='print flops and exit')
    args = parser.parse_args()

    if args.gpu:
        os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu

    if args.v2 and args.group != parser.get_default('group'):
        logger.error("group= is not used in ShuffleNetV2!")

    model = Model()

    if args.eval:
        batch = 128    # something that can run on one gpu
        ds = get_data('val', batch)
        eval_on_ILSVRC12(model, get_model_loader(args.load), ds)
    elif args.flops:
        # manually build the graph with batch=1
        input_desc = [
            InputDesc(tf.float32, [1, 224, 224, 3], 'input'),
            InputDesc(tf.int32, [1], 'label')
        ]
        input = PlaceholderInput()
        input.setup(input_desc)
        with TowerContext('', is_training=False):
            model.build_graph(*input.get_input_tensors())
        model_utils.describe_trainable_vars()

        tf.profiler.profile(
            tf.get_default_graph(),
            cmd='op',
コード例 #20
0
    # CAM_viz or eval or train
    if args.CAM_viz != None:
        batch = 128

        # @ 20171125
        if args.CAM_viz == 'train':
            assert args.crop_method_TR not in ['CAMCrop', 'CAMCropR'], '*** CAMCrop or CAMCropR IS NOT ALLOWED IN CAM_viz ***'

        # @ 20171122 : visualize the CAM for better understanding of the CNN
        #              introduce strict_order to make it convenient for image saving process
        # @ 20171124 : introduce remainder_TR to extract all the CAM_viz of images in the training set
        ds = get_data(args.CAM_viz, args.data, batch, args.crop_method_TS if args.CAM_viz == 'val' else args.crop_method_TR, \
                      repeat_times = 1, strict_order = True, remainder_TR = True if args.CAM_viz == 'train' else False)

        viz_CAM(model, get_model_loader(args.load), args.CAM_viz, ds, args.CAM_dir,
            save_PKL = args.CAM_viz_PKL, save_REP = args.CAM_viz_REP)

    elif args.eval:
        batch = 128 # something that can run on one gpu

        # @ 20171121 to make it convenient to calculate average estimation
        batch = int(batch // args.repeat_times * args.repeat_times)

        ds = get_data('val', args.data, batch, args.crop_method_TS, repeat_times = args.repeat_times, strict_order = True)

        eval_on_AVA2012(model, get_model_loader(args.load), ds, args.repeat_times)

    else:
        # add @ 20171128: the strategy of parameter initalization with a ImageNet pre-trained model
        #                 should be recorded within the name string of the directory of training log
コード例 #21
0
def main(_):
  # Images for inception classifier are normalized to be in [-1, 1] interval,
  # eps is a difference between pixels so it should be in [0, 2] interval.
  # Renormalizing epsilon from [0, 255] to [0, 2].
  full_start = timer()
  eps = 2.0 * FLAGS.max_epsilon / 255.0

  batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]

  tf.logging.set_verbosity(tf.logging.INFO)

  all_images_taget_class, all_images_true_label = load_target_class(FLAGS.input_dir)

  with tf.Graph().as_default():
    # Prepare graph
    x_input = tf.placeholder(tf.float32, shape=batch_shape)
    x_max = tf.clip_by_value(x_input + eps, -1.0, 1.0)
    x_min = tf.clip_by_value(x_input - eps, -1.0, 1.0)

    target_class_input = tf.placeholder(tf.int32, shape=[FLAGS.batch_size])
    true_label_input = tf.placeholder(tf.int32, shape=[FLAGS.batch_size])
    i = tf.constant(0)
    grad = tf.zeros(shape=batch_shape)

    x_adv, _, _, _, _, _, _ = tf.while_loop(stop, graph, [x_input, target_class_input,true_label_input, i, x_max, x_min, grad])
    s1 = tf.train.Saver(slim.get_model_variables(scope='AdvInceptionResnetV2'))
    s2 = tf.train.Saver(slim.get_model_variables(scope='Ens3AdvInceptionV3'))
    s3 = tf.train.Saver(slim.get_model_variables(scope='Ens4AdvInceptionV3'))
    s4 = tf.train.Saver(slim.get_model_variables(scope='EnsAdvInceptionResnetV2'))
    s5 = tf.train.Saver(slim.get_model_variables(scope='AdvInceptionV3'))
    s6 = tf.train.Saver(slim.get_model_variables(scope='InceptionV3'))
    s7 = tf.train.Saver(slim.get_model_variables(scope='resnet_v2_101'))
    s8 = tf.train.Saver(slim.get_model_variables(scope='resnet_v2_50'))
    #s4 = tf.train.Saver(slim.get_model_variables(scope='EnsAdvInceptionResnetV2'))
    model = resnext.ResNeXtDenoiseAllModel()
    print('Created Graph')
    
    #byheziwen
    '''x_input1 = tf.placeholder(tf.float32, shape=batch_shape)
    x_input2 = tf.placeholder(tf.float32, shape=batch_shape)
    x_all_16 = all_16(x_input1, x_input2)'''
    
    # Run computation
    with tf.Session() as sess:
      processed = 0.0
      s1.restore(sess, FLAGS.checkpoint_path_adv_inception_resnet_v2)
      s2.restore(sess, FLAGS.checkpoint_path_ens3_adv_inception_v3)
      s3.restore(sess, FLAGS.checkpoint_path_ens4_adv_inception_v3)
      s4.restore(sess, FLAGS.checkpoint_path_ens_adv_inception_resnet_v2)
      s5.restore(sess, FLAGS.checkpoint_path_adv_inception_v3)
      s6.restore(sess, FLAGS.checkpoint_path_inception_v3)
      s7.restore(sess, FLAGS.checkpoint_path_resnet_v2_101)
      s8.restore(sess, FLAGS.checkpoint_path_resnet_v2_50)     
      #s4.restore(sess, FLAGS.checkpoint_path_ens_adv_inception_resnet_v2)
      model = get_model_loader(FLAGS.checkpoint_path_resnext_101).init(sess)
      print('Initialized Models')

      for filenames, images in load_images(FLAGS.input_dir, batch_shape):
        target_class_for_batch = (
            [all_images_taget_class[n] for n in filenames]
            + [0] * (FLAGS.batch_size - len(filenames)))
        true_label_for_batch = (
            [all_images_true_label[n] for n in filenames]
            + [0] * (FLAGS.batch_size - len(filenames)))
        adv_images = sess.run(x_adv, feed_dict={x_input: images, target_class_input: target_class_for_batch, true_label_input:true_label_for_batch})
        #by heziwen
        #adv_images = sess.run(x_all_16, feed_dict={x_input1:adv_images, x_input2:images})
        
        save_images(adv_images, filenames, FLAGS.output_dir)
        processed += FLAGS.batch_size
      full_end = timer()
      print("DONE: Processed {} images in {} sec".format(processed, full_end - full_start))
コード例 #22
0
ファイル: dump-model-params.py プロジェクト: zfxu/tensorpack
from tensorpack import logger
from tensorpack.tfutils import varmanip, get_model_loader

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Keep only TRAINABLE and MODEL variables in a checkpoint.')
    parser.add_argument('--meta', help='metagraph file', required=True)
    parser.add_argument(dest='input', help='input model file, has to be a TF checkpoint')
    parser.add_argument(dest='output', help='output model file, can be npz or TF checkpoint')
    args = parser.parse_args()

    tf.train.import_meta_graph(args.meta)

    # loading...
    init = get_model_loader(args.input)
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
    init.init(sess)

    # dump ...
    with sess.as_default():
        if args.output.endswith('npy') or args.output.endswith('npz'):
            varmanip.dump_session_params(args.output)
        else:
            var = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
            var.extend(tf.get_collection(tf.GraphKeys.MODEL_VARIABLES))
            gvars = set([k.name for k in tf.global_variables()])
            var = [v for v in var if v.name in gvars]
            var_dict = {}
コード例 #23
0
ファイル: main.py プロジェクト: zyg11/ghostnet
    # create GhostNet
    from ghost_net import GhostNet
    model = GhostNet(width=args.width_ratio, se=args.se, 
                      weight_decay=args.weight_decay,
                      dw_code=dw_code, ratio_code=ratio_code,
                      label_smoothing=args.label_smoothing)
    model.data_format = args.data_format
    print('model created')

    # start evaluation
    if args.eval:
        batch = 256    # something that can run on your gpu
        ds = get_data('val', batch)
        start = time()
        eval_on_ILSVRC12(model, get_model_loader(args.load), ds)
        stop = time()
        print('Evaluation used time: %.2fs.' % (stop-start))
    elif args.flops > 0:
        # manually build the graph with batch=1
        image_shape = 224
        input_desc = [
            InputDesc(tf.float32, [1, image_shape, image_shape, 3], 'input'),
            InputDesc(tf.int32, [1], 'label')
        ]
        input = PlaceholderInput()
        input.setup(input_desc)
        with TowerContext('', is_training=False):
            model.build_graph(*input.get_input_tensors())
        model_utils.describe_trainable_vars()
コード例 #24
0
def eval_retrieval(evalargs):
    # Set up evaluation:
    save_dir = evalargs.save_dir
    mkdir_p(evalargs.save_dir)

    model_configs = get_model_config(evalargs.ModelPath)

    # Set up graph:
    pred_config = PredictConfig(
        model=DH3D(model_configs),
        session_init=get_model_loader(evalargs.ModelPath),
        input_names=['pointclouds'],
        output_names=['globaldesc'],  # ['globaldesc'], output_weights
    )
    predictor = OfflinePredictor(pred_config)

    # Data:
    df, totalbatch = get_eval_global_testdata(model_configs,
                                              evalargs.data_path,
                                              evalargs.ref_gt_file)

    # Predict:
    pcdnum = 0
    for [pcds, names] in df:  # pcds is a list, batchsize x numpts x 3
        batch = pcds.shape[0]
        if totalbatch > batch:
            numpts = pcds.shape[1]
            pcddim = pcds.shape[2]
            padzeros = np.zeros([totalbatch - batch, numpts, pcddim],
                                dtype=np.float32)
            pcds = np.vstack([pcds, padzeros])
        results = predictor(pcds)

        global_feats = results[0]

        for i in range(batch):
            pcdnum += 1
            globaldesc = global_feats[i]
            name = names[i]
            savename = os.path.join(evalargs.save_dir, name)
            basedir = os.path.dirname(savename)
            mkdir_p(basedir)
            globaldesc.tofile(savename)

    print('predicted {} poitnclouds \n'.format(pcdnum))

    # Evaluation recall:
    if evalargs.eval_recall:
        evaluator = GlobalDesc_eval(
            result_savedir='./',
            desc_dir=save_dir,
            database_file=os.path.join(evalargs.data_path,
                                       evalargs.ref_gt_file),
            query_file=os.path.join(evalargs.data_path, evalargs.qry_gt_file),
            max_num_nn=25)
        evaluator.evaluate()
        print("evaluation finished!\n")

    if evalargs.delete_tmp:
        # delete all the descriptors
        descdirs = [os.path.join(save_dir, f) for f in os.listdir(save_dir)]
        descdirs = [d for d in descdirs if os.path.isdir(d)]
        for d in descdirs:
            shutil.rmtree(d)
コード例 #25
0
ファイル: predict.py プロジェクト: zhaorui8/tensorpack
    if not tf.test.is_gpu_available():
        from tensorflow.python.framework import test_util
        assert get_tf_version_tuple() >= (1, 7) and test_util.IsMklEnabled(), \
            "Inference requires either GPU support or MKL support!"
    assert args.load
    finalize_configs(is_training=False)

    if args.predict or args.visualize:
        cfg.TEST.RESULT_SCORE_THRESH = cfg.TEST.RESULT_SCORE_THRESH_VIS

    if args.visualize:
        do_visualize(MODEL, args.load)
    else:
        predcfg = PredictConfig(
            model=MODEL,
            session_init=get_model_loader(args.load),
            input_names=MODEL.get_inference_tensor_names()[0],
            output_names=MODEL.get_inference_tensor_names()[1])

        if args.compact:
            ModelExporter(predcfg).export_compact(args.compact, optimize=False)
        elif args.serving:
            ModelExporter(predcfg).export_serving(args.serving, optimize=False)

        if args.predict:
            predictor = OfflinePredictor(predcfg)
            for image_file in args.predict:
                do_predict(predictor, image_file)
        elif args.evaluate:
            assert args.evaluate.endswith('.json'), args.evaluate
            do_evaluate(predcfg, args.evaluate)
コード例 #26
0
    parser.add_argument('--mode',
                        choices=['resnet', 'preact', 'se'],
                        help='variants of resnet to use',
                        default='resnet')
    args = parser.parse_args()

    if args.gpu:
        os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu

    model = Model(args.depth, args.mode)
    model.data_format = args.data_format

    if args.eval:
        batch = 1  # something that can run on one gpu
        ds = get_data('val', batch)
        eval_on_iNaturalist(model, get_model_loader(args.load), ds)
        print('eval Done!')

    elif args.test:
        batch = 1  # something that can run on one gpu
        ds = get_data('test', batch)
        test_on_iNaturalist(model, get_model_loader(args.load), ds)

    else:
        if args.fake:
            logger.set_logger_dir(os.path.join('train_log', 'tmp'), 'd')
        else:
            logger.set_logger_dir(
                os.path.join(
                    'train_log',
                    'iNaturalist-{}-d{}'.format(args.mode, args.depth)))
コード例 #27
0
    parser.add_argument('--batch', default=256, type=int,
                        help='total batch size. 32 per GPU gives best accuracy, higher values should be similarly good')
    parser.add_argument('--mode', choices=['resnet', 'preact', 'se'],
                        help='variants of resnet to use', default='resnet')
    args = parser.parse_args()

    if args.gpu:
        os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu

    model = Model(args.depth, args.mode)
    model.data_format = args.data_format

    if args.eval:
        batch = 1    # something that can run on one gpu
        ds = get_data('val', batch)
        eval_on_iNaturalist(model, get_model_loader(args.load), ds)
        print('eval Done!')

    elif args.test:
        batch = 1    # something that can run on one gpu
        ds = get_data('test', batch)
        test_on_iNaturalist(model, get_model_loader(args.load), ds)

    else:
        if args.fake:
            logger.set_logger_dir(os.path.join('train_log', 'tmp'), 'd')
        else:
            logger.set_logger_dir(
                os.path.join('train_log', 'iNaturalist-{}-d{}'.format(args.mode, args.depth)))

        config = get_config(model, fake=args.fake)
コード例 #28
0
        "Note that it's best to keep per-GPU batch size in [32, 64] to obtain the best accuracy."
        "Pretrained models listed in README were trained with batch=32x8.")
    parser.add_argument('--mode',
                        choices=['resnet', 'preact', 'se'],
                        help='variants of resnet to use',
                        default='resnet')
    args = parser.parse_args()

    if args.gpu:
        os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu

    model = Model(args.depth, args.mode)
    model.data_format = args.data_format
    if args.eval:
        batch = 128  # something that can run on one gpu
        ds = get_data('val', batch)
        eval_on_ILSVRC12(model, get_model_loader(args.load), ds)
    else:
        if args.fake:
            logger.set_logger_dir(os.path.join('train_log', 'tmp'), 'd')
        else:
            logger.set_logger_dir(
                os.path.join('train_log',
                             'imagenet-{}-d{}'.format(args.mode, args.depth)))

        config = get_config(model, fake=args.fake)
        if args.load:
            config.session_init = get_model_loader(args.load)
        trainer = SyncMultiGPUTrainerReplicated(max(get_nr_gpu(), 1))
        launch_train_with_config(config, trainer)
コード例 #29
0
def do_train(model):
    batch = args.batch
    total_batch = batch * hvd.size()

    if args.fake:
        data = FakeData([[batch, 224, 224, 3], [batch]],
                        1000,
                        random=False,
                        dtype=['uint8', 'int32'])
        data = StagingInput(QueueInput(data))
        callbacks = []
        steps_per_epoch = 50
    else:
        logger.info("#Tower: {}; Batch size per tower: {}".format(
            hvd.size(), batch))
        zmq_addr = 'ipc://@imagenet-train-b{}'.format(batch)
        if args.no_zmq_ops:
            dataflow = RemoteDataZMQ(zmq_addr, hwm=150, bind=False)
            data = QueueInput(dataflow)
        else:
            data = ZMQInput(zmq_addr, 30, bind=False)
        data = StagingInput(data)

        steps_per_epoch = int(np.round(1281167 / total_batch))

    BASE_LR = 0.1 * (total_batch // 256)
    """
    ImageNet in 1 Hour, Sec 2.1:
    Linear Scaling Rule: When the minibatch size is
    multiplied by k, multiply the learning rate by k.
    """
    logger.info("Base LR: {}".format(BASE_LR))
    callbacks = [
        ModelSaver(max_to_keep=10),
        EstimatedTimeLeft(),
        ScheduledHyperParamSetter('learning_rate', [(0, BASE_LR),
                                                    (35, BASE_LR * 1e-1),
                                                    (70, BASE_LR * 1e-2),
                                                    (95, BASE_LR * 1e-3)])
    ]
    """
    Feature Denoising, Sec 5:
    Our models are trained for a total of
    110 epochs; we decrease the learning rate by 10× at the 35-
    th, 70-th, and 95-th epoch
    """
    max_epoch = 110

    if BASE_LR > 0.1:
        callbacks.append(
            ScheduledHyperParamSetter('learning_rate',
                                      [(0, 0.1),
                                       (5 * steps_per_epoch, BASE_LR)],
                                      interp='linear',
                                      step_based=True))
        """
        ImageNet in 1 Hour, Sec 2.2:
        we start from a learning rate of η and increment it by a constant amount at
        each iteration such that it reaches ηˆ = kη after 5 epochs
        """

    if not args.fake:
        # add distributed evaluation, for various attackers that we care.
        def add_eval_callback(name, attacker, condition):
            cb = create_eval_callback(
                name,
                model.get_inference_func(attacker),
                # always eval in the last 2 epochs no matter what
                lambda epoch_num: condition(epoch_num) or epoch_num > max_epoch
                - 2)
            callbacks.append(cb)

        add_eval_callback('eval-clean', NoOpAttacker(), lambda e: True)
        add_eval_callback(
            'eval-10step',
            PGDAttacker(10, args.attack_epsilon, args.attack_step_size),
            lambda e: True)
        add_eval_callback(
            'eval-50step',
            PGDAttacker(50, args.attack_epsilon, args.attack_step_size),
            lambda e: e % 20 == 0)
        add_eval_callback(
            'eval-100step',
            PGDAttacker(100, args.attack_epsilon, args.attack_step_size),
            lambda e: e % 10 == 0 or e > max_epoch - 5)
        for k in [20, 30, 40, 60, 70, 80, 90]:
            add_eval_callback(
                'eval-{}step'.format(k),
                PGDAttacker(k, args.attack_epsilon, args.attack_step_size),
                lambda e: False)

    trainer = HorovodTrainer(average=True)
    trainer.setup_graph(model.get_inputs_desc(), data, model.build_graph,
                        model.get_optimizer)
    trainer.train_with_defaults(callbacks=callbacks,
                                steps_per_epoch=steps_per_epoch,
                                session_init=get_model_loader(args.load)
                                if args.load is not None else None,
                                max_epoch=max_epoch,
                                starting_epoch=args.starting_epoch)
コード例 #30
0
    if args.v2 and args.group != parser.get_default('group'):
        logger.error("group= is not used in ShuffleNetV2!")

    if args.batch != 1024:
        logger.warn(
            "Total batch size != 1024, you need to change other hyperparameters to get the same results."
        )
    TOTAL_BATCH_SIZE = args.batch

    model = Model()

    if args.eval:
        batch = 128  # something that can run on one gpu
        ds = get_data('val', batch)
        eval_classification(model, get_model_loader(args.load), ds)
    elif args.flops:
        # manually build the graph with batch=1
        with TowerContext('', is_training=False):
            model.build_graph(
                tf.placeholder(tf.float32, [1, 224, 224, 3], 'input'),
                tf.placeholder(tf.int32, [1], 'label'))
        model_utils.describe_trainable_vars()

        tf.profiler.profile(
            tf.get_default_graph(),
            cmd='op',
            options=tf.profiler.ProfileOptionBuilder.float_operation())
        logger.info(
            "Note that TensorFlow counts flops in a different way from the paper."
        )
                    help='ResNet depth',
                    type=int,
                    default=152,
                    choices=[50, 101, 152])
parser.add_argument('--arch',
                    help='Name of architectures defined in nets.py',
                    default='ResNetDenoise')
parser.add_argument('--load', help='path to checkpoint')
parser.add_argument('--input', help='path to input image')
args = parser.parse_args()

model = getattr(nets, args.arch + 'Model')(args)

input = tf.placeholder(tf.float32, shape=(None, 224, 224, 3))
image = input / 127.5 - 1.0
image = tf.transpose(image, [0, 3, 1, 2])
with TowerContext('', is_training=False):
    logits = model.get_logits(image)

sess = tf.Session()
get_model_loader(args.load).init(sess)

sample = cv2.imread(args.input)  # this is a BGR image, not RGB
# imagenet evaluation uses standard imagenet pre-processing
# (resize shortest edge to 256 + center crop 224).
# However, for images of unknown sources, let's just do a naive resize.
sample = cv2.resize(sample, (224, 224))

l = sess.run(logits, feed_dict={input: np.array([sample])})
print("Prediction: ", l.argmax())