Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions graph_net/tests/test_squeezenet1_1_extract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import paddle
import os
import json
from paddle.vision.models import squeezenet1_1


def extract_squeezenet():
# 1. Environment setup
os.environ["FLAGS_enable_pir_api"] = "1"
paddle.enable_static()

# 2. Capture computation graph
main_program = paddle.static.Program()
with paddle.static.program_guard(main_program):
inputs = paddle.static.data(
name="image", shape=[1, 3, 224, 224], dtype="float32"
)
model = squeezenet1_1(pretrained=False)
model(inputs)

# 3. Get absolute save path
current_file_path = os.path.abspath(__file__)
project_root = os.path.dirname(os.path.dirname(os.path.dirname(current_file_path)))
target_dir = os.path.join(project_root, "paddle_samples/PaddleX/squeezenet1_1")
save_path = os.path.join(target_dir, "graph_net.json")

# 4. Standardize format: Convert program to a valid JSON string
# We wrap it in a dictionary to ensure valid JSON structure with double quotes
program_content = str(main_program)
json_data = {"protocol": "PIR", "program_desc": program_content}

# 5. Write to file using json.dump (this enforces double quotes for keys)
os.makedirs(target_dir, exist_ok=True)
with open(save_path, "w") as f:
json.dump(json_data, f, indent=4)

print(f"Extraction successful! Standard JSON generated at: {save_path}")


if __name__ == "__main__":
extract_squeezenet()
Empty file.
1 change: 1 addition & 0 deletions paddle_samples/PaddleX/squeezenet1_1/graph_hash.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a87bc38868b688b562b67634fb432eab7051fff838290f80ec278d02c9391a80
4 changes: 4 additions & 0 deletions paddle_samples/PaddleX/squeezenet1_1/graph_net.json

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions paddle_samples/PaddleX/squeezenet1_1/input_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Input metadata for SqueezeNet 1.1


# This follows the style of existing samples in the repo
class Program_input_tensor_image:
name = "image"
shape = [1, 3, 224, 224]
dtype = "float32"
data = None


# The validation script needs this dict to match with model.forward(**state_dict)
input_meta = {"image": {"shape": [1, 3, 224, 224], "dtype": "float32"}}
28 changes: 28 additions & 0 deletions paddle_samples/PaddleX/squeezenet1_1/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import paddle
from paddle.vision.models import squeezenet1_1


class GraphModule(paddle.nn.Layer):
def __init__(self):
super().__init__()
self.model = squeezenet1_1(pretrained=False)

def forward(self, *args, **kwargs):
# 1. Try to get tensor from positional arguments
if args:
x = args[0]
# 2. Try to get tensor from keyword arguments
else:
x = None
for val in kwargs.values():
if isinstance(val, (paddle.Tensor, paddle.static.Variable)):
x = val
break

if x is None:
# Log keys for debugging in CI if it fails again
raise ValueError(
f"No input tensor found. args_len: {len(args)}, kwargs_keys: {list(kwargs.keys())}"
)

return self.model(x)
2 changes: 2 additions & 0 deletions paddle_samples/PaddleX/squeezenet1_1/weight_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Weight metadata for SqueezeNet 1.1
weight_meta = {}