1. 程式人生 > >『計算機視覺』Mask-RCNN_訓練網絡其三:model準備

『計算機視覺』Mask-RCNN_訓練網絡其三:model準備

exce att ace exc 創建 wrap png ipy The

一、模型初始化

1、創建模型並載入預訓練參數

準備了數據集後,我們開始構建model,training網絡結構上一節已經介紹完了,現在我們看一看訓練時如何調用training結構的網絡。

技術分享圖片

如上所示,我們首先建立圖結構(詳見上節『計算機視覺』Mask-RCNN_訓練網絡其二:train網絡結構),然後選擇初始化參數方案

例子(train_shape.ipynb)中使用的是COCO預訓練模型,如果想要"Finds the last checkpoint file of the last trained model in the
model directory
",那麽選擇"last"選項。

載入參數方法如下,註意幾個之前接觸不多的操作,

  1. 載入h5文件使用模塊為h5py
  2. keras model有屬性.layers以list形式返回全部的層對象
  3. keras.engine下的saving模塊load_weights_from_hdf5_group_by_name按照名字對應,而load_weights_from_hdf5_group按照記錄順序對應

    def load_weights(self, filepath, by_name=False, exclude=None):
        """Modified version of the corresponding Keras function with
        the addition of multi-GPU support and the ability to exclude
        some layers from loading.
        exclude: list of layer names to exclude
        """
        import h5py
        # Conditional import to support versions of Keras before 2.2
        # TODO: remove in about 6 months (end of 2018)
        try:
            from keras.engine import saving
        except ImportError:
            # Keras before 2.2 used the ‘topology‘ namespace.
            from keras.engine import topology as saving

        if exclude:
            by_name = True

        if h5py is None:
            raise ImportError(‘`load_weights` requires h5py.‘)
        f = h5py.File(filepath, mode=‘r‘)
        if ‘layer_names‘ not in f.attrs and ‘model_weights‘ in f:
            f = f[‘model_weights‘]

        # In multi-GPU training, we wrap the model. Get layers
        # of the inner model because they have the weights.
        keras_model = self.keras_model
        layers = keras_model.inner_model.layers if hasattr(keras_model, "inner_model")            else keras_model.layers

        # Exclude some layers
        if exclude:
            layers = filter(lambda l: l.name not in exclude, layers)

        if by_name:
            saving.load_weights_from_hdf5_group_by_name(f, layers)
        else:
            saving.load_weights_from_hdf5_group(f, layers)
        if hasattr(f, ‘close‘):
            f.close()

        # Update the log directory
        self.set_log_dir(filepath)

2、從h5文件一窺load模式

『計算機視覺』Mask-RCNN_訓練網絡其三:model準備