{"id":18419892,"url":"https://github.com/shichenliu/caffe-2-tensorflow","last_synced_at":"2025-04-07T13:31:49.087Z","repository":{"id":85878270,"uuid":"80135563","full_name":"ShichenLiu/caffe-2-tensorflow","owner":"ShichenLiu","description":"A basic instruction on how to transfer caffe project to tensorflow","archived":false,"fork":false,"pushed_at":"2017-02-09T18:13:11.000Z","size":9,"stargazers_count":10,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-22T19:12:15.062Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ShichenLiu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-26T17:09:17.000Z","updated_at":"2019-05-08T09:52:56.000Z","dependencies_parsed_at":null,"dependency_job_id":"234b5cd0-2f63-4ae4-be44-e875f33106b1","html_url":"https://github.com/ShichenLiu/caffe-2-tensorflow","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShichenLiu%2Fcaffe-2-tensorflow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShichenLiu%2Fcaffe-2-tensorflow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShichenLiu%2Fcaffe-2-tensorflow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShichenLiu%2Fcaffe-2-tensorflow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ShichenLiu","download_url":"https://codeload.github.com/ShichenLiu/caffe-2-tensorflow/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247661795,"owners_count":20975118,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T04:18:53.906Z","updated_at":"2025-04-07T13:31:49.079Z","avatar_url":"https://github.com/ShichenLiu.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# From Caffe to Tensorflow\nThis is an instruction on how to transfer caffe project to tensorflow. If you are not familiar with tensorflow or don't understand what is going on, please refer to [Tensorflow Section](http://???)\n**Under construction, not finished yet**\n\n## Basic knowledge\nLet me clarify some principles at first. If you have an existing project implemented on Caffe, and you want to try something new on Tensorflow, where you hope Tensorflow to behave exactly the same as Caffe, then you can keep reading. Remember, when you meet any problem and have to debug, you should diminish all randomizations which I will explain as below one by one. Notice that some of the scripts are found in stackoverflow. Finally an example will be given to you.\n\n## Load Image\nAs we know, caffe use **BGR** channels as input. So if you are using *cv2* lib to load image, then don't worry, because this lib also read image as **BGR**, otherwise you should take care. However, there is another problem on loading image, the *mean file*. There are two ways to set mean in caffe\n\n1. mean value: 3 numbers are given which indicates three channels' mean value, you only need to subtract respectively.\n2. mean file: a mean file is givin, typically a *binaryproto* file. This is a pixelwise mean file, it is simply an *uint8* [3, width, height] matrix. The best way is to convert this file to numpy matrix and save as *npy* file. To achieve this with below script, *pycaffe* is needed.\n\n```python\nimport caffe\nimport numpy as np\n\nblob = caffe.proto.caffe_pb2.BlobProto()\ndata = open('data/ilsvrc12/imagenet_mean.binaryproto', 'rb').read()\nblob.ParseFromString(data)\narray = np.array(caffe.io.blobproto_to_array(blob))\nout = array[0]\nnp.save('imagenet_mean.npy', out)\n```\n\nThen you can load images with following codes\n\n```python\ndef preprocess_img(self, img, batch_size, train_phase, oversample=False):\n    '''\n    pre-process input image:\n    Args:\n        img: 4-D tensor\n        batch_size: Int \n        train_phase: Bool\n    Return:\n        distorted_img: 4-D tensor\n    '''\n    reshaped_image = tf.cast(img, tf.float32)\n    mean = tf.constant(np.load(self.mean_file), dtype=tf.float32, shape=[1, 256, 256, 3])\n    reshaped_image -= mean\n    crop_height = IMAGE_SIZE\n    crop_width = IMAGE_SIZE\n    if train_phase:\n        distorted_img = tf.pack([tf.random_crop(tf.image.random_flip_left_right(each_image), [crop_height, crop_width, 3]) for each_image in tf.unpack(reshaped_image)])\n    else:\n        if oversample:\n            distorted_img1 = tf.pack([tf.image.crop_to_bounding_box(tf.image.flip_left_right(each_image), 0, 0, crop_height, crop_width) for each_image in tf.unpack(reshaped_image)])\n            distorted_img2 = tf.pack([tf.image.crop_to_bounding_box(tf.image.flip_left_right(each_image), 28, 28, crop_height, crop_width) for each_image in tf.unpack(reshaped_image)])\n            distorted_img3 = tf.pack([tf.image.crop_to_bounding_box(tf.image.flip_left_right(each_image), 28, 0, crop_height, crop_width) for each_image in tf.unpack(reshaped_image)])\n            distorted_img4 = tf.pack([tf.image.crop_to_bounding_box(tf.image.flip_left_right(each_image), 0, 28, crop_height, crop_width) for each_image in tf.unpack(reshaped_image)])\n            distorted_img5 = tf.pack([tf.image.crop_to_bounding_box(tf.image.flip_left_right(each_image), 14, 14, crop_height, crop_width) for each_image in tf.unpack(reshaped_image)])\n            distorted_img6 = tf.pack([tf.image.crop_to_bounding_box(each_image, 0, 0, crop_height, crop_width) for each_image in tf.unpack(reshaped_image)])\n            distorted_img7 = tf.pack([tf.image.crop_to_bounding_box(each_image, 28, 28, crop_height, crop_width) for each_image in tf.unpack(reshaped_image)])\n            distorted_img8 = tf.pack([tf.image.crop_to_bounding_box(each_image, 28, 0, crop_height, crop_width) for each_image in tf.unpack(reshaped_image)])\n            distorted_img9 = tf.pack([tf.image.crop_to_bounding_box(each_image, 0, 28, crop_height, crop_width) for each_image in tf.unpack(reshaped_image)])\n            distorted_img0 = tf.pack([tf.image.crop_to_bounding_box(each_image, 14, 14, crop_height, crop_width) for each_image in tf.unpack(reshaped_image)])            \n            distorted_img = tf.concat(0, [distorted_img1, distorted_img2, distorted_img3, distorted_img4, distorted_img5, distorted_img6, distorted_img7, distorted_img8, distorted_img9, distorted_img0])\n        else:\n            distorted_img = tf.pack([tf.image.crop_to_bounding_box(each_image, 14, 14, crop_height, crop_width) for each_image in tf.unpack(reshaped_image)])\n    return distorted_img\n```\n\nIf you have to debug, here are something you need to notice:\n\n1. random shuffle: each epoch caffe would random shuffle all data. Modify shuffle option in the prototxt.\n2. mirror: at train phase, each image might have 0.5 possibility to be left-right flipped. Modify mirror option in the prototxt.\n3. random crop: even though each image have already been resized to 256x256, it will further be cropped to 227x227. One way is to use a monocolor image that is identical to crop, the other is to modify data_transform.cpp.\n4. pretrain model: use a pretrain model that can cover all variables.\n5. drop out:\n6. after above, there should be no more randomization in your debugging.\n\n## Convolutional\nThere are two ways of padding in Tensorflow\n\n1. VALID: *aka.* zero-padding, always starts from top-left, ignoring bottom-most and right-most pixels.\n2. SAME: try to even padding at left and right\n\ne.g. Conv1 layer in Alexnet is VALID padding, while others are SAME padding as well as pooling layers.\n\n## Optimizer Strategy\n\nThere are only one formula given by tensorflow, however you can make your own optimizer strategy with it.\ne.g.\n```python\n### Step\nself.lr = tf.train.exponential_decay(self.learning_rate, self.global_step, self.decay_step, self.decay_factor)\n### Inv\nself.lr = tf.train.exponential_decay(self.learning_rate, -0.75, 1.0, 1.0+0.002*tf.cast(self.global_step, tf.float32))\n```\n\n## Train \u0026 Validation\n\nYou can extract features using Alexnet and then classify like this\n```python\nsource_img = tf.placeholder(tf.float32, \n    [batch_size, 256, 256, 3])\ntest_img = tf.placeholder(tf.float32, \n    [1, 256, 256, 3])\nsource_label = tf.placeholder(tf.float32, \n    [batch_size, n_class])\ntest_label = tf.placeholder(tf.float32, \n    [1, n_class])\nglobal_step = tf.Variable(0, trainable=False)\n### Construct CNN\ncnn = Alexnet(model_weights)\n### Construct train net\nsource_img = preprocess_img(source_img, batch_size, True)\nwith tf.variable_scope(\"cnn\"):\n    source_feature = cnn.extract(source_img)\nlr_mult = cnn.lr_mult\nwith tf.variable_scope(\"classifier\"):\n    source_fc8 = classifier(source_feature)\n### Construct test net\nlog('setup', 'Construct Test Net')\ntest_img = preprocess_img(test_img, 1, False)\nwith tf.variable_scope(\"cnn\", reuse=True):\n    test_feature = cnn.extract(test_img, train_phase=False)\nwith tf.variable_scope(\"classifier\", reuse=True):\n    test_fc8 = classifier(test_feature)\ntest_output = tf.reduce_mean(test_fc8, 0)\ntest_result = tf.equal(tf.argmax(test_output, 0), tf.argmax(test_label, 1))\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshichenliu%2Fcaffe-2-tensorflow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshichenliu%2Fcaffe-2-tensorflow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshichenliu%2Fcaffe-2-tensorflow/lists"}