MobileNet icon indicating copy to clipboard operation
MobileNet copied to clipboard

ValueError: Can not squeeze dim[1], expected a dimension of 1, got 3 for 'MobileNet/SpatialSqueeze' (op: 'Squeeze') with input shapes: [32,3,17,1024].

Open sridarah opened this issue 7 years ago • 3 comments

Dear Zehao Shi,

I am receiving the following error while running your model.

(?, ?, 3) Traceback (most recent call last): File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 671, in _call_cpp_shape_fn_impl input_tensors_as_shapes, status) File "C:\ProgramData\Anaconda3\lib\contextlib.py", line 89, in exit next(self.gen) File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 466, in raise_exception_on_not_ok_status pywrap_tensorflow.TF_GetCode(status)) tensorflow.python.framework.errors_impl.InvalidArgumentError: Can not squeeze dim[1], expected a dimension of 1, got 3 for 'MobileNet/SpatialSqueeze' (op: 'Squeeze') with input shapes: [32,3,17,1024].

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:/Users/hp/PycharmProjects/Mobilenets/MobileNet-master/train_object_detector.py", line 628, in tf.app.run() File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\platform\app.py", line 48, in run _sys.exit(main(_sys.argv[:1] + flags_passthrough)) File "C:/Users/hp/PycharmProjects/Mobilenets/MobileNet-master/train_object_detector.py", line 524, in main clones = model_deploy.create_clones(deploy_config, clone_fn, [batch_queue]) File "C:\Users\hp\PycharmProjects\Mobilenets\MobileNet-master\deployment\model_deploy.py", line 195, in create_clones outputs = model_fn(args, kwargs) File "C:/Users/hp/PycharmProjects/Mobilenets/MobileNet-master/train_object_detector.py", line 499, in clone_fn end_points = network_fn(images) File "C:\Users\hp\PycharmProjects\Mobilenets\MobileNet-master\nets\nets_factory.py", line 112, in network_fn return func(images, num_classes, is_training=is_training, width_multiplier=width_multiplier) File "C:\Users\hp\PycharmProjects\Mobilenets\MobileNet-master\nets\mobilenet.py", line 83, in mobilenet net = tf.squeeze(net, [1, 2], name='SpatialSqueeze') File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 2259, in squeeze return gen_array_ops._squeeze(input, axis, name) File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 3378, in _squeeze squeeze_dims=squeeze_dims, name=name) File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 768, in apply_op op_def=op_def) File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2338, in create_op set_shapes_for_outputs(ret) File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1719, in set_shapes_for_outputs shapes = shape_func(op) File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1669, in call_with_requiring return call_cpp_shape_fn(op, require_shape_fn=True) File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 610, in call_cpp_shape_fn debug_python_shape_fn, require_shape_fn) File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 676, in _call_cpp_shape_fn_impl raise ValueError(err.message) ValueError: Can not squeeze dim[1], expected a dimension of 1, got 3 for 'MobileNet/SpatialSqueeze' (op: 'Squeeze') with input shapes: [32,3,17,1024].*

Please suggest me a solution. Thanks!

sridarah avatar Aug 21 '17 10:08 sridarah

Was this ever resolved?

braddengross avatar Feb 19 '18 21:02 braddengross

This problem is from the input image size , some models like inception_v1 and inception_v2 needs an input size (1,224,224,3) while inception_resnet_v2 needs an input size of (1,299,299,3)

Mohamedsabry109 avatar Oct 07 '18 11:10 Mohamedsabry109

Hi I am also facing issue regarding this "InvalidArgumentError: Can not squeeze dim[1], expected a dimension of 1, got 5"

Here is my code ` def create_tag_mapping(mapping_csv): # create a set of all known tags labels = set() for i in range(len(mapping_csv)): tags = mapping_csv['Labels'][i].split(' ') labels.update(tags) labels = list(labels) labels.sort() # dict that maps labels to integers, and the reverse labels_map = {labels[i]:i for i in range(len(labels))} inv_labels_map = {i:labels[i] for i in range(len(labels))} return labels_map, inv_labels_map

def create_file_mapping(mapping_csv): mapping = dict() for i in range(len(mapping_csv)): name, tags = mapping_csv['Id'][i], mapping_csv['Labels'][i] mapping[name] = tags.split(' ') return mapping

def custom_encode(tags, mapping): encoding=[] for tag in tags: if tag == 'L': encoding.append(0) elif tag == 'M': encoding.append(1) else: encoding.append(2) return encoding

def load_dataset(path, file_mapping, tag_mapping): photos, targets = list(), list() for filename in os.listdir(path): photo = load_img(path + filename, target_size=(760,415)) photo = img_to_array(photo, dtype='uint8') tags = file_mapping[filename[:-4]] target = custom_encode(tags, tag_mapping) photos.append(photo) targets.append(target) X = np.asarray(photos, dtype='uint8') y = np.asarray(targets, dtype='uint8') return X, y

trainingLabels = 'training-labels.csv' mapping_csv = pd.read_csv(trainingLabels) tag_mapping, _ = create_tag_mapping(mapping_csv) file_mapping = create_file_mapping(mapping_csv)

folder = 'images/dataset/'

X, y = load_dataset(folder, file_mapping, tag_mapping) print(X.shape, y.shape)

trainX, testX, trainY, testY = train_test_split(X, y, test_size=0.3, random_state=1) print(trainX.shape, trainY.shape, testX.shape, testY.shape)

img_x,img_y=760,415 trainX=trainX.reshape(trainX.shape[0], img_x,img_y,3) testX=testX.reshape(testX.shape[0], img_x,img_y,3)

model = Sequential() model.add(Conv2D(32, (5, 5), strides=(1,1), activation='relu', input_shape=(img_x, img_y,3))) model.add(MaxPooling2D((2, 2), strides=(2,2))) model.add(Flatten()) model.add(Dense(128, activation='relu', kernel_initializer='he_uniform')) model.add(Dense(15)) model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy']) history=model.fit(trainX, trainY, batch_size=15, epochs=10, verbose=1)`

umairshahid436 avatar Nov 13 '19 15:11 umairshahid436