From 06e440a6d1b30ae03a19d94e53a0030354e9cee0 Mon Sep 17 00:00:00 2001 From: zhanghuan <1262329256@qq.com> Date: Thu, 11 Sep 2025 14:41:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DtoAndroid.py,=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E9=80=92=E5=BD=92=E8=B0=83=E7=94=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- net/food_net.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/net/food_net.py b/net/food_net.py index cdafcdd..30bac5c 100644 --- a/net/food_net.py +++ b/net/food_net.py @@ -97,11 +97,16 @@ class FoodCNN(nn.Module): return x - def forward(self, x): - # 如果启用内部预处理且输入是tensor - if self.use_internal_preprocess and isinstance(x, torch.Tensor): - x = self.internal_preprocess(x) + def _forward_network(self, x): + """ + 网络的核心前向传播(不包含预处理) + Args: + x: 预处理后的tensor,形状为 [batch, 3, 32, 32] + + Returns: + torch.Tensor: 模型输出 + """ # 第一个卷积块 x = F.relu(self.conv1(x)) x = F.relu(self.conv2(x)) @@ -130,6 +135,13 @@ class FoodCNN(nn.Module): return x + def forward(self, x): + # 如果启用内部预处理且输入是tensor + if self.use_internal_preprocess and isinstance(x, torch.Tensor): + x = self.internal_preprocess(x) + + return self._forward_network(x) + def forward_mobile(self, x): """ 移动端前向传播(包含预处理) @@ -143,8 +155,8 @@ class FoodCNN(nn.Module): # 移动端预处理 x = self.mobile_preprocess(x) - # 标准前向传播 - return self.forward(x) + # 执行网络前向传播 + return self._forward_network(x) def create_food_cnn(use_internal_preprocess=False):