现在统一使用Torch中的interpolate这个缩放方法,不用torchvision中的transforms的Resize了,应该就可以了。
This commit is contained in:
+14
-4
@@ -54,7 +54,7 @@ class FoodCNN(nn.Module):
|
|||||||
|
|
||||||
def preprocess_image(self, image):
|
def preprocess_image(self, image):
|
||||||
"""
|
"""
|
||||||
预处理单张图片
|
预处理单张图片(使用与移动端相同的插值方法)
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
image: PIL Image 或 numpy array
|
image: PIL Image 或 numpy array
|
||||||
@@ -69,10 +69,20 @@ class FoodCNN(nn.Module):
|
|||||||
else:
|
else:
|
||||||
raise ValueError("输入必须是PIL Image或numpy array")
|
raise ValueError("输入必须是PIL Image或numpy array")
|
||||||
|
|
||||||
# 应用预处理变换
|
# 转换为tensor(不做resize)
|
||||||
processed = self.preprocess(image)
|
tensor = transforms.ToTensor()(image)
|
||||||
# 添加batch维度
|
# 添加batch维度
|
||||||
return processed.unsqueeze(0)
|
tensor = tensor.unsqueeze(0)
|
||||||
|
|
||||||
|
# 使用与移动端相同的插值方法缩放到32x32
|
||||||
|
tensor = F.interpolate(tensor, size=(32, 32), mode='bilinear', align_corners=False)
|
||||||
|
|
||||||
|
# ImageNet标准化
|
||||||
|
mean = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1)
|
||||||
|
std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1)
|
||||||
|
tensor = (tensor - mean) / std
|
||||||
|
|
||||||
|
return tensor
|
||||||
|
|
||||||
def mobile_preprocess(self, x):
|
def mobile_preprocess(self, x):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ VAL_DATA_DIR = os.path.join(DATASET_DIR, 'val')
|
|||||||
TEST_DATA_DIR = os.path.join(DATASET_DIR, 'test')
|
TEST_DATA_DIR = os.path.join(DATASET_DIR, 'test')
|
||||||
|
|
||||||
# 模型保存路径
|
# 模型保存路径
|
||||||
MODEL_DIR = os.path.join(BASE_DIR, 'model', '09')
|
MODEL_DIR = os.path.join(BASE_DIR, 'model', '10')
|
||||||
BEST_MODEL_PATH = os.path.join(MODEL_DIR, 'best_food_model.pth')
|
BEST_MODEL_PATH = os.path.join(MODEL_DIR, 'best_food_model.pth')
|
||||||
TRAINING_CURVES_PATH = os.path.join(MODEL_DIR, 'training_curves.png')
|
TRAINING_CURVES_PATH = os.path.join(MODEL_DIR, 'training_curves.png')
|
||||||
TRAINING_RESULTS_PATH = os.path.join(MODEL_DIR, 'training_results.txt')
|
TRAINING_RESULTS_PATH = os.path.join(MODEL_DIR, 'training_results.txt')
|
||||||
|
|||||||
Reference in New Issue
Block a user