如何在循环中检索中间预测结果
How to retrieve intermediate predictions results in a loop
需要计算每一层预测结果的时间,并将模型的输出作为另一层的输入。将每一层视为一个模型并获得中间结果并将这些结果传递给下一层。当我尝试 运行 时,会引发代码断言错误。
model = vgg16.VGG16(weights='imagenet')
filename = 'I:/PhD/SecondYear/Experiment/DNN/image/cat.4001.jpg'
original_image = load_img(filename, target_size=(224, 224))
numpy_image = img_to_array(original_image)
input_image = np.expand_dims(numpy_image, axis=0)
processed_image_vgg16 = vgg16.preprocess_input(input_image.copy())
new_input=keras.layers.Input(shape=(224, 224, 3))
input=new_input
def profiling(model, test_input):
next_layer = input
for layer in model.layers:
out= layer(next_layer)
#next_layer = out
intermediate_model= keras.Model(next_layer,out)
start = time.time()
x = intermediate_model.predict(test_input)
next_layer = out
end = time.time() - start
print(end)
profiling(model,processed_image_vgg16)
我对你的功能做了细微的改动:
def profiling(model, test_input):
data_input = test_input
for layer in model.layers:
start = time.time()
im_input = tf.keras.layers.Input(shape=layer.input.shape[1:])
# Get current layer's input shape and create input tensor.
im_output = layer(im_input)
# Apply current layer's operation on current input.
intermediate_model= keras.models.Model(im_input,im_output)
# Create model from input and output
data_input = intermediate_model.predict(data_input)
# predict and update output to data_input variable which will be used in next iteration.
end = time.time() - start
print("Layer: ",type(layer).__name__,end)
profiling(model,processed_image_vgg16)
# output:
# Layer: InputLayer 0.026311635971069336
# Layer: Conv2D 0.048013925552368164
# Layer: Conv2D 0.11106109619140625
# Layer: MaxPooling2D 0.03328657150268555
# ...
这将为您提供分层子模型的预测时间。
需要计算每一层预测结果的时间,并将模型的输出作为另一层的输入。将每一层视为一个模型并获得中间结果并将这些结果传递给下一层。当我尝试 运行 时,会引发代码断言错误。
model = vgg16.VGG16(weights='imagenet')
filename = 'I:/PhD/SecondYear/Experiment/DNN/image/cat.4001.jpg'
original_image = load_img(filename, target_size=(224, 224))
numpy_image = img_to_array(original_image)
input_image = np.expand_dims(numpy_image, axis=0)
processed_image_vgg16 = vgg16.preprocess_input(input_image.copy())
new_input=keras.layers.Input(shape=(224, 224, 3))
input=new_input
def profiling(model, test_input):
next_layer = input
for layer in model.layers:
out= layer(next_layer)
#next_layer = out
intermediate_model= keras.Model(next_layer,out)
start = time.time()
x = intermediate_model.predict(test_input)
next_layer = out
end = time.time() - start
print(end)
profiling(model,processed_image_vgg16)
我对你的功能做了细微的改动:
def profiling(model, test_input):
data_input = test_input
for layer in model.layers:
start = time.time()
im_input = tf.keras.layers.Input(shape=layer.input.shape[1:])
# Get current layer's input shape and create input tensor.
im_output = layer(im_input)
# Apply current layer's operation on current input.
intermediate_model= keras.models.Model(im_input,im_output)
# Create model from input and output
data_input = intermediate_model.predict(data_input)
# predict and update output to data_input variable which will be used in next iteration.
end = time.time() - start
print("Layer: ",type(layer).__name__,end)
profiling(model,processed_image_vgg16)
# output:
# Layer: InputLayer 0.026311635971069336
# Layer: Conv2D 0.048013925552368164
# Layer: Conv2D 0.11106109619140625
# Layer: MaxPooling2D 0.03328657150268555
# ...
这将为您提供分层子模型的预测时间。