-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackend.py
More file actions
191 lines (133 loc) · 6.27 KB
/
Copy pathBackend.py
File metadata and controls
191 lines (133 loc) · 6.27 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
import glob as gb
import pandas as pd
import seaborn as sns
import matplotlib.image as mpimg
import imagehash
from PIL import Image
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import EarlyStopping
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from keras.applications import VGG16
from keras.applications import ResNet101
from keras.applications import EfficientNetB0
from keras.models import Sequential
from keras.layers import Dense, GlobalAveragePooling2D
from keras.optimizers import Adam
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
train_path = 'C:\\Users\\raghu\\OneDrive\\Desktop\\train'
test_path = 'C:\\Users\\raghu\\OneDrive\\Desktop\\test'
from keras.preprocessing.image import ImageDataGenerator
# Data augmentation for training
train_datagen = ImageDataGenerator(
rescale=1.0/255, # Rescale pixel values to [0, 1]
rotation_range=20, # Random rotation within 20 degrees
width_shift_range=0.2, # Random horizontal shift by 20% of image width
height_shift_range=0.2, # Random vertical shift by 20% of image height
horizontal_flip=True, # Random horizontal flipping
fill_mode='nearest' # Fill mode for new pixels after shifts/rotations
)
size_images = 150
batch_size = 32
train_generator = train_datagen.flow_from_directory(
train_path, # Path to the training data
target_size=(size_images, size_images), # Resize images to this size
batch_size = batch_size, # Number of images in each batch
class_mode ='categorical' # Mode for class labels (categorical for one-hot encoding)
)
test_datagen = ImageDataGenerator(rescale=1.0/255)
test_generator = test_datagen.flow_from_directory(
test_path,
target_size=(size_images, size_images),
batch_size = batch_size,
class_mode='categorical')
def Show_Images(target_gen):
# Get a batch of images and labels
batch_images, batch_labels = next(target_gen)
# Get class labels
class_labels = list(target_gen.class_indices.keys())
# Display images with labels
plt.figure(figsize=(20, 20))
for n , i in enumerate(list(np.random.randint(0,len(batch_images),36))):
plt.subplot(6, 6, n + 1)
plt.imshow(batch_images[i])
plt.title(class_labels[np.argmax(batch_labels[i])]) # Display the class label
plt.axis('off')
plt.show()
Show_Images(train_generator)
Show_Images(test_generator)
early_stopping = EarlyStopping(
min_delta=0.001, # minimium amount of change to count as an improvement
patience=5, # how many epochs to wait before stopping
restore_best_weights=True,
)
# Configure the learning rate reduction callback
learning_rate_reduce = ReduceLROnPlateau(
monitor='val_acc', # Metric to monitor for changes (usually validation accuracy)
patience=5, # Number of epochs with no improvement after which learning rate will be reduced
verbose=1, # Verbosity mode (0: silent, 1: update messages)
factor=0.5, # Factor by which the learning rate will be reduced (e.g., 0.5 means halving)
min_lr=0.00001 # Lower bound for the learning rate (it won't go below this value)
)
from tensorflow.keras.applications import ResNet101
base_model=ResNet101(weights='imagenet', include_top=False, input_shape=(size_images, size_images, 3))
# Freeze the layers of the base model
for layer in base_model.layers:
layer.trainable = False
from tensorflow.keras import layers
model = tf.keras.models.Sequential([
base_model,
layers.Flatten(),
layers.Dense(256,activation='relu'),
layers.Dense(128,activation='relu'),
layers.Dense(64,activation='relu'),
layers.Dropout(0.5),
layers.Dense(6,activation='softmax'),
])
# Define the parameters for the exponential decay schedule
initial_learning_rate = 0.1 # Initial learning rate
decay_steps = 1000 # Number of steps before decaying the learning rate
decay_rate = 0.5 # Rate at which the learning rate decays
staircase = True # Whether to use staircase decay (integer division of step by decay_steps)
# Create the exponential decay schedule
def lr_schedule(epoch, lr):
if staircase:
return lr * decay_rate ** (epoch // decay_steps)
else:
return lr * decay_rate ** (epoch / decay_steps)
early_stopping = EarlyStopping(
min_delta=0.001, # minimium amount of change to count as an improvement
patience=5, # how many epochs to wait before stopping
restore_best_weights=True,
)
# Configure the learning rate reduction callback
learning_rate_reduce = ReduceLROnPlateau(
monitor='val_acc', # Metric to monitor for changes (usually validation accuracy)
patience=5, # Number of epochs with no improvement after which learning rate will be reduced
verbose=1, # Verbosity mode (0: silent, 1: update messages)
factor=0.5, # Factor by which the learning rate will be reduced (e.g., 0.5 means halving)
min_lr=0.00001 # Lower bound for the learning rate (it won't go below this value)
)
from keras.callbacks import LearningRateScheduler
lr_callback = LearningRateScheduler(lr_schedule)
callback=[early_stopping , learning_rate_reduce , lr_callback]
model.compile(optimizer=keras.optimizers.Adam(lr=lr_callback),
loss="categorical_crossentropy",
metrics=['accuracy']
)
from tensorflow.keras.layers import Dense
# Get the model architecture up to the last layer
model = tf.keras.Sequential(model.layers[:-1])
# Add a new output layer for 4 classes
new_output = Dense(4, activation='softmax') # Assuming 4 classes
model.add(new_output) # Add the new output layer
# Compile the model again after modifying the architecture
model.compile(optimizer='adam', loss='categorical_crossentro')