forked from anmolp476/OpticGenie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
113 lines (64 loc) · 2.8 KB
/
Copy pathmodel.py
File metadata and controls
113 lines (64 loc) · 2.8 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import cv2
import numpy as np
from numpy import load
import tensorflow as tf
from tensorflow import keras
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
classNames = ["banana", "apple", "pear", "grapes", "orange", "kiwi", "watermelon",
"pomegranate", "pineapple", "mango", "cucumber", "carrot", "capsicum", "onion", "potato", "lemon", "tomato", "raddish",
"beetroot", "cabbage", "lettuce", "spinach", "soy beans", "cauliflower", "bell pepper","chilli pepper",
"turnip", "corn", "sweetcorn", "sweetpotato", "paprika", "jalepeno", "ginger", "garlic", "peas", "eggplant"]
trainData = load('c:/tempFolder/trainData.npy')
trainFeatures = load('c:/tempFolder/trainFeatures.npy')
testData = load('c:/tempFolder/testData.npy')
largeTestData = load('c:/tempFolder/largeTestData.npy')
testFeatures = load('c:/tempFolder/testFeatures.npy')
print("Finished loading all the data.")
# testingImage = trainData[1]
# cv2.imshow('testingImage', testingImage)
# thisIndex = trainFeatures[1]
# print(classNames[thisIndex])
# cv2.waitKey(0)
print("Train shape: ", trainData.shape)
print("Features shape: ", trainFeatures.shape)
print("Test data shape: ", testData)
print("Test features shape: ", testFeatures)
trainData = trainData/255.0
testData = testData/255.0
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28,28,3)),
keras.layers.Dense(512, activation="relu"),
keras.layers.Dense(36, activation="softmax")
])
print("Finished building the skeleton of the neural network")
model.compile(
optimizer=tf.optimizers.Adam(),
loss = 'sparse_categorical_crossentropy',
metrics=['accuracy']
)
print("Finished compiling the model.")
loadedModelDir = "c:/tempFolder/model.h5"
if os.path.exists(loadedModelDir):
model = keras.models.load_model(loadedModelDir)
print("Loaded the pre-trained model")
else:
model.fit(trainData, trainFeatures, epochs=200)
model.save(loadedModelDir)
print("Trained then saved the model.")
lossValues, accValues = model.evaluate(testData, testFeatures, verbose=1)
print("############# Testing Accuracy: ", accValues)
# predictionVal = model.predict(testData)
# for predicting, testFeature in zip(predictionVal, testFeatures):
# fruitVegIndex = np.argmax(predicting)
# fruitVegPredicted = classNames[fruitVegIndex]
# fruitVegCorrect = classNames[testFeature]
# print("Predicted class: ", fruitVegPredicted, " Actual class: ", fruitVegCorrect)
img = cv2.imread("C:/Users/pra_d/OneDrive/Desktop/Anmol/Projects/WebDev/repos/opticgenie/raddish.jpg")
img = cv2.resize(img, (28, 28))
img = img/255.0
img = np.expand_dims(img, axis=0)
prediction = model.predict(img)
predicted_index = np.argmax(prediction)
predicted_class = classNames[predicted_index]
print("The predicted class is:", predicted_class)