Hi,
I am just trying to run the following:
from keras.applications.nasnet import NASNetMobile
from keras.preprocessing import image
from keras.applications.imagenet_utils import decode_predictions
from keras.applications.inception_v3 import preprocess_input
import numpy as np
from PIL import Image
model = NASNetMobile(weights='imagenet')
img_path = '220px-Lynx_lynx_poing.jpg'
img = image.load_img(img_path, target_size=(224, 224))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
preds = model.predict(img)
and getting the following error for model.predict call:
InvalidArgumentError Traceback (most recent call last)
/anaconda/envs/py35/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, args)
1360 try:
-> 1361 return fn(args)
1362 except errors.OpError as e:
/anaconda/envs/py35/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
1339 return tf_session.TF_Run(session, options, feed_dict, fetch_list,
-> 1340 target_list, status, run_metadata)
1341
/anaconda/envs/py35/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
515 compat.as_text(c_api.TF_Message(self.status.status)),
--> 516 c_api.TF_GetCode(self.status.status))
517 # Delete the underlying status object from memory otherwise it stays alive
InvalidArgumentError: Incompatible shapes: [1,28,28,22] vs. [1,56,56,22]
[[Node: reduction_A_block_stem_2/block_3/reduction_add3_stem_2/add = Add[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](reduction_A_block_stem_2/block_3/reduction_left3_stem_2/AvgPool, reduction_A_block_stem_2/block_3/separable_conv_block_reduction_right3_stem_2/separable_conv_2_bn_reduction_right3_stem_2/cond/Merge)]]
[[Node: predictions/Softmax/_8225 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_9516_predictions/Softmax", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
During handling of the above exception, another exception occurred:
InvalidArgumentError Traceback (most recent call last)
/anaconda/envs/py35/lib/python3.5/site-packages/keras/engine/training.py in predict(self, x, batch_size, verbose, steps)
1833 f = self.predict_function
1834 return self._predict_loop(f, ins, batch_size=batch_size,
-> 1835 verbose=verbose, steps=steps)
1836
1837 def train_on_batch(self, x, y,
/anaconda/envs/py35/lib/python3.5/site-packages/keras/engine/training.py in _predict_loop(self, f, ins, batch_size, verbose, steps)
1329 ins_batch[i] = ins_batch[i].toarray()
1330
-> 1331 batch_outs = f(ins_batch)
1332 if not isinstance(batch_outs, list):
1333 batch_outs = [batch_outs]
/anaconda/envs/py35/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
2480 session = get_session()
2481 updated = session.run(fetches=fetches, feed_dict=feed_dict,
-> 2482 **self.session_kwargs)
2483 return updated[:len(self.outputs)]
2484
/anaconda/envs/py35/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
903 try:
904 result = self._run(None, fetches, feed_dict, options_ptr,
--> 905 run_metadata_ptr)
906 if run_metadata:
907 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/anaconda/envs/py35/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1135 if final_fetches or final_targets or (handle and feed_dict_tensor):
1136 results = self._do_run(handle, final_targets, final_fetches,
-> 1137 feed_dict_tensor, options, run_metadata)
1138 else:
1139 results = []
/anaconda/envs/py35/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1353 if handle is None:
1354 return self._do_call(_run_fn, self._session, feeds, fetches, targets,
-> 1355 options, run_metadata)
1356 else:
1357 return self._do_call(_prun_fn, self._session, handle, feeds, fetches)
/anaconda/envs/py35/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1372 except KeyError:
1373 pass
-> 1374 raise type(e)(node_def, op, message)
1375
1376 def _extend_graph(self):
InvalidArgumentError: Incompatible shapes: [1,28,28,22] vs. [1,56,56,22]
[[Node: reduction_A_block_stem_2/block_3/reduction_add3_stem_2/add = Add[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](reduction_A_block_stem_2/block_3/reduction_left3_stem_2/AvgPool, reduction_A_block_stem_2/block_3/separable_conv_block_reduction_right3_stem_2/separable_conv_2_bn_reduction_right3_stem_2/cond/Merge)]]
[[Node: predictions/Softmax/_8225 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_9516_predictions/Softmax", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Caused by op 'reduction_A_block_stem_2/block_3/reduction_add3_stem_2/add', defined at:
File "/anaconda/envs/py35/lib/python3.5/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/anaconda/envs/py35/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/anaconda/envs/py35/lib/python3.5/site-packages/ipykernel/__main__.py", line 3, in
app.launch_new_instance()
File "/anaconda/envs/py35/lib/python3.5/site-packages/traitlets/config/application.py", line 658, in launch_instance
app.start()
File "/anaconda/envs/py35/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 478, in start
self.io_loop.start()
File "/anaconda/envs/py35/lib/python3.5/site-packages/zmq/eventloop/ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "/anaconda/envs/py35/lib/python3.5/site-packages/tornado/ioloop.py", line 888, in start
handler_func(fd_obj, events)
File "/anaconda/envs/py35/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
return fn(args, kwargs)
File "/anaconda/envs/py35/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "/anaconda/envs/py35/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "/anaconda/envs/py35/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
callback(args, *kwargs)
File "/anaconda/envs/py35/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
return fn(args, *kwargs)
File "/anaconda/envs/py35/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "/anaconda/envs/py35/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
handler(stream, idents, msg)
File "/anaconda/envs/py35/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "/anaconda/envs/py35/lib/python3.5/site-packages/ipykernel/ipkernel.py", line 208, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/anaconda/envs/py35/lib/python3.5/site-packages/ipykernel/zmqshell.py", line 537, in run_cell
return super(ZMQInteractiveShell, self).run_cell(args, *kwargs)
File "/anaconda/envs/py35/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2728, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/anaconda/envs/py35/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2856, in run_ast_nodes
if self.run_code(code, result):
File "/anaconda/envs/py35/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "
get_ipython().run_cell_magic('time', '', "model = NASNetMobile(weights='imagenet')")
File "/anaconda/envs/py35/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2131, in run_cell_magic
result = fn(magic_arg_s, cell)
File "
File "/anaconda/envs/py35/lib/python3.5/site-packages/IPython/core/magic.py", line 187, in
call = lambda f, *a, *
File "/anaconda/envs/py35/lib/python3.5/site-packages/IPython/core/magics/execution.py", line 1238, in time
exec(code, glob, local_ns)
File "
File "/anaconda/envs/py35/lib/python3.5/site-packages/keras/applications/nasnet.py", line 440, in NASNetMobile
default_size=224)
File "/anaconda/envs/py35/lib/python3.5/site-packages/keras/applications/nasnet.py", line 230, in NASNet
block_id='stem_2')
File "/anaconda/envs/py35/lib/python3.5/site-packages/keras/applications/nasnet.py", line 648, in _reduction_a_cell
x3 = add([x3_1, x3_2], name='reduction_add3_%s' % block_id)
File "/anaconda/envs/py35/lib/python3.5/site-packages/keras/layers/merge.py", line 547, in add
return Add(kwargs)(inputs)
File "/anaconda/envs/py35/lib/python3.5/site-packages/keras/engine/topology.py", line 619, in __call__
output = self.call(inputs, **kwargs)
File "/anaconda/envs/py35/lib/python3.5/site-packages/keras/layers/merge.py", line 155, in call
return self._merge_function(inputs)
File "/anaconda/envs/py35/lib/python3.5/site-packages/keras/layers/merge.py", line 219, in _merge_function
output += inputs[i]
File "/anaconda/envs/py35/lib/python3.5/site-packages/tensorflow/python/ops/math_ops.py", line 934, in binary_op_wrapper
return func(x, y, name=name)
File "/anaconda/envs/py35/lib/python3.5/site-packages/tensorflow/python/ops/gen_math_ops.py", line 183, in add
"Add", x=x, y=y, name=name)
File "/anaconda/envs/py35/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/anaconda/envs/py35/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3271, in create_op
op_def=op_def)
File "/anaconda/envs/py35/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1650, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): Incompatible shapes: [1,28,28,22] vs. [1,56,56,22]
[[Node: reduction_A_block_stem_2/block_3/reduction_add3_stem_2/add = Add[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](reduction_A_block_stem_2/block_3/reduction_left3_stem_2/AvgPool, reduction_A_block_stem_2/block_3/separable_conv_block_reduction_right3_stem_2/separable_conv_2_bn_reduction_right3_stem_2/cond/Merge)]]
[[Node: predictions/Softmax/_8225 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_9516_predictions/Softmax", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Can you let me know what maybe wrong?
ps. I had to first follow this fix in this open issue here: https://github.com/keras-team/keras/issues/10138 to load NASNetMobile.
This was fixed by model = NASNetMobile(input_shape=(224, 224, 3), include_top=True, weights='imagenet')
Most helpful comment
This was fixed by model = NASNetMobile(input_shape=(224, 224, 3), include_top=True, weights='imagenet')