Image Classification Using CNN (Convolution Neural Networks) Algorithm
Image Classification Using CNN (Convolution Neural Networks) Algorithm
NETWORKS) ALGORITHM
ABSTRACT
Deep Learning has emerged as a new area in machine learning and is applied to a number of
signal and image applications. The main purpose of the work presented in this paper, is to apply
the concept of a Deep Learning algorithm namely, Convolutional neural networks (CNN) in
image classification. The algorithm is tested on various standard datasets, like remote sensing
data of aerial images (UC Merced Land Use Dataset) and scene images from SUN database. The
performance of the algorithm is evaluated based on the quality metric known as Mean Squared
Error (MSE) and classification accuracy. The graphical representation of the experimental
results is given on the basis of MSE against the number of training epochs. The experimental
result analysis based on the quality metrics and the graphical representation proves that the
algorithm (CNN) gives fairly good classification accuracy for all the tested datasets.
IMAGE CLASSIFICATION USING CNN (CONVOLUTION
NEURAL NETWORKS) ALGORITHM
INTRODUCTION
Lillsand and Kiefer defined image processing as involving manipulation of digital images
with the use of computer. It is a broad subject and involves processes that are mathematically
complex. Image processing involves some basic operations namely image
restoration/rectification, image enhancement, image classification, images fusion etc. Image
classification forms an important part of image processing. The objective of image
classification is the automatic allocation of image to thematic classes. Two types of
classification are supervised classification and unsupervised classification.
The process of image classification involves two steps, training of the system followed by
testing. The training process means, to take the characteristic properties of the images (form a
class) and form a unique description for a particular class. The process is done for all classes
depending on the type of classification problem; binary classification or multi-class
classification. The testing step means to categorize the test images under various classes for
which system was trained. This assigning of class is done based on the partitioning between
classes based on the training features.
Since 2006, deep structured learning, or more commonly called deep learning or hierarchical
learning, has emerged as a new area of machine learning research. Several definitions are
available for Deep Learning; coating one of the many definitions from Deep Learning is defined
as: A class of machine learning techniques that exploit many layers of non- linear information
processing for supervised or unsupervised feature extraction and transformation and for pattern
analysis and classification.
This work aims at the application of Convolutional Neural Network or CNN for image
classification. The image data used for testing the algorithm includes remote sensing data of
aerial images and scene data from SUN database.
SYSTEM ANALYSIS
EXISTING SYSTEM
PROPOSED SYSTEM
We propose a strategy for consolidating highlights from different layers in given CNN
models. In addition, effectively learned CNN models with preparing pictures are reused
to separate highlights from numerous layers. The proposed combination strategy is
assessed by picture classification benchmark informational indexes, CIFAR-10, NORB,
and SVHN. In all cases, we show that the proposed strategy improves the detailed
exhibitions of the current models by 0.38%, 3.22% and 0.13%, separately.
SYSTEM REQUIREMENTS
HARDWARE REQUIREMENTS
For developing the application the following are the Hardware Requirements:
FUNCTIONAL REQUIREMENTS
SOFTWARE REQUIREMENTS
For developing the application the following are the Software Requirements:
1. Python
2. Django
1. Windows 7
2. Windows XP
3. Windows 8
1. Python
MODULES
1. Model construction
2. model training
3. model testing
4. model evaluation
Model construction depends on machine learning algorithms. In this projects case, it was neural
networks.
Before model training it is important to scale data for their further use.
After model construction it is time for model training. In this phase, the model is trained using
training data and expected output for this data.
Progress is visible on the console when the script runs. At the end it will report the final accuracy
of the model.
Once the model has been trained it is possible to carry out model testing. During this phase a
second set of data is loaded. This data set has never been seen by the model and therefore it’s true
accuracy will be verified.
After the model training is complete, and it is understood that the model shows the right result, it
can be saved by: model.save (“name_of_file.h5”).
Finally, the saved model can be used in the real world. The name of this phase is model
evaluation. This means that the model can be used to evaluate new data.
SYSTEM DESIGN
INTRODUCTION
The design phase of software development deals with transforming the customer
requirements into a form implementable using a programming language. The software design
process can be divided into the following three levels of phases of design:
System Design
Architectural Design
Detailed Design
UML DIAGRAMS
This chapter provides the design phase of the Application. To design the project, we use the
UML diagrams. The Unified Modelling Language (UML) is a general- purpose, developmental,
modelling language in the field of software engineering that is intended to provide a standard
way to visualize the design of a system.
generateCNNTrain&TestModel
uploadTestImage
User
classifyPictureInImage
exit
The use case diagram is used to represent all the functional use cases that are involved in the
project.
CLASS DIAGRAM
User
images Application
images
generateCNNTrain&TestModel()
uploadTestImage()
operationPerformed()
classifyPictureInImage()
exit()
The above mentioned class diagram represents the Chatbot system workflow model.
SEQUENCE DIAGRAM
User Application
generateCNNTrain&TestModel
uploadTestImage
classifyPictureInImages
exit
The above diagram represents the sequence of flow of actions in the system.
COLLABORATION DIAGRAM:-
1: generateCNNTrain&TestModel
2: uploadTestImage
3: classifyPictureInImages
4: exit
User Applicati
on
INPUT DESIGN
The input design is the link between the information system and the user. It
comprises the developing specification and procedures for data preparation and those steps are
necessary to put transaction data in to a usable form for processing can be achieved by
inspecting the computer to read data from a written or printed document or it can occur by
having people keying the data directly into the system. The design of input focuses on
controlling the amount of input required, controlling the errors, avoiding delay, avoiding extra
steps and keeping the process simple. The input is designed in such a way so that it provides
security and ease of use with retaining the privacy. Input Design considered the following
things:
What data should be given as input?
How the data should be arranged or coded?
The dialog to guide the operating personnel in providing input.
Methods for preparing input validations and steps to follow when error occur.
OBJECTIVES
1
OUTPUT DESIGN
A quality output is one, which meets the requirements of the end user and presents
the information clearly. In any system results of processing are communicated to the users and
to other system through outputs. In output design it is determined how the information is to be
displaced for immediate need and also the hard copy output. It is the most important and direct
source information to the user. Efficient and intelligent output design improves the system’s
relationship to help user decision-making.
1. Designing computer output should proceed in an organized, well thought out
manner; the right output must be developed while ensuring that each output element is designed
so that people will find the system can use easily and effectively. When analysis design
computer output, they should Identify the specific output that is needed to meet the
requirements.
2. Select methods for presenting information.
3. Create document, report, or other formats that contain information produced by
the system.
The output form of an information system should accomplish one or more of the
following objectives.
Convey information about past activities, current status or projections of the Future.
Signal important events, opportunities, problems, or warnings.
Trigger an action.
Confirm an action.
1
LITERATURE SURVEY
LITERATURE REVIEW
Remote Sensing and Image Interpretation, 7th Edition is designed to be primarily used in two
ways: as a textbook in the introductory courses in remote sensing and image interpretation, and
as a reference for the burgeoning number of practitioners who use geospatial information and
analysis in their work. Because of the wide range of academic and professional settings in which
this book might be used, we have made the discussion “discipline neutral.” In short, anyone
involved in geospatial data acquisition and analysis should find this book to be a valuable text
and reference.
This monograph provides an over view of general deep learning methodology and its
applications to a variety of signal and information processing tasks.
The application areas are chosen with the following three criteria in mind:
1
TITLE: A Logical Calculus of Ideas Immanent in Nervous Activity.
Because of the “all-or-none” character of nervous activity, neural events and the relations among
them can be treated by means of propositional logic. It is found that the behavior of every net
can be described in these terms, with the addition of more complicated logical means for nets
containing circles; and that for any logical expression satisfying certain conditions, one can find
a net behaving in the fashion it describes. It is shown that many particular choices among
possible neurophysiological assumptions are equivalent, in the sense that for every net behaving
under one assumption, there exists another net which behaves under the other and gives the
same results, although perhaps not in the same time. Various applications of the calculus are
discussed.
A Convolutional neural network (CNN) is a neural network that has one or more convolutional
layers and is used mainly for image processing, classification, segmentation and also for other
auto correlated data. A convolution is essentially sliding a filter over the input. One helpful way
to think about convolutions is this quote from Dr Prasad Samarakoon: “A convolution can be
thought as “looking at a function’s surroundings to make better/accurate predictions of its
outcome.”Rather than looking at an entire image at once to find certain features it can be more
effective to look at smaller portions of the image.
The striate cortex was studied in lightly anaesthetized macaque and spider monkeys by
recording extra cellularly from single units and stimulating the retinas with spots or patterns of
light. Most cells can be categorized as simple, complex, or hypercomplex, with response
properties very similar to those previously described in the cat. On the average, however,
receptive fields are smaller, and there is a greater sensitivity to changes in stimulus orientation.
1
A small proportion of the cells are colour coded.2. Evidence is presented for at least two
independent systems of columns extending vertically from surface to white matter. Columns of
the first type contain cells with common receptive-field orientations. They are similar to the
orientation columns described in the cat, but are probably smaller in cross-sectional area. In the
second system cells are aggregated into columns according to eye preference. The ocular
dominance columns are larger than the orientation columns, and the two sets of boundaries seem
to be independent.3. There is a tendency for cells to be grouped according to symmetry of
responses to movement; in some regions the cells respond equally well to the two opposite
directions of movement of a line, but other regions contain a mixture of cells favouring one
direction and cells favouring the other.4. A horizontal organization corresponding to the cortical
layering can also be discerned. The upper layers (II and the upper two-thirds of III) contain
complex and hypercomplex cells, but simple cells are virtually absent. The cells are mostly
binocularly driven. Simple cells are found deep in layer III, and in IV A and IV B. In layer IV B
they form a large proportion of the population, whereas complex cells are rare. In layers IV A
and IV B one finds units lacking orientation specificity; it is not clear whether these are cell
bodies or axons of geniculate cells. In layer IV most cells are driven by one eye only; this layer
consists of a mosaic with cells of some regions responding to one eye only, those of other
regions responding to the other eye. Layers V and VI contain mostly complex and
hypercomplex cells, binocularly driven.5. The cortex is seen as a system organized vertically
and horizontally in entirely different ways. In the vertical system (in which cells lying along a
vertical line in the cortex have common features) stimulus dimensions such as retinal position,
line orientation, ocular dominance, and perhaps directionality of movement, are mapped in sets
of superimposed but independent mosaics. The horizontal system segregates cells in layers by
hierarchical orders, the lowest orders (simple cells monocularly driven) located in and near layer
IV, the higher orders in the upper and lower layers.
1
SOFTWARE ENVIRONMENT
PYTHON
Python is a general-purpose interpreted, interactive, object-oriented, and high-level
programming language. An interpreted language, Python has a design philosophy that
emphasizes code readability (notably using whitespace indentation to delimit code blocks rather
than curly brackets or keywords), and a syntax that allows programmers to express concepts in
fewer lines of code than might be used in languages such as C++or Java. It provides constructs
that enable clear programming on both small and large scales. Python interpreters are available
for many operating systems. CPython, the reference implementation of Python, is open
source software and has a community-based development model, as do nearly all of its variant
implementations. CPython is managed by the non-profit Python Software Foundation. Python
features a dynamic type system and automatic memory management. It supports
multiple programming paradigms, including object-
oriented, imperative, functional and procedural, and has a large and comprehensive standard
library.
Interactive Mode Programming
Invoking the interpreter without passing a script file as a parameter brings up the following
prompt −
$ python
Python 2.4.3 (#1, Nov 11 2010, 13:34:43)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Type the following text at the Python prompt and press the Enter −
>>> print "Hello, Python!"
If you are running new version of Python, then you would need to use print statement with
parenthesis as in print ("Hello, Python!");. However in Python version 2.4.3, this produces the
following result −
Hello, Python!
Script Mode Programming
Invoking the interpreter with a script parameter begins execution of the script and continues
until the script is finished. When the script is finished, the interpreter is no longer active.
1
Let us write a simple Python program in a script. Python files have extension .py. Type the
following source code in a test.py file −
Live Demo
print "Hello, Python!"
We assume that you have Python interpreter set in PATH variable. Now, try to run this program
as follows −
$ python test.py
This produces the following result −
Hello, Python!
Let us try another way to execute a Python script. Here is the modified test.py file −
Live Demo
#!/usr/bin/python
print "Hello, Python!"
We assume that you have Python interpreter available in /usr/bin directory. Now, try to run this
program as follows −
$ chmod +x test.py # This is to make file executable
$./test.py
This produces the following result −
Hello, Python!
Python Identifiers
A Python identifier is a name used to identify a variable, function, class, module or other object.
An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more
letters, underscores and digits (0 to 9).
Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a
case sensitive programming language. Thus, Manpower and manpower are two different
identifiers in Python.
Here are naming conventions for Python identifiers −
Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
Starting an identifier with a single leading underscore indicates that the identifier is private.
Starting an identifier with two leading underscores indicates a strongly private identifier.
If the identifier also ends with two trailing underscores, the identifier is a language-defined
special name.
1
Reserved Words
The following list shows the Python keywords. These are reserved words and you cannot use
them as constant or variable or any other identifier names. All the Python keywords contain
lowercase letters only.
and exec not
assert finally or
break for pass
class from print
continue global raise
def if return
del import try
elif in while
else is with
except lambdayield
Lines and Indentation
Python provides no braces to indicate blocks of code for class and function definitions or flow
control. Blocks of code are denoted by line indentation, which is rigidly enforced.
The number of spaces in the indentation is variable, but all statements within the block must be
indented the same amount. For example −
if True:
print "True"
else:
print "False"
However, the following block generates an error −
if True:
print "Answer"
print "True"
else:
print "Answer"
print "False"
Thus, in Python all the continuous lines indented with same number of spaces would form a
block. The following example has various statement blocks −
Note − Do not try to understand the logic at this point of time. Just make sure you understood
1
various blocks even if they are without braces.
#!/usr/bin/python
import sys
try:
# open file stream
file = open(file_name, "w")
except IOError:
print "There was an error writing to", file_name
sys.exit()
print "Enter '", file_finish,
print "' When finished"
while file_text != file_finish:
file_text = raw_input("Enter text: ")
if file_text == file_finish:
# close the file
file.close
break
file.write(file_text)
file.write("\n")
file.close()
file_name = raw_input("Enter filename: ")
if len(file_name) == 0:
print "Next time please enter something"
sys.exit()
try:
file = open(file_name, "r")
except IOError:
print "There was an error reading file"
sys.exit()
file_text = file.read()
file.close()
print file_text
1
Multi-Line Statements
Statements in Python typically end with a new line. Python does, however, allow the use of the
line continuation character (\) to denote that the line should continue. For example −
total = item_one + \
item_two + \
item_three
Statements contained within the [], {}, or () brackets do not need to use the line continuation
character. For example −
days = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday']
Quotation in Python
Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long
as the same type of quote starts and ends the string.
The triple quotes are used to span the string across multiple lines. For example, all the following
are legal −
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""
Comments in Python
A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and
up to the end of the physical line are part of the comment and the Python interpreter ignores
them.
Live Demo
#!/usr/bin/python
# First comment
print "Hello, Python!" # second comment
This produces the following result −
Hello, Python!
You can type a comment on the same line after a statement or expression −
name = "Madisetti" # This is again comment
You can comment multiple lines as follows −
# This is a comment.
1
# This is a comment, too.
# This is a comment, too.
# I said that already.
Following triple-quoted string is also ignored by Python interpreter and can be used as a
multiline comments:
'''
This is a multiline
comment.
'''
Using Blank Lines
A line containing only whitespace, possibly with a comment, is known as a blank line and
Python totally ignores it.
In an interactive interpreter session, you must enter an empty physical line to terminate a
multiline statement.
Waiting for the User
The following line of the program displays the prompt, the statement saying “Press the enter key
to exit”, and waits for the user to take action −
#!/usr/bin/python
raw_input("\n\nPress the enter key to exit.")
Here, "\n\n" is used to create two new lines before displaying the actual line. Once the user
presses the key, the program ends. This is a nice trick to keep a console window open until the
user is done with an application.
Multiple Statements on a Single Line
The semicolon ( ; ) allows multiple statements on the single line given that neither statement
starts a new code block. Here is a sample snip using the semicolon.
import sys; x = 'foo'; sys.stdout.write(x + '\n')
Multiple Statement Groups as Suites
A group of individual statements, which make a single code block are called suites in Python.
Compound or complex statements, such as if, while, def, and class require a header line and a
suite.
Header lines begin the statement (with the keyword) and terminate with a colon ( : ) and are
followed by one or more lines which make up the suite. For example −
if expression :
2
suite
elif expression :
suite
else :
suite
Command Line Arguments
Many programs can be run to provide you with some basic information about how they should
be run. Python enables you to do this with -h −
$ python -h
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-c cmd : program passed in as string (terminates option list)
-d : debug output from parser (also PYTHONDEBUG=x)
-E : ignore environment variables (such as PYTHONPATH)
-h : print this help message and exit
You can also program your script in such a way that it should accept various options. Command
Line Arguments is an advanced topic and should be studied a bit later once you have gone
through rest of the Python concepts.
Python Lists
The list is a most versatile datatype available in Python which can be written as a list of comma-
separated values (items) between square brackets. Important thing about a list is that items in a
list need not be of the same type.
Creating a list is as simple as putting different comma-separated values between square brackets.
For example −
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5];
list3 = ["a", "b", "c", "d"]
Similar to string indices, list indices start at 0, and lists can be sliced, concatenated and so on.
A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The
differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use
parentheses, whereas lists use square brackets.
Creating a tuple is as simple as putting different comma-separated values. Optionally you can
put these comma-separated values between parentheses also. For example −
2
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5);
tup3 = "a", "b", "c", "d";
The empty tuple is written as two parentheses containing nothing −
tup1 = ();
To write a tuple containing a single value you have to include a comma, even though there is
only one value −
tup1 = (50,);
Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on.
Accessing Values in Tuples
To access values in tuple, use the square brackets for slicing along with the index or indices to
obtain value available at that index. For example −
Live Demo
#!/usr/bin/python
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5, 6, 7 );
print "tup1[0]: ", tup1[0];
print "tup2[1:5]: ", tup2[1:5];
When the above code is executed, it produces the following result −
tup1[0]: physics
tup2[1:5]: [2, 3, 4, 5]
Updating Tuples
Accessing Values in Dictionary
To access dictionary elements, you can use the familiar square brackets along with the key to
obtain its value. Following is a simple example −
Live Demo
#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print "dict['Name']: ", dict['Name']
print "dict['Age']: ", dict['Age']
When the above code is executed, it produces the following result −
dict['Name']: Zara
dict['Age']: 7
2
If we attempt to access a data item with a key, which is not part of the dictionary, we get an
error as follows −
Live Demo
#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print "dict['Alice']: ", dict['Alice']
When the above code is executed, it produces the following result −
dict['Alice']:
Traceback (most recent call last):
File "test.py", line 4, in <module>
print "dict['Alice']: ", dict['Alice'];
KeyError: 'Alice'
Updating Dictionary
You can update a dictionary by adding a new entry or a key-value pair, modifying an existing
entry, or deleting an existing entry as shown below in the simple example −
Live Demo
#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8; # update existing entry
dict['School'] = "DPS School"; # Add new entry
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']
When the above code is executed, it produces the following result −
dict['Age']: 8
dict['School']: DPS School
Delete Dictionary Elements
You can either remove individual dictionary elements or clear the entire contents of a dictionary.
You can also delete entire dictionary in a single operation.
To explicitly remove an entire dictionary, just use the del statement. Following is a simple
example −
Live Demo
#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
2
del dict['Name']; # remove entry with key 'Name'
dict.clear(); # remove all entries in dict
del dict ; # delete entire dictionary
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']
This produces the following result. Note that an exception is raised because after del dict
dictionary does not exist any more −
dict['Age']:
Traceback (most recent call last):
File "test.py", line 8, in <module>
print "dict['Age']: ", dict['Age'];
TypeError: 'type' object is unsubscriptable
Note – del () method is discussed in subsequent section.
Properties of Dictionary Keys
Dictionary values have no restrictions. They can be any arbitrary Python object, either standard
objects or user-defined objects. However, same is not true for the keys.
There are two important points to remember about dictionary keys −
(a) More than one entry per key not allowed. Which means no duplicate key is allowed. When
duplicate keys encountered during assignment, the last assignment wins. For example −
Live Demo
#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'}
print "dict['Name']: ", dict['Name']
When the above code is executed, it produces the following result
dict['Name']: Manni
(b) Keys must be immutable. Which means you can use strings, numbers or tuples as dictionary
keys but something like ['key'] is not allowed. Following is a simple example −
Live Demo
#!/usr/bin/python
dict = {['Name']: 'Zara', 'Age': 7}
print "dict['Name']: ", dict['Name']
When the above code is executed, it produces the following result −
Traceback (most recent call last):
2
File "test.py", line 3, in <module>
dict = {['Name']: 'Zara', 'Age': 7};
TypeError: unhashable type: 'list'
Tuples are immutable which means you cannot update or change the values of tuple elements.
You are able to take portions of existing tuples to create new tuples as the following example
demonstrates
Live Demo
#!/usr/bin/python
tup1 = (12, 34.56);
tup2 = ('abc', 'xyz');
# Following action is not valid for tuples
# tup1[0] = 100;
# So let's create a new tuple as follows
tup3 = tup1 + tup2;
print tup3;
When the above code is executed, it produces the following result −
(12, 34.56, 'abc', 'xyz')
Delete Tuple Elements
Removing individual tuple elements is not possible. There is, of course, nothing wrong with
putting together another tuple with the undesired elements discarded.
To explicitly remove an entire tuple, just use the del statement. For example −
Live Demo
#!/usr/bin/python
tup = ('physics', 'chemistry', 1997, 2000);
print tup;
del tup;
print "After deleting tup : ";
print tup;
This produces the following result. Note an exception raised, this is because after del tup tuple
does not exist any more −
('physics', 'chemistry', 1997, 2000)
After deleting tup :
Traceback (most recent call last):
2
File "test.py", line 9, in <module>
print tup;
NameError: name 'tup' is not defined
DJANGO
Django is a high-level Python Web framework that encourages rapid
development and clean, pragmatic design. Built by experienced developers, it
takes care of much of the hassle of Web development, so you can focus on
writing your app without needing to reinvent the wheel. It’s free and open
source.
Django's primary goal is to ease the creation of complex, database-driven websites. Django
emphasizes reusabilityand "pluggability" of components, rapid development, and the principle
of don't repeat yourself. Python is used throughout, even for settings files and data models.
Django also provides an optional administrative create, read, update and delete interface that is
generated dynamically through introspection and configured via admin models
2
Create a Project
Whether you are on Windows or Linux, just get a terminal or a cmd prompt and navigate to the
place you want your project to be created, then use this code −
$ django-admin startproject myproject
This will create a "myproject" folder with the following structure −
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
wsgi.py
2
September 03, 2015 - 11:41:50
Django version 1.6.11, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
A project is a sum of many applications. Every application has an objective and can be reused
into another project, like the contact form on a website can be an application, and can be reused
for others. See it as a module of your project.
Create an Application
We assume you are in your project folder. In our main “myproject” folder, the same folder then
manage.py −
$ python manage.py startapp myapp
You just created myapp application and like project, Django create a “myapp” folder with the
application structure −
myapp/
__init__.py
admin.py
models.py
tests.py
views.py
__init__.py − Just to make sure python handles this folder as a package.
admin.py − This file helps you make the app modifiable in the admin interface.
models.py − This is where all the application models are stored.
tests.py − This is where your unit tests are.
views.py − This is where your application views are.
Get the Project to Know About Your Application
At this stage we have our "myapp" application, now we need to register it with our Django
project "myproject". To do so, update INSTALLED_APPS tuple in the settings.py file of your
project (add your app name) −
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
2
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
)
Creating forms in Django, is really similar to creating a model. Here again, we just need to
inherit from Django class and the class attributes will be the form fields. Let's add a forms.py
file in myapp folder to contain our app forms. We will create a login form.
myapp/forms.py
#-*- coding: utf-8 -*-
from django import forms
class LoginForm(forms.Form):
user = forms.CharField(max_length = 100)
password = forms.CharField(widget = forms.PasswordInput())
As seen above, the field type can take "widget" argument for html rendering; in our case, we
want the password to be hidden, not displayed. Many others widget are present in Django:
DateInput for dates, CheckboxInput for checkboxes, etc.
Using Form in a View
There are two kinds of HTTP requests, GET and POST. In Django, the request object passed as
parameter to your view has an attribute called "method" where the type of the request is set, and
all data passed via POST can be accessed via the request.POST dictionary.
Let's create a login view in our myapp/views.py −
#-*- coding: utf-8 -*-
from myapp.forms import LoginForm
def login(request):
username = "not logged in"
if request.method == "POST":
#Get the posted form
MyLoginForm = LoginForm(request.POST)
if MyLoginForm.is_valid():
username = MyLoginForm.cleaned_data['username']
else:
MyLoginForm = Loginform()
return render(request, 'loggedin.html', {"username" : username})
3
The view will display the result of the login form posted through the loggedin.html. To test it,
we will first need the login form template. Let's call it login.html.
<html>
<body>
<form name = "form" action = "{% url "myapp.views.login" %}"
method = "POST" >{% csrf_token %}
<div style = "max-width:470px;">
<center>
<input type = "text" style = "margin-left:20%;"
placeholder = "Identifiant" name = "username" />
</center>
</div>
<br>
<div style = "max-width:470px;">
<center>
<input type = "password" style = "margin-left:20%;"
placeholder = "password" name = "password" />
</center>
</div>
<br>
<div style = "max-width:470px;">
<center>
<button style = "border:0px; background-color:#4285F4; margin-top:8%;
height:35px; width:80%;margin-left:19%;" type = "submit"
value = "Login" >
<strong>Login</strong>
</button>
</center>
</div>
</form>
</body>
</html>
3
The template will display a login form and post the result to our login view above. You have
probably noticed the tag in the template, which is just to prevent Cross-site Request Forgery
(CSRF) attack on your site.
{% csrf_token %}
Once we have the login template, we need the loggedin.html template that will be rendered after
form treatment.
<html>
<body>
You are : <strong>{{username}}</strong>
</body>
</html>
Now, we just need our pair of URLs to get started: myapp/urls.py
from django.conf.urls import patterns, url
from django.views.generic import TemplateView
urlpatterns = patterns('myapp.views',
url(r'^connection/',TemplateView.as_view(template_name = 'login.html')),
url(r'^login/', 'login', name = 'login'))
When accessing "/myapp/connection", we will get the following login.html template rendered −
Setting Up Sessions
In Django, enabling session is done in your project settings.py, by adding some lines to the
MIDDLEWARE_CLASSES and the INSTALLED_APPS options. This should be done while
creating the project, but it's always good to know, so MIDDLEWARE_CLASSES should have −
'django.contrib.sessions.middleware.SessionMiddleware'
And INSTALLED_APPS should have −
'django.contrib.sessions'
By default, Django saves session information in database (django_session table or collection),
but you can configure the engine to store information using other ways like: in file or in cache.
When session is enabled, every request (first argument of any view in Django) has a session
(dict) attribute.
Let's create a simple sample to see how to create and save sessions. We have built a simple login
system before (see Django form processing chapter and Django Cookies Handling chapter). Let
us save the username in a cookie so, if not signed out, when accessing our login page you won’t
see the login form. Basically, let's make our login system we used in Django Cookies handling
3
more secure, by saving cookies server side.
For this, first lets change our login view to save our username cookie server side −
def login(request):
username = 'not logged in'
if request.method == 'POST':
MyLoginForm = LoginForm(request.POST)
if MyLoginForm.is_valid():
username = MyLoginForm.cleaned_data['username']
request.session['username'] = username
else:
MyLoginForm = LoginForm()
return render(request, 'loggedin.html', {"username" : username}
Then let us create formView view for the login form, where we won’t display the form if cookie
is set −
def formView(request):
if request.session.has_key('username'):
username = request.session['username']
return render(request, 'loggedin.html', {"username" : username})
else:
return render(request, 'login.html', {})
Now let us change the url.py file to change the url so it pairs with our new view −
from django.conf.urls import patterns, url
from django.views.generic import TemplateView
urlpatterns = patterns('myapp.views',
url(r'^connection/','formView', name = 'loginform'),
url(r'^login/', 'login', name = 'login'))
When accessing /myapp/connection, you will get to see the following page
SYSTEM STUDY
FEASIBILITY STUDY
3
The feasibility of the project is analyzed in this phase and business proposal is put forth with a very general
plan for the project and some cost estimates. During system analysis the feasibility study of the
proposed system is to be carried out. This is to ensure that the proposed system is not a burden
to the company. For feasibility analysis, some understanding of the major requirements for the
system is essential.
Economical Feasibility
Technical Feasibility
Social Feasibility
ECONOMICAL FEASIBILITY
This study is carried out to check the economic impact that the system will have on the
organization. The amount of fund that the company can pour into the research and development
of the system is limited. The expenditures must be justified. Thus the developed system as well
within the budget and this was achieved because most of the technologies used are freely
available. Only the customized products had to be purchased.
TECHNICAL FEASIBILITY
This study is carried out to check the technical feasibility, that is,
the technical requirements of the system. Any system developed must not have
a high demand on the available technical resources. This will lead to high
demands on the available technical resources. This will lead to high demands
being placed on the client. The developed system must have a modest
requirement, as only minimal or null changes are required for implementing this
system.
SOCIAL FEASIBILITY
The aspect of study is to check the level of acceptance of the system by the user. This
includes the process of training the user to use the system efficiently. The user must not feel
3
threatened by the system, instead must accept it as a necessity. The level of acceptance by the
users solely depends on the methods that are employed to educate the user about the system and
to make him familiar with it. His level of confidence must be raised so that he is also able to
make some constructive criticism, which is welcomed, as he is the final user of the system.
SYSTEM TESTING
NTRODUCTION
The purpose of testing is to discover errors. Testing is the process of trying to discover every
conceivable fault or weakness in a work product. It provides a way to check the functionality of
components, sub assemblies, assemblies and/or a finished product It is the process of exercising
software with the intent of ensuring that the Software system meets its requirements and user
expectations and does not fail in an unacceptable manner. There are various types of test. Each
test type addresses a specific testing requirement.
TYPES OF TESTING
Unit testing
Unit testing involves the design of test cases that validate that the internal
program logic is functioning properly, and that program inputs produce valid outputs. All
decision branches and internal code flow should be validated. It is the testing of individual
software units of the application .it is done after the completion of an individual unit before
3
integration. This is a structural testing, that relies on knowledge of its construction and is
invasive. Unit tests perform basic tests at component level and test a specific business process,
application, and/or system configuration. Unit tests ensure that each unique path of a business
process performs accurately to the documented specifications and contains clearly defined
inputs and expected results.
ntegration testing
unctional test
System testing ensures that the entire integrated software system meets
requirements. It tests a configuration to ensure known and predictable results. An example of
system testing is the configuration oriented system integration test. System testing is based on
3
process descriptions and flows, emphasizing pre-driven process links and integration points.
White Box Testing
White Box Testing is a testing in which in which the software tester has
knowledge of the inner workings, structure and language of the software, or at least its purpose.
It is purpose. It is used to test areas that cannot be reached from a black box level.
Black Box Testing
Black Box Testing is testing the software without any knowledge of the inner
workings, structure or language of the module being tested. Black box tests, as most other kinds
of tests, must be written from a definitive source document, such as specification or
requirements document, such as specification or requirements document. It is a testing in which
the software under test is treated, as a black box .you cannot “see” into it. The test provides
inputs and responds to outputs without considering how the software works.
Unit Testing
Unit testing is usually conducted as part of a combined code and unit test phase
of the software lifecycle, although it is not uncommon for coding and unit testing to be
conducted as two distinct phases.
Testing Approaches
Field testing will be performed manually and functional tests will be written in detail.
Test objectives
Features to be tested
Testing Levels
ntegration Testing
3
Software integration testing is the incremental integration testing of two or more
integrated software components on a single platform to produce failures caused by interface
defects.
The task of the integration test is to check that components or software applications, e.g.
components in a software system or – one step up – software applications at the company level –
interact without error.
Test Results: All the test cases mentioned above passed successfully. No defects encountered.
Acceptance Testing
User Acceptance Testing is a critical phase of any project and requires significant participation
by the end user. It also ensures that the system meets the functional requirements.
Test Results: All the test cases mentioned above passed successfully. No defects encountered.
SAMPLE CODE
main = tkinter.Tk()
main.title("Image Classification Using CNN") #designing main screen
main.geometry("600x500")
3
global filename
global loaded_model
def generateModel():
global loaded_model
if os.path.exists('model.json'):
with open('model.json', "r") as json_file:
loaded_model_json = json_file.read()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("model_weights.h5")
loaded_model._make_predict_function()
print(loaded_model.summary)
messagebox.showinfo("Model Generated", "CNN Model Generated on Train & Test Data.
See black console for details")
else:
classifier = Sequential()
classifier.add(Convolution2D(32, 3, 3, input_shape = (48, 48, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Convolution2D(32, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Flatten())
classifier.add(Dense(output_dim = 128, activation = 'relu'))
classifier.add(Dense(output_dim = 5, activation = 'softmax'))
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
train_datagen = ImageDataGenerator()
test_datagen = ImageDataGenerator()
training_set = train_datagen.flow_from_directory('data/train',
target_size = (48, 48),
batch_size = 32,
class_mode = 'categorical',
shuffle=True)
test_set = test_datagen.flow_from_directory('data/validation',
target_size = (48, 48),
batch_size = 32,
class_mode = 'categorical',
shuffle=False)
classifier.fit_generator(training_set,
samples_per_epoch = 8000,
nb_epoch = 1,
validation_data = test_set,
nb_val_samples = 2000)
classifier.save_weights('model_weights.h5')
model_json = classifier.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
print(training_set.class_indices)
print(classifier.summary)
messagebox.showinfo("Model Generated", "CNN Model Generated on Train & Test Data.
3
See black console for details")
def classify():
imagetest = image.load_img(filename, target_size = (48,48))
imagetest = image.img_to_array(imagetest)
imagetest = np.expand_dims(imagetest, axis = 0)
preds = loaded_model.predict(imagetest)
print(str(preds)+" "+str(np.argmax(preds)))
predict = np.argmax(preds)
msg = ""
if predict == 0:
msg = "Image Contains Car"
if predict == 1:
msg = "Image Contains Cat"
if predict == 2:
msg = "Image Contains Dog"
if predict == 3:
msg = "Image Contains Person"
if predict == 4:
msg = "Image Contains Plane"
imagedisplay = cv2.imread(filename)
orig = imagedisplay.copy()
output = imutils.resize(orig, width=400)
cv2.putText(output, msg, (10, 25), cv2.FONT_HERSHEY_SIMPLEX,0.7, (0, 255, 0), 2)
cv2.imshow("Predicted Image Result", output)
cv2.waitKey(0)
def exit():
global main
main.destroy()
main.config(bg='light coral')
main.mainloop()
4
SCREENSHOTS
In above screen click on ‘Generate CNN Train & Test Model’ to generate CNN model with all
images given inside data folder. In this application I am building CNN model with CAR, CAT,
DOG, PLANE and PERSON images. This application can identify or predict images up to 90%
and this CNN can work up to 100% also but we need to train it with all possible images and high
processing CPU. So in this normal CPU my generated model can identity above images with
90% accuracy. Now click on ‘Upload Test Image’ button to upload image
4
In above screen I am uploading image called 8.jpg which contains dog picture.
After uploading image click on ‘Classify Picture In Image’ button to predict photo in uploaded
image
4
In uploaded image in above screen we can see application classify image as Dog. Similarly u
can test with other images
CONCLUSION
In this work, I figured out what is deep learning. I assembled and trained the CNN model to
classify photographs of cars and elephants. I have tested that this model works really well with a
small number of photos. I measured how the accuracy depends on the number of epochs in order
to detect potential overfitting problem. I determined that 10 epochs are enough for a successful
training of the model.
My next step would be to try this model on more data sets and try to apply it to practical tasks. I
would also like to experiment with the neural network design in order to see how a higher
efficiency can be achieved in various problems.
4
REFERENCES
[1] Lillesand, T.M. and Kiefer, R.W. and Chipman, J.W., in “Remote Sensing and Image
Interpretation” 5th ed. Wiley, 2004
[2] Li Deng and Dong Yu “Deep Learning: methods and applications” by Microsoft research
[Online] available at: http://research.microsoft.com/pubs/209355/NOW-Book-RevisedFeb2014-
online.pdf
[3] McCulloch, Warren; Walter Pitts, "A Logical Calculus of Ideas Immanent in Nervous
Activity”, Bulletin of Mathematical Biophysics 5 (4): 115–133(1943)
[5] Hubel, D. and Wiesel, T. (1968). Receptive fields and functional architecture of monkey
striate cortex. Journal of Physiology (London), 195, 215–243C. J. Kaufman, Rocky Mountain
Research Laboratories, Boulder, Colo., personal communication, 1992. (Personal
communication)
[6] Yann LeCun, Leon Bottou, Yodhua Bengio and Patrick Haffner, “Gradient-Based Learning
Applied to Document Recognition”, Proc. Of IEEE, November 1998.
[9] Adelson, Edward H., Charles H. Anderson, James R. Bergen, Peter J. Burt, and Joan M.
Ogden. "Pyramid methods in image processing." RCA engineer 29, no. 6 (1984):33-41.