-2

I would like to edit a prototxt file using python. I simply need to change the source from one directory to another. Is there a simple way to go about this, to not have to redefine the net and rewrite the entire file?

For example, in my prototxt file I have

layer {
  name: "data"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    mirror: true
    crop_size: 227
    mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"
  }
  data_param {
    source: "examples/imagenet/ilsvrc12_train_lmdb"
    batch_size: 256
    backend: LMDB
  }
}

and I ONLY want to change the field 'source'. This is from a relatively long net def (posted here is just a typical example, I have my own net def), so not having to manually enter every field like in the caffe examples would save me lots of time

Thanks

jerpint
  • 379
  • 2
  • 14
  • you would have to read / parse the file, make the change, then write it all back. Modifying the file in place would mean you would need to write the exact same number of characters as you remove or you risk corrupting the entire file. – Tadhg McDonald-Jensen Jun 17 '16 at 15:35

1 Answers1

1

I suspect that you'd have an easier time if you use your operating system's command line. UNIX (Linux) has good editors (sed, awk, etc.). Make the OS handle the file open, change, and rewrite for you.

The command you need to build would find source: and replace the remainder of the line with a string you provide.

The applicable Python includes:

import os
os.system("<your command>")

Also see this SO question.

Community
  • 1
  • 1
Prune
  • 72,213
  • 14
  • 48
  • 72
  • Thanks, that's what I will probably do , I can edit them with gedit from terminal, so this might be the simplest solution – jerpint Jun 19 '16 at 14:12