finished
This commit is contained in:
parent
7b11c01c02
commit
bbb20e3b3a
37
main.py
37
main.py
|
|
@ -4,6 +4,7 @@ import tensorflow as tf
|
|||
import os
|
||||
|
||||
|
||||
|
||||
#Import Data
|
||||
PATH = os.path.join(os.getcwd(), "crop-data")
|
||||
|
||||
|
|
@ -23,12 +24,13 @@ validation_data = tf.keras.utils.image_dataset_from_directory(validation_data,
|
|||
batch_size=BATCH_SIZE,
|
||||
image_size=IMG_SIZE)
|
||||
|
||||
test_data = tf.keras.utils.image_dataset_from_directory(test_data,
|
||||
shuffle=True,
|
||||
batch_size=BATCH_SIZE,
|
||||
image_size=IMG_SIZE)
|
||||
|
||||
train_size = len(training_data)-len(validation_data)
|
||||
test_data = training_data.skip(train_size).take(len(validation_data))
|
||||
class_names = training_data.class_names
|
||||
training_data = training_data.take(train_size)
|
||||
|
||||
print(f"Train: {len(training_data)}\nValid: {len(validation_data)}\nTest: {len(test_data)}")
|
||||
|
||||
|
||||
#View Data
|
||||
plt.figure(figsize=(10,10))
|
||||
|
|
@ -54,9 +56,6 @@ data_augmentation = tf.keras.Sequential([
|
|||
tf.keras.layers.RandomRotation(0.2)
|
||||
])
|
||||
|
||||
#Preprocessing
|
||||
preprocess_input = tf.keras.applications.mobilenet_v3.preprocess_input
|
||||
|
||||
#Create Base Model From MobileNetV3
|
||||
IMG_SHAPE = IMG_SIZE + (3,)
|
||||
base_model = tf.keras.applications.MobileNetV3Large(
|
||||
|
|
@ -85,7 +84,6 @@ predication_batch = prediction_layer(feature_batch_avg)
|
|||
|
||||
inputs = tf.keras.Input(shape=(160,160,3))
|
||||
x = data_augmentation(inputs)
|
||||
x = preprocess_input(x)
|
||||
x = base_model(x, training=False)
|
||||
x = global_avg_layer(x)
|
||||
x = tf.keras.layers.Dropout(0.2)(x)
|
||||
|
|
@ -102,17 +100,13 @@ base_learning_rate = 0.0001
|
|||
|
||||
training_data = training_data.map(lambda x,y: (x, tf.one_hot(y,38)))
|
||||
validation_data = validation_data.map(lambda x,y: (x, tf.one_hot(y,38)))
|
||||
|
||||
|
||||
test_data = test_data.map(lambda x,y: (x, tf.one_hot(y,38)))
|
||||
|
||||
optimizer = tf.keras.optimizers.Adam(learning_rate=base_learning_rate)
|
||||
loss = tf.keras.losses.CategoricalCrossentropy()
|
||||
metrics = [tf.keras.metrics.CategoricalAccuracy()]
|
||||
|
||||
|
||||
phy_dev = tf.config.list_physical_devices("GPU")
|
||||
for gpu in phy_dev:
|
||||
tf.config.experimental.set_memory_growth(gpu, True)
|
||||
|
||||
model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
|
||||
|
||||
|
|
@ -131,7 +125,7 @@ lr_schedule = tf.keras.callbacks.ReduceLROnPlateau(
|
|||
min_lr=1e-6
|
||||
)
|
||||
|
||||
earky_stopping = tf.keras.callbacks.EarlyStopping(
|
||||
early_stopping = tf.keras.callbacks.EarlyStopping(
|
||||
monitor="val_loss",
|
||||
patience=10,
|
||||
restore_best_weights=True
|
||||
|
|
@ -140,6 +134,15 @@ earky_stopping = tf.keras.callbacks.EarlyStopping(
|
|||
history = model.fit(training_data,
|
||||
epochs=initial_epochs,
|
||||
validation_data=validation_data,
|
||||
callbacks=[lr_schedule])
|
||||
callbacks=[lr_schedule, early_stopping])
|
||||
|
||||
model.save("crop-classifier-callbacks.keras")
|
||||
model.save("crop-classifier-better-test.keras")
|
||||
|
||||
#Evaluate Model
|
||||
results = model.evaluate(validation_data)
|
||||
print(f"Validation Loss: {results[0]}")
|
||||
print(f"Validation Accuracy: {results[1]}")
|
||||
|
||||
results = model.evaluate(test_data)
|
||||
print(f"Test Loss: {results[0]}")
|
||||
print(f"Test Accuracy: {results[1]}")
|
||||
|
|
|
|||
BIN
report.odt
BIN
report.odt
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1,42 @@
|
|||
import numpy as np
|
||||
import tensorflow as tf
|
||||
import os
|
||||
|
||||
|
||||
#Import Data
|
||||
PATH = os.path.join(os.getcwd(), "crop-data")
|
||||
|
||||
validation_data = os.path.join(PATH, "valid")
|
||||
validation_data_32 = tf.keras.utils.image_dataset_from_directory(validation_data,
|
||||
shuffle=True,
|
||||
batch_size=32,
|
||||
image_size=(160,160))
|
||||
|
||||
validation_data_64 = tf.keras.utils.image_dataset_from_directory(validation_data,
|
||||
shuffle=True,
|
||||
batch_size=64,
|
||||
image_size=(160,160))
|
||||
|
||||
AUTOTUNE = tf.data.AUTOTUNE
|
||||
validation_data_32 = validation_data_32.prefetch(buffer_size=AUTOTUNE)
|
||||
validation_data_64 = validation_data_64.prefetch(buffer_size=AUTOTUNE)
|
||||
|
||||
validation_data_32 = validation_data_32.map(lambda x,y: (x, tf.one_hot(y,38)))
|
||||
validation_data_64 = validation_data_64.map(lambda x,y: (x, tf.one_hot(y,38)))
|
||||
|
||||
|
||||
model1 = tf.keras.models.load_model("crop-classifier.keras")
|
||||
model2 = tf.keras.models.load_model("crop-classifier-64-batch.keras")
|
||||
model3 = tf.keras.models.load_model("crop-classifier-callbacks.keras")
|
||||
|
||||
result = model1.evaluate(validation_data_32)
|
||||
print(f"Validation Loss: {result[0]}")
|
||||
print(f"Accuracy: {result[1]}")
|
||||
|
||||
result = model2.evaluate(validation_data_64)
|
||||
print(f"Validation Loss: {result[0]}")
|
||||
print(f"Accuracy: {result[1]}")
|
||||
|
||||
result = model3.evaluate(validation_data_64)
|
||||
print(f"Validation Loss: {result[0]}")
|
||||
print(f"Accuracy: {result[1]}")
|
||||
46
test.py
46
test.py
|
|
@ -1,4 +1,42 @@
|
|||
#Load model
|
||||
#Load test data
|
||||
#Test
|
||||
#Print results
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
import os
|
||||
|
||||
|
||||
#Import Data
|
||||
PATH = os.path.join(os.getcwd(), "crop-data")
|
||||
|
||||
test_data = os.path.join(PATH, "test")
|
||||
test_data_32 = tf.keras.utils.image_dataset_from_directory(test_data,
|
||||
shuffle=True,
|
||||
batch_size=32,
|
||||
image_size=(160,160))
|
||||
|
||||
test_data_64 = tf.keras.utils.image_dataset_from_directory(test_data,
|
||||
shuffle=True,
|
||||
batch_size=64,
|
||||
image_size=(160,160))
|
||||
|
||||
AUTOTUNE = tf.data.AUTOTUNE
|
||||
test_data_32 = test_data_32.prefetch(buffer_size=AUTOTUNE)
|
||||
test_data_64 = test_data_64.prefetch(buffer_size=AUTOTUNE)
|
||||
|
||||
test_data_32 = test_data_32.map(lambda x,y: (x, tf.one_hot(y,38)))
|
||||
test_data_64 = test_data_64.map(lambda x,y: (x, tf.one_hot(y,38)))
|
||||
|
||||
|
||||
model1 = tf.keras.models.load_model("crop-classifier.keras")
|
||||
model2 = tf.keras.models.load_model("crop-classifier-64-batch.keras")
|
||||
model3 = tf.keras.models.load_model("crop-classifier-callbacks.keras")
|
||||
|
||||
result = model1.evaluate(test_data_32)
|
||||
print(f"Test Loss: {result[0]}")
|
||||
print(f"Test Accuracy: {result[1]}")
|
||||
|
||||
result = model2.evaluate(test_data_64)
|
||||
print(f"Test Loss: {result[0]}")
|
||||
print(f"Test Accuracy: {result[1]}")
|
||||
|
||||
result = model3.evaluate(test_data_64)
|
||||
print(f"Test Loss: {result[0]}")
|
||||
print(f"Test Accuracy: {result[1]}")
|
||||
|
|
|
|||
Loading…
Reference in New Issue