我试图在 keras 中提取特征(使用 tensorflow 和 theano 背景)。但变得不成功。
我的代码是:
model = Sequential()
model.add(Convolution2D(64, 5, 5, input_shape=(3,img_width, img_height)))
model.add(LeakyReLU(alpha=.01))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(64, 2, 2))
model.add(LeakyReLU(alpha=.01))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.1))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dense(8))
model.add(ActivityRegularization(l2=0.01))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Dense(1))
model.add(Activation('sigmoid'))
test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size=(img_width, img_height),
batch_size=16,
class_mode='binary')
scores = model.evaluate_generator(test_generator, 237)
print("Accuracy = ", scores[1])
我已经将 fit_generator 用于训练、验证和测试用例。
我从例如第 3 层获取输出的代码是:
get_activations = theano.function([model.layers[0].input],
model.layers[3].output(train=False), allow_input_downcast=True)
activations = get_activations(test_generator)
但是在执行它之后我收到一个错误:
File "test.py", line 96, in <module>
get_activations = theano.function([model.layers[0].input],
model.layers[3].output(train=False), allow_input_downcast=True)
TypeError: 'TensorVariable' object is not callable
我怎么能在 theano 或 tensorflow 模式下(其中任何一个或两者)。我在代码中使用了 fit_generator 进行图像增强。
请帮忙。