Home > Enterprise >  Can not squeeze dim[1], expected a dimension of 1, got 2 [[{{node predict/feature_vector/SpatialSque
Can not squeeze dim[1], expected a dimension of 1, got 2 [[{{node predict/feature_vector/SpatialSque

Time:03-18

I am finding it difficult to train the following model when using the 'Mobilenet_tranferLearning'. I am augmenting and loading the files from the directory using ImageDataGenerator and flow_from_directory method. What is interesting is that my code does not throw any errors when using InceptionV3, but does when I use 'Mobilenet_tranferLearning'. I would appreciate some pointers as I believe I am using the correct loss function 'categorical_crossentropy' which I have also defined in train_generator (class_mode='categorical').

train_datagen = ImageDataGenerator(
rescale= 1./255,
shear_range= 0.2,
zoom_range= 0.2,
horizontal_flip= True,
rotation_range= 20,
width_shift_range= 0.2,
height_shift_range= 0.2,   
validation_split=0.2,)


valid_datagen = ImageDataGenerator(
rescale= 1./255, 
validation_split=0.2,)

train_generator = train_datagen.flow_from_directory(  
'/content/fold/images/Images',  
target_size= (243, 243), 
color_mode= 'rgb',
batch_size= 64,  
class_mode= 'categorical',
subset='training',
shuffle= True, 
seed= 1337) 

valid_generator = valid_datagen.flow_from_directory(
'/content/fold/images/Images',
target_size= (243, 243),
color_mode= 'rgb',
batch_size= 64,  
class_mode= 'categorical',
subset='validation',
shuffle= True, 
seed= 1337)

`import tensorflow_hub as hub 
# from tensorflow.keras import Activations
classifier_url ="https://hub.tensorflow.google.cn/google/tf2- 
 preview/mobilenet_v2/feature_vector/4"
 baseModel = hub.KerasLayer(classifier_url, input_shape=(224,224,3), output_shape=[1280], 
 name="Mobilenet")
 baseModel.trainable = False # freeze mobilenet weights
 myModel = Sequential(name="Mobilenet_tranferLearning")
 myModel.add(baseModel)
 myModel.add(Flatten())
 myModel.add(Dropout(0.2))
 myModel.add(Dense(120,activation='softmax'))
 myModel.summary()`
 
 myModel.compile(optimizer= 'adam', loss= 'categorical_crossentropy', metrics= ['accuracy'])


  history = myModel.fit(train_generator,
                epochs=25,
                validation_data=valid_generator)`

I am receiving the following error:

InvalidArgumentError: Graph execution error:

Detected at node 'predict/feature_vector/SpatialSqueeze' defined at (most recent call last): File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "/usr/lib/python3.7/runpy.py", line 85, in _run_code exec(code, run_globals) File "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py", line 16, in app.launch_new_instance() File "/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py", line 846, in launch_instance app.start() File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelapp.py", line 499, in start self.io_loop.start() File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 132, in start self.asyncio_loop.run_forever() File "/usr/lib/python3.7/asyncio/base_events.py", line 541, in run_forever self._run_once() File "/usr/lib/python3.7/asyncio/base_events.py", line 1786, in _run_once handle._run() File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run self._context.run(self._callback, *self._args) File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 122, in _handle_events handler_func(fileobj, events) File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper return fn(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 452, in _handle_events self._handle_recv() File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 481, in _handle_recv self._run_callback(callback, msg) File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 431, in _run_callback callback(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper return fn(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher return self.dispatch_shell(stream, msg) File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell handler(stream, idents, msg) File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request user_expressions, allow_stdin) File "/usr/local/lib/python3.7/dist-packages/ipykernel/ipkernel.py", line 208, in do_execute res = shell.run_cell(code, store_history=store_history, silent=silent) File "/usr/local/lib/python3.7/dist-packages/ipykernel/zmqshell.py", line 537, in run_cell return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2718, in run_cell interactivity=interactivity, compiler=compiler, result=result) File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2822, in run_ast_nodes if self.run_code(code, result): File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 4, in baseModel = hub.KerasLayer(classifier_url, input_shape=(224,224,3), output_shape=[1280], name="Mobilenet") File "/usr/local/lib/python3.7/dist-packages/tensorflow_hub/keras_layer.py", line 153, in init self._func = load_module(handle, tags, self._load_options) File "/usr/local/lib/python3.7/dist-packages/tensorflow_hub/keras_layer.py", line 449, in load_module return module_v2.load(handle, tags=tags, options=set_load_options) File "/usr/local/lib/python3.7/dist-packages/tensorflow_hub/module_v2.py", line 106, in load obj = tf.compat.v1.saved_model.load_v2(module_path, tags=tags) Node: 'predict/feature_vector/SpatialSqueeze' Can not squeeze dim[1], expected a dimension of 1, got 2 [[{{node predict/feature_vector/SpatialSqueeze}}]] [Op:__inference_train_function_253305]

CodePudding user response:

Make sure you have the same image size (224, 224) in flow_from_directory and in the hub.KerasLayer. Here is a working example:

import tensorflow_hub as hub
import tensorflow as tf

img_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255, rotation_range=20)

flowers = tf.keras.utils.get_file(
    'flower_photos',
    'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
    untar=True)

train_ds = img_gen.flow_from_directory(flowers, target_size=(224, 224), batch_size=32, shuffle=True)
classifier_url ="https://hub.tensorflow.google.cn/google/tf2-preview/mobilenet_v2/feature_vector/4"
baseModel = hub.KerasLayer(classifier_url, input_shape=(224,224,3), output_shape=[1280], 
name="Mobilenet")
baseModel.trainable = False # freeze mobilenet weights
myModel = tf.keras.Sequential(name="Mobilenet_tranferLearning")
myModel.add(baseModel)
myModel.add(tf.keras.layers.Flatten())
myModel.add(tf.keras.layers.Dropout(0.2))
myModel.add(tf.keras.layers.Dense(5,activation='softmax'))
myModel.summary()

myModel.compile(optimizer= 'adam', loss= 'categorical_crossentropy', metrics= ['accuracy'])

history = myModel.fit(train_ds, epochs=25)
  • Related