Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2022-169
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2022-169
2022-169
Commits
138459b7
Commit
138459b7
authored
Oct 10, 2022
by
Heshani H.U
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
eac82891
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
80 additions
and
0 deletions
+80
-0
train.py
train.py
+80
-0
No files found.
train.py
0 → 100644
View file @
138459b7
# Importing the Keras libraries and packages
from
keras.models
import
Sequential
from
keras.layers
import
Convolution2D
from
keras.layers
import
MaxPooling2D
from
keras.layers
import
Flatten
from
keras.layers
import
Dense
,
Dropout
import
os
os
.
environ
[
"CUDA_VISIBLE_DEVICES"
]
=
"1"
sz
=
128
# Step 1 - Building the CNN
# Initializing the CNN
classifier
=
Sequential
()
# First convolution layer and pooling
classifier
.
add
(
Convolution2D
(
32
,
(
3
,
3
),
input_shape
=
(
sz
,
sz
,
1
),
activation
=
'relu'
))
classifier
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
)))
# Second convolution layer and pooling
classifier
.
add
(
Convolution2D
(
32
,
(
3
,
3
),
activation
=
'relu'
))
# input_shape is going to be the pooled feature maps from the previous convolution layer
classifier
.
add
(
MaxPooling2D
(
pool_size
=
(
2
,
2
)))
#classifier.add(Convolution2D(32, (3, 3), activation='relu'))
# input_shape is going to be the pooled feature maps from the previous convolution layer
#classifier.add(MaxPooling2D(pool_size=(2, 2)))
# Flattening the layers
classifier
.
add
(
Flatten
())
# Adding a fully connected layer
classifier
.
add
(
Dense
(
units
=
128
,
activation
=
'relu'
))
classifier
.
add
(
Dropout
(
0.40
))
classifier
.
add
(
Dense
(
units
=
96
,
activation
=
'relu'
))
classifier
.
add
(
Dropout
(
0.40
))
classifier
.
add
(
Dense
(
units
=
64
,
activation
=
'relu'
))
classifier
.
add
(
Dense
(
units
=
27
,
activation
=
'softmax'
))
# softmax for more than 2
# Compiling the CNN
classifier
.
compile
(
optimizer
=
'adam'
,
loss
=
'categorical_crossentropy'
,
metrics
=
[
'accuracy'
])
# categorical_crossentropy for more than 2
# Step 2 - Preparing the train/test data and training the model
classifier
.
summary
()
# Code copied from - https://keras.io/preprocessing/image/
from
keras.preprocessing.image
import
ImageDataGenerator
train_datagen
=
ImageDataGenerator
(
rescale
=
1.
/
255
,
shear_range
=
0.2
,
zoom_range
=
0.2
,
horizontal_flip
=
True
)
test_datagen
=
ImageDataGenerator
(
rescale
=
1.
/
255
)
training_set
=
train_datagen
.
flow_from_directory
(
'data2/train'
,
target_size
=
(
sz
,
sz
),
batch_size
=
10
,
color_mode
=
'grayscale'
,
class_mode
=
'categorical'
)
test_set
=
test_datagen
.
flow_from_directory
(
'data2/test'
,
target_size
=
(
sz
,
sz
),
batch_size
=
10
,
color_mode
=
'grayscale'
,
class_mode
=
'categorical'
)
classifier
.
fit_generator
(
training_set
,
steps_per_epoch
=
12841
,
# No of images in training set
epochs
=
5
,
validation_data
=
test_set
,
validation_steps
=
4268
)
# No of images in test set
# Saving the model
model_json
=
classifier
.
to_json
()
with
open
(
"model-bw.json"
,
"w"
)
as
json_file
:
json_file
.
write
(
model_json
)
print
(
'Model Saved'
)
classifier
.
save_weights
(
'model-bw.h5'
)
print
(
'Weights saved'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment