Mac上安装DeepSeek后的迁移学习指南

云信安装大师
90
AI 质量分
2 5 月, 2025
4 分钟阅读
0 阅读

Mac上安装DeepSeek后的迁移学习指南

引言

DeepSeek是一个强大的深度学习框架,特别适合进行迁移学习任务。本文将指导你在Mac上安装DeepSeek,并完成一个完整的迁移学习项目。通过本教程,你将学会如何利用预训练模型快速实现自己的AI应用。

准备工作

系统要求

  • macOS 10.15 (Catalina) 或更高版本
  • Python 3.8+
  • 建议使用M1/M2芯片的Mac以获得更好的性能

推荐环境

  • 使用conda或venv创建虚拟环境
  • 建议16GB以上内存(处理大型模型时)

第一步:安装DeepSeek

1. 创建虚拟环境(推荐)

代码片段
# 使用conda创建环境(如果没有conda可以跳过)
conda create -n deepseek_env python=3.8
conda activate deepseek_env

# 或者使用venv
python -m venv deepseek_env
source deepseek_env/bin/activate

2. 安装DeepSeek

代码片段
pip install deepseek --upgrade

注意:如果遇到权限问题,可以尝试添加 --user 参数

3. 验证安装

代码片段
import deepseek
print(deepseek.__version__)

第二步:准备迁移学习环境

1. 安装必要依赖

代码片段
pip install numpy pandas matplotlib torch torchvision pillow tqdm

2. GPU加速支持(M1/M2芯片)

如果你的Mac配备M1/M2芯片,可以启用Metal加速:

代码片段
pip install torch torchvision --extra-index-url https://download.pytorch.org/whl/nightly/cpu

第三步:迁移学习实战 – 图像分类示例

我们将使用预训练的ResNet模型在自定义数据集上进行微调。

1. 准备数据集结构

代码片段
my_dataset/
├── train/
│   ├── class1/
│   │   ├── img1.jpg
│   │   └── img2.jpg
│   └── class2/
│       ├── img1.jpg
│       └── img2.jpg
└── val/
    ├── class1/
    └── class2/

2. Python实现代码

代码片段
import os
import torch
import deepseek as ds
from torchvision import transforms, datasets, models
from torch.utils.data import DataLoader

# Step 1: 数据预处理和加载
def load_data(data_dir='./my_dataset', batch_size=32):
    # Define transformations for training and validation sets
    data_transforms = {
        'train': transforms.Compose([
            transforms.RandomResizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
        'val': transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
    }

    # Load datasets with ImageFolder and apply transformations
    image_datasets = {
        x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x])
        for x in ['train', 'val']
    }

    # Create dataloaders for training and validation sets
    dataloaders = {
        x: DataLoader(image_datasets[x], batch_size=batch_size, shuffle=True)
        for x in ['train', 'val']
    }

    return dataloaders, image_datasets['train'].classes

# Step 2: Initialize DeepSeek model with pretrained weights from TorchVision ResNet50 model.
def initialize_model(num_classes):
    model = models.resnet50(pretrained=True)

    # Freeze all layers except the final layer.
    for param in model.parameters():
        param.requires_grad = False

    # Replace the last fully connected layer.
    num_ftrs = model.fc.in_features

    # Use DeepSeek's optimized classifier.
    model.fc = ds.nn.SmartClassifier(num_ftrs, num_classes)

    return model

# Step3: Train the model using DeepSeek's trainer.
def train_model(model, dataloaders, criterion=None, optimizer=None,
                num_epochs=25):

    if criterion is None:
        criterion = torch.nn.CrossEntropyLoss()

    if optimizer is None:
        optimizer = torch.optim.SGD(model.fc.parameters(), lr=0.001, momentum=0.9)

    # Initialize DeepSeek trainer.
    trainer = ds.train.Trainer(
        model=model,
        train_loader=dataloaders['train'],
        val_loader=dataloaders['val'],
        criterion=criterion,
        optimizer=optimizer,
        device='mps' if torch.backends.mps.is_available() else 'cpu'
    )

    # Start training with progress tracking.
    stats = trainer.fit(num_epochs=num_epochs)

    return model, stats


if __name__ == '__main__':

     print("Loading data...")
     dataloaders ,class_names=load_data()

     print(f"Found {len(class_names)} classes: {class_names}")

     print("Initializing model...")
     model=initialize_model(len(class_names))

     print("Training...")
     trained_model ,stats=train_model(model,dataloaders,num_epochs=10)

     print("Training complete!")

     # Save the trained weights only (recommended).
     torch.save(trained_model.state_dict(),'transfer_learned_model.pth')

第四步:模型评估与预测

训练完成后,我们可以评估模型性能并进行预测:

代码片段
def evaluate_model(model,dataloader):

      import numpy as np

      was_training=False

      if hasattr(model,'training'):
          was_training=model.training

      model.eval()

      corrects=[]

      with torch.no_grad():

          for inputs ,labels in dataloader:

              outputs=model(inputs.to('mps' if torch.backends.mps.is_available() else 'cpu'))

              _,preds=torch.max(outputs.data.cpu(),dim=-1)

              correct=(preds==labels).numpy()

              corrects.extend(correct.tolist())

      accuracy=np.mean(corrects)*100.

      print(f"Accuracy: {accuracy:.2f}%")

      if was_training:
          model.train()

      return accuracy


def predict_image(image_path):

      from PIL import Image

      transform_val=get_transforms()['val']

      image_tensor=transform_val(Image.open(image_path)).unsqueeze_(dim=0)

      with torch.no_grad():

          output=trained_model(image_tensor.to('mps' if torch.backends.mps.is_available() else 'cpu'))

          _,pred_idx=torch.max(output,dim=-1) 

          predicted_class_name=[class_names[idx] for idx in pred_idx.cpu().numpy()]

          return predicted_class_name[0]

第五步:优化技巧

为了提高迁移学习效果,可以尝试以下方法:

渐进式解冻 -逐步解冻更多层进行微调:

代码片段
def unfreeze_layers(model,num_layers_to_unfreeze):

       total_layers=list(model.children())

       for layer in total_layers[-num_layers_to_unfreeze:]:

           for param in layer .parameters():
                param .requires_grad=True 

       return None 

unfreeze_layers(trained_model ,3) # Unfreeze last three layers 

学习率调度器 -动态调整学习率:

代码片段
from torch .optim import lr_scheduler 

exp_lr_scheduler=scheduler.StepLR(optimizer,
                                 step_size=7,
                                 gamma=.1)

trainer.set_scheduler(exp_lr_scheduler)                                 

常见问题解决

问题1: RuntimeError: PyTorch no longer supports this macOS version

解决方案:

代码片段
conda install pytorch::pytorch torchvision::torchvision -c pytorch-nightly --force-reinstall --no-deps --yes || pip install --pre --force-reinstall "torch>=2" "torchvision>=0"

问题2: Out of memory when loading large models

解决方案:

代码片段
# Reduce batch size (e.g., from32to16or8)

batch_size=16  

dataloader_kwargs={'num_workers':4,'pin_memory':True}  

train_dl=get_dataloader(train_ds,batch_size=batch_size,**dataloader_kwargs)  

val_dl=get_dataloader(val_ds,batch_size=batch_size,**dataloader_kwargs)  

总结

通过本教程,我们完成了以下工作:

✔️在Mac上正确安装了DeepSeek框架
✔️配置了适合迁移学习的Python环境
✔️实现了完整的图像分类迁移学习流程
✔️学习了优化技巧和常见问题解决方法

迁移学习的优势在于能够利用预训练模型的强大特征提取能力,即使在小数据集上也能获得良好表现。DeepSeek提供的工具进一步简化了这一过程。

原创 高质量