fchollet / deep-learning-with-python-notebooks

Jupyter notebooks for the code samples of the book "Deep Learning with Python"
MIT License
18.71k stars 8.66k forks source link

5.4 the different between windows and linux #59

Closed wangxupeng closed 6 years ago

wangxupeng commented 6 years ago
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
from keras import models
import sys
from keras.applications.imagenet_utils import preprocess_input

if __name__ == '__main__':
    sys.path.append(r"./")
    model = load_model(r"./outputs/cats_and_dogs_small_2.h5")
    model.summary()

    img_path = r'./cats_and_dogs_small/test/cats/cat.1700.jpg'
    img = image.load_img(img_path, target_size=(150, 150))
    img_tensor = image.img_to_array(img)
    img_tensor = np.expand_dims(img_tensor, axis=0)
    # Remember that the model was trained on inputs
    # that were preprocessed in the following way:
    img_tensor = preprocess_input(img_tensor)

    # Extracts the outputs of the top 8 layers:
    layer_outputs = [layer.output for layer in model.layers[:8]]
    # Creates a model that will return these outputs, given the model input:
    activation_model = models.Model(inputs=model.input, outputs=layer_outputs)

    # This will return a list of 5 Numpy arrays:
    # one array per layer activation
    activations = activation_model.predict(img_tensor)
    first_layer_activation = activations[0]
    print(first_layer_activation.shape)
    print(first_layer_activation)

I run this code in linux is fine, but when I use windows, It occur some errors.

Traceback (most recent call last):
  File "D:/python/python codes/dogs_and_cats/visualization.py", line 29, in <module>
    activations = activation_model.predict(img_tensor)
  File "D:\python\anaconda\lib\site-packages\keras\engine\training.py", line 1167, in predict
    steps=steps)
  File "D:\python\anaconda\lib\site-packages\keras\engine\training_arrays.py", line 294, in predict_loop
    batch_outs = f(ins_batch)
  File "D:\python\anaconda\lib\site-packages\keras\backend\tensorflow_backend.py", line 2666, in __call__
    return self._call(inputs)
  File "D:\python\anaconda\lib\site-packages\keras\backend\tensorflow_backend.py", line 2635, in _call
    session)
  File "D:\python\anaconda\lib\site-packages\keras\backend\tensorflow_backend.py", line 2587, in _make_callable
    callable_fn = session._make_callable_from_options(callable_opts)
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\client\session.py", line 1480, in _make_callable_from_options
    return BaseSession._Callable(self, callable_options)
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\client\session.py", line 1441, in __init__
    session._session, options_ptr, status)
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 519, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: input_1:0 is both fed and fetched.
Exception ignored in: <bound method BaseSession._Callable.__del__ of <tensorflow.python.client.session.BaseSession._Callable object at 0x000001E29716B160>>
Traceback (most recent call last):
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\client\session.py", line 1464, in __del__
    self._session._session, self._handle, status)
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 519, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: No such callable handle: 2072641053968
SuperCrystal commented 5 years ago
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
from keras import models
import sys
from keras.applications.imagenet_utils import preprocess_input

if __name__ == '__main__':
    sys.path.append(r"./")
    model = load_model(r"./outputs/cats_and_dogs_small_2.h5")
    model.summary()

    img_path = r'./cats_and_dogs_small/test/cats/cat.1700.jpg'
    img = image.load_img(img_path, target_size=(150, 150))
    img_tensor = image.img_to_array(img)
    img_tensor = np.expand_dims(img_tensor, axis=0)
    # Remember that the model was trained on inputs
    # that were preprocessed in the following way:
    img_tensor = preprocess_input(img_tensor)

    # Extracts the outputs of the top 8 layers:
    layer_outputs = [layer.output for layer in model.layers[:8]]
    # Creates a model that will return these outputs, given the model input:
    activation_model = models.Model(inputs=model.input, outputs=layer_outputs)

    # This will return a list of 5 Numpy arrays:
    # one array per layer activation
    activations = activation_model.predict(img_tensor)
    first_layer_activation = activations[0]
    print(first_layer_activation.shape)
    print(first_layer_activation)

I run this code in linux is fine, but when I use windows, It occur some errors.

Traceback (most recent call last):
  File "D:/python/python codes/dogs_and_cats/visualization.py", line 29, in <module>
    activations = activation_model.predict(img_tensor)
  File "D:\python\anaconda\lib\site-packages\keras\engine\training.py", line 1167, in predict
    steps=steps)
  File "D:\python\anaconda\lib\site-packages\keras\engine\training_arrays.py", line 294, in predict_loop
    batch_outs = f(ins_batch)
  File "D:\python\anaconda\lib\site-packages\keras\backend\tensorflow_backend.py", line 2666, in __call__
    return self._call(inputs)
  File "D:\python\anaconda\lib\site-packages\keras\backend\tensorflow_backend.py", line 2635, in _call
    session)
  File "D:\python\anaconda\lib\site-packages\keras\backend\tensorflow_backend.py", line 2587, in _make_callable
    callable_fn = session._make_callable_from_options(callable_opts)
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\client\session.py", line 1480, in _make_callable_from_options
    return BaseSession._Callable(self, callable_options)
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\client\session.py", line 1441, in __init__
    session._session, options_ptr, status)
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 519, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: input_1:0 is both fed and fetched.
Exception ignored in: <bound method BaseSession._Callable.__del__ of <tensorflow.python.client.session.BaseSession._Callable object at 0x000001E29716B160>>
Traceback (most recent call last):
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\client\session.py", line 1464, in __del__
    self._session._session, self._handle, status)
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 519, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: No such callable handle: 2072641053968

Hello, I have met with the same problem with you in win10. Did you figure out the possible solution? Thanks!