def test_fc_fp16_initializer(self): model = model_helper.ModelHelper(name="test") data = model.net.AddExternalInput("data") fc1 = brew.fc(model, data, "fc1", dim_in=1, dim_out=1) # default operator, PseudoFP16Initializer fc2 = brew.fc(model, fc1, "fc2", dim_in=1, dim_out=1, WeightInitializer=PseudoFP16Initializer) # specified operator, PseudoFP16Initializer fc3 = brew.fc(model, fc2, "fc3", dim_in=1, dim_out=1, weight_init=("ConstantFill", {}), WeightInitializer=PseudoFP16Initializer)
def test_topological_sort_longest_path(self): m = model_helper.ModelHelper() # 0 m.Copy("conv0_w_comp", "conv0_w") # 1 conv0 = brew.conv(m, "data", "conv0", 32, 32, 4) # 2 m.Copy("conv2_w", "conv2_w") # 3 brew.conv(m, conv0, "conv2", 16, 32, 4) g = memonger.compute_interference_graph(m.net.Proto().op) orders_org = memonger.topological_sort_traversal(g) orders_gt_org = [2, 0, 1, 3] self.assertEqual(orders_gt_org, orders_org) orders = memonger.topological_sort_traversal_longest_path(g) # longer path is in front of the shorter one orders_gt = [0, 1, 2, 3] self.assertEqual(orders_gt, list(orders))
def test_simple_transform(self, input_dim, output_dim, batch_size): m = model_helper.ModelHelper() fc1 = brew.fc(m, "data", "fc1", dim_in=input_dim, dim_out=output_dim) fc2 = brew.fc(m, fc1, "fc2", dim_in=output_dim, dim_out=output_dim) conv = brew.conv(m, fc2, "conv", dim_in=output_dim, dim_out=output_dim, use_cudnn=True, engine="CUDNN", kernel=3) conv.Relu([], conv)\ .Softmax([], "pred") \ .LabelCrossEntropy(["label"], ["xent"]) \ .AveragedLoss([], "loss") transformed_net_proto = workspace.ApplyTransform( "ConvToNNPack", m.net.Proto()) self.assertEqual(transformed_net_proto.op[2].engine, "NNPACK")
def testShapeInferenceDistances(self): model = model_helper.ModelHelper(name="test_model") model.net.L1Distance(["x1", "y1"], "dl1_D1") model.net.SquaredL2Distance(["x1", "y1"], "dl2_D1") model.net.CosineSimilarity(["x1", "y1"], "dcos_D1") model.net.DotProduct(["x1", "y1"], "ddot_D1") model.net.DotProductWithPadding(["x1", "y1"], "ddotpad_D1") model.net.L1Distance(["x2", "y2"], "dl1_D2") model.net.SquaredL2Distance(["x2", "y2"], "dl2_D2") model.net.CosineSimilarity(["x2", "y2"], "dcos_D2") model.net.DotProduct(["x2", "y2"], "ddot_D2") model.net.DotProductWithPadding(["x2", "z2"], "ddotpad_D2") workspace.FeedBlob("x1", np.random.rand(10).astype(np.float32)) workspace.FeedBlob("y1", np.random.rand(10).astype(np.float32)) workspace.FeedBlob("x2", np.random.rand(10, 5).astype(np.float32)) workspace.FeedBlob("y2", np.random.rand(10, 5).astype(np.float32)) workspace.FeedBlob("z2", np.random.rand(10, 4).astype(np.float32)) self.InferTensorRunAndCompare(model)
def testShapeInferenceSlice(self): model = model_helper.ModelHelper(name="test_model") model.net.Slice(["x"], ["y"], starts=[0, 0, 0, 0], ends=[-1, -1, -3, -1]) workspace.FeedBlob("x", np.random.rand(64, 1, 255, 384).astype(np.float32)) slice_starts = np.array([0, 0, 0, 0]).astype(np.int32) slice_ends = np.array([-1, -1, -3, -1]).astype(np.int32) slice_starts = model.net.GivenTensorIntFill([], shape=[4], values=slice_starts) slice_ends = model.net.GivenTensorIntFill([], shape=[4], values=slice_ends) model.net.Slice(["x2", slice_starts, slice_ends], ["y2"]) workspace.FeedBlob("x2", np.random.rand(64, 1, 255, 384).astype(np.float32)) self.InferTensorRunAndCompare(model, ["y2"])
def createTrainModel(training_lmdb_path, batch_size): """Create and return a training model, complete with training ops.""" train_model = model_helper.ModelHelper(name='train_net', arg_scope=arg_scope) AddInput(train_model, db=training_lmdb_path, db_type=data_db_type, batch_size=batch_size, mirror=0) losses = Add_Action_Tufts_Model(train_model, num_classes, image_height, image_width, image_channels, is_test=0) train_model.AddGradientOperators(losses) AddOptimizerOps_adam(train_model) AddCheckpoints(train_model, checkpoint_iters, db_type="lmdb") workspace.RunNetOnce(train_model.param_init_net) workspace.CreateNet(train_model.net, overwrite=True) return train_model
def fc_explicit_param_names(self): brew.Register(fc_explicit_param_names) model = model_helper.ModelHelper(name="test_model") dim_in = 10 dim_out = 100 weight_name = "test_weight_name" bias_name = "test_bias_name" inputs_name = "test_inputs" output_name = "test_output" input_distribution = stats.norm() inputs = input_distribution.rvs(size=(1, dim_in)).astype(np.float32) workspace.FeedBlob(inputs_name, inputs) weights = np.random.normal(size=(dim_out, dim_in)).astype(np.float32) bias = np.transpose(np.random.normal(size=(dim_out)).astype(np.float32)) brew.fc_explicit_param_names( model, inputs_name, output_name, dim_in=dim_in, dim_out=dim_out, bias_name=bias_name, weight_name=weight_name, weight_init=("GivenTensorFill", {"values": weights}), bias_init=("GivenTensorFill", {"values": bias}), ) workspace.RunNetOnce(model.param_init_net) model.net.Proto().type = "async_scheduling" workspace.CreateNet(model.net) workspace.RunNet(model.net) expected_output = np.dot(inputs, np.transpose(weights)) + bias outputs_diff = expected_output - workspace.FetchBlob(output_name) self.assertEqual(np.linalg.norm(outputs_diff), 0)
def test_read_from_db(self): random_label = np.random.randint(0, 100) VIDEO = "/mnt/vol/gfsdataswarm-oregon/users/trandu/sample.avi" temp_list = tempfile.NamedTemporaryFile(delete=False).name line_str = '{} 0 {}\n'.format(VIDEO, random_label) self.create_a_list( temp_list, line_str, 16) video_db_dir = tempfile.mkdtemp() self.create_video_db(temp_list, video_db_dir) model = model_helper.ModelHelper(name="Video Loader from LMDB") reader = model.CreateDB( "sample", db=video_db_dir, db_type="lmdb") model.VideoInput( reader, ["data", "label"], name="data", batch_size=10, width=171, height=128, crop=112, length=8, sampling_rate=2, mirror=1, use_local_file=0, temporal_jitter=1) workspace.RunNetOnce(model.param_init_net) workspace.RunNetOnce(model.net) data = workspace.FetchBlob("data") label = workspace.FetchBlob("label") np.testing.assert_equal(label, random_label) np.testing.assert_equal(data.shape, [10, 3, 8, 112, 112]) os.remove(temp_list) shutil.rmtree(video_db_dir)
def benchmark(opts): """Runs inference or training benchmarks depending on **opts['phase']** value. You may want to call **workspace.ResetWorkspace()** to clear everything once this method has exited. :param dict opts: Options for a benchmark. Must contain `model` and 'phase'.\ Other options are optional. :return: Tuple of model title and numpy array containing batch times. :rtype: (string, numpy array) Usage example: >>> opts = {'model': 'resnet50', 'phase': 'training'} >>> model_title, times = benchmark(opts) This function checks that **opts** contains all mandatory parameters, sets optional parameters to default values and depending on **phase** value, calls either :py:func:`benchmark_inference` or :py:func:`benchmark_training`. """ assert 'model' in opts, "Missing 'model' in options." assert 'phase' in opts, "Missing 'phase' in options." assert opts['phase'] in [ 'inference', 'training' ], "Invalid value for 'phase' (%s). Must be 'inference' or 'training'." % ( opts['phase']) opts['batch_size'] = opts.get('batch_size', 16) opts['num_warmup_batches'] = opts.get('num_warmup_batches', 10) opts['num_batches'] = opts.get('num_batches', 10) opts['device'] = opts.get('device', 'gpu') opts['num_gpus'] = opts.get('num_gpus', 1) opts['dtype'] = opts.get('dtype', 'float') opts['enable_tensor_core'] = opts.get('enable_tensor_core', False) model = model_helper.ModelHelper(name=opts['model']) if opts['phase'] == 'inference': return benchmark_inference(model, opts) else: return benchmark_training(model, opts)
def test_apply_transform_if_faster(self, value): init_net = core.Net("init_net") init_net.ConstantFill([], ["data"], shape=[5, 5, 5, 5], value=value) init_net.ConstantFill([], ["conv_w"], shape=[5, 5, 3, 3], value=value) init_net.ConstantFill([], ["conv_b"], shape=[5], value=value) self.assertEqual( workspace.RunNetOnce(init_net.Proto().SerializeToString()), True) m = model_helper.ModelHelper() conv = brew.conv(m, "data", "conv", dim_in=5, dim_out=5, kernel=3, use_cudnn=True, engine="CUDNN") conv.Relu([], conv)\ .Softmax([], "pred") \ .AveragedLoss([], "loss") self.assertEqual( workspace.RunNetOnce(m.net.Proto().SerializeToString()), True) proto = workspace.ApplyTransformIfFaster( "ConvToNNPack", m.net.Proto(), init_net.Proto()) self.assertEqual( workspace.RunNetOnce(proto.SerializeToString()), True) proto = workspace.ApplyTransformIfFaster( "ConvToNNPack", m.net.Proto(), init_net.Proto(), warmup_runs=10, main_runs=100, improvement_threshold=2.0) self.assertEqual( workspace.RunNetOnce(proto.SerializeToString()), True)
def create_train_model(data_folder): """Create model for training with MNIST train dataset.""" # Create the model helper for the train model train_model = model_helper.ModelHelper(name="mnist_lenet_train_model") # Specify the input is from the train lmdb data, label = add_model_inputs( train_model, batch_size=64, db=os.path.join(data_folder, "mnist-train-nchw-lmdb"), db_type="lmdb", ) # Build the LeNet-5 network softmax_layer = build_mnist_lenet(train_model, data) # Compute cross entropy between softmax scores and labels cross_entropy = train_model.LabelCrossEntropy([softmax_layer, label], "cross_entropy") # Compute the expected loss loss = train_model.AveragedLoss(cross_entropy, "loss") # Use the average loss we just computed to add gradient operators to the model train_model.AddGradientOperators([loss]) # Specify the optimization algorithm optimizer.build_sgd( train_model, base_learning_rate=0.1, policy="step", stepsize=1, gamma=0.999, ) # Track the accuracy of the model add_accuracy_op(train_model, softmax_layer, label) return train_model
def test_optical_flow_with_temporal_jittering(self): random_label = np.random.randint(0, 100) VIDEO = "/mnt/vol/gfsdataswarm-oregon/users/trandu/sample.avi" if not os.path.exists(VIDEO): raise unittest.SkipTest("Missing data") temp_list = tempfile.NamedTemporaryFile(delete=False).name line_str = "{} 0 {}\n".format(VIDEO, random_label) self.create_a_list(temp_list, line_str, 16) video_db_dir = tempfile.mkdtemp() self.create_video_db(temp_list, video_db_dir) model = model_helper.ModelHelper(name="Video Loader from LMDB") reader = model.CreateDB("sample", db=video_db_dir, db_type="lmdb") model.net.VideoInput( reader, ["data", "label"], name="data", batch_size=16, clip_per_video=1, crop_size=112, scale_w=171, scale_h=128, length_of=8, sampling_rate_of=1, frame_gap_of=1, decode_type=0, video_res_type=0, get_rgb=False, get_optical_flow=True, ) workspace.RunNetOnce(model.param_init_net) workspace.RunNetOnce(model.net) data = workspace.FetchBlob("data") label = workspace.FetchBlob("label") np.testing.assert_equal(label, random_label) np.testing.assert_equal(data.shape, [16, 2, 8, 112, 112]) os.remove(temp_list) shutil.rmtree(video_db_dir)
def test_get_entry_from_blobs_modify_output_record(self): model = model_helper.ModelHelper(name="test") data = model.net.AddExternalInput("data") fc1 = brew.fc(model, data, "fc1", dim_in=4, dim_out=4) # no operator name set, will use default brew.fc(model, fc1, "fc2", dim_in=4, dim_out=4) i1, i2 = np.random.randint(4), np.random.randint(5) - 1 net_modifier = GetEntryFromBlobs( blobs=['fc1_w', 'fc2_w'], logging_frequency=10, i1=i1, i2=i2, ) net_modifier(model.net, modify_output_record=True) workspace.FeedBlob('data', np.random.rand(10, 4).astype(np.float32)) workspace.RunNetOnce(model.param_init_net) workspace.RunNetOnce(model.net) fc1_w = workspace.FetchBlob('fc1_w') if i2 < 0: fc1_w_entry = workspace.FetchBlob('fc1_w_{0}_all'.format(i1)) else: fc1_w_entry = workspace.FetchBlob('fc1_w_{0}_{1}'.format(i1, i2)) if i2 < 0: self.assertEqual(fc1_w_entry.size, 4) for j in range(4): self.assertEqual(fc1_w_entry[0][j], fc1_w[i1][j]) else: self.assertEqual(fc1_w_entry.size, 1) self.assertEqual(fc1_w_entry[0], fc1_w[i1][i2]) assert 'fc1_w' + net_modifier.field_name_suffix() in\ model.net.output_record().field_blobs(),\ model.net.output_record().field_blobs() assert 'fc2_w' + net_modifier.field_name_suffix() in\ model.net.output_record().field_blobs(),\ model.net.output_record().field_blobs()
def test_leaky_relu_model_helper_helper(self, N, C, H, W, order, alpha, seed): np.random.seed(seed) arg_scope = {'order': order} model = model_helper.ModelHelper(name="test_model", arg_scope=arg_scope) model.LeakyRelu('input', 'output', alpha=alpha) input_blob = np.random.rand(N, C, H, W).astype(np.float32) if order == 'NHWC': input_blob = np.transpose(input_blob, axes=(0, 2, 3, 1)) self.ws.create_blob('input').feed(input_blob) self.ws.create_net(model.param_init_net).run() self.ws.create_net(model.net).run() output_blob = self.ws.blobs['output'].fetch() if order == 'NHWC': output_blob = np.transpose(output_blob, axes=(0, 3, 1, 2)) assert output_blob.shape == (N, C, H, W)
def main(): workspace.ResetWorkspace() workspace.GlobalInit(['caffe2', '--caffe2_log_level=2', '--caffe2_net_async_thread_pool_size=1']) model = model_helper.ModelHelper(name="FC") input_data = GetInput() workspace.FeedBlob("input", input_data) out = GetModel(model) model.net.Proto().type = "simple" workspace.RunNetOnce(model.param_init_net) workspace.CreateNet(model.net, overwrite=True) print(" =============== CAFFE2 ==================") print(" [" + str(FLAGS.batch_size) + "x" + str(FLAGS.node) + "] * [" + str(FLAGS.node) + "x" + str(FLAGS.node) + "]") for arg in vars(FLAGS): print("***%s: %s" % (arg, getattr(FLAGS, arg))) if 'OMP_NUM_THREADS' in os.environ: print("***OMP_NUM_THREADS: ", os.environ['OMP_NUM_THREADS']) if 'MKL_NUM_THREADS' in os.environ: print("***MKL_NUM_THREADS: ", os.environ['MKL_NUM_THREADS']) flops = 2 * FLAGS.batch_size * FLAGS.node * FLAGS.node workspace.RunNet(model.net.Proto().name, num_iter=1) for i in range(10): t1 = time.time() workspace.RunNet(model.net.Proto().name, num_iter=FLAGS.loop_count/10) t2 = time.time() total_time = t2 - t1 avg_time = total_time / (FLAGS.loop_count / 10) / (layer + 1) print('----- During ' + str((FLAGS.loop_count/10)*i) + ' to ' + str((FLAGS.loop_count/10)*(i+1)) + ' steps -----') print(" Average time: %s secs" % avg_time) print(' GFlop/sec : {}'.format(flops / avg_time / 1e9))
def init_model(): # Create Place-holder for data workspace.FeedBlob("data", x_train[:BATCHSIZE], device_option=device_opts) workspace.FeedBlob("label", y_train[:BATCHSIZE], device_option=device_opts) # Initialise model train_arg_scope = { 'order': 'NCHW', 'use_cudnn': True, 'cudnn_exhaustive_search': True, 'ws_nbytes_limit': (64 * 1024 * 1024), } train_model = model_helper.ModelHelper( name="train_net", arg_scope=train_arg_scope ) softmax = create_model(train_model, device_opts=device_opts) add_training_operators(softmax, train_model, device_opts=device_opts) # Initialise workspace workspace.RunNetOnce(train_model.param_init_net) workspace.CreateNet(train_model.net) return train_model
def build_validation_model(config): # set device device_opt = caffe2_pb2.DeviceOption() if config['gpu_id'] is not None: device_opt.device_type = caffe2_pb2.CUDA device_opt.cuda_gpu_id = config['gpu_id'] # build model with core.DeviceScope(device_opt): validation_model = model_helper.ModelHelper( name='{}_validation_model'.format(config['name']), init_params=False, ) data, label = add_input(validation_model, config, is_test=True) pred = add_model(validation_model, config, data, is_test=True) loss = add_loss(validation_model, config, pred, label) add_accuracy(validation_model) # init workspace for validation net workspace.RunNetOnce(validation_model.param_init_net) workspace.CreateNet(validation_model.net) return validation_model
def test_gradient_clipping_by_value(self): model = model_helper.ModelHelper(name="test") data = model.net.AddExternalInput("data") fc1 = brew.fc(model, data, "fc1", dim_in=4, dim_out=2) # no operator name set, will use default fc2 = brew.fc(model, fc1, "fc2", dim_in=2, dim_out=1) sigm = model.net.Sigmoid(fc2, 'sigm') sq = model.net.SquaredL2Distance([sigm, 'label'], 'sq') loss = model.net.SumElements(sq, 'loss') grad_map = model.AddGradientOperators([loss]) grad_map_for_param = {key: grad_map[key] for key in ['fc1_w', 'fc2_w']} clip_max = 1e-8 clip_min = 0 net_modifier = GradientClipping( grad_clip_method='by_value', clip_max=clip_max, clip_min=clip_min, ) net_modifier(model.net, grad_map=grad_map_for_param) workspace.FeedBlob('data', np.random.rand(10, 4).astype(np.float32)) workspace.FeedBlob('label', np.random.rand(10, 1).astype(np.float32)) workspace.RunNetOnce(model.param_init_net) workspace.RunNetOnce(model.net) # 5 forward ops + 6 backward ops + 2 * (1 gradient clipping ops) self.assertEqual(len(model.net.Proto().op), 13) fc1_w_grad = workspace.FetchBlob('fc1_w_grad') self.assertLessEqual(np.amax(fc1_w_grad), clip_max) self.assertGreaterEqual(np.amin(fc1_w_grad), clip_min)
def Resnet50(args): gpus = [0] device_opt = core.DeviceOption(caffe2_pb2.HIP if workspace.has_hip_support else caffe2_pb2.CUDA) device_opt.device_id = gpus[0] num_labels = 1000 base_learning_rate = 0.0004 * args.batch_size # Weight decay (L2 regularization) weight_decay = 1e-4 ################## # Define the Model ################## train_model = model_helper.ModelHelper(name="resnet50_train") def create_resnet50_model_ops(model, loss_scale=1.0): # residual network [softmax, loss] = resnet.create_resnet50(model, "data", num_input_channels=3, num_labels=num_labels, label="label", no_bias=True, ) prefix = model.net.Proto().name loss = model.net.Scale(loss, prefix + "_loss", scale=loss_scale) brew.accuracy(model, [softmax, "label"], prefix + "_accuracy") return [loss] with core.NameScope(""): with core.DeviceScope(device_opt): losses = create_resnet50_model_ops(train_model) if not args.forward_only: blobs_to_gradients = train_model.AddGradientOperators(losses) add_parameter_update_ops_resnet(train_model, base_learning_rate, weight_decay) if not args.forward_only: optimize_gradient_memory_resnet(train_model, [blobs_to_gradients[losses[0]]]) return train_model, 224
def test_compute_l1_averaged_norm_for_blobs(self): model = model_helper.ModelHelper(name="test") data = model.net.AddExternalInput("data") fc1 = brew.fc(model, data, "fc1", dim_in=4, dim_out=2) # no operator name set, will use default brew.fc(model, fc1, "fc2", dim_in=2, dim_out=1) net_modifier = ComputeNormForBlobs( blobs=['fc1_w', 'fc2_w'], logging_frequency=10, p=1, compute_averaged_norm=True, ) net_modifier(model.net) workspace.FeedBlob('data', np.random.rand(10, 4).astype(np.float32)) workspace.RunNetOnce(model.param_init_net) workspace.RunNetOnce(model.net) fc1_w = workspace.FetchBlob('fc1_w') fc1_w_averaged_l1_norm = workspace.FetchBlob('fc1_w_averaged_l1_norm') self.assertEqual(fc1_w_averaged_l1_norm.size, 1) self.assertAlmostEqual(fc1_w_averaged_l1_norm[0], np.sum(np.abs(fc1_w)) / fc1_w.size, delta=1e-5) self.assertEqual(len(model.net.Proto().op), 6) assert 'fc1_w' + net_modifier.field_name_suffix() in\ model.net.output_record().field_blobs(),\ model.net.output_record().field_blobs() assert 'fc2_w' + net_modifier.field_name_suffix() in\ model.net.output_record().field_blobs(),\ model.net.output_record().field_blobs()
def test_compute_histogram_for_blobs(self): model = model_helper.ModelHelper(name="test") data = model.net.AddExternalInput("data") fc1 = brew.fc(model, data, "fc1", dim_in=4, dim_out=2) # no operator name set, will use default brew.fc(model, fc1, "fc2", dim_in=2, dim_out=1) num_buckets = 20 lower_bound = 0.2 upper_bound = 0.8 accumulate = False net_modifier = ComputeHistogramForBlobs(blobs=['fc1_w', 'fc2_w'], logging_frequency=10, num_buckets=num_buckets, lower_bound=lower_bound, upper_bound=upper_bound, accumulate=accumulate) net_modifier(model.net) workspace.FeedBlob('data', np.random.rand(10, 4).astype(np.float32)) workspace.RunNetOnce(model.param_init_net) workspace.RunNetOnce(model.net) fc1_w = workspace.FetchBlob('fc1_w') fc1_w_curr_normalized_hist = workspace.FetchBlob('fc1_w_curr_normalized_hist') cur_hist, acc_hist = self.histogram(fc1_w, lower_bound=lower_bound, upper_bound=upper_bound, num_buckets=num_buckets) self.assertEqual(fc1_w_curr_normalized_hist.size, num_buckets + 2) self.assertAlmostEqual(np.linalg.norm( fc1_w_curr_normalized_hist - cur_hist), 0.0, delta=1e-5) self.assertEqual(len(model.net.Proto().op), 12) assert model.net.output_record() is None
def testNonParallelModel(self): workspace.ResetWorkspace() model = model_helper.ModelHelper(name="test") old_seq_id = data_workers.global_coordinator._fetcher_id_seq coordinator = data_workers.init_data_input_workers( model, ["data", "label"], dummy_fetcher, 32, 2, input_source_name="unittest" ) new_seq_id = data_workers.global_coordinator._fetcher_id_seq self.assertEqual(new_seq_id, old_seq_id + 2) coordinator.start() workspace.RunNetOnce(model.param_init_net) workspace.CreateNet(model.net) for _i in range(500): with timeout_guard.CompleteInTimeOrDie(5): workspace.RunNet(model.net.Proto().name) data = workspace.FetchBlob("data") labels = workspace.FetchBlob("label") self.assertEqual(data.shape[0], labels.shape[0]) self.assertEqual(data.shape[0], 32) for j in range(32): self.assertEqual(labels[j], data[j, 0]) self.assertEqual(labels[j], data[j, 1]) self.assertEqual(labels[j], data[j, 2]) coordinator.stop_coordinator("unittest") self.assertEqual(coordinator._coordinators, [])
def TranslateSqueezenetModel(name, classCount, initNetPath, predictNetPath, devOps, argScope, learningRate=10**-2): predNetPb = c2p2.NetDef() with open(predictNetPath, 'rb') as f: predNetPb.ParseFromString(f.read()) initNetPb = c2p2.NetDef() with open(initNetPath, 'rb') as f: initNetPb.ParseFromString(f.read()) model = model_helper.ModelHelper(name, arg_scope=argScope, init_params=False) for op in initNetPb.op: if op.output[0] in ['conv10_w', 'conv10_b']: tag = (ParameterTags.WEIGHT if op.output[0].endswith('_w') else ParameterTags.BIAS) # create params inside model model.create_param(op.output[0], op.arg[0], initializers.ExternalInitializer(), tags=tag) # remove conv10_w and conv10_b ops from protobuf - ids -> 50,51 # these ops were added to the model below -> XavierFill, ConstantFill initNetPb.op.pop(50) initNetPb.op.pop(50) fixInPlaceOps(predNetPb.op) model.net = core.Net(predNetPb) model.Squeeze('softmaxout', 'softmax', dims=[2, 3]) model.param_init_net = core.Net(initNetPb) model.param_init_net.XavierFill([], 'conv10_w', shape=[classCount, 512, 1, 1]) model.param_init_net.ConstantFill([], 'conv10_b', shape=[classCount]) ScaffoldModelTrainingOperators(model, 'softmax', 'label', learningRate) # InscribeDeviceOptionsToModel(model, devOps) return model, core.Net(predNetPb)
def model_helper_test(): data = np.random.rand(16, 100).astype(np.float32) # create the input data label = (np.random.rand(16)*10).astype(np.int32) # create the label workspace.FeedBlob("data", data) workspace.FeedBlob('label', label) m = model_helper.ModelHelper(name="my_first_net") # create model weight = m.param_init_net.XavierFill([], 'fc_w', shape=[10, 100]) bias = m.param_init_net.ConstantFill([], 'fc_b', shape=[10, ]) fc_1 = m.net.FC(["data", "fc_w", "fc_b"], "fc1") pred = m.net.Sigmoid(fc_1, "pred") softmax, loss = m.net.SoftmaxWithLoss([pred, "label"], ["softmax", "loss"]) print("m.net=", m.net.Proto()) print("m.param_init_net.Proto=", m.param_init_net.Proto()) workspace.RunNetOnce(m.param_init_net) pass
def test_fast_memonger_unique_outputs(self): m = model_helper.ModelHelper() fc = [] for i in range(2): z = brew.fc( m, "data{}".format(i), "fc".format(i), dim_in=2, dim_out=2) fc.append(z) r = [] # Trick is here to have same input appear twice in a same Sum for x in fc: for y in fc: r.append(brew.sum(m, [x, y], 1)) concated = brew.concat(m, r, "concated") brew.relu(m, concated, "merged") static_blobs = \ [o for op in m.param_init_net.Proto().op for o in op.output] + \ ["merged"] + ["data{}".format(i) for i in range(len(fc))] optimized_net = memonger.optimize_inference_fast( m.Proto(), static_blobs) for op in optimized_net.op: self.assertEqual(len(op.output), len(set(op.output)), str(op))
def test_fc_initializer(self): model = model_helper.ModelHelper(name="test") data = model.net.AddExternalInput("data") fc1 = brew.fc(model, data, "fc1", dim_in=1, dim_out=1) fc2 = brew.fc(model, fc1, "fc2", dim_in=1, dim_out=1, weight_initializer=create_xavier_fill_initializer()) # no operator name set, will use default fc3 = brew.fc(model, fc2, "fc3", dim_in=1, dim_out=1, weight_initializer=Initializer()) # no operator name set, will use custom fc4 = brew.fc(model, fc3, "fc4", dim_in=1, dim_out=1, weight_initializer=Initializer(), weight_init=("ConstantFill", {}), ) # conflicting operator name with self.assertRaises(Exception): brew.fc(model, fc4, "fc5", dim_in=1, dim_out=1, weight_initializer=create_xavier_fill_initializer(), weight_init=("ConstantFill", {}), )
def test_cpu2gpu_gpu2cpu_sparse_gradients(self): model = model_helper.ModelHelper(name="copy_test") v = model.param_init_net.UniformFill([], ["v"], shape=[16, 4]) indices = model.param_init_net.UniformFill([], ["v"], shape=[16, 4]) cpu_opt = core.DeviceOption(caffe2_pb2.CPU, 0) gpu_opt = core.DeviceOption(caffe2_pb2.CUDA, 0) with core.DeviceScope(gpu_opt): vcpu = model.CopyGPUToCPU(v, "vcpu") with core.DeviceScope(cpu_opt): g = model.Gather([vcpu, indices], "g") with core.DeviceScope(gpu_opt): ggpu = model.CopyCPUToGPU(g, "ggpu") f = brew.fc(model, ggpu, "out", dim_in=4, dim_out=6) (softmax, loss) = model.SoftmaxWithLoss( [f, "label"], ["softmax", "loss"], ) gradient_map = model.AddGradientOperators([loss]) self.assertTrue("v" in gradient_map) self.assertTrue(isinstance(gradient_map['v'], core.GradientSlice))
def BatchNormalization_network(): value_info = { 'x': (onnx.TensorProto.FLOAT, [2, 2, 5, 5]), 'scale': (onnx.TensorProto.FLOAT, [2]), 'bias': (onnx.TensorProto.FLOAT, [2]), 'mean': (onnx.TensorProto.FLOAT, [2]), 'var': (onnx.TensorProto.FLOAT, [2]) } model = model_helper.ModelHelper(name='test_network') model.net.AddExternalInput('x') model.net.AddExternalInput('scale') model.net.AddExternalInput('bias') model.net.AddExternalInput('mean') model.net.AddExternalInput('var') model.net.SpatialBN(['x', 'scale', 'bias', 'mean', 'var'], 'y', is_test=True) # is_test=False not really supported model.net.AddExternalOutput(model.net.GetBlobRef('y')) return model.net.Proto(), model.param_init_net.Proto(), value_info
def test_compute_statistics_for_blobs(self): model = model_helper.ModelHelper(name="test") data = model.net.AddExternalInput("data") fc1 = brew.fc(model, data, "fc1", dim_in=4, dim_out=2) # no operator name set, will use default brew.fc(model, fc1, "fc2", dim_in=2, dim_out=1) net_modifier = ComputeStatisticsForBlobs( blobs=['fc1_w', 'fc2_w'], logging_frequency=10, ) net_modifier(model.net) workspace.FeedBlob('data', np.random.rand(10, 4).astype(np.float32)) workspace.RunNetOnce(model.param_init_net) workspace.RunNetOnce(model.net) fc1_w = workspace.FetchBlob('fc1_w') fc1_w_summary = workspace.FetchBlob('fc1_w_summary') # std is unbiased here stats_ref = np.array([ fc1_w.flatten().min(), fc1_w.flatten().max(), fc1_w.flatten().mean(), fc1_w.flatten().std(ddof=1) ]) self.assertAlmostEqual(np.linalg.norm(stats_ref - fc1_w_summary), 0, delta=1e-5) self.assertEqual(fc1_w_summary.size, 4) assert model.net.output_record() is None
def Inception_v2(order, gpu_engine_ws): device_opts = caffe2_pb2.DeviceOption() device_opts.device_type = caffe2_pb2.HIP device_opts.hip_gpu_id = 0 INIT_NET_PB = '/work/models/inception_v2/init_net.pb' PREDICT_NET_PB = '/work/models/inception_v2/predict_net.pb' init_def = caffe2_pb2.NetDef() with open(INIT_NET_PB, 'rb') as f: init_def.ParseFromString(f.read()) init_def.device_option.CopyFrom(device_opts) net_def = caffe2_pb2.NetDef() with open(PREDICT_NET_PB, 'rb') as f: net_def.ParseFromString(f.read()) net_def.device_option.CopyFrom(device_opts) init_net = core.Net(init_def) predict_net = core.Net(net_def) my_arg_scope = { 'order': order, 'use_gpu_engine': True, 'gpu_engine_exhaustive_search': True, } if gpu_engine_ws: my_arg_scope['ws_nbytes_limit'] = gpu_engine_ws model = model_helper.ModelHelper( name="GoogleNet", arg_scope=my_arg_scope, ) model.param_init_net = init_net model.net = predict_net xent = model.net.LabelCrossEntropy(["prob", "label"], "xent") model.net.AveragedLoss(xent, "loss") return model, 224