neural network
68
app/createModel.py
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
from tensorflow.keras.models import Sequential, save_model
|
||||||
|
from tensorflow.keras.layers import Dense, Flatten, Conv2D
|
||||||
|
from tensorflow.keras.losses import sparse_categorical_crossentropy
|
||||||
|
from tensorflow.keras.optimizers import Adam
|
||||||
|
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
||||||
|
from config import RESOURCE_DIR
|
||||||
|
import os
|
||||||
|
|
||||||
|
# labels
|
||||||
|
|
||||||
|
labels = ["cabbage", "carrot", "corn", "lettuce", "paprika", "potato", "tomato"]
|
||||||
|
|
||||||
|
# Data configuration
|
||||||
|
training_set_folder = os.path.join(RESOURCE_DIR, 'smaller_train')
|
||||||
|
test_set_folder = os.path.join(RESOURCE_DIR, 'smaller_test')
|
||||||
|
|
||||||
|
# Model config
|
||||||
|
batch_size = 25
|
||||||
|
img_width, img_height, img_num_channels = 25, 25, 3
|
||||||
|
loss_function = sparse_categorical_crossentropy
|
||||||
|
no_classes = 7
|
||||||
|
no_epochs = 40
|
||||||
|
optimizer = Adam()
|
||||||
|
verbosity = 1
|
||||||
|
|
||||||
|
# Determine shape of the data
|
||||||
|
input_shape = (img_width, img_height, img_num_channels)
|
||||||
|
|
||||||
|
# Create a generator
|
||||||
|
train_datagen = ImageDataGenerator(
|
||||||
|
rescale=1./255
|
||||||
|
)
|
||||||
|
train_datagen = train_datagen.flow_from_directory(
|
||||||
|
training_set_folder,
|
||||||
|
save_to_dir=os.path.join(RESOURCE_DIR, "adapted-images"),
|
||||||
|
save_format='jpeg',
|
||||||
|
batch_size=batch_size,
|
||||||
|
target_size=(25, 25),
|
||||||
|
class_mode='sparse')
|
||||||
|
|
||||||
|
# Create the model
|
||||||
|
model = Sequential()
|
||||||
|
model.add(Conv2D(16, kernel_size=(5, 5), activation='relu', input_shape=input_shape))
|
||||||
|
model.add(Conv2D(32, kernel_size=(5, 5), activation='relu'))
|
||||||
|
model.add(Conv2D(64, kernel_size=(5, 5), activation='relu'))
|
||||||
|
model.add(Conv2D(128, kernel_size=(5, 5), activation='relu'))
|
||||||
|
model.add(Flatten())
|
||||||
|
model.add(Dense(16, activation='relu'))
|
||||||
|
model.add(Dense(no_classes, activation='softmax'))
|
||||||
|
|
||||||
|
# Display a model summary
|
||||||
|
model.summary()
|
||||||
|
|
||||||
|
# Compile the model
|
||||||
|
model.compile(loss=loss_function,
|
||||||
|
optimizer=optimizer,
|
||||||
|
metrics=['accuracy'])
|
||||||
|
|
||||||
|
# Start training
|
||||||
|
model.fit(
|
||||||
|
train_datagen,
|
||||||
|
epochs=no_epochs,
|
||||||
|
shuffle=False)
|
||||||
|
|
||||||
|
# Saving model
|
||||||
|
filepath = os.path.join(RESOURCE_DIR, 'saved_model')
|
||||||
|
save_model(model, filepath)
|
||||||
|
|
30
app/loadFromSavedModel.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
from tensorflow.keras.models import load_model
|
||||||
|
from tensorflow import keras as k
|
||||||
|
import numpy as np
|
||||||
|
from config import RESOURCE_DIR
|
||||||
|
import os
|
||||||
|
|
||||||
|
# Load the model
|
||||||
|
model = load_model(
|
||||||
|
os.path.join(RESOURCE_DIR, "saved_model"),
|
||||||
|
custom_objects=None,
|
||||||
|
compile=True
|
||||||
|
)
|
||||||
|
|
||||||
|
# Data fror predictions
|
||||||
|
img_width, img_height, img_num_channels = 25, 25, 3
|
||||||
|
labels = ["cabbage", "carrot", "corn", "lettuce", "paprika", "potato", "tomato"]
|
||||||
|
|
||||||
|
# Predictions
|
||||||
|
filename = 'Image_1.jpg'
|
||||||
|
loaded_image = k.preprocessing.image.load_img(path=RESOURCE_DIR + '/smaller_test/potato/' + filename,
|
||||||
|
target_size=(img_width, img_height, img_num_channels))
|
||||||
|
# convert to array and resample dividing by 255
|
||||||
|
img_array = k.preprocessing.image.img_to_array(loaded_image) / 255.
|
||||||
|
|
||||||
|
# add sample dimension. the predictor is expecting (1, CHANNELS, IMG_WIDTH, IMG_HEIGHT)
|
||||||
|
img_np_array = np.expand_dims(img_array, axis=0)
|
||||||
|
|
||||||
|
predictions = model.predict(img_np_array)
|
||||||
|
prediction = np.argmax(predictions[0])
|
||||||
|
print(f'Ground truth: {filename} - Prediction: {labels[prediction]}')
|
BIN
requirements.txt
13
resources/saved_model/keras_metadata.pb
Normal file
BIN
resources/saved_model/saved_model.pb
Normal file
BIN
resources/saved_model/variables/variables.data-00000-of-00001
Normal file
BIN
resources/saved_model/variables/variables.index
Normal file
BIN
resources/smaller_test/cabbage/Image_1.jpg
Normal file
After Width: | Height: | Size: 269 KiB |
BIN
resources/smaller_test/cabbage/Image_10.jpg
Normal file
After Width: | Height: | Size: 269 KiB |
BIN
resources/smaller_test/cabbage/Image_2.jpg
Normal file
After Width: | Height: | Size: 1.4 MiB |
BIN
resources/smaller_test/cabbage/Image_3.jpg
Normal file
After Width: | Height: | Size: 94 KiB |
BIN
resources/smaller_test/cabbage/Image_4.jpg
Normal file
After Width: | Height: | Size: 1.6 MiB |
BIN
resources/smaller_test/cabbage/Image_5.jpg
Normal file
After Width: | Height: | Size: 299 KiB |
BIN
resources/smaller_test/cabbage/Image_6.jpg
Normal file
After Width: | Height: | Size: 238 KiB |
BIN
resources/smaller_test/cabbage/Image_7.jpg
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
resources/smaller_test/cabbage/Image_8.jpg
Normal file
After Width: | Height: | Size: 182 KiB |
BIN
resources/smaller_test/cabbage/Image_9.jpg
Normal file
After Width: | Height: | Size: 555 KiB |
BIN
resources/smaller_test/carrot/Image_1.jpg
Normal file
After Width: | Height: | Size: 191 KiB |
BIN
resources/smaller_test/carrot/Image_10.jpg
Normal file
After Width: | Height: | Size: 191 KiB |
BIN
resources/smaller_test/carrot/Image_2.jpg
Normal file
After Width: | Height: | Size: 103 KiB |
BIN
resources/smaller_test/carrot/Image_3.jpg
Normal file
After Width: | Height: | Size: 373 KiB |
BIN
resources/smaller_test/carrot/Image_4.png
Normal file
After Width: | Height: | Size: 190 KiB |
BIN
resources/smaller_test/carrot/Image_5.jpg
Normal file
After Width: | Height: | Size: 334 KiB |
BIN
resources/smaller_test/carrot/Image_6.jpg
Normal file
After Width: | Height: | Size: 57 KiB |
BIN
resources/smaller_test/carrot/Image_7.JPG
Normal file
After Width: | Height: | Size: 180 KiB |
BIN
resources/smaller_test/carrot/Image_8.jpg
Normal file
After Width: | Height: | Size: 200 KiB |
BIN
resources/smaller_test/carrot/Image_9.jpeg
Normal file
After Width: | Height: | Size: 81 KiB |
BIN
resources/smaller_test/corn/Image_1.jpg
Normal file
After Width: | Height: | Size: 533 KiB |
BIN
resources/smaller_test/corn/Image_10.jpg
Normal file
After Width: | Height: | Size: 329 KiB |
BIN
resources/smaller_test/corn/Image_2.jpg
Normal file
After Width: | Height: | Size: 1.4 MiB |
BIN
resources/smaller_test/corn/Image_3.jpg
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
resources/smaller_test/corn/Image_4.jpg
Normal file
After Width: | Height: | Size: 138 KiB |
BIN
resources/smaller_test/corn/Image_5.jpg
Normal file
After Width: | Height: | Size: 6.4 MiB |
BIN
resources/smaller_test/corn/Image_6.jpg
Normal file
After Width: | Height: | Size: 202 KiB |
BIN
resources/smaller_test/corn/Image_7.jpg
Normal file
After Width: | Height: | Size: 166 KiB |
BIN
resources/smaller_test/corn/Image_8.jpg
Normal file
After Width: | Height: | Size: 166 KiB |
BIN
resources/smaller_test/corn/Image_9.jpg
Normal file
After Width: | Height: | Size: 306 KiB |
BIN
resources/smaller_test/lettuce/Image_1.jpg
Normal file
After Width: | Height: | Size: 881 KiB |
BIN
resources/smaller_test/lettuce/Image_10.jpg
Normal file
After Width: | Height: | Size: 348 KiB |
BIN
resources/smaller_test/lettuce/Image_2.jpg
Normal file
After Width: | Height: | Size: 2.1 MiB |
BIN
resources/smaller_test/lettuce/Image_3.jpg
Normal file
After Width: | Height: | Size: 118 KiB |
BIN
resources/smaller_test/lettuce/Image_4.jpg
Normal file
After Width: | Height: | Size: 1.4 MiB |
BIN
resources/smaller_test/lettuce/Image_5.jpeg
Normal file
After Width: | Height: | Size: 514 KiB |
BIN
resources/smaller_test/lettuce/Image_6.jpg
Normal file
After Width: | Height: | Size: 539 KiB |
BIN
resources/smaller_test/lettuce/Image_7.jpg
Normal file
After Width: | Height: | Size: 77 KiB |
BIN
resources/smaller_test/lettuce/Image_8.jpg
Normal file
After Width: | Height: | Size: 742 KiB |
BIN
resources/smaller_test/lettuce/Image_9.jpg
Normal file
After Width: | Height: | Size: 403 KiB |
BIN
resources/smaller_test/paprika/Image_1.jpg
Normal file
After Width: | Height: | Size: 293 KiB |
BIN
resources/smaller_test/paprika/Image_10.jpg
Normal file
After Width: | Height: | Size: 293 KiB |
BIN
resources/smaller_test/paprika/Image_2.jpg
Normal file
After Width: | Height: | Size: 84 KiB |
BIN
resources/smaller_test/paprika/Image_3.jpg
Normal file
After Width: | Height: | Size: 113 KiB |
BIN
resources/smaller_test/paprika/Image_4.jpg
Normal file
After Width: | Height: | Size: 81 KiB |
BIN
resources/smaller_test/paprika/Image_5.jpg
Normal file
After Width: | Height: | Size: 42 KiB |
BIN
resources/smaller_test/paprika/Image_6.jpg
Normal file
After Width: | Height: | Size: 77 KiB |
BIN
resources/smaller_test/paprika/Image_7.jpg
Normal file
After Width: | Height: | Size: 100 KiB |
BIN
resources/smaller_test/paprika/Image_8.jpg
Normal file
After Width: | Height: | Size: 90 KiB |
BIN
resources/smaller_test/paprika/Image_9.jpg
Normal file
After Width: | Height: | Size: 1.3 MiB |
BIN
resources/smaller_test/potato/Image_1.jpg
Normal file
After Width: | Height: | Size: 141 KiB |
BIN
resources/smaller_test/potato/Image_10.jpg
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
resources/smaller_test/potato/Image_2.jpg
Normal file
After Width: | Height: | Size: 61 KiB |
BIN
resources/smaller_test/potato/Image_3.jpg
Normal file
After Width: | Height: | Size: 164 KiB |
BIN
resources/smaller_test/potato/Image_4.jpg
Normal file
After Width: | Height: | Size: 24 KiB |
BIN
resources/smaller_test/potato/Image_5.JPG
Normal file
After Width: | Height: | Size: 72 KiB |
BIN
resources/smaller_test/potato/Image_6.jpg
Normal file
After Width: | Height: | Size: 60 KiB |
BIN
resources/smaller_test/potato/Image_7.jpg
Normal file
After Width: | Height: | Size: 111 KiB |
BIN
resources/smaller_test/potato/Image_8.jpg
Normal file
After Width: | Height: | Size: 27 KiB |
BIN
resources/smaller_test/potato/Image_9.jpg
Normal file
After Width: | Height: | Size: 132 KiB |
BIN
resources/smaller_test/tomato/Image_1.jpg
Normal file
After Width: | Height: | Size: 122 KiB |
BIN
resources/smaller_test/tomato/Image_10.jpg
Normal file
After Width: | Height: | Size: 122 KiB |
BIN
resources/smaller_test/tomato/Image_2.jpg
Normal file
After Width: | Height: | Size: 101 KiB |
BIN
resources/smaller_test/tomato/Image_3.jpg
Normal file
After Width: | Height: | Size: 658 KiB |
BIN
resources/smaller_test/tomato/Image_4.jpg
Normal file
After Width: | Height: | Size: 465 KiB |
BIN
resources/smaller_test/tomato/Image_5.jpg
Normal file
After Width: | Height: | Size: 213 KiB |
BIN
resources/smaller_test/tomato/Image_6.jpg
Normal file
After Width: | Height: | Size: 328 KiB |
BIN
resources/smaller_test/tomato/Image_7.jpg
Normal file
After Width: | Height: | Size: 1.0 MiB |
BIN
resources/smaller_test/tomato/Image_8.jpg
Normal file
After Width: | Height: | Size: 3.7 MiB |
BIN
resources/smaller_test/tomato/Image_9.jpg
Normal file
After Width: | Height: | Size: 124 KiB |
BIN
resources/smaller_train/cabbage/Image_1.jpg
Normal file
After Width: | Height: | Size: 269 KiB |
BIN
resources/smaller_train/cabbage/Image_10.jpg
Normal file
After Width: | Height: | Size: 516 KiB |
BIN
resources/smaller_train/cabbage/Image_100.jpg
Normal file
After Width: | Height: | Size: 516 KiB |
BIN
resources/smaller_train/cabbage/Image_11.jpg
Normal file
After Width: | Height: | Size: 86 KiB |
BIN
resources/smaller_train/cabbage/Image_12.jpg
Normal file
After Width: | Height: | Size: 256 KiB |
BIN
resources/smaller_train/cabbage/Image_13.jpg
Normal file
After Width: | Height: | Size: 1.1 MiB |
BIN
resources/smaller_train/cabbage/Image_14.jpg
Normal file
After Width: | Height: | Size: 63 KiB |
BIN
resources/smaller_train/cabbage/Image_15.jpg
Normal file
After Width: | Height: | Size: 712 KiB |
BIN
resources/smaller_train/cabbage/Image_16.jpg
Normal file
After Width: | Height: | Size: 137 KiB |
BIN
resources/smaller_train/cabbage/Image_17.jpg
Normal file
After Width: | Height: | Size: 163 KiB |
BIN
resources/smaller_train/cabbage/Image_18.jpg
Normal file
After Width: | Height: | Size: 136 KiB |
BIN
resources/smaller_train/cabbage/Image_19.jpg
Normal file
After Width: | Height: | Size: 163 KiB |
BIN
resources/smaller_train/cabbage/Image_2.jpg
Normal file
After Width: | Height: | Size: 1.4 MiB |
BIN
resources/smaller_train/cabbage/Image_20.jpg
Normal file
After Width: | Height: | Size: 120 KiB |
BIN
resources/smaller_train/cabbage/Image_21.jpg
Normal file
After Width: | Height: | Size: 96 KiB |
BIN
resources/smaller_train/cabbage/Image_22.jpg
Normal file
After Width: | Height: | Size: 142 KiB |
BIN
resources/smaller_train/cabbage/Image_23.jpg
Normal file
After Width: | Height: | Size: 621 KiB |
BIN
resources/smaller_train/cabbage/Image_24.jpg
Normal file
After Width: | Height: | Size: 193 KiB |
BIN
resources/smaller_train/cabbage/Image_25.jpg
Normal file
After Width: | Height: | Size: 297 KiB |
BIN
resources/smaller_train/cabbage/Image_26.JPG
Normal file
After Width: | Height: | Size: 255 KiB |
BIN
resources/smaller_train/cabbage/Image_27.jpg
Normal file
After Width: | Height: | Size: 136 KiB |
BIN
resources/smaller_train/cabbage/Image_28.jpg
Normal file
After Width: | Height: | Size: 606 KiB |
BIN
resources/smaller_train/cabbage/Image_29.jpg
Normal file
After Width: | Height: | Size: 47 KiB |