Пример #1
0
def preprocess_image(img):
    prep = img
    if prep.shape == 3 and prep.shape[2] > 1:
        prep = rgb2gray(prep)
    if prep.shape != (28, 28, 1):
        prep = resize_image(prep, (28, 28, 1))
    return prep
Пример #2
0
def preprocess_image(img):
    prep = img
    if prep.shape == 3 and prep.shape[2] > 1:
        prep = rgb2gray(prep)
    if prep.shape != (28, 28, 1):
        prep = resize_image(prep, (28, 28, 1))
    return prep
Пример #3
0
 def _extract(self, inputs, layername):
     # NOTE: we import the following codes from caffe.Classifier
     shape = (len(inputs), self.net.image_dims[0], self.net.image_dims[1], inputs[0].shape[2])
     input_ = np.zeros(shape, dtype=np.float32)
     for ix, in_ in enumerate(inputs):
         input_[ix] = resize_image(in_, self.net.image_dims)
     # Take center crop.
     center = np.array(self.net.image_dims) / 2.0
     crop = np.tile(center, (1, 2))[0] + np.concatenate([-self.net.crop_dims / 2.0, self.net.crop_dims / 2.0])
     input_ = input_[:, crop[0] : crop[2], crop[1] : crop[3], :]
     # Classify
     caffe_in = np.zeros(np.array(input_.shape)[[0, 3, 1, 2]], dtype=np.float32)
     for ix, in_ in enumerate(input_):
         caffe_in[ix] = self.net.transformer.preprocess(self.net.inputs[0], in_)
     out = self.net.forward_all(blobs=[layername], **{self.net.inputs[0]: caffe_in})[layername]
     return out
Пример #4
0
 def _extract(self, inputs, layername):
     # NOTE: we import the following codes from caffe.Classifier
     shape = (len(inputs), self.net.image_dims[0], self.net.image_dims[1],
              inputs[0].shape[2])
     input_ = np.zeros(shape, dtype=np.float32)
     for ix, in_ in enumerate(inputs):
         input_[ix] = resize_image(in_, self.net.image_dims)
     # Take center crop.
     center = np.array(self.net.image_dims) / 2.0
     crop = np.tile(center, (1, 2))[0] + np.concatenate(
         [-self.net.crop_dims / 2.0, self.net.crop_dims / 2.0])
     input_ = input_[:, crop[0]:crop[2], crop[1]:crop[3], :]
     # Classify
     caffe_in = np.zeros(np.array(input_.shape)[[0, 3, 1, 2]],
                         dtype=np.float32)
     for ix, in_ in enumerate(input_):
         caffe_in[ix] = \
             self.net.transformer.preprocess(self.net.inputs[0], in_)
     out = self.net.forward_all(blobs=[layername],
                                **{self.net.inputs[0]: caffe_in})[layername]
     return out
Пример #5
0
from caffe.io import oversample, resize_image
from PIL import Image
import numpy as np

MODEL_FILE = 'models/bvlc_reference_caffenet/deploy.prototxt'
WEIGHTS_FILE = 'models/bvlc_reference_caffenet/caffenet.caffemodel'
MEAN_FILE = 'data/imagenet/mean.lmdb'

blob = caffe.proto.caffe_pb2.BlobProto()
data = open( MEAN_FILE , 'rb' ).read()
blob.ParseFromString(data)
mean_arr = np.array( caffe.io.blobproto_to_array(blob) )[0]

img = Image.open('data/imagenet/train/n03085013/n03085013_2061.JPEG')
img = np.array(img, dtype='float') # convert to single.
img = resize_image(img, (256, 256))
img = np.array([img[:, :, 0], img[:, :, 1], img[:, :, 2]])
img = img - mean_arr # substract mean.
# central crop.
(h, w) = img.shape[1:]
(th, tw) = (227, 227)
(ch, cw) = (int(h / 2), int(w / 2))
(cth, ctw) = (int(th / 2), int(tw / 2))
img = img[:, ch-cth:ch-cth+th, cw-ctw:cw-ctw+tw]
im_input = img[np.newaxis, :, :, :]

net = caffe.Net(MODEL_FILE, WEIGHTS_FILE)

net.blobs['data'].reshape(*im_input.shape)
net.blobs['data'].data[:] = im_input
Пример #6
0
from caffe.io import oversample, resize_image
from PIL import Image
import numpy as np

MODEL_FILE = 'models/bvlc_reference_caffenet/deploy.prototxt'
WEIGHTS_FILE = 'models/bvlc_reference_caffenet/caffenet.caffemodel'
MEAN_FILE = 'data/imagenet/mean.lmdb'

blob = caffe.proto.caffe_pb2.BlobProto()
data = open(MEAN_FILE, 'rb').read()
blob.ParseFromString(data)
mean_arr = np.array(caffe.io.blobproto_to_array(blob))[0]

img = Image.open('data/imagenet/train/n03085013/n03085013_2061.JPEG')
img = np.array(img, dtype='float')  # convert to single.
img = resize_image(img, (256, 256))
img = np.array([img[:, :, 0], img[:, :, 1], img[:, :, 2]])
img = img - mean_arr  # substract mean.
# central crop.
(h, w) = img.shape[1:]
(th, tw) = (227, 227)
(ch, cw) = (int(h / 2), int(w / 2))
(cth, ctw) = (int(th / 2), int(tw / 2))
img = img[:, ch - cth:ch - cth + th, cw - ctw:cw - ctw + tw]
im_input = img[np.newaxis, :, :, :]

net = caffe.Net(MODEL_FILE, WEIGHTS_FILE)

net.blobs['data'].reshape(*im_input.shape)
net.blobs['data'].data[:] = im_input