Skip to content

Instantly share code, notes, and snippets.

@AyishaR
AyishaR / vww_dataset_prep.py
Last active July 30, 2023 16:13
Create a subset of the COCO dataset with appropriate labels to train a model for visual wake word detection.
import pandas as pd
import json
import numpy as np
import os
ax = json.load(open('')) # add link to json file from COCO official website
ax = ax['annotations'] # annotations hold category information
# create new dataframe to hold information on images and corresponding categories they hold
clm = ['image_id', 'category_id']
model = models.Sequential([
layers.Dense(128, activation = 'relu', input_shape = Xtrain[0].shape),
layers.Dense(64, activation = 'relu'),
#layers.Dense(16, activation = 'relu'),
layers.Dense(8, activation = 'relu'),
layers.Dense(1)
])
cb = callbacks.EarlyStopping(patience = 10, restore_best_weights = True)
# Splitting into train, val and test set -- 80-10-10 split
# First, an 80-20 split
train_df, val_test_df = train_test_split(df, test_size = 0.2)
# Then split the 20% into half
val_df, test_df = train_test_split(val_test_df, test_size = 0.5)
# Splitting into X (input) and y (output)
# defining the input and output columns to separate the dataset in the later cells.
input_columns = df.columns.tolist()
input_columns.remove('label')
output_columns = ['label']
df[['red','green','blue']] /= 255
# pick random test data sample from one batch
x = random.randint(0, len(Xtest) - 1)
output = model.predict(Xtest[x].reshape(1, -1))[0]
pred = np.argmax(output)
print("Predicted: ", pred, "(", output[pred], ")")
print("True: ", np.argmax(np.array(ytest)[x]))
model = models.Sequential([
layers.Dense(256, activation = 'relu', input_shape = Xtrain[0].shape),
layers.Dense(64, activation = 'relu'),
layers.Dense(16, activation = 'relu'),
layers.Dense(4, activation = 'softmax')
])
cb = [callbacks.EarlyStopping(patience = 5, restore_best_weights = True)]
model.compile(optimizer=optimizers.Adam(0.0001), loss=losses.CategoricalCrossentropy(), metrics=['accuracy'])
ss_scaler = StandardScaler()
# Fit on training set alone
Xtrain = ss_scaler.fit_transform(Xtrain)
# Use it to transform val and test input
Xval = ss_scaler.transform(Xval)
Xtest = ss_scaler.transform(Xtest)