<jupyter_start><jupyter_text>Install required packages<jupyter_code>import sys

!{sys.executable} -m pip install --upgrade pip
!{sys.executable} -m pip install --upgrade opencv-python numpy pymodi tensorflow --user<jupyter_output><empty_output><jupyter_text>Import packages<jupyter_code>import numpy as np
import cv2
from IPython.display import display, Image, clear_output
import time
import modi<jupyter_output>Running PyMODI (v0.9.1)<jupyter_text>Define a necessary helper method<jupyter_code>def new_color():
    rgb = np.random.randint(0, 255, size=3)
    
    solid_color = np.zeros((300, 300, 3), np.uint8)
    solid_color[:] = rgb[::-1]
    _, ret = cv2.imencode('.png', solid_color)
    image = Image(data=ret)
    display(image)
    return rgb

new_color()<jupyter_output><empty_output><jupyter_text>Intialize MODI object<jupyter_code>bundle = modi.MODI()

env = bundle.envs[0]
led = bundle.leds[0]<jupyter_output>Run Ser Task.
Start initializing connected MODI modules
Env (392) has been connected!
Led (3712) has been connected!<jupyter_text>Generate data set<jupyter_code>train_set = []
label_set = []

clear_output(wait=True)
new_color()
input("Place your env module on the color and press Enter when ready...")

for i in range(30):
    clear_output(wait=True)
    rgb = new_color()
    led.rgb = rgb // 5
    time.sleep(0.5)
    for i in range(10):
        label = rgb / 255
        data = [env.red, env.green, env.blue]
        label_set.append(label)
        train_set.append(data)
        print(f"{data} -> {label}")
        time.sleep(0.1)
    time.sleep(0.1)

train_set = np.array(train_set)
label_set = np.array(label_set)<jupyter_output><empty_output><jupyter_text>Check out the generated dataset and save them to ./data<jupyter_code>print(train_set.shape)
print(label_set.shape)

np.save('./data/label_set.npy', label_set, allow_pickle=True)
np.save('./data/train_set.npy', train_set, allow_pickle=True)<jupyter_output>(300, 3)
(300, 3)<jupyter_text>Training!!<jupyter_code>from tensorflow.keras.models import Sequential, save_model
from tensorflow.keras.layers import Dense

train_set = np.load('./data/train_set.npy', allow_pickle=True)
label_set = np.load('./data/label_set.npy', allow_pickle=True)

shuffled_idx = np.random.permutation(train_set.shape[0])
split_idx = int(train_set.shape[0] * 0.8)
X_Train = train_set[shuffled_idx[:split_idx]]
y_Train = label_set[shuffled_idx[:split_idx]]
X_Test = train_set[shuffled_idx[split_idx:]]
y_Test = label_set[shuffled_idx[split_idx:]]

model = Sequential()
model.add(Dense(10, input_shape=(3,)))
model.add(Dense(10))
model.add(Dense(3, activation='sigmoid'))

model.compile(loss='mse', optimizer='adam')

EPOCH = 50
BATCH = 8
model.fit(X_Train, y_Train, epochs=EPOCH, batch_size=BATCH, verbose=1,
          validation_data=(X_Test, y_Test))

save_model(model, 'weights.h5')<jupyter_output><empty_output><jupyter_text>Inference!!<jupyter_code>from tensorflow.keras.models import load_model
import time

model = load_model('weights.h5')

while True:
    clear_ouput(wait=True)
    rgb_read = np.array([env.red, env.green, env.blue])[np.newaxis, :]
    result = model.predict(rgb_read)[0] * 255 // 1
    color = np.zeros((300, 300, 3), np.uint8)
    color[:] = result[::-1]
    _, ret = cv2.imencode('.png', color)
    image = Image(data=ret)
    display(image)
    if cv2.waitKey(50) & 0xFF == ord('q'):
        break
    led.rgb = result // 5<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14144641301190822, 'fraction_numerical': 0.024978216671507406, 'mean_word_length': 4.989565217391305, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '522203', 'license': 'MIT', 'n_tokens_mistral': 1361, 'n_tokens_neox': 1294, 'n_words': 243, 'path': 'examples/ml_examples/physical_color_spuit/color_spuit.ipynb', 'repo_name': 'jha929/pyMODI', 'size': '3443'}