Exemple #1
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])
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])
Exemple #3
0
def run_test(path, input):
    param_dict = dict(np.load(path))
    predictor = OfflinePredictor(
        PredictConfig(input_signature=[
            tf.TensorSpec((None, 227, 227, 3), tf.float32, '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])
def get_inference_augmentor(target_shape):
    # 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_crop = pp_mean[16:-16, 16:-16, :]

    transformers = [
        imgaug.ResizeShortestEdge(256),
        imgaug.CenterCrop((224, 224)),
        imgaug.MapImage(lambda x: x - pp_mean_crop),
    ]

    if target_shape != 224:
        transformers.append(
            imgaug.ResizeShortestEdge(target_shape, cv2.INTER_CUBIC))

    return transformers
Exemple #5
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
                    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]])