ViT Vision Transformer超详细解析,网络构建,可视化,数据预处理,全流程实例教程

关于ViT的分析和教程,网上又虚又空的东西比较多,本文通过一个实例,将ViT全解析。

包括三部分内容,网络构建;orchview.draw_graph 将网络每一层的结构与输入输出可视化;数据预处理。附完整代码

网络构建

创建一个model.py,其中实现ViT网络构建

import torch.nn as nn
import torch
import torch.optim as optim
import torch.nn.functional as F
import lightning as Lclass AttentionBlock(nn.Module):def __init__(self, embed_dim, hidden_dim, num_heads, dropout=0.0):"""Inputs:embed_dim - Dimensionality of input and attention feature vectorshidden_dim - Dimensionality of hidden layer in feed-forward network(usually 2-4x larger than embed_dim)num_heads - Number of heads to use in the Multi-Head Attention blockdropout - Amount of dropout to apply in the feed-forward network"""super().__init__()self.layer_norm_1 = nn.LayerNorm(embed_dim)self.attn = nn.MultiheadAttention(embed_dim, num_heads)self.layer_norm_2 = nn.LayerNorm(embed_dim)self.linear = nn.Sequential(nn.Linear(embed_dim, hidden_dim),nn.GELU(),nn.Dropout(dropout),nn.Linear(hidden_dim, embed_dim),nn.Dropout(dropout),)def forward(self, x):inp_x = self.layer_norm_1(x)x = x + self.attn(inp_x, inp_x, inp_x)[0]x = x + self.linear(self.layer_norm_2(x))return xclass VisionTransformer(nn.Module):def __init__(self,embed_dim,hidden_dim,num_channels,num_heads,num_layers,num_classes,patch_size,num_patches,dropout=0.0,):"""Inputs:embed_dim - Dimensionality of the input feature vectors to the Transformerhidden_dim - Dimensionality of the hidden layer in the feed-forward networkswithin the Transformernum_channels - Number of channels of the input (3 for RGB)num_heads - Number of heads to use in the Multi-Head Attention blocknum_layers - Number of layers to use in the Transformernum_classes - Number of classes to predictpatch_size - Number of pixels that the patches have per dimensionnum_patches - Maximum number of patches an image can havedropout - Amount of dropout to apply in the feed-forward network andon the input encoding"""super().__init__()self.patch_size = patch_size# Layers/Networksself.input_layer = nn.Linear(num_channels * (patch_size**2), embed_dim)self.transformer = nn.Sequential(*(AttentionBlock(embed_dim, hidden_dim, num_heads, dropout=dropout) for _ in range(num_layers)))self.mlp_head = nn.Sequential(nn.LayerNorm(embed_dim), nn.Linear(embed_dim, num_classes))self.dropout = nn.Dropout(dropout)# Parameters/Embeddingsself.cls_token = nn.Parameter(torch.randn(1, 1, embed_dim))self.pos_embedding = nn.Parameter(torch.randn(1, 1 + num_patches, embed_dim))def img_to_patch(self, x, patch_size, flatten_channels=True):"""Inputs:x - Tensor representing the image of shape [B, C, H, W]patch_size - Number of pixels per dimension of the patches (integer)flatten_channels - If True, the patches will be returned in a flattened formatas a feature vector instead of a image grid."""B, C, H, W = x.shapex = x.reshape(B, C, H // patch_size, patch_size, W // patch_size, patch_size)x = x.permute(0, 2, 4, 1, 3, 5)  # [B, H', W', C, p_H, p_W]x = x.flatten(1, 2)  # [B, H'*W', C, p_H, p_W]if flatten_channels:x = x.flatten(2, 4)  # [B, H'*W', C*p_H*p_W]return xdef forward(self, x):# Preprocess inputx = self.img_to_patch(x, self.patch_size)B, T, _ = x.shapex = self.input_layer(x)# Add CLS token and positional encodingcls_token = self.cls_token.repeat(B, 1, 1)x = torch.cat([cls_token, x], dim=1)x = x + self.pos_embedding[:, : T + 1]# Apply Transforrmerx = self.dropout(x)x = x.transpose(0, 1)x = self.transformer(x)# Perform classification predictioncls = x[0]out = self.mlp_head(cls)return outclass ViT(L.LightningModule):def __init__(self, model_kwargs, lr):super().__init__()self.save_hyperparameters()self.model = VisionTransformer(**model_kwargs)def forward(self, x):return self.model(x)def configure_optimizers(self):optimizer = optim.AdamW(self.parameters(), lr=self.hparams.lr)lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[100, 150], gamma=0.1)return [optimizer], [lr_scheduler]def _calculate_loss(self, batch, mode="train"):imgs, labels = batchpreds = self.model(imgs)loss = F.cross_entropy(preds, labels)acc = (preds.argmax(dim=-1) == labels).float().mean()self.log("%s_loss" % mode, loss)self.log("%s_acc" % mode, acc)return lossdef training_step(self, batch, batch_idx):loss = self._calculate_loss(batch, mode="train")return lossdef validation_step(self, batch, batch_idx):self._calculate_loss(batch, mode="val")def test_step(self, batch, batch_idx):self._calculate_loss(batch, mode="test")

在其他文件中引入model.py,实现网络搭建

from model import ViTmodel = ViT(model_kwargs={"embed_dim": 256,"hidden_dim": 512,"num_heads": 8,"num_layers": 6,"patch_size": 4,"num_channels": 3,"num_patches": 64,"num_classes": 10,"dropout": 0.2,},lr=3e-4,)

也可以下载预训练的模型

# Files to download
base_url = "https://raw.githubusercontent.com/phlippe/saved_models/main/"
CHECKPOINT_PATH = os.environ.get("PATH_CHECKPOINT", "saved_models/VisionTransformers/")
pretrained_files = ["tutorial15/ViT.ckpt","tutorial15/tensorboards/ViT/events.out.tfevents.ViT","tutorial5/tensorboards/ResNet/events.out.tfevents.resnet",
]
# Create checkpoint path if it doesn't exist yet
os.makedirs(CHECKPOINT_PATH, exist_ok=True)# For each file, check whether it already exists. If not, try downloading it.
for file_name in pretrained_files:file_path = os.path.join(CHECKPOINT_PATH, file_name.split("/", 1)[1])if "/" in file_name.split("/", 1)[1]:os.makedirs(file_path.rsplit("/", 1)[0], exist_ok=True)if not os.path.isfile(file_path):file_url = base_url + file_nameprint("Downloading %s..." % file_url)try:urllib.request.urlretrieve(file_url, file_path)except HTTPError as e:print("Something went wrong. Please try to download the file from the GDrive folder, or contact the author with the full output including the following error:\n",e,)pretrained_filename = os.path.join(CHECKPOINT_PATH, "ViT.ckpt")
model = ViT.load_from_checkpoint(pretrained_filename)

torchview.draw_graph 网络可视化

model_graph = draw_graph(model, input_size=(1, 3, 16, 16))
model_graph.resize_graph(scale=5.0)
model_graph.visual_graph.render(format='svg')

运行这段代码,会生成一个svg格式的图片,显示网络结构和每一层的输入输出

训练数据准备

新建一个prepare_data.py

import os
import json
import torch
from PIL import Image
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transformsclass CustomDataset(Dataset):def __init__(self, image_dir, names, labels, transform=None):self.image_dir = image_dirself.names = namesself.labels = labelsself.transform = transformdef __len__(self):return len(self.labels)def __getitem__(self, idx):name_ = self.names[idx]img_name = os.path.join(self.image_dir, name_)image = Image.open(img_name)if self.transform:image = self.transform(image)label = self.labels[idx]return image, labeldef load_img_ann(ann_path):"""return [{img_name, [ (x, y, h, w, label), ... ]}]"""with open(ann_path) as fp:root = json.load(fp)img_dict = {}img_label_dict = {}for img_info in root['images']:img_id = img_info['id']img_name = img_info['file_name']img_dict[img_id] = {'name': img_name}for ann_info in root['annotations']:img_id = ann_info['image_id']img_category_id = ann_info['category_id']img_name = img_dict[img_id]['name']img_label_dict[img_id] = {'name': img_name, 'category_id': img_category_id}return img_label_dictdef get_dataloader():annota_dir = '/home/robotics/Downloads/coco_dataset/annotations/instances_val2017.json'img_dir = "/home/robotics/Downloads/coco_dataset/val2017"img_dict = load_img_ann(annota_dir)values = list(img_dict.values())img_names = []labels = []for item in values:category_id = item['category_id']labels.append(category_id)img_name = item['name']img_names.append(img_name)# 检查剔除黑白的图片img_names_rgb = []labels_rgb = []for i in range(len(img_names)):# 检查文件扩展名,确保它是图片文件(可以根据需要扩展支持的文件类型)file_path = os.path.join(img_dir, img_names[i])# 打开图片文件img = Image.open(file_path)# 获取通道数num_channels = img.modeif num_channels == "RGB" and labels[i] < 10:img_names_rgb.append(img_names[i])labels_rgb.append(labels[i])# 定义一系列图像转换操作transform = transforms.Compose([transforms.Resize((16, 16)),  # 调整图像大小transforms.RandomHorizontalFlip(),  # 随机水平翻转transforms.ToTensor(),  # 将图像转换为张量transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])  # 标准化图像])# 假设 image_dir 是包含所有图像文件的文件夹路径,labels 是标签列表train_set = CustomDataset(img_dir, img_names_rgb[-500:], labels_rgb[-500:], transform=transform)val_set = CustomDataset(img_dir, img_names_rgb[-500:-100], labels_rgb[-500:-100], transform=transform)test_set = CustomDataset(img_dir, img_names_rgb[-100:], labels_rgb[-100:], transform=transform)# 创建一个 DataLoadertrain_loader = DataLoader(train_set, batch_size=32, shuffle=True, drop_last=False)val_loader = DataLoader(val_set, batch_size=32, shuffle=True, drop_last=False, num_workers=4)test_loader = DataLoader(test_set, batch_size=32, shuffle=True, drop_last=False, num_workers=4)return train_loader, val_loader, test_loaderif __name__ == "__main__":train_loader, val_loader, test_loader = get_dataloader()for batch in train_loader:print(batch)

解释一下上面的代码:

这里使用的是coco数据集的2017,可以在官网自行下载,下载下来以后,annotations包含如下内容

这里我们使用的是 instances_val2017.json,如果是正经做训练,应该用train2017,但是train2017文件太大了,处理起来速度很慢,本文仅为说明,不追求训练效果,所以使用val2017进行说明,instances就是用于图像识别的annotation,里面包括了每张图片的label和box,本文创建的ViT 不输出box,仅输出类别。函数

def load_img_ann(ann_path):

是为了将图片的id(每张图片的唯一主键),name和category_id(属于哪一个类别,也就是label)关联起来。

        # 获取通道数num_channels = img.modeif num_channels == "RGB" and labels[i] < 10:img_names_rgb.append(img_names[i])labels_rgb.append(labels[i])

注意coco数据集有单通道的黑白图片,要剔除,因为本文的ViT比较简单,输出只能10个类别,所以预处理图片的时候,只选择10个类别。

定义操作变换

    # 定义一系列图像转换操作transform = transforms.Compose([transforms.Resize((16, 16)),  # 调整图像大小transforms.RandomHorizontalFlip(),  # 随机水平翻转transforms.ToTensor(),  # 将图像转换为张量transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])  # 标准化图像])

创建一个自己的Dataset类,继承自 torch.utils.data.Dataset

class CustomDataset(Dataset):def __init__(self, image_dir, names, labels, transform=None):self.image_dir = image_dirself.names = namesself.labels = labelsself.transform = transformdef __len__(self):return len(self.labels)def __getitem__(self, idx):name_ = self.names[idx]img_name = os.path.join(self.image_dir, name_)image = Image.open(img_name)if self.transform:image = self.transform(image)label = self.labels[idx]return image, label

