This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSectionCModelFitting.py
96 lines (78 loc) · 3.57 KB
/
SectionCModelFitting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
#The code within this file has been derived from:
#https://medium.com/@sidathasiri/building-a-convolutional-neural-network-for-image-classification-with-tensorflow-f1f2f56bd83b
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import RMSprop
import keras_tuner as kt
base_path = os.getcwd()+'/Images'
X_train_path = os.path.join(base_path,'Train')
y_test_path = os.path.join(base_path,'Test')
#add image augmentation to train data to improve accuracy
X_generator = ImageDataGenerator(1.0/255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
y_generator = ImageDataGenerator(1.0/255.)
TARGET_SIZE = (80,80)
generate_X_data = X_generator.flow_from_directory(X_train_path,batch_size=20,class_mode='binary',target_size = TARGET_SIZE)
generate_y_data = y_generator.flow_from_directory(y_test_path,batch_size=20,class_mode='binary',target_size = TARGET_SIZE)
CNN_Model = keras.Sequential([
#the images have been resized to 80,80 and are RGB so need a channel of 3
layers.Input(shape=(80,80,3)),
layers.Conv2D(filters=16, kernel_size=(3,3)),
layers.MaxPooling2D(pool_size=(2,2)),
layers.Conv2D(filters=32, kernel_size=(3,3)),
layers.MaxPooling2D(pool_size=(2,2)),
layers.Conv2D(filters=64, kernel_size=(3,3)),
layers.MaxPooling2D(pool_size=(2,2)),
layers.Flatten(),
layers.Dropout(0.5),
layers.Dense(256, activation='relu'),
layers.Dense(1,activation='sigmoid')
])
#RMSprop can be used since the classification is binary
CNN_Model.compile(optimizer=RMSprop(lr=0.001),
loss='binary_crossentropy',
metrics = ['accuracy'])
print('\n')
#CNN_Model.fit(generate_X_data,
# validation_data=generate_y_data,
# steps_per_epoch=42,
# epochs=10,
# validation_steps=15,
# verbose=1)
#test_loss, test_acc = CNN_Model.evaluate(generate_y_data, verbose=2)
#print('\n Test accuracy:', test_acc)
#CNN_Model.save("CNN_Image_Classification_Model.h5")
def create_model(hyperparam):
model = keras.models.Sequential()
model.add(layers.Conv2D(
filters=hyperparam.Int('convolution_1',min_value=16, max_value=32, step=16), kernel_size=hyperparam.Choice('convolution_1', values = [3,6]),
activation='relu',input_shape=(80,80,3)
)),
model.add(layers.Conv2D(
filters=hyperparam.Int('convolution_2', min_value=32, max_value=64, step=16),kernel_size=hyperparam.Choice('convolution_2', values = [3,6]),
activation='relu'
)),
model.add(layers.Flatten()),
model.add(layers.Dense(256)),
model.add(layers.Activation("relu"))
model.compile(optimizer=keras.optimizers.Adam(hyperparam.Choice('learning_rate', values=[0.01, 0.001])),
loss='binary_crossentropy',
metrics=['accuracy'])
return model
tuner = kt.Hyperband(create_model,
objective='val_accuracy',
max_epochs=10,
factor=3)
tuner.search(generate_X_data,validation_data=generate_y_data, epochs=10)