Dear All,
I have a pipeline in Keras wherein I extract fixed features from images (e.g. forward only) like so.
For instance, I extract 1408 features for VGG and 2048 features for Resnet50. To understand what I am doing conceptually, please refer to:
class ResNetFV:
__name__ = "ResNet"
# Construct a feature extractor based on pre-trained model
def __init__(self,input_size=224,num_class=1,batch_size=32):
self.model = ResNet50(include_top=False, weights='imagenet')
self.input = Input(shape=(input_size, input_size, 3), name='image_input')
feature_extractor = self.model(self.input)
flattener = Flatten()(feature_extractor)
self.bottleneck_feature_extractor = Model(inputs=self.input, outputs=flattener)
self.batch_size = batch_size
self.data_format = K.image_data_format()
def predict(self, x):
if self.data_format == "channels_first":
x = x.transpose(0, 3, 1, 2)
x = preprocess_resnet(x.astype(K.floatx()))
self.bottleneck_features_train_raw = self.bottleneck_feature_extractor.predict(x, batch_size=self.batch_size)
self.bottleneck_features_train_reduced = self.bottleneck_features_train_raw.squeeze()
return self.bottleneck_features_train_reduced
class VGGFV:
__name__ = "VGGFV"
def __init__(self, feature_layer="block5_conv3", input_size=224,num_class=1,batch_size=32):
base_model = VGG16(weights='imagenet', include_top=False,
input_shape=[input_size,input_size,3], classes=num_class)
x2 = GlobalAveragePooling2D()(base_model.get_layer("block2_conv2").output) # 128
x3 = GlobalAveragePooling2D()(base_model.get_layer("block3_conv3").output) # 256
x4 = GlobalAveragePooling2D()(base_model.get_layer("block4_conv3").output) # 512
x5 = GlobalAveragePooling2D()(base_model.get_layer("block5_conv3").output) # 512
x = Concatenate()([x2,x3,x4,x5])
model = Model(inputs=base_model.input, outputs=x)
optimizer = Adam(lr=0.0001)
model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
self.model=model
self.batch_size = batch_size
self.data_format = K.image_data_format()
def predict(self, x):
if self.data_format == "channels_first":
x = x.transpose(0, 3, 1, 2)
x = preprocess_vgg(x.astype(K.floatx()))
return self.model.predict(x, batch_size=self.batch_size).squeeze()
I am now trying to do the same in TRT.
trt_graph_def = trt.create_inference_graph(
input_graph_def=classifier_graph_def,
outputs=['resnet_v1_50/predictions/Reshape_1'],
#outputs=['resnet_v1_50/block1'], THIS DOES NOT WORK
max_batch_size=batch_size,
max_workspace_size_bytes=workspace_size_bytes,
precision_mode=precision_mode)
#trt_graph_def=trt.calib_graph_to_infer_graph(trt_graph_def) # For only 'INT8'
print('Generated TensorRT graph def')
After loading the graph, i am trying to access resnet_v1_50/block1", however, this results in the following exception:
Fetch node resnet_v1_50/block1 doesn't exist in graph
What I do not understand is why these CNN blocks are not accessible? (e.g. block1/unit_2/bottleneck_v1/conv3)?
Same for VGG, how can I replicate the code in TRT to extract 1408 features?
The code I am using is very similar to this one:
BR,