BunkerMinecraft commited on
Commit
6121ad5
·
verified ·
1 Parent(s): 188dc73

Update inference.py

Browse files
Files changed (1) hide show
  1. inference.py +19 -2
inference.py CHANGED
@@ -5,8 +5,20 @@ import numpy as np
5
 
6
  with open("config.json") as f:
7
  cfg = json.load(f)
 
 
8
 
9
- model = tf.keras.applications.EfficientNetB0(weights=None)
 
 
 
 
 
 
 
 
 
 
10
  model.load_weights("model.weights.h5")
11
 
12
  def predict(image: Image.Image):
@@ -14,4 +26,9 @@ def predict(image: Image.Image):
14
  x = np.array(img) / 255.0
15
  x = np.expand_dims(x, 0)
16
  y = model.predict(x)
17
- return {"predictions": y.tolist()}
 
 
 
 
 
 
5
 
6
  with open("config.json") as f:
7
  cfg = json.load(f)
8
+
9
+ class_names = ['battery', 'glass', 'metal', 'organic', 'paper', 'plastic']
10
 
11
+ base_model = tf.keras.applications.EfficientNetB7(weights=None)
12
+ model = models.Sequential([
13
+ base_model,
14
+ layers.GlobalAveragePooling2D(),
15
+ layers.Dropout(dropout_rate),
16
+ layers.Dense(256, activation='relu', kernel_regularizer=kr),
17
+ layers.Dropout(dropout_rate),
18
+ layers.Dense(128, activation='relu', kernel_regularizer=kr),
19
+ layers.Dropout(dropout_rate),
20
+ layers.Dense(num_classes, activation='softmax')
21
+ ])
22
  model.load_weights("model.weights.h5")
23
 
24
  def predict(image: Image.Image):
 
26
  x = np.array(img) / 255.0
27
  x = np.expand_dims(x, 0)
28
  y = model.predict(x)
29
+ class_idx = int(np.argmax(y, axis=1)[0])
30
+ confidence = float(np.max(y))
31
+ return {
32
+ "class": class_names[class_idx],
33
+ "confidence": confidence
34
+ }