Пример #1
0
def get_inference_augmentor():
  meta = ILSVRCMeta()
  pp_mean = meta.get_per_pixel_mean()
  pp_mean_224 = pp_mean[16:-16, 16:-16, :]

  transformers = imgaug.AugmentorList([
    imgaug.ResizeShortestEdge(256),
    imgaug.CenterCrop((224, 224)),
    imgaug.MapImage(lambda x: x - pp_mean_224),
  ])
  return transformers
Пример #2
0
def run_test(path, input):
    param_dict = dict(np.load(path))
    predict_func = OfflinePredictor(
        PredictConfig(
            inputs_desc=[InputDesc(tf.float32, (None, 224, 224, 3), 'input')],
            tower_func=tower_func,
            session_init=DictRestore(param_dict),
            input_names=['input'],
            output_names=['prob']  # prob:0 is the probability distribution
        ))

    im = cv2.imread(input)
    assert im is not None, input
    im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
    im = cv2.resize(im, (224, 224)).reshape((1, 224, 224, 3)).astype('float32')

    # VGG19 requires channelwise mean substraction
    VGG_MEAN = [103.939, 116.779, 123.68]
    im -= VGG_MEAN[::-1]
    outputs = predict_func(im)[0]
    prob = outputs[0]
    ret = prob.argsort()[-10:][::-1]
    print("Top10 predictions:", ret)

    meta = ILSVRCMeta().get_synset_words_1000()
    print("Top10 class names:", [meta[k] for k in ret])
Пример #3
0
def run_test(path, input):
    param_dict = np.load(path).item()

    pred_config = PredictConfig(
        model=Model(),
        input_var_names=['input'],
        session_init=ParamRestore(param_dict),
        session_config=get_default_sess_config(0.9),
        output_var_names=['output']  # output:0 is the probability distribution
    )
    predict_func = get_predict_func(pred_config)

    import cv2
    im = cv2.imread(input)
    assert im is not None
    im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
    im = cv2.resize(im, (224, 224))
    im = np.reshape(im, (1, 224, 224, 3)).astype('float32')
    im = im - 110
    outputs = predict_func([im])[0]
    prob = outputs[0]
    ret = prob.argsort()[-10:][::-1]
    print(ret)

    meta = ILSVRCMeta().get_synset_words_1000()
    print([meta[k] for k in ret])
Пример #4
0
def run_test(path, input):
    param_dict = dict(np.load(path))
    param_dict = {k.replace('/W', '/kernel').replace('/b', '/bias'): v for k, v in six.iteritems(param_dict)}

    predict_func = OfflinePredictor(PredictConfig(
        input_signature=[tf.TensorSpec((None, 224, 224, 3), tf.float32, 'input')],
        tower_func=tower_func,
        session_init=SmartInit(param_dict),
        input_names=['input'],
        output_names=['prob']   # prob:0 is the probability distribution
    ))

    im = cv2.imread(input)
    assert im is not None, input
    im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
    im = cv2.resize(im, (224, 224)).reshape((1, 224, 224, 3)).astype('float32')

    # VGG19 requires channelwise mean substraction
    VGG_MEAN = [103.939, 116.779, 123.68]
    im -= VGG_MEAN[::-1]
    outputs = predict_func(im)[0]
    prob = outputs[0]
    ret = prob.argsort()[-10:][::-1]
    print("Top10 predictions:", ret)

    meta = ILSVRCMeta().get_synset_words_1000()
    print("Top10 class names:", [meta[k] for k in ret])
Пример #5
0
def run_test(path, input):
    param_dict = np.load(path).item()

    pred_config = PredictConfig(
        model=Model(),
        input_data_mapping=[0],
        session_init=ParamRestore(param_dict),
        output_var_names=['output:0', 'pool5/MaxPool:0'])
    predict_func = get_predict_func(pred_config)

    im = cv2.imread(input)
    assert im is not None
    im = im.astype('float32')
    im = cv2.resize(im, (224, 224)).reshape((1, 224, 224, 3))
    im = im - 110
    raw_out = predict_func([im])
    tfout = raw_out[1][0]

    from tensorio import read_value
    dumpf = 'dump.tensortxt'
    with open(dumpf) as f:
        name, arr = read_value(f)
    os.unlink(dumpf)
    hout = arr[:, :, :, 0]
    diff = hout - tfout
    maxdiff = np.abs(diff).max()
    print "Diff:", maxdiff
    assert maxdiff < 1e-3
    return

    prob = raw_out[0][0]
    ret = prob.argsort()[-10:][::-1]
    print ret
    meta = ILSVRCMeta().get_synset_words_1000()
    print[meta[k] for k in ret]