先创建Dataset,再创建dataloader,从Dataset取minibatch。

    # 假设 image_dir 是包含所有图像文件的文件夹路径,labels 是标签列表train_set = CustomDataset(img_dir, img_names_rgb[-500:], labels_rgb[-500:], transform=transform)val_set = CustomDataset(img_dir, img_names_rgb[-500:-100], labels_rgb[-500:-100], transform=transform)test_set = CustomDataset(img_dir, img_names_rgb[-100:], labels_rgb[-100:], transform=transform)# 创建一个 DataLoadertrain_loader = DataLoader(train_set, batch_size=32, shuffle=True, drop_last=False)val_loader = DataLoader(val_set, batch_size=32, shuffle=True, drop_last=False, num_workers=4)test_loader = DataLoader(test_set, batch_size=32, shuffle=True, drop_last=False, num_workers=4)

以上,数据准备工作完成,对模型进行训练

    trainer = L.Trainer(default_root_dir=os.path.join(CHECKPOINT_PATH, "ViT"),accelerator="auto",devices=1,max_epochs=180,callbacks=[ModelCheckpoint(save_weights_only=True, mode="max", monitor="val_acc"),LearningRateMonitor("epoch"),],)trainer.logger._log_graph = True  # If True, we plot the computation graph in tensorboardtrainer.logger._default_hp_metric = None  # Optional logging argument that we don't needtrainer.fit(model, train_loader, val_loader)# Load best checkpoint after trainingmodel = ViT.load_from_checkpoint(trainer.checkpoint_callback.best_model_path)# Test best model on validation and test setval_result = trainer.test(model, dataloaders=val_loader, verbose=False)test_result = trainer.test(model, dataloaders=test_loader, verbose=False)result = {"test": test_result[0]["test_acc"], "val": val_result[0]["test_acc"]}

完整代码:

一共包括三个文件:model.py 搭建网络的功能, prepare_data.py 数据预处理工作, main.py 网络训练

model.py内容:

import torch.nn as nn
import torch
import torch.optim as optim
import torch.nn.functional as F
import lightning as Lclass AttentionBlock(nn.Module):def __init__(self, embed_dim, hidden_dim, num_heads, dropout=0.0):"""Inputs:embed_dim - Dimensionality of input and attention feature vectorshidden_dim - Dimensionality of hidden layer in feed-forward network(usually 2-4x larger than embed_dim)num_heads - Number of heads to use in the Multi-Head Attention blockdropout - Amount of dropout to apply in the feed-forward network"""super().__init__()self.layer_norm_1 = nn.LayerNorm(embed_dim)self.attn = nn.MultiheadAttention(embed_dim, num_heads)self.layer_norm_2 = nn.LayerNorm(embed_dim)self.linear = nn.Sequential(nn.Linear(embed_dim, hidden_dim),nn.GELU(),nn.Dropout(dropout),nn.Linear(hidden_dim, embed_dim),nn.Dropout(dropout),)def forward(self, x):inp_x = self.layer_norm_1(x)x = x + self.attn(inp_x, inp_x, inp_x)[0]x = x + self.linear(self.layer_norm_2(x))return xclass VisionTransformer(nn.Module):def __init__(self,embed_dim,hidden_dim,num_channels,num_heads,num_layers,num_classes,patch_size,num_patches,dropout=0.0,):"""Inputs:embed_dim - Dimensionality of the input feature vectors to the Transformerhidden_dim - Dimensionality of the hidden layer in the feed-forward networkswithin the Transformernum_channels - Number of channels of the input (3 for RGB)num_heads - Number of heads to use in the Multi-Head Attention blocknum_layers - Number of layers to use in the Transformernum_classes - Number of classes to predictpatch_size - Number of pixels that the patches have per dimensionnum_patches - Maximum number of patches an image can havedropout - Amount of dropout to apply in the feed-forward network andon the input encoding"""super().__init__()self.patch_size = patch_size# Layers/Networksself.input_layer = nn.Linear(num_channels * (patch_size**2), embed_dim)self.transformer = nn.Sequential(*(AttentionBlock(embed_dim, hidden_dim, num_heads, dropout=dropout) for _ in range(num_layers)))self.mlp_head = nn.Sequential(nn.LayerNorm(embed_dim), nn.Linear(embed_dim, num_classes))self.dropout = nn.Dropout(dropout)# Parameters/Embeddingsself.cls_token = nn.Parameter(torch.randn(1, 1, embed_dim))self.pos_embedding = nn.Parameter(torch.randn(1, 1 + num_patches, embed_dim))def img_to_patch(self, x, patch_size, flatten_channels=True):"""Inputs:x - Tensor representing the image of shape [B, C, H, W]patch_size - Number of pixels per dimension of the patches (integer)flatten_channels - If True, the patches will be returned in a flattened formatas a feature vector instead of a image grid."""B, C, H, W = x.shapex = x.reshape(B, C, H // patch_size, patch_size, W // patch_size, patch_size)x = x.permute(0, 2, 4, 1, 3, 5)  # [B, H', W', C, p_H, p_W]x = x.flatten(1, 2)  # [B, H'*W', C, p_H, p_W]if flatten_channels:x = x.flatten(2, 4)  # [B, H'*W', C*p_H*p_W]return xdef forward(self, x):# Preprocess inputx = self.img_to_patch(x, self.patch_size)B, T, _ = x.shapex = self.input_layer(x)# Add CLS token and positional encodingcls_token = self.cls_token.repeat(B, 1, 1)x = torch.cat([cls_token, x], dim=1)x = x + self.pos_embedding[:, : T + 1]# Apply Transforrmerx = self.dropout(x)x = x.transpose(0, 1)x = self.transformer(x)# Perform classification predictioncls = x[0]out = self.mlp_head(cls)return outclass ViT(L.LightningModule):def __init__(self, model_kwargs, lr):super().__init__()self.save_hyperparameters()self.model = VisionTransformer(**model_kwargs)def forward(self, x):return self.model(x)def configure_optimizers(self):optimizer = optim.AdamW(self.parameters(), lr=self.hparams.lr)lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[100, 150], gamma=0.1)return [optimizer], [lr_scheduler]def _calculate_loss(self, batch, mode="train"):imgs, labels = batchpreds = self.model(imgs)loss = F.cross_entropy(preds, labels)acc = (preds.argmax(dim=-1) == labels).float().mean()self.log("%s_loss" % mode, loss)self.log("%s_acc" % mode, acc)return lossdef training_step(self, batch, batch_idx):loss = self._calculate_loss(batch, mode="train")return lossdef validation_step(self, batch, batch_idx):self._calculate_loss(batch, mode="val")def test_step(self, batch, batch_idx):self._calculate_loss(batch, mode="test")

 prepare_data.py内容:

import os
import json
import torch
from PIL import Image
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transformsclass CustomDataset(Dataset):def __init__(self, image_dir, names, labels, transform=None):self.image_dir = image_dirself.names = namesself.labels = labelsself.transform = transformdef __len__(self):return len(self.labels)def __getitem__(self, idx):name_ = self.names[idx]img_name = os.path.join(self.image_dir, name_)image = Image.open(img_name)if self.transform:image = self.transform(image)label = self.labels[idx]return image, labeldef load_img_ann(ann_path):"""return [{img_name, [ (x, y, h, w, label), ... ]}]"""with open(ann_path) as fp:root = json.load(fp)img_dict = {}img_label_dict = {}for img_info in root['images']:img_id = img_info['id']img_name = img_info['file_name']img_dict[img_id] = {'name': img_name}for ann_info in root['annotations']:img_id = ann_info['image_id']img_category_id = ann_info['category_id']img_name = img_dict[img_id]['name']img_label_dict[img_id] = {'name': img_name, 'category_id': img_category_id}return img_label_dictdef get_dataloader():annota_dir = '/home/robotics/Downloads/coco_dataset/annotations/instances_val2017.json'img_dir = "/home/robotics/Downloads/coco_dataset/val2017"img_dict = load_img_ann(annota_dir)values = list(img_dict.values())img_names = []labels = []for item in values:category_id = item['category_id']labels.append(category_id)img_name = item['name']img_names.append(img_name)# 检查剔除黑白的图片img_names_rgb = []labels_rgb = []for i in range(len(img_names)):# 检查文件扩展名,确保它是图片文件(可以根据需要扩展支持的文件类型)file_path = os.path.join(img_dir, img_names[i])# 打开图片文件img = Image.open(file_path)# 获取通道数num_channels = img.modeif num_channels == "RGB" and labels[i] < 10:img_names_rgb.append(img_names[i])labels_rgb.append(labels[i])# 定义一系列图像转换操作transform = transforms.Compose([transforms.Resize((16, 16)),  # 调整图像大小transforms.RandomHorizontalFlip(),  # 随机水平翻转transforms.ToTensor(),  # 将图像转换为张量transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])  # 标准化图像])# 假设 image_dir 是包含所有图像文件的文件夹路径,labels 是标签列表train_set = CustomDataset(img_dir, img_names_rgb[-500:], labels_rgb[-500:], transform=transform)val_set = CustomDataset(img_dir, img_names_rgb[-500:-100], labels_rgb[-500:-100], transform=transform)test_set = CustomDataset(img_dir, img_names_rgb[-100:], labels_rgb[-100:], transform=transform)# 创建一个 DataLoadertrain_loader = DataLoader(train_set, batch_size=32, shuffle=True, drop_last=False)val_loader = DataLoader(val_set, batch_size=32, shuffle=True, drop_last=False, num_workers=4)test_loader = DataLoader(test_set, batch_size=32, shuffle=True, drop_last=False, num_workers=4)return train_loader, val_loader, test_loaderif __name__ == "__main__":train_loader, val_loader, test_loader = get_dataloader()for batch in train_loader:print(batch)

