Keras模型预测失败

时间:2017-03-12 06:45:04

标签: tensorflow keras

我是ML中的一个菜鸟。我一直试图在花卉数据集上重新训练模型,以便在keras-js上取得成功。每当我尝试在模型上运行预测时,我得到“错误:predict()必须采用一个对象,其中键是模型的命名输入:input_1。”请帮助这里是我的代码

test.py

import sys
import json

import numpy as np
from collections import defaultdict

# It's very important to put this import before keras,
# as explained here: Loading tensorflow before scipy.misc seems to cause imread to fail #1541
# https://github.com/tensorflow/tensorflow/issues/1541
import scipy.misc

from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import SGD
from keras import backend as K
from keras.utils import np_utils

import dataset
import net

np.random.seed(1337)

spe = 500
n = 299
batch_size = 128
nb_epoch = 1
nb_phase_two_epoch = 1

data_directory, test_directory, model_file_prefix = sys.argv[1:]

print "loading dataset"

X, y, tags = dataset.dataset(data_directory, n)
nb_classes = len(tags)


sample_count = len(y)
train_size = sample_count * 4 // 5
X_train = X[:train_size]
y_train = y[:train_size]
Y_train = np_utils.to_categorical(y_train, nb_classes)
X_test  = X[train_size:]
y_test  = y[train_size:]
Y_test = np_utils.to_categorical(y_test, nb_classes)

datagen = ImageDataGenerator(
    featurewise_center=False,
    samplewise_center=False,
    featurewise_std_normalization=False,
    samplewise_std_normalization=False,
    zca_whitening=False,
    rotation_range=0,
    width_shift_range=0.125,
    height_shift_range=0.125,
    horizontal_flip=True,
    vertical_flip=False,
    fill_mode='nearest')

#datagen.fit(X_train)
train_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
data_directory,
target_size=(299,299),
batch_size=16,
class_mode='categorical'
)

validation_generator = train_datagen.flow_from_directory(
data_directory,
target_size=(299,299),
batch_size=16,
class_mode='categorical'
)

def evaluate(model, vis_filename=None):
Y_pred = model.predict(X_test, batch_size=batch_size)
y_pred = np.argmax(Y_pred, axis=1)

accuracy = float(np.sum(y_test==y_pred)) / len(y_test)
print "accuracy:", accuracy

confusion = np.zeros((nb_classes, nb_classes), dtype=np.int32)
for (predicted_index, actual_index, image) in zip(y_pred, y_test, X_test):
    confusion[predicted_index, actual_index] += 1

print "rows are predicted classes, columns are actual classes"
for predicted_index, predicted_tag in enumerate(tags):
    print predicted_tag[:7],
    for actual_index, actual_tag in enumerate(tags):
        print "\t%d" % confusion[predicted_index, actual_index],
    print
if vis_filename is not None:
    bucket_size = 10
    image_size = n // 4 # right now that's 56
    vis_image_size = nb_classes * image_size * bucket_size
    vis_image = 255 * np.ones((vis_image_size, vis_image_size, 3), dtype='uint8')
    example_counts = defaultdict(int)
    for (predicted_tag, actual_tag, normalized_image) in zip(y_pred, y_test, X_test):
        example_count = example_counts[(predicted_tag, actual_tag)]
        if example_count >= bucket_size**2:
            continue
        image = dataset.reverse_preprocess_input(normalized_image)
        image = image.transpose((1, 2, 0))
        image = scipy.misc.imresize(image, (image_size, image_size)).astype(np.uint8)
        tilepos_x = bucket_size * predicted_tag
        tilepos_y = bucket_size * actual_tag
        tilepos_x += example_count % bucket_size
        tilepos_y += example_count // bucket_size
        pos_x, pos_y = tilepos_x * image_size, tilepos_y * image_size
        vis_image[pos_y:pos_y+image_size, pos_x:pos_x+image_size, :] = image
        example_counts[(predicted_tag, actual_tag)] += 1
    vis_image[::image_size * bucket_size, :] = 0
    vis_image[:, ::image_size * bucket_size] = 0
    scipy.misc.imsave(vis_filename, vis_image)

print "loading original inception model"

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

# train the model on the new data for a few epochs

print "training the newly added dense layers"
print "samples per eph ",spe#X_train.shape[0]

model.fit_generator(train_generator,
        samples_per_epoch=spe,
        nb_epoch=nb_epoch,
        validation_data=validation_generator,
        nb_val_samples=spe,
        )

net.save(model, tags, model_file_prefix)

# at this point, the top layers are well trained and we can start fine-tuning
# convolutional layers from inception V3. We will freeze the bottom N layers
# and train the remaining top layers.

# we chose to train the top 2 inception blocks, i.e. we will freeze
# the first 172 layers and unfreeze the rest:
for layer in model.layers[:172]:
   layer.trainable = False
for layer in model.layers[172:]:
   layer.trainable = True

# we need to recompile the model for these modifications to take effect
# we use SGD with a low learning rate
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=["accuracy"])

# we train our model again (this time fine-tuning the top 2 inception blocks
# alongside the top Dense layers

print "fine-tuning top 2 inception blocks alongside the top dense layers"

for i in range(1,11):
    print "mega-epoch %d/10" % i
    model.fit_generator(train_generator,
        samples_per_epoch=spe,
        nb_epoch=nb_phase_two_epoch,
        validation_data=validation_generator,
        nb_val_samples=spe,
        )

    #evaluate(model, str(i).zfill(3)+".png")

   # evaluate(model, "000.jpg")
    net.save(model, tags, model_file_prefix)

使用keras-js运行时出现错误

错误:predict()必须使用一个对象,其中键是模型的命名输入:input_1。

请帮助

1 个答案:

答案 0 :(得分:0)

您的代码阅读起来不容易 - 缩进已经关闭,而且我真的不知道数据集中的内容以及其他导入内容。

那说,问题可能是X_test的格式。在训练期间,您使用ImageDatagenerator的输出,该输出将图像重新缩放到(299,299)以及其他操作。在评估期间,您可以直接在X_test中使用原始数据。

希望这有助于/ p