ValueError: cannot select an axis to squeeze out which has size not equal to one

Viewed 2719

I have a numpy array of shape out_hmaps=(2, 6, 64, 64, 1)

i want it to be (6, 64, 64, 1). I tried np.squeeze(np.array(out_hmaps),0) it worked once ,but then throws the following error.

 File "train.py", line 56, in <module>
xnet.train(epochs=args.epochs, model_path=args.model_path, batch_size=args.batch_size)
File "C:/Users/srira/Desktop/hourglass_keras-master/src/net\hourglass.py", line 54, in train
self.model.fit_generator(generator=train_gen, steps_per_epoch=train_dataset.get_dataset_size() // batch_size,
File "D:\Softwares\Anaconda\lib\site-packages\keras\engine\training.py", line 1918, in fit_generator
return self.fit(
File "D:\Softwares\Anaconda\lib\site-packages\keras\engine\training.py", line 1108, in fit
data_handler = data_adapter.get_data_handler(
File "D:\Softwares\Anaconda\lib\site-packages\keras\engine\data_adapter.py", line 1348, in 
get_data_handler
return DataHandler(*args, **kwargs)
File "D:\Softwares\Anaconda\lib\site-packages\keras\engine\data_adapter.py", line 1138, in __init__
self._adapter = adapter_cls(
File "D:\Softwares\Anaconda\lib\site-packages\keras\engine\data_adapter.py", line 793, in __init__
peek, x = self._peek_and_restore(x)
File "D:\Softwares\Anaconda\lib\site-packages\keras\engine\data_adapter.py", line 850, in _peek_and_restore
peek = next(x)
File "C:/Users/srira/Desktop/hourglass_keras-master/src/data_gen\mpii_datagen.py", line 88, in generator
yield np.array(train_input), np.squeeze(np.array(out_hmaps),0)
File "<__array_function__ internals>", line 5, in squeeze
File "D:\Softwares\Anaconda\lib\site-packages\numpy\core\fromnumeric.py", line 1508, in squeeze
return squeeze(axis=axis)
ValueError: cannot select an axis to squeeze out which has size not equal to one

Is there any other way to do it?

1 Answers

Method np.squeeze will only work if the axis you're looking to squeeze has a size of 1, otherwise, it simply can't make the axis disappear without discarding part of the array's data...

In you're case, going from (2, 6, 64, 64, 1), to (6, 64, 64, 1), you would just select one of the two sub-arrays. Either with out_hmaps[0] (first one), or out_hmaps[1] (second one).

Related