Example #1
0
    def test_merge_graph_defs_partitioned_call_remap(self):
        expected_proto = GraphDef()
        text_format.Merge(
            """
                node {
                  name: "graph_1/X"
                  op: "PartitionedCall"
                  attr {
                    key: "f"
                    value {
                      func {
                        name: "graph_1_foo"
                      }
                    }
                  }
                }
                library {
                  function {
                    signature {
                      name: "graph_1_foo"
                      input_arg {
                        name: "x"
                        type: DT_HALF
                      }
                      output_arg {
                        name: "identity"
                        type: DT_HALF
                      }
                    }
                  }
                }
            """,
            expected_proto,
        )

        graph_def_a = GraphDef()
        text_format.Merge(
            """
                node {
                  name: "X"
                  op: "PartitionedCall"
                  attr {
                    key: "f"
                    value {
                      func {
                        name: "foo"
                      }
                    }
                  }
                }
                library {
                  function {
                    signature {
                      name: "foo"
                      input_arg {
                        name: "x"
                        type: DT_HALF
                      }
                      output_arg {
                        name: "identity"
                        type: DT_HALF
                      }
                    }
                  }
                }
            """,
            graph_def_a,
        )
        graph_def_b = GraphDef()

        self.assertProtoEquals(
            expected_proto,
            graph_util.merge_graph_defs([graph_def_a, graph_def_b]),
        )
 def get_fake_keypoint_proto(self, customize_head_params=False):
   task_proto_txt = """
     task_name: "human_pose"
     task_loss_weight: 0.9
     keypoint_regression_loss_weight: 1.0
     keypoint_heatmap_loss_weight: 0.1
     keypoint_offset_loss_weight: 0.5
     heatmap_bias_init: 2.14
     keypoint_class_name: "/m/01g317"
     loss {
       classification_loss {
         penalty_reduced_logistic_focal_loss {
           alpha: 3.0
           beta: 4.0
         }
       }
       localization_loss {
         l1_localization_loss {
         }
       }
     }
     keypoint_label_to_std {
       key: "nose"
       value: 0.3
     }
     keypoint_label_to_std {
       key: "hip"
       value: 0.0
     }
     keypoint_candidate_score_threshold: 0.3
     num_candidates_per_keypoint: 12
     peak_max_pool_kernel_size: 5
     unmatched_keypoint_score: 0.05
     box_scale: 1.7
     candidate_search_scale: 0.2
     candidate_ranking_mode: "score_distance_ratio"
     offset_peak_radius: 3
     per_keypoint_offset: true
     predict_depth: true
     per_keypoint_depth: true
     keypoint_depth_loss_weight: 0.3
     score_distance_multiplier: 11.0
     std_dev_multiplier: 2.8
     rescoring_threshold: 0.5
     gaussian_denom_ratio: 0.3
     argmax_postprocessing: True
   """
   if customize_head_params:
     task_proto_txt += """
     heatmap_head_params {
       num_filters: 64
       num_filters: 32
       kernel_sizes: 5
       kernel_sizes: 3
     }
     offset_head_params {
       num_filters: 128
       num_filters: 64
       kernel_sizes: 5
       kernel_sizes: 3
     }
     """
   config = text_format.Merge(task_proto_txt,
                              center_net_pb2.CenterNet.KeypointEstimation())
   return config
Example #3
0
def optimize_model(config_path,
                   checkpoint_path,
                   use_trt=True,
                   force_nms_cpu=True,
                   replace_relu6=True,
                   remove_assert=True,
                   override_nms_score_threshold=None,
                   override_resizer_shape=None,
                   max_batch_size=1,
                   precision_mode='FP32',
                   minimum_segment_size=2,
                   max_workspace_size_bytes=1 << 32,
                   maximum_cached_engines=100,
                   calib_images_dir=None,
                   num_calib_images=None,
                   calib_image_shape=None,
                   tmp_dir='.optimize_model_tmp_dir',
                   remove_tmp_dir=True,
                   output_path=None,
                   display_every=100):
    """Optimizes an object detection model using TensorRT

    Optimizes an object detection model using TensorRT.  This method also
    performs pre-tensorrt optimizations specific to the TensorFlow object
    detection API models.  Please see the list of arguments for other
    optimization parameters.

    Args
    ----
        config_path: A string representing the path of the object detection
            pipeline config file.
        checkpoint_path: A string representing the path of the object
            detection model checkpoint.
        use_trt: A boolean representing whether to optimize with TensorRT. If
            False, regular TensorFlow will be used but other optimizations
            (like NMS device placement) will still be applied.
        force_nms_cpu: A boolean indicating whether to place NMS operations on
            the CPU.
        replace_relu6: A boolean indicating whether to replace relu6(x)
            operations with relu(x) - relu(x-6).
        remove_assert: A boolean indicating whether to remove Assert
            operations from the graph.
        override_nms_score_threshold: An optional float representing
            a NMS score threshold to override that specified in the object
            detection configuration file.
        override_resizer_shape: An optional list/tuple of integers
            representing a fixed shape to override the default image resizer
            specified in the object detection configuration file.
        max_batch_size: An integer representing the max batch size to use for
            TensorRT optimization.
        precision_mode: A string representing the precision mode to use for
            TensorRT optimization.  Must be one of 'FP32', 'FP16', or 'INT8'.
        minimum_segment_size: An integer representing the minimum segment size
            to use for TensorRT graph segmentation.
        max_workspace_size_bytes: An integer representing the max workspace
            size for TensorRT optimization.
        maximum_cached_engines: An integer represenging the number of TRT engines
            that can be stored in the cache.
        calib_images_dir: A string representing a directory containing images to
            use for int8 calibration. 
        num_calib_images: An integer representing the number of calibration 
            images to use.  If None, will use all images in directory.
        calib_image_shape: A tuple of integers representing the height, 
            width that images will be resized to for calibration. 
        tmp_dir: A string representing a directory for temporary files.  This
            directory will be created and removed by this function and should
            not already exist.  If the directory exists, an error will be
            thrown.
        remove_tmp_dir: A boolean indicating whether we should remove the
            tmp_dir or throw error.
        output_path: An optional string representing the path to save the
            optimized GraphDef to.
        display_every: print log for calibration every display_every iteration

    Returns
    -------
        A GraphDef representing the optimized model.
    """
    if max_batch_size > 1 and calib_image_shape is None:
        raise RuntimeError(
            'Fixed calibration image shape must be provided for max_batch_size > 1'
        )
    if os.path.exists(tmp_dir):
        if not remove_tmp_dir:
            raise RuntimeError(
                'Cannot create temporary directory, path exists: %s' % tmp_dir)
        subprocess.call(['rm', '-rf', tmp_dir])

    # load config from file
    config = pipeline_pb2.TrainEvalPipelineConfig()
    with open(config_path, 'r') as f:
        text_format.Merge(f.read(), config, allow_unknown_extension=True)

    # override some config parameters
    if config.model.HasField('ssd'):
        config.model.ssd.feature_extractor.override_base_feature_extractor_hyperparams = True
        if override_nms_score_threshold is not None:
            config.model.ssd.post_processing.batch_non_max_suppression.score_threshold = override_nms_score_threshold
        if override_resizer_shape is not None:
            config.model.ssd.image_resizer.fixed_shape_resizer.height = override_resizer_shape[
                0]
            config.model.ssd.image_resizer.fixed_shape_resizer.width = override_resizer_shape[
                1]
    elif config.model.HasField('faster_rcnn'):
        if override_nms_score_threshold is not None:
            config.model.faster_rcnn.second_stage_post_processing.batch_non_max_suppression.score_threshold = override_nms_score_threshold
        if override_resizer_shape is not None:
            config.model.faster_rcnn.image_resizer.fixed_shape_resizer.height = override_resizer_shape[
                0]
            config.model.faster_rcnn.image_resizer.fixed_shape_resizer.width = override_resizer_shape[
                1]

    tf_config = tf.ConfigProto()
    tf_config.gpu_options.allow_growth = True

    # export inference graph to file (initial), this will create tmp_dir
    with tf.Session(config=tf_config):
        with tf.Graph().as_default():
            exporter.export_inference_graph(
                INPUT_NAME,
                config,
                checkpoint_path,
                tmp_dir,
                input_shape=[max_batch_size, None, None, 3])

    # read frozen graph from file
    frozen_graph_path = os.path.join(tmp_dir, FROZEN_GRAPH_NAME)
    frozen_graph = tf.GraphDef()
    with open(frozen_graph_path, 'rb') as f:
        frozen_graph.ParseFromString(f.read())

    # apply graph modifications
    if force_nms_cpu:
        frozen_graph = f_force_nms_cpu(frozen_graph)
    if replace_relu6:
        frozen_graph = f_replace_relu6(frozen_graph)
    if remove_assert:
        frozen_graph = f_remove_assert(frozen_graph)

    # get input names
    output_names = [BOXES_NAME, CLASSES_NAME, SCORES_NAME, NUM_DETECTIONS_NAME]

    # optionally perform TensorRT optimization
    if use_trt:
        graph_size = len(frozen_graph.SerializeToString())
        num_nodes = len(frozen_graph.node)
        start_time = time.time()

        converter = trt.TrtGraphConverter(
            input_graph_def=frozen_graph,
            nodes_blacklist=output_names,
            max_batch_size=max_batch_size,
            max_workspace_size_bytes=max_workspace_size_bytes,
            precision_mode=precision_mode,
            minimum_segment_size=minimum_segment_size,
            is_dynamic_op=True,
            maximum_cached_engines=maximum_cached_engines)
        frozen_graph = converter.convert()

        end_time = time.time()
        print("graph_size(MB)(native_tf): %.1f" % (float(graph_size) /
                                                   (1 << 20)))
        print("graph_size(MB)(trt): %.1f" %
              (float(len(frozen_graph.SerializeToString())) / (1 << 20)))
        print("num_nodes(native_tf): %d" % num_nodes)
        print("num_nodes(tftrt_total): %d" % len(frozen_graph.node))
        print("num_nodes(trt_only): %d" %
              len([1
                   for n in frozen_graph.node if str(n.op) == 'TRTEngineOp']))
        print("time(s) (trt_conversion): %.4f" % (end_time - start_time))

        # perform calibration for int8 precision
        if precision_mode == 'INT8':
            # get calibration images
            if calib_images_dir is None:
                raise ValueError(
                    'calib_images_dir must be provided for INT8 optimization.')
            image_paths = glob.glob(os.path.join(calib_images_dir, '*.jpg'))
            if len(image_paths) == 0:
                raise ValueError(
                    'No images were found in calib_images_dir for INT8 calibration.'
                )
            image_paths = image_paths[:num_calib_images]
            num_batches = len(image_paths) // max_batch_size

            def feed_dict_fn():
                # read batch of images
                batch_images = []
                for image_path in image_paths[feed_dict_fn.
                                              index:feed_dict_fn.index +
                                              max_batch_size]:
                    image = _read_image(image_path, calib_image_shape)
                    batch_images.append(image)
                feed_dict_fn.index += max_batch_size
                return {INPUT_NAME + ':0': np.array(batch_images)}

            feed_dict_fn.index = 0

            print('Calibrating INT8...')
            start_time = time.time()
            frozen_graph = converter.calibrate(
                fetch_names=[x + ':0' for x in output_names],
                num_runs=num_batches,
                feed_dict_fn=feed_dict_fn)
            calibration_time = time.time() - start_time
            print("time(s) (trt_calibration): %.4f" % calibration_time)

    # re-enable variable batch size, this was forced to max
    # batch size during export to enable TensorRT optimization
    for node in frozen_graph.node:
        if INPUT_NAME == node.name:
            node.attr['shape'].shape.dim[0].size = -1

    # write optimized model to disk
    if output_path is not None:
        with open(output_path, 'wb') as f:
            f.write(frozen_graph.SerializeToString())

    # remove temporary directory
    subprocess.call(['rm', '-rf', tmp_dir])

    return frozen_graph
