MobileNet
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].
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
Please suggest me a solution. Thanks!
Was this ever resolved?
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)
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)`