Skip to content

A convolutional neural network from scratch to classify images (demo available)

Notifications You must be signed in to change notification settings

ZachEddy/ImageClassifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Image Classifier

A convolutional neural network implemented in Python to classify images in the following ten categories:
  • Airplanes
  • Automobiles
  • Birds
  • Cats
  • Deer
  • Dogs
  • Frogs
  • Horses
  • Ships
  • Trucks

Background

Neural networks take input(s), perform computation in sequential layers, then generate output(s). For example, a network could determine an athlete's sport given height, weight, and favorite Gatorade flavor. It may find that most heavy-set tall people play football, and most lightweight tall people run long-distance. Gatorade flavor presumably won't influence classification. For instance, it seems unlikely that most football players prefer grape, while runners prefer strawberry. Physical attributes offer better insight in this context, not matters of personal preference — this is something a neural network can learn. However, the network has to undergo training before it can make classifications accurately. This process involves individually inputting hundreds, thousands, or potentially millions of examples with known outcomes. Each time the network classifies incorrectly, it will adjust slightly with the goal of improving accuracy.

Convolutional networks are similar, but function for three-dimensional inputs. Images are two-dimensional along height and width. They form a third, depth-wise dimension with RGB color channels. This particular network can examine images, find recurrent patterns, then classify based on what it learned. For example, after looking at hundreds of horses, it will eventually discover the common features — ears, noses, hoofs, etc.

TLDR: Neural networks make informed classifications between input and ouput after training on example data. Basic neural networks are one-dimensional, convolutional neural networks are three-dimensional and more complex.

Dependencies

NumPy: a math library written in Python. You can install it via command-line if necessary:

$ pip install numpy

More information about setting up NumPy here.

Installing

Clone the repository and you're set.
$ git clone https://github.com/ZachEddy/ImageClassifier

Note: this includes the CIFAR-10, a compressed set of 50,000 32x32 images. Cloning could take 3-5 minutes.

Running

Quickstart

Run python __main__.py at the top-level directory. I have the network defaulted to load a pre-trained network and classify ten images from a testing set. The default network trained overnight with 40,000 total images.

Create your own network

You can easily train, save, and load your own network. Inside net_initialize.py, you can create networks with different layer patterns. Each layer has a few user-defined parameters. For example, you can change the number of filters in a convolution layer with the following:

{'type':'conv', 'field_size':5, 'filter_count':10, 'stride':1, 'padding':2, 'name':"conv_one"}
{'type':'conv', 'field_size':5, 'filter_count':15, 'stride':1, 'padding':2, 'name':"conv_one"}

Changing the layer pattern and/or their associated layer parameters will impact:

  1. network accuracy - how consistently the network classifies correctly.
  2. training time - how long it takes the network to learn.

Lastly, this line of code inside net_initialize creates a new network based on the layer structure provided by the user:

return net_network(layer_structure,"network_name")

After training completes, the weights, biases, and layer structure get saved as network_name in the saved_networks directory.

Load a network

Alternatively, you can load a network to classify something after the training process finishes.

return net_network(None,"pretrained_network_name")

None informs the network that no layer structure has been provided by the user. It will search for pretrained_network inside the saved_networks directory instead.


Take a look at the net_initialize.py code; I think this will make it much more conceptually concrete. Also, having a basic understanding of Convolutional Neural Networks will really help. I recommend this detailed explanation from a Stanford course on ConvNets.

Reference

About

A convolutional neural network from scratch to classify images (demo available)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages