伪代码
代码使用3层卷积神经网络conv 和两层全连接Fully Connected Layer
// conv -> conv -> conv -> fc -> fc
return new SequentialBlock()
.add(Conv2d.builder()
//卷积核大小 8*8
.setKernelShape(new Shape(8, 8))
//步幅 4*4
.optStride(new Shape(4, 4))
.optPadding(new Shape(3, 3))
//卷积核层数
.setFilters(4).build())
.add(Activation::relu)
.add(Conv2d.builder()
.setKernelShape(new Shape(4, 4))
.optStride(new Shape(2, 2))
.setFilters(32).build())
.add(Activation::relu)
.add(Conv2d.builder()
.setKernelShape(new Shape(3, 3))
.optStride(new Shape(1, 1))
.setFilters(64).build())
.add(Activation::relu)
.add(Blocks.batchFlattenBlock())
.add(Linear
.builder()
.setUnits(512).build())
.add(Activation::relu)
.add(Linear
.builder()
.setUnits(2).build());
DJL使用pytorch模型识别狗的种类
pom
<dependency>
<groupId>ai.djl.pytorch</groupId>
<artifactId>pytorch-engine</artifactId>
<version>0.16.0</version>
</dependency>
<dependency>
<groupId>ai.djl.pytorch</groupId>
<artifactId>pytorch-native-auto</artifactId>
<version>1.9.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>ai.djl.pytorch</groupId>
<artifactId>pytorch-jni</artifactId>
<version>1.9.1-0.16.0</version>
<scope>runtime</scope>
</dependency>
java文件
package com.example.demo.util;
import ai.djl.MalformedModelException;
import ai.djl.Model;
import ai.djl.inference.Predictor;
import ai.djl.modality.Classifications;
import ai.djl.modality.cv.Image;
import ai.djl.modality.cv.ImageFactory;
import ai.djl.modality.cv.transform.CenterCrop;
import ai.djl.modality.cv.transform.Resize;
import ai.djl.modality.cv.transform.ToTensor;
import ai.djl.modality.cv.translator.ImageClassificationTranslator;
import ai.djl.repository.zoo.ModelNotFoundException;
import ai.djl.training.util.DownloadUtils;
import ai.djl.training.util.ProgressBar;
import ai.djl.translate.Pipeline;
import ai.djl.translate.TranslateException;
import ai.djl.translate.Translator;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
//狗狗种类预测
public class PyTorchLearn {
public static void main(String[] args) throws IOException, TranslateException, MalformedModelException, ModelNotFoundException {
//下载模型到本地
DownloadUtils.download("https://djl-ai.s3.amazonaws.com/mlrepo/model/cv/image_classification/ai/djl/pytorch/resnet/0.0.1/traced_resnet18.pt.gz", "build/pytorch_models/resnet18/resnet18.pt", new ProgressBar());
DownloadUtils.download("https://djl-ai.s3.amazonaws.com/mlrepo/model/cv/image_classification/ai/djl/pytorch/synset.txt", "build/pytorch_models/resnet18/synset.txt", new ProgressBar());
Path modelDir = Paths.get("build/pytorch_models/resnet18");
//加载模型
Model model = Model.newInstance("resnet");
model.load(modelDir, "resnet18");
// 先创建一个管道(每个图像要经过的预处理)
Pipeline pipeline = new Pipeline();
pipeline.add(new CenterCrop()).add(new Resize(224, 224)).add(new ToTensor());
// 然后创建转换器
Translator<Image, Classifications> translator = ImageClassificationTranslator.builder()
.setPipeline(pipeline)
//载入所有标签进去
.optSynsetArtifactName("synset.txt")
//如果你的模型最后一层没有经过softmax就启用它
.optApplySoftmax(true)
//最终显示概率最高的3个
.optTopK(3)
.build();
Image img = ImageFactory.getInstance().fromUrl("https://img1.baidu.com/it/u=281078762,293009126&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500");
img.getWrappedImage();
// 执行推理
Predictor<Image, Classifications> predictor = model.newPredictor(translator);
Classifications classifications = predictor.predict(img);
System.out.println(classifications);
}
}