deep-learning-models icon indicating copy to clipboard operation
deep-learning-models copied to clipboard

Inception preprocessing issue

Open nizhib opened this issue 9 years ago • 1 comments

I have a problem with using Inception for fine-tuning. Actually, there is either an issue with initalized Inception itself according to its predictions or the preprocessing script is not suitable for both ResNet and Inception models.

Running

import numpy as np

from keras.preprocessing import image

from imagenet_utils import preprocess_input, decode_predictions
from resnet50 import ResNet50

model = ResNet50(weights='imagenet')

img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
print('Predicted:', decode_predictions(preds))

gives me African_elephant with p=0.97174376, whilst running

import numpy as np

from keras.preprocessing import image

from imagenet_utils import preprocess_input, decode_predictions
from inception_v3 import InceptionV3

model = InceptionV3(weights='imagenet')

img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(299, 299))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
print('Predicted:', decode_predictions(preds))

states that my elephant is a suspension_bridge with p=0.99999976.

I've already tried deleting keras cache and I use tf backend.

UPD. It occurs that the latter is true — preprocess_input from imagenet_utils is for ResNet only (at least not for Inception), and the Inception header should look like this

from imagenet_utils import decode_predictions
from inception_v3 import InceptionV3, preprocess_input

that leads to reasonable outputs in the end.

Anyway taking out some preprocessing function into imagenet_utils script while keeping another imagenet-targeted one in model file is not expected behavior. Main keras repository replicates that. Mind me doing PR draft for better distinction of different preprocesing functions?

nizhib avatar Oct 21 '16 14:10 nizhib

I am using this pattern with fine tuning and two output classes, observations: random classification output results with a single test image, about 50/50. Training was on 1 image for each class and indicated accuracy of 1.0 with an error less 1e-07. Displaying the image after the img_to_array call is an inverted color image. Is it possible I have a layer mismatch? The top 2 blocks are shown below or layers 172:.

        image = image_utils.load_img(file, target_size=(299, 299))
        image = image_utils.img_to_array(image)
        image = np.expand_dims(image, axis=0)
        image = preprocess_input(image)
        preds = model.predict(image)
[end of 'start history model'
Epoch 20/20
64/64 [==============================] - 5s - loss: 1.1921e-07 - acc: 1.0000 - val_loss: 1.1921e-07 - val_acc: 1.0000

end of 'train new model'
Epoch 20/20
2/1 [============================================================] - 0s - loss: 1.1921e-07 - val_loss: 1.1921e-07
]

Layers 172:

(172, 'mixed8')
(173, 'convolution2d_81')
(174, 'batchnormalization_81')
(175, 'convolution2d_78')
(176, 'convolution2d_82')
(177, 'batchnormalization_78')
(178, 'batchnormalization_82')
(179, 'convolution2d_79')
(180, 'convolution2d_80')
(181, 'convolution2d_83')
(182, 'convolution2d_84')
(183, 'averagepooling2d_9')
(184, 'convolution2d_77')
(185, 'batchnormalization_79')
(186, 'batchnormalization_80')
(187, 'batchnormalization_83')
(188, 'batchnormalization_84')
(189, 'convolution2d_85')
(190, 'batchnormalization_77')
(191, 'mixed9_0')
(192, 'merge_1')
(193, 'batchnormalization_85')
(194, 'mixed9')
(195, 'convolution2d_90')
(196, 'batchnormalization_90')
(197, 'convolution2d_87')
(198, 'convolution2d_91')
(199, 'batchnormalization_87')
(200, 'batchnormalization_91')
(201, 'convolution2d_88')
(202, 'convolution2d_89')
(203, 'convolution2d_92')
(204, 'convolution2d_93')
(205, 'averagepooling2d_10')
(206, 'convolution2d_86')
(207, 'batchnormalization_88')
(208, 'batchnormalization_89')
(209, 'batchnormalization_92')
(210, 'batchnormalization_93')
(211, 'convolution2d_94')
(212, 'batchnormalization_86')
(213, 'mixed9_1')
(214, 'merge_2')
(215, 'batchnormalization_94')
(216, 'mixed10')

apiszcz avatar Mar 14 '17 08:03 apiszcz