错误使用
1、构建模型:
import tensorflow as tf
import collections
from efficientnet import tfkeras
class MyModel(tf.keras.Model):
def __init__(self, height=None,width=None,channel=None):
super(MyModel,self).__init__()
self.inputshape = tf.keras.layers.InputLayer(input_shape=(height,width,channel))
self.effnet = tfkeras.EfficientNetB0(
include_top=False,
weights='imagenet',
input_shape=(height,width,channel)
)
self.pool = tf.keras.layers.GlobalAveragePooling2D(name="GlobalAP")
self.dense3 = tf.keras.layers.Dense(512,activation="relu",kernel_initializer="he_normal")
self.dense4 = tf.keras.layers.Dense(6,activation="softmax")
def call(self, input):
# x = self.resnet50(input)
# print(x.shape)
x = self.inputshape(input)
x = self.effnet(x)
print(x)
x = self.pool(x)
x = self.dense3(x)
self.out = self.dense4(x)
return self.out
2、调用已训练模型中间层输出
for index, (train_index, valid_index) in enumerate(kfold.split(train_data,label)):
print("========= %d =========="%index)
x_train, x_valid, y_train, y_valid = X_train[train_index], X_train[valid_index], Y_train[train_index], Y_train[valid_index]
md = model.MyModel(width, height, channel)
# =======================================================================
## 获取中间结果
get_GlobalAveragePooling = tf.keras.backend.function([md.layers[0].input,
tf.keras.backend.learning_phase()], [md.GlobalAP.output])
# =====================