mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

The shapes of symbols differ from those inferred

Open helloworldlxb opened this issue 10 years ago • 2 comments

import mxnet as mx
import numpy as np
import cv2

data = mx.sym.Variable('data')
conv1 = mx.sym.Convolution(data=data, kernel=(3,3), num_filter=1024)
conv1 = mx.sym.Activation(data=conv1, act_type="relu")
pool1 = mx.sym.Pooling(data=conv1, kernel=(2,2), stride=(2,2), pool_type='max')
conv2 = mx.sym.Convolution(data=pool1, kernel=(3,3), num_filter=1024)
conv2 = mx.sym.Activation(data=conv2, act_type="relu")
pool2 = mx.sym.Pooling(data=conv2, kernel=(3,3), stride=(2,2), pool_type='max')

pred = mx.sym.FullyConnected(data=pool2, num_hidden=2)
label = mx.sym.Variable('softmax_label')
loss = mx.sym.SoftmaxOutput(data=pred, label=label)

# input_shapes = {'data':(86001,1,240,320)}
# print conv1.infer_shape(**input_shapes)
# print pool1.infer_shape(**input_shapes)
# print conv2.infer_shape(**input_shapes)
# print pool2.infer_shape(**input_shapes)
# print pred.infer_shape(**input_shapes)
# print loss.infer_shape(**input_shapes)


model = mx.model.FeedForward(loss, num_epoch = 2000, learning_rate=0.01)

train_file = open('F:/User/Lxb/data/KTH/cnn_train.txt')
X = []
Y = []
for line in train_file:
    filename = line.split()[0]
    label = line.split()[1][0]
    if label == '2':
        break
    X.append(cv2.imread("F:/User/Lxb/data/KTH/train_cnn/" + filename, cv2.IMREAD_GRAYSCALE))
    Y.append(int(label))
X = np.array(X).reshape((-1,1,240,320))
Y = np.array(Y)
print X.shape, Y.shape
train_file.close()

# # val_file = open('F:/User/Lxb/data/KTH/cnn_val.txt')
# # val_X = []
# # val_Y = []
# # for line in val_file:
    # # filename = line.split()[0]
    # # label = line.split()[1][0]
    # # if label == '2':
        # # break
    # # val_X.append(cv2.imread("F:/User/Lxb/data/KTH/val_cnn/" + filename, cv2.IMREAD_GRAYSCALE))
    # # val_Y.append(int(label))
# # val_X = np.array(val_X).reshape((-1,1,240,320))
# # val_Y = np.array(val_Y)

# # val_file.close()
# # print "Loading Finished"
model.fit(X=X, y=Y)# , eval_data=(val_X, val_Y,))

When inferring the shape, it works well. However, when fitting, it reports: mxnet.base.MXNetError: include\mxnet/ndarray.h: 245: Check failed: (shape_.Size()) >= (shape.Size()) NDArray.Reshape: target sha pe size is different from current shape

helloworldlxb avatar May 10 '16 04:05 helloworldlxb

Well, I reduced the number of filters and then it worked...

helloworldlxb avatar May 10 '16 05:05 helloworldlxb

This issue is closed due to lack of activity in the last 90 days. Feel free to reopen if this is still an active issue. Thanks!

phunterlau avatar Sep 28 '17 05:09 phunterlau