Пример #6
0
def get_inference_augmentor():
    # load ResNet mean from Kaiming:
    # from tensorpack.utils.loadcaffe import get_caffe_pb
    # obj = get_caffe_pb().BlobProto()
    # obj.ParseFromString(open('ResNet_mean.binaryproto').read())
    # pp_mean_224 = np.array(obj.data).reshape(3, 224, 224).transpose(1,2,0)

    meta = ILSVRCMeta()
    pp_mean = meta.get_per_pixel_mean()
    pp_mean_224 = pp_mean[16:-16, 16:-16, :]

    transformers = [
        imgaug.ResizeShortestEdge(256),
        imgaug.CenterCrop((224, 224)),
        imgaug.MapImage(lambda x: x - pp_mean_224),
    ]
    return transformers
Пример #7
0
def get_inference_augmentor():
    # load ResNet mean from Kaiming:
    # from tensorpack.utils.loadcaffe import get_caffe_pb
    # obj = get_caffe_pb().BlobProto()
    # obj.ParseFromString(open('ResNet_mean.binaryproto').read())
    # pp_mean_224 = np.array(obj.data).reshape(3, 224, 224).transpose(1,2,0)

    meta = ILSVRCMeta()
    pp_mean = meta.get_per_pixel_mean()
    pp_mean_224 = pp_mean[16:-16, 16:-16, :]

    transformers = [
        imgaug.ResizeShortestEdge(256),
        imgaug.CenterCrop((224, 224)),
        imgaug.MapImage(lambda x: x - pp_mean_224),
    ]
    return transformers
Пример #8
0
def get_inference_augmentor():
    # load ResNet mean from Kaiming:
    #from tensorpack.utils.loadcaffe import get_caffe_pb
    #obj = get_caffe_pb().BlobProto()
    #obj.ParseFromString(open('ResNet_mean.binaryproto').read())
    #pp_mean_224 = np.array(obj.data).reshape(3, 224, 224).transpose(1,2,0)

    meta = ILSVRCMeta()
    pp_mean = meta.get_per_pixel_mean()
    pp_mean_224 = pp_mean[16:-16,16:-16,:]

    def resize_func(im):
        h, w = im.shape[:2]
        scale = 256.0 / min(h, w)
        desSize = map(int, [scale * w, scale * h])
        im = cv2.resize(im, tuple(desSize), interpolation=cv2.INTER_CUBIC)
        return im
    transformers = imgaug.AugmentorList([
        imgaug.MapImage(resize_func),
        imgaug.CenterCrop((224, 224)),
        imgaug.MapImage(lambda x: x - pp_mean_224),
    ])
    return transformers
Пример #9
0
def run_test(params, input):
    image_mean = np.array([0.485, 0.456, 0.406], dtype='float32')
    pred_config = PredictConfig(model=Model(),
                                input_var_names=['input'],
                                session_init=ParamRestore(params),
                                output_var_names=['prob_output'])
    predict_func = get_predict_func(pred_config)

    im = cv2.imread(input)
    im = cv2.resize(im, (224, 224)) - image_mean * 255
    im = np.reshape(im, (1, 224, 224, 3)).astype('float32')
    prob = predict_func([im])[0]

    ret = prob[0].argsort()[-10:][::-1]
    print(ret)
    meta = ILSVRCMeta().get_synset_words_1000()
    print([meta[k] for k in ret])
Пример #10
0
def run_test(params, input):
    pred_config = PredictConfig(model=Model(),
                                session_init=DictRestore(params),
                                input_names=['input'],
                                output_names=['prob'])
    predict_func = OfflinePredictor(pred_config)

    prepro = get_inference_augmentor()
    im = cv2.imread(input).astype('float32')
    im = prepro.augment(im)
    im = np.reshape(im, (1, 224, 224, 3))
    outputs = predict_func([im])
    prob = outputs[0]

    ret = prob[0].argsort()[-10:][::-1]
    print(ret)
    meta = ILSVRCMeta().get_synset_words_1000()
    print([meta[k] for k in ret])
Пример #11
0
def run_test(path, input):
    param_dict = np.load(path, encoding='latin1').item()
    predict_func = OfflinePredictor(
        PredictConfig(model=Model(),
                      session_init=ParamRestore(param_dict),
                      input_names=['input'],
                      output_names=['prob']))

    im = cv2.imread(input)
    assert im is not None, input
    im = cv2.resize(im, (227, 227))[:, :, ::-1].reshape(
        (1, 227, 227, 3)).astype('float32') - 110
    outputs = predict_func([im])[0]
    prob = outputs[0]
    ret = prob.argsort()[-10:][::-1]
    print("Top10 predictions:", ret)

    meta = ILSVRCMeta().get_synset_words_1000()
    print("Top10 class names:", [meta[k] for k in ret])
Пример #12
0
def run_test(path, input):
    param_dict = dict(np.load(path))
    predictor = OfflinePredictor(
        PredictConfig(
            inputs_desc=[InputDesc(tf.float32, (None, 227, 227, 3), 'input')],
            tower_func=tower_func,
            session_init=DictRestore(param_dict),
            input_names=['input'],
            output_names=['prob']))

    im = cv2.imread(input)
    assert im is not None, input
    im = cv2.resize(im, (227, 227))[None, :, :, ::-1].astype('float32') - 110
    outputs = predictor(im)[0]
    prob = outputs[0]
    ret = prob.argsort()[-10:][::-1]
    print("Top10 predictions:", ret)

    meta = ILSVRCMeta().get_synset_words_1000()
    print("Top10 class names:", [meta[k] for k in ret])
Пример #13
0
def run_test(path, input):
    predictor = OfflinePredictor(
        PredictConfig(input_signature=[
            tf.TensorSpec((None, 227, 227, 3), tf.float32, 'input')
        ],
                      tower_func=tower_func,
                      session_init=SmartInit(path),
                      input_names=['input'],
                      output_names=['prob']))

    im = cv2.imread(input)
    assert im is not None, input
    im = cv2.resize(im, (227, 227))[None, :, :, ::-1].astype('float32') - 110
    outputs = predictor(im)[0]
    prob = outputs[0]
    ret = prob.argsort()[-10:][::-1]
    print("Top10 predictions:", ret)

    meta = ILSVRCMeta().get_synset_words_1000()
    print("Top10 class names:", [meta[k] for k in ret])
def run_test(path, input):
    param_dict = np.load(path, encoding='latin1').item()
    predict_func = OfflinePredictor(PredictConfig(
        model=Model(),
        session_init=DictRestore(param_dict),
        input_names=['input'],
        output_names=['prob']   # prob:0 is the probability distribution
    ))

    im = cv2.imread(input)
    assert im is not None, input
    im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
    im = cv2.resize(im, (224, 224)).reshape((1, 224, 224, 3)).astype('float32')
    im = im - 110
    outputs = predict_func([im])[0]
    prob = outputs[0]
    ret = prob.argsort()[-10:][::-1]
    print("Top10 predictions:", ret)

    meta = ILSVRCMeta().get_synset_words_1000()
    print("Top10 class names:", [meta[k] for k in ret])
                    choices=[50, 101, 152])
parser.add_argument('--arch',
                    help='Name of architectures defined in nets.py',
                    default='ResNetDenoise')
parser.add_argument('--load', help='path to checkpoint')
parser.add_argument('--input', help='path to input image')
args = parser.parse_args()

model = getattr(nets, args.arch + 'Model')(args)

input = tf.placeholder(tf.float32, shape=(None, 224, 224, 3))
image = input / 127.5 - 1.0
image = tf.transpose(image, [0, 3, 1, 2])
with TowerContext('', is_training=False):
    logits = model.get_logits(image)

sess = tf.Session()
get_model_loader(args.load).init(sess)

sample = cv2.imread(args.input)  # this is a BGR image, not RGB
# imagenet evaluation uses standard imagenet pre-processing
# (resize shortest edge to 256 + center crop 224).
# However, for images of unknown sources, let's just do a naive resize.
sample = cv2.resize(sample, (224, 224))

prob = sess.run(logits, feed_dict={input: np.array([sample])})
print("Prediction: ", prob.argmax())

synset = ILSVRCMeta().get_synset_words_1000()
print("Top 5: ", [synset[k] for k in prob[0].argsort()[-5:][::-1]])