I am new at keras and CNN and am working on building at CNN for sequential analysis of movement in a image. What I am having issues with is the reshaping the data and the labels that go into the fitting and testing the data for the model. So the original size/shape of the numpy file is (18, 50,50,16) which is saved in a text file from another program. I know the text file is ok because I can read it in and display it correctly with the debug_potion method. So that looks good. There are 18 images in the folder for the data and otherData variable. I dont really know what the 16 is but the image size are 50*50.

The issue is the reshaping of that data is the problem. Can anyone suggest how to reshape this data in a way that I can train it. I think I need to do onehot encoding but not quite sure how. Any help will be appreciated. Here is what I have so far.

def debug_potion(data):
    for i in range(0, 16):
        # get for each joint
        potion = Potion(data)
        plt.show(potion.display(joints=[i, i], channels=[0, 1, 2]))

# Build Model for sequential building
def create_model(input_shape):
    model = Sequential()
    model.add(Conv2D(32, kernel_size=(3, 3),
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dense(128, activation='relu'))
    model.add(Dense(num_classes, activation='softmax'))

    return model

def read_in_data(file):
    with open(file, "rb") as f:
        return pickle.load(f)

batch_size = 1
num_classes = 2
epochs = 12

# input image dimensions
img_rows, img_cols = 50, 50
input_shape = (img_rows, img_cols, 16)

data = read_in_data("heatmap.txt")
otherData = read_in_data("othermove.txt")

data = np.array(data)
otherData = np.array(otherData)
print("Data shape", otherData.shape)

xtrain = []
ytrain = []
xtest  = []
ytest  = []

for x in range(0,len(data)):
    if x < 15:
        ytrain.append(("FWAC", num_classes))
        ytest.append(("FWAC", num_classes))

for x in range(0,len(otherData)):
    if x < 15:
        ytrain.append(("Other", num_classes))
        ytest.append(("Other", num_classes))

#Create x test and train arrays
xtrain = np.array(xtrain)
xtrain = xtrain.reshape(30,img_rows, img_cols,16)
ytrain = np.array(ytrain)

xtest = np.array(xtest)
xtest = xtest.reshape(108, img_rows, img_cols,16)
ytest = np.array(ytest)

# Build Model
model = create_model(input_shape)

model.fit(xtrain, ytrain,
          batch_size = batch_size,
          epochs = epochs,
          validation_data=(xtest, ytest))

score = model.evaluate(xtest,ytest, verbose=0)
print(("Test loss", score[0]))
print("Test Accuracy", score[1])
  • $\begingroup$ Videos generally have 4 dimensions $\endgroup$
    – Aditya
    Oct 31, 2018 at 6:03

1 Answer 1


As far as labels are concerned, you can one-hot-encode by using (assuming ytrain is converted to numpy array) the below code. Image reshaping looks fine but if you are having issues with image reshaping then, you might be giving the first argument i.e., the number of images wrong. So try this

    xtrain = xtrain.reshape(xtrain.shape[0],img_rows,img_cols,16)

    ytrain = keras.utils.to_categorical(ytrain, num_classes)

Make sure you import to_categorical from keras.utils

  • $\begingroup$ That worked thank you for helping me out. I am still learning and working at figuring all these labels and stuff out $\endgroup$
    – MNM
    Nov 6, 2018 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.