训练的时候缩放,推理的时候,在模型中缩放!
This commit is contained in:
@@ -29,20 +29,20 @@ print(f"使用设备: {device}")
|
||||
|
||||
|
||||
|
||||
# 数据预处理
|
||||
# 数据预处理 - 必须包含Resize以保证batch中tensor尺寸一致,只有Normalize由模型内部完成
|
||||
transform_train = transforms.Compose([
|
||||
transforms.Resize((32, 32)),
|
||||
transforms.Resize((32, 32)), # 必须保留,确保batch中tensor尺寸一致
|
||||
transforms.RandomHorizontalFlip(p=0.5),
|
||||
transforms.RandomRotation(10),
|
||||
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),
|
||||
transforms.ToTensor(),
|
||||
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
|
||||
# 注意:只有Normalize由模型内部处理
|
||||
])
|
||||
|
||||
transform_test = transforms.Compose([
|
||||
transforms.Resize((32, 32)),
|
||||
transforms.Resize((32, 32)), # 必须保留,确保batch中tensor尺寸一致
|
||||
transforms.ToTensor(),
|
||||
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
|
||||
# 注意:只有Normalize由模型内部处理
|
||||
])
|
||||
|
||||
# 训练函数
|
||||
@@ -153,8 +153,8 @@ if __name__ == '__main__':
|
||||
print(f"验证集大小: {len(val_dataset)}")
|
||||
print(f"测试集大小: {len(test_dataset)}")
|
||||
|
||||
# 创建模型
|
||||
model = create_food_cnn().to(device)
|
||||
# 创建模型 - 启用内部预处理
|
||||
model = create_food_cnn(use_internal_preprocess=True).to(device)
|
||||
print(f"模型参数数量: {sum(p.numel() for p in model.parameters() if p.requires_grad)}")
|
||||
|
||||
# 定义损失函数和优化器
|
||||
|
||||
Reference in New Issue
Block a user