Example #1
0
 def inputs(self):
     # Use a combined state for efficiency.
     # The first h channels are the current state, and the last h channels are the next state.
     return [
         InputDesc(tf.uint8,
                   (None, ) + self.image_shape + (self.channel + 1, ),
                   'comb_state'),
         InputDesc(tf.int64, (None, ), 'action'),
         InputDesc(tf.float32, (None, ), 'reward'),
         InputDesc(tf.bool, (None, ), 'isOver'),
         InputDesc(tf.bool, (None, ), 'human')
     ]
Example #2
0
 def _get_inputs(self):
     # Use a combined state for efficiency.
     # The first h channels are the current state, and the last h channels are the next state.
     return [
         InputDesc(
             tf.uint8,
             (None, ) + (self.agents, ) + self.image_shape +
             (self.channel + 1, ),
             "comb_state",
         ),
         InputDesc(tf.int64, (None, self.agents), "action"),
         InputDesc(tf.float32, (None, self.agents), "reward"),
         InputDesc(tf.bool, (None, self.agents), "isOver"),
     ]
Example #3
0
 def _get_inputs(self):
     return [
         # assumes only one symmetric unit as input batch x  vlen x vlen
         InputDesc(tf.float32,
                   (cfg.NCAND, self._nviews, self._vlen, self._vlen),
                   'projections'),
     ]
Example #4
0
def calc_flops(model):
    # 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',
        options=tf.profiler.ProfileOptionBuilder.float_operation())
    logger.info(
        "Note that TensorFlow counts flops in a different way from the paper.")
    logger.info(
        "TensorFlow counts multiply+add as two flops, however the paper counts them "
        "as 1 flop because it can be executed in one instruction.")
    def inputs(self):
        """Return metadata about entry data.

        Returns:
            list[tensorpack.InputDesc]

        Raises:
            ValueError: If any of the elements in self.metadata['details'] has an unsupported
                        value in the `type` key.

        """
        inputs = []
        for col_id, col_info in enumerate(self.metadata['details']):
            if col_info['type'] == 'value':
                gaussian_components = col_info['n']
                inputs.append(
                    InputDesc(tf.float32, (self.batch_size, 1),
                              'input%02dvalue' % col_id))

                inputs.append(
                    InputDesc(tf.float32,
                              (self.batch_size, gaussian_components),
                              'input%02dcluster' % col_id))

            elif col_info['type'] == 'category':
                inputs.append(
                    InputDesc(tf.int32, (self.batch_size, 1),
                              'input%02d' % col_id))

            else:
                raise ValueError(
                    "self.metadata['details'][{}]['type'] must be either `category` or "
                    "`values`. Instead it was {}.".format(
                        col_id, col_info['type']))

        return inputs
Example #6
0
 def _get_inputs(self):
     return [
         InputDesc(tf.float32, (BATCH_SIZE, IMAGE_WIDTH, IMAGE_HEIGHT, 3),
                   'input_a'),
         InputDesc(tf.float32, (BATCH_SIZE, ), 'score_a'),
         InputDesc(tf.float32, (BATCH_SIZE, IMAGE_WIDTH, IMAGE_HEIGHT, 3),
                   'input_p'),
         InputDesc(tf.float32, (BATCH_SIZE, ), 'score_p'),
         InputDesc(tf.float32, (BATCH_SIZE, IMAGE_WIDTH, IMAGE_HEIGHT, 3),
                   'input_n'),
         InputDesc(tf.float32, (BATCH_SIZE, ), 'score_n')
     ]
Example #7
0
                        help='print flops and exit')
    args = parser.parse_args()

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

    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=True):
            model.build_graph(*input.get_input_tensors())

        tf.profiler.profile(
            tf.get_default_graph(),
            cmd='op',
            options=tf.profiler.ProfileOptionBuilder.float_operation())
    else:
        logger.set_logger_dir(os.path.join('train_log', 'shufflenet'))

        nr_tower = max(get_nr_gpu(), 1)