main.py内容:

import os
os.environ['CUDA_LAUNCH_BLOCKING'] = '1' # 下面老是报错 shape 不一致
import urllib.request
from urllib.error import HTTPError
import lightning as L
from model import ViT
from torchview import draw_graph
from lightning.pytorch.callbacks import LearningRateMonitor, ModelCheckpointfrom prepare_data import get_dataloader# 加载模型
# Files to download
base_url = "https://raw.githubusercontent.com/phlippe/saved_models/main/"
CHECKPOINT_PATH = os.environ.get("PATH_CHECKPOINT", "saved_models/VisionTransformers/")
pretrained_files = ["tutorial15/ViT.ckpt","tutorial15/tensorboards/ViT/events.out.tfevents.ViT","tutorial5/tensorboards/ResNet/events.out.tfevents.resnet",
]
# Create checkpoint path if it doesn't exist yet
os.makedirs(CHECKPOINT_PATH, exist_ok=True)# For each file, check whether it already exists. If not, try downloading it.
for file_name in pretrained_files:file_path = os.path.join(CHECKPOINT_PATH, file_name.split("/", 1)[1])if "/" in file_name.split("/", 1)[1]:os.makedirs(file_path.rsplit("/", 1)[0], exist_ok=True)if not os.path.isfile(file_path):file_url = base_url + file_nameprint("Downloading %s..." % file_url)try:urllib.request.urlretrieve(file_url, file_path)except HTTPError as e:print("Something went wrong. Please try to download the file from the GDrive folder, or contact the author with the full output including the following error:\n",e,)pretrained_filename = os.path.join(CHECKPOINT_PATH, "ViT.ckpt")
needTrain = False
if not os.path.isfile(pretrained_filename):print("Found pretrained model at %s, loading..." % pretrained_filename)# Automatically loads the model with the saved hyperparametersmodel = ViT.load_from_checkpoint(pretrained_filename)
else:L.seed_everything(42)  # To be reproducablemodel = ViT(model_kwargs={"embed_dim": 256,"hidden_dim": 512,"num_heads": 8,"num_layers": 6,"patch_size": 4,"num_channels": 3,"num_patches": 64,"num_classes": 10,"dropout": 0.2,},lr=3e-4,)needTrain = True# 网络结构可视化
model_graph = draw_graph(model, input_size=(1, 3, 16, 16))
model_graph.resize_graph(scale=5.0)
model_graph.visual_graph.render(format='svg')# 准备训练数据
train_loader, val_loader, test_loader = get_dataloader()if needTrain:trainer = L.Trainer(default_root_dir=os.path.join(CHECKPOINT_PATH, "ViT"),accelerator="auto",devices=1,max_epochs=180,callbacks=[ModelCheckpoint(save_weights_only=True, mode="max", monitor="val_acc"),LearningRateMonitor("epoch"),],)trainer.logger._log_graph = True  # If True, we plot the computation graph in tensorboardtrainer.logger._default_hp_metric = None  # Optional logging argument that we don't needtrainer.fit(model, train_loader, val_loader)# Load best checkpoint after trainingmodel = ViT.load_from_checkpoint(trainer.checkpoint_callback.best_model_path)# Test best model on validation and test setval_result = trainer.test(model, dataloaders=val_loader, verbose=False)test_result = trainer.test(model, dataloaders=test_loader, verbose=False)result = {"test": test_result[0]["test_acc"], "val": val_result[0]["test_acc"]}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/178652.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

