qmjnh commited on
Commit
436fea1
·
verified ·
1 Parent(s): 91f1bbc

attempt fix keras3 compatibility

Browse files
Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -7,21 +7,20 @@ def softmax(vector):
7
  e = np.exp(vector)
8
  return e / e.sum()
9
 
10
- def image_to_output (input_img):
11
- gr_img = []
12
- gr_img.append(input_img)
13
- img2 = tf.image.resize(tf.cast(gr_img, tf.float32)/255. , [224, 224])
14
 
15
- # print(img2)
16
-
17
- x_test = np.asarray(img2)
 
 
 
18
 
19
- prediction = model2(x_test,batch_size=1).flatten()
 
 
 
20
  prediction = softmax(prediction)
21
-
22
  confidences = {labels[i]: float(prediction[i]) for i in range(102)}
23
- # confidences = {labels[i]:float(top[i]) for i in range(num_predictions)}
24
-
25
  return confidences
26
 
27
  # Download the model checkpoint
@@ -47,8 +46,11 @@ for item in [
47
  f.write(params.content)
48
 
49
 
50
- # Load the model
51
- model2=keras.layers.TFSMLayer(pretrained_repo)
 
 
 
52
 
53
  # Read the labels
54
  with open('flower_names.txt') as f:
 
7
  e = np.exp(vector)
8
  return e / e.sum()
9
 
 
 
 
 
10
 
11
+ def image_to_output(input_img):
12
+ # Preprocess the image as before
13
+ img2 = tf.image.resize(tf.cast([input_img], tf.float32)/255., [224, 224])
14
+
15
+ # Run inference
16
+ output = infer(tf.constant(img2))
17
 
18
+ print(output)
19
+
20
+ # Process the output
21
+ prediction = output['output_0'].numpy().flatten()
22
  prediction = softmax(prediction)
 
23
  confidences = {labels[i]: float(prediction[i]) for i in range(102)}
 
 
24
  return confidences
25
 
26
  # Download the model checkpoint
 
46
  f.write(params.content)
47
 
48
 
49
+ # Load the SavedModel
50
+ loaded = tf.saved_model.load(pretrained_repo)
51
+
52
+ # Get the inference function
53
+ infer = loaded.signatures["serving_default"]
54
 
55
  # Read the labels
56
  with open('flower_names.txt') as f: