Keras Notes(1):預訓練模型使用

模型

  • 通過keras的api創建模型(tensorflow做後端)參考
class MyModel(Model):
  def __init__(self):
    super(MyModel, self).__init__()
    self.conv1 = Conv2D(32, 3, activation='relu')
    self.flatten = Flatten()
    self.d1 = Dense(128, activation='relu')
    self.d2 = Dense(10, activation='softmax')
  def call(self, x):
    x = self.conv1(x)
    x = self.flatten(x)
    x = self.d1(x)
    return self.d2(x)

# Create an instance of the model
model = MyModel()

使用keras提供的模型

class MyModel():
    def __init__(self):
        self.img_shape=[300,300,3]
        self.my_model= ResNet50(input_shape=self.img_shape,#輸入,include_top=False纔有效
        						include_top=False,#不加入頂部的全連接層(原模型的輸入層是[224,224,3])
        						weights=None,#
        						classes=8#這裏用於圖像分類,classes是分類的數量)
        self.my_model.load_weights(path,by_name=True)#path=》權重的路徑
        for layer in self.my_model.layers:           #設置爲不可訓練
            layer.trainable = False
        self.last_layer=self.my_model.get_layer('bn5c_branch2c')#將模型中最後幾層替換
        self.last_output=self.last_layer.output
        self.x=layers.Flatten()(self.last_output)
        self.x = layers.Dense(1024, activation='relu')( self.x)
        # Add a dropout rate of 0.2
        self.x= layers.Dropout(0.2)(self.x)                  
        # Add a final sigmoid layer for classification
        self.x = layers.Dense(8, activation='softmax')(self.x)#8是分類的數量,softmax用於多分類
        self.my_model=Model(self.my_model.input, self.x)
        self.my_model.summary()

預訓練權重

load_weights是類Network的一個方法,類Model是Netwo的子類,可調用load_weights加載權重

class Model(Network):
“”“The Model class adds training & evaluation routines to a Network.
“””

load_weights定義如下,版本Keras2.3.1

  • by_name
    默認按模型的網絡結構加載權重(模型結構需與保存權重時的結構一致),設置爲true則需同名
  • filepath 文件路徑(*.h5
  • skip_mismatch
  • reshape
@saving.allow_read_from_gcs
    def load_weights(self, filepath, by_name=False,
                     skip_mismatch=False, reshape=False):
        """Loads all layer weights from a HDF5 save file.
		
        If `by_name` is False (default) weights are loaded
        based on the network's topology, meaning the architecture
        should be the same as when the weights were saved.
        Note that layers that don't have weights are not taken
        into account in the topological ordering, so adding or
        removing layers is fine as long as they don't have weights.

        If `by_name` is True, weights are loaded into layers
        only if they share the same name. This is useful
        for fine-tuning or transfer-learning models where
        some of the layers have changed.

        # Arguments
            filepath: String, path to the weights file to load.
            by_name: Boolean, whether to load weights by name
                or by topological order.
            skip_mismatch: Boolean, whether to skip loading of layers
                where there is a mismatch in the number of weights,
                or a mismatch in the shape of the weight
                (only valid when `by_name`=True).
            reshape: Reshape weights to fit the layer when the correct number
                of weight arrays is present but their shape does not match.


        # Raises
            ImportError: If h5py is not available.
        """
        if h5py is None:
            raise ImportError('`load_weights` requires h5py.')
        with h5py.File(filepath, mode='r') as f:
            if 'layer_names' not in f.attrs and 'model_weights' in f:
                f = f['model_weights']
            if by_name:
                saving.load_weights_from_hdf5_group_by_name(
                    f, self.layers, skip_mismatch=skip_mismatch,
                    reshape=reshape)
            else:
                saving.load_weights_from_hdf5_group(
                    f, self.layers, reshape=reshape)
            if hasattr(f, 'close'):
                f.close()
            elif hasattr(f.file, 'close'):
                f.file.close()

參考

# 必須使用該方法下載模型,然後加載
下載需要flyai包裏的函數,實例時resnet50的tensorflow模型包含輸入層,無輸入權重的帶_notop結尾
from flyai.utils import remote_helper
path = remote_helper.get_remote_date('https://www.flyai.com/m/v0.2|resnet50_weights_tf_dim_ordering_tf_kernels.h5')
  • Numpy

  • one_hot

#to_categorical
keras.utils.to_categorical(y, num_classes=None, dtype='float32')

from keras.utils import to_categorical
for i in range(5):
    one_hot=to_categorical(i)
    print(one_hot)

效果,這裏需要特別注意,在模型訓練時需要樣本的y值one_hot,而這裏得到的如下的結果不能直接使用,需要使shape一致,使用num_classes設置calss的數目

[1.]
[0. 1.]
[0. 0. 1.]
[0. 0. 0. 1.]
[0. 0. 0. 0. 1.]

for i in range(5):
    one_hot=to_categorical(i,8)
    print(one_hot)

[1. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0.]

注意事項

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章