Sha*_*rak 5 python numpy python-3.x keras
Keras文档称它返回"N Numpy预测数组".在具有4个类的496个图像示例上使用它,我得到一个4维数组(496,4,4,512).其他2个维度是什么?最后,我希望有一个X(示例)数组和一个Y(标签)数组.
img_width, img_height = 150, 150
top_model_weights_path = 'bottleneck_fc_model.h5'
train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
nb_train_samples = 496
nb_validation_samples = 213
epochs = 50
batch_size = 16
number_of_classes = 3
datagen = ImageDataGenerator(rescale=1. / 255)
# build the VGG16 network (exclude last layer)
model = applications.VGG16(include_top=False, weights='imagenet')
# generate training data from image files
train_generator = datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical',
shuffle=False)
# predict bottleneck features on training data
bottleneck_features_train = model.predict_generator(
train_generator, nb_train_samples // batch_size)
print(bottleneck_features_train.shape)
train_data = np.load(open('bottleneck_features_train.npy', 'rb'))
print(train_data.shape)
Run Code Online (Sandbox Code Playgroud)
您正在做的是从输入到模型的图像中提取瓶颈特征。您获得的形状 (496, 4, 4, 512) 是 ( n_samples, feature_height, feature_width, feature:channels ) 您通过传递取出模型的密集层
include_top=False
Run Code Online (Sandbox Code Playgroud)
为了以图形方式解释,您通过该模型传递了样本
没有最后 4 层。(你有不同的高度和宽度,因为你的凝视图像是 150x150 而不是像标准 VGG16 中的 224x224)
您获得的不是类别的预测,而是图像重要特征的综合表示。
要获得您似乎需要的内容,您可以像这样修改代码
model = applications.VGG16(include_top=False, weights='imagenet')
for layer in model.layers:
layer.trainable = False
model = Dense(512, activation='relu')(model) #512 is a parameter you can tweak, the higher, the more complex the model
model = Dense(number_of_classes, activation='softmax')(model)
Run Code Online (Sandbox Code Playgroud)
现在,您可以对用于训练模型的样本调用 model.fit(X,Y),将 496 个样本图像作为 X,将您准备的真实标签作为 Y。
训练结束后,您可以使用 model.predict 来预测您需要的类。
| 归档时间: |
|
| 查看次数: |
4127 次 |
| 最近记录: |