Esempio n. 1
0
 def get_deploy_file_content(self):
     return get_file_content(self.deploy_file)
Esempio n. 2
0
 def get_model_file_content(self):
     return get_file_content(self.model_file)
Esempio n. 3
0
 def get_solver_file_content(self):
     return get_file_content(self.solver_file)
Esempio n. 4
0
def download_snapshot(snapshot_id, transfer):
    snapshot = CaffeCNNSnapshot.objects.get(id=snapshot_id)
    snapshot_dir = os.path.join(
        'snapshots',
        str(snapshot_id),
    )
    snapshot_dirpath = os.path.join(settings.CAFFE_ROOT, snapshot_dir)
    ensuredir(snapshot_dirpath)
    weights_relpath = os.path.join(
        snapshot_dir,
        'snapshot.caffemodel'
    )
    deployfile_relpath = os.path.join(
        snapshot_dir,
        'deploy.prototxt'
    )
    trainfile_relpath = os.path.join(
        snapshot_dir,
        'train_val.prototxt'
    )
    weights_path = os.path.join(settings.CAFFE_ROOT, weights_relpath)
    if not os.path.exists(weights_path):
        print 'Downloading snapshot {}...'.format(weights_path)
        snapshot_content = get_file_content(snapshot.model_snapshot)
        sha1 = hashlib.sha1(snapshot_content).hexdigest()

        if sha1 != snapshot.sha1:
            raise ValueError(
                'The SHA1 digest of the downloaded snapshot '
                'doesn\'t match the uploaded one'
            )
        safe_save_content(snapshot_dirpath, weights_path, snapshot_content)

    deployfile_path = os.path.join(settings.CAFFE_ROOT, deployfile_relpath)
    if not os.path.exists(deployfile_path):
        safe_save_content(
            snapshot_dirpath, deployfile_path,
            snapshot.training_run.get_deploy_file_content()
        )

    trainfile_path = os.path.join(settings.CAFFE_ROOT, trainfile_relpath)
    if not os.path.exists(trainfile_path):
        safe_save_content(
            snapshot_dirpath, trainfile_path,
            snapshot.training_run.get_model_file_content()
        )

    if transfer:
        # Transfer the weights so they are compatible with the fully
        # convolutional network
        transferred_weights_relpath = os.path.join(
            snapshot_dir,
            'snapshot-conv.caffemodel'
        )
        if not os.path.exists(os.path.join(settings.CAFFE_ROOT, transferred_weights_relpath)):
            transfer_weights(
                temp_dir=snapshot_dirpath,
                deployfile_source_path=os.path.join(settings.CAFFE_ROOT, trainfile_relpath),
                weights_source_path=os.path.join(settings.CAFFE_ROOT, weights_relpath),
                deployfile_target_path=os.path.join(settings.CAFFE_ROOT, deployfile_relpath),
                weights_target_path=os.path.join(settings.CAFFE_ROOT, transferred_weights_relpath),
                verbose=True,
            )
    else:
        transferred_weights_relpath = weights_relpath

    return deployfile_relpath, transferred_weights_relpath