输入0与图层global_average_pooling2d_5不兼容:预期ndim = 4,发现ndim = 2

时间:2018-06-04 11:36:35

标签: keras keras-layer keras-2

我想在最初的inceptionv3层进行getrain。我试图遵循这个教程(在一组新的类上微调InceptionV3) https://keras.io/applications/#usage-examples-for-image-classification-models

但如果我设置include_top = False,我就无法解码_预测。

from keras.applications import inception_v3 as inc_net
base_model = inc_net.InceptionV3()
print("InceptionV3 loaded")
from keras.models import Sequential
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D

# idk needs a loot of improvement, but just for testing      
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer -- let's say we have 5classes
predictions = Dense(5, activation='softmax')(x)

model = Model(inputs=base_model.input, outputs=predictions)

#freezing all layers
for layer in base_model.layers:
    layer.trainable = False

model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

如果我喜欢这样,我会

Input 0 is incompatible with layer global_average_pooling2d_5: expected ndim=4, found ndim=2

我知道我应该使用include_top = False。但是我如何编译/保存我可以使用decode_predictions的模型?

我用.h5保存模型并加载权重,但如果我这样做,我得到 期望一批预测(即2D形状阵列(样本,1000))。找到形状为:(1,5)

的数组

这是我的完整代码,如果再培训到目前为止(我很确定如果你知道一些方法来改进它很糟糕,我很乐意接受你提供的任何教程)

import numpy as np
import cv2
import matplotlib.pyplot as plt

num_classes = 5
#train_samples = 3000 #sumoftrain
#test_samples = 670  #sumofvalidation


from keras.applications import inception_v3 as inc_net
base_model = inc_net.InceptionV3((weights='imagenet', include_top=False))
print("InceptionV3 loaded")

from keras.models import Sequential
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
# idk needs a loot of improvement, but just for testing


x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer -- let's say we have 200 classes
predictions = Dense(num_classes, activation='softmax')(x)

model = Model(inputs=base_model.input, outputs=predictions)

#freezing all layers
for layer in base_model.layers:
    layer.trainable = False

model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range = 0.2, horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
training_set= train_datagen.flow_from_directory('./data/flower_photos/train/', target_size = (299,299), batch_size=32)

#for i, layer in enumerate(base_model.layers):
#   print(i, layer.name)

model.fit_generator(training_set, steps_per_epoch=100, epochs=3, validation_steps=25)

for layer in model.layers[:249]:
   layer.trainable = False
for layer in model.layers[249:]:
   layer.trainable = True
# we need to recompile the model for these modifications to take effect
# we use SGD with a low learning rate
from keras.optimizers import SGD
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy'])

model.fit_generator(training_set, steps_per_epoch=300, epochs=5, validation_steps=50)

# SAVE THE MODEL
# serialize model to JSON
from keras.models import model_from_json

model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")

这是加载和测试的代码

from keras.models import model_from_json
# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")

# evaluate loaded model on test data
loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

from keras.preprocessing import image
# Function for valdiate img
def transform_img_fn(path_list):
    out = []
    for img_path in path_list:
        img = image.load_img(img_path, target_size=(299, 299))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = inc_net.preprocess_input(x)
        out.append(x)
    return np.vstack(out)

# Testing Inceptionv3
from keras.applications.imagenet_utils import decode_predictions
import numpy as np
from keras.applications import inception_v3 as inc_net
import matplotlib.pyplot as plt
import os

images = transform_img_fn([os.path.join('data','rose.jpg')])
plt.imshow(images[0] / 2 + 0.5)
#base_model = inc_net.InceptionV3(weights='imagenet', include_top=True)

preds = loaded_model.predict(images)
for x in decode_predictions(preds)[0]:
    print(x)

0 个答案:

没有答案