<commit_before># Automated Neuron Detection

## Overview
Calcium imaging is a common technique used to capture images of neuron activity. 
Once these images are captured, researchers often have to go through the time-consuming
task of manually identifying where the neurons are located in each image.

![movie](https://github.com/iamshang1/Projects/blob/master/Advanced_ML/Neuron_Detection/movie.gif)
![zooming](https://github.com/iamshang1/Projects/blob/master/Advanced_ML/Neuron_Detection/zooming.gif)

This exercise attempts to automate the process of neuron detection in calcium images
using machine learning techniques. The dataset used for this exercise consists of 19 training
and 9 test sets of neuron images downloaded from [http://neurofinder.codeneuro.org/](http://neurofinder.codeneuro.org/).
We use a convolutional neural networks with sliding window as our model for detecting neurons.

## Preprocessing
Each set of images in the codeneuro dataset is composed of several thousand images
over time of the same group of neurons. For each set of images, we create a single image by 
taking the mean pixel intensity for each pixel over time and clipping any pixel intensities three
standard deviations above the mean (across all pixels).

Before feeding the images to the convolutional network, each image was put through a standard
scalar so pixel values were normalized with mean 0 and standard deviation 1. Furthermore, random
horizontal and vertical flipping was used on the images during training.

Most of the original images are 512x512 pixels in shape. All images were 0-padded to 552x552 pixels
before they were sent to the convolutional neural network.

## Convolutional Network Architecture
Our convolutional neural network uses a sliding window of 40x40 pixels to predict a probability
for each of the center 20x20 pixels in the window. This probability represents the likelihood
that the given pixel belongs to a neuron.

We used the following architecture for our convolutional network: 
- 40x40 pixel sliding window input
- 3 convolutional layers using 3x3 filter shape, 50 feature maps each, ELU activation
- maxpool with 2x2 window
- 3 convolutional layers using 3x3 filter shape, 100 feature maps each, ELU activation
- feedforward layer of 2000 nodes, tanh activation
- output layer of 400 nodes representing 20x20 pixels in center of window
- adam gradient descent
- orthogonal weight initialization
- batch size 100, 10000 training iterations

During training, we were also careful to ensure that each training batch was
evenly split between pixels belonging to neurons and pixels that did not belong to
neurons.

During testing, we ran the sliding window with a stride of 1 across the test image. This means
every pixel was predicted multiple times. For each pixel, we took the mean probability across
all predictions and rounded it to 1 or 0 to determine the final label for that pixel.<commit_msg>clarified window selection used in training<commit_after># Automated Neuron Detection

## Overview
Calcium imaging is a common technique used to capture images of neuron activity. 
Once these images are captured, researchers often have to go through the time-consuming
task of manually identifying where the neurons are located in each image.

![movie](https://github.com/iamshang1/Projects/blob/master/Advanced_ML/Neuron_Detection/movie.gif)
![zooming](https://github.com/iamshang1/Projects/blob/master/Advanced_ML/Neuron_Detection/zooming.gif)

This exercise attempts to automate the process of neuron detection in calcium images
using machine learning techniques. The dataset used for this exercise consists of 19 training
and 9 test sets of neuron images downloaded from [http://neurofinder.codeneuro.org/](http://neurofinder.codeneuro.org/).
We use a convolutional neural networks with sliding window as our model for detecting neurons.

## Preprocessing
Each set of images in the codeneuro dataset is composed of several thousand images
over time of the same group of neurons. For each set of images, we create a single image by 
taking the mean pixel intensity for each pixel over time and clipping any pixel intensities three
standard deviations above the mean (across all pixels).

Before feeding the images to the convolutional network, each image was put through a standard
scalar so pixel values were normalized with mean 0 and standard deviation 1. Furthermore, random
horizontal and vertical flipping was used on the images during training.

Most of the original images are 512x512 pixels in shape. All images were 0-padded to 552x552 pixels
before they were sent to the convolutional neural network.

## Convolutional Network Architecture
Our convolutional neural network uses a sliding window of 40x40 pixels to predict a probability
for each of the center 20x20 pixels in the window. This probability represents the likelihood
that the given pixel belongs to a neuron.

We used the following architecture for our convolutional network: 
- 40x40 pixel sliding window input
- 3 convolutional layers using 3x3 filter shape, 50 feature maps each, ELU activation
- maxpool with 2x2 window
- 3 convolutional layers using 3x3 filter shape, 100 feature maps each, ELU activation
- feedforward layer of 2000 nodes, tanh activation
- output layer of 400 nodes representing 20x20 pixels in center of window
- adam gradient descent
- orthogonal weight initialization
- batch size 100, 10000 training iterations

During training, we selected random windows from the training images. We were careful to ensure 
that each training batch wasevenly split windows centered on neurons and windows not centered
on neurons.

During testing, we ran the sliding window with a stride of 1 across the test image. This means
every pixel was predicted multiple times. For each pixel, we took the mean probability across
all predictions and rounded it to 1 or 0 to determine the final label for that pixel.
{'fraction_non_alphanumeric': 0.03526830914613567, 'fraction_numerical': 0.022612217347283158, 'mean_word_length': 4.862512363996044, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3731797', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1337, 'n_words': 839}