吳良超 融合 cnn+lstm
阿新 • • 發佈:2018-12-29
from keras.applications.vgg16 import VGG16 from keras.models import Sequential, Model from keras.layers import Input, TimeDistributed, Flatten, GRU, Dense, Dropout from keras import optimizers def build_model(): pretrained_cnn = VGG16(weights='imagenet', include_top=False) # pretrained_cnn.trainable = False for layer in pretrained_cnn.layers[:-5]: layer.trainable = False # input shape required by pretrained_cnn input = Input(shape = (224, 224, 3)) x = pretrained_cnn(input) x = Flatten()(x) x = Dense(2048)(x) x = Dropout(0.5)(x) pretrained_cnn = Model(inputs = input, output = x) input_shape = (None, 224, 224, 3) # (seq_len, width, height, channel) model = Sequential() model.add(TimeDistributed(pretrained_cnn, input_shape=input_shape)) model.add(GRU(1024, kernel_initializer='orthogonal', bias_initializer='ones', dropout=0.5, recurrent_dropout=0.5)) model.add(Dense(categories, activation = 'softmax')) model.compile(loss='categorical_crossentropy', optimizer = optimizers.SGD(lr=0.01, momentum=0.9, clipnorm=1., clipvalue=0.5), metrics=['accuracy']) return model
keras 官方給出的例子
from tensorflow import keras from tensorflow.keras import layers from tensorflow.keras.applications import InceptionV3 video=keras.Input(shape=(None,150,150,3)) cnn=InceptionV3(weights='imagenet',include_top=False,pooling='avg') cnn.trainable=False frame_features=layers.TimeDistributed(cnn)(video) video_vector=layers.LSTM(256)(frame_features)