caffe 网络#

%cd ../..
import set_env
from caffe_utils import _test_network
import numpy as np
from tvm.contrib.download import download_testdata
from caffe import params as P

caffe Mobilenetv2#

def _test_mobilenetv2(data):
    """One iteration of Mobilenetv2"""
    mean_val = np.array([103.939, 116.779, 123.68], dtype=np.float32)
    mean_val = np.reshape(mean_val, (1, 3, 1, 1))
    mean_val = np.tile(mean_val, (1, 1, 224, 224))
    data_process = data - mean_val
    data_process = data_process / 58.8
    data_process = data_process.astype(np.float32)

    proto_file_url = (
        "https://github.com/shicai/MobileNet-Caffe/raw/master/mobilenet_v2_deploy.prototxt"
    )
    blob_file_url = (
        "https://github.com/shicai/MobileNet-Caffe/blob/master/mobilenet_v2.caffemodel?raw=true"
    )
    proto_file = download_testdata(proto_file_url, "mobilenetv2.prototxt", module="model")
    blob_file = download_testdata(blob_file_url, "mobilenetv2.caffemodel", module="model")
    _test_network(data_process, proto_file, blob_file)
data = np.random.randint(0, 256, size=(1, 3, 224, 224)).astype(np.float32)
_test_mobilenetv2(data)

caffe Alexnet#

import pytest
def _test_alexnet(data):
    """One iteration of Alexnet"""
    mean_val = np.array([103.939, 116.779, 123.68], dtype=np.float32)
    mean_val = np.reshape(mean_val, (1, 3, 1, 1))
    mean_val = np.tile(mean_val, (1, 1, 227, 227))
    data_process = data - mean_val
    data_process = data_process.astype(np.float32)

    proto_file_url = (
        "https://github.com/BVLC/caffe/raw/master/models/" + "bvlc_alexnet/deploy.prototxt"
    )
    blob_file_url = "http://dl.caffe.berkeleyvision.org/bvlc_alexnet.caffemodel"
    proto_file = download_testdata(proto_file_url, "alexnet.prototxt", module="model")
    blob_file = download_testdata(blob_file_url, "alexnet.caffemodel", module="model")
    _test_network(data_process, proto_file, blob_file)


@pytest.mark.skip(reason="See https://github.com/apache/tvm/issues/13227")
def test_forward_Alexnet():
    """Alexnet"""
    data = np.random.randint(0, 256, size=(1, 3, 227, 227)).astype(np.float32)
    _test_alexnet(data)

caffe Resnet50#

def _test_resnet50(data):
    """One iteration of Resnet50"""
    mean_val = np.array([103.939, 116.779, 123.68], dtype=np.float32)
    mean_val = np.reshape(mean_val, (1, 3, 1, 1))
    mean_val = np.tile(mean_val, (1, 1, 224, 224))
    data_process = data - mean_val
    data_process = data_process.astype(np.float32)

    proto_file_url = (
        "https://github.com/fernchen/CaffeModels/raw/master/resnet/ResNet-50-deploy.prototxt"
    )
    blob_file_url = (
        "https://github.com/fernchen/CaffeModels/raw/master/resnet/ResNet-50-model.caffemodel"
    )

    proto_file = download_testdata(proto_file_url, "resnet50.prototxt", module="model")
    blob_file = download_testdata(blob_file_url, "resnet50.caffemodel", module="model")

    _test_network(data_process, proto_file, blob_file)


def test_forward_Resnet50():
    """Resnet50"""
    data = np.random.randint(0, 256, size=(1, 3, 224, 224)).astype(np.float32)
    _test_resnet50(data)

caffe Inceptionv4#

def _test_inceptionv1(data):
    """One iteration of Inceptionv4"""
    mean_val = np.array([103.939, 116.779, 123.68], dtype=np.float32)
    mean_val = np.reshape(mean_val, (1, 3, 1, 1))
    mean_val = np.tile(mean_val, (1, 1, 224, 224))
    data_process = data - mean_val
    data_process = data_process / 58.8
    data_process = data_process.astype(np.float32)

    proto_file_url = (
        "https://github.com/BVLC/caffe/raw/master/models" + "/bvlc_googlenet/deploy.prototxt"
    )
    blob_file_url = "http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel"
    proto_file = download_testdata(proto_file_url, "inceptionv1.prototxt", module="model")
    blob_file = download_testdata(blob_file_url, "inceptionv1.caffemodel", module="model")
    _test_network(data_process, proto_file, blob_file)


@pytest.mark.skip(reason="See issue https://github.com/apache/tvm/issues/13227")
def test_forward_Inceptionv1():
    """Inceptionv4"""
    data = np.random.randint(0, 256, size=(1, 3, 224, 224)).astype(np.float32)
    _test_inceptionv1(data)