Example #4
0
        "--display-classes",
        default=None,
        help="If provided, only display specified class. Separate by ','")

    args = parser.parse_args()
    result_file = args.resultfile
    img_dir = args.imgdir
    if not os.path.exists(img_dir):
        print "{} does not exist".format(img_dir)
        sys.exit()
    labelmap_file = args.labelmap_file
    labelmap = None
    if labelmap_file and os.path.exists(labelmap_file):
        file = open(labelmap_file, 'r')
        labelmap = caffe_pb2.LabelMap()
        text_format.Merge(str(file.read()), labelmap)
    visualize_threshold = args.visualize_threshold
    save_dir = args.save_dir
    if save_dir and not os.path.exists(save_dir):
        os.makedirs(save_dir)
    display_classes = args.display_classes

    img_results = OrderedDict()
    with open(result_file, "r") as f:
        for line in f.readlines():
            ###############
            img_name, score, xmin, ymin, xmax, ymax = line.strip("\n").split()
            img_file = "{}/{}.{}".format(img_dir, img_name, 'jpg')
            result = dict()
            ###############
            result["label"] = 1
def main():
    args = parser.parse_args()
    response = urllib.urlopen(
        'https://www.googleapis.com/webfonts/v1/webfonts?key={}'.format(
            args.key))
    try:
        webfontList = json.loads(response.read())['items']
        webfontListFamilyNames = [item['family'] for item in webfontList]
    except (ValueError, KeyError):
        sys.exit(1)

    for dirpath, dirnames, filenames in os.walk(args.repo):
        metadataProtoFile = os.path.join(dirpath, 'METADATA.pb')
        if not os.path.exists(metadataProtoFile):
            continue

        metadata = FamilyProto()
        text_data = open(metadataProtoFile, "rb").read()
        text_format.Merge(text_data, metadata)
        try:
            family = metadata.name
        except KeyError:
            print('ER: "{}" does not contain FamilyName'.format(
                metadataProtoFile),
                  file=sys.stderr)
            continue

        try:
            index = webfontListFamilyNames.index(family)
            webfontsItem = webfontList[index]
        except ValueError:
            print('ER: Family "{}" could not be found in API'.format(family))
            continue

        webfontVariants = []
        log_messages = []
        for variant, fonturl in webfontsItem['files'].items():
            cache_font_path = get_cache_font_path(args.cache, fonturl)
            webfontVariants.append(variant)

            if args.ignore_copy_existing_ttf and os.path.exists(
                    cache_font_path):
                continue

            with open(cache_font_path, 'w') as fp:
                filenameWeightStyleIndex = [
                    getVariantName(item) for item in metadata.fonts
                ].index(variant)
                filename = metadata.fonts[filenameWeightStyleIndex].filename
                if args.verbose:
                    print('Downloading "{}" as "{}"'.format(fonturl, filename))

                #Saving:
                fp.write(urllib.urlopen(fonturl).read())

                #Symlinking:
                src = cache_font_path
                dst_dir = os.path.dirname(cache_font_path)
                dst = os.path.join(dst_dir, filename)
                if not os.path.exists(dst):
                    os.symlink(src, dst)

        for subset in webfontsItem['subsets']:
            if subset == "menu":
                # note about Google Web Fonts:
                # Menu subsets are no longer generated offline.
                continue

            if subset not in metadata.subsets:
                print('ER: "{}" lacks subset "{}" in git'.format(
                    family, subset),
                      file=sys.stderr)
            else:
                if args.verbose:
                    print('OK: "{}" subset "{}" in sync'.format(
                        family, subset))

        for subset in metadata.subsets:
            if subset != "menu" and subset not in webfontsItem['subsets']:
                print('ER: "{}" lacks subset "{}" in API'.format(
                    family, subset),
                      file=sys.stderr)

        if metadata.category.lower() != webfontsItem['category']:
            print(
                'ER: "{}" category "{}" in git does not match category "{}" in API'
                .format(family, metadata.category, webfontsItem['category']))
        else:
            if args.verbose:
                print('OK: "{}" category "{}" in sync'.format(
                    family, metadata.category))

        for variant in webfontVariants:
            try:
                filenameWeightStyleIndex = [
                    getVariantName(item) for item in metadata.fonts
                ].index(variant)
                repoFileName = metadata.fonts[
                    filenameWeightStyleIndex].filename

                fonturl = webfontsItem['files'][variant]
                fontpath = get_cache_font_path(args.cache, fonturl)

                import hashlib
                google_md5 = hashlib.md5(open(fontpath,
                                              'rb').read()).hexdigest()
                repo_md5 = hashlib.md5(
                    open(os.path.join(dirpath, repoFileName),
                         'rb').read()).hexdigest()

                if repo_md5 == google_md5:
                    log_messages.append(
                        [variant, 'OK', '"{}" in sync'.format(repoFileName)])
                else:
                    log_messages.append([
                        variant, 'ER',
                        '"{}" checksum mismatch, file in API does not match file in git'
                        .format(repoFileName)
                    ])

            except ValueError:
                log_messages.append([
                    variant, 'ER',
                    '"{}" available in API but not in git'.format(
                        font.filename)
                ])

        for font in metadata.fonts:
            variant = getVariantName(font)
            try:
                webfontVariants.index(variant)
            except ValueError:
                log_messages.append([
                    variant, 'ER',
                    '"{}" available in git but not in API'.format(
                        font.filename)
                ])

        #sort all the messages by their respective metadataFileName and print them:
        for message in sorted(log_messages, key=lambda x: x[0].lower()):
            variant, status, text = message
            if status == "OK":
                if args.verbose:
                    print('{}: {}'.format(status, text))
            else:
                print('{}: {}'.format(status, text), file=sys.stderr)
Example #6
0
                if not anno.isdigit():
                    print "annotation: {} is not an integer".format(anno)
            elif anno_type == "detection":
                if not os.path.exists(root_dir + anno):
                    print "annofation file: {} does not exist".format(root_dir + anno)
                    sys.exit()
            break
    # check if label map file exist
    if anno_type == "detection":
        if not os.path.exists(label_map_file):
            print "label map file: {} does not exist".format(label_map_file)
            sys.exit()
        label_map = caffe_pb2.LabelMap()
        lmf = open(label_map_file, "r")
        try:
            text_format.Merge(str(lmf.read()), label_map)
        except:
            print "Cannot parse label map file: {}".format(label_map_file)
            sys.exit()
    out_parent_dir = os.path.dirname(out_dir)
    if not os.path.exists(out_parent_dir):
        os.makedirs(out_parent_dir)
    if os.path.exists(out_dir) and not redo:
        print "{} already exists and I do not hear redo".format(out_dir)
        sys.exit()
    if os.path.exists(out_dir):
        shutil.rmtree(out_dir)

    # get caffe root directory
    caffe_root = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    if anno_type == "detection":
Example #7
0
def main(argv):
  del argv  # unused
  # Constructs lexical resources for SyntaxNet in the given resource path, from
  # the training data.
  lexicon.build_lexicon(
      lexicon_dir,
      training_sentence,
      training_corpus_format='sentence-prototext')

  # Construct the ComponentSpec for tagging. This is a simple left-to-right RNN
  # sequence tagger.
  tagger = spec_builder.ComponentSpecBuilder('tagger')
  tagger.set_network_unit(name='FeedForwardNetwork', hidden_layer_sizes='256')
  tagger.set_transition_system(name='tagger')
  tagger.add_fixed_feature(name='words', fml='input.word', embedding_dim=64)
  tagger.add_rnn_link(embedding_dim=-1)
  tagger.fill_from_resources(lexicon_dir)

  # Construct the ComponentSpec for parsing.
  parser = spec_builder.ComponentSpecBuilder('parser')
  parser.set_network_unit(
      name='FeedForwardNetwork',
      hidden_layer_sizes='256',
      layer_norm_hidden='True')
  parser.set_transition_system(name='arc-standard')
  parser.add_token_link(
      source=tagger,
      fml='input.focus stack.focus stack(1).focus',
      embedding_dim=32,
      source_layer='logits')

  # Recurrent connection for the arc-standard parser. For both tokens on the
  # stack, we connect to the last time step to either SHIFT or REDUCE that
  # token. This allows the parser to build up compositional representations of
  # phrases.
  parser.add_link(
      source=parser,  # recurrent connection
      name='rnn-stack',  # unique identifier
      fml='stack.focus stack(1).focus',  # look for both stack tokens
      source_translator='shift-reduce-step',  # maps token indices -> step
      embedding_dim=32)  # project down to 32 dims
  parser.fill_from_resources(lexicon_dir)

  master_spec = spec_pb2.MasterSpec()
  master_spec.component.extend([tagger.spec, parser.spec])

  hyperparam_config = spec_pb2.GridPoint()

  # Build the TensorFlow graph.
  graph = tf.Graph()
  with graph.as_default():
    builder = graph_builder.MasterBuilder(master_spec, hyperparam_config)

    target = spec_pb2.TrainTarget()
    target.name = 'all'
    target.unroll_using_oracle.extend([True, True])
    dry_run = builder.add_training_from_config(target, trace_only=True)

  # Read in serialized protos from training data.
  sentence = sentence_pb2.Sentence()
  text_format.Merge(open(training_sentence).read(), sentence)
  training_set = [sentence.SerializeToString()]

  with tf.Session(graph=graph) as sess:
    # Make sure to re-initialize all underlying state.
    sess.run(tf.initialize_all_variables())
    traces = sess.run(
        dry_run['traces'], feed_dict={dry_run['input_batch']: training_set})

  with open('dragnn_tutorial_2.html', 'w') as f:
    f.write(
        visualization.trace_html(
            traces[0], height='400px', master_spec=master_spec).encode('utf-8'))
 def _ReadFileToProto(filename):
   """Read a filename, create a protobuf from its contents."""
   ret_val = api_objects_pb2.TFAPIObject()
   text_format.Merge(file_io.read_file_to_string(filename), ret_val)
   return ret_val
Example #9
0
    def _restore(self, path):
        """Restores this estimator from given path.

        Note: will rebuild the graph and initialize all parameters,
        and will ignore provided model.

        Args:
            path: Path to checkpoints and other information.
        """
        # Currently Saver requires absolute path to work correctly.
        path = os.path.abspath(path)

        self._graph = tf.Graph()
        with self._graph.as_default():
            endpoints_filename = os.path.join(path, 'endpoints')
            if not os.path.exists(endpoints_filename):
                raise ValueError("Restore folder doesn't contain endpoints.")
            with open(endpoints_filename) as foutputs:
                endpoints = foutputs.read().split('\n')
            graph_filename = os.path.join(path, 'graph.pbtxt')
            if not os.path.exists(graph_filename):
                raise ValueError("Restore folder doesn't contain graph definition.")
            with open(graph_filename) as fgraph:
                graph_def = tf.GraphDef()
                text_format.Merge(fgraph.read(), graph_def)
                (self._inp, self._out,
                 self._model_predictions, self._model_loss) = tf.import_graph_def(
                     graph_def, name='', return_elements=endpoints)
            saver_filename = os.path.join(path, 'saver.pbtxt')
            if not os.path.exists(saver_filename):
                raise ValueError("Restore folder doesn't contain saver defintion.")
            with open(saver_filename) as fsaver:
                from tensorflow.python.training import saver_pb2
                saver_def = saver_pb2.SaverDef()
                text_format.Merge(fsaver.read(), saver_def)
                self._saver = tf.train.Saver(saver_def=saver_def)

            # Restore trainer
            self._global_step = self._graph.get_tensor_by_name('global_step:0')
            trainer_op = self._graph.get_operation_by_name('train')
            self._trainer = RestoredTrainer(
                self._model_loss, self._global_step, trainer_op)

            # Restore summaries.
            self._summaries = self._graph.get_operation_by_name('MergeSummary/MergeSummary')

            # Restore session.
            self._session = tf.Session(self.tf_master,
                                       config=tf.ConfigProto(
                                           log_device_placement=self.verbose > 1,
                                           inter_op_parallelism_threads=self.num_cores,
                                           intra_op_parallelism_threads=self.num_cores))
            checkpoint_path = tf.train.latest_checkpoint(path)
            if checkpoint_path is None:
                raise ValueError("Missing checkpoint files in the %s. Please "
                                 "make sure you are you have checkpoint file that describes "
                                 "latest checkpoints and appropriate checkpoints are there. "
                                 "If you have moved the folder, you at this point need to "
                                 "update manually update the paths in the checkpoint file." % path)
            self._saver.restore(self._session, checkpoint_path)
        # Set to be initialized.
        self._initialized = True
Example #10
0
 def testMergeEmptyText(self):
     message = unittest_pb2.TestAllTypes()
     text = ''
     text_format.Merge(text, message)
     self.assertEquals(unittest_pb2.TestAllTypes(), message)
Example #11
0
def image_classification_model_create():
    """
    Create a new ImageClassificationModelJob

    Returns JSON when requested: {job_id,name,status} or {errors:[]}
    """
    form = ImageClassificationModelForm()
    form.dataset.choices = get_datasets()
    form.standard_networks.choices = get_standard_networks()
    form.standard_networks.default = get_default_standard_network()
    form.previous_networks.choices = get_previous_networks()

    prev_network_snapshots = get_previous_network_snapshots()

    if not form.validate_on_submit():
        if request_wants_json():
            return flask.jsonify({'errors': form.errors}), 400
        else:
            return flask.render_template(
                'models/images/classification/new.html',
                form=form,
                previous_network_snapshots=prev_network_snapshots,
                multi_gpu=config_value('caffe_root')['multi_gpu'],
            ), 400

    datasetJob = scheduler.get_job(form.dataset.data)
    if not datasetJob:
        raise werkzeug.exceptions.BadRequest('Unknown dataset job_id "%s"' %
                                             form.dataset.data)

    job = None
    try:
        job = ImageClassificationModelJob(
            name=form.model_name.data,
            dataset_id=datasetJob.id(),
        )

        network = caffe_pb2.NetParameter()
        pretrained_model = None
        if form.method.data == 'standard':
            found = False
            networks_dir = os.path.join(os.path.dirname(digits.__file__),
                                        'standard-networks')
            for filename in os.listdir(networks_dir):
                path = os.path.join(networks_dir, filename)
                if os.path.isfile(path):
                    match = re.match(
                        r'%s.prototxt' % form.standard_networks.data, filename)
                    if match:
                        with open(path) as infile:
                            text_format.Merge(infile.read(), network)
                        found = True
                        break
            if not found:
                raise werkzeug.exceptions.BadRequest(
                    'Unknown standard model "%s"' %
                    form.standard_networks.data)
        elif form.method.data == 'previous':
            old_job = scheduler.get_job(form.previous_networks.data)
            if not old_job:
                raise werkzeug.exceptions.BadRequest(
                    'Job not found: %s' % form.previous_networks.data)

            network.CopyFrom(old_job.train_task().network)
            # Rename the final layer
            # XXX making some assumptions about network architecture here
            ip_layers = [l for l in network.layer if l.type == 'InnerProduct']
            if len(ip_layers) > 0:
                ip_layers[-1].name = '%s_retrain' % ip_layers[-1].name

            for choice in form.previous_networks.choices:
                if choice[0] == form.previous_networks.data:
                    epoch = float(
                        flask.request.form['%s-snapshot' %
                                           form.previous_networks.data])
                    if epoch != 0:
                        for filename, e in old_job.train_task().snapshots:
                            if e == epoch:
                                pretrained_model = filename
                                break

                        if pretrained_model is None:
                            raise werkzeug.exceptions.BadRequest(
                                "For the job %s, selected pretrained_model for epoch %d is invalid!"
                                % (form.previous_networks.data, epoch))
                        if not (os.path.exists(pretrained_model)):
                            raise werkzeug.exceptions.BadRequest(
                                "Pretrained_model for the selected epoch doesn't exists. May be deleted by another user/process. Please restart the server to load the correct pretrained_model details"
                            )
                    break

        elif form.method.data == 'custom':
            text_format.Merge(form.custom_network.data, network)
            pretrained_model = form.custom_network_snapshot.data.strip()
        else:
            raise werkzeug.exceptions.BadRequest('Unrecognized method: "%s"' %
                                                 form.method.data)

        policy = {'policy': form.lr_policy.data}
        if form.lr_policy.data == 'fixed':
            pass
        elif form.lr_policy.data == 'step':
            policy['stepsize'] = form.lr_step_size.data
            policy['gamma'] = form.lr_step_gamma.data
        elif form.lr_policy.data == 'multistep':
            policy['stepvalue'] = form.lr_multistep_values.data
            policy['gamma'] = form.lr_multistep_gamma.data
        elif form.lr_policy.data == 'exp':
            policy['gamma'] = form.lr_exp_gamma.data
        elif form.lr_policy.data == 'inv':
            policy['gamma'] = form.lr_inv_gamma.data
            policy['power'] = form.lr_inv_power.data
        elif form.lr_policy.data == 'poly':
            policy['power'] = form.lr_poly_power.data
        elif form.lr_policy.data == 'sigmoid':
            policy['stepsize'] = form.lr_sigmoid_step.data
            policy['gamma'] = form.lr_sigmoid_gamma.data
        else:
            raise werkzeug.exceptions.BadRequest(
                'Invalid learning rate policy')

        if config_value('caffe_root')['multi_gpu']:
            if form.select_gpus.data:
                selected_gpus = [str(gpu) for gpu in form.select_gpus.data]
                gpu_count = None
            elif form.select_gpu_count.data:
                gpu_count = form.select_gpu_count.data
                selected_gpus = None
            else:
                gpu_count = 1
                selected_gpus = None
        else:
            if form.select_gpu.data == 'next':
                gpu_count = 1
                selected_gpus = None
            else:
                selected_gpus = [str(form.select_gpu.data)]
                gpu_count = None

        job.tasks.append(
            tasks.CaffeTrainTask(
                job_dir=job.dir(),
                dataset=datasetJob,
                train_epochs=form.train_epochs.data,
                snapshot_interval=form.snapshot_interval.data,
                learning_rate=form.learning_rate.data,
                lr_policy=policy,
                gpu_count=gpu_count,
                selected_gpus=selected_gpus,
                batch_size=form.batch_size.data,
                val_interval=form.val_interval.data,
                pretrained_model=pretrained_model,
                crop_size=form.crop_size.data,
                use_mean=bool(form.use_mean.data),
                network=network,
                random_seed=form.random_seed.data,
                solver_type=form.solver_type.data,
            ))

        scheduler.add_job(job)
        if request_wants_json():
            return flask.jsonify(job.json_dict())
        else:
            return flask.redirect(flask.url_for('models_show',
                                                job_id=job.id()))

    except:
        if job:
            scheduler.delete_job(job)
        raise
Example #12
0
    cfg.config_path = FLAGS.config_path
    cfg.model_dir = FLAGS.model_dir
    os.makedirs(cfg.model_dir, exist_ok=True)
    cfg.multi_gpu = False
    cfg.measure_time = False
    cfg.display_step = 50

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    # directly provide a config object. this usually used
    # when you want to train with several different parameters in
    # one script.
    config = pipeline_pb2.TrainEvalPipelineConfig()
    with open(cfg.config_path, "r") as f:
        proto_str = f.read()
        text_format.Merge(proto_str, config)

    config_file_bkp = "pipeline.config"
    with open(cfg.model_dir + "/" + config_file_bkp, "w") as f:
        f.write(proto_str)

    input_cfg = config.train_input_reader
    eval_input_cfg = config.eval_input_reader
    model_cfg = config.model.second
    train_cfg = config.train_config

    net = build_network(model_cfg, measure_time=False).to(device)
    target_assigner = net.target_assigner
    voxel_generator = net.voxel_generator
    print("num parameters:", len(list(net.parameters())))
def load_expt(filename):
    fh = open(filename, 'rb')
    expt = Experiment()
    text_format.Merge(fh.read(), expt)
    fh.close()
    return expt
Example #14
0
def load_caffe_proto_model(caffe_pb2,
                           proto_path: str,
                           model_path: [str, None] = None):
    # 1. python protobuf is used
    if api_implementation._implementation_type == 'python':
        message = 'Please expect that Model Optimizer conversion might be slow. ' \
                  'You are currently using Python protobuf library implementation. \n'
        try:
            from google.protobuf.pyext import cpp_message
            # Check os windows and env variable PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION
            if os.name == 'nt' and os.environ.get(
                    'PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION',
                    default='') != 'cpp':
                # 2. cpp implementation is available but not used
                message += 'However, cpp implementation is available, you can boost ' \
                           'model conversion by setting PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION env variable to cpp. \n' \
                           'Run: set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp \n'
        except ImportError:
            # 3. cpp implementation is not available
            message += 'However you can use the C++ protobuf implementation that is supplied with the OpenVINO toolkit' \
                       'or build protobuf library from sources. \n' \
                       'Navigate to "install_prerequisites" folder and run: ' \
                       'python -m easy_install protobuf-3.5.1-py($your_python_version)-win-amd64.egg \n' \
                       'set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp'
        print(message + '\n\n' + refer_to_faq_msg(80))

    # Read proto layers
    try:
        proto = caffe_pb2.NetParameter()
        with open(proto_path, "r") as file:
            text_format.Merge(str(file.read()), proto)
    except Exception as e:
        log.error(
            'Exception message: {}\n\n'.format(e) + '    Possible reasons:\n' +
            '      1. {} does not exist\n'.format(proto_path) +
            '      2. {} does not have a valid structure, for example, it was downloaded as html\n'
            .format(proto_path) +
            '      3. {} contains custom layers or attributes that are not supported\n'
            .format(proto_path) +
            '         in Model Optimizer by default.\n\n' +
            '    After you made sure that {} has a valid structure and still see this issue, then\n'
            .format(proto_path) +
            '    you need to generate a python parser for caffe.proto that was used when the model\n'
            + '    was created.\n' +
            '    Run "python3 generate_caffe_pb2.py --input_proto ${PATH_TO_CAFFE}/src/caffe/proto/caffe.proto"'
            + refer_to_faq_msg(1) + '\n\n',
            extra={'framework_error': True})
        raise FrameworkError('Model Optimizer is not able to parse {}'.format(
            proto_path)) from e

    # Read model layer if exists
    model = None
    try:
        if model_path:
            model = caffe_pb2.NetParameter()
            with open(model_path, "rb") as infile:
                map = mmap.mmap(infile.fileno(), 0, access=mmap.ACCESS_READ)
                model.MergeFromString(map)
    except Exception as e:
        log.error(
            'Exception message: {}\n\n'.format(e) + '    Possible reasons:\n' +
            '      1. {} does not exist\n'.format(model_path) +
            '      2. {} does not have a valid structure\n'.format(model_path),
            extra={'framework_error': True})
        raise FrameworkError('Model Optimizer is not able to parse {}'.format(
            model_path)) from e

    return proto, model
Example #15
0
    def _get_model_configs_from_proto(self):
        """Creates a model text proto for testing.

    Returns:
      A dictionary of model configs.
    """

        model_text_proto = """
    [object_detection.protos.lstm_model] {
      train_unroll_length: 4
      eval_unroll_length: 4
    }
    model {
      ssd {
        feature_extractor {
          type: 'lstm_mobilenet_v1_fpn'
          conv_hyperparams {
            regularizer {
                l2_regularizer {
                }
              }
              initializer {
                truncated_normal_initializer {
                }
              }
          }
        }
        negative_class_weight: 2.0
        box_coder {
          faster_rcnn_box_coder {
          }
        }
        matcher {
          argmax_matcher {
          }
        }
        similarity_calculator {
          iou_similarity {
          }
        }
        anchor_generator {
          ssd_anchor_generator {
            aspect_ratios: 1.0
          }
        }
        image_resizer {
          fixed_shape_resizer {
            height: 32
            width: 32
          }
        }
        box_predictor {
          convolutional_box_predictor {
            conv_hyperparams {
              regularizer {
                l2_regularizer {
                }
              }
              initializer {
                truncated_normal_initializer {
                }
              }
            }
          }
        }
        normalize_loc_loss_by_codesize: true
        loss {
          classification_loss {
            weighted_softmax {
            }
          }
          localization_loss {
            weighted_smooth_l1 {
            }
          }
        }
      }
    }"""

        pipeline_config = pipeline_pb2.TrainEvalPipelineConfig()
        text_format.Merge(model_text_proto, pipeline_config)
        configs = {}
        configs['model'] = pipeline_config.model
        configs['lstm_model'] = pipeline_config.Extensions[
            internal_pipeline_pb2.lstm_model]

        return configs
Example #16
0
File: score_te.py Project: nnop/det
        iter = int(basename.split("_iter_")[1])
        if iter > max_iter:
            max_iter = iter
if max_iter == 0:
    print("Cannot find snapshot in {}".format(snapshot_dir))
    sys.exit()

# The pretrained model.
pretrain_model = "{}_iter_{}.caffemodel".format(snapshot_prefix, max_iter)
# Stores LabelMapItem.
label_map_file = "helper/labelmap.prototxt"

# MultiBoxLoss parameters.
labmap = caffe_pb2.LabelMap()
with open(label_map_file) as f:
    text_format.Merge(f.read(), labmap)
