Skip to content

Commit 97cb60c

Browse files
committed
Adding projects and CNNs
1 parent 0fa67e8 commit 97cb60c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+4831
-80
lines changed

.DS_Store

0 Bytes
Binary file not shown.
File renamed without changes.
File renamed without changes.

convolution_networks/.DS_Store

6 KB
Binary file not shown.
Binary file not shown.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import tensorflow as tf
2+
import numpy as np
3+
from conf import *
4+
from cifar10 import *
5+
6+
# Working of our algorithm is as follows:
7+
# Conv1_layer -> Conv2_layer -> Flatten_layer -> FullyConnected_layer -> FullyConnected_layer(With 10 Classes)
8+
9+
# Reading the data
10+
image_data, image_cls, img_one_hot_cls = (load_training_data())
11+
12+
image_data_flat = image_data.reshape([-1,3072])
13+
14+
# Function for defining weights
15+
def new_weights(shape):
16+
return tf.Variable(tf.truncated_normal(shape, stddev=0.05))
17+
18+
# Function for defining biases
19+
def new_bias(length):
20+
return tf.Variable(tf.constant(0.5, shape=[length]))
21+
22+
# Function to create the convolution layer with/without max-pooling
23+
def new_conv_layer(input, num_input_channels, filter_size, num_filters, use_pooling=True):
24+
shape = [filter_size, filter_size, num_input_channels, num_filters]
25+
weights = new_weights(shape = shape)
26+
biases = new_bias(length = num_filters)
27+
28+
# tf.nn.conv2d needs a 4D input
29+
layer = tf.nn.conv2d(input = input, filter= weights, strides=[1,1,1,1], padding='SAME')
30+
layer += biases
31+
if use_pooling:
32+
layer = tf.nn.max_pool(value = layer, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
33+
# relu activation function converts all negatives to zero
34+
layer = tf.nn.relu(layer)
35+
return layer, weights
36+
37+
# After all convolutions, we need to flatten the layer
38+
def flatten_layer(layer):
39+
layer_shape = layer.get_shape()
40+
num_features = layer_shape[1:4].num_elements()
41+
layer_flat = tf.reshape(layer, [-1, num_features])
42+
return layer_flat, num_features
43+
44+
# Fully connected layer
45+
def new_fc_layer(input, num_inputs, num_outputs, use_relu=True):
46+
weights = new_weights(shape=[num_inputs, num_outputs])
47+
biases = new_bias(length= num_outputs)
48+
layer = tf.matmul(input, weights) + biases
49+
if use_relu:
50+
layer = tf.nn.relu(layer)
51+
return layer
52+
53+
54+
# The placeholder to hold the X and Y values while training
55+
x = tf.placeholder(tf.float32, shape=[None, img_size_flat], name='x')
56+
y_true = tf.placeholder(tf.float32, shape=[None, 10], name='y_true')
57+
58+
x_image = tf.reshape(x, [-1, img_size, img_size, num_channels])
59+
y_true_cls = tf.argmax(y_true, dimension=1)
60+
61+
# The beginning of the process
62+
layer_conv1, weights_conv1 = new_conv_layer(input = x_image, num_input_channels= num_channels, filter_size = filter1_size, num_filters = number_of_filter1, use_pooling=True)
63+
layer_conv2, weights_conv2 = new_conv_layer(input = layer_conv1, num_input_channels= number_of_filter1, filter_size = filter2_size, num_filters = number_of_filter2, use_pooling=True)
64+
layer_flat, num_features = flatten_layer(layer_conv2)
65+
layer_fc1 = new_fc_layer(layer_flat, num_features, fc_size, True)
66+
layer_fc2 = new_fc_layer(layer_fc1, fc_size, num_classes, False)
67+
68+
# Finally Softmax function
69+
y_pred = tf.nn.softmax(layer_fc2)
70+
y_pred_cls = tf.argmax(y_pred, dimension=1)
71+
72+
# Cost function calculation and optimization function
73+
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=layer_fc2,labels=y_true)
74+
cost = tf.reduce_mean(cross_entropy)
75+
optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)
76+
77+
# Checking for the right predictions
78+
correct_prediction = tf.equal(y_pred_cls, y_true_cls)
79+
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
80+
81+
# TF Session initiation
82+
session = tf.Session()
83+
session.run(tf.global_variables_initializer())
84+
85+
# The trainer function to iterate the training process to learn further
86+
def optimize(num_iterations):
87+
x_batch, y_true_batch = image_data_flat, img_one_hot_cls
88+
feed_dict_train = {x: x_batch[:500], y_true: y_true_batch[:500]}
89+
feed_dict_test = {x: x_batch[500:1000], y_true: y_true_batch[500:1000]}
90+
for i in range(num_iterations):
91+
session.run(optimizer, feed_dict=feed_dict_train)
92+
# Print status every 10 iterations.
93+
if i % 10 == 0:
94+
# Calculate the accuracy on the training-set.
95+
acc = session.run(accuracy, feed_dict=feed_dict_test)
96+
97+
# Message for printing.
98+
print "Step ",i+1,': ', acc*100
99+
100+
optimize(STEPS)
101+
102+
# test_data = image_data[1115].reshape([1,3072])
103+
# print image_cls[1115]
104+
# print session.run(y_pred_cls, {x: test_data})
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
########################################################################
2+
#
3+
# Functions for downloading the CIFAR-10 data-set from the internet
4+
# and loading it into memory.
5+
#
6+
# Implemented in Python 3.5
7+
#
8+
# Usage:
9+
# 1) Set the variable data_path with the desired storage path.
10+
# 2) Call maybe_download_and_extract() to download the data-set
11+
# if it is not already located in the given data_path.
12+
# 3) Call load_class_names() to get an array of the class-names.
13+
# 4) Call load_training_data() and load_test_data() to get
14+
# the images, class-numbers and one-hot encoded class-labels
15+
# for the training-set and test-set.
16+
# 5) Use the returned data in your own program.
17+
#
18+
# Format:
19+
# The images for the training- and test-sets are returned as 4-dim numpy
20+
# arrays each with the shape: [image_number, height, width, channel]
21+
# where the individual pixels are floats between 0.0 and 1.0.
22+
#
23+
########################################################################
24+
#
25+
# This file is part of the TensorFlow Tutorials available at:
26+
#
27+
# https://github.com/Hvass-Labs/TensorFlow-Tutorials
28+
#
29+
# Published under the MIT License. See the file LICENSE for details.
30+
#
31+
# Copyright 2016 by Magnus Erik Hvass Pedersen
32+
#
33+
########################################################################
34+
35+
import numpy as np
36+
import pickle
37+
import os
38+
import download
39+
40+
########################################################################
41+
42+
# Directory where you want to download and save the data-set.
43+
# Set this before you start calling any of the functions below.
44+
data_path = "data/cifar10/"
45+
46+
# URL for the data-set on the internet.
47+
data_url = "https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz"
48+
49+
########################################################################
50+
# Various constants for the size of the images.
51+
# Use these constants in your own program.
52+
53+
# Width and height of each image.
54+
img_size = 32
55+
56+
# Number of channels in each image, 3 channels: Red, Green, Blue.
57+
num_channels = 3
58+
59+
# Length of an image when flattened to a 1-dim array.
60+
img_size_flat = img_size * img_size * num_channels
61+
62+
# Number of classes.
63+
num_classes = 10
64+
65+
########################################################################
66+
# Various constants used to allocate arrays of the correct size.
67+
68+
# Number of files for the training-set.
69+
_num_files_train = 1
70+
71+
# Number of images for each batch-file in the training-set.
72+
_images_per_file = 10000
73+
74+
# Total number of images in the training-set.
75+
# This is used to pre-allocate arrays for efficiency.
76+
_num_images_train = _num_files_train * _images_per_file
77+
78+
########################################################################
79+
# Private functions for downloading, unpacking and loading data-files.
80+
81+
82+
def _get_file_path(filename=""):
83+
"""
84+
Return the full path of a data-file for the data-set.
85+
86+
If filename=="" then return the directory of the files.
87+
"""
88+
89+
return os.path.join(data_path, filename)
90+
91+
92+
def one_hot_encoded(class_numbers, num_classes=None):
93+
"""
94+
Generate the One-Hot encoded class-labels from an array of integers.
95+
96+
For example, if class_number=2 and num_classes=4 then
97+
the one-hot encoded label is the float array: [0. 0. 1. 0.]
98+
99+
:param class_numbers:
100+
Array of integers with class-numbers.
101+
Assume the integers are from zero to num_classes-1 inclusive.
102+
103+
:param num_classes:
104+
Number of classes. If None then use max(cls)-1.
105+
106+
:return:
107+
2-dim array of shape: [len(cls), num_classes]
108+
"""
109+
110+
# Find the number of classes if None is provided.
111+
if num_classes is None:
112+
num_classes = np.max(class_numbers) - 1
113+
114+
return np.eye(num_classes, dtype=float)[class_numbers]
115+
116+
117+
def _unpickle(filename):
118+
"""
119+
Unpickle the given file and return the data.
120+
121+
Note that the appropriate dir-name is prepended the filename.
122+
"""
123+
124+
# Create full path for the file.
125+
file_path = _get_file_path(filename)
126+
127+
print("Loading data: " + file_path)
128+
129+
with open(file_path, mode='rb') as file:
130+
# In Python 3.X it is important to set the encoding,
131+
# otherwise an exception is raised here.
132+
data = pickle.load(file)
133+
134+
return data
135+
136+
137+
def _convert_images(raw):
138+
"""
139+
Convert images from the CIFAR-10 format and
140+
return a 4-dim array with shape: [image_number, height, width, channel]
141+
where the pixels are floats between 0.0 and 1.0.
142+
"""
143+
144+
# Convert the raw images from the data-files to floating-points.
145+
raw_float = np.array(raw, dtype=float) / 255.0
146+
147+
# Reshape the array to 4-dimensions.
148+
images = raw_float.reshape([-1, num_channels, img_size, img_size])
149+
150+
# Reorder the indices of the array.
151+
images = images.transpose([0, 2, 3, 1])
152+
153+
return images
154+
155+
156+
def _load_data(filename):
157+
"""
158+
Load a pickled data-file from the CIFAR-10 data-set
159+
and return the converted images (see above) and the class-number
160+
for each image.
161+
"""
162+
163+
# Load the pickled data-file.
164+
data = _unpickle(filename)
165+
166+
# Get the raw images.
167+
raw_images = data[b'data']
168+
169+
# Get the class-numbers for each image. Convert to numpy-array.
170+
cls = np.array(data[b'labels'])
171+
172+
# Convert the images.
173+
images = _convert_images(raw_images)
174+
175+
return images, cls
176+
177+
178+
########################################################################
179+
# Public functions that you may call to download the data-set from
180+
# the internet and load the data into memory.
181+
182+
183+
def maybe_download_and_extract():
184+
"""
185+
Download and extract the CIFAR-10 data-set if it doesn't already exist
186+
in data_path (set this variable first to the desired path).
187+
"""
188+
189+
download.maybe_download_and_extract(url=data_url, download_dir=data_path)
190+
191+
192+
def load_class_names():
193+
"""
194+
Load the names for the classes in the CIFAR-10 data-set.
195+
196+
Returns a list with the names. Example: names[3] is the name
197+
associated with class-number 3.
198+
"""
199+
200+
# Load the class-names from the pickled file.
201+
raw = _unpickle(filename="batches.meta")[b'label_names']
202+
203+
# Convert from binary strings.
204+
names = [x.decode('utf-8') for x in raw]
205+
206+
return names
207+
208+
209+
def load_training_data():
210+
"""
211+
Load all the training-data for the CIFAR-10 data-set.
212+
213+
The data-set is split into 5 data-files which are merged here.
214+
215+
Returns the images, class-numbers and one-hot encoded class-labels.
216+
"""
217+
218+
# Pre-allocate the arrays for the images and class-numbers for efficiency.
219+
images = np.zeros(shape=[_num_images_train, img_size, img_size, num_channels], dtype=float)
220+
cls = np.zeros(shape=[_num_images_train], dtype=int)
221+
222+
# Begin-index for the current batch.
223+
begin = 0
224+
225+
# For each data-file.
226+
for i in range(_num_files_train):
227+
# Load the images and class-numbers from the data-file.
228+
images_batch, cls_batch = _load_data(filename="data_batch_" + str(i + 1))
229+
230+
# Number of images in this batch.
231+
num_images = len(images_batch)
232+
233+
# End-index for the current batch.
234+
end = begin + num_images
235+
236+
# Store the images into the array.
237+
images[begin:end, :] = images_batch
238+
239+
# Store the class-numbers into the array.
240+
cls[begin:end] = cls_batch
241+
242+
# The begin-index for the next batch is the current end-index.
243+
begin = end
244+
245+
return images, cls, one_hot_encoded(class_numbers=cls, num_classes=num_classes)
246+
247+
248+
def load_test_data():
249+
"""
250+
Load all the test-data for the CIFAR-10 data-set.
251+
252+
Returns the images, class-numbers and one-hot encoded class-labels.
253+
"""
254+
255+
images, cls = _load_data(filename="test_batch")
256+
257+
return images, cls, one_hot_encoded(class_numbers=cls, num_classes=num_classes)
258+
259+
########################################################################
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Convolutional Layer 1
2+
filter1_size = 5
3+
number_of_filter1 = 16
4+
5+
# Convolutional Layer 2
6+
filter2_size = 5
7+
number_of_filter2 = 36
8+
9+
# Fully Connected Layer
10+
fc_size = 128
11+
12+
# No of training steps
13+
STEPS = 100
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
data_url = "https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz"
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)