Keras: What is the result of the predict_generator function?

Keras documentation says it returns a "Numpy Prediction Array". Using it on 496 sample images with 4 classes, I get a 4-dimensional array (496, 4, 4, 512). What are 2 more dimensions? In the end, I would like to have an array of X (examples) and an array of Y (labels).

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)
+4
source share
1 answer

What you do is extract bottlenecks from the images that you submit to the model. The form (496, 4, 4, 512) that you get is (n_samples, feature_height, feature_width, feature: channels) You pulled out the dense layers of the model, skipping

include_top=False

,

Model VGG16

4 . ( , 150x150, 224x224, VGG16).

, , - , .

, , ,

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)

model.fit(X, Y) , , , X 496 Y, .

model.predict , .

+1

Source: https://habr.com/ru/post/1675000/


All Articles