偶数矩阵判断【C语言作业】

题目 若一个布尔矩阵所有行和所有列的和都是偶数&#xff0c;则称为偶数矩阵。请编写一个程序&#xff0c;判断一个布尔矩阵是否是偶数矩阵。 要求&#xff1a; &#xff08;1&#xff09;输入:首先输入一个正整数n(n<100),代表该矩阵的大小&#xff0c;接下来是n行n列的矩…

第5天:基础入门-资产架构amp;端口amp;应用amp;CDNamp;WAFamp;站库分离amp;负载均衡

第5天&#xff1a;基础入门-资产架构&端口&应用&CDN&WAF&站库分离&负载均衡 #知识点&#xff1a;1. 资产架构-端口&目录&插件接口&多站点&多应用 2. 番外安全-域名&服务器本身&服务厂商&管理人员 3. 考虑阻碍-站库分离&am…

c++-AVL树

文章目录 前言一、AVL树1、AVL树概念2、AVL树模拟实现3、AVL树的旋转操作3.1 左单旋3.2 左单旋代码实现3.3 右单旋3.4 右单旋代码实现。3.5 什么时候调用左单旋和右单旋3.6 左右双旋3.7 左右双旋代码实现3.8 右左双旋3.9 右左双旋代码实现3.10 什么时候调用左右双旋和右左双旋 …

Kafka - 监控工具 Kafka Eagle:实时洞察Kafka集群的利器

文章目录 引言Kafka Eagle简介Kafka Eagle的特点Kafka Eagle的优势使用Kafka Eagle的步骤结论 引言 在现代大数据架构中&#xff0c;Apache Kafka已成为一个不可或缺的组件&#xff0c;用于可靠地处理和传输大规模的数据流。然而&#xff0c;随着Kafka集群规模的不断增长&…

编写shell脚本,利用mysqldump实现MySQL数据库分库分表备份

查看数据和数据表 mysql -uroot -p123456 -e show databases mysql -uroot -p123456 -e show tables from cb_d 删除头部Database和数据库自带的表 mysql -uroot -p123456 -e show databases -N | egrep -v "information_schema|mysql|performance_schema|sys"编写…

Linux之sched_setscheduler调度策略总结(六十)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 人生格言&#xff1a; 人生…

设计模式第一课-单例模式(懒汉模式和饿汉模式)

单例模式 个人理解&#xff1a;单例模式实际就是通过类加载的方式获取到一个对象&#xff0c;并且保证这个对象在使用中只有一个&#xff0c;不允许再次被创建 一、懒汉模式 1、懒汉模式的基础写法 代码解释&#xff1a; &#xff08;1&#xff09;、编写LazySingleton类的…

Shiny Server和ShinyProxy是什么,有什么区别?

调研以及参与过多个生物公司的生信工具研发&#xff0c;不管是ShinyServer还是ShinyProxy都有一定研究&#xff0c;尤其是ShinyServer。如果仅是本地化测试想快速的搭建Shiny应用&#xff0c;我推荐用Shiny Server&#xff0c;如果多并发用户且更好的线上管理Shiny应用&#xf…

React Native 样式及其布局

React Native 样式及其布局 参考 https://reactnative.cn/docs/flexbox 一、样式 在 React Native 中&#xff0c;你并不需要学习什么特殊的语法来定义样式。我们仍然是使用 JavaScript 来写样式。所有的核心组件都接受名为style的属性。这些样式名基本上是遵循了 web 上的 …

