Blog posts

2023

Calm Hands, help reduce nail-biting using computer vision and AI

3 minute read

Published:

drawing Calm Hands helps the user reduce nail-biting during computer use. It provides realtime feedback about nail-biting habits using a deep neural net that monitors images from your webcam stream. This process is entirely local and images are never saved. Feedback is provided through audio and visual cues to alert you of when you are biting your nails. Realtime data visualization is provided as well. Check out the full repo here. Built with: Fastai, OpenCV, Tkinter, CustomTkinter, Matplotlib.

How I Made This

Step 1. Collect training and heldout test images

First I had to collect several hundred images of my biting my nails and not biting my nails. So I created camera.py and the Cam class. I call this in collect_training_data.ipynb. This allowed me to collect hundreds of photos in a variety of locations, lighting setups, and angles very easily.
from camera import Cam

cam = Cam()
# Collect 2 frames per second for 60 seconds.
cam.write_frame_stream(length = 60, wait = .5, path = 'frames/biting')

Step 2. Train the image classifier in Google Colab with fastai

I trained an edgenext_small model (imported from the timm library) using fastai. I used the proven method of finetuning a pretrained image classifier on this specific task.
learn = vision_learner(dls, 'edgenext_small', metrics=error_rate)
learn.fine_tune(3,base_lr = .001)
With ~1000 images and 3 cycles of training, I was at >90% accuracy. But I found there were specific positions and angles that the model was getting wrong.

Step 3. Collecting more data based on model mistakes

I created the realtime_model_preds.ipynb to collect more data quickly, based on the predictions of the existing model. First I made a couple of functions to make predictions and print them out.
def do_prediction(frame):
    with learn.no_bar(), learn.no_logging():
        return learn.predict(frame)

def print_pred(pred):

    conf = round(float(pred[2][pred[1]])*100,2)
    output = f'The model is {conf}% confident that you are {pred[0]}'
    print(output)
Then, I fed the functions a stream of frames from the webcam. This would print out the model predictions of each frame from my webcam.
    # If I press 1, save the frame to the 'good' folder.
    # If I press 2, save it to the biting folder
    key_press_dict = {49:'train/good',50:'train/bad'}
    path = cam.get_path_from_keypress(key_press_dict)
    cam.write_frame(frame,labelled_folder_path=path)
I moved around until I found positions that the model was incorrectly predicting. Then, I pressed either ‘1’ or ‘2’ on the keyboard to save that frame to the correct folder and add it to the training set. After collecting several hundred more photos, I retrained the model. Now it was performing much better on the edge cases.

Step 4. Creating the app

I used tkinter and customtkinter to create the GUI for the app. It displays the webcam feed on one side, and displays an matplotlib plot of the predictions of the model. It also provides instant auditory feedback if I’m biting my nails. Creating the GUI was much more complicated than I thought. I’ve never really created a user interface before, and getting all of the positioning and callbacks working correctly took a while. Also, getting the plot to update smoothly and correctly took a lot of effort. But I think the final product looks and functions well! drawing Read more

2022

Advice for becoming a data scientist as a psychology/neuroscience PhD student

2 minute read

Published:

Here’s some advice if you’re considering moving into data science during a psychology PhD. I tried to make it as practical as possible.

1. Learn fundamentals of programming, statistics, and data science.

Learn Python or R, and probably not Matlab. I see way fewer positions that mention Matlab compared to R and especially Python. Take online courses in machine learning and statistics. I highly recommend The Missing Semester of Your CS Education if you have no CS background, like me. Don’t start with deep learning and neural networks. Make sure you understand data science and machine learning fundamentals very well before moving onto deep learning. If you do, I like the fastai course. And check out some of my other recommended Courses & Textbooks.

2. Do data science.

This obviously happens in parallel to step 1. Most neuroscience/psychology research already involves lots of data science elements. You’re probably already a better data scientist than you realize. Building and cleaning data sets, data visualization, statistical testing, modelling, predictions, writing, science communication, etc. Emphasize these aspects of your research. Projects and publications will help show your skills on your resume and during interviews. Make a website to showcase your research and side projects. I use the Minimal Mistakes GitHub Page theme.

3. Communicate early and often.

Tell your PI/advisor that you’re considering doing a summer internship. Some advisors might not let you. I was lucky enough to have a supportive advisor. Tell you’re lab mates, fellow grad students, and collaborators. When people know you’re interested, they are more likely to tell you about interesting positions they know of.

4. Apply to internships.

Aim to do at least 1 summer internship during your PhD. 2 even is better. Ideally between your 3rd & 4th, and 4th & 5th years. Apply to lots of internships. I think I applied to over 50. Use job posting sites like Indeed or Google Jobs. Check Twitter (search “PhD Internship”, “PhD Data Internship”, etc.). Ask friends and collaborators who you know went into industry. Connections might be extremely important because, honestly, it can be difficult to convince recruiters that a psychology PhD student would make a data scientist.

5. Do your internships.

Do a data science internship! You’ll hopefully learn a lot about putting models into production, collaboration, and more structured development. Some internships turn into full-time offers. Once you have one or two internships under your belt, you’ll be in a much better position when applying for full-time jobs upon graduation.

Additional reading.

Advice for PhD Students Thinking about Data Science Internships
Crushed it! Landing a data science job
What candidates can and cannot control in their job hunt
Advice for Applying to Data Science Jobs Read more

Competitive fighting game with heuristic-based AI

6 minute read

Published:

I created a 1v1 fighting game in Python using PyGame. Included is a 1-player option that allows you to play against a challenging AI opponent. It can make fast decisions based on many different factors of the current game state. I personally lose to it almost half of the time. Read more

2021

Visualizing and Analyzing Bicycle Infrastructure using OSMnx

6 minute read

Published:

I’ve commuted by bicycle for over 6 years. After moving to Chicago for graduate school, I quickly came to appreciate the dedicated lakefront bike trail. Bike trails and bike lanes make commuting much safer and more enjoyable. However, some cities do a better job than others at building this infrastructure. The goal of this project is to visualize cycleway networks and find the most and least bikeable US cities. Check out the Github repo here! Read more

2020