Keras模型预测失败 [英] Keras model predict fails

查看:82
本文介绍了Keras模型预测失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ML中有点菜鸟味.香港专业教育学院一直试图在花数据集上训练一个模型,使其在keras-js上获得成功.每当我尝试在模型上运行预测时,我都会收到错误:predict()必须采用对象,其中键是模型的命名输入:input_1."请帮忙,这是我的代码

im a bit of a noob in ML. ive been trying to get a model retrained on the flowers dataset to work on keras-js withous success. whenever i try to run predict on the model i get "Error: predict() must take an object where the keys are the named inputs of the model: input_1." pls help here is my code

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运行时,出现错误

when run with keras-js i get the error

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

Error: predict() must take an object where the keys are the named inputs of the model: input_1.

请帮助

推荐答案

阅读代码并不容易-缩进功能已关闭,我真的不知道数据集和其他导入内容中的内容.

Wasn't easy to read your code - indentation was off and I don't really know what's in dataset and the other imports.

也就是说,问题可能出在X_test的格式上.在训练过程中,您将使用ImageDatagenerator的输出,它将与其他操作一起将图像重新缩放为(299,299).在评估期间,您可以直接使用X_test中的原始数据.

That said, the problem is probably the format of X_test. During training, you use output from ImageDatagenerator which rescales the images to (299,299) together with the other manipulations. During evaluation, you use the raw data in X_test directly.

希望这对/p

这篇关于Keras模型预测失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