Skip to content

Instantly share code, notes, and snippets.

@real-jiakai
Last active November 10, 2023 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save real-jiakai/8cbde49bc949bacb1f15d0d82e3e5d11 to your computer and use it in GitHub Desktop.
Save real-jiakai/8cbde49bc949bacb1f15d0d82e3e5d11 to your computer and use it in GitHub Desktop.
监督学习笔记。

1、FP16、BF16是什么?

image

2、使用预训练的ResNet-50模型,基于ImageNet数据集进行分类。

# 导入必要的库
import torch
import torchvision.models as models
import torchvision.transforms as transforms
from PIL import Image

# 加载预训练模型
# 初始化ResNet-50模型。'DEFAULT'值使模型使用预训练的权重。
model = models.resnet50(weights='DEFAULT')

# 将模型设置为评估模式。在评估模式下,模型的行为(例如,关闭批处理规范化和丢弃层的训练行为)会有所不同,
# 以确保模型在推理时的准确性和可靠性。
model.eval()

# 定义图像预处理流程
# 这个流程包括:调整图像大小、中心裁剪、将图像转换为张量、以及正则化。
transform = transforms.Compose([
    transforms.Resize(256),          # 调整图像大小为256x256
    transforms.CenterCrop(224),      # 中心裁剪图像为224x224
    transforms.ToTensor(),           # 将图像转换为张量
    transforms.Normalize(            # 正则化张量
        mean=[0.485, 0.456, 0.406],  # 使用Imagenet数据集的均值
        std=[0.229, 0.224, 0.225]    # 使用Imagenet数据集的标准差
    ),
])

# 定义图像类别预测函数
def predict_image_class(image_path):
    # 打开图像
    image = Image.open(image_path)
    # 对图像应用预处理流程
    image = transform(image).unsqueeze(0)
    # 不追踪梯度,以减少内存使用
    with torch.no_grad():
        # 获取模型输出
        outputs = model(image)
        # 获取预测类别的索引
        _, predicted = outputs.max(1)
    # 返回预测类别的索引
    return predicted.item()

# 测试图像类别预测函数
image_path = 'assets/test.jpg'  # 替换为您的图片路径
class_id = predict_image_class(image_path)
print(f"The predicted class ID for the image is: {class_id}")

3、langchain是什么?

image

4、jsonl文件是啥?

image

5、openai的Legacy Completions API是啥?

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment