Пример #1
0
def tfslim_vgg16():
    import tensorflow as tf
    from nets import nets_factory
    from preprocessing import vgg_preprocessing
    from model_tools.activations.tensorflow import load_resize_image
    tf.reset_default_graph()

    image_size = 224
    placeholder = tf.placeholder(dtype=tf.string, shape=[64])
    preprocess_image = lambda image: vgg_preprocessing.preprocess_image(
        image, image_size, image_size, resize_side_min=image_size)
    preprocess = lambda image_path: preprocess_image(
        load_resize_image(image_path, image_size))
    preprocess = tf.map_fn(preprocess, placeholder, dtype=tf.float32)

    model_ctr = nets_factory.get_network_fn('vgg_16',
                                            num_classes=1001,
                                            is_training=False)
    logits, endpoints = model_ctr(preprocess)

    session = tf.Session()
    session.run(tf.initialize_all_variables())
    return TensorflowSlimWrapper(identifier='tf-vgg16',
                                 labels_offset=1,
                                 endpoints=endpoints,
                                 inputs=placeholder,
                                 session=session)
Пример #2
0
def tfslim_custom():
    from model_tools.activations.tensorflow import load_resize_image
    import tensorflow as tf
    slim = tf.contrib.slim
    tf.compat.v1.reset_default_graph()

    image_size = 224
    placeholder = tf.compat.v1.placeholder(dtype=tf.string, shape=[64])
    preprocess = lambda image_path: load_resize_image(image_path, image_size)
    preprocess = tf.map_fn(preprocess, placeholder, dtype=tf.float32)

    with tf.compat.v1.variable_scope('my_model', values=[preprocess]) as sc:
        end_points_collection = sc.original_name_scope + '_end_points'
        # Collect outputs for conv2d, fully_connected and max_pool2d.
        with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d],
                            outputs_collections=[end_points_collection]):
            net = slim.conv2d(preprocess, 64, [11, 11], 4, padding='VALID', scope='conv1')
            net = slim.max_pool2d(net, [5, 5], 5, scope='pool1')
            net = slim.max_pool2d(net, [3, 3], 2, scope='pool2')
            net = slim.flatten(net, scope='flatten')
            net = slim.fully_connected(net, 1000, scope='logits')
            endpoints = slim.utils.convert_collection_to_dict(end_points_collection)

    session = tf.compat.v1.Session()
    session.run(tf.compat.v1.initialize_all_variables())
    return TensorflowSlimWrapper(identifier='tf-custom', labels_offset=0,
                                 endpoints=endpoints, inputs=placeholder, session=session)
Пример #3
0
def build_model_ending_points(img_paths):
    image_size = 224
    _load_func = lambda image_path: load_resize_image(image_path, image_size)
    imgs = tf.map_fn(_load_func, img_paths, dtype=tf.float32)

    setting_name = 'cate_res18_exp1'
    ending_points, _ = get_network_outputs(
            {'images': imgs},
            prep_type='mean_std',
            model_type='vm_model',
            setting_name=setting_name,
            #module_name=['encode', 'category'],
            module_name=['encode'],
            )
    for key in ending_points:
        ending_points[key] = tf.transpose(ending_points[key], [0, 3, 1, 2])
    return ending_points
Пример #4
0
 def _get_imgs_from_paths(self, img_paths):
     _load_func = lambda image_path: load_resize_image(image_path, 224)
     imgs = tf.map_fn(_load_func, img_paths, dtype=tf.float32)
     return imgs