-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_model.py
24 lines (20 loc) · 965 Bytes
/
image_model.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
from tensorflow.keras.applications import VGG16
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import Adam
def create_image_model():
"""Create and compile the image model using VGG16."""
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
x = Flatten()(base_model.output)
x = Dense(1024, activation='relu')(x)
predictions = Dense(1, activation='sigmoid')(x)
model = Model(inputs=base_model.input, outputs=predictions)
for layer in base_model.layers:
layer.trainable = False
model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])
return model
def train_image_model(X_images, y_images):
"""Train the image model."""
model = create_image_model()
model.fit(X_images, y_images, epochs=10, batch_size=32, validation_split=0.2)
return model