Example #8
0
        batch_size = TOTAL_BATCH_SIZE // num_gpu
        assert args.data is not None
        df_train = get_imagenet_dataflow(args.data, 'train', batch_size,
                                         fbresnet_augmentor(True))
        df_val = get_imagenet_dataflow(args.data, 'val', batch_size,
                                       fbresnet_augmentor(False))

        def one_hot(label):
            return np.eye(1000)[label]

        df_train = MapDataComponent(df_train, one_hot, 1)
        df_val = MapDataComponent(df_val, one_hot, 1)

    M = KerasModel(
        resnet50,
        inputs_desc=[InputDesc(tf.uint8, [None, 224, 224, 3], 'images')],
        targets_desc=[InputDesc(tf.float32, [None, 1000], 'labels')],
        input=df_train,
        trainer=SyncMultiGPUTrainerReplicated(num_gpu))

    lr = tf.get_variable('learning_rate', initializer=0.1, trainable=False)
    tf.summary.scalar('lr', lr)

    M.compile(optimizer=tf.train.MomentumOptimizer(lr, 0.9, use_nesterov=True),
              loss='categorical_crossentropy',
              metrics='categorical_accuracy')

    callbacks = [
        ModelSaver(),
        ScheduledHyperParamSetter('learning_rate', [(0, 0.1), (3, BASE_LR)],
                                  interp='linear'),  # warmup
 def _get_inputs(self):
     return [
         InputDesc(tf.float32, input_shape, 'volume'),
         InputDesc(tf.int32, input_shape, 'gt_seg'),
         InputDesc(tf.float32, affs_shape, 'gt_affs')
     ]
Example #10
0
    if args.flops:
        finalize_configs(is_training=False)

        MODEL = PredModel(1, cfg.PREPROC.INPUT_SHAPE_EVAL)

        in_shape = [
            1, cfg.PREPROC.INPUT_SHAPE_EVAL[0],
            cfg.PREPROC.INPUT_SHAPE_EVAL[1], 3
        ]
        pred_config = PredictConfig(
            model=MODEL,
            input_names=MODEL.get_inference_tensor_names()[0],
            output_names=MODEL.get_inference_tensor_names()[1])
        pred_config.inputs_desc[0] = InputDesc(type=tf.float32,
                                               shape=in_shape,
                                               name='data')
        inputs = PlaceholderInput()
        inputs.setup(pred_config.inputs_desc)
        with PredictTowerContext(''):
            MODEL.build_graph(*inputs.get_input_tensors())
        model_utils.describe_trainable_vars()
        tf.profiler.profile(
            tf.get_default_graph(),
            cmd='op',
            options=tf.profiler.ProfileOptionBuilder.float_operation())
        sys.exit(0)

    assert args.load

    # is_training = False
                                        data_type='val',
                                        nr_thread=nr_thread,
                                        buffer_size=buffer_size)

        #########################################
        def one_hot(label):
            return np.eye(len(target_name))[label]

        train_gen = dataflow.MapDataComponent(train_gen, one_hot, 1)
        val_set = dataflow.MapDataComponent(val_set, one_hot, 1)

        ###start building models
        model_test_GPU_new = KerasModel(
            build_model_resnet50,
            inputs_desc=[
                InputDesc(tf.float32, (None, ) + img_shape, 'images')
            ],
            targets_desc=[
                InputDesc(tf.float32, (None, len(target_name)), 'labels')
            ],
            input=train_gen,
            trainer=SyncMultiGPUTrainerParameterServer(num_GPU))

        model_test_GPU_new.compile(optimizer=opt_use,
                                   loss='categorical_crossentropy',
                                   metrics='categorical_accuracy')
        callbacks = [
            ModelSaver(checkpoint_dir=checkpoint_dir),
            # MinSaver('val-error-top1'),   ##backup the model with best validation error
            GPUUtilizationTracker(
            ),  ## record GPU utilizations during training
Example #12
0
 def _get_inputs(self):
     # uint8 instead of float32 is used as input type to reduce copy overhead.
     # It might hurt the performance a liiiitle bit.
     # The pretrained models were trained with float32.
     return [InputDesc(tf.uint8, [None, INPUT_SHAPE, INPUT_SHAPE, 3], 'input'),
             InputDesc(tf.int32, [None], 'label')]
Example #13
0
 def _get_inputs(self):
     return [
         InputDesc(tf.float32, [None, 32, 32, 3], 'input'),
         InputDesc(tf.int32, [None], 'label')
     ]
Example #14
0
        M.add(KL.Conv2D(32, 3, padding='same', activation='relu'))

        M.add(KL.Flatten())
        M.add(
            KL.Dense(512,
                     activation='relu',
                     kernel_regularizer=keras.regularizers.l2(1e-5)))
        M.add(KL.Dropout(0.5))
        M.add(
            KL.Dense(10,
                     activation=None,
                     kernel_regularizer=keras.regularizers.l2(1e-5)))
        M.add(KL.Activation('softmax'))
        return M

    dataset_train, dataset_test = get_data()

    M = KerasModel(model_func,
                   inputs_desc=[
                       InputDesc(tf.float32, [None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                 'images')
                   ],
                   targets_desc=[InputDesc(tf.float32, [None, 10], 'labels')],
                   input=QueueInput(dataset_train))
    M.compile(optimizer=tf.train.AdamOptimizer(1e-3),
              loss='categorical_crossentropy',
              metrics='categorical_accuracy')
    M.fit(validation_data=dataset_test,
          steps_per_epoch=dataset_train.size(),
          callbacks=[ModelSaver()])
    return stations


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('image_path')
    parser.add_argument('--model_path', default='log/checkpoint')
    parser.add_argument('--output_path', default='figures/')
    parser.add_argument('--size', type=int, default=32)
    args = parser.parse_args()

    np.random.seed(0)
    # initialize the model
    predict_func = OfflinePredictor(
        PredictConfig(inputs_desc=[
            InputDesc(tf.float32, [None, INPUT_SIZE, INPUT_SIZE, 2],
                      'input_image')
        ],
                      tower_func=model.feedforward,
                      session_init=SaverRestore(args.model_path),
                      input_names=['input_image'],
                      output_names=['prob']))

    # simulate suda's gridworld input
    image = cv2.imread(
        args.image_path,
        cv2.IMREAD_GRAYSCALE)  # 0 if obstacle, 255 if free space
    h, w = image.shape[:2]
    obj = img2obj(image)  # list containing row major indices of objects

    # specify position is recent memory
    radius = 6
Example #16
0
 def _get_inputs(self):
     return [
         InputDesc(self.image_dtype,
                   [None, self.image_shape, self.image_shape, 3], 'input'),
         InputDesc(tf.int32, [None], 'label')
     ]
Example #17
0
 def _get_inputs(self):
     # [HR, LR]
     return [
         InputDesc(tf.float32, self.hr_shape, 'Ihr'),
         InputDesc(tf.float32, self.lr_shape, 'Ihr')
     ]