num_classes = len(labmap.item)
share_location = True
background_label_id = 0
train_on_diff_gt = True
normalization_mode = P.Loss.VALID
code_type = P.PriorBox.CENTER_SIZE
ignore_cross_boundary_bbox = False
mining_type = P.MultiBoxLoss.MAX_NEGATIVE
neg_pos_ratio = 3.
loc_weight = (neg_pos_ratio + 1.) / 4.
multibox_loss_param = {
    'loc_loss_type': P.MultiBoxLoss.SMOOTH_L1,
    'conf_loss_type': P.MultiBoxLoss.SOFTMAX,
    'loc_weight': loc_weight,
    'num_classes': num_classes,
Example #17
0
def main(unused_argv):
    """Main entry point for SDK Fn Harness."""
    if 'LOGGING_API_SERVICE_DESCRIPTOR' in os.environ:
        try:
            logging_service_descriptor = endpoints_pb2.ApiServiceDescriptor()
            text_format.Merge(os.environ['LOGGING_API_SERVICE_DESCRIPTOR'],
                              logging_service_descriptor)

            # Send all logs to the runner.
            fn_log_handler = FnApiLogRecordHandler(logging_service_descriptor)
            # TODO(BEAM-5468): This should be picked up from pipeline options.
            logging.getLogger().setLevel(logging.INFO)
            logging.getLogger().addHandler(fn_log_handler)
            _LOGGER.info('Logging handler created.')
        except Exception:
            _LOGGER.error(
                "Failed to set up logging handler, continuing without.",
                exc_info=True)
            fn_log_handler = None
    else:
        fn_log_handler = None

    # Start status HTTP server thread.
    thread = threading.Thread(name='status_http_server',
                              target=StatusServer().start)
    thread.daemon = True
    thread.setName('status-server-demon')
    thread.start()

    if 'PIPELINE_OPTIONS' in os.environ:
        sdk_pipeline_options = _parse_pipeline_options(
            os.environ['PIPELINE_OPTIONS'])
    else:
        sdk_pipeline_options = PipelineOptions.from_dictionary({})

    if 'SEMI_PERSISTENT_DIRECTORY' in os.environ:
        semi_persistent_directory = os.environ['SEMI_PERSISTENT_DIRECTORY']
    else:
        semi_persistent_directory = None

    _LOGGER.info('semi_persistent_directory: %s', semi_persistent_directory)
    _worker_id = os.environ.get('WORKER_ID', None)

    try:
        _load_main_session(semi_persistent_directory)
    except Exception:  # pylint: disable=broad-except
        exception_details = traceback.format_exc()
        _LOGGER.error('Could not load main session: %s',
                      exception_details,
                      exc_info=True)

    try:
        _LOGGER.info('Python sdk harness started with pipeline_options: %s',
                     sdk_pipeline_options.get_all_options(drop_default=True))
        service_descriptor = endpoints_pb2.ApiServiceDescriptor()
        text_format.Merge(os.environ['CONTROL_API_SERVICE_DESCRIPTOR'],
                          service_descriptor)
        # TODO(robertwb): Support credentials.
        assert not service_descriptor.oauth2_client_credentials_grant.url
        SdkHarness(
            control_address=service_descriptor.url,
            worker_id=_worker_id,
            state_cache_size=_get_state_cache_size(sdk_pipeline_options),
            profiler_factory=profiler.Profile.factory_from_options(
                sdk_pipeline_options.view_as(ProfilingOptions))).run()
        _LOGGER.info('Python sdk harness exiting.')
    except:  # pylint: disable=broad-except
        _LOGGER.exception('Python sdk harness failed: ')
        raise
    finally:
        if fn_log_handler:
            fn_log_handler.close()
Example #18
0
def evaluate(config_path,
             model_dir=None,
             result_path=None,
             ckpt_path=None,
             measure_time=False,
             batch_size=None):
    """Don't support pickle_result anymore. if you want to generate kitti label file,
    please use kitti_anno_to_label_file in second.data.kitti_dataset.
    """
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    result_name = 'eval_results'
    if result_path is None:
        model_dir = pathlib.Path(model_dir)
        result_path = model_dir / result_name
    else:
        result_path = pathlib.Path(result_path)
    if isinstance(config_path, str):
        # directly provide a config object. this usually used
        # when you want to eval with several different parameters in
        # one script.
        config = pipeline_pb2.TrainEvalPipelineConfig()
        with open(config_path, "r") as f:
            proto_str = f.read()
            text_format.Merge(proto_str, config)
    else:
        config = config_path

    input_cfg = config.eval_input_reader
    model_cfg = config.model.second
    train_cfg = config.train_config

    center_limit_range = model_cfg.post_center_limit_range
    ######################
    # BUILD VOXEL GENERATOR
    ######################
    net = build_network(model_cfg, measure_time=measure_time).to(device)
    if train_cfg.enable_mixed_precision:
        net.half()
        print("half inference!")
        net.metrics_to_float()
        net.convert_norm_to_float(net)
    target_assigner = net.target_assigner
    voxel_generator = net.voxel_generator
    class_names = target_assigner.classes

    if ckpt_path is None:
        assert model_dir is not None
        torchplus.train.try_restore_latest_checkpoints(model_dir, [net])
    else:
        torchplus.train.restore(ckpt_path, net)

    batch_size = batch_size or input_cfg.batch_size
    eval_dataset = input_reader_builder.build(input_cfg,
                                              model_cfg,
                                              training=False,
                                              voxel_generator=voxel_generator,
                                              target_assigner=target_assigner)
    eval_dataloader = torch.utils.data.DataLoader(
        eval_dataset,
        batch_size=batch_size,
        shuffle=False,
        num_workers=0,  # input_cfg.num_workers,
        pin_memory=False,
        collate_fn=merge_second_batch)

    if train_cfg.enable_mixed_precision:
        float_dtype = torch.float16
    else:
        float_dtype = torch.float32

    net.eval()
    result_path_step = result_path / f"step_{net.get_global_step()}"
    result_path_step.mkdir(parents=True, exist_ok=True)
    t = time.time()
    detections = []
    print("Generate output labels...")
    bar = ProgressBar()
    bar.start((len(eval_dataset) + batch_size - 1) // batch_size)
    prep_example_times = []
    prep_times = []
    t2 = time.time()

    for example in iter(eval_dataloader):
        if measure_time:
            prep_times.append(time.time() - t2)
            torch.cuda.synchronize()
            t1 = time.time()
        example = example_convert_to_torch(example, float_dtype)
        if measure_time:
            torch.cuda.synchronize()
            prep_example_times.append(time.time() - t1)
        detections += net(example)
        bar.print_bar()
        if measure_time:
            t2 = time.time()

    sec_per_example = len(eval_dataset) / (time.time() - t)
    print(f'generate label finished({sec_per_example:.2f}/s). start eval:')
    if measure_time:
        print(
            f"avg example to torch time: {np.mean(prep_example_times) * 1000:.3f} ms"
        )
        print(f"avg prep time: {np.mean(prep_times) * 1000:.3f} ms")
    for name, val in net.get_avg_time_dict().items():
        print(f"avg {name} time = {val * 1000:.3f} ms")
    with open(result_path_step / "result.pkl", 'wb') as f:
        pickle.dump(detections, f)
    """
    with open(result_path_step / "result.pkl", 'rb') as f:
        detections = pickle.load(f)
    print(detections[0].keys())
    """
    result_dict = eval_dataset.dataset.evaluation(detections,
                                                  str(result_path_step))
    if result_dict is not None:
        for k, v in result_dict["results"].items():
            print("Evaluation {}".format(k))
            print(v)
        # metric dict: class -> metric type -> diff -> overlap -> recall, prec, ...
    return detections
Example #19
0
 def load(self):
     '''Load the layer definitions from the prototxt.'''
     self.params = get_caffe_resolver().NetParameter()
     with open(self.def_path, 'rb') as def_file:
         text_format.Merge(def_file.read(), self.params)
Example #20
0
def train(config_path,
          model_dir,
          result_path=None,
          create_folder=False,
          display_step=50,
          summary_step=5,
          resume=False):
    """train a VoxelNet model specified by a config file.
    """
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    if create_folder:
        if pathlib.Path(model_dir).exists():
            model_dir = torchplus.train.create_folder(model_dir)
    model_dir = pathlib.Path(model_dir)
    if not resume and model_dir.exists():
        raise ValueError("model dir exists and you don't specify resume.")
    model_dir.mkdir(parents=True, exist_ok=True)
    if result_path is None:
        result_path = model_dir / 'results'
    config_file_bkp = "pipeline.config"
    if isinstance(config_path, str):
        # directly provide a config object. this usually used
        # when you want to train with several different parameters in
        # one script.
        config = pipeline_pb2.TrainEvalPipelineConfig()
        with open(config_path, "r") as f:
            proto_str = f.read()
            text_format.Merge(proto_str, config)
    else:
        config = config_path
        proto_str = text_format.MessageToString(config, indent=2)
    with (model_dir / config_file_bkp).open("w") as f:
        f.write(proto_str)

    input_cfg = config.train_input_reader
    eval_input_cfg = config.eval_input_reader
    model_cfg = config.model.second
    train_cfg = config.train_config

    net = build_network(model_cfg).to(device)
    if train_cfg.enable_mixed_precision:
        net.half()
        net.metrics_to_float()
        net.convert_norm_to_float(net)
    target_assigner = net.target_assigner
    voxel_generator = net.voxel_generator
    class_names = target_assigner.classes

    # net_train = torch.nn.DataParallel(net).cuda()
    print("num_trainable parameters:", len(list(net.parameters())))
    # for n, p in net.named_parameters():
    #     print(n, p.shape)
    ######################
    # BUILD OPTIMIZER
    ######################
    # we need global_step to create lr_scheduler, so restore net first.
    torchplus.train.try_restore_latest_checkpoints(model_dir, [net])
    gstep = net.get_global_step() - 1
    optimizer_cfg = train_cfg.optimizer
    loss_scale = train_cfg.loss_scale_factor
    mixed_optimizer = optimizer_builder.build(
        optimizer_cfg,
        net,
        mixed=train_cfg.enable_mixed_precision,
        loss_scale=loss_scale)
    optimizer = mixed_optimizer
    center_limit_range = model_cfg.post_center_limit_range
    """
    if train_cfg.enable_mixed_precision:
        mixed_optimizer = torchplus.train.MixedPrecisionWrapper(
            optimizer, loss_scale)
    else:
        mixed_optimizer = optimizer
    """
    # must restore optimizer AFTER using MixedPrecisionWrapper
    torchplus.train.try_restore_latest_checkpoints(model_dir,
                                                   [mixed_optimizer])
    lr_scheduler = lr_scheduler_builder.build(optimizer_cfg, optimizer,
                                              train_cfg.steps)
    if train_cfg.enable_mixed_precision:
        float_dtype = torch.float16
    else:
        float_dtype = torch.float32
    ######################
    # PREPARE INPUT
    ######################
    dataset = input_reader_builder.build(input_cfg,
                                         model_cfg,
                                         training=True,
                                         voxel_generator=voxel_generator,
                                         target_assigner=target_assigner)
    eval_dataset = input_reader_builder.build(eval_input_cfg,
                                              model_cfg,
                                              training=False,
                                              voxel_generator=voxel_generator,
                                              target_assigner=target_assigner)

    dataloader = torch.utils.data.DataLoader(
        dataset,
        batch_size=input_cfg.batch_size,
        shuffle=True,
        num_workers=input_cfg.preprocess.num_workers,
        pin_memory=False,
        collate_fn=merge_second_batch,
        worker_init_fn=_worker_init_fn)
    eval_dataloader = torch.utils.data.DataLoader(
        eval_dataset,
        batch_size=eval_input_cfg.batch_size,
        shuffle=False,
        num_workers=eval_input_cfg.preprocess.num_workers,
        pin_memory=False,
        collate_fn=merge_second_batch)

    data_iter = iter(dataloader)

    ######################
    # TRAINING
    ######################
    model_logging = SimpleModelLog(model_dir)
    model_logging.open()
    model_logging.log_text(proto_str + "\n", 0, tag="config")

    total_step_elapsed = 0
    remain_steps = train_cfg.steps - net.get_global_step()
    t = time.time()
    ckpt_start_time = t

    total_loop = train_cfg.steps // train_cfg.steps_per_eval + 1
    # total_loop = remain_steps // train_cfg.steps_per_eval + 1
    clear_metrics_every_epoch = train_cfg.clear_metrics_every_epoch

    if train_cfg.steps % train_cfg.steps_per_eval == 0:
        total_loop -= 1
    mixed_optimizer.zero_grad()
    try:
        for _ in range(total_loop):
            if total_step_elapsed + train_cfg.steps_per_eval > train_cfg.steps:
                steps = train_cfg.steps % train_cfg.steps_per_eval
            else:
                steps = train_cfg.steps_per_eval
            for step in range(steps):
                lr_scheduler.step(net.get_global_step())
                try:
                    example = next(data_iter)
                except StopIteration:
                    print("end epoch")
                    if clear_metrics_every_epoch:
                        net.clear_metrics()
                    data_iter = iter(dataloader)
                    example = next(data_iter)
                example_torch = example_convert_to_torch(example, float_dtype)

                batch_size = example["anchors"].shape[0]

                ret_dict = net(example_torch)

                # box_preds = ret_dict["box_preds"]
                cls_preds = ret_dict["cls_preds"]
                loss = ret_dict["loss"].mean()
                cls_loss_reduced = ret_dict["cls_loss_reduced"].mean()
                loc_loss_reduced = ret_dict["loc_loss_reduced"].mean()
                cls_pos_loss = ret_dict["cls_pos_loss"]
                cls_neg_loss = ret_dict["cls_neg_loss"]
                loc_loss = ret_dict["loc_loss"]
                cls_loss = ret_dict["cls_loss"]
                dir_loss_reduced = ret_dict["dir_loss_reduced"]
                cared = ret_dict["cared"]
                labels = example_torch["labels"]
                if train_cfg.enable_mixed_precision:
                    loss *= loss_scale
                loss.backward()
                torch.nn.utils.clip_grad_norm_(net.parameters(), 10.0)
                mixed_optimizer.step()
                mixed_optimizer.zero_grad()
                net.update_global_step()
                net_metrics = net.update_metrics(cls_loss_reduced,
                                                 loc_loss_reduced, cls_preds,
                                                 labels, cared)

                step_time = (time.time() - t)
                t = time.time()
                metrics = {}
                num_pos = int((labels > 0)[0].float().sum().cpu().numpy())
                num_neg = int((labels == 0)[0].float().sum().cpu().numpy())
                if 'anchors_mask' not in example_torch:
                    num_anchors = example_torch['anchors'].shape[1]
                else:
                    num_anchors = int(example_torch['anchors_mask'][0].sum())
                global_step = net.get_global_step()
                if global_step % display_step == 0:
                    loc_loss_elem = [
                        float(loc_loss[:, :, i].sum().detach().cpu().numpy() /
                              batch_size) for i in range(loc_loss.shape[-1])
                    ]
                    metrics["runtime"] = {
                        "step": global_step,
                        "steptime": step_time,
                    }
                    metrics.update(net_metrics)
                    metrics["loss"]["loc_elem"] = loc_loss_elem
                    metrics["loss"]["cls_pos_rt"] = float(
                        cls_pos_loss.detach().cpu().numpy())
                    metrics["loss"]["cls_neg_rt"] = float(
                        cls_neg_loss.detach().cpu().numpy())
                    if model_cfg.use_direction_classifier:
                        metrics["loss"]["dir_rt"] = float(
                            dir_loss_reduced.detach().cpu().numpy())

                    metrics["misc"] = {
                        "num_vox": int(example_torch["voxels"].shape[0]),
                        "num_pos": int(num_pos),
                        "num_neg": int(num_neg),
                        "num_anchors": int(num_anchors),
                        "lr": float(optimizer.lr),
                    }
                    # metrics["lr"] = float(
                    #     mixed_optimizer.param_groups[0]['lr'])
                    model_logging.log_metrics(metrics, global_step)
                ckpt_elasped_time = time.time() - ckpt_start_time
                if ckpt_elasped_time > train_cfg.save_checkpoints_secs:
                    torchplus.train.save_models(model_dir, [net, optimizer],
                                                net.get_global_step())
                    ckpt_start_time = time.time()
            total_step_elapsed += steps
            torchplus.train.save_models(model_dir, [net, optimizer],
                                        net.get_global_step())
            net.eval()
            result_path_step = result_path / f"step_{net.get_global_step()}"
            result_path_step.mkdir(parents=True, exist_ok=True)
            model_logging.log_text("#################################",
                                   global_step)
            model_logging.log_text("# EVAL", global_step)
            model_logging.log_text("#################################",
                                   global_step)
            model_logging.log_text("Generate output labels...", global_step)
            t = time.time()
            detections = []
            prog_bar = ProgressBar()
            net.clear_timer()
            prog_bar.start(
                (len(eval_dataset) + eval_input_cfg.batch_size - 1) //
                eval_input_cfg.batch_size)
            for example in iter(eval_dataloader):
                example = example_convert_to_torch(example, float_dtype)
                detections += net(example)
                prog_bar.print_bar()

            sec_per_ex = len(eval_dataset) / (time.time() - t)
            model_logging.log_text(
                f'generate label finished({sec_per_ex:.2f}/s). start eval:',
                global_step)
            result_dict = eval_dataset.dataset.evaluation(
                detections, str(result_path_step))
            for k, v in result_dict["results"].items():
                model_logging.log_text("Evaluation {}".format(k), global_step)
                model_logging.log_text(v, global_step)
            model_logging.log_metrics(result_dict["detail"], global_step)
            with open(result_path_step / "result.pkl", 'wb') as f:
                pickle.dump(detections, f)
            net.train()
    except Exception as e:
        torchplus.train.save_models(model_dir, [net, optimizer],
                                    net.get_global_step())
        raise e
    finally:
        model_logging.close()
    # save model before exit
    torchplus.train.save_models(model_dir, [net, optimizer],
                                net.get_global_step())
def main():
    global THREAD_RUNNING
    args = parse_args()
    print('Called with args:')
    print(args)

    if not os.path.isfile(args.caffe_prototxt):
        sys.exit('File not found: {}'.format(args.caffe_prototxt))
    if not os.path.isfile(args.caffe_model):
        sys.exit('File not found: {}'.format(args.caffe_model))
    if not os.path.isfile(args.labelmap_file):
        sys.exit('File not found: {}'.format(args.labelmap_file))

    # Initialize Caffe
    if args.cpu_mode:
        print('Running Caffe in CPU mode')
        caffe.set_mode_cpu()
    else:
        print('Running Caffe in GPU mode')
        caffe.set_device(0)
        caffe.set_mode_gpu()
    net = caffe.Net(args.caffe_prototxt, args.caffe_model, caffe.TEST)

    # Build the class (index/name) dictionary from labelmap file
    lm_handle = open(args.labelmap_file, 'r')
    lm_map = caffe_pb2.LabelMap()
    text_format.Merge(str(lm_handle.read()), lm_map)
    cls_dict = {x.label: x.display_name for x in lm_map.item}

    # Open camera
    if args.use_file:
        cap = cv2.VideoCapture(args.filename)
        # ignore image width/height settings here
    elif args.use_rtsp:
        cap = open_cam_rtsp(args.rtsp_uri, args.image_width, args.image_height,
                            args.rtsp_latency)
    elif args.use_usb:
        cap = open_cam_usb(args.video_dev, args.image_width, args.image_height)
    else:  # By default, use the Jetson onboard camera
        cap = open_cam_onboard(args.image_width, args.image_height)

    if not cap.isOpened():
        sys.exit('Failed to open camera!')

    ret, frame = cap.read()
    time.sleep(5)
    cv2.imwrite("image.png", frame, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])
    # time.sleep(5)
    img = frame
    cv2.namedWindow("image")
    cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN)
    cv2.imshow("image", img)

    # Start the sub-thread, which is responsible for grabbing images
    THREAD_RUNNING = True
    th = threading.Thread(target=grab_img, args=(cap, ))
    th.start()

    # Grab image and do object detection (until stopped by user)
    open_window(args.image_width, args.image_height)
    read_cam_and_detect(net, cls_dict, args.conf_th)

    # Terminate the sub-thread
    THREAD_RUNNING = False
    th.join()

    cap.release()
    if cv2.waitKey(0) & 0xff == 27:
        cv2.destroyAllWindows()
    cv2.destroyAllWindows()
