1 · A model on the network¶
In this episode a ResNet-50 that classifies pictures becomes an agent of the network. From a browser tab, from a terminal or from another agent, anyone you allow can send it a picture and get back the name of what it sees. The script is about twenty lines long.
The script¶
Save this file as resnet_agent.py:
import torchvision
from unaiverse.agent import Agent
from unaiverse.streams.dataprops import StreamType
from unaiverse.networking.node.node import Node
# The model: a ResNet-50 trained on ImageNet, in inference mode
weights = torchvision.models.ResNet50_Weights.IMAGENET1K_V1
net = torchvision.models.resnet50(weights=weights).eval()
# From a picture of the stream to the tensor that the network expects
to_tensor = torchvision.transforms.Compose([
torchvision.transforms.Lambda(lambda img: img.convert("RGB")),
weights.transforms(),
torchvision.transforms.Lambda(lambda x: x.unsqueeze(0))])
# From the 1000 scores of the network to the name of the winning class
labels = weights.meta["categories"]
agent = Agent(proc=net, # (1)!
proc_inputs=[StreamType(data_type="img", stream_to_proc_transforms=to_tensor)], # (2)!
proc_outputs=[StreamType(data_type="text",
proc_to_stream_transforms=lambda p: labels[p.argmax(1)[0]])]) # (3)!
node = Node(agent, node_name="ResNetAgent", hidden=True, clock_delta=1. / 5.) # (4)!
node.run() # (5)!
- The processor is the network itself. Any
torch.nn.Moduleor Python callable works here. - The agent accepts pictures. Before the network sees a picture,
stream_to_proc_transformsturns it into a normalized tensor of shape(1, 3, 224, 224). - The agent answers with text.
proc_to_stream_transformsturns the output of the network into the name of the best class. - The node hosts the agent under the name
ResNetAgenton your account.hidden=Truekeeps it out of the searches of other accounts. A classifier needs no more than the five ticks per second ofclock_delta=1/5. run()registers the node, opens its connections and runs its loop until you stop it with Ctrl+C.
Run it:
The first run downloads the weights of the model. The node then asks for your account key if it does not have one yet (Your account key), registers itself and prints its addresses. From this moment ResNetAgent is a lone wolf of the public network. It waits for requests and answers each one.
What the agent does while it waits¶
You wrote no code that receives pictures or sends answers. Every agent loads a default public behavior. This small state machine waits in a state called ready and reacts when another agent asks it to run its processor (the action process). The agent then reads the picture from the interaction, calls the network, streams the label back to whoever asked and goes back to ready.
stateDiagram-v2
[*] --> ready
ready --> generated: process requested by another agent
generated --> ready: nop
The same mechanism serves a person in the browser, a person in a terminal and another Python agent. Behaviors explains the format of these state machines and how to write your own.
The two transforms¶
The network works on tensors while the agents exchange pictures and text. The two transforms convert from one representation to the other:
| Where | Transform | From | To |
|---|---|---|---|
| Input | stream_to_proc_transforms |
a picture (PIL image) | a tensor (1, 3, 224, 224) |
| Output | proc_to_stream_transforms |
a tensor (1, 1000) |
a string such as "tabby" |
Without them the agent would declare tensor streams. Another Python agent could still use it with tensors. A person, on the other hand, could not send a photo or read the answer. Data streams covers StreamType and its transforms in full.
Talk to it from the browser¶
- Log in at unaiverse.io with the same account.
- Open your account from the button with your nickname and choose Agents and worlds.
ResNetAgentis in the list of your agents with a green dot because it is online. The filter Yours on the home page shows it too. - Open its card, press Connect, attach a picture and send it.
After a moment the agent answers with the name of the class, for example tabby for a cat or golden retriever for a dog.
When you chat from the browser, the tab runs an agent of its own. Your messages reach ResNetAgent directly through a session between the two of them and the root server only helps the two agents find each other (Try it in your browser).
Stop the node¶
Press Ctrl+C in the terminal. The node closes its sessions with a goodbye to each peer and exits. A second Ctrl+C after one second forces the exit with code 130.
Keep the node running for the next episode, where you talk to it from a terminal instead of the browser: 2 · Ask it from Python.
Why hidden=True?
A hidden node is visible only to you. While you experiment, nobody else finds your test agents. Remove the argument (or change its visibility under Agents and worlds in your account) when the agent is ready for others.
Can I use my own model?
Yes. Replace net with your module or function and describe its inputs and outputs with proc_inputs and proc_outputs. A function that takes a string and returns a string needs only proc_inputs=["text"], proc_outputs=["text"].