美文网首页
使用不同规模的`VGG`

使用不同规模的`VGG`

作者: minus_1 | 来源:发表于2018-12-14 14:53 被阅读0次

    vgg-16_keras 参考

    1. VGG-16 结构

    VGG-16 Framework

      更改网络结构(delete some layers)可以参考如下的代码——对应keras代码——并进行修改。(NOTES: 将input shape修改为(3, 300,300)的例子)

    2. 可用代码

    (NOTES: python=2.7; tensorflow-backed keras: pip install keras; pip install opencv for import cv2)

      下述代码和图 VGG-16 Framework 中的layer有一一对应的关系:(NOTES: 明显看出 VGG-16 中间层的规模分别为 input=(224,224,3) \rightarrow (224,224,64) \rightarrow (112,112,128) \rightarrow (56,56,256) \rightarrow (28,28,512) \rightarrow (14,14,512) \rightarrow (7,7,512) \rightarrow (1,1,4096) \rightarrow ... )
      所以我觉得对应分辨率可以调整为 224*224, 112*112, 56*56, 28*28, ...这样子,删掉对应的layer,然后剩下的layer再调整(主要调整channel)。 然后再训练和预测(model.train(), model.predict())。

    from keras.models import Sequential
    from keras.layers.core import Flatten, Dense, Dropout
    from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
    from keras.optimizers import SGD
    import cv2, numpy as np
    
    def VGG_16(weights_path=None):
        model = Sequential()
        model.add(ZeroPadding2D((1,1),input_shape=(3,224,224)))
        model.add(Convolution2D(64, 3, 3, activation='relu'))
        model.add(ZeroPadding2D((1,1)))
        model.add(Convolution2D(64, 3, 3, activation='relu'))
        model.add(MaxPooling2D((2,2), strides=(2,2)))
    
        model.add(ZeroPadding2D((1,1)))
        model.add(Convolution2D(128, 3, 3, activation='relu'))
        model.add(ZeroPadding2D((1,1)))
        model.add(Convolution2D(128, 3, 3, activation='relu'))
        model.add(MaxPooling2D((2,2), strides=(2,2)))
    
        model.add(ZeroPadding2D((1,1)))
        model.add(Convolution2D(256, 3, 3, activation='relu'))
        model.add(ZeroPadding2D((1,1)))
        model.add(Convolution2D(256, 3, 3, activation='relu'))
        model.add(ZeroPadding2D((1,1)))
        model.add(Convolution2D(256, 3, 3, activation='relu'))
        model.add(MaxPooling2D((2,2), strides=(2,2)))
    
        model.add(ZeroPadding2D((1,1)))
        model.add(Convolution2D(512, 3, 3, activation='relu'))
        model.add(ZeroPadding2D((1,1)))
        model.add(Convolution2D(512, 3, 3, activation='relu'))
        model.add(ZeroPadding2D((1,1)))
        model.add(Convolution2D(512, 3, 3, activation='relu'))
        model.add(MaxPooling2D((2,2), strides=(2,2)))
    
        model.add(ZeroPadding2D((1,1)))
        model.add(Convolution2D(512, 3, 3, activation='relu'))
        model.add(ZeroPadding2D((1,1)))
        model.add(Convolution2D(512, 3, 3, activation='relu'))
        model.add(ZeroPadding2D((1,1)))
        model.add(Convolution2D(512, 3, 3, activation='relu'))
        model.add(MaxPooling2D((2,2), strides=(2,2)))
    
        model.add(Flatten())
        model.add(Dense(4096, activation='relu'))
        model.add(Dropout(0.5))
        model.add(Dense(4096, activation='relu'))
        model.add(Dropout(0.5))
        model.add(Dense(1000, activation='softmax'))
    
        if weights_path:
            model.load_weights(weights_path)
    
        return model
    
    if __name__ == "__main__":
        im = cv2.resize(cv2.imread('cat.jpg'), (224, 224)).astype(np.float32)
        im[:,:,0] -= 103.939
        im[:,:,1] -= 116.779
        im[:,:,2] -= 123.68
        im = im.transpose((2,0,1))
        im = np.expand_dims(im, axis=0)
    
        # Test pretrained model
        model = VGG_16('vgg16_weights.h5')
        sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
        model.compile(optimizer=sgd, loss='categorical_crossentropy')
        out = model.predict(im)
        print np.argmax(out)
    

    model in keras:

    • 模型恢复 .h5格式
    model.load_weights('file_path')
    
    • 模型保存 .h5格式
    model.save_weights('file_path')
    
    • ...

    -1. time模块计时


    END OF FILE

    相关文章

      网友评论

          本文标题:使用不同规模的`VGG`

          本文链接:https://www.haomeiwen.com/subject/ctfghqtx.html