我正在使用 Python 运行斩首 DCNN(本例中为 Inception-V3)来获取图像特征。我使用的是 Anaconda、Py3.6 和 Windows7。
使用 TensorFlow 时,我将会话保存在变量中(感谢 jdehesa)并运行:
sess, graph, allTensors, modelInfo = initiate_graph(modelDir)
while True:
filename = getImageFile()
if not filename: break
imgResized = fileReadAndResize(filename)
feats = np.squeeze( sess.run(allTensors['dcnn'], {allTensors['resizedInput']: imgResized}) )
doSmthWithFeats(feats)
当像这样运行时(其中allTensors['dcnn']
在没有最后一层的情况下运行 Inception-V3),我明白了sess.run(...)
大约需要 0.4 秒。
我想转向 Keras,因为它简单且易于运行和比较多个标准 DCNN。
我的 Keras 并行是:
def createModel():
model = InceptionV3(input_tensor=Input(shape=(299, 299, 3)), include_top=False)
x = model.output
x = AveragePooling2D((8, 8), strides=(8, 8))(x)
return Model(model.input, x)
modelIncptV3 = createModel()
while True:
...
feats = model.predict(np.expand_dims(imgResized, 0)).flatten()
doSmthWithFeats(feats)
现在,这里feats
创建线大约需要 1.1 秒(同时产生相同的功能)。
我尝试先编译模型
model.compile( optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) )
现在每个图像的运行时间约为 1.0 秒。
我在 TF 中的不同代码中遇到了类似的问题,运行时的差异是由于为每个图像启动了一个会话。这像吗?我可以做些什么来保留会话?还有其他想法可以使性能与普通 TF 相当吗?
谢谢