Example #22
0
    def testStruct(self):
        struct = struct_pb2.Struct()
        self.assertIsInstance(struct, collections.Mapping)
        self.assertEqual(0, len(struct))
        struct_class = struct.__class__

        struct['key1'] = 5
        struct['key2'] = 'abc'
        struct['key3'] = True
        struct.get_or_create_struct('key4')['subkey'] = 11.0
        struct_list = struct.get_or_create_list('key5')
        self.assertIsInstance(struct_list, collections.Sequence)
        struct_list.extend([6, 'seven', True, False, None])
        struct_list.add_struct()['subkey2'] = 9
        struct['key6'] = {'subkey': {}}
        struct['key7'] = [2, False]

        self.assertEqual(7, len(struct))
        self.assertTrue(isinstance(struct, well_known_types.Struct))
        self.assertEqual(5, struct['key1'])
        self.assertEqual('abc', struct['key2'])
        self.assertIs(True, struct['key3'])
        self.assertEqual(11, struct['key4']['subkey'])
        inner_struct = struct_class()
        inner_struct['subkey2'] = 9
        self.assertEqual([6, 'seven', True, False, None, inner_struct],
                         list(struct['key5'].items()))
        self.assertEqual({}, dict(struct['key6']['subkey'].fields))
        self.assertEqual([2, False], list(struct['key7'].items()))

        serialized = struct.SerializeToString()
        struct2 = struct_pb2.Struct()
        struct2.ParseFromString(serialized)

        self.assertEqual(struct, struct2)
        for key, value in struct.items():
            self.assertIn(key, struct)
            self.assertIn(key, struct2)
            self.assertEqual(value, struct2[key])

        self.assertEqual(7, len(struct.keys()))
        self.assertEqual(7, len(struct.values()))
        for key in struct.keys():
            self.assertIn(key, struct)
            self.assertIn(key, struct2)
            self.assertEqual(struct[key], struct2[key])

        item = (next(iter(struct.keys())), next(iter(struct.values())))
        self.assertEqual(item, next(iter(struct.items())))

        self.assertTrue(isinstance(struct2, well_known_types.Struct))
        self.assertEqual(5, struct2['key1'])
        self.assertEqual('abc', struct2['key2'])
        self.assertIs(True, struct2['key3'])
        self.assertEqual(11, struct2['key4']['subkey'])
        self.assertEqual([6, 'seven', True, False, None, inner_struct],
                         list(struct2['key5'].items()))

        struct_list = struct2['key5']
        self.assertEqual(6, struct_list[0])
        self.assertEqual('seven', struct_list[1])
        self.assertEqual(True, struct_list[2])
        self.assertEqual(False, struct_list[3])
        self.assertEqual(None, struct_list[4])
        self.assertEqual(inner_struct, struct_list[5])

        struct_list[1] = 7
        self.assertEqual(7, struct_list[1])

        struct_list.add_list().extend([1, 'two', True, False, None])
        self.assertEqual([1, 'two', True, False, None],
                         list(struct_list[6].items()))
        struct_list.extend([{
            'nested_struct': 30
        }, ['nested_list', 99], {}, []])
        self.assertEqual(11, len(struct_list.values))
        self.assertEqual(30, struct_list[7]['nested_struct'])
        self.assertEqual('nested_list', struct_list[8][0])
        self.assertEqual(99, struct_list[8][1])
        self.assertEqual({}, dict(struct_list[9].fields))
        self.assertEqual([], list(struct_list[10].items()))
        struct_list[0] = {'replace': 'set'}
        struct_list[1] = ['replace', 'set']
        self.assertEqual('set', struct_list[0]['replace'])
        self.assertEqual(['replace', 'set'], list(struct_list[1].items()))

        text_serialized = str(struct)
        struct3 = struct_pb2.Struct()
        text_format.Merge(text_serialized, struct3)
        self.assertEqual(struct, struct3)

        struct.get_or_create_struct('key3')['replace'] = 12
        self.assertEqual(12, struct['key3']['replace'])

        # Tests empty list.
        struct.get_or_create_list('empty_list')
        empty_list = struct['empty_list']
        self.assertEqual([], list(empty_list.items()))
        list2 = struct_pb2.ListValue()
        list2.add_list()
        empty_list = list2[0]
        self.assertEqual([], list(empty_list.items()))

        # Tests empty struct.
        struct.get_or_create_struct('empty_struct')
        empty_struct = struct['empty_struct']
        self.assertEqual({}, dict(empty_struct.fields))
        list2.add_struct()
        empty_struct = list2[1]
        self.assertEqual({}, dict(empty_struct.fields))

        self.assertEqual(9, len(struct))
        del struct['key3']
        del struct['key4']
        self.assertEqual(7, len(struct))
        self.assertEqual(6, len(struct['key5']))
        del struct['key5'][1]
        self.assertEqual(5, len(struct['key5']))
        self.assertEqual([6, True, False, None, inner_struct],
                         list(struct['key5'].items()))
Example #23
0
def savearray(a, filename, fmt='png'):
    a = np.uint8(np.clip(a, 0, 255))
    with open(filename, 'wb') as f:
        PIL.Image.fromarray(a).save(f, fmt)
        #display(Image(data=f.getvalue()))


model_path = '/home/vagrant/caffe/models/bvlc_googlenet/'  # substitute your path here
net_fn = model_path + 'deploy.prototxt'
param_fn = model_path + 'bvlc_googlenet.caffemodel'

# Patching model to be able to compute gradients.
# Note that you can also manually add "force_backward: true" line to "deploy.prototxt".
model = caffe.io.caffe_pb2.NetParameter()
text_format.Merge(open(net_fn).read(), model)
model.force_backward = True
open('tmp.prototxt', 'w').write(str(model))

