def load_data(self,data_opt={'did':0}): did = data_opt['did'] if did==0: """We will create a dummy imagenet data of one single image.""" data = np.random.rand(1, 220, 220, 3).astype(np.float32) label = np.zeros((1,1000)).astype(np.float32) label[0][np.random.randint(1000, size=1)] = 1 #label = np.random.randint(1000, size=1) elif did==1: fns = data_opt['fns'] pid = data_opt['pid'] lls = data_opt['lls'] data = np.empty((0, INPUT_DIM, INPUT_DIM, 3)).astype(np.float32) label = np.zeros((0, OUTPUT_DIM)).astype(np.float32) for fid,filename in enumerate(fns): image = skimage.io.imread(filename).astype(np.uint8) image = transform.scale_and_extract(transform.as_rgb(image), 256).astype(np.float32) * 255. - self.jf_data_mean if _JEFFNET_FLIP: image = image[::-1, :].copy() data = np.vstack((data,self.subpatch(image,pid))) tmp_ll = np.zeros((data.shape[0]-label.shape[0],1000)).astype(np.float32) tmp_ll[:,lls] = 1 label = np.vstack((label,tmp_ll)) #print data.shape,label.shape self.dataset = core_layers.NdarrayDataLayer(name='input', sources=[data, label])
def load_and_preprocess(net, imagepath, center_only=False, scale=True, center_size=256): image = mpimg.imread(imagepath) # first, extract the center_sizexcenter_size center. image = transform.scale_and_extract(transform.as_rgb(image), center_size) # convert to [0,255] float32 if scale: image = image.astype(np.float32) * 255. # Flip the image image = image[::-1, :].copy() # subtract the mean image -= net._data_mean # oversample the images images = net.oversample(image, center_only) return images
def classifiyWholeImage(self,image): # first, extract the 227x227 center. image = transform.scale_and_extract(transform.as_rgb(image), 256) # convert to [0,255] float32 image = image.astype(np.float32) * 255. if _JEFFNET_FLIP: # Flip the image if necessary, maintaining the c_contiguous order image = image[::-1, :].copy() # subtract the mean image -= self._data_mean # oversample the images image = scipy.ndimage.interpolation.zoom(image,(227./256.,227./256.,1)) images = image[None,:,:,:] predictions = self.classify_direct(images) return predictions.mean(0)
def prepare_image(self, image): """Returns image of shape `(256, 256, 3)`, as expected by `transform` when `classify_direct = True`. """ from decaf.util import transform # soft dep _JEFFNET_FLIP = True # first, extract the 256x256 center. image = transform.scale_and_extract(transform.as_rgb(image), 256) # convert to [0,255] float32 image = image.astype(np.float32) * 255. if _JEFFNET_FLIP: # Flip the image if necessary, maintaining the c_contiguous order image = image[::-1, :].copy() # subtract the mean image -= self.net_._data_mean return image
def classify(self, image, center_only=False): """Classifies an input image. Input: image: an image of 3 channels and has data type uint8. Only the center region will be used for classification. Output: scores: a numpy vector of size 1000 containing the predicted scores for the 1000 classes. """ # first, extract the 256x256 center. image = transform.scale_and_extract(transform.as_rgb(image), 256) # convert to [0,255] float32 image = image.astype(np.float32) * 255. if _JEFFNET_FLIP: # Flip the image if necessary, maintaining the c_contiguous order image = image[::-1, :].copy() # subtract the mean image -= self._data_mean # oversample the images images = DecafNet.oversample(image, center_only) predictions = self.classify_direct(images) return predictions.mean(0)