<jupyter_start><jupyter_text>Preparing data for the target model<jupyter_code># Change the column to take as the feature (i.e., Finger, Handedness)
feature_column = "Handedness"

# Use this filter to specify the fingers to taken into account (e.g., thumb only, thumb/others, etc.)
df = df[df.Finger == "thumb"]<jupyter_output><empty_output><jupyter_text>Create training and test sets<jupyter_code>split_factor = 0.8
ptcp_train = df.Participant.unique()[:math.floor(len(df.Participant.unique())*split_factor)]
ptcp_test = df.Participant.unique()[math.floor(len(df.Participant.unique())*split_factor):]
df_train = df[df.Participant.isin(ptcp_train)].copy(deep=True)
df_test = df[df.Participant.isin(ptcp_test)].copy(deep=True)
# Training set: Convert target names into numbers for classification
X = ""
y = ""
target_names = df[feature_column].unique()
for l in target_names:
    df_temp = df_train[(df_train[feature_column] == l)]
    X_temp = [df_temp.BlobImgFlattened.iloc[i] for i in range(len(df_temp))]
    y_temp = np.ones(len(X_temp)) * np.where(target_names == l)[0][0]
    
    if (type(X) is str):
        X = X_temp
        y = y_temp
    else:
        X.extend(X_temp)
        y = np.append(y, y_temp)
train_x = np.array(X)
train_y = np.array(y)


# Test set: Convert target names into numbers for classification
X = ""
y = ""
for l in target_names:
    df_temp = df_test[(df_test[feature_column] == l)]
    X_temp = [df_temp.BlobImgFlattened.iloc[i] for i in range(len(df_temp))]
    y_temp = np.ones(len(X_temp)) * np.where(target_names == l)[0][0]
    
    if (type(X) is str):
        X = X_temp
        y = y_temp
    else:
        X.extend(X_temp)
        y = np.append(y, y_temp)

test_x = np.array(X)
test_y = np.array(y)<jupyter_output><empty_output><jupyter_text>Train and test with Keras<jupyter_code>batch_size = 50
num_classes = len(df_train[feature_column].unique())
epochs = 10000 # run until it gets stopped...

# input image dimensions
img_rows, img_cols = 27, 15

# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = (train_x, train_y), (test_x, test_y)
# GPU Config
config = tf.ConfigProto(log_device_placement=True, allow_soft_placement=True, device_count = {'GPU' : 4})
config.gpu_options.allow_growth=True
config.gpu_options.per_process_gpu_memory_fraction=0.2
config.gpu_options.allocator_type = 'BFC'
with tf.device('/gpu:1'):
    session = tf.Session(config=config)
    K.set_session(session)

    if K.image_data_format() == 'channels_first':
        x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
        x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
        input_shape = (1, img_rows, img_cols)
    else:
        x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
        x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
        input_shape = (img_rows, img_cols, 1)

    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')

    # remove all negative values (noise)
    x_train[x_train < 0] = 0
    x_test[x_test < 0] = 0
    
    # max value is 268 for our data set
    x_train /= 268
    x_test /= 268
    
    print('x_train shape:', x_train.shape)
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')

    # convert class vectors to binary class matrices
    y_train = keras.utils.to_categorical(y_train, num_classes)
    y_test = keras.utils.to_categorical(y_test, num_classes)

    model = Sequential()

    model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', 
                     activation ='relu', input_shape = input_shape))
    model.add(BatchNormalization(axis=-1))
    model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', 
                     activation ='relu'))
    model.add(BatchNormalization(axis=-1))
    model.add(MaxPool2D(pool_size=(2,2)))
    model.add(Dropout(0.5))


    model.add(Conv2D(filters = 128, kernel_size = (3,3),padding = 'Same', 
                     activation ='relu'))
    model.add(BatchNormalization(axis=-1))
    model.add(Conv2D(filters = 128, kernel_size = (3,3),padding = 'Same', 
                     activation ='relu'))
    model.add(BatchNormalization(axis=-1))
    model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
    model.add(Dropout(0.5))


    model.add(Flatten())
    model.add(Dense(256, activation = "relu", 
                    kernel_initializer=glorot_uniform()
                   ))
    model.add(BatchNormalization(axis=-1))
    model.add(Dropout(0.5))
    model.add(Dense(num_classes, activation = "softmax"))
    
    
    optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)

    model.compile(loss=keras.losses.categorical_crossentropy,
                  optimizer=optimizer,
                  metrics=['accuracy'])

    learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', 
                                            patience=3, 
                                            verbose=1, 
                                            factor=0.5, 
                                            min_lr=0.00001)
    
    # Change to your TensorBoard folder:
    tensorflowFolder = "/home/huyvietle/tensorboardfiles/FingerSpecific_LeftVsRightThumb_118"
    tfbCallback = keras.callbacks.TensorBoard(log_dir=tensorflowFolder, histogram_freq=0,  
              write_graph=True, write_images=True)

    model.fit(x_train, y_train,
              batch_size=batch_size,
              epochs=epochs,
              verbose=1,
              validation_data=(x_test, y_test),
              callbacks=[tfbCallback, learning_rate_reduction])
    score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
model.save("model.h5")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13115856734487977, 'fraction_numerical': 0.02135530519589709, 'mean_word_length': 3.1857846586910625, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '194071', 'license': 'MIT', 'n_tokens_mistral': 2168, 'n_tokens_neox': 2055, 'n_words': 387, 'path': '02-Classification.ipynb', 'repo_name': 'interactionlab/CapFingerId', 'size': '5947'}