net = caffe.Classifier(
    'tmp.prototxt',
    param_fn,
    mean=np.float32([104.0, 116.0,
                     122.0]),  # ImageNet mean, training set dependent
    channel_swap=(
        2, 1,
        0))  # the reference model has channels in BGR order instead of RGB


# a couple of utility functions for converting to and from Caffe's input image layout
def preprocess(net, img):
Example #24
0
    # Make sure protobuf Python implementation is built
    host = subprocess.check_output('hostname').strip('\n1234567890')
    protoc = lbann_dir + '/build/gnu.Release.' + host + '.llnl.gov/install/bin/protoc'
    proto_python_dir = lbann_dir + '/build/gnu.Release.' + host + '.llnl.gov/protobuf/src/python'
    os.putenv('PROTOC', protoc)
    subprocess.call('cd ' + proto_python_dir + '; ' + sys.executable + ' ' +
                    proto_python_dir + '/setup.py build',
                    shell=True)
    sys.path.append(proto_python_dir)
    import google.protobuf.text_format as txtf

    # Compile LBANN protobuf
    subprocess.call([
        protoc, '-I=' + lbann_proto_dir, '--python_out=' + work_dir,
        lbann_proto_dir + '/lbann.proto'
    ])
    sys.path.append(work_dir)
    global lbann_pb2
    import lbann_pb2

    # Load template prototext
    with open(template_proto, 'r') as f:
        pb = txtf.Merge(f.read(), lbann_pb2.LbannPB())

    # Configure prototext model
    configure_model(pb.model)

    # Export prototext
    with open(output_proto, 'w') as f:
        f.write(txtf.MessageToString(pb))
  def test_create_center_net_model(self, customize_head_params):
    """Test building a CenterNet model from proto txt."""
    proto_txt = """
      center_net {
        num_classes: 10
        feature_extractor {
          type: "hourglass_52"
          channel_stds: [4, 5, 6]
          bgr_ordering: true
        }
        image_resizer {
          keep_aspect_ratio_resizer {
            min_dimension: 512
            max_dimension: 512
            pad_to_max_dimension: true
          }
        }
      }
    """
    # Set up the configuration proto.
    config = text_format.Merge(proto_txt, model_pb2.DetectionModel())
    config.center_net.object_center_params.CopyFrom(
        self.get_fake_object_center_proto(
            customize_head_params=customize_head_params))
    config.center_net.object_detection_task.CopyFrom(
        self.get_fake_object_detection_proto(
            customize_head_params=customize_head_params))
    config.center_net.keypoint_estimation_task.append(
        self.get_fake_keypoint_proto(
            customize_head_params=customize_head_params))
    config.center_net.keypoint_label_map_path = (
        self.get_fake_label_map_file_path())
    config.center_net.mask_estimation_task.CopyFrom(
        self.get_fake_mask_proto(
            customize_head_params=customize_head_params))
    config.center_net.densepose_estimation_task.CopyFrom(
        self.get_fake_densepose_proto())

    # Build the model from the configuration.
    model = model_builder.build(config, is_training=True)

    # Check object center related parameters.
    self.assertEqual(model._num_classes, 10)
    self.assertIsInstance(model._center_params.classification_loss,
                          losses.PenaltyReducedLogisticFocalLoss)
    self.assertEqual(model._center_params.classification_loss._alpha, 3.0)
    self.assertEqual(model._center_params.classification_loss._beta, 4.0)
    self.assertAlmostEqual(model._center_params.min_box_overlap_iou, 0.2)
    self.assertAlmostEqual(
        model._center_params.heatmap_bias_init, 3.14, places=4)
    self.assertEqual(model._center_params.max_box_predictions, 15)
    if customize_head_params:
      self.assertEqual(model._center_params.center_head_num_filters, [64, 32])
      self.assertEqual(model._center_params.center_head_kernel_sizes, [5, 3])
    else:
      self.assertEqual(model._center_params.center_head_num_filters, [256])
      self.assertEqual(model._center_params.center_head_kernel_sizes, [3])
    self.assertEqual(model._center_params.peak_max_pool_kernel_size, 5)

    # Check object detection related parameters.
    self.assertAlmostEqual(model._od_params.offset_loss_weight, 0.1)
    self.assertAlmostEqual(model._od_params.scale_loss_weight, 0.2)
    self.assertAlmostEqual(model._od_params.task_loss_weight, 0.5)
    self.assertIsInstance(model._od_params.localization_loss,
                          losses.L1LocalizationLoss)
    self.assertEqual(model._od_params.offset_head_num_filters, [256])
    self.assertEqual(model._od_params.offset_head_kernel_sizes, [3])
    if customize_head_params:
      self.assertEqual(model._od_params.scale_head_num_filters, [128, 64])
      self.assertEqual(model._od_params.scale_head_kernel_sizes, [5, 3])
    else:
      self.assertEqual(model._od_params.scale_head_num_filters, [256])
      self.assertEqual(model._od_params.scale_head_kernel_sizes, [3])

    # Check keypoint estimation related parameters.
    kp_params = model._kp_params_dict['human_pose']
    self.assertAlmostEqual(kp_params.task_loss_weight, 0.9)
    self.assertAlmostEqual(kp_params.keypoint_regression_loss_weight, 1.0)
    self.assertAlmostEqual(kp_params.keypoint_offset_loss_weight, 0.5)
    self.assertAlmostEqual(kp_params.heatmap_bias_init, 2.14, places=4)
    self.assertEqual(kp_params.classification_loss._alpha, 3.0)
    self.assertEqual(kp_params.keypoint_indices, [0, 1, 2, 3])
    self.assertEqual(kp_params.keypoint_labels,
                     ['nose', 'left_shoulder', 'right_shoulder', 'hip'])
    self.assertAllClose(kp_params.keypoint_std_dev, [0.3, 1.0, 1.0, 0.0])
    self.assertEqual(kp_params.classification_loss._beta, 4.0)
    self.assertIsInstance(kp_params.localization_loss,
                          losses.L1LocalizationLoss)
    self.assertAlmostEqual(kp_params.keypoint_candidate_score_threshold, 0.3)
    self.assertEqual(kp_params.num_candidates_per_keypoint, 12)
    self.assertEqual(kp_params.peak_max_pool_kernel_size, 5)
    self.assertAlmostEqual(kp_params.unmatched_keypoint_score, 0.05)
    self.assertAlmostEqual(kp_params.box_scale, 1.7)
    self.assertAlmostEqual(kp_params.candidate_search_scale, 0.2)
    self.assertEqual(kp_params.candidate_ranking_mode, 'score_distance_ratio')
    self.assertEqual(kp_params.offset_peak_radius, 3)
    self.assertEqual(kp_params.per_keypoint_offset, True)
    self.assertEqual(kp_params.predict_depth, True)
    self.assertEqual(kp_params.per_keypoint_depth, True)
    self.assertAlmostEqual(kp_params.keypoint_depth_loss_weight, 0.3)
    self.assertAlmostEqual(kp_params.score_distance_multiplier, 11.0)
    self.assertAlmostEqual(kp_params.std_dev_multiplier, 2.8)
    self.assertAlmostEqual(kp_params.rescoring_threshold, 0.5)
    if customize_head_params:
      # Set by the config.
      self.assertEqual(kp_params.heatmap_head_num_filters, [64, 32])
      self.assertEqual(kp_params.heatmap_head_kernel_sizes, [5, 3])
      self.assertEqual(kp_params.offset_head_num_filters, [128, 64])
      self.assertEqual(kp_params.offset_head_kernel_sizes, [5, 3])
    else:
      # Default values:
      self.assertEqual(kp_params.heatmap_head_num_filters, [256])
      self.assertEqual(kp_params.heatmap_head_kernel_sizes, [3])
      self.assertEqual(kp_params.offset_head_num_filters, [256])
      self.assertEqual(kp_params.offset_head_kernel_sizes, [3])
    self.assertAlmostEqual(kp_params.gaussian_denom_ratio, 0.3)
    self.assertEqual(kp_params.argmax_postprocessing, True)

    # Check mask related parameters.
    self.assertAlmostEqual(model._mask_params.task_loss_weight, 0.7)
    self.assertIsInstance(model._mask_params.classification_loss,
                          losses.WeightedSoftmaxClassificationLoss)
    self.assertEqual(model._mask_params.mask_height, 8)
    self.assertEqual(model._mask_params.mask_width, 8)
    self.assertAlmostEqual(model._mask_params.score_threshold, 0.7)
    self.assertAlmostEqual(
        model._mask_params.heatmap_bias_init, -2.0, places=4)
    if customize_head_params:
      self.assertEqual(model._mask_params.mask_head_num_filters, [128, 64])
      self.assertEqual(model._mask_params.mask_head_kernel_sizes, [5, 3])
    else:
      self.assertEqual(model._mask_params.mask_head_num_filters, [256])
      self.assertEqual(model._mask_params.mask_head_kernel_sizes, [3])

    # Check DensePose related parameters.
    self.assertEqual(model._densepose_params.class_id, 0)
    self.assertIsInstance(model._densepose_params.classification_loss,
                          losses.WeightedSoftmaxClassificationLoss)
    self.assertIsInstance(model._densepose_params.localization_loss,
                          losses.L1LocalizationLoss)
    self.assertAlmostEqual(model._densepose_params.part_loss_weight, 1.0)
    self.assertAlmostEqual(model._densepose_params.coordinate_loss_weight, 2.0)
    self.assertEqual(model._densepose_params.num_parts, 24)
    self.assertAlmostEqual(model._densepose_params.task_loss_weight, 0.5)
    self.assertTrue(model._densepose_params.upsample_to_input_res)
    self.assertEqual(model._densepose_params.upsample_method, 'bilinear')
    self.assertAlmostEqual(
        model._densepose_params.heatmap_bias_init, -2.0, places=4)

    # Check feature extractor parameters.
    self.assertIsInstance(
        model._feature_extractor, center_net_hourglass_feature_extractor
        .CenterNetHourglassFeatureExtractor)
    self.assertAllClose(model._feature_extractor._channel_means, [0, 0, 0])
    self.assertAllClose(model._feature_extractor._channel_stds, [4, 5, 6])
    self.assertTrue(model._feature_extractor._bgr_ordering)
    backbone = model._feature_extractor._network
    self.assertIsInstance(backbone, hourglass_network.HourglassNetwork)
    self.assertTrue(backbone.num_hourglasses, 1)
