0
$\begingroup$

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),
                 activation='relu',
                 input_shape=input_shape))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(num_classes, activation='softmax'))

    model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])
    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:
        xtrain.append(data)
        ytrain.append(("FWAC", num_classes))
    else:
        xtest.append(data)
        ytest.append(("FWAC", num_classes))

for x in range(0,len(otherData)):
    if x < 15:
        xtrain.append(otherData)
        ytrain.append(("Other", num_classes))
    else:
        xtest.append(otherData)
        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)
print(xtrain.shape)
print(xtest.shape)
print(ytrain.shape)
print(ytest.shape)

# Build Model
model = create_model(input_shape)

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

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

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

| improve this answer | |
$\endgroup$
  • $\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 '18 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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