danielritchie commited on
Commit
6034b35
·
1 Parent(s): b39c1dd

Pure NumPy Forward Pass

Browse files
Files changed (2) hide show
  1. requirements.txt +0 -1
  2. utils/color_model.py +37 -42
requirements.txt CHANGED
@@ -1,6 +1,5 @@
1
  gradio
2
  numpy
3
  matplotlib
4
- tensorflow
5
  huggingface_hub
6
 
 
1
  gradio
2
  numpy
3
  matplotlib
 
4
  huggingface_hub
5
 
utils/color_model.py CHANGED
@@ -1,65 +1,60 @@
 
1
  import numpy as np
2
- import tensorflow as tf
3
  from huggingface_hub import hf_hub_download
4
 
5
-
6
- # Download model file from HF
7
- model_path = hf_hub_download(
8
  repo_id="danielritchie/vibe-color-model",
9
- filename="vibe_model.tflite" # confirm exact filename
10
  )
11
 
12
- # Load TFLite interpreter
13
- interpreter = tf.lite.Interpreter(model_path=model_path)
14
- interpreter.allocate_tensors()
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- input_details = interpreter.get_input_details()
17
- output_details = interpreter.get_output_details()
 
18
 
19
 
20
  def infer_color(vad):
21
- input_data = np.array([[
22
  vad["V"],
23
  vad["A"],
24
  vad["D"],
25
  vad["Cx"],
26
  vad["Co"]
27
- ]], dtype=np.float32)
28
 
29
- interpreter.set_tensor(input_details[0]["index"], input_data)
30
- interpreter.invoke()
31
- output_data = interpreter.get_tensor(output_details[0]["index"])
32
 
33
- r, g, b, e, i = output_data[0]
 
34
 
35
- return {
36
- "R": float(r),
37
- "G": float(g),
38
- "B": float(b),
39
- "E": float(e),
40
- "I": float(i)
41
- }
42
 
 
43
 
44
- def scale_rgb(rgb):
45
  return {
46
- "R": int(max(0, min(255, rgb["R"] * 255))),
47
- "G": int(max(0, min(255, rgb["G"] * 255))),
48
- "B": int(max(0, min(255, rgb["B"] * 255))),
49
- "E": rgb["E"],
50
- "I": rgb["I"]
51
  }
52
 
53
-
54
- def render_color(rgb):
55
- return f"""
56
- <div style="
57
- width:100%;
58
- height:240px;
59
- border-radius:18px;
60
- background: rgb({rgb['R']},{rgb['G']},{rgb['B']});
61
- box-shadow: 0px 6px 32px rgba(0,0,0,0.25);
62
- transition: all 0.3s ease-in-out;
63
- "></div>
64
- """
65
-
 
1
+ import json
2
  import numpy as np
 
3
  from huggingface_hub import hf_hub_download
4
 
5
+ # Download weights
6
+ weights_path = hf_hub_download(
 
7
  repo_id="danielritchie/vibe-color-model",
8
+ filename="vibe_weights.json"
9
  )
10
 
11
+ with open(weights_path, "r") as f:
12
+ weights = json.load(f)
13
+
14
+ # Extract layers
15
+ W0 = np.array(weights["layer_0"]["weights"], dtype=np.float32)
16
+ b0 = np.array(weights["layer_0"]["biases"], dtype=np.float32)
17
+
18
+ W1 = np.array(weights["layer_1"]["weights"], dtype=np.float32)
19
+ b1 = np.array(weights["layer_1"]["biases"], dtype=np.float32)
20
+
21
+ W2 = np.array(weights["layer_2"]["weights"], dtype=np.float32)
22
+ b2 = np.array(weights["layer_2"]["biases"], dtype=np.float32)
23
+
24
+
25
+ def relu(x):
26
+ return np.maximum(0, x)
27
 
28
+
29
+ def sigmoid(x):
30
+ return 1 / (1 + np.exp(-x))
31
 
32
 
33
  def infer_color(vad):
34
+ x = np.array([
35
  vad["V"],
36
  vad["A"],
37
  vad["D"],
38
  vad["Cx"],
39
  vad["Co"]
40
+ ], dtype=np.float32)
41
 
42
+ # Layer 0
43
+ x = relu(np.dot(x, W0) + b0)
 
44
 
45
+ # Layer 1
46
+ x = relu(np.dot(x, W1) + b1)
47
 
48
+ # Layer 2
49
+ x = sigmoid(np.dot(x, W2) + b2)
 
 
 
 
 
50
 
51
+ r, g, b, e, i = x.tolist()
52
 
 
53
  return {
54
+ "R": r,
55
+ "G": g,
56
+ "B": b,
57
+ "E": e,
58
+ "I": i
59
  }
60