2017-08-11 6 views
0

J'essaie d'utiliser le modèle entraîné basé sur Keras 2 incepctionV3 pour prédire une image à des fins de test. Mon travail de modèle original bien, alors je tente de créer un modèle avec input_shape spécifié (299,299,3)Comment puis-je créer un (None, 299,299,3) à partir de (299,299,3)?

base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(299,299,3)) 

Le processus de formation a l'air bien, mais lorsque je tente de l'utiliser pour prédire l'image qu'elle provoque cette erreur.

ValueError: Error when checking : expected input_1 to have shape (None, 299, 299, 3) but got array with shape (1, 229, 229, 3)

import sys 
    import argparse 
    import numpy as np 
    from PIL import Image 
    from io import BytesIO 

    from keras.preprocessing import image 
    from keras.models import load_model 
    from keras.applications.inception_v3 import preprocess_input 


    target_size = (229, 229) #fixed size for InceptionV3 architecture 


    def predict(model, img, target_size): 
     """Run model prediction on image 
     Args: 
     model: keras model 
     img: PIL format image 
     target_size: (w,h) tuple 
     Returns: 
     list of predicted labels and their probabilities 
     """ 
     if img.size != target_size: 
     img = img.resize(target_size) 

     x = image.img_to_array(img) 
     print(x.shape) 
     print("model input",model.inputs) 
     print("model output",model.outputs) 
     x = np.expand_dims(x, axis=0) 
     #x = x[None,:,:,:] 
     print(x.shape) 
     x = preprocess_input(x) 

     print(x.shape) 
     preds = model.predict(x) 
     print('Predicted:',preds) 
     return preds[0] 

Voici l'imprimé

(229, 229, 3) 
('model input', [<tf.Tensor 'input_1:0' shape=(?, 299, 299, 3) dtype=float32>]) 
('model output', [<tf.Tensor 'dense_2/Softmax:0' shape=(?, 5) dtype=float32>]) 
(1, 229, 229, 3) 
(1, 229, 229, 3) 

(1,299,299,3) signifie 1 image 299 X 299 avec 3 canaux. Quelle est l'entrée attendue de mon modèle entraîné (None, 299,299,3) signifiant dans ce cas? Comment puis-je créer un (None, 299,299,3) à partir de (299,299,3)?

Répondre

2

ici le problème est la taille de l'image, définissez la taille de besoin 299, 299

target_size = (299, 299) #fixed size for InceptionV3 architecture 
1

Vous devez utiliser

preds = model.predict(x, batch_size=1) 

batch_size = 32 par défaut. Et vous n'avez qu'une image à prédire. (Aucun, 299,299,3) signifiant dans ce cas que model.predict attend un tableau avec la forme (n, 299,299,3) avec n> batch_size pour le traiter lot par lot et renvoie (n, outputs_dim) un tableau dimensionnel de prédictions.