-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVGG16_Functionalities.py
More file actions
80 lines (68 loc) · 2.58 KB
/
VGG16_Functionalities.py
File metadata and controls
80 lines (68 loc) · 2.58 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
import os
import numpy as np
import tensorflow as tf
from tensorflow.keras.applications import VGG16
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Flatten, Dropout, GlobalAveragePooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
# ===================== CONFIGURATION =====================
IMAGE_SIZE = (224, 224) # Standard for VGG16
BATCH_SIZE = 32
EPOCHS = 20
NUM_CLASSES = 4 # E.g., Carcinoma, Squamous Cell Carcinoma, Adenocarcinoma, Normal
LEARNING_RATE = 1e-4
TRAIN_PATH = 'dataset/train'
VAL_PATH = 'dataset/val'
MODEL_SAVE_PATH = 'vgg16_liver_model.h5'
# ===================== DATA AUGMENTATION =====================
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=15,
shear_range=0.1,
zoom_range=0.1,
horizontal_flip=True,
width_shift_range=0.1,
height_shift_range=0.1
)
val_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
TRAIN_PATH,
target_size=IMAGE_SIZE,
batch_size=BATCH_SIZE,
class_mode='categorical'
)
val_generator = val_datagen.flow_from_directory(
VAL_PATH,
target_size=IMAGE_SIZE,
batch_size=BATCH_SIZE,
class_mode='categorical'
)
# ===================== MODEL BUILDING =====================
# Load the base VGG16 model pre-trained on ImageNet
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
base_model.trainable = False # Freeze the convolutional base
# Add custom classification layers
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
predictions = Dense(NUM_CLASSES, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
# ===================== COMPILE MODEL =====================
model.compile(optimizer=Adam(learning_rate=LEARNING_RATE),
loss='categorical_crossentropy',
metrics=['accuracy'])
# ===================== CALLBACKS =====================
checkpoint = ModelCheckpoint(MODEL_SAVE_PATH, monitor='val_accuracy', save_best_only=True, verbose=1)
early_stop = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)
# ===================== TRAIN MODEL =====================
history = model.fit(
train_generator,
validation_data=val_generator,
epochs=EPOCHS,
callbacks=[checkpoint, early_stop]
)
# ===================== SAVE FINAL MODEL =====================
model.save("vgg16_liver_final_model.h5")