Commit b6c19984 authored by dengjb's avatar dengjb
Browse files

update

parents
# encoding: utf-8
"""
@authorr: liaoxingyu
@contact: sherlockliao01@gmail.com
"""
from fastreid.modeling.losses import *
from fastreid.modeling.meta_arch import Baseline
from fastreid.modeling.meta_arch.build import META_ARCH_REGISTRY
@META_ARCH_REGISTRY.register()
class PartialBaseline(Baseline):
def losses(self, outputs, gt_labels):
r"""
Compute loss from modeling's outputs, the loss function input arguments
must be the same as the outputs of the model forwarding.
"""
loss_dict = super().losses(outputs, gt_labels)
fore_cls_outputs = outputs["fore_cls_outputs"]
fore_feat = outputs["foreground_features"]
loss_names = self.loss_kwargs['loss_names']
if 'CrossEntropyLoss' in loss_names:
ce_kwargs = self.loss_kwargs.get('ce')
loss_dict['loss_fore_cls'] = cross_entropy_loss(
fore_cls_outputs,
gt_labels,
ce_kwargs.get('eps'),
ce_kwargs.get('alpha')
) * ce_kwargs.get('scale')
if 'TripletLoss' in loss_names:
tri_kwargs = self.loss_kwargs.get('tri')
loss_dict['loss_fore_triplet'] = triplet_loss(
fore_feat,
gt_labels,
tri_kwargs.get('margin'),
tri_kwargs.get('norm_feat'),
tri_kwargs.get('hard_mining')
) * tri_kwargs.get('scale')
return loss_dict
#!/usr/bin/env python
# encoding: utf-8
"""
@author: sherlock
@contact: sherlockliao01@gmail.com
"""
import logging
import os
import sys
sys.path.append('.')
from fastreid.config import get_cfg
from fastreid.engine import DefaultTrainer, default_argument_parser, default_setup, launch
from fastreid.utils.checkpoint import Checkpointer
from fastreid.engine import hooks
from partialreid import *
class Trainer(DefaultTrainer):
@classmethod
def build_evaluator(cls, cfg, dataset_name, output_dir=None):
data_loader, num_query = cls.build_test_loader(cfg, dataset_name)
return data_loader, DsrEvaluator(cfg, num_query, output_dir)
def setup(args):
"""
Create configs and perform basic setups.
"""
cfg = get_cfg()
add_partialreid_config(cfg)
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.freeze()
default_setup(cfg, args)
return cfg
def main(args):
cfg = setup(args)
if args.eval_only:
logger = logging.getLogger("fastreid.trainer")
cfg.defrost()
cfg.MODEL.BACKBONE.PRETRAIN = False
model = Trainer.build_model(cfg)
Checkpointer(model).load(cfg.MODEL.WEIGHTS) # load trained model
if cfg.TEST.PRECISE_BN.ENABLED and hooks.get_bn_modules(model):
prebn_cfg = cfg.clone()
prebn_cfg.DATALOADER.NUM_WORKERS = 0 # save some memory and time for PreciseBN
prebn_cfg.DATASETS.NAMES = tuple([cfg.TEST.PRECISE_BN.DATASET]) # set dataset name for PreciseBN
logger.info("Prepare precise BN dataset")
hooks.PreciseBN(
# Run at the same freq as (but before) evaluation.
model,
# Build a new data loader to not affect training
Trainer.build_train_loader(prebn_cfg),
cfg.TEST.PRECISE_BN.NUM_ITER,
).update_stats()
res = Trainer.test(cfg, model)
return res
trainer = Trainer(cfg)
trainer.resume_or_load(resume=args.resume)
return trainer.train()
if __name__ == "__main__":
args = default_argument_parser().parse_args()
print("Command Line Args:", args)
launch(
main,
args.num_gpus,
num_machines=args.num_machines,
machine_rank=args.machine_rank,
dist_url=args.dist_url,
args=(args,),
)
Here are a few projects that are built on fastreid.
They are examples of how to use fastrei as a library, to make your projects more maintainable.
# Projects by JDAI
Note that these are research projects, and therefore may not have the same level of support or stability of fastreid.
- [Deep Spatial Feature Reconstruction for Partial Person Re-identification](https://github.com/JDAI-CV/fast-reid/tree/master/projects/PartialReID)
- [Black Re-ID: A Head-shoulder Descriptor for the Challenging Problem of Person Re-Identification](https://github.com/JDAI-CV/fast-reid/tree/master/projects/HAA)
- [Image Classification](https://github.com/JDAI-CV/fast-reid/tree/master/projects/FastCls)
- [Face Recognition](https://github.com/JDAI-CV/fast-reid/tree/master/projects/FastFace)
- [Image Retrieval](https://github.com/JDAI-CV/fast-reid/tree/master/projects/FastRetri)
- [Attribute Recognition](https://github.com/JDAI-CV/fast-reid/tree/master/projects/FastAttr)
- [Hyper-Parameters Optimization](https://github.com/JDAI-CV/fast-reid/tree/master/projects/FastTune)
- [Overhaul Distillation](https://github.com/JDAI-CV/fast-reid/tree/master/projects/FastDistill)
- Semi-Supervised Domain Generalizable Person Re-Identification. [code](https://github.com/xiaomingzhid/sskd) and [paper](https://arxiv.org/pdf/2108.05045.pdf)
# External Projects
External projects in the community that use fastreid:
- [FastReID of Interpreter Project (ICCV 2021)](https://github.com/SheldongChen/AMD.github.io)
# Competitions
- NAIC20 reid track [1-st solution](https://github.com/JDAI-CV/fast-reid/tree/master/projects/NAIC20)
# encoding: utf-8
"""
@author: sherlock
@contact: sherlockliao01@gmail.com
"""
# encoding: utf-8
"""
@author: liaoxingyu
@contact: sherlockliao01@gmail.com
"""
import sys
sys.path.append('.')
from data import get_dataloader
from config import cfg
import argparse
from data.datasets import init_dataset
# cfg.DATALOADER.SAMPLER = 'triplet'
cfg.DATASETS.NAMES = ("market1501", "dukemtmc", "cuhk03", "msmt17",)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="ReID Baseline Training")
parser.add_argument(
'-cfg', "--config_file",
default="",
metavar="FILE",
help="path to config file",
type=str
)
# parser.add_argument("--local_rank", type=int, default=0)
parser.add_argument("opts", help="Modify config options using the command-line", default=None,
nargs=argparse.REMAINDER)
args = parser.parse_args()
cfg.merge_from_list(args.opts)
# dataset = init_dataset('msmt17', combineall=True)
get_dataloader(cfg)
# tng_dataloader, val_dataloader, num_classes, num_query = get_dataloader(cfg)
# def get_ex(): return open_image('datasets/beijingStation/query/000245_c10s2_1561732033722.000000.jpg')
# im = get_ex()
# print(data.train_ds[0])
# print(data.test_ds[0])
# a = next(iter(data.train_dl))
# from IPython import embed; embed()
# from ipdb import set_trace; set_trace()
# im.apply_tfms(crop_pad(size=(300, 300)))
import unittest
import numpy as np
import os
from glob import glob
class TestFeatureAlign(unittest.TestCase):
def test_caffe_pytorch_feat_align(self):
caffe_feat_path = "/export/home/lxy/cvpalgo-fast-reid/tools/deploy/caffe_R50_output"
pytorch_feat_path = "/export/home/lxy/cvpalgo-fast-reid/demo/logs/R50_256x128_pytorch_feat_output"
feat_filenames = os.listdir(caffe_feat_path)
for feat_name in feat_filenames:
caffe_feat = np.load(os.path.join(caffe_feat_path, feat_name))
pytorch_feat = np.load(os.path.join(pytorch_feat_path, feat_name))
sim = np.dot(caffe_feat, pytorch_feat.transpose())[0][0]
assert sim > 0.97, f"Got similarity {sim} and feature of {feat_name} is not aligned"
def test_model_performance(self):
caffe_feat_path = "/export/home/lxy/cvpalgo-fast-reid/tools/deploy/caffe_R50_output"
feat_filenames = os.listdir(caffe_feat_path)
feats = []
for feat_name in feat_filenames:
caffe_feat = np.load(os.path.join(caffe_feat_path, feat_name))
feats.append(caffe_feat)
from ipdb import set_trace; set_trace()
if __name__ == '__main__':
unittest.main()
import torch
from fastai.vision import *
from fastai.basic_data import *
from fastai.layers import *
import sys
sys.path.append('.')
from engine.interpreter import ReidInterpretation
from data import get_data_bunch
from modeling import build_model
from config import cfg
cfg.DATASETS.NAMES = ('market1501',)
cfg.DATASETS.TEST_NAMES = 'market1501'
cfg.MODEL.BACKBONE = 'resnet50'
data_bunch, test_labels, num_query = get_data_bunch(cfg)
model = build_model(cfg, 10)
model.load_params_wo_fc(torch.load('logs/2019.8.14/market/baseline/models/model_149.pth')['model'])
learn = Learner(data_bunch, model)
feats, _ = learn.get_preds(DatasetType.Test, activ=Lambda(lambda x: x))
\ No newline at end of file
import sys
import unittest
import torch
from torch import nn
sys.path.append('.')
from solver.lr_scheduler import WarmupMultiStepLR
from solver.build import make_optimizer
from config import cfg
class MyTestCase(unittest.TestCase):
def test_something(self):
net = nn.Linear(10, 10)
optimizer = make_optimizer(cfg, net)
lr_scheduler = WarmupMultiStepLR(optimizer, [20, 40], warmup_iters=10)
for i in range(50):
lr_scheduler.step()
for j in range(3):
print(i, lr_scheduler.get_lr()[0])
optimizer.step()
if __name__ == '__main__':
unittest.main()
import unittest
import torch
import sys
sys.path.append('.')
from fastreid.config import cfg
from fastreid.modeling.backbones import build_resnet_backbone
from fastreid.modeling.backbones.resnet_ibn_a import se_resnet101_ibn_a
from torch import nn
class MyTestCase(unittest.TestCase):
def test_se_resnet101(self):
cfg.MODEL.BACKBONE.NAME = 'resnet101'
cfg.MODEL.BACKBONE.DEPTH = 101
cfg.MODEL.BACKBONE.WITH_IBN = True
cfg.MODEL.BACKBONE.WITH_SE = True
cfg.MODEL.BACKBONE.PRETRAIN_PATH = '/export/home/lxy/.cache/torch/checkpoints/se_resnet101_ibn_a.pth.tar'
net1 = build_resnet_backbone(cfg)
net1.cuda()
net2 = nn.DataParallel(se_resnet101_ibn_a())
res = net2.load_state_dict(torch.load(cfg.MODEL.BACKBONE.PRETRAIN_PATH)['state_dict'], strict=False)
net2.cuda()
x = torch.randn(10, 3, 256, 128).cuda()
y1 = net1(x)
y2 = net2(x)
assert y1.sum() == y2.sum(), 'train mode problem'
net1.eval()
net2.eval()
y1 = net1(x)
y2 = net2(x)
assert y1.sum() == y2.sum(), 'eval mode problem'
if __name__ == '__main__':
unittest.main()
import unittest
import sys
sys.path.append('.')
from fastreid.data.samplers import TrainingSampler
class SamplerTestCase(unittest.TestCase):
def test_training_sampler(self):
sampler = TrainingSampler(5)
for i in sampler:
from ipdb import set_trace; set_trace()
print(i)
if __name__ == '__main__':
unittest.main()
import sys
import unittest
import torch
sys.path.append('.')
from fastreid.config import get_cfg
from fastreid.modeling.backbones import build_backbone
class MyTestCase(unittest.TestCase):
def test_fusebn(self):
cfg = get_cfg()
cfg.defrost()
cfg.MODEL.BACKBONE.NAME = 'build_repvgg_backbone'
cfg.MODEL.BACKBONE.DEPTH = 'B1g2'
cfg.MODEL.BACKBONE.PRETRAIN = False
model = build_backbone(cfg)
model.eval()
test_inp = torch.randn((1, 3, 256, 128))
y = model(test_inp)
model.deploy(mode=True)
from ipdb import set_trace; set_trace()
fused_y = model(test_inp)
print("final error :", torch.max(torch.abs(fused_y - y)).item())
if __name__ == '__main__':
unittest.main()
# The Caffe in nn_tools Provides some convenient API
If there are some problem in parse your prototxt or caffemodel, Please replace
the caffe.proto with your own version and compile it with command
`protoc --python_out ./ caffe.proto`
## caffe_net.py
Using `from nn_tools.Caffe import caffe_net` to import this model
### Prototxt
+ `net=caffe_net.Prototxt(file_name)` to open a prototxt file
+ `net.init_caffemodel(caffe_cmd_path='caffe')` to generate a caffemodel file in the current work directory \
if your `caffe` cmd not in the $PATH, specify your caffe cmd path by the `caffe_cmd_path` kwargs.
### Caffemodel
+ `net=caffe_net.Caffemodel(file_name)` to open a caffemodel
+ `net.save_prototxt(path)` to save the caffemodel to a prototxt file (not containing the weight data)
+ `net.get_layer_data(layer_name)` return the numpy ndarray data of the layer
+ `net.set_layer_date(layer_name, datas)` specify the data of one layer in the caffemodel .`datas` is normally a list of numpy ndarray `[weights,bias]`
+ `net.save(path)` save the changed caffemodel
### Functions for both Prototxt and Caffemodel
+ `net.add_layer(layer_params,before='',after='')` add a new layer with `Layer_Param` object
+ `net.remove_layer_by_name(layer_name)`
+ `net.get_layer_by_name(layer_name)` or `net.layer(layer_name)` get the raw Layer object defined in caffe_pb2
syntax = "proto2";
package caffe;
// Specifies the shape (dimensions) of a Blob.
message BlobShape {
repeated int64 dim = 1 [packed = true];
}
message BlobProto {
optional BlobShape shape = 7;
repeated float data = 5 [packed = true];
repeated float diff = 6 [packed = true];
repeated double double_data = 8 [packed = true];
repeated double double_diff = 9 [packed = true];
// 4D dimensions -- deprecated. Use "shape" instead.
optional int32 num = 1 [default = 0];
optional int32 channels = 2 [default = 0];
optional int32 height = 3 [default = 0];
optional int32 width = 4 [default = 0];
}
// The BlobProtoVector is simply a way to pass multiple blobproto instances
// around.
message BlobProtoVector {
repeated BlobProto blobs = 1;
}
message Datum {
optional int32 channels = 1;
optional int32 height = 2;
optional int32 width = 3;
// the actual image data, in bytes
optional bytes data = 4;
optional int32 label = 5;
// Optionally, the datum could also hold float data.
repeated float float_data = 6;
// If true data contains an encoded image that need to be decoded
optional bool encoded = 7 [default = false];
repeated float labels = 8;
}
// *******************add by xia for ssd******************
// The label (display) name and label id.
message LabelMapItem {
// Both name and label are required.
optional string name = 1;
optional int32 label = 2;
// display_name is optional.
optional string display_name = 3;
}
message LabelMap {
repeated LabelMapItem item = 1;
}
// Sample a bbox in the normalized space [0, 1] with provided constraints.
message Sampler {
// Minimum scale of the sampled bbox.
optional float min_scale = 1 [default = 1.];
// Maximum scale of the sampled bbox.
optional float max_scale = 2 [default = 1.];
// Minimum aspect ratio of the sampled bbox.
optional float min_aspect_ratio = 3 [default = 1.];
// Maximum aspect ratio of the sampled bbox.
optional float max_aspect_ratio = 4 [default = 1.];
}
// Constraints for selecting sampled bbox.
message SampleConstraint {
// Minimum Jaccard overlap between sampled bbox and all bboxes in
// AnnotationGroup.
optional float min_jaccard_overlap = 1;
// Maximum Jaccard overlap between sampled bbox and all bboxes in
// AnnotationGroup.
optional float max_jaccard_overlap = 2;
// Minimum coverage of sampled bbox by all bboxes in AnnotationGroup.
optional float min_sample_coverage = 3;
// Maximum coverage of sampled bbox by all bboxes in AnnotationGroup.
optional float max_sample_coverage = 4;
// Minimum coverage of all bboxes in AnnotationGroup by sampled bbox.
optional float min_object_coverage = 5;
// Maximum coverage of all bboxes in AnnotationGroup by sampled bbox.
optional float max_object_coverage = 6;
}
// Sample a batch of bboxes with provided constraints.
message BatchSampler {
// Use original image as the source for sampling.
optional bool use_original_image = 1 [default = true];
// Constraints for sampling bbox.
optional Sampler sampler = 2;
// Constraints for determining if a sampled bbox is positive or negative.
optional SampleConstraint sample_constraint = 3;
// If provided, break when found certain number of samples satisfing the
// sample_constraint.
optional uint32 max_sample = 4;
// Maximum number of trials for sampling to avoid infinite loop.
optional uint32 max_trials = 5 [default = 100];
}
// Condition for emitting annotations.
message EmitConstraint {
enum EmitType {
CENTER = 0;
MIN_OVERLAP = 1;
}
optional EmitType emit_type = 1 [default = CENTER];
// If emit_type is MIN_OVERLAP, provide the emit_overlap.
optional float emit_overlap = 2;
}
// The normalized bounding box [0, 1] w.r.t. the input image size.
message NormalizedBBox {
optional float xmin = 1;
optional float ymin = 2;
optional float xmax = 3;
optional float ymax = 4;
optional int32 label = 5;
optional bool difficult = 6;
optional float score = 7;
optional float size = 8;
}
// Annotation for each object instance.
message Annotation {
optional int32 instance_id = 1 [default = 0];
optional NormalizedBBox bbox = 2;
}
// Group of annotations for a particular label.
message AnnotationGroup {
optional int32 group_label = 1;
repeated Annotation annotation = 2;
}
// An extension of Datum which contains "rich" annotations.
message AnnotatedDatum {
enum AnnotationType {
BBOX = 0;
}
optional Datum datum = 1;
// If there are "rich" annotations, specify the type of annotation.
// Currently it only supports bounding box.
// If there are no "rich" annotations, use label in datum instead.
optional AnnotationType type = 2;
// Each group contains annotation for a particular class.
repeated AnnotationGroup annotation_group = 3;
}
// *******************add by xia for mtcnn******************
message MTCNNBBox {
optional float xmin = 1;
optional float ymin = 2;
optional float xmax = 3;
optional float ymax = 4;
}
message MTCNNDatum {
optional Datum datum = 1;
//repeated MTCNNBBox rois = 2;
optional MTCNNBBox roi = 2;
repeated float pts = 3;
}
//**************************************************************
message FillerParameter {
// The filler type.
optional string type = 1 [default = 'constant'];
optional float value = 2 [default = 0]; // the value in constant filler
optional float min = 3 [default = 0]; // the min value in uniform filler
optional float max = 4 [default = 1]; // the max value in uniform filler
optional float mean = 5 [default = 0]; // the mean value in Gaussian filler
optional float std = 6 [default = 1]; // the std value in Gaussian filler
// The expected number of non-zero output weights for a given input in
// Gaussian filler -- the default -1 means don't perform sparsification.
optional int32 sparse = 7 [default = -1];
// Normalize the filler variance by fan_in, fan_out, or their average.
// Applies to 'xavier' and 'msra' fillers.
enum VarianceNorm {
FAN_IN = 0;
FAN_OUT = 1;
AVERAGE = 2;
}
optional VarianceNorm variance_norm = 8 [default = FAN_IN];
// added by me
optional string file = 9;
}
message NetParameter {
optional string name = 1; // consider giving the network a name
// The input blobs to the network.
repeated string input = 3;
// The shape of the input blobs.
repeated BlobShape input_shape = 8;
// 4D input dimensions -- deprecated. Use "shape" instead.
// If specified, for each input blob there should be four
// values specifying the num, channels, height and width of the input blob.
// Thus, there should be a total of (4 * #input) numbers.
repeated int32 input_dim = 4;
// Whether the network will force every layer to carry out backward operation.
// If set False, then whether to carry out backward is determined
// automatically according to the net structure and learning rates.
optional bool force_backward = 5 [default = false];
// The current "state" of the network, including the phase, level, and stage.
// Some layers may be included/excluded depending on this state and the states
// specified in the layers' include and exclude fields.
optional NetState state = 6;
// Print debugging information about results while running Net::Forward,
// Net::Backward, and Net::Update.
optional bool debug_info = 7 [default = false];
// The layers that make up the net. Each of their configurations, including
// connectivity and behavior, is specified as a LayerParameter.
repeated LayerParameter layer = 100; // ID 100 so layers are printed last.
// DEPRECATED: use 'layer' instead.
repeated V1LayerParameter layers = 2;
}
// NOTE
// Update the next available ID when you add a new SolverParameter field.
//
// SolverParameter next available ID: 41 (last added: type)
message SolverParameter {
//////////////////////////////////////////////////////////////////////////////
// Specifying the train and test networks
//
// Exactly one train net must be specified using one of the following fields:
// train_net_param, train_net, net_param, net
// One or more test nets may be specified using any of the following fields:
// test_net_param, test_net, net_param, net
// If more than one test net field is specified (e.g., both net and
// test_net are specified), they will be evaluated in the field order given
// above: (1) test_net_param, (2) test_net, (3) net_param/net.
// A test_iter must be specified for each test_net.
// A test_level and/or a test_stage may also be specified for each test_net.
//////////////////////////////////////////////////////////////////////////////
// Proto filename for the train net, possibly combined with one or more
// test nets.
optional string net = 24;
// Inline train net param, possibly combined with one or more test nets.
optional NetParameter net_param = 25;
optional string train_net = 1; // Proto filename for the train net.
repeated string test_net = 2; // Proto filenames for the test nets.
optional NetParameter train_net_param = 21; // Inline train net params.
repeated NetParameter test_net_param = 22; // Inline test net params.
// The states for the train/test nets. Must be unspecified or
// specified once per net.
//
// By default, all states will have solver = true;
// train_state will have phase = TRAIN,
// and all test_state's will have phase = TEST.
// Other defaults are set according to the NetState defaults.
optional NetState train_state = 26;
repeated NetState test_state = 27;
// The number of iterations for each test net.
repeated int32 test_iter = 3;
// The number of iterations between two testing phases.
optional int32 test_interval = 4 [default = 0];
optional bool test_compute_loss = 19 [default = false];
// If true, run an initial test pass before the first iteration,
// ensuring memory availability and printing the starting value of the loss.
optional bool test_initialization = 32 [default = true];
optional float base_lr = 5; // The base learning rate
// the number of iterations between displaying info. If display = 0, no info
// will be displayed.
optional int32 display = 6;
// Display the loss averaged over the last average_loss iterations
optional int32 average_loss = 33 [default = 1];
optional int32 max_iter = 7; // the maximum number of iterations
// accumulate gradients over `iter_size` x `batch_size` instances
optional int32 iter_size = 36 [default = 1];
// The learning rate decay policy. The currently implemented learning rate
// policies are as follows:
// - fixed: always return base_lr.
// - step: return base_lr * gamma ^ (floor(iter / step))
// - exp: return base_lr * gamma ^ iter
// - inv: return base_lr * (1 + gamma * iter) ^ (- power)
// - multistep: similar to step but it allows non uniform steps defined by
// stepvalue
// - poly: the effective learning rate follows a polynomial decay, to be
// zero by the max_iter. return base_lr (1 - iter/max_iter) ^ (power)
// - sigmoid: the effective learning rate follows a sigmod decay
// return base_lr ( 1/(1 + exp(-gamma * (iter - stepsize))))
//
// where base_lr, max_iter, gamma, step, stepvalue and power are defined
// in the solver parameter protocol buffer, and iter is the current iteration.
optional string lr_policy = 8;
optional float gamma = 9; // The parameter to compute the learning rate.
optional float power = 10; // The parameter to compute the learning rate.
optional float momentum = 11; // The momentum value.
optional float weight_decay = 12; // The weight decay.
// regularization types supported: L1 and L2
// controlled by weight_decay
optional string regularization_type = 29 [default = "L2"];
// the stepsize for learning rate policy "step"
optional int32 stepsize = 13;
// the stepsize for learning rate policy "multistep"
repeated int32 stepvalue = 34;
// for rate policy "multifixed"
repeated float stagelr = 50;
repeated int32 stageiter = 51;
// Set clip_gradients to >= 0 to clip parameter gradients to that L2 norm,
// whenever their actual L2 norm is larger.
optional float clip_gradients = 35 [default = -1];
optional int32 snapshot = 14 [default = 0]; // The snapshot interval
optional string snapshot_prefix = 15; // The prefix for the snapshot.
// whether to snapshot diff in the results or not. Snapshotting diff will help
// debugging but the final protocol buffer size will be much larger.
optional bool snapshot_diff = 16 [default = false];
enum SnapshotFormat {
HDF5 = 0;
BINARYPROTO = 1;
}
optional SnapshotFormat snapshot_format = 37 [default = BINARYPROTO];
// the mode solver will use: 0 for CPU and 1 for GPU. Use GPU in default.
enum SolverMode {
CPU = 0;
GPU = 1;
}
optional SolverMode solver_mode = 17 [default = GPU];
// the device_id will that be used in GPU mode. Use device_id = 0 in default.
optional int32 device_id = 18 [default = 0];
// If non-negative, the seed with which the Solver will initialize the Caffe
// random number generator -- useful for reproducible results. Otherwise,
// (and by default) initialize using a seed derived from the system clock.
optional int64 random_seed = 20 [default = -1];
// type of the solver
optional string type = 40 [default = "SGD"];
// numerical stability for RMSProp, AdaGrad and AdaDelta and Adam
optional float delta = 31 [default = 1e-8];
// parameters for the Adam solver
optional float momentum2 = 39 [default = 0.999];
// RMSProp decay value
// MeanSquare(t) = rms_decay*MeanSquare(t-1) + (1-rms_decay)*SquareGradient(t)
optional float rms_decay = 38;
// If true, print information about the state of the net that may help with
// debugging learning problems.
optional bool debug_info = 23 [default = false];
// If false, don't save a snapshot after training finishes.
optional bool snapshot_after_train = 28 [default = true];
// DEPRECATED: old solver enum types, use string instead
enum SolverType {
SGD = 0;
NESTEROV = 1;
ADAGRAD = 2;
RMSPROP = 3;
ADADELTA = 4;
ADAM = 5;
}
// DEPRECATED: use type instead of solver_type
optional SolverType solver_type = 30 [default = SGD];
}
// A message that stores the solver snapshots
message SolverState {
optional int32 iter = 1; // The current iteration
optional string learned_net = 2; // The file that stores the learned net.
repeated BlobProto history = 3; // The history for sgd solvers
optional int32 current_step = 4 [default = 0]; // The current step for learning rate
}
enum Phase {
TRAIN = 0;
TEST = 1;
}
message NetState {
optional Phase phase = 1 [default = TEST];
optional int32 level = 2 [default = 0];
repeated string stage = 3;
}
message NetStateRule {
// Set phase to require the NetState have a particular phase (TRAIN or TEST)
// to meet this rule.
optional Phase phase = 1;
// Set the minimum and/or maximum levels in which the layer should be used.
// Leave undefined to meet the rule regardless of level.
optional int32 min_level = 2;
optional int32 max_level = 3;
// Customizable sets of stages to include or exclude.
// The net must have ALL of the specified stages and NONE of the specified
// "not_stage"s to meet the rule.
// (Use multiple NetStateRules to specify conjunctions of stages.)
repeated string stage = 4;
repeated string not_stage = 5;
}
// added by Me
message SpatialTransformerParameter {
// How to use the parameter passed by localisation network
optional string transform_type = 1 [default = "affine"];
// What is the sampling technique
optional string sampler_type = 2 [default = "bilinear"];
// If not set,stay same with the input dimension H and W
optional int32 output_H = 3;
optional int32 output_W = 4;
// If false, only compute dTheta, DO NOT compute dU
optional bool to_compute_dU = 5 [default = true];
// The default value for some parameters
optional double theta_1_1 = 6;
optional double theta_1_2 = 7;
optional double theta_1_3 = 8;
optional double theta_2_1 = 9;
optional double theta_2_2 = 10;
optional double theta_2_3 = 11;
}
// added by Me
message STLossParameter {
// Indicate the resolution of the output images after ST transformation
required int32 output_H = 1;
required int32 output_W = 2;
}
// Specifies training parameters (multipliers on global learning constants,
// and the name and other settings used for weight sharing).
message ParamSpec {
// The names of the parameter blobs -- useful for sharing parameters among
// layers, but never required otherwise. To share a parameter between two
// layers, give it a (non-empty) name.
optional string name = 1;
// Whether to require shared weights to have the same shape, or just the same
// count -- defaults to STRICT if unspecified.
optional DimCheckMode share_mode = 2;
enum DimCheckMode {
// STRICT (default) requires that num, channels, height, width each match.
STRICT = 0;
// PERMISSIVE requires only the count (num*channels*height*width) to match.
PERMISSIVE = 1;
}
// The multiplier on the global learning rate for this parameter.
optional float lr_mult = 3 [default = 1.0];
// The multiplier on the global weight decay for this parameter.
optional float decay_mult = 4 [default = 1.0];
}
// NOTE
// Update the next available ID when you add a new LayerParameter field.
//
// LayerParameter next available layer-specific ID: 143 (last added: scale_param)
message LayerParameter {
optional string name = 1; // the layer name
optional string type = 2; // the layer type
repeated string bottom = 3; // the name of each bottom blob
repeated string top = 4; // the name of each top blob
// The train / test phase for computation.
optional Phase phase = 10;
// The amount of weight to assign each top blob in the objective.
// Each layer assigns a default value, usually of either 0 or 1,
// to each top blob.
repeated float loss_weight = 5;
// Specifies training parameters (multipliers on global learning constants,
// and the name and other settings used for weight sharing).
repeated ParamSpec param = 6;
// The blobs containing the numeric parameters of the layer.
repeated BlobProto blobs = 7;
// Specifies on which bottoms the backpropagation should be skipped.
// The size must be either 0 or equal to the number of bottoms.
repeated bool propagate_down = 11;
// Rules controlling whether and when a layer is included in the network,
// based on the current NetState. You may specify a non-zero number of rules
// to include OR exclude, but not both. If no include or exclude rules are
// specified, the layer is always included. If the current NetState meets
// ANY (i.e., one or more) of the specified rules, the layer is
// included/excluded.
repeated NetStateRule include = 8;
repeated NetStateRule exclude = 9;
// Parameters for data pre-processing.
optional TransformationParameter transform_param = 100;
// Parameters shared by loss layers.
optional LossParameter loss_param = 101;
// Yolo detection loss layer
optional DetectionLossParameter detection_loss_param = 200;
// Yolo detection evaluation layer
optional EvalDetectionParameter eval_detection_param = 201;
// Yolo 9000
optional RegionLossParameter region_loss_param = 202;
optional ReorgParameter reorg_param = 203;
// Layer type-specific parameters.
//
// Note: certain layers may have more than one computational engine
// for their implementation. These layers include an Engine type and
// engine parameter for selecting the implementation.
// The default for the engine is set by the ENGINE switch at compile-time.
optional AccuracyParameter accuracy_param = 102;
optional ArgMaxParameter argmax_param = 103;
optional BatchNormParameter batch_norm_param = 139;
optional BiasParameter bias_param = 141;
optional ConcatParameter concat_param = 104;
optional ContrastiveLossParameter contrastive_loss_param = 105;
optional ConvolutionParameter convolution_param = 106;
optional DataParameter data_param = 107;
optional DropoutParameter dropout_param = 108;
optional DummyDataParameter dummy_data_param = 109;
optional EltwiseParameter eltwise_param = 110;
optional ELUParameter elu_param = 140;
optional EmbedParameter embed_param = 137;
optional ExpParameter exp_param = 111;
optional FlattenParameter flatten_param = 135;
optional HDF5DataParameter hdf5_data_param = 112;
optional HDF5OutputParameter hdf5_output_param = 113;
optional HingeLossParameter hinge_loss_param = 114;
optional ImageDataParameter image_data_param = 115;
optional InfogainLossParameter infogain_loss_param = 116;
optional InnerProductParameter inner_product_param = 117;
optional InputParameter input_param = 143;
optional LogParameter log_param = 134;
optional LRNParameter lrn_param = 118;
optional MemoryDataParameter memory_data_param = 119;
optional MVNParameter mvn_param = 120;
optional PoolingParameter pooling_param = 121;
optional PowerParameter power_param = 122;
optional PReLUParameter prelu_param = 131;
optional PythonParameter python_param = 130;
optional RecurrentParameter recurrent_param = 146;
optional ReductionParameter reduction_param = 136;
optional ReLUParameter relu_param = 123;
optional ReshapeParameter reshape_param = 133;
optional ROIPoolingParameter roi_pooling_param = 8266711; //roi pooling
optional ScaleParameter scale_param = 142;
optional SigmoidParameter sigmoid_param = 124;
optional SmoothL1LossParameter smooth_l1_loss_param = 8266712;
optional SoftmaxParameter softmax_param = 125;
optional SPPParameter spp_param = 132;
optional SliceParameter slice_param = 126;
optional TanHParameter tanh_param = 127;
optional ThresholdParameter threshold_param = 128;
optional TileParameter tile_param = 138;
optional WindowDataParameter window_data_param = 129;
// added by Me
optional SpatialTransformerParameter st_param = 148;
optional STLossParameter st_loss_param = 145;
//***************add by xia**************************
optional RPNParameter rpn_param = 150; // rpn
optional FocalLossParameter focal_loss_param = 155; // Focal Loss layer
optional AsdnDataParameter asdn_data_param = 159; //asdn
optional BNParameter bn_param = 160; //bn
optional MTCNNDataParameter mtcnn_data_param = 161; //mtcnn
optional InterpParameter interp_param = 162; //Interp
optional PSROIPoolingParameter psroi_pooling_param = 163; //rfcn
//**************************ssd*******************************************
optional AnnotatedDataParameter annotated_data_param = 164; //ssd
optional PriorBoxParameter prior_box_param = 165;
optional CropParameter crop_param = 167;
optional DetectionEvaluateParameter detection_evaluate_param = 168;
optional DetectionOutputParameter detection_output_param = 169;
//optional NormalizeParameter normalize_param = 170;
optional MultiBoxLossParameter multibox_loss_param = 171;
optional PermuteParameter permute_param = 172;
optional VideoDataParameter video_data_param = 173;
//*************************a softmax loss***********************************
optional MarginInnerProductParameter margin_inner_product_param = 174;
//*************************center loss***********************************
optional CenterLossParameter center_loss_param = 175;
//*************************deformabel conv***********************************
optional DeformableConvolutionParameter deformable_convolution_param = 176;
//***************Additive Margin Softmax for Face Verification***************
optional LabelSpecificAddParameter label_specific_add_param = 177;
optional AdditiveMarginInnerProductParameter additive_margin_inner_product_param = 178;
optional CosinAddmParameter cosin_add_m_param = 179;
optional CosinMulmParameter cosin_mul_m_param = 180;
optional ChannelScaleParameter channel_scale_param = 181;
optional FlipParameter flip_param = 182;
optional TripletLossParameter triplet_loss_param = 183;
optional CoupledClusterLossParameter coupled_cluster_loss_param = 184;
optional GeneralTripletParameter general_triplet_loss_param = 185;
optional ROIAlignParameter roi_align_param = 186;
//**************add by wdd***************
optional UpsampleParameter upsample_param = 100003;
optional MatMulParameter matmul_param = 100005;
optional PassThroughParameter pass_through_param = 100004;
optional NormalizeParameter norm_param = 100001;
}
//*********************add by wdd******************
message UpsampleParameter {
optional uint32 scale = 1 [default = 2];
optional uint32 scale_h = 2;
optional uint32 scale_w = 3;
optional bool pad_out_h = 4 [default = false];
optional bool pad_out_w = 5 [default = false];
optional uint32 upsample_h = 6;
optional uint32 upsample_w = 7;
}
message MatMulParameter {
optional uint32 dim_1 = 1;//row of input matrix one
optional uint32 dim_2 = 2;//column of input matrix one and row of input matrix two
optional uint32 dim_3 = 3;//column of input matrix two
}
message PassThroughParameter {
optional uint32 num_output = 1 [default = 0];
optional uint32 block_height = 2 [default = 0];
optional uint32 block_width = 3 [default = 0];
}
message NormalizeParameter{
optional bool across_spatial = 1 [default = true];
optional FillerParameter scale_filler = 2;
optional bool channel_shared = 3 [default = true];
optional float eps = 4 [default = 1e-10];
optional float sqrt_a = 5 [default = 1];
}
//*******************add by xia****ssd data*********
message AnnotatedDataParameter {
// Define the sampler.
repeated BatchSampler batch_sampler = 1;
// Store label name and label id in LabelMap format.
optional string label_map_file = 2;
// If provided, it will replace the AnnotationType stored in each
// AnnotatedDatum.
optional AnnotatedDatum.AnnotationType anno_type = 3;
}
//*******************add by xia****asdn data*********
message AsdnDataParameter{
optional int32 count_drop = 1 [default = 15];
optional int32 permute_count = 2 [default = 20];
optional int32 count_drop_neg = 3 [default = 0];
optional int32 channels = 4 [default = 1024];
optional int32 iter_size = 5 [default = 2];
optional int32 maintain_before = 6 [default = 1];
}
//*******************add by xia****mtcnn*********
message MTCNNDataParameter{
optional bool augmented = 1 [default = true];
optional bool flip = 2 [default = true];
// -1 means batch_size
optional int32 num_positive = 3 [default = -1];
optional int32 num_negitive = 4 [default = -1];
optional int32 num_part = 5 [default = -1];
optional uint32 resize_width = 6 [default = 0];
optional uint32 resize_height = 7 [default = 0];
optional float min_negitive_scale = 8 [default = 0.5];
optional float max_negitive_scale = 9 [default = 1.5];
}
//***************add by xia******InterpLayer*********
message InterpParameter {
optional int32 height = 1 [default = 0]; // Height of output
optional int32 width = 2 [default = 0]; // Width of output
optional int32 zoom_factor = 3 [default = 1]; // zoom factor
optional int32 shrink_factor = 4 [default = 1]; // shrink factor
optional int32 pad_beg = 5 [default = 0]; // padding at begin of input
optional int32 pad_end = 6 [default = 0]; // padding at end of input
}
//*******************add by xia******rfcn********************************
message PSROIPoolingParameter {
required float spatial_scale = 1;
required int32 output_dim = 2; // output channel number
required int32 group_size = 3; // number of groups to encode position-sensitive score maps
}
//***************************************************
message FlipParameter {
optional bool flip_width = 1 [default = true];
optional bool flip_height = 2 [default = false];
}
message BNParameter {
optional FillerParameter slope_filler = 1;
optional FillerParameter bias_filler = 2;
optional float momentum = 3 [default = 0.9];
optional float eps = 4 [default = 1e-5];
// If true, will use the moving average mean and std for training and test.
// Will override the lr_param and freeze all the parameters.
// Make sure to initialize the layer properly with pretrained parameters.
optional bool frozen = 5 [default = false];
enum Engine {
DEFAULT = 0;
CAFFE = 1;
CUDNN = 2;
}
optional Engine engine = 6 [default = DEFAULT];
}
//************************add by xia*******************************
// Focal Loss for Dense Object Detection
message FocalLossParameter {
enum Type {
ORIGIN = 0; // FL(p_t) = -(1 - p_t) ^ gama * log(p_t), where p_t = p if y == 1 else 1 - p, whre p = sigmoid(x)
LINEAR = 1; // FL*(p_t) = -log(p_t) / gama, where p_t = sigmoid(gama * x_t + beta), where x_t = x * y, y is the ground truth label {-1, 1}
}
optional Type type = 1 [default = ORIGIN];
optional float gamma = 2 [default = 2];
// cross-categories weights to solve the imbalance problem
optional float alpha = 3 [default = 0.25];
optional float beta = 4 [default = 1.0];
}
//**************************FocalLoss****************************************
// Message that stores parameters used to apply transformation
// to the data layer's data
message TransformationParameter {
// For data pre-processing, we can do simple scaling and subtracting the
// data mean, if provided. Note that the mean subtraction is always carried
// out before scaling.
optional float scale = 1 [default = 1];
// Specify if we want to randomly mirror data.
optional bool mirror = 2 [default = false];
// Specify if we would like to randomly crop an image.
optional uint32 crop_size = 3 [default = 0];
optional uint32 crop_h = 11 [default = 0];
optional uint32 crop_w = 12 [default = 0];
// mean_file and mean_value cannot be specified at the same time
optional string mean_file = 4;
// if specified can be repeated once (would substract it from all the channels)
// or can be repeated the same number of times as channels
// (would subtract them from the corresponding channel)
repeated float mean_value = 5;
// Force the decoded image to have 3 color channels.
optional bool force_color = 6 [default = false];
// Force the decoded image to have 1 color channels.
optional bool force_gray = 7 [default = false];
// Resize policy
optional ResizeParameter resize_param = 8;
// Noise policy
optional NoiseParameter noise_param = 9;
// Distortion policy
optional DistortionParameter distort_param = 13;
// Expand policy
optional ExpansionParameter expand_param = 14;
// Constraint for emitting the annotation after transformation.
optional EmitConstraint emit_constraint = 10;
}
//*******************add by xia****ssd******************************************************
// Message that stores parameters used by data transformer for resize policy
message ResizeParameter {
//Probability of using this resize policy
optional float prob = 1 [default = 1];
enum Resize_mode {
WARP = 1;
FIT_SMALL_SIZE = 2;
FIT_LARGE_SIZE_AND_PAD = 3;
}
optional Resize_mode resize_mode = 2 [default = WARP];
optional uint32 height = 3 [default = 0];
optional uint32 width = 4 [default = 0];
// A parameter used to update bbox in FIT_SMALL_SIZE mode.
optional uint32 height_scale = 8 [default = 0];
optional uint32 width_scale = 9 [default = 0];
enum Pad_mode {
CONSTANT = 1;
MIRRORED = 2;
REPEAT_NEAREST = 3;
}
// Padding mode for BE_SMALL_SIZE_AND_PAD mode and object centering
optional Pad_mode pad_mode = 5 [default = CONSTANT];
// if specified can be repeated once (would fill all the channels)
// or can be repeated the same number of times as channels
// (would use it them to the corresponding channel)
repeated float pad_value = 6;
enum Interp_mode { //Same as in OpenCV
LINEAR = 1;
AREA = 2;
NEAREST = 3;
CUBIC = 4;
LANCZOS4 = 5;
}
//interpolation for for resizing
repeated Interp_mode interp_mode = 7;
}
message SaltPepperParameter {
//Percentage of pixels
optional float fraction = 1 [default = 0];
repeated float value = 2;
}
// Message that stores parameters used by data transformer for transformation
// policy
message NoiseParameter {
//Probability of using this resize policy
optional float prob = 1 [default = 0];
// Histogram equalized
optional bool hist_eq = 2 [default = false];
// Color inversion
optional bool inverse = 3 [default = false];
// Grayscale
optional bool decolorize = 4 [default = false];
// Gaussian blur
optional bool gauss_blur = 5 [default = false];
// JPEG compression quality (-1 = no compression)
optional float jpeg = 6 [default = -1];
// Posterization
optional bool posterize = 7 [default = false];
// Erosion
optional bool erode = 8 [default = false];
// Salt-and-pepper noise
optional bool saltpepper = 9 [default = false];
optional SaltPepperParameter saltpepper_param = 10;
// Local histogram equalization
optional bool clahe = 11 [default = false];
// Color space conversion
optional bool convert_to_hsv = 12 [default = false];
// Color space conversion
optional bool convert_to_lab = 13 [default = false];
}
// Message that stores parameters used by data transformer for distortion policy
message DistortionParameter {
// The probability of adjusting brightness.
optional float brightness_prob = 1 [default = 0.0];
// Amount to add to the pixel values within [-delta, delta].
// The possible value is within [0, 255]. Recommend 32.
optional float brightness_delta = 2 [default = 0.0];
// The probability of adjusting contrast.
optional float contrast_prob = 3 [default = 0.0];
// Lower bound for random contrast factor. Recommend 0.5.
optional float contrast_lower = 4 [default = 0.0];
// Upper bound for random contrast factor. Recommend 1.5.
optional float contrast_upper = 5 [default = 0.0];
// The probability of adjusting hue.
optional float hue_prob = 6 [default = 0.0];
// Amount to add to the hue channel within [-delta, delta].
// The possible value is within [0, 180]. Recommend 36.
optional float hue_delta = 7 [default = 0.0];
// The probability of adjusting saturation.
optional float saturation_prob = 8 [default = 0.0];
// Lower bound for the random saturation factor. Recommend 0.5.
optional float saturation_lower = 9 [default = 0.0];
// Upper bound for the random saturation factor. Recommend 1.5.
optional float saturation_upper = 10 [default = 0.0];
// The probability of randomly order the image channels.
optional float random_order_prob = 11 [default = 0.0];
}
// Message that stores parameters used by data transformer for expansion policy
message ExpansionParameter {
//Probability of using this expansion policy
optional float prob = 1 [default = 1];
// The ratio to expand the image.
optional float max_expand_ratio = 2 [default = 1.];
}
//**************************************************************************************************
// Message that stores parameters shared by loss layers
message LossParameter {
// If specified, ignore instances with the given label.
optional int32 ignore_label = 1;
// How to normalize the loss for loss layers that aggregate across batches,
// spatial dimensions, or other dimensions. Currently only implemented in
// SoftmaxWithLoss layer.
enum NormalizationMode {
// Divide by the number of examples in the batch times spatial dimensions.
// Outputs that receive the ignore label will NOT be ignored in computing
// the normalization factor.
FULL = 0;
// Divide by the total number of output locations that do not take the
// ignore_label. If ignore_label is not set, this behaves like FULL.
VALID = 1;
// Divide by the batch size.
BATCH_SIZE = 2;
// Do not normalize the loss.
NONE = 3;
}
optional NormalizationMode normalization = 3 [default = VALID];
// Deprecated. Ignored if normalization is specified. If normalization
// is not specified, then setting this to false will be equivalent to
// normalization = BATCH_SIZE to be consistent with previous behavior.
optional bool normalize = 2;
}
// Messages that store parameters used by individual layer types follow, in
// alphabetical order.
message AccuracyParameter {
// When computing accuracy, count as correct by comparing the true label to
// the top k scoring classes. By default, only compare to the top scoring
// class (i.e. argmax).
optional uint32 top_k = 1 [default = 1];
// The "label" axis of the prediction blob, whose argmax corresponds to the
// predicted label -- may be negative to index from the end (e.g., -1 for the
// last axis). For example, if axis == 1 and the predictions are
// (N x C x H x W), the label blob is expected to contain N*H*W ground truth
// labels with integer values in {0, 1, ..., C-1}.
optional int32 axis = 2 [default = 1];
// If specified, ignore instances with the given label.
optional int32 ignore_label = 3;
}
message ArgMaxParameter {
// If true produce pairs (argmax, maxval)
optional bool out_max_val = 1 [default = false];
optional uint32 top_k = 2 [default = 1];
// The axis along which to maximise -- may be negative to index from the
// end (e.g., -1 for the last axis).
// By default ArgMaxLayer maximizes over the flattened trailing dimensions
// for each index of the first / num dimension.
optional int32 axis = 3;
}
message ConcatParameter {
// The axis along which to concatenate -- may be negative to index from the
// end (e.g., -1 for the last axis). Other axes must have the
// same dimension for all the bottom blobs.
// By default, ConcatLayer concatenates blobs along the "channels" axis (1).
optional int32 axis = 2 [default = 1];
// DEPRECATED: alias for "axis" -- does not support negative indexing.
optional uint32 concat_dim = 1 [default = 1];
}
message BatchNormParameter {
// If false, accumulate global mean/variance values via a moving average. If
// true, use those accumulated values instead of computing mean/variance
// across the batch.
optional bool use_global_stats = 1;
// How much does the moving average decay each iteration?
optional float moving_average_fraction = 2 [default = .999];
// Small value to add to the variance estimate so that we don't divide by
// zero.
optional float eps = 3 [default = 1e-5];
}
message BiasParameter {
// The first axis of bottom[0] (the first input Blob) along which to apply
// bottom[1] (the second input Blob). May be negative to index from the end
// (e.g., -1 for the last axis).
//
// For example, if bottom[0] is 4D with shape 100x3x40x60, the output
// top[0] will have the same shape, and bottom[1] may have any of the
// following shapes (for the given value of axis):
// (axis == 0 == -4) 100; 100x3; 100x3x40; 100x3x40x60
// (axis == 1 == -3) 3; 3x40; 3x40x60
// (axis == 2 == -2) 40; 40x60
// (axis == 3 == -1) 60
// Furthermore, bottom[1] may have the empty shape (regardless of the value of
// "axis") -- a scalar bias.
optional int32 axis = 1 [default = 1];
// (num_axes is ignored unless just one bottom is given and the bias is
// a learned parameter of the layer. Otherwise, num_axes is determined by the
// number of axes by the second bottom.)
// The number of axes of the input (bottom[0]) covered by the bias
// parameter, or -1 to cover all axes of bottom[0] starting from `axis`.
// Set num_axes := 0, to add a zero-axis Blob: a scalar.
optional int32 num_axes = 2 [default = 1];
// (filler is ignored unless just one bottom is given and the bias is
// a learned parameter of the layer.)
// The initialization for the learned bias parameter.
// Default is the zero (0) initialization, resulting in the BiasLayer
// initially performing the identity operation.
optional FillerParameter filler = 3;
}
message ContrastiveLossParameter {
// margin for dissimilar pair
optional float margin = 1 [default = 1.0];
// The first implementation of this cost did not exactly match the cost of
// Hadsell et al 2006 -- using (margin - d^2) instead of (margin - d)^2.
// legacy_version = false (the default) uses (margin - d)^2 as proposed in the
// Hadsell paper. New models should probably use this version.
// legacy_version = true uses (margin - d^2). This is kept to support /
// reproduce existing models and results
optional bool legacy_version = 2 [default = false];
}
message DetectionLossParameter {
// Yolo detection loss layer
optional uint32 side = 1 [default = 7];
optional uint32 num_class = 2 [default = 20];
optional uint32 num_object = 3 [default = 2];
optional float object_scale = 4 [default = 1.0];
optional float noobject_scale = 5 [default = 0.5];
optional float class_scale = 6 [default = 1.0];
optional float coord_scale = 7 [default = 5.0];
optional bool sqrt = 8 [default = true];
optional bool constriant = 9 [default = false];
}
message RegionLossParameter{
//Yolo 9000
optional uint32 side = 1 [default = 13];
optional uint32 num_class = 2 [default = 20];
optional uint32 bias_match = 3 [default = 1];
optional uint32 coords = 4 [default = 4];
optional uint32 num = 5 [default = 5];
optional uint32 softmax = 6 [default = 1];
optional float jitter = 7 [default = 0.2];
optional uint32 rescore = 8 [default = 1];
optional float object_scale = 9 [default = 1.0];
optional float class_scale = 10 [default = 1.0];
optional float noobject_scale = 11 [default = 0.5];
optional float coord_scale = 12 [default = 5.0];
optional uint32 absolute = 13 [default = 1];
optional float thresh = 14 [default = 0.2];
optional uint32 random = 15 [default = 1];
repeated float biases = 16;
optional string softmax_tree = 17;
optional string class_map = 18;
}
message ReorgParameter {
optional uint32 stride = 1;
optional bool reverse = 2 [default = false];
}
message EvalDetectionParameter {
enum ScoreType {
OBJ = 0;
PROB = 1;
MULTIPLY = 2;
}
// Yolo detection evaluation layer
optional uint32 side = 1 [default = 7];
optional uint32 num_class = 2 [default = 20];
optional uint32 num_object = 3 [default = 2];
optional float threshold = 4 [default = 0.5];
optional bool sqrt = 5 [default = true];
optional bool constriant = 6 [default = true];
optional ScoreType score_type = 7 [default = MULTIPLY];
optional float nms = 8 [default = -1];
repeated float biases = 9;
}
message ConvolutionParameter {
optional uint32 num_output = 1; // The number of outputs for the layer
optional bool bias_term = 2 [default = true]; // whether to have bias terms
// Pad, kernel size, and stride are all given as a single value for equal
// dimensions in all spatial dimensions, or once per spatial dimension.
repeated uint32 pad = 3; // The padding size; defaults to 0
repeated uint32 kernel_size = 4; // The kernel size
repeated uint32 stride = 6; // The stride; defaults to 1
// Factor used to dilate the kernel, (implicitly) zero-filling the resulting
// holes. (Kernel dilation is sometimes referred to by its use in the
// algorithme à trous from Holschneider et al. 1987.)
repeated uint32 dilation = 18; // The dilation; defaults to 1
// For 2D convolution only, the *_h and *_w versions may also be used to
// specify both spatial dimensions.
optional uint32 pad_h = 9 [default = 0]; // The padding height (2D only)
optional uint32 pad_w = 10 [default = 0]; // The padding width (2D only)
optional uint32 kernel_h = 11; // The kernel height (2D only)
optional uint32 kernel_w = 12; // The kernel width (2D only)
optional uint32 stride_h = 13; // The stride height (2D only)
optional uint32 stride_w = 14; // The stride width (2D only)
optional uint32 group = 5 [default = 1]; // The group size for group conv
optional FillerParameter weight_filler = 7; // The filler for the weight
optional FillerParameter bias_filler = 8; // The filler for the bias
enum Engine {
DEFAULT = 0;
CAFFE = 1;
CUDNN = 2;
}
optional Engine engine = 15 [default = DEFAULT];
// The axis to interpret as "channels" when performing convolution.
// Preceding dimensions are treated as independent inputs;
// succeeding dimensions are treated as "spatial".
// With (N, C, H, W) inputs, and axis == 1 (the default), we perform
// N independent 2D convolutions, sliding C-channel (or (C/g)-channels, for
// groups g>1) filters across the spatial axes (H, W) of the input.
// With (N, C, D, H, W) inputs, and axis == 1, we perform
// N independent 3D convolutions, sliding (C/g)-channels
// filters across the spatial axes (D, H, W) of the input.
optional int32 axis = 16 [default = 1];
// Whether to force use of the general ND convolution, even if a specific
// implementation for blobs of the appropriate number of spatial dimensions
// is available. (Currently, there is only a 2D-specific convolution
// implementation; for input blobs with num_axes != 2, this option is
// ignored and the ND implementation will be used.)
optional bool force_nd_im2col = 17 [default = false];
}
message CropParameter {
// To crop, elements of the first bottom are selected to fit the dimensions
// of the second, reference bottom. The crop is configured by
// - the crop `axis` to pick the dimensions for cropping
// - the crop `offset` to set the shift for all/each dimension
// to align the cropped bottom with the reference bottom.
// All dimensions up to but excluding `axis` are preserved, while
// the dimensions including and trailing `axis` are cropped.
// If only one `offset` is set, then all dimensions are offset by this amount.
// Otherwise, the number of offsets must equal the number of cropped axes to
// shift the crop in each dimension accordingly.
// Note: standard dimensions are N,C,H,W so the default is a spatial crop,
// and `axis` may be negative to index from the end (e.g., -1 for the last
// axis).
optional int32 axis = 1 [default = 2];
repeated uint32 offset = 2;
}
message DataParameter {
enum DB {
LEVELDB = 0;
LMDB = 1;
}
// Specify the data source.
optional string source = 1;
// Specify the batch size.
optional uint32 batch_size = 4;
// The rand_skip variable is for the data layer to skip a few data points
// to avoid all asynchronous sgd clients to start at the same point. The skip
// point would be set as rand_skip * rand(0,1). Note that rand_skip should not
// be larger than the number of keys in the database.
// DEPRECATED. Each solver accesses a different subset of the database.
optional uint32 rand_skip = 7 [default = 0];
optional DB backend = 8 [default = LEVELDB];
// DEPRECATED. See TransformationParameter. For data pre-processing, we can do
// simple scaling and subtracting the data mean, if provided. Note that the
// mean subtraction is always carried out before scaling.
optional float scale = 2 [default = 1];
optional string mean_file = 3;
// DEPRECATED. See TransformationParameter. Specify if we would like to randomly
// crop an image.
optional uint32 crop_size = 5 [default = 0];
// DEPRECATED. See TransformationParameter. Specify if we want to randomly mirror
// data.
optional bool mirror = 6 [default = false];
// Force the encoded image to have 3 color channels
optional bool force_encoded_color = 9 [default = false];
// Prefetch queue (Number of batches to prefetch to host memory, increase if
// data access bandwidth varies).
optional uint32 prefetch = 10 [default = 4];
repeated uint32 side = 11;
}
//**********************************ssd*******************************************
// Message that store parameters used by DetectionEvaluateLayer
message DetectionEvaluateParameter {
// Number of classes that are actually predicted. Required!
optional uint32 num_classes = 1;
// Label id for background class. Needed for sanity check so that
// background class is neither in the ground truth nor the detections.
optional uint32 background_label_id = 2 [default = 0];
// Threshold for deciding true/false positive.
optional float overlap_threshold = 3 [default = 0.5];
// If true, also consider difficult ground truth for evaluation.
optional bool evaluate_difficult_gt = 4 [default = true];
// A file which contains a list of names and sizes with same order
// of the input DB. The file is in the following format:
// name height width
// ...
// If provided, we will scale the prediction and ground truth NormalizedBBox
// for evaluation.
optional string name_size_file = 5;
// The resize parameter used in converting NormalizedBBox to original image.
optional ResizeParameter resize_param = 6;
}
message NonMaximumSuppressionParameter {
// Threshold to be used in nms.
optional float nms_threshold = 1 [default = 0.3];
// Maximum number of results to be kept.
optional int32 top_k = 2;
// Parameter for adaptive nms.
optional float eta = 3 [default = 1.0];
}
message SaveOutputParameter {
// Output directory. If not empty, we will save the results.
optional string output_directory = 1;
// Output name prefix.
optional string output_name_prefix = 2;
// Output format.
// VOC - PASCAL VOC output format.
// COCO - MS COCO output format.
optional string output_format = 3;
// If you want to output results, must also provide the following two files.
// Otherwise, we will ignore saving results.
// label map file.
optional string label_map_file = 4;
// A file which contains a list of names and sizes with same order
// of the input DB. The file is in the following format:
// name height width
// ...
optional string name_size_file = 5;
// Number of test images. It can be less than the lines specified in
// name_size_file. For example, when we only want to evaluate on part
// of the test images.
optional uint32 num_test_image = 6;
// The resize parameter used in saving the data.
optional ResizeParameter resize_param = 7;
}
// Message that store parameters used by DetectionOutputLayer
message DetectionOutputParameter {
// Number of classes to be predicted. Required!
optional uint32 num_classes = 1;
// If true, bounding box are shared among different classes.
optional bool share_location = 2 [default = true];
// Background label id. If there is no background class,
// set it as -1.
optional int32 background_label_id = 3 [default = 0];
// Parameters used for non maximum suppression.
optional NonMaximumSuppressionParameter nms_param = 4;
// Parameters used for saving detection results.
optional SaveOutputParameter save_output_param = 5;
// Type of coding method for bbox.
optional PriorBoxParameter.CodeType code_type = 6 [default = CORNER];
// If true, variance is encoded in target; otherwise we need to adjust the
// predicted offset accordingly.
optional bool variance_encoded_in_target = 8 [default = false];
// Number of total bboxes to be kept per image after nms step.
// -1 means keeping all bboxes after nms step.
optional int32 keep_top_k = 7 [default = -1];
// Only consider detections whose confidences are larger than a threshold.
// If not provided, consider all boxes.
optional float confidence_threshold = 9;
// If true, visualize the detection results.
optional bool visualize = 10 [default = false];
// The threshold used to visualize the detection results.
optional float visualize_threshold = 11;
// If provided, save outputs to video file.
optional string save_file = 12;
}
//*******************************************************************************
message DropoutParameter {
optional float dropout_ratio = 1 [default = 0.5]; // dropout ratio
optional bool scale_train = 2 [default = true]; // scale train or test phase
}
// DummyDataLayer fills any number of arbitrarily shaped blobs with random
// (or constant) data generated by "Fillers" (see "message FillerParameter").
message DummyDataParameter {
// This layer produces N >= 1 top blobs. DummyDataParameter must specify 1 or N
// shape fields, and 0, 1 or N data_fillers.
//
// If 0 data_fillers are specified, ConstantFiller with a value of 0 is used.
// If 1 data_filler is specified, it is applied to all top blobs. If N are
// specified, the ith is applied to the ith top blob.
repeated FillerParameter data_filler = 1;
repeated BlobShape shape = 6;
// 4D dimensions -- deprecated. Use "shape" instead.
repeated uint32 num = 2;
repeated uint32 channels = 3;
repeated uint32 height = 4;
repeated uint32 width = 5;
}
message EltwiseParameter {
enum EltwiseOp {
PROD = 0;
SUM = 1;
MAX = 2;
}
optional EltwiseOp operation = 1 [default = SUM]; // element-wise operation
repeated float coeff = 2; // blob-wise coefficient for SUM operation
// Whether to use an asymptotically slower (for >2 inputs) but stabler method
// of computing the gradient for the PROD operation. (No effect for SUM op.)
optional bool stable_prod_grad = 3 [default = true];
}
// Message that stores parameters used by ELULayer
message ELUParameter {
// Described in:
// Clevert, D.-A., Unterthiner, T., & Hochreiter, S. (2015). Fast and Accurate
// Deep Network Learning by Exponential Linear Units (ELUs). arXiv
optional float alpha = 1 [default = 1];
}
// Message that stores parameters used by EmbedLayer
message EmbedParameter {
optional uint32 num_output = 1; // The number of outputs for the layer
// The input is given as integers to be interpreted as one-hot
// vector indices with dimension num_input. Hence num_input should be
// 1 greater than the maximum possible input value.
optional uint32 input_dim = 2;
optional bool bias_term = 3 [default = true]; // Whether to use a bias term
optional FillerParameter weight_filler = 4; // The filler for the weight
optional FillerParameter bias_filler = 5; // The filler for the bias
}
// Message that stores parameters used by ExpLayer
message ExpParameter {
// ExpLayer computes outputs y = base ^ (shift + scale * x), for base > 0.
// Or if base is set to the default (-1), base is set to e,
// so y = exp(shift + scale * x).
optional float base = 1 [default = -1.0];
optional float scale = 2 [default = 1.0];
optional float shift = 3 [default = 0.0];
}
/// Message that stores parameters used by FlattenLayer
message FlattenParameter {
// The first axis to flatten: all preceding axes are retained in the output.
// May be negative to index from the end (e.g., -1 for the last axis).
optional int32 axis = 1 [default = 1];
// The last axis to flatten: all following axes are retained in the output.
// May be negative to index from the end (e.g., the default -1 for the last
// axis).
optional int32 end_axis = 2 [default = -1];
}
// Message that stores parameters used by HDF5DataLayer
message HDF5DataParameter {
// Specify the data source.
optional string source = 1;
// Specify the batch size.
optional uint32 batch_size = 2;
// Specify whether to shuffle the data.
// If shuffle == true, the ordering of the HDF5 files is shuffled,
// and the ordering of data within any given HDF5 file is shuffled,
// but data between different files are not interleaved; all of a file's
// data are output (in a random order) before moving onto another file.
optional bool shuffle = 3 [default = false];
}
message HDF5OutputParameter {
optional string file_name = 1;
}
message HingeLossParameter {
enum Norm {
L1 = 1;
L2 = 2;
}
// Specify the Norm to use L1 or L2
optional Norm norm = 1 [default = L1];
}
message ImageDataParameter {
// Specify the data source.
optional string source = 1;
// Specify the batch size.
optional uint32 batch_size = 4 [default = 1];
// The rand_skip variable is for the data layer to skip a few data points
// to avoid all asynchronous sgd clients to start at the same point. The skip
// point would be set as rand_skip * rand(0,1). Note that rand_skip should not
// be larger than the number of keys in the database.
optional uint32 rand_skip = 7 [default = 0];
// Whether or not ImageLayer should shuffle the list of files at every epoch.
optional bool shuffle = 8 [default = false];
// It will also resize images if new_height or new_width are not zero.
optional uint32 new_height = 9 [default = 0];
optional uint32 new_width = 10 [default = 0];
// Specify if the images are color or gray
optional bool is_color = 11 [default = true];
// DEPRECATED. See TransformationParameter. For data pre-processing, we can do
// simple scaling and subtracting the data mean, if provided. Note that the
// mean subtraction is always carried out before scaling.
optional float scale = 2 [default = 1];
optional string mean_file = 3;
// DEPRECATED. See TransformationParameter. Specify if we would like to randomly
// crop an image.
optional uint32 crop_size = 5 [default = 0];
// DEPRECATED. See TransformationParameter. Specify if we want to randomly mirror
// data.
optional bool mirror = 6 [default = false];
optional string root_folder = 12 [default = ""];
}
message InfogainLossParameter {
// Specify the infogain matrix source.
optional string source = 1;
}
message InnerProductParameter {
optional uint32 num_output = 1; // The number of outputs for the layer
optional bool bias_term = 2 [default = true]; // whether to have bias terms
optional FillerParameter weight_filler = 3; // The filler for the weight
optional FillerParameter bias_filler = 4; // The filler for the bias
// The first axis to be lumped into a single inner product computation;
// all preceding axes are retained in the output.
// May be negative to index from the end (e.g., -1 for the last axis).
optional int32 axis = 5 [default = 1];
// Specify whether to transpose the weight matrix or not.
// If transpose == true, any operations will be performed on the transpose
// of the weight matrix. The weight matrix itself is not going to be transposed
// but rather the transfer flag of operations will be toggled accordingly.
optional bool transpose = 6 [default = false];
optional bool normalize = 7 [default = false];
}
message InputParameter {
// This layer produces N >= 1 top blob(s) to be assigned manually.
// Define N shapes to set a shape for each top.
// Define 1 shape to set the same shape for every top.
// Define no shape to defer to reshaping manually.
repeated BlobShape shape = 1;
}
// Message that stores parameters used by LogLayer
message LogParameter {
// LogLayer computes outputs y = log_base(shift + scale * x), for base > 0.
// Or if base is set to the default (-1), base is set to e,
// so y = ln(shift + scale * x) = log_e(shift + scale * x)
optional float base = 1 [default = -1.0];
optional float scale = 2 [default = 1.0];
optional float shift = 3 [default = 0.0];
}
// Message that stores parameters used by LRNLayer
message LRNParameter {
optional uint32 local_size = 1 [default = 5];
optional float alpha = 2 [default = 1.];
optional float beta = 3 [default = 0.75];
enum NormRegion {
ACROSS_CHANNELS = 0;
WITHIN_CHANNEL = 1;
}
optional NormRegion norm_region = 4 [default = ACROSS_CHANNELS];
optional float k = 5 [default = 1.];
enum Engine {
DEFAULT = 0;
CAFFE = 1;
CUDNN = 2;
}
optional Engine engine = 6 [default = DEFAULT];
}
message MemoryDataParameter {
optional uint32 batch_size = 1;
optional uint32 channels = 2;
optional uint32 height = 3;
optional uint32 width = 4;
}
//**************************ssd********************************************
// Message that store parameters used by MultiBoxLossLayer
message MultiBoxLossParameter {
// Localization loss type.
enum LocLossType {
L2 = 0;
SMOOTH_L1 = 1;
}
optional LocLossType loc_loss_type = 1 [default = SMOOTH_L1];
// Confidence loss type.
enum ConfLossType {
SOFTMAX = 0;
LOGISTIC = 1;
}
optional ConfLossType conf_loss_type = 2 [default = SOFTMAX];
// Weight for localization loss.
optional float loc_weight = 3 [default = 1.0];
// Number of classes to be predicted. Required!
optional uint32 num_classes = 4;
// If true, bounding box are shared among different classes.
optional bool share_location = 5 [default = true];
// Matching method during training.
enum MatchType {
BIPARTITE = 0;
PER_PREDICTION = 1;
}
optional MatchType match_type = 6 [default = PER_PREDICTION];
// If match_type is PER_PREDICTION, use overlap_threshold to
// determine the extra matching bboxes.
optional float overlap_threshold = 7 [default = 0.5];
// Use prior for matching.
optional bool use_prior_for_matching = 8 [default = true];
// Background label id.
optional uint32 background_label_id = 9 [default = 0];
// If true, also consider difficult ground truth.
optional bool use_difficult_gt = 10 [default = true];
// If true, perform negative mining.
// DEPRECATED: use mining_type instead.
optional bool do_neg_mining = 11;
// The negative/positive ratio.
optional float neg_pos_ratio = 12 [default = 3.0];
// The negative overlap upperbound for the unmatched predictions.
optional float neg_overlap = 13 [default = 0.5];
// Type of coding method for bbox.
optional PriorBoxParameter.CodeType code_type = 14 [default = CORNER];
// If true, encode the variance of prior box in the loc loss target instead of
// in bbox.
optional bool encode_variance_in_target = 16 [default = false];
// If true, map all object classes to agnostic class. It is useful for learning
// objectness detector.
optional bool map_object_to_agnostic = 17 [default = false];
// If true, ignore cross boundary bbox during matching.
// Cross boundary bbox is a bbox who is outside of the image region.
optional bool ignore_cross_boundary_bbox = 18 [default = false];
// If true, only backpropagate on corners which are inside of the image
// region when encode_type is CORNER or CORNER_SIZE.
optional bool bp_inside = 19 [default = false];
// Mining type during training.
// NONE : use all negatives.
// MAX_NEGATIVE : select negatives based on the score.
// HARD_EXAMPLE : select hard examples based on "Training Region-based Object Detectors with Online Hard Example Mining", Shrivastava et.al.
enum MiningType {
NONE = 0;
MAX_NEGATIVE = 1;
HARD_EXAMPLE = 2;
}
optional MiningType mining_type = 20 [default = MAX_NEGATIVE];
// Parameters used for non maximum suppression durig hard example mining.
optional NonMaximumSuppressionParameter nms_param = 21;
optional int32 sample_size = 22 [default = 64];
optional bool use_prior_for_nms = 23 [default = false];
}
// Message that stores parameters used by NormalizeLayer
//message NormalizeParameter {
// //optional bool across_spatial = 1 [default = true];
// // Initial value of scale. Default is 1.0 for all
// //optional FillerParameter scale_filler = 2;
// // Whether or not scale parameters are shared across channels.
// //optional bool channel_shared = 3 [default = true];
// // Epsilon for not dividing by zero while normalizing variance
// //optional float eps = 4 [default = 1e-10];
// //**************************************************
// optional string normalize_type = 1 [default = "L2"];
// optional bool fix_gradient = 2 [default = false];
// optional bool bp_norm = 3 [default = false];
//}
message PermuteParameter {
// The new orders of the axes of data. Notice it should be with
// in the same range as the input data, and it starts from 0.
// Do not provide repeated order.
repeated uint32 order = 1;
}
//**************************end***********************************************
message MVNParameter {
// This parameter can be set to false to normalize mean only
optional bool normalize_variance = 1 [default = true];
// This parameter can be set to true to perform DNN-like MVN
optional bool across_channels = 2 [default = false];
// Epsilon for not dividing by zero while normalizing variance
optional float eps = 3 [default = 1e-9];
}
message ParameterParameter {
optional BlobShape shape = 1;
}
message PoolingParameter {
enum PoolMethod {
MAX = 0;
AVE = 1;
STOCHASTIC = 2;
}
optional PoolMethod pool = 1 [default = MAX]; // The pooling method
// Pad, kernel size, and stride are all given as a single value for equal
// dimensions in height and width or as Y, X pairs.
optional uint32 pad = 4 [default = 0]; // The padding size (equal in Y, X)
optional uint32 pad_h = 9 [default = 0]; // The padding height
optional uint32 pad_w = 10 [default = 0]; // The padding width
optional uint32 kernel_size = 2; // The kernel size (square)
optional uint32 kernel_h = 5; // The kernel height
optional uint32 kernel_w = 6; // The kernel width
optional uint32 stride = 3 [default = 1]; // The stride (equal in Y, X)
optional uint32 stride_h = 7; // The stride height
optional uint32 stride_w = 8; // The stride width
enum Engine {
DEFAULT = 0;
CAFFE = 1;
CUDNN = 2;
}
optional Engine engine = 11 [default = DEFAULT];
// If global_pooling then it will pool over the size of the bottom by doing
// kernel_h = bottom->height and kernel_w = bottom->width
optional bool global_pooling = 12 [default = false];
///////////////////////
// Specify floor/ceil mode
optional bool ceil_mode = 13 [default = true];
///////////////////////////////
}
message PowerParameter {
// PowerLayer computes outputs y = (shift + scale * x) ^ power.
optional float power = 1 [default = 1.0];
optional float scale = 2 [default = 1.0];
optional float shift = 3 [default = 0.0];
}
//*************ssd********************************************************************
// Message that store parameters used by PriorBoxLayer
message PriorBoxParameter {
// Encode/decode type.
enum CodeType {
CORNER = 1;
CENTER_SIZE = 2;
CORNER_SIZE = 3;
}
// Minimum box size (in pixels). Required!
repeated float min_size = 1;
// Maximum box size (in pixels). Required!
repeated float max_size = 2;
// Various of aspect ratios. Duplicate ratios will be ignored.
// If none is provided, we use default ratio 1.
repeated float aspect_ratio = 3;
// If true, will flip each aspect ratio.
// For example, if there is aspect ratio "r",
// we will generate aspect ratio "1.0/r" as well.
optional bool flip = 4 [default = true];
// If true, will clip the prior so that it is within [0, 1]
optional bool clip = 5 [default = false];
// Variance for adjusting the prior bboxes.
repeated float variance = 6;
// By default, we calculate img_height, img_width, step_x, step_y based on
// bottom[0] (feat) and bottom[1] (img). Unless these values are explicitely
// provided.
// Explicitly provide the img_size.
optional uint32 img_size = 7;
// Either img_size or img_h/img_w should be specified; not both.
optional uint32 img_h = 8;
optional uint32 img_w = 9;
// Explicitly provide the step size.
optional float step = 10;
// Either step or step_h/step_w should be specified; not both.
optional float step_h = 11;
optional float step_w = 12;
// Offset to the top left corner of each cell.
optional float offset = 13 [default = 0.5];
}
//*********************************************************************************
message PythonParameter {
optional string module = 1;
optional string layer = 2;
// This value is set to the attribute `param_str` of the `PythonLayer` object
// in Python before calling the `setup()` method. This could be a number,
// string, dictionary in Python dict format, JSON, etc. You may parse this
// string in `setup` method and use it in `forward` and `backward`.
optional string param_str = 3 [default = ''];
// Whether this PythonLayer is shared among worker solvers during data parallelism.
// If true, each worker solver sequentially run forward from this layer.
// This value should be set true if you are using it as a data layer.
optional bool share_in_parallel = 4 [default = false];
}
message RecurrentParameter {
// The dimension of the output (and usually hidden state) representation --
// must be explicitly set to non-zero.
optional uint32 num_output = 1 [default = 0];
optional FillerParameter weight_filler = 2; // The filler for the weight
optional FillerParameter bias_filler = 3; // The filler for the bias
// Whether to enable displaying debug_info in the unrolled recurrent net.
optional bool debug_info = 4 [default = false];
// Whether to add as additional inputs (bottoms) the initial hidden state
// blobs, and add as additional outputs (tops) the final timestep hidden state
// blobs. The number of additional bottom/top blobs required depends on the
// recurrent architecture -- e.g., 1 for RNNs, 2 for LSTMs.
optional bool expose_hidden = 5 [default = false];
}
// Message that stores parameters used by ReductionLayer
message ReductionParameter {
enum ReductionOp {
SUM = 1;
ASUM = 2;
SUMSQ = 3;
MEAN = 4;
}
optional ReductionOp operation = 1 [default = SUM]; // reduction operation
// The first axis to reduce to a scalar -- may be negative to index from the
// end (e.g., -1 for the last axis).
// (Currently, only reduction along ALL "tail" axes is supported; reduction
// of axis M through N, where N < num_axes - 1, is unsupported.)
// Suppose we have an n-axis bottom Blob with shape:
// (d0, d1, d2, ..., d(m-1), dm, d(m+1), ..., d(n-1)).
// If axis == m, the output Blob will have shape
// (d0, d1, d2, ..., d(m-1)),
// and the ReductionOp operation is performed (d0 * d1 * d2 * ... * d(m-1))
// times, each including (dm * d(m+1) * ... * d(n-1)) individual data.
// If axis == 0 (the default), the output Blob always has the empty shape
// (count 1), performing reduction across the entire input --
// often useful for creating new loss functions.
optional int32 axis = 2 [default = 0];
optional float coeff = 3 [default = 1.0]; // coefficient for output
}
// Message that stores parameters used by ReLULayer
message ReLUParameter {
// Allow non-zero slope for negative inputs to speed up optimization
// Described in:
// Maas, A. L., Hannun, A. Y., & Ng, A. Y. (2013). Rectifier nonlinearities
// improve neural network acoustic models. In ICML Workshop on Deep Learning
// for Audio, Speech, and Language Processing.
optional float negative_slope = 1 [default = 0];
enum Engine {
DEFAULT = 0;
CAFFE = 1;
CUDNN = 2;
}
optional Engine engine = 2 [default = DEFAULT];
}
message ReshapeParameter {
// Specify the output dimensions. If some of the dimensions are set to 0,
// the corresponding dimension from the bottom layer is used (unchanged).
// Exactly one dimension may be set to -1, in which case its value is
// inferred from the count of the bottom blob and the remaining dimensions.
// For example, suppose we want to reshape a 2D blob "input" with shape 2 x 8:
//
// layer {
// type: "Reshape" bottom: "input" top: "output"
// reshape_param { ... }
// }
//
// If "input" is 2D with shape 2 x 8, then the following reshape_param
// specifications are all equivalent, producing a 3D blob "output" with shape
// 2 x 2 x 4:
//
// reshape_param { shape { dim: 2 dim: 2 dim: 4 } }
// reshape_param { shape { dim: 0 dim: 2 dim: 4 } }
// reshape_param { shape { dim: 0 dim: 2 dim: -1 } }
// reshape_param { shape { dim: -1 dim: 0 dim: 2 } }
//
optional BlobShape shape = 1;
// axis and num_axes control the portion of the bottom blob's shape that are
// replaced by (included in) the reshape. By default (axis == 0 and
// num_axes == -1), the entire bottom blob shape is included in the reshape,
// and hence the shape field must specify the entire output shape.
//
// axis may be non-zero to retain some portion of the beginning of the input
// shape (and may be negative to index from the end; e.g., -1 to begin the
// reshape after the last axis, including nothing in the reshape,
// -2 to include only the last axis, etc.).
//
// For example, suppose "input" is a 2D blob with shape 2 x 8.
// Then the following ReshapeLayer specifications are all equivalent,
// producing a blob "output" with shape 2 x 2 x 4:
//
// reshape_param { shape { dim: 2 dim: 2 dim: 4 } }
// reshape_param { shape { dim: 2 dim: 4 } axis: 1 }
// reshape_param { shape { dim: 2 dim: 4 } axis: -3 }
//
// num_axes specifies the extent of the reshape.
// If num_axes >= 0 (and axis >= 0), the reshape will be performed only on
// input axes in the range [axis, axis+num_axes].
// num_axes may also be -1, the default, to include all remaining axes
// (starting from axis).
//
// For example, suppose "input" is a 2D blob with shape 2 x 8.
// Then the following ReshapeLayer specifications are equivalent,
// producing a blob "output" with shape 1 x 2 x 8.
//
// reshape_param { shape { dim: 1 dim: 2 dim: 8 } }
// reshape_param { shape { dim: 1 dim: 2 } num_axes: 1 }
// reshape_param { shape { dim: 1 } num_axes: 0 }
//
// On the other hand, these would produce output blob shape 2 x 1 x 8:
//
// reshape_param { shape { dim: 2 dim: 1 dim: 8 } }
// reshape_param { shape { dim: 1 } axis: 1 num_axes: 0 }
//
optional int32 axis = 2 [default = 0];
optional int32 num_axes = 3 [default = -1];
}
// Message that stores parameters used by ROIPoolingLayer
message ROIPoolingParameter {
// Pad, kernel size, and stride are all given as a single value for equal
// dimensions in height and width or as Y, X pairs.
optional uint32 pooled_h = 1 [default = 0]; // The pooled output height
optional uint32 pooled_w = 2 [default = 0]; // The pooled output width
// Multiplicative spatial scale factor to translate ROI coords from their
// input scale to the scale used when pooling
optional float spatial_scale = 3 [default = 1];
}
message ScaleParameter {
// The first axis of bottom[0] (the first input Blob) along which to apply
// bottom[1] (the second input Blob). May be negative to index from the end
// (e.g., -1 for the last axis).
//
// For example, if bottom[0] is 4D with shape 100x3x40x60, the output
// top[0] will have the same shape, and bottom[1] may have any of the
// following shapes (for the given value of axis):
// (axis == 0 == -4) 100; 100x3; 100x3x40; 100x3x40x60
// (axis == 1 == -3) 3; 3x40; 3x40x60
// (axis == 2 == -2) 40; 40x60
// (axis == 3 == -1) 60
// Furthermore, bottom[1] may have the empty shape (regardless of the value of
// "axis") -- a scalar multiplier.
optional int32 axis = 1 [default = 1];
// (num_axes is ignored unless just one bottom is given and the scale is
// a learned parameter of the layer. Otherwise, num_axes is determined by the
// number of axes by the second bottom.)
// The number of axes of the input (bottom[0]) covered by the scale
// parameter, or -1 to cover all axes of bottom[0] starting from `axis`.
// Set num_axes := 0, to multiply with a zero-axis Blob: a scalar.
optional int32 num_axes = 2 [default = 1];
// (filler is ignored unless just one bottom is given and the scale is
// a learned parameter of the layer.)
// The initialization for the learned scale parameter.
// Default is the unit (1) initialization, resulting in the ScaleLayer
// initially performing the identity operation.
optional FillerParameter filler = 3;
// Whether to also learn a bias (equivalent to a ScaleLayer+BiasLayer, but
// may be more efficient). Initialized with bias_filler (defaults to 0).
optional bool bias_term = 4 [default = false];
optional FillerParameter bias_filler = 5;
optional float min_value = 6;
optional float max_value = 7;
}
message SigmoidParameter {
enum Engine {
DEFAULT = 0;
CAFFE = 1;
CUDNN = 2;
}
optional Engine engine = 1 [default = DEFAULT];
}
message SmoothL1LossParameter {
// SmoothL1Loss(x) =
// 0.5 * (sigma * x) ** 2 -- if x < 1.0 / sigma / sigma
// |x| - 0.5 / sigma / sigma -- otherwise
optional float sigma = 1 [default = 1];
}
message SliceParameter {
// The axis along which to slice -- may be negative to index from the end
// (e.g., -1 for the last axis).
// By default, SliceLayer concatenates blobs along the "channels" axis (1).
optional int32 axis = 3 [default = 1];
repeated uint32 slice_point = 2;
// DEPRECATED: alias for "axis" -- does not support negative indexing.
optional uint32 slice_dim = 1 [default = 1];
}
// Message that stores parameters used by SoftmaxLayer, SoftmaxWithLossLayer
message SoftmaxParameter {
enum Engine {
DEFAULT = 0;
CAFFE = 1;
CUDNN = 2;
}
optional Engine engine = 1 [default = DEFAULT];
// The axis along which to perform the softmax -- may be negative to index
// from the end (e.g., -1 for the last axis).
// Any other axes will be evaluated as independent softmaxes.
optional int32 axis = 2 [default = 1];
}
message TanHParameter {
enum Engine {
DEFAULT = 0;
CAFFE = 1;
CUDNN = 2;
}
optional Engine engine = 1 [default = DEFAULT];
}
// Message that stores parameters used by TileLayer
message TileParameter {
// The index of the axis to tile.
optional int32 axis = 1 [default = 1];
// The number of copies (tiles) of the blob to output.
optional int32 tiles = 2;
}
// Message that stores parameters used by ThresholdLayer
message ThresholdParameter {
optional float threshold = 1 [default = 0]; // Strictly positive values
}
message WindowDataParameter {
// Specify the data source.
optional string source = 1;
// For data pre-processing, we can do simple scaling and subtracting the
// data mean, if provided. Note that the mean subtraction is always carried
// out before scaling.
optional float scale = 2 [default = 1];
optional string mean_file = 3;
// Specify the batch size.
optional uint32 batch_size = 4;
// Specify if we would like to randomly crop an image.
optional uint32 crop_size = 5 [default = 0];
// Specify if we want to randomly mirror data.
optional bool mirror = 6 [default = false];
// Foreground (object) overlap threshold
optional float fg_threshold = 7 [default = 0.5];
// Background (non-object) overlap threshold
optional float bg_threshold = 8 [default = 0.5];
// Fraction of batch that should be foreground objects
optional float fg_fraction = 9 [default = 0.25];
// Amount of contextual padding to add around a window
// (used only by the window_data_layer)
optional uint32 context_pad = 10 [default = 0];
// Mode for cropping out a detection window
// warp: cropped window is warped to a fixed size and aspect ratio
// square: the tightest square around the window is cropped
optional string crop_mode = 11 [default = "warp"];
// cache_images: will load all images in memory for faster access
optional bool cache_images = 12 [default = false];
// append root_folder to locate images
optional string root_folder = 13 [default = ""];
}
message SPPParameter {
enum PoolMethod {
MAX = 0;
AVE = 1;
STOCHASTIC = 2;
}
optional uint32 pyramid_height = 1;
optional PoolMethod pool = 2 [default = MAX]; // The pooling method
enum Engine {
DEFAULT = 0;
CAFFE = 1;
CUDNN = 2;
}
optional Engine engine = 6 [default = DEFAULT];
}
// DEPRECATED: use LayerParameter.
message V1LayerParameter {
repeated string bottom = 2;
repeated string top = 3;
optional string name = 4;
repeated NetStateRule include = 32;
repeated NetStateRule exclude = 33;
enum LayerType {
NONE = 0;
ABSVAL = 35;
ACCURACY = 1;
ARGMAX = 30;
BNLL = 2;
CONCAT = 3;
CONTRASTIVE_LOSS = 37;
CONVOLUTION = 4;
DATA = 5;
DECONVOLUTION = 39;
DROPOUT = 6;
DUMMY_DATA = 32;
EUCLIDEAN_LOSS = 7;
ELTWISE = 25;
EXP = 38;
FLATTEN = 8;
HDF5_DATA = 9;
HDF5_OUTPUT = 10;
HINGE_LOSS = 28;
IM2COL = 11;
IMAGE_DATA = 12;
INFOGAIN_LOSS = 13;
INNER_PRODUCT = 14;
LRN = 15;
MEMORY_DATA = 29;
MULTINOMIAL_LOGISTIC_LOSS = 16;
MVN = 34;
POOLING = 17;
POWER = 26;
RELU = 18;
SIGMOID = 19;
SIGMOID_CROSS_ENTROPY_LOSS = 27;
SILENCE = 36;
SOFTMAX = 20;
SOFTMAX_LOSS = 21;
SPLIT = 22;
SLICE = 33;
TANH = 23;
WINDOW_DATA = 24;
THRESHOLD = 31;
}
optional LayerType type = 5;
repeated BlobProto blobs = 6;
repeated string param = 1001;
repeated DimCheckMode blob_share_mode = 1002;
enum DimCheckMode {
STRICT = 0;
PERMISSIVE = 1;
}
repeated float blobs_lr = 7;
repeated float weight_decay = 8;
repeated float loss_weight = 35;
optional AccuracyParameter accuracy_param = 27;
optional ArgMaxParameter argmax_param = 23;
optional ConcatParameter concat_param = 9;
optional ContrastiveLossParameter contrastive_loss_param = 40;
optional ConvolutionParameter convolution_param = 10;
optional DataParameter data_param = 11;
optional DropoutParameter dropout_param = 12;
optional DummyDataParameter dummy_data_param = 26;
optional EltwiseParameter eltwise_param = 24;
optional ExpParameter exp_param = 41;
optional HDF5DataParameter hdf5_data_param = 13;
optional HDF5OutputParameter hdf5_output_param = 14;
optional HingeLossParameter hinge_loss_param = 29;
optional ImageDataParameter image_data_param = 15;
optional InfogainLossParameter infogain_loss_param = 16;
optional InnerProductParameter inner_product_param = 17;
optional LRNParameter lrn_param = 18;
optional MemoryDataParameter memory_data_param = 22;
optional MVNParameter mvn_param = 34;
optional PoolingParameter pooling_param = 19;
optional PowerParameter power_param = 21;
optional ReLUParameter relu_param = 30;
optional SigmoidParameter sigmoid_param = 38;
optional SoftmaxParameter softmax_param = 39;
optional SliceParameter slice_param = 31;
optional TanHParameter tanh_param = 37;
optional ThresholdParameter threshold_param = 25;
optional WindowDataParameter window_data_param = 20;
optional TransformationParameter transform_param = 36;
optional LossParameter loss_param = 42;
optional DetectionLossParameter detection_loss_param = 200;
optional EvalDetectionParameter eval_detection_param = 201;
optional V0LayerParameter layer = 1;
}
// DEPRECATED: V0LayerParameter is the old way of specifying layer parameters
// in Caffe. We keep this message type around for legacy support.
message V0LayerParameter {
optional string name = 1; // the layer name
optional string type = 2; // the string to specify the layer type
// Parameters to specify layers with inner products.
optional uint32 num_output = 3; // The number of outputs for the layer
optional bool biasterm = 4 [default = true]; // whether to have bias terms
optional FillerParameter weight_filler = 5; // The filler for the weight
optional FillerParameter bias_filler = 6; // The filler for the bias
optional uint32 pad = 7 [default = 0]; // The padding size
optional uint32 kernelsize = 8; // The kernel size
optional uint32 group = 9 [default = 1]; // The group size for group conv
optional uint32 stride = 10 [default = 1]; // The stride
enum PoolMethod {
MAX = 0;
AVE = 1;
STOCHASTIC = 2;
}
optional PoolMethod pool = 11 [default = MAX]; // The pooling method
optional float dropout_ratio = 12 [default = 0.5]; // dropout ratio
optional uint32 local_size = 13 [default = 5]; // for local response norm
optional float alpha = 14 [default = 1.]; // for local response norm
optional float beta = 15 [default = 0.75]; // for local response norm
optional float k = 22 [default = 1.];
// For data layers, specify the data source
optional string source = 16;
// For data pre-processing, we can do simple scaling and subtracting the
// data mean, if provided. Note that the mean subtraction is always carried
// out before scaling.
optional float scale = 17 [default = 1];
optional string meanfile = 18;
// For data layers, specify the batch size.
optional uint32 batchsize = 19;
// For data layers, specify if we would like to randomly crop an image.
optional uint32 cropsize = 20 [default = 0];
// For data layers, specify if we want to randomly mirror data.
optional bool mirror = 21 [default = false];
// The blobs containing the numeric parameters of the layer
repeated BlobProto blobs = 50;
// The ratio that is multiplied on the global learning rate. If you want to
// set the learning ratio for one blob, you need to set it for all blobs.
repeated float blobs_lr = 51;
// The weight decay that is multiplied on the global weight decay.
repeated float weight_decay = 52;
// The rand_skip variable is for the data layer to skip a few data points
// to avoid all asynchronous sgd clients to start at the same point. The skip
// point would be set as rand_skip * rand(0,1). Note that rand_skip should not
// be larger than the number of keys in the database.
optional uint32 rand_skip = 53 [default = 0];
// Fields related to detection (det_*)
// foreground (object) overlap threshold
optional float det_fg_threshold = 54 [default = 0.5];
// background (non-object) overlap threshold
optional float det_bg_threshold = 55 [default = 0.5];
// Fraction of batch that should be foreground objects
optional float det_fg_fraction = 56 [default = 0.25];
// optional bool OBSOLETE_can_clobber = 57 [default = true];
// Amount of contextual padding to add around a window
// (used only by the window_data_layer)
optional uint32 det_context_pad = 58 [default = 0];
// Mode for cropping out a detection window
// warp: cropped window is warped to a fixed size and aspect ratio
// square: the tightest square around the window is cropped
optional string det_crop_mode = 59 [default = "warp"];
// For ReshapeLayer, one needs to specify the new dimensions.
optional int32 new_num = 60 [default = 0];
optional int32 new_channels = 61 [default = 0];
optional int32 new_height = 62 [default = 0];
optional int32 new_width = 63 [default = 0];
// Whether or not ImageLayer should shuffle the list of files at every epoch.
// It will also resize images if new_height or new_width are not zero.
optional bool shuffle_images = 64 [default = false];
// For ConcatLayer, one needs to specify the dimension for concatenation, and
// the other dimensions must be the same for all the bottom blobs.
// By default it will concatenate blobs along the channels dimension.
optional uint32 concat_dim = 65 [default = 1];
optional HDF5OutputParameter hdf5_output_param = 1001;
}
message PReLUParameter {
// Parametric ReLU described in K. He et al, Delving Deep into Rectifiers:
// Surpassing Human-Level Performance on ImageNet Classification, 2015.
// Initial value of a_i. Default is a_i=0.25 for all i.
optional FillerParameter filler = 1;
// Whether or not slope paramters are shared across channels.
optional bool channel_shared = 2 [default = false];
}
//********add by xia****************
message RPNParameter {
optional uint32 feat_stride = 1;
optional uint32 basesize = 2;
repeated uint32 scale = 3;
repeated float ratio = 4;
optional uint32 boxminsize =5;
optional uint32 per_nms_topn = 9;
optional uint32 post_nms_topn = 11;
optional float nms_thresh = 8;
}
message VideoDataParameter{
enum VideoType {
WEBCAM = 0;
VIDEO = 1;
}
optional VideoType video_type = 1 [default = WEBCAM];
optional int32 device_id = 2 [default = 0];
optional string video_file = 3;
// Number of frames to be skipped before processing a frame.
optional uint32 skip_frames = 4 [default = 0];
}
message CenterLossParameter {
optional uint32 num_output = 1; // The number of outputs for the layer
optional FillerParameter center_filler = 2; // The filler for the centers
// The first axis to be lumped into a single inner product computation;
// all preceding axes are retained in the output.
// May be negative to index from the end (e.g., -1 for the last axis).
optional int32 axis = 3 [default = 1];
}
message MarginInnerProductParameter {
optional uint32 num_output = 1; // The number of outputs for the layer
enum MarginType {
SINGLE = 0;
DOUBLE = 1;
TRIPLE = 2;
QUADRUPLE = 3;
}
optional MarginType type = 2 [default = SINGLE];
optional FillerParameter weight_filler = 3; // The filler for the weight
// The first axis to be lumped into a single inner product computation;
// all preceding axes are retained in the output.
// May be negative to index from the end (e.g., -1 for the last axis).
optional int32 axis = 4 [default = 1];
optional float base = 5 [default = 1];
optional float gamma = 6 [default = 0];
optional float power = 7 [default = 1];
optional int32 iteration = 8 [default = 0];
optional float lambda_min = 9 [default = 0];
}
message AdditiveMarginInnerProductParameter {
optional uint32 num_output = 1; // The number of outputs for the layer
optional FillerParameter weight_filler = 2; // The filler for the weight
optional float m = 3 [default = 0.35];
optional int32 axis = 4 [default = 1];
}
message DeformableConvolutionParameter {
optional uint32 num_output = 1;
optional bool bias_term = 2 [default = true];
repeated uint32 pad = 3; // The padding size; defaults to 0
repeated uint32 kernel_size = 4; // The kernel size
repeated uint32 stride = 6; // The stride; defaults to 1
repeated uint32 dilation = 18; // The dilation; defaults to 1
optional uint32 pad_h = 9 [default = 0]; // The padding height (2D only)
optional uint32 pad_w = 10 [default = 0]; // The padding width (2D only)
optional uint32 kernel_h = 11; // The kernel height (2D only)
optional uint32 kernel_w = 12; // The kernel width (2D only)
optional uint32 stride_h = 13; // The stride height (2D only)
optional uint32 stride_w = 14; // The stride width (2D only)
optional uint32 group = 5 [default = 4];
optional uint32 deformable_group = 25 [default = 4];
optional FillerParameter weight_filler = 7; // The filler for the weight
optional FillerParameter bias_filler = 8; // The filler for the bias
enum Engine {
DEFAULT = 0;
CAFFE = 1;
CUDNN = 2;
}
optional Engine engine = 15 [default = DEFAULT];
optional int32 axis = 16 [default = 1];
optional bool force_nd_im2col = 17 [default = false];
}
message LabelSpecificAddParameter {
optional float bias = 1 [default = 0.0];
optional bool transform_test = 2 [default = false];
}
message ChannelScaleParameter{
optional bool do_forward = 1 [default = true];
optional bool do_backward_feature = 2 [default = true];
optional bool do_backward_scale = 3 [default = true];
optional bool global_scale = 4 [default = false];
optional float max_global_scale = 5 [default = 1000.0];
optional float min_global_scale = 6 [default = 0.0];
optional float init_global_scale = 7 [default = 1.0];
}
message CosinAddmParameter {
optional float m = 1 [default = 0.5];
optional bool transform_test = 2 [default = false];
}
message CosinMulmParameter {
optional float m = 1 [default = 4];
optional bool transform_test = 2 [default = false];
}
message CoupledClusterLossParameter {
optional float margin = 1 [default = 1];
optional int32 group_size = 2 [default = 3];
optional float scale = 3 [default = 1];
optional bool log_flag = 4 [default = false];
// optional int32 pos_num = 3 [default = 1];
// optional int32 neg_num = 4 [default = 1];
}
message TripletLossParameter {
optional float margin = 1 [default = 1];
optional int32 group_size = 2 [default = 3];
optional float scale = 3 [default = 1];
// optional int32 pos_num = 3 [default = 1];
// optional int32 neg_num = 4 [default = 1];
}
message GeneralTripletParameter {
optional float margin = 1 [default = 0.2];
optional bool add_center_loss = 2 [default = true];
optional bool hardest_only = 3 [default = false];
optional bool positive_first = 4 [default = false];
optional float positive_upper_bound = 5 [default = 1.0];
optional float positive_weight = 6 [default = 1.0];
optional float negative_weight = 7 [default = 1.0];
}
message ROIAlignParameter {
optional uint32 pooled_h = 1 [default = 0]; // The pooled output height
optional uint32 pooled_w = 2 [default = 0]; // The pooled output width
optional float spatial_scale = 3 [default = 1];
}
import lmdb
from Caffe import caffe_pb2 as pb2
import numpy as np
class Read_Caffe_LMDB():
def __init__(self,path,dtype=np.uint8):
self.env=lmdb.open(path, readonly=True)
self.dtype=dtype
self.txn=self.env.begin()
self.cursor=self.txn.cursor()
@staticmethod
def to_numpy(value,dtype=np.uint8):
datum = pb2.Datum()
datum.ParseFromString(value)
flat_x = np.fromstring(datum.data, dtype=dtype)
data = flat_x.reshape(datum.channels, datum.height, datum.width)
label=flat_x = datum.label
return data,label
def iterator(self):
while True:
key,value=self.cursor.key(),self.cursor.value()
yield self.to_numpy(value,self.dtype)
if not self.cursor.next():
return
def __iter__(self):
self.cursor.first()
it = self.iterator()
return it
def __len__(self):
return int(self.env.stat()['entries'])
from __future__ import absolute_import
from . import caffe_pb2 as pb
import google.protobuf.text_format as text_format
import numpy as np
from .layer_param import Layer_param
class _Net(object):
def __init__(self):
self.net=pb.NetParameter()
def layer_index(self,layer_name):
# find a layer's index by name. if the layer was found, return the layer position in the net, else return -1.
for i, layer in enumerate(self.net.layer):
if layer.name == layer_name:
return i
def add_layer(self,layer_params,before='',after=''):
# find the before of after layer's position
index = -1
if after != '':
index = self.layer_index(after) + 1
if before != '':
index = self.layer_index(before)
new_layer = pb.LayerParameter()
new_layer.CopyFrom(layer_params.param)
#insert the layer into the layer protolist
if index != -1:
self.net.layer.add()
for i in range(len(self.net.layer) - 1, index, -1):
self.net.layer[i].CopyFrom(self.net.layer[i - 1])
self.net.layer[index].CopyFrom(new_layer)
else:
self.net.layer.extend([new_layer])
def remove_layer_by_name(self,layer_name):
for i,layer in enumerate(self.net.layer):
if layer.name == layer_name:
del self.net.layer[i]
return
raise(AttributeError, "cannot found layer %s" % str(layer_name))
def get_layer_by_name(self, layer_name):
# get the layer by layer_name
for layer in self.net.layer:
if layer.name == layer_name:
return layer
raise(AttributeError, "cannot found layer %s" % str(layer_name))
def save_prototxt(self,path):
prototxt=pb.NetParameter()
prototxt.CopyFrom(self.net)
for layer in prototxt.layer:
del layer.blobs[:]
with open(path,'w') as f:
f.write(text_format.MessageToString(prototxt))
def layer(self,layer_name):
return self.get_layer_by_name(layer_name)
def layers(self):
return list(self.net.layer)
class Prototxt(_Net):
def __init__(self,file_name=''):
super(Prototxt,self).__init__()
self.file_name=file_name
if file_name!='':
f = open(file_name,'r')
text_format.Parse(f.read(), self.net)
pass
def init_caffemodel(self,caffe_cmd_path='caffe'):
"""
:param caffe_cmd_path: The shell command of caffe, normally at <path-to-caffe>/build/tools/caffe
"""
s=pb.SolverParameter()
s.train_net=self.file_name
s.max_iter=0
s.base_lr=1
s.solver_mode = pb.SolverParameter.CPU
s.snapshot_prefix='./nn'
with open('/tmp/nn_tools_solver.prototxt','w') as f:
f.write(str(s))
import os
os.system('%s train --solver /tmp/nn_tools_solver.prototxt'%caffe_cmd_path)
class Caffemodel(_Net):
def __init__(self, file_name=''):
super(Caffemodel,self).__init__()
# caffe_model dir
if file_name!='':
f = open(file_name,'rb')
self.net.ParseFromString(f.read())
f.close()
def save(self, path):
with open(path,'wb') as f:
f.write(self.net.SerializeToString())
def add_layer_with_data(self,layer_params,datas, before='', after=''):
"""
Args:
layer_params:A Layer_Param object
datas:a fixed dimension numpy object list
after: put the layer after a specified layer
before: put the layer before a specified layer
"""
self.add_layer(layer_params,before,after)
new_layer =self.layer(layer_params.name)
#process blobs
del new_layer.blobs[:]
for data in datas:
new_blob=new_layer.blobs.add()
for dim in data.shape:
new_blob.shape.dim.append(dim)
new_blob.data.extend(data.flatten().astype(float))
def get_layer_data(self,layer_name):
layer=self.layer(layer_name)
datas=[]
for blob in layer.blobs:
shape=list(blob.shape.dim)
data=np.array(blob.data).reshape(shape)
datas.append(data)
return datas
def set_layer_data(self,layer_name,datas):
# datas is normally a list of [weights,bias]
layer=self.layer(layer_name)
for blob,data in zip(layer.blobs,datas):
blob.data[:]=data.flatten()
pass
class Net():
def __init__(self,*args,**kwargs):
raise(TypeError,'the class Net is no longer used, please use Caffemodel or Prototxt instead')
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
from __future__ import absolute_import
from . import caffe_pb2 as pb
def pair_process(item, strict_one=True):
if hasattr(item, '__iter__'):
for i in item:
if i != item[0]:
if strict_one:
raise ValueError("number in item {} must be the same".format(item))
else:
print("IMPORTANT WARNING: number in item {} must be the same".format(item))
return item[0]
return item
def pair_reduce(item):
if hasattr(item, '__iter__'):
for i in item:
if i != item[0]:
return item
return [item[0]]
return [item]
class Layer_param():
def __init__(self, name='', type='', top=(), bottom=()):
self.param = pb.LayerParameter()
self.name = self.param.name = name
self.type = self.param.type = type
self.top = self.param.top
self.top.extend(top)
self.bottom = self.param.bottom
self.bottom.extend(bottom)
def fc_param(self, num_output, weight_filler='xavier', bias_filler='constant', has_bias=True):
if self.type != 'InnerProduct':
raise TypeError('the layer type must be InnerProduct if you want set fc param')
fc_param = pb.InnerProductParameter()
fc_param.num_output = num_output
fc_param.weight_filler.type = weight_filler
fc_param.bias_term = has_bias
if has_bias:
fc_param.bias_filler.type = bias_filler
self.param.inner_product_param.CopyFrom(fc_param)
def conv_param(self, num_output, kernel_size, stride=(1), pad=(0,),
weight_filler_type='xavier', bias_filler_type='constant',
bias_term=True, dilation=None, groups=None):
"""
add a conv_param layer if you spec the layer type "Convolution"
Args:
num_output: a int
kernel_size: int list
stride: a int list
weight_filler_type: the weight filer type
bias_filler_type: the bias filler type
Returns:
"""
if self.type not in ['Convolution', 'Deconvolution']:
raise TypeError('the layer type must be Convolution or Deconvolution if you want set conv param')
conv_param = pb.ConvolutionParameter()
conv_param.num_output = num_output
conv_param.kernel_size.extend(pair_reduce(kernel_size))
conv_param.stride.extend(pair_reduce(stride))
conv_param.pad.extend(pair_reduce(pad))
conv_param.bias_term = bias_term
conv_param.weight_filler.type = weight_filler_type
if bias_term:
conv_param.bias_filler.type = bias_filler_type
if dilation:
conv_param.dilation.extend(pair_reduce(dilation))
if groups:
conv_param.group = groups
self.param.convolution_param.CopyFrom(conv_param)
def pool_param(self, type='MAX', kernel_size=2, stride=2, pad=None, ceil_mode=False):
pool_param = pb.PoolingParameter()
pool_param.pool = pool_param.PoolMethod.Value(type)
pool_param.kernel_size = pair_process(kernel_size)
pool_param.stride = pair_process(stride)
pool_param.ceil_mode = ceil_mode
if pad:
if isinstance(pad, tuple):
pool_param.pad_h = pad[0]
pool_param.pad_w = pad[1]
else:
pool_param.pad = pad
self.param.pooling_param.CopyFrom(pool_param)
def batch_norm_param(self, use_global_stats=0, moving_average_fraction=None, eps=None):
bn_param = pb.BatchNormParameter()
bn_param.use_global_stats = use_global_stats
if moving_average_fraction:
bn_param.moving_average_fraction = moving_average_fraction
if eps:
bn_param.eps = eps
self.param.batch_norm_param.CopyFrom(bn_param)
def upsample_param(self, size=None, scale_factor=None):
upsample_param = pb.UpsampleParameter()
if scale_factor:
if isinstance(scale_factor, int):
upsample_param.scale = scale_factor
else:
upsample_param.scale_h = scale_factor[0]
upsample_param.scale_w = scale_factor[1]
if size:
if isinstance(size, int):
upsample_param.upsample_h = size
else:
upsample_param.upsample_h = size[0]
upsample_param.upsample_w = size[1]
# upsample_param.upsample_h = size[0] * scale_factor
# upsample_param.upsample_w = size[1] * scale_factor
self.param.upsample_param.CopyFrom(upsample_param)
def interp_param(self, size=None, scale_factor=None):
interp_param = pb.InterpParameter()
if scale_factor:
if isinstance(scale_factor, int):
interp_param.zoom_factor = scale_factor
if size:
print('size:', size)
interp_param.height = size[0]
interp_param.width = size[1]
self.param.interp_param.CopyFrom(interp_param)
def add_data(self, *args):
"""Args are data numpy array
"""
del self.param.blobs[:]
for data in args:
new_blob = self.param.blobs.add()
for dim in data.shape:
new_blob.shape.dim.append(dim)
new_blob.data.extend(data.flatten().astype(float))
def set_params_by_dict(self, dic):
pass
def copy_from(self, layer_param):
pass
def set_enum(param, key, value):
setattr(param, key, param.Value(value))
raise ImportError("the nn_tools.Caffe.net is no longer used, please use nn_tools.Caffe.caffe_net")
# Model Deployment
This directory contains:
1. The scripts that convert a fastreid model to Caffe/ONNX/TRT format.
2. The exmpales that load a R50 baseline model in Caffe/ONNX/TRT and run inference.
## Tutorial
### Caffe Convert
<details>
<summary>step-to-step pipeline for caffe convert</summary>
This is a tiny example for converting fastreid-baseline in `meta_arch` to Caffe model, if you want to convert more complex architecture, you need to customize more things.
1. Run `caffe_export.py` to get the converted Caffe model,
```bash
python tools/deploy/caffe_export.py --config-file configs/market1501/bagtricks_R50/config.yml --name baseline_R50 --output caffe_R50_model --opts MODEL.WEIGHTS logs/market1501/bagtricks_R50/model_final.pth
```
then you can check the Caffe model and prototxt in `./caffe_R50_model`.
2. Change `prototxt` following next three steps:
1) Modify `MaxPooling` in `baseline_R50.prototxt` and delete `ceil_mode: false`.
2) Add `avg_pooling` in `baseline_R50.prototxt`
```prototxt
layer {
name: "avgpool1"
type: "Pooling"
bottom: "relu_blob49"
top: "avgpool_blob1"
pooling_param {
pool: AVE
global_pooling: true
}
}
```
2) Change the last layer `top` name to `output`
```prototxt
layer {
name: "bn_scale54"
type: "Scale"
bottom: "batch_norm_blob54"
top: "output" # bn_norm_blob54
scale_param {
bias_term: true
}
}
```
3. (optional) You can open [Netscope](https://ethereon.github.io/netscope/quickstart.html), then enter you network `prototxt` to visualize the network.
4. Run `caffe_inference.py` to save Caffe model features with input images
```bash
python caffe_inference.py --model-def outputs/caffe_model/baseline_R50.prototxt \
--model-weights outputs/caffe_model/baseline_R50.caffemodel \
--input test_data/*.jpg --output caffe_output
```
6. Run `demo/demo.py` to get fastreid model features with the same input images, then verify that Caffe and PyTorch are computing the same value for the network.
```python
np.testing.assert_allclose(torch_out, ort_out, rtol=1e-3, atol=1e-6)
```
</details>
### ONNX Convert
<details>
<summary>step-to-step pipeline for onnx convert</summary>
This is a tiny example for converting fastreid-baseline in `meta_arch` to ONNX model. ONNX supports most operators in pytorch as far as I know and if some operators are not supported by ONNX, you need to customize these.
1. Run `onnx_export.py` to get the converted ONNX model,
```bash
python onnx_export.py --config-file root-path/bagtricks_R50/config.yml --name baseline_R50 --output outputs/onnx_model --opts MODEL.WEIGHTS root-path/logs/market1501/bagtricks_R50/model_final.pth
```
then you can check the ONNX model in `outputs/onnx_model`.
2. (optional) You can use [Netron](https://github.com/lutzroeder/netron) to visualize the network.
3. Run `onnx_inference.py` to save ONNX model features with input images
```bash
python onnx_inference.py --model-path outputs/onnx_model/baseline_R50.onnx \
--input test_data/*.jpg --output onnx_output
```
4. Run `demo/demo.py` to get fastreid model features with the same input images, then verify that ONNX Runtime and PyTorch are computing the same value for the network.
```python
np.testing.assert_allclose(torch_out, ort_out, rtol=1e-3, atol=1e-6)
```
</details>
### TensorRT Convert
<details>
<summary>step-to-step pipeline for trt convert</summary>
This is a tiny example for converting fastreid-baseline in `meta_arch` to TRT model.
First you need to convert the pytorch model to ONNX format following [ONNX Convert](https://github.com/JDAI-CV/fast-reid#fastreid), and you need to remember your `output` name. Then you can convert ONNX model to TensorRT following instructions below.
1. Run command line below to get the converted TRT model from ONNX model,
```bash
python trt_export.py --name baseline_R50 --output outputs/trt_model \
--mode fp32 --batch-size 8 --height 256 --width 128 \
--onnx-model outputs/onnx_model/baseline.onnx
```
then you can check the TRT model in `outputs/trt_model`.
2. Run `trt_inference.py` to save TRT model features with input images
```bash
python3 trt_inference.py --model-path outputs/trt_model/baseline.engine \
--input test_data/*.jpg --batch-size 8 --height 256 --width 128 --output trt_output
```
3. Run `demo/demo.py` to get fastreid model features with the same input images, then verify that TensorRT and PyTorch are computing the same value for the network.
```python
np.testing.assert_allclose(torch_out, trt_out, rtol=1e-3, atol=1e-6)
```
Notice: The int8 mode in tensorRT runtime is not supported now and there are some bugs in calibrator. Need help!
</details>
## Acknowledgements
Thank to [CPFLAME](https://github.com/CPFLAME), [gcong18](https://github.com/gcong18), [YuxiangJohn](https://github.com/YuxiangJohn) and [wiggin66](https://github.com/wiggin66) at JDAI Model Acceleration Group for help in PyTorch model converting.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment