便于对火把进行研究
jdit的Python项目详细描述
jdit是一个基于pytorch的面向研究处理的框架。 只关心你的想法。你不需要构建一个冗长乏味的代码 进行一个深入的学习项目来验证你的想法。
你只需要执行你的想法,不需要做任何训练 框架、多GPU、检查点、流程可视化、性能 评估等等。
指南:https://dingguanglei.com/tag/jdit
文件:https://jdit.readthedocs.io/en/latest/index.html
如果你有任何问题,或者你发现错误,你可以联系作者。
安装
需要:
tensorboard >= 1.14.0 pytorch >= 1.1.0
安装要求。
pip install -r requirements.txt
来源
建议使用此方法,因为您可以保留最新版本。
从github克隆
git clone https://github.com/dingguanglei/jdit
设置 使用
setup.py
安装包。python setup.py bdist_wheel
安装 您将在
jdit/dist/
中找到包。使用pip安装。pip install dist/jdit-0.x.0-py3-none-any.whl
来自PIP
pip install jdit
快速启动
在构建和安装jdit包之后,可以创建一个新目录 快速测试。假设您得到一个新的目录示例。运行 此代码位于ipython cmd中。(也可以创建main.py文件。)
from jdit.trainer.instances.fashingClassification import start_fashingClassTrainer start_fashingClassTrainer()
以下是start_fashingClassTrainer()
# coding=utf-8 import torch import torch.nn as nn import torch.nn.functional as F from jdit.trainer.classification import ClassificationTrainer from jdit import Model from jdit.optimizer import Optimizer from jdit.dataset import FashionMNIST # This is your model. Defined by torch.nn.Module class SimpleModel(nn.Module): def __init__(self, depth=64, num_class=10): super(SimpleModel, self).__init__() self.num_class = num_class self.layer1 = nn.Conv2d(1, depth, 3, 1, 1) self.layer2 = nn.Conv2d(depth, depth * 2, 4, 2, 1) self.layer3 = nn.Conv2d(depth * 2, depth * 4, 4, 2, 1) self.layer4 = nn.Conv2d(depth * 4, depth * 8, 4, 2, 1) self.layer5 = nn.Conv2d(depth * 8, num_class, 4, 1, 0) def forward(self, input): out = F.relu(self.layer1(input)) out = F.relu(self.layer2(out)) out = F.relu(self.layer3(out)) out = F.relu(self.layer4(out)) out = self.layer5(out) out = out.view(-1, self.num_class) return out # A trainer, you need to rewrite the loss and valid function. class FashingClassTrainer(ClassificationTrainer): def __init__(self, logdir, nepochs, gpu_ids, net, opt, datasets, num_class): super(FashingClassTrainer, self).__init__(logdir, nepochs, gpu_ids, net, opt, datasets, num_class) data, label = self.datasets.samples_train # plot samples of dataset in tensorboard. self.watcher.embedding(data, data, label, 1) def compute_loss(self): var_dic = {} var_dic["CEP"] = loss = nn.CrossEntropyLoss()(self.output, self.labels.squeeze().long()) _, predict = torch.max(self.output.detach(), 1) # 0100=>1 0010=>2 total = predict.size(0) * 1.0 labels = self.labels.squeeze().long() correct = predict.eq(labels).cpu().sum().float() acc = correct / total var_dic["ACC"] = acc return loss, var_dic def compute_valid(self): var_dic = {} var_dic["CEP"] = cep = nn.CrossEntropyLoss()(self.output, self.labels.squeeze().long()) _, predict = torch.max(self.output.detach(), 1) # 0100=>1 0010=>2 total = predict.size(0) * 1.0 labels = self.labels.squeeze().long() correct = predict.eq(labels).cpu().sum().float() acc = correct / total var_dic["ACC"] = acc return var_dic def start_fashingClassTrainer(gpus=(), nepochs=10, run_type="train"): num_class = 10 depth = 32 gpus = gpus batch_size = 64 nepochs = nepochs logdir = "log/fashion_classify" opt_hpm = {"optimizer": "Adam", "lr_decay": 0.94, "decay_position": 10, "decay_type": "epoch", "lr": 1e-3, "weight_decay": 2e-5, "betas": (0.9, 0.99)} print('===> Build dataset') mnist = FashionMNIST(batch_size=batch_size) torch.backends.cudnn.benchmark = True print('===> Building model') net = Model(SimpleModel(depth=depth), gpu_ids_abs=gpus, init_method="kaiming", check_point_pos=1) print('===> Building optimizer') opt = Optimizer(net.parameters(), **opt_hpm) print('===> Training') print("using `tensorboard --logdir=log` to see learning curves and net structure." "training and valid_epoch data, configures info and checkpoint were save in `log` directory.") Trainer = FashingClassTrainer(logdir, nepochs, gpus, net, opt, mnist, num_class) if run_type == "train": Trainer.train() elif run_type == "debug": Trainer.debug() if __name__ == '__main__': start_fashingClassTrainer()
然后你会看到下面这样的东西。
===> Build dataset use 8 thread Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz Processing... Done ===> Building model ResNet Total number of parameters: 2776522 ResNet model use CPU apply kaiming weight init ===> Building optimizer ===> Training using `tensorboard --logdir=log` to see learning curves and net structure. training and valid_epoch data, configures info and checkpoint were save in `log` directory. 0%| | 0/10 [00:00<?, ?epoch/s] 0step [00:00, step?/s]
在Tensorboard中查看学习曲线。注意你关于var_dic["ACC"], var_dic["CEP"]
的代码。
这将在Tensorboard中显示。
学习曲线
模型结构
数据集
您需要申请self.watcher.embedding(data, data, label)
)
对数
所有这些都将保存在log/fashion_classify
中,因为参数logdir = "log/fashion_classify"
。
过程数据
我们最关心的是培训过程和有效的过程数据。
它们保存在Train.csv
和Valid.csv
中。以下是内容。
火车.csv
有效的.csv
型号
模型信息将保存在net.csv
。(文件名由变量名(net
)给定。)
如果您的模型在此过程中发生更改,它将记录在此文件中。
学习率跟踪
从文件opt.csv
可以看到学习率的变化。将只保存更改的功能。
数据集信息
从文件datasets.csv
可以看到数据集的信息。
其他
- 对于文件
performance.csv
,它节省了培训期间的内存成本。 - 对于文件
FashingClassTrainer.csv
,它会保存一些参数,例如epoch的数量。 checkpoint
目录中的模型检查点。
结论
由于这个简单的分类示例,这里没有显示许多有趣的特性。 但是你可以从这个简单的例子中得到直觉。 你的代码似乎没有这些功能。所以,JDIT就是这么做的。
虽然这只是一个例子,但您仍然可以构建自己的项目 使用JDIT框架很容易。JDIT框架可以处理
- 数据可视化。(试验过程中的学习曲线、图像)
- CPU、GPU或GPU。(在指定设备上培训您的型号)
- 中间数据存储。(将培训数据保存到csv文件中)
- 自动建立检查点模型。
- 。
更多
对于其他模板,您可以在此处查看和学习表单。
指南列表: