-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
71 lines (53 loc) · 2.26 KB
/
main.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
from pydantic import BaseModel,Field
from typing import Literal
class HyperParameters(BaseModel):
model_size: Literal["EfficientNetB0", "EfficientNetB1","EfficientNetB2","EfficientNetB3","EfficientNetB4","EfficientNetB5","EfficientNetB6","EfficientNetB7"]
LearningRate:float=Field(gt=0,lt=1)
BatchSize:int
Epochs:int
def model():
import tensorflow as tf
from tensorflow.keras.applications import EfficientNetB1
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
except RuntimeError as e:
print(e)
BatchSize = 4
LearningRate =0.01
EPOCHS = 2
n_classes = 101
base_model = EfficientNetB1(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
x = base_model.output
x = GlobalAveragePooling2D()(x)
predictions = Dense(n_classes, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
model.compile(optimizer=Adam(learning_rate=LearningRate), loss='categorical_crossentropy', metrics=['accuracy'])
train_datagen = ImageDataGenerator(preprocessing_function=tf.keras.applications.efficientnet.preprocess_input, validation_split=0.3)
train_generator = train_datagen.flow_from_directory(
r'food-101/food-101/images',
target_size=(224, 224),
batch_size=BatchSize,
class_mode='categorical',
subset='training')
validation_generator = train_datagen.flow_from_directory(
r'food-101/food-101/images',
target_size=(224, 224),
batch_size=BatchSize,
class_mode='categorical',
subset='validation')
model.fit(
train_generator,
steps_per_epoch=train_generator.samples // BatchSize,
validation_data=validation_generator,
validation_steps=validation_generator.samples // BatchSize,
epochs=EPOCHS)
model.save("food-image-recognition.h5")
if __name__ == '__main__':
model()