Example #26
0
def tuneModelDefinition(model_path, iteration):
    working_dir = sys.path[0]
    caffe_path = os.path.join(working_dir, "..", "..", "build", "tools",
                              "caffe")
    if not os.path.exists(caffe_path):
        print "Caffe binary does not exist; please build Caffe binary first."
        sys, exit(1)

    base_model_name = os.path.basename(model_path)
    model_dir = os.path.dirname(model_path)
    winograd_model_name = base_model_name.split(".")[0] + "_winograd.prototxt"
    winograd_model_path = os.path.join(model_dir, winograd_model_name)
    direct_model_name = base_model_name.split(".")[0] + "_direct.prototxt"
    direct_model_path = os.path.join(model_dir, direct_model_name)

    base_net = caffe_pb2.NetParameter()
    with open(model_path) as f:
        s = f.read()
        txtf.Merge(s, base_net)

    direct_net = copy.deepcopy(base_net)
    for index in range(0, len(direct_net.layer)):
        l = direct_net.layer[index]
        if l.type == "Convolution":
            l.convolution_param.conv_algorithm = "direct"

    with open(direct_model_path, "w") as f:
        f.write(txtf.MessageToString(direct_net, float_format=".17g"))

    winograd_net = copy.deepcopy(base_net)
    for index in range(0, len(winograd_net.layer)):
        l = winograd_net.layer[index]
        if l.type == "Convolution":
            l.convolution_param.conv_algorithm = "winograd"

    with open(winograd_model_path, "w") as f:
        f.write(txtf.MessageToString(winograd_net, float_format=".17g"))

    mkldnn_direct_log = "mkldnn_direct.log"
    mkldnn_winograd_log = "mkldnn_winograd.log"
    mkldnn_direct_log_path = os.path.join(model_dir, mkldnn_direct_log)
    mkldnn_winograd_log_path = os.path.join(model_dir, mkldnn_winograd_log)

    mkldnn_direct_command = caffe_path + " time -model " + direct_model_path + " -engine MKLDNN -iterations " + str(
        iteration) + " >& " + mkldnn_direct_log_path
    os.system(mkldnn_direct_command)
    mkldnn_winograd_command = caffe_path + " time -model " + winograd_model_path + " -engine MKLDNN -iterations " + str(
        iteration) + " >& " + mkldnn_winograd_log_path
    os.system(mkldnn_winograd_command)

    (model_str,
     mkldnn_direct_time_lines) = utils.parseLog(mkldnn_direct_log_path)
    mkldnn_direct_layer_time_map = utils.parseTimeLines(
        mkldnn_direct_time_lines)
    (model_str,
     mkldnn_winograd_time_lines) = utils.parseLog(mkldnn_winograd_log_path)
    mkldnn_winograd_layer_time_map = utils.parseTimeLines(
        mkldnn_winograd_time_lines)

    hybrid_model_name = base_model_name.split(".")[0] + "_hybrid.prototxt"
    hybrid_model_path = os.path.join(model_dir, hybrid_model_name)
    genOptimalModel(base_net, mkldnn_direct_layer_time_map,
                    mkldnn_winograd_layer_time_map, hybrid_model_path)
Example #27
0
    return lbann.Model(num_epochs,
                       weights=weights,
                       layers=layers,
                       metrics=metrics,
                       objective_function=obj,
                       callbacks=callbacks)


if __name__ == '__main__':
    import lbann

    mini_batch_size = 128
    trainer = lbann.Trainer(mini_batch_size=mini_batch_size)
    model = construct_model()
    # Setup optimizer
    opt = lbann.Adam(learn_rate=0.0001,beta1=0.9,beta2=0.99,eps=1e-8)
    # Load data reader from prototext
    data_reader_proto = lbann.lbann_pb2.LbannPB()
    with open(data_reader_prototext, 'r') as f:
      txtf.Merge(f.read(), data_reader_proto)
    data_reader_proto = data_reader_proto.data_reader

    status = lbann.run(trainer,model, data_reader_proto, opt,
                       scheduler='slurm',
                       nodes=1,
                       procs_per_node=1,
                       time_limit=360,
                       setup_only=True,
                       job_name='jag_wae')
    print(status)
Example #28
0
    def testOldGraph(self):
        # Load graph generated from earlier version of TF where
        # placeholder shape was not set.
        #
        # a = tf.placeholder(tf.float32)
        # b = a + 1.0
        #
        # Older graph's default shape is 'shape {}', not 'shape {
        # unknown_rank: true }'
        graph = """
node {
  name: "Placeholder"
  op: "Placeholder"
  attr {
    key: "dtype"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "shape"
    value {
      shape {
      }
    }
  }
}
node {
  name: "add/y"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_FLOAT
        tensor_shape {
        }
        float_val: 1.0
      }
    }
  }
}
node {
  name: "add"
  op: "Add"
  input: "Placeholder"
  input: "add/y"
  attr {
    key: "T"
    value {
      type: DT_FLOAT
    }
  }
}
versions {
  producer: 21
}
"""
        gdef = graph_pb2.GraphDef()
        text_format.Merge(graph, gdef)
        with self.test_session():
            p, ret = importer.import_graph_def(
                gdef, return_elements=["Placeholder:0", "add:0"])

            # Feed in a vector of two elements.  Since the producer version
            # of 21, a shape of {} is interpreted as "any shape".  If
            # producer version were 22, then we'd get a shape mismatch
            # error.
            self.assertAllEqual([2.0, 3.0],
                                ret.eval(feed_dict={p: [1.0, 2.0]}))
Example #29
0
    def load(basename, **kwargs):
        basename = unzip_model(basename)
        model = RNNTaggerModel()
        model.sess = kwargs.get('sess', tf.Session())
        checkpoint_name = kwargs.get('checkpoint_name', basename)
        checkpoint_name = checkpoint_name or basename
        with open(basename + '.state') as f:
            state = json.load(f)
            model.mxlen = state.get('mxlen', 100)
            model.maxw = state.get('maxw', 100)
            model.crf = bool(state.get('crf', False))
            model.crf_mask = bool(state.get('crf_mask', False))
            model.span_type = state.get('span_type')
            model.proj = bool(state.get('proj', False))

        with open(basename + '.saver') as fsv:
            saver_def = tf.train.SaverDef()
            text_format.Merge(fsv.read(), saver_def)

        with gfile.FastGFile(basename + '.graph', 'rb') as f:
            gd = tf.GraphDef()
            gd.ParseFromString(f.read())
            model.sess.graph.as_default()
            tf.import_graph_def(gd, name='')

            model.sess.run(saver_def.restore_op_name,
                           {saver_def.filename_tensor_name: checkpoint_name})
            model.x = tf.get_default_graph().get_tensor_by_name('x:0')
            model.xch = tf.get_default_graph().get_tensor_by_name('xch:0')
            model.y = tf.get_default_graph().get_tensor_by_name('y:0')
            model.lengths = tf.get_default_graph().get_tensor_by_name(
                'lengths:0')
            model.pkeep = tf.get_default_graph().get_tensor_by_name('pkeep:0')
            model.loss = tf.get_default_graph().get_tensor_by_name(
                'Loss/loss:0')
            # model.all_optimizer_var_updates_op = tf.get_default_graph().get_tensor_by_name("Loss_1/loss:0")
            model.best = tf.get_default_graph().get_tensor_by_name(
                'output/ArgMax:0')
            model.probs = tf.get_default_graph().get_tensor_by_name(
                'output/Reshape_1:0')  # TODO: rename
            try:
                model.A = tf.get_default_graph().get_tensor_by_name(
                    'Loss/transitions:0')
                #print('Found transition matrix in graph, setting crf=True')
                if not model.crf:
                    print(
                        'Warning: meta-data says no CRF but model contains transition matrix!'
                    )
                    model.crf = True
            except:
                if model.crf is True:
                    print(
                        'Warning: meta-data says there is a CRF but not transition matrix found!'
                    )
                model.A = None
                model.crf = False

        with open(basename + '.labels', 'r') as f:
            model.labels = json.load(f)

        model.word_vocab = {}
        if os.path.exists(basename + '-word.vocab'):
            with open(basename + '-word.vocab', 'r') as f:
                model.word_vocab = json.load(f)

        with open(basename + '-char.vocab', 'r') as f:
            model.char_vocab = json.load(f)

        model.saver = tf.train.Saver(saver_def=saver_def)
        return model
Example #30
0
    def test_merge_graph_defs_function(self):
        expected_proto = """
            library {
              function {
                signature {
                  name: "graph_1_foo"
                  input_arg {
                    name: "x"
                    type: DT_HALF
                  }
                  output_arg {
                    name: "identity"
                    type: DT_HALF
                  }
                }
                node_def {
                  name: "add"
                  op: "Add"
                  input: "x"
                  input: "y"
                }
              }
              function {
                signature {
                  name: "graph_2_foo"
                  input_arg {
                    name: "x"
                    type: DT_INT32
                  }
                  output_arg {
                    name: "identity"
                    type: DT_INT32
                  }
                }
                node_def {
                  name: "add"
                  op: "Add"
                  input: "x"
                  input: "y"
                }
              }
              function {
                signature {
                  name: "graph_2_foo_1"
                  input_arg {
                    name: "x"
                    type: DT_HALF
                  }
                  output_arg {
                    name: "identity"
                    type: DT_HALF
                  }
                }
                node_def {
                  name: "add"
                  op: "Add"
                  input: "x"
                  input: "y"
                }
              }
            }
        """

        graph_def_a = GraphDef()
        text_format.Merge(
            """
                library {
                  function {
                    signature {
                      name: "foo"
                      input_arg {
                        name: "x"
                        type: DT_HALF
                      }
                      output_arg {
                        name: "identity"
                        type: DT_HALF
                      }
                    }
                    node_def {
                      name: "add"
                      op: "Add"
                      input: "x"
                      input: "y"
                    }
                  }
                }
            """,
            graph_def_a,
        )

        graph_def_b = GraphDef()
        text_format.Merge(
            """
                library {
                  function {
                    signature {
                      name: "foo"
                      input_arg {
                        name: "x"
                        type: DT_INT32
                      }
                      output_arg {
                        name: "identity"
                        type: DT_INT32
                      }
                    }
                    node_def {
                      name: "add"
                      op: "Add"
                      input: "x"
                      input: "y"
                    }
                  }
                  function {
                    signature {
                      name: "foo_1"
                      input_arg {
                        name: "x"
                        type: DT_HALF
                      }
                      output_arg {
                        name: "identity"
                        type: DT_HALF
                      }
                    }
                    node_def {
                      name: "add"
                      op: "Add"
                      input: "x"
                      input: "y"
                    }
                  }
                }
            """,
            graph_def_b,
        )

        self.assertProtoEquals(
            expected_proto,
            graph_util.merge_graph_defs([graph_def_a, graph_def_b]),
        )