def add_input(model, config, is_test=False): """ Add an database input data """ if is_test: db_reader = model.CreateDB( "val_db_reader", db=config['evaluate_data']['data_path'], db_type=config['evaluate_data']['data_format'], ) data, label = brew.image_input( model, db_reader, ['data', 'label'], batch_size=config['evaluate_data']['input_transform'] ['batch_size'], use_gpu_transform=config['evaluate_data']['input_transform'] ['use_gpu_transform'], scale=config['evaluate_data']['input_transform']['scale'], crop=config['evaluate_data']['input_transform']['crop_size'], mean_per_channel=config['evaluate_data']['input_transform'] ['mean_per_channel'], std_per_channel=config['evaluate_data']['input_transform'] ['std_per_channel'], mirror=True, is_test=True, ) else: db_reader = model.CreateDB( "train_db_reader", db=config['training_data']['data_path'], db_type=config['training_data']['data_format'], ) data, label = brew.image_input( model, db_reader, ['data', 'label'], batch_size=config['training_data']['input_transform'] ['batch_size'], use_gpu_transform=config['training_data']['input_transform'] ['use_gpu_transform'], scale=config['training_data']['input_transform']['scale'], crop=config['training_data']['input_transform']['crop_size'], mean_per_channel=config['training_data']['input_transform'] ['mean_per_channel'], std_per_channel=config['training_data']['input_transform'] ['std_per_channel'], mirror=True, is_test=False, ) # stop bp model.StopGradient('data', 'data') # model.StopGradient('label', 'label') # no need return data, label
def ImageInput(self, blob_in, blob_out, use_gpu_transform=False, **kwargs): return brew.image_input(self, blob_in, blob_out, order=self.order, use_gpu_transform=use_gpu_transform, **kwargs)
def add_data_inputs(self, model, reader, use_gpu_transform, num_decode_threads = 1): """Adds real data pipeline. Multiple GPUs in one box will share the same reader masking sure they use different data. :param model_helper.ModelHelper model: Model to add update parameters operators for. :param obj reader: Something returned by model.CreateDB(...) :param int num_decode_threads: Number of image decoding threads. For deep computationally expensive models this can be as small as 1. For high throughput models such as AlexNetOWT a value of 6-8 for 4 GPUs seems to be reasonable (Voltas, ~9k images/second) """ replica_batch = self.batch_size data, _ = brew.image_input( # data, label model, [reader], ["data", "softmax_label"], batch_size=replica_batch, # This is a per device (replica) batch size output_type=self.dtype, # "float" or "float16" use_gpu_transform=use_gpu_transform, use_caffe_datum=True, mean=128., #std=128., scale=self.input_shape[2], crop=self.input_shape[2], mirror=True, is_test=False, decode_threads=num_decode_threads ) data = model.StopGradient(data, data)
def add_data_inputs(self, model, reader, use_gpu_transform): """Adds real data pipeline. Multiple GPUs in one box will share the same reader masking sure they use different data. :param model_helper.ModelHelper model: Model to add update parameters operators for. :param obj reader: Something returned by model.CreateDB(...) """ data, _ = brew.image_input( # data, label model, reader, ["data", "softmax_label"], batch_size=self.batch_size, # Per device batch size output_type=self.dtype, # "float" or "float16" use_gpu_transform=use_gpu_transform, use_caffe_datum=True, mean=128., std=128., scale=256, crop=self.input_shape[2], mirror=1, is_test=False, ) data = model.StopGradient(data, data)
def add_input(self, model, db_path, is_test=True): """ Add an database input data """ assert (is_test == True) db_reader = model.CreateDB( "test_db_reader", # "val_db_reader", db=db_path, db_type=self.db_type) data, label = brew.image_input( model, db_reader, ['data', 'label'], batch_size=self.batch_size, use_gpu_transform=self.use_gpu_transform, scale=self.scale, crop=self.crop, mean_per_channel=[eval(item) for item in \ self.mean_per_channel.split(',')], std_per_channel=[eval(item) for item in \ self.std_per_channel.split(',')], mirror=True, is_test=is_test, label_type=self.label_type, num_labels=self.num_labels, )
def AddImageInput( model, reader, batch_size, img_size, dtype, is_test, mean_per_channel=None, std_per_channel=None, ): ''' The image input operator loads image and label data from the reader and applies transformations to the images (random cropping, mirroring, ...). ''' data, label = brew.image_input( model, reader, ["data", "label"], batch_size=batch_size, output_type=dtype, use_gpu_transform=True if core.IsGPUDeviceType(model._device_type) else False, use_caffe_datum=True, mean_per_channel=mean_per_channel, std_per_channel=std_per_channel, # mean_per_channel takes precedence over mean mean=128., std=128., scale=256, crop=img_size, mirror=1, is_test=is_test, ) data = model.StopGradient(data, data)
def read_data_db(dbpath): # db_env = lmdb.open(dbpath)#, map_size=int(1024*1024*1024*30)) # size:30GB # # print(dir(db_env.info())) # # with db_env.begin(write=True) as txn: # txn = db_env.begin() # display(db_env) # exit() model = model_helper.ModelHelper(name="lmdbtest") # data, label = model.TensorProtosDBInput( # [], ["data", "label"], batch_size = batch_size, # db=dbpath, db_type="lmdb") reader = model.CreateDB("test_reader",db=dbpath,db_type="lmdb") data, label = brew.image_input( model, reader, ["data", "label"], batch_size=10, use_caffe_datum=False, mean=128., std=128., minsize=224, crop=224, mirror=1 ) workspace.RunNetOnce(model.param_init_net) workspace.CreateNet(model.net) for _ in range(0, 2): workspace.RunNet(model.net.Proto().name) img_datas = workspace.FetchBlob("data") labels = workspace.FetchBlob("label") print(labels) for k in xrange(0, 10): print(img_datas[k].shape)
def add_image_input_ops(model): ''' The image input operator loads image and label data from the reader and applies transformations to the images (random cropping, mirroring, ...). ''' data, label = brew.image_input( model, reader, ["data", "label"], batch_size=batch_per_device, # mean: to remove color values that are common # mean=128., # std is going to be modified randomly to influence the mean subtraction # std=128., # scale to rescale each image to a common size scale=224, # crop to the square each image to exact dimensions crop=224, # not running in test mode is_test=False, # mirroring of the images will occur randomly mirror=1 ) # prevent back-propagation: optional performance improvement; may not be observable at small scale data = model.StopGradient(data, data)
def test_simple_model(self): model = model_helper.ModelHelper(name="mnist") data, label = brew.image_input(model, ["db"], ["data", "label"], order="NCHW", use_gpu_transform=False, is_test=0) with core.NameScope("conv1"): conv1 = brew.conv(model, data, 'conv1', dim_in=1, dim_out=20, kernel=5) # Image size: 24 x 24 -> 12 x 12 pool1 = brew.max_pool(model, conv1, 'pool1', kernel=2, stride=2) # Image size: 12 x 12 -> 8 x 8 conv2 = brew.conv(model, pool1, 'conv2', dim_in=20, dim_out=100, kernel=5) # Image size: 8 x 8 -> 4 x 4 pool2 = brew.max_pool(model, conv2, 'pool2', kernel=2, stride=2) with core.NameScope("classifier"): # 50 * 4 * 4 stands for dim_out from previous layer multiplied by the image size fc3 = brew.fc(model, pool2, 'fc3', dim_in=100 * 4 * 4, dim_out=500) relu = brew.relu(model, fc3, fc3) pred = brew.fc(model, relu, 'pred', 500, 10) softmax = brew.softmax(model, pred, 'softmax') xent = model.LabelCrossEntropy([softmax, label], 'xent') # compute the expected loss loss = model.AveragedLoss(xent, "loss") model.net.RunAllOnGPU() model.param_init_net.RunAllOnGPU() model.AddGradientOperators([loss], skip=1) blob_name_tracker = {} graph = tb.model_to_graph_def( model, blob_name_tracker=blob_name_tracker, shapes={}, show_simplified=False, ) self.assertEqual( blob_name_tracker['GRADIENTS/conv1/conv1_b_grad'], 'conv1/conv1_b_grad', ) self.maxDiff = None # We can't guarantee the order in which they appear, so we sort # both before we compare them sep = "node {" expected = "\n".join( sorted(sep + "\n " + part.strip() for part in EXPECTED_MNIST.strip().split(sep) if part.strip())) actual = "\n".join( sorted(sep + "\n " + part.strip() for part in str(graph).strip().split(sep) if part.strip())) self.assertMultiLineEqual(actual, expected)
def ImageInput(self, blob_in, blob_out, use_gpu_transform=False, **kwargs): return brew.image_input( self, blob_in, blob_out, order=self.order, use_gpu_transform=use_gpu_transform, **kwargs )
def AddInputOps(model, reader, batch_size): """Add input ops.""" data, label = brew.image_input(model, [reader], ['data', 'label'], batch_size=batch_size, use_caffe_datum=False, use_gpu_transform=True, scale=28, crop=28, mirror=False, color=False, mean=128.0, std=256.0, is_test=True) data = model.StopGradient(data, data)
def test_extract_simple(self): from caffe2.python import brew from caffe2.python.model_helper import ModelHelper, ExtractPredictorNet model = ModelHelper(name="test", arg_scope={'order': 'NCHW'}) [data, label] = brew.image_input( model, "reader", ["xx/data", "label"], is_test=1, ) cnv = brew.conv(model, data, 'cnv', 32, 32, 4) a = brew.fc(model, cnv, 'a', 100, 200) pred = brew.fc(model, a, 'pred', 200, 5) brew.softmax(model, [pred, label], "softmax") (predict_net, export_blobs) = ExtractPredictorNet( net_proto=model.net.Proto(), input_blobs=["xx/data"], output_blobs=["pred"], renames={"xx/data": "image"}, ) export_blobs = set(export_blobs) ops = list(predict_net.Proto().op) for op in ops: self.assertFalse(op.type == "Softmax") self.assertFalse("xx/data" in op.input) # Note: image input should not be included self.assertEquals(ops[0].type, "Conv") self.assertEquals(ops[1].type, "FC") self.assertEquals(ops[2].type, "FC") self.assertEquals(len(ops), 3) # test rename happened self.assertEquals(ops[0].input[0], "image") # Check export blobs self.assertTrue("image" not in export_blobs) self.assertTrue("xx/data" not in export_blobs) self.assertEqual(set([str(p) for p in model.params]), export_blobs) # Check external inputs/outputs self.assertTrue("image" in predict_net.Proto().external_input) self.assertEquals(set(["pred"]), set(predict_net.Proto().external_output)) self.assertEqual( set(predict_net.Proto().external_input) - set([str(p) for p in model.params]), set(["image"]))
def read_data_db(dbpath): # count = [] # db_env = lmdb.open(dbpath)#, map_size=int(1024*1024*1024*30)) # size:30GB # with db_env.begin() as txn: # cur = txn.cursor() # for k, v in cur: # count.append(k) # print(dir(db_env.info())) # count = [] # with db_env.begin(write=True) as txn: # txn = db_env.begin() # count.append(display(db_env)) # print(count[:3]) # print(len(count)) # np.save("trainval.npy", count) # count = 50728 #================== model = model_helper.ModelHelper(name="lmdbtest") # data, label = model.TensorProtosDBInput( # [], ["data", "label"], batch_size = batch_size, # db=dbpath, db_type="lmdb") reader = model.CreateDB("test_reader",db=dbpath,db_type="lmdb") data, label = brew.image_input( model, reader, ["data", "label"], batch_size=batch_size, use_caffe_datum=False, mean=128., std=128., minsize=224, crop=224, mirror=1 ) workspace.RunNetOnce(model.param_init_net) workspace.CreateNet(model.net) for _ in range(0, 2): workspace.RunNet(model.net.Proto().name) img_datas = workspace.FetchBlob("data") print(img_datas.shape) labels = workspace.FetchBlob("label") for k in xrange(0, batch_size): img = img_datas[k] img = img.swapaxes(0, 1).swapaxes(1, 2) cv2.namedWindow('img', cv2.WINDOW_AUTOSIZE) cv2.imshow('img', img) cv2.waitKey(0) cv2.destroyAllWindows()
def test_extract_simple(self): from caffe2.python import brew from caffe2.python.model_helper import ModelHelper, ExtractPredictorNet model = ModelHelper(name="test", arg_scope={'order': 'NCHW'}) [data, label] = brew.image_input( model, "reader", ["xx/data", "label"], is_test=1, ) cnv = brew.conv(model, data, 'cnv', 32, 32, 4) a = brew.fc(model, cnv, 'a', 100, 200) pred = brew.fc(model, a, 'pred', 200, 5) brew.softmax(model, [pred, label], "softmax") (predict_net, export_blobs) = ExtractPredictorNet( net_proto=model.net.Proto(), input_blobs=["xx/data"], output_blobs=["pred"], renames={"xx/data": "image"}, ) export_blobs = set(export_blobs) ops = list(predict_net.Proto().op) for op in ops: self.assertFalse(op.type == "Softmax") self.assertFalse("xx/data" in op.input) # Note: image input should not be included self.assertEquals(ops[0].type, "Conv") self.assertEquals(ops[1].type, "FC") self.assertEquals(ops[2].type, "FC") self.assertEquals(len(ops), 3) # test rename happened self.assertEquals(ops[0].input[0], "image") # Check export blobs self.assertTrue("image" not in export_blobs) self.assertTrue("xx/data" not in export_blobs) self.assertEqual(set([str(p) for p in model.params]), export_blobs) # Check external inputs/outputs self.assertTrue("image" in predict_net.Proto().external_input) self.assertEquals(set(["pred"]), set(predict_net.Proto().external_output)) self.assertEqual( set(predict_net.Proto().external_input) - set([str(p) for p in model.params]), set(["image"]) )
def AddInputOps(model, reader, batch_size, dtype): """Add input ops.""" data, label = brew.image_input( model, [reader], ['data', 'label'], batch_size=batch_size, use_caffe_datum=False, use_gpu_transform=True, scale=32, crop=32, mirror=1, color=True, mean=128.0, output_type='float16' if dtype == DataType.FLOAT16 else 'float', is_test=False) data = model.StopGradient(data, data)
def AddImageInput(model, reader, batch_size, img_size): ''' Image input operator that loads data from reader and applies certain transformations to the images. ''' data, label = brew.image_input(model, reader, ["data", "label"], batch_size=batch_size, use_caffe_datum=False, mean=128., std=128., minsize=32, crop=img_size, mirror=1) data = model.StopGradient(data, data)
def AddImageInput(model, reader, batch_size, img_size, dtype, is_test): data, label = brew.image_input( model, reader, ["data", "label"], batch_size=batch_size, output_type=dtype, use_gpu_transform=True if model._device_type == 1 else False, use_caffe_datum=True, mean=128., std=128., scale=256, crop=img_size, mirror=1, is_test=is_test, ) data = model.StopGradient(data, data)
def test_tensorboard_graphs(self): model = model_helper.ModelHelper(name="overfeat") data, label = brew.image_input( model, ["db"], ["data", "label"], is_test=0 ) with core.NameScope("conv1"): conv1 = brew.conv(model, data, "conv1", 3, 96, 11, stride=4) relu1 = brew.relu(model, conv1, conv1) pool1 = brew.max_pool(model, relu1, "pool1", kernel=2, stride=2) with core.NameScope("classifier"): fc = brew.fc(model, pool1, "fc", 4096, 1000) pred = brew.softmax(model, fc, "pred") xent = model.LabelCrossEntropy([pred, label], "xent") loss = model.AveragedLoss(xent, "loss") model.AddGradientOperators([loss], skip=1) c2_dir = tempfile.mkdtemp() tf_dir = tempfile.mkdtemp() with open(os.path.join(c2_dir, "init"), "w") as f: f.write(str(model.param_init_net.Proto())) with open(os.path.join(c2_dir, "net"), "w") as f: f.write(str(model.net.Proto())) runner = click.testing.CliRunner() result = runner.invoke( tb.cli, ["tensorboard-graphs", "--c2-netdef", os.path.join(c2_dir, "init"), "--c2-netdef", os.path.join(c2_dir, "net"), "--tf-dir", tf_dir]) self.assertEqual(result.exit_code, 0) entries = list(os.walk(tf_dir)) self.assertEqual(len(entries), 1) ((d, _, (fname,)),) = entries self.assertEqual(tf_dir, d) events = list(tf.train.summary_iterator(os.path.join(tf_dir, fname))) self.assertEqual(len(events), 3) events = events[1:] nets = [model.param_init_net, model.net] for i, (event, net) in enumerate(zip(events, nets), start=1): self.assertEqual(event.step, i) self.assertEqual(event.wall_time, i) g = tf.GraphDef() g.ParseFromString(event.graph_def) self.assertMultiLineEqual( str(g), str(tb_exporter.nets_to_graph_def([net])))
def test_tensorboard_graphs(self): model = model_helper.ModelHelper(name="overfeat") data, label = brew.image_input( model, ["db"], ["data", "label"], is_test=0 ) with core.NameScope("conv1"): conv1 = brew.conv(model, data, "conv1", 3, 96, 11, stride=4) relu1 = brew.relu(model, conv1, conv1) pool1 = brew.max_pool(model, relu1, "pool1", kernel=2, stride=2) with core.NameScope("classifier"): fc = brew.fc(model, pool1, "fc", 4096, 1000) pred = brew.softmax(model, fc, "pred") xent = model.LabelCrossEntropy([pred, label], "xent") loss = model.AveragedLoss(xent, "loss") model.AddGradientOperators([loss], skip=1) c2_dir = tempfile.mkdtemp() tf_dir = tempfile.mkdtemp() with open(os.path.join(c2_dir, "init"), "w") as f: f.write(str(model.param_init_net.Proto())) with open(os.path.join(c2_dir, "net"), "w") as f: f.write(str(model.net.Proto())) runner = click.testing.CliRunner() result = runner.invoke( tb.cli, ["tensorboard-graphs", "--c2-netdef", os.path.join(c2_dir, "init"), "--c2-netdef", os.path.join(c2_dir, "net"), "--tf-dir", tf_dir]) self.assertEqual(result.exit_code, 0) entries = list(os.walk(tf_dir)) self.assertEqual(len(entries), 1) ((d, _, (fname,)),) = entries self.assertEqual(tf_dir, d) events = load_events(os.path.join(tf_dir, fname)) self.assertEqual(len(events), 3) events = events[1:] nets = [model.param_init_net, model.net] for i, (event, net) in enumerate(zip(events, nets), start=1): self.assertEqual(event.step, i) self.assertEqual(event.wall_time, i) g = GraphDef() g.ParseFromString(event.graph_def) self.assertMultiLineEqual( str(g), str(tb_exporter.nets_to_graph_def([net])))
def AddImageInput(model, reader, batch_size, img_size, data_type, cpu_based, mean=128., std=128., scale=256, mirror=1, is_test=False): """ Adds an image input to the model, supplied via a reader. :param model: the caffe2 model to which we're adding the image input :param reader: the reader which supplies the images from a DB :param batch_size: the batch size :param img_size: specifies the size of the images (images are considered to be square, and will be cropped) :param data_type: the output data type (float or float16) :param cpu_based: indicates if this run is CPU based :param mean: the global channel mean value (used for normalization purposes) :param std: the global channel deviation (used for normalization purposes) :param scale: the scale variable (values will be scaled by 1./scale) :param mirror: indicates whether or not to mirror the available images for extra robustness (1 = True; 0 = False) :param is_test: if True, then the crop will be made at the exact same position in the image """ data, label = brew.image_input( model, reader, ["data", "label"], batch_size=batch_size, output_type=data_type, use_gpu_transform=not cpu_based, use_caffe_datum=True, # mean_per_channel takes precedence over mean mean=float(mean), std=float(std), scale=scale, crop=img_size, mirror=mirror, is_test=is_test, ) # Avoid updating the data itself data = model.StopGradient(data, data)
def AddImageInput(model, reader, batch_size, is_test): data, label = brew.image_input( model, reader, ["data", "label"], batch_size=batch_size, output_type='float', use_gpu_transform=True, use_caffe_datum=False, #mean=128., #std=128., std=57.375, mean=114.75, scale=227, crop=227, mirror=1, is_test=is_test, ) data = model.StopGradient(data, data)
def AddImageInput(model, reader, batch_size, img_size, dtype): ''' The image input operator loads image and label data from the reader and applies transformations to the images (random cropping, mirroring, ...). ''' data, label = brew.image_input( model, reader, ["data", "label"], batch_size=batch_size, output_type=dtype, use_gpu_transform=True if model._device_type == 1 else False, use_caffe_datum=True, mean=128., std=128., scale=256, crop=img_size, mirror=1 ) data = model.StopGradient(data, data)
def add_image_input_ops(model): # utilize the ImageInput operator to prep the images data, label = brew.image_input( model, reader, ["data", "label"], batch_size=batch_per_device, # mean: to remove color values that are common mean=128., # std is going to be modified randomly to influence the mean subtraction std=128., # scale to rescale each image to a common size scale=256, # crop to the square each image to exact dimensions crop=224, # not running in test mode is_test=False, # mirroring of the images will occur randomly mirror=1 ) # prevent back-propagation: optional performance improvement; may not be observable at small scale data = model.net.StopGradient(data, data)
def add_image_input_ops(model): # utilize the ImageInput operator to prep the images data, label = brew.image_input( model, reader, ["data", "label"], batch_size=args.batch_size, # mean: to remove color values that are common mean=128., # std is going to be modified randomly to influence the mean subtraction std=128., # scale to rescale each image to a common size scale=256, # crop to the square each image to exact dimensions crop=224, # not running in test mode is_test=False, # mirroring of the images will occur randomly mirror=1, use_caffe_datum=False, ) # prevent back-propagation: optional performance improvement; may not be observable at small scale data = model.net.StopGradient(data, data)
def AddImageInput(model, reader, batch_size, img_size, dtype, is_test): ''' The image input operator loads image and label data from the reader and applies transformations to the images (random cropping, mirroring, ...). ''' data, label = brew.image_input( model, reader, ["data", "label"], batch_size=batch_size, # output_type=dtype, use_gpu_transform=False, use_caffe_datum=False, # mean=128., # std=128., scale=img_size, crop=img_size, db_type='lmdb', # mirror=1, is_test=is_test, ) data = model.StopGradient(data, data) return data, label
import caffe2.contrib.tensorboard.tensorboard as tb import caffe2.contrib.tensorboard.tensorboard_exporter as tb_exporter from caffe2.python import brew, core, model_helper model = model_helper.ModelHelper(name="overfeat") data, label = brew.image_input(model, ["db"], ["data", "label"], is_test=0) with core.NameScope("conv1"): conv1 = brew.conv(model, data, "conv1", 3, 96, 11, stride=4) relu1 = brew.relu(model, conv1, conv1) pool1 = brew.max_pool(model, relu1, "pool1", kernel=2, stride=2) with core.NameScope("conv2"): conv2 = brew.conv(model, pool1, "conv2", 96, 256, 5) relu2 = brew.relu(model, conv2, conv2) pool2 = brew.max_pool(model, relu2, "pool2", kernel=2, stride=2) with core.NameScope("conv3"): conv3 = brew.conv(model, pool2, "conv3", 256, 512, 3, pad=1) relu3 = brew.relu(model, conv3, conv3) with core.NameScope("conv4"): conv4 = brew.conv(model, relu3, "conv4", 512, 1024, 3, pad=1) relu4 = brew.relu(model, conv4, conv4) with core.NameScope("conv5"): conv5 = brew.conv(model, relu4, "conv5", 1024, 1024, 3, pad=1) relu5 = brew.relu(model, conv5, conv5) pool5 = brew.max_pool(model, relu5, "pool5", kernel=2, stride=2) with core.NameScope("fc6"): fc6 = brew.fc(model, pool5, "fc6", 1024 * 6 * 6, 3072) relu6 = brew.relu(model, fc6, "fc6") with core.NameScope("fc7"): fc7 = brew.fc(model, relu6, "fc7", 3072, 4096) relu7 = brew.relu(model, fc7, "fc7")