安全与HTTP协议:为何明文传输数据成为争议焦点?

&#x1f3ac; 江城开朗的豌豆&#xff1a;个人主页 &#x1f525; 个人专栏 :《 VUE 》 《 javaScript 》 &#x1f4dd; 个人网站 :《 江城开朗的豌豆&#x1fadb; 》 ⛺️ 生活的理想&#xff0c;就是为了理想的生活 ! 目录 ⭐ 专栏简介 &#x1f4d8; 文章引言 一、H…

CentOS 搭建 Hadoop3 高可用集群

Hadoop FullyDistributed Mode 完全分布式 spark101spark102spark103192.168.171.101192.168.171.102192.168.171.103namenodenamenodejournalnodejournalnodejournalnodedatanodedatanodedatanodenodemanagernodemanagernodemanagerrecource managerrecource managerjob hist…

2023.10.31 关于 Spring 的基本概念

目录 Spring 容器 对象生命周期 IoC Spring IoC DI Spring Spring 的全称为 Spring Framework&#xff0c;是一个开源的 Java 应用程序框架它提供了一种综合的编程和配置模型&#xff0c;用于构建现代化企业级的应用程序 一句话概括 Spring 是包含了众多工具方法的 IoC …

docker进阶

文章目录 docker 进阶Part1 常用命令总结docker version 查看docker客户端和服务端信息docker info 查看更加详细信息docker images 列出所有镜像基本用法常用选项 docker search 搜索镜像基本用法示例用法 docker pull 拉取镜像基本用法示例用法 docker rmi 删除镜像基本用法示…

FMC子卡解决方案:FMC214-基于FMC兼容1.8V IO的Full Camera Link 输出子卡

FMC214-基于FMC兼容1.8V IO的Full Camera Link 输出子卡 一、板卡概述   基于FMC兼容1.8V IO的Full Camera Link 输出子卡支持Base、Middle、Full Camera link信号输出&#xff0c;兼容1.8V、2.5V、3.3V IO FPGA信号输出。适配xilinx不同型号开发板和公司内部各FMC载板。北…

Macroscope安全漏洞检测工具简介

学习目标&#xff1a; 本介绍旨在帮助感兴趣者尽快了解 Macroscope&#xff0c;这是一款用于安全测试自动化和漏洞管理的企业工具。 全覆盖应用程序安全测试&#xff1a; 如下图所示&#xff0c;如果使用多种互补工具&#xff08;SAST/DAST/SCA 等&#xff09;来检测应用程序…

webgoat(A2) Broken Authentication

身份验证绕过 身份验证绕过以多种方式发生&#xff0c;但通常会利用配置或逻辑中的某些缺陷。篡改以达到正确的条件。 隐藏输入 最简单的形式是依赖于网页/DOM 中的隐藏输入。 删除参数 有时&#xff0c;如果攻击者不知道参数的正确值&#xff0c;他们可能会从提交中完全删…

安企神局域网监控软件,员工电脑终端的安全管理软件

安企神局域网监控软件&#xff0c;员工电脑终端的安全管理软件 安企神局域网监控软件下载使用 公司老板其实最怕的就是公司机密遭到泄露&#xff0c;而一般泄露的方法都是通过一些通讯软件而泄露出去的&#xff0c;如微信、qq等等&#xff0c;所以很多老板都想知道有什么软件…

idea 配置checkstyle全过程

checkstyle是提高代码质量,检查代码规范的很好用的一款工具&#xff0c;本文简单介绍一下集成的步骤&#xff0c;并提供一份完整的checkstyle的代码规范格式文件&#xff0c;以及常见的格式问题的解决方法。 一&#xff0c;安装 打开idea的文件选项&#xff0c;选择设置&…

Intel oneAPI笔记(1)--oneAPI简介、SYCL编程简介

oneAPI简介 Intel oneAPI是Intel提供的统一编程模型和软件开发框架。 它旨在简化可充分利用英特尔各种硬件架构&#xff08;包括 CPU、GPU 和 FPGA&#xff09;的应用程序的开发 oneAPI一个重要的特性是开放性&#xff0c;支持多种类型的架构和不同的硬件供应商&#xff0c;是…