def __init__(self, tarball_path):
    """Creates and loads pretrained deeplab model."""
    self.graph = tf.Graph()

    graph_def = None
    # Extract frozen graph from tar archive.
    tar_file = tarfile.open(tarball_path)
    for tar_info in tar_file.getmembers():
      if self.FROZEN_GRAPH_NAME in os.path.basename(tar_info.name):
        file_handle = tar_file.extractfile(tar_info)
        graph_def = tf1.GraphDef.FromString(file_handle.read())
        break

    tar_file.close()

    if graph_def is None:
      raise RuntimeError('Cannot find inference graph in tar archive.')

    with self.graph.as_default():
      tf.import_graph_def(graph_def, name='')

    config = tf1.ConfigProto()
    # device_count = {'CPU': 0}
    config.gpu_options.allow_growth = True
    self.sess = tf1.Session(graph=self.graph, config=config)
예제 #2
0
    def _evaluate_graph_def(self, graph_def, inputs, outputs, input_data):
        """Evaluates the GraphDef using Sessions."""
        with tf.Graph().as_default() as graph:
            tf.import_graph_def(graph_def, name="")
            sess = tf.compat.v1.Session(graph=graph)

        input_tensors = self._get_tensors(sess, inputs)
        output_tensors = self._get_tensors(sess, outputs)
        return sess.run(output_tensors,
                        feed_dict=dict(zip(input_tensors, input_data)))
예제 #3
0
def load_graph(model_file):
    graph = tf.Graph()
    graph_def = tf.GraphDef()

    with open(model_file, "rb") as f:
        graph_def.ParseFromString(f.read())
    with graph.as_default():
        tf.import_graph_def(graph_def)

    return graph
예제 #4
0
def main(_):
    # What model to download.
    MODEL_NAME = FLAGS.model_name

    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_FROZEN_GRAPH = 'pretrained/' + MODEL_NAME + '/frozen_inference_graph.pb'

    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join('data', FLAGS.data_label)

    ACTIVITY = FLAGS.acivity
    # PATH_TO_TEST_IMAGES_DIR = FLAGS.path_to_test_images_dir%ACTIVITY

    BATCH = FLAGS.batch
    outputfolder = FLAGS.output_dir % ACTIVITY

    if not os.path.exists(outputfolder):
        os.makedirs(outputfolder)

    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.compat.v1.GraphDef()
        with gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

    category_index = label_map_util.create_category_index_from_labelmap(
        PATH_TO_LABELS, use_display_name=True)

    activity = 'swing'
    numpy_dir = '/media/felicia/Data/object_detection/data/%s/' % activity
    numpy_name = 'image_bbgame_swing'
    len_num_shards = 6

    # Size, in inches, of the output images.

    for i in range(len_num_shards):

        valid_boxes = []
        valid_scores = []
        valid_classes = []

        numpy_path = os.path.join(
            numpy_dir,
            '%s-%s-of-%s.npy' % (numpy_name, str(i), str(len_num_shards)))

        logging.info('Loading %s', numpy_path)

        numpy_dict = np.load(numpy_path, allow_pickle=True)
        numpy_dict = numpy_dict.item()

        images = numpy_dict['images']
        labels = numpy_dict['activity']
        steps = numpy_dict['steps']
        vd_names = numpy_dict['videos']

        nbatch = len(images) // BATCH + (1 if len(images) % BATCH > 0 else 0)

        for j in tqdm(range(nbatch)):
            idx_st = j * BATCH
            idx_ed = (j + 1) * BATCH if (j + 1) * BATCH < len(images) else len(
                images)
            image_batch = images[idx_st:idx_ed]  # B*H*W*C

            output_dict = run_inference_for_minibatch_images(
                image_batch, detection_graph)

            minibatch_boxes, minibatch_scores, minibatch_classes = valid_boxes_on_minibatch_images(
                output_dict['detection_boxes'],
                output_dict['detection_classes'],
                output_dict['detection_scores'],
                max_boxes,
                min_score,
                selected_class=1)

            valid_boxes += minibatch_boxes
            valid_scores += minibatch_scores
            valid_classes += minibatch_classes

        valid_output = {
            'boxes': valid_boxes,
            'scores': valid_scores,
            'classes': valid_classes
        }

        output_filename = os.path.join(
            outputfolder,
            '%s-%s-of-%s.npy' % (FLAGS.name, str(i), str(len_num_shards)))

        with open(output_filename, 'wb') as file:
            np.save(file, valid_output)
예제 #5
0
def main():
    #download google pre-trained neural network
    local_zip_file = 'inception5h.zip'
    if not os.path.exists(local_zip_file):
        #download
        model_url = urllib.request.urlopen(url)
        with open(local_zip_file, 'wb') as output:
            output.write(model_url.read())

        #extract
        with zipfile.ZipFile(local_zip_file, 'r') as zip_ref:
            zip_ref.extractall(data_dir)
    model_fn = 'tensorflow_inseption_graph.pb'

    #Creating tf session and loading the model
    graph = tf.Graph()
    sess = tfc.InteractiveSession(graph=graph)
    with tfc.gfile.FastGFile((local_zip_file), 'rb') as f:
        graph_def = tf.io.gfile.GFile()
        graph_def.ParseFromString(f.read())
    t_input = tf.placeholder(np.float32, name='input')  #define input tensor
    imagenet_mean = 117.0
    t_preprocessed = tf.expand_dims(t_input - imagenet_mean, 0)
    tf.import_graph_def(graph_def, {'input': t_preprocessed})

    layers = [
        op.name for op in graph.get_operations()
        if op.type == 'Cony2D' and 'import/' in op.name
    ]
    feature_nums = [
        int(graph.get_tensor_by_name(model_name + ':0').get_shape()[-1])
        for name in layers
    ]

    print('Number of layers: ', len(layers))
    print('Total numbers of feature channels:', sum(feature_nums))

    def render_deepdream(t_obj,
                         img0=img_noise,
                         iter_n=10,
                         step=1.5,
                         octave_n=4,
                         octave_scale=1.4):
        t_score = tf.reduce_mean(t_obj)  #defining optimization objective
        t_grad = tf.gradients(t_score, t_input)[0]

        #split the image into a number of octaves
        img = img0
        octaves = []
        for _ in range(octave_n - 1):
            hw = img.shape[:2]
            lo = resize(img, np.int32(np.float32(hw) / octave_scale))
            hi = img - resize(low, hw)
            img = lo
            octaves.append(hi)

        #generate details octave by octave
        for octave in range(octave_n):
            if octave > 0:
                hi = octaves[-octave]
                img = resize(img, hi.shape[:2]) + hi
            for _ in range(iter_n):
                g = calc_grad_tiled(img, t_grad)
                img += g * (step / (np.abs(g).mean() + 1e-7))
            #output deep dreamed image
            showarray(img / 255.0)

    #Pick a layer to enchance my image
    layer = 'mixed4d_3x3_bottleneck_pre_relu'
    channel = 139

    img0 = PIL.Image.open('image.jpg')
    img0 = np.float32(img0)

    #Apply gradient ascent to the layer
    render_deepdream(tf.square(T('mixed4c')), img0)