Must the input height of a 1D CNN be constant?

末鹿安然 提交于 2019-12-13 03:38:53

问题


I'm currently doing my honours research project on online/dynamic signature verification. I am using the SVC 2004 dataset (Task 2). I have done the following data processing:

def load_dataset_normalized(path):
file_names = os.listdir(path)

num_of_persons = len(file_names)

initial_starting_point = np.zeros(np.shape([7]))

x_dataset = []
y_dataset = []

for infile in file_names:
    full_file_name = os.path.join(path, infile)
    file = open(full_file_name, "r")
    file_lines = file.readlines()

    num_of_points = int(file_lines[0])

    x = []
    y = []
    time_stamp = []
    button_status = []
    azimuth_angles = []
    altitude = []
    pressure = []

    for idx, line in enumerate(file_lines[1:]):
        idx+=1
        nums = line.split(' ')

        if idx == 1:
            nums[2] = 0
            initial_starting_point = nums

            x.append(int(nums[0]))
            y.append(int(nums[1]))
            time_stamp.append(0)
            button_status.append(int(nums[3]))
            azimuth_angles.append(int(nums[4]))
            altitude.append(int(nums[5]))
            pressure.append(int(nums[6]))

        else:
            x.append(int(nums[0]))
            y.append(int(nums[1]))
            time_stamp.append(10)
            button_status.append(int(nums[3]))
            azimuth_angles.append(int(nums[4]))
            altitude.append(int(nums[5]))
            pressure.append(int(nums[6]))

    max_x = max(x)
    max_y = max(y)
    max_azimuth_angle = max(azimuth_angles)
    max_altitude = max(altitude)
    max_pressure = max(pressure)

    min_x = min(x)
    min_y = min(y)
    min_azimuth_angle = min(azimuth_angles)
    min_altitude = min(altitude)
    min_pressure = min(pressure)

    #Alignment normalization:
    for i in range(num_of_points):
        x[i] -= int(initial_starting_point[0])
        y[i] -= int(initial_starting_point[1])
        azimuth_angles[i] -= int(initial_starting_point[4])
        altitude[i] -= int(initial_starting_point[5])
        pressure[i] -= int(initial_starting_point[6])

    #Size normalization
    for i in range(num_of_points):
        x[i] = ((x[i] - max_x) / (min_x - max_x))
        y[i] = ((y[i] - max_y) / (min_y - max_y))
        azimuth_angles[i] = ((azimuth_angles[i] - max_azimuth_angle) / (min_azimuth_angle - max_azimuth_angle))
        altitude[i] = ((altitude[i] - max_altitude) / (min_altitude - max_altitude))
        pressure[i] = ((pressure[i] - max_pressure) / (min_pressure - max_pressure))

    #data points to dataset
    x_line = []
    for i in range (num_of_points):
        x_line.append([x[i], y[i], time_stamp[i], button_status[i], azimuth_angles[i], altitude[i], pressure[i]])

        if i == num_of_points-1:
            x_dataset.append(x_line)

    infile_without_extension = infile.replace('.TXT','')
    index_of_s = infile_without_extension.find("S")
    index_of_num = index_of_s + 1
    sig_ID = int(infile_without_extension[index_of_num:])
    if sig_ID < 21:
        y_dataset.append([1,0])
    else:
        y_dataset.append([0,1])

x_dataset = np.asarray(x_dataset)
y_dataset = np.asarray(y_dataset)
return x_dataset, y_dataset

I also have another method that takes the values as they are in the text file and created an "original" dataset.

Now, the aim of my research is to create a CRNN (convolutional recurrent neural network) that can identify if a signature is authentic or forged. Here is the code for the model:

class crnn_model:
def __init__(self, trainX, trainy, testX, testy, optimizer_method):
    self.trainX = trainX
    self.trainy = trainy
    self.testX = testX
    self.testy = testy

    self.evaluate_model(optimizer_method)

def evaluate_model(self, optimizer_method):
    verbose, epochs, batch_size = 0, 40, 10
    n_timesteps, n_features, n_outputs = len(self.trainX), 7, 2
    print(n_timesteps)
    model = keras.Sequential()
    model.add(keras.layers.Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps, n_features), use_bias=True))
    model.add(keras.layers.Conv1D(filters=64, kernel_size=3, activation='relu'))
    model.add(keras.layers.Dropout(0.5))
    model.add(keras.layers.MaxPooling1D(pool_size=2))
    model.add(keras.layers.Flatten())
    model.add(keras.layers.LSTM(2, input_shape=[30592,1], return_sequences=True))
    model.summary()

    # Compile the model
    model.compile(optimizer=optimizer_method, loss='categorical_crossentropy', metrics=['accuracy'])

    #fit model
    model.fit(self.trainX, self.trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)

    # evaluate model
    _, accuracy = model.evaluate(self.testX, self.testy, batch_size=batch_size, verbose=0)
    return accuracy

Here is the problem I am having: the number of points used to store each signature is different, hence making the input height of the input matrix vary from one signature to the next. Must I now force the dataset to some uniform/constant number of points?

Much appreciated for your time.

来源:https://stackoverflow.com/questions/57759627/must-the-input-height-of-a-1d-cnn-be-constant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!