<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Getting Caffe1 Models and Datasets
This tutorial will help you acquire a variety of pre-trained models from the original Caffe repo, and translate these models to a format that Caffe2 expects. If you don't already have the Caffe repo, then clone it like so
Step1:  Now you can setup your root folder for Caffe below if you put it somewhere else. You should only be changing the path that's being set for CAFFE_ROOT.
Step2:  Here's where you pick your model. There are several listed below such as AlexNet, GoogleNet, and Flickr Style. Uncomment the model you want to download.
Step3:  As a reminder, in Caffe, the deploy model is saved in two parts
Step4:  Now that we have both the deploy.prototxt and .caffemodel files, we can translate the model to the Caffe2 saved model format, which consists of two serialized protobuf files
<ASSISTANT_TASK:>
Python Code:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
print("Required modules imported.")
Explanation: Getting Caffe1 Models and Datasets
This tutorial will help you acquire a variety of pre-trained models from the original Caffe repo, and translate these models to a format that Caffe2 expects. If you don't already have the Caffe repo, then clone it like so:
git clone https://github.com/BVLC/caffe.git
Start by importing the required modules.
End of explanation
# You should have checked out original Caffe
# git clone https://github.com/BVLC/caffe.git
# change the CAFFE_ROOT directory below accordingly
CAFFE_ROOT = os.path.expanduser('~/caffe')
# Make sure Caffe exists where you specified
if not os.path.exists(CAFFE_ROOT):
    print("Houston, you may have a problem.") 
    print("Did you change CAFFE_ROOT to point to your local Caffe repo?")
    print("Try running: git clone https://github.com/BVLC/caffe.git")
Explanation: Now you can setup your root folder for Caffe below if you put it somewhere else. You should only be changing the path that's being set for CAFFE_ROOT.
End of explanation
# Pick a model, and if you don't have it, it will be downloaded
# format below is the model's folder, model's dataset inside that folder
#MODEL = 'bvlc_alexnet', 'bvlc_alexnet.caffemodel' 
#MODEL = 'bvlc_googlenet', 'bvlc_googlenet.caffemodel'
#MODEL = 'finetune_flickr_style', 'finetune_flickr_style.caffemodel'
#MODEL = 'bvlc_reference_caffenet', 'bvlc_reference_caffenet.caffemodel'
MODEL = 'bvlc_reference_rcnn_ilsvrc13', 'bvlc_reference_rcnn_ilsvrc13.caffemodel'
Explanation: Here's where you pick your model. There are several listed below such as AlexNet, GoogleNet, and Flickr Style. Uncomment the model you want to download.
End of explanation
# Scripts to download the models reside here (~/caffe/models)
# After downloading the data will exist with the script
CAFFE_MODELS = os.path.join(CAFFE_ROOT, 'models')
# this is like: ~/caffe/models/bvlc_alexnet/deploy.prototxt
CAFFE_MODEL_FILE = os.path.join(CAFFE_MODELS, MODEL[0], 'deploy.prototxt')
# this is like: ~/caffe/models/bvlc_alexnet/bvlc_alexnet.caffemodel
CAFFE_PRETRAINED = os.path.join(CAFFE_MODELS, MODEL[0], MODEL[1])
    
# If the model folder doesn't have the goods, then download it
# This is usually a pretty big file with the .caffemodel extension
if not os.path.exists(CAFFE_PRETRAINED):
    print(CAFFE_PRETRAINED + " not found. Attempting download. Be patient...\n")
    os.system(
        os.path.join(CAFFE_ROOT, 'scripts/download_model_binary.py') +
        ' ' +
        os.path.join(CAFFE_ROOT, 'models', MODEL[0]))
else:
    print("You already have " + CAFFE_PRETRAINED + ", skipping download...\n")
# If the .prototxt file was missing then you're in trouble; cannot continue
if not os.path.exists(CAFFE_MODEL_FILE):
    print("Caffe model file, " + CAFFE_MODEL_FILE + " was not found!")
else:
    print("Both the deploy.prototxt and .caffemodel files were found, ready to continue!")
    # Now we have init net and predict net .pb files to use
Explanation: As a reminder, in Caffe, the deploy model is saved in two parts:
1) deploy.prototxt: contained the network architecture in human-readable protobuf format
2) .caffemodel file: contained the model weights and parameters for loading
Therefore, to translate the model to Caffe2, we need both of these files. We already have the deploy.prototxt files for all of the models in ~/caffe/models, so we need the learned weights.
Below, we'll check to see if the .caffemodel file from the last model that we uncommented above already exists. If it does not already exist in the location that we specify, we will download it using the download_model_binary.py script in the Caffe repo. Note that .caffemodel files are typically fairly large files, so downloading one will take a few moments. We will be sure to print a message so we know when we can continue.
End of explanation
# Set the CAFFE2_ROOT
CAFFE2_ROOT = os.path.expanduser('~/caffe2')
init_net_out = os.path.join(CAFFE_MODELS, MODEL[0], 'init_net.pb')
predict_net_out = os.path.join(CAFFE_MODELS, MODEL[0], 'predict_net.pb')
# Run the caffe_translator.py script to translate to Caffe2 if files do not already exist
if (not os.path.exists(init_net_out)) or (not os.path.exists(predict_net_out)):
    print("Protobuf files not found. Running translation. Be patient...\n")
    os.system(
        'python' + ' ' + os.path.join(CAFFE2_ROOT, 'caffe2/python/caffe_translator.py') +
        ' ' + CAFFE_MODEL_FILE + ' ' + CAFFE_PRETRAINED + ' ' + 
        '--init_net' + ' ' + init_net_out + ' ' +
        '--predict_net' + ' ' + predict_net_out
    )
else:
    print("You already have both .pb files, skipping translation...\n")    
# Print if files are where they are expected to be
if (not os.path.exists(init_net_out)) or (not os.path.exists(predict_net_out)):
    print(init_net_out + " and/or " + predict_net_out + " was NOT FOUND!")
else:
    print("Protobuf files can be found at: \n", 
              os.path.join(CAFFE_MODELS, MODEL[0])), "!"
Explanation: Now that we have both the deploy.prototxt and .caffemodel files, we can translate the model to the Caffe2 saved model format, which consists of two serialized protobuf files:
1) init_net.pb
2) predict_net.pb
To do this, we will use Caffe2's translator script at ~/caffe2/caffe2/python/caffe_translator.py.
Again, depending on the size of the model, this may take a minute or two
End of explanation
<END_TASK>