def overfeatFeatures(image): image = imresize(image, (231, 231)).astype(np.float32) h = image.shape[0] w = image.shape[1] c = image.shape[2] # numpy loads image with colors as last dimension, transpose tensor image = image.reshape(w * h, c) image = image.transpose() image = image.reshape(c, h, w) # run overfeat on the image overfeat.fprop(image) features = overfeat.get_output(19).copy() return features
def runForImage(self, input_img): image = imread(input_img) if len(image.shape) == 2: return 0 h0 = image.shape[0] w0 = image.shape[1] d0 = float(min(h0, w0)) if len(image.shape) == 2: pdb.set_trace() image = image[int(round((h0-d0)/2.)):int(round((h0-d0)/2.)+d0), int(round((w0-d0)/2.)):int(round((w0-d0)/2.)+d0)] elif len(image.shape)== 3: image = image[int(round((h0-d0)/2.)):int(round((h0-d0)/2.)+d0), int(round((w0-d0)/2.)):int(round((w0-d0)/2.)+d0),:] image = imresize(image, (self.height, self.width)).astype(numpy.float32) # numpy loads image with colors as last dimension, transpose tensor h = image.shape[0] w = image.shape[1] if len(image.shape) == 2: c = 1 return elif len(image.shape)== 3: #pdb.set_trace() c = image.shape[2] image = image.reshape(w*h, c) image = image.transpose() image = image.reshape(c, h, w) #print "Image size :", image.shape # run overfeat on the image self.outputAtLastLayer = overfeat.fprop(image) return 1
def extract_features(file_name): global X image = imread(file_name) image.resize((image.shape[0], image.shape[1], 1)) # overfeat expects rgb, so replicate the grayscale values twice image = np.repeat(image, 3, 2) image = imresize(image, (231, 231)).astype(np.float32) # numpy loads image with colors as last dimension, so transpose tensor h = image.shape[0] w = image.shape[1] c = image.shape[2] image = image.reshape(w * h, c) image = image.transpose() image = image.reshape(c, h, w) b = overfeat.fprop(image) b = b.flatten() top = [(b[i], i) for i in xrange(len(b))] top.sort() klasses = [ overfeat.get_class_name(top[-(i + 1)][1]) for i in xrange(len(top)) ] X.append(klasses)
def extract_features(layer, file_name): global X image = imread(file_name) image.resize((image.shape[0], image.shape[1], 1)) # overfeat expects rgb, so replicate the grayscale values twice image = np.repeat(image, 3, 2) image = imresize(image, (231, 231)).astype(np.float32) # numpy loads image with colors as last dimension, so transpose tensor h = image.shape[0] w = image.shape[1] c = image.shape[2] image = image.reshape(w * h, c) image = image.transpose() image = image.reshape(c, h, w) b = overfeat.fprop(image) b = b.flatten() x = overfeat.get_output(layer) # maxpooling x = np.amax(np.amax(x, 1), 1) x = x.reshape(x.shape[0], 1).T if X is None: X = x else: X = np.append(X, x, axis=0)
def _compute_features(self, images): import overfeat # soft dep features = [] for image in images: image = self.prepare_image(image) overfeat.fprop(image) feat = overfeat.get_output(self.feature_layer) if self.merge == 'maxmean': feat = feat.max(2).mean(1) elif self.merge == 'meanmax': feat = feat.mean(2).max(1) else: feat = self.merge(feat) features.append(feat) return np.vstack(features)
def getDictOfFeatures(directory, dirname): d = {} for filename in os.listdir(directory): if filename == ".directory": continue image = imread(os.path.join(directory, filename)).astype(numpy.float32) # numpy loads image with colors as last dimension, transpose tensor h = image.shape[0] w = image.shape[1] c = image.shape[2] image = image.reshape(w * h, c) image = image.transpose() image = image.reshape(c, h, w) # run overfeat on the image overfeat.fprop(image) features = overfeat.get_output(19) d[filename] = features.copy() pickle.dump(d, open(dirname + "_full.txt", "w")) return d
def get_overfeat_features(imgs, weights_path, typ, layer=None, cache=None): """Returns features at layer for given image(s) from OverFeat model. Small (fast) network: 22 layers Large (accurate) network: 25 layers Args: imgs: Iterable of images each of shape (h,w,c) weights_path: Path to the OverFeat weights typ: 0 for small, 1 for large version of OverFeat layer: The layer to extract features from cache: Dict containing descs/other cached values """ if cache is None: cache = {} if 'overfeat_descs' not in cache: # Initialize network print('Loading OverFeat ({}) model...'.format(typ)) overfeat.init(weights_path, typ) # Determine feature layer if none specified if layer is None: if overfeat.get_n_layers() == 22: # small layer = 19 # 16 also recommended else: # large # Layer used by Zhang et al. layer = 22 # Determine resize dim if typ == 0: resize = 231 # small network else: resize = 221 # large network # Allocate for feature descriptors descs = [] # Run images through network print('Running images through OverFeat, extracting features ' 'at layer {}.'.format(layer)) for idx, img in enumerate(imgs): if (idx + 1) % 100 == 0: print('Processing image {}...'.format(idx + 1)) # Preprocess image img = overfeat_preprocess(img, resize) # Run through model _ = overfeat.fprop(img) # Retrieve feature output desc = overfeat.get_output(layer) descs.append(desc) # Free network overfeat.free() # NumPy-ify descs = np.asarray(descs) cache.update(overfeat_descs=descs) else: descs = cache['overfeat_descs'] return descs, cache
def extract_overfeat(image_path): # if overfeat_initialized == None or not overfeat_initialized: # overfeat_initialized = True print "Overfeat: ", image_path image = imread(image_path) print "Image shape: ", image.shape if len(image.shape) == 2 or image.shape[2] == 2: image = skimage.color.gray2rgb(image) elif image.shape[2] == 4: image_rgb = numpy.zeros((image.shape[0], image.shape[1], 3), numpy.uint8) image_rgb[:, :, 0] = image[:, :, 0] image_rgb[:, :, 1] = image[:, :, 1] image_rgb[:, :, 2] = image[:, :, 2] image = image_rgb h0 = image.shape[0] w0 = image.shape[1] d0 = float(min(h0, w0)) image = image[int(round((h0 - d0) / 2.)):int(round((h0 - d0) / 2.) + d0), int(round((w0 - d0) / 2.)):int(round((w0 - d0) / 2.) + d0), :] image = imresize(image, (231, 231)).astype(numpy.float32) #image = cv2.resize(image, (231, 231)).astype(numpy.float32) # numpy loads image with colors as last dimension, transpose tensor h = image.shape[0] w = image.shape[1] c = image.shape[2] image = image.reshape(w * h, c) image = image.transpose() image = image.reshape(c, h, w) print "Image size :", image.shape out_categories = overfeat.fprop(image) #layer 21,22,23 layer_output = overfeat.get_output(20) print "Layer size: ", layer_output.shape layer_output = layer_output.flatten() descriptors = [] descriptors.append(layer_output) out_categories = out_categories.flatten() top = [(out_categories[i], i) for i in xrange(len(out_categories))] top.sort() print "\nTop classes :" for i in xrange(5): print(overfeat.get_class_name(top[-(i + 1)][1])) return descriptors
def extract_overfeat(image_path): # if overfeat_initialized == None or not overfeat_initialized: # overfeat_initialized = True print "Overfeat: ", image_path image = imread(image_path) print "Image shape: ", image.shape if len(image.shape) == 2 or image.shape[2] == 2: image = skimage.color.gray2rgb(image) elif image.shape[2] == 4: image_rgb = numpy.zeros((image.shape[0],image.shape[1], 3), numpy.uint8) image_rgb[:,:,0] = image[:,:,0] image_rgb[:,:,1] = image[:,:,1] image_rgb[:,:,2] = image[:,:,2] image = image_rgb h0 = image.shape[0] w0 = image.shape[1] d0 = float(min(h0, w0)) image = image[int(round((h0-d0)/2.)):int(round((h0-d0)/2.)+d0), int(round((w0-d0)/2.)):int(round((w0-d0)/2.)+d0), :] image = imresize(image, (231, 231)).astype(numpy.float32) #image = cv2.resize(image, (231, 231)).astype(numpy.float32) # numpy loads image with colors as last dimension, transpose tensor h = image.shape[0] w = image.shape[1] c = image.shape[2] image = image.reshape(w*h, c) image = image.transpose() image = image.reshape(c, h, w) print "Image size :", image.shape out_categories = overfeat.fprop(image) #layer 21,22,23 layer_output = overfeat.get_output(20) print "Layer size: ", layer_output.shape layer_output = layer_output.flatten() descriptors = [] descriptors.append(layer_output) out_categories = out_categories.flatten() top = [(out_categories[i], i) for i in xrange(len(out_categories))] top.sort() print "\nTop classes :" for i in xrange(5): print(overfeat.get_class_name(top[-(i+1)][1])) return descriptors
def run_overfeat(image, layer=None): ''' runs an image through overfeat. returns the 1,000-length likelihoods vector and N-length layer in the net as copied and formatted numpy arrays. layer None: means return 4,096-length feature vector just prior to the output layer. int: return that layer instead. ''' if not layer: # get layer just before output layer feature_layer = overfeat.get_n_layers() - 2 overfeat_likelihoods = overfeat.fprop(image) overfeat_features = overfeat.get_output(feature_layer) # flatten and copy. NOTE: copy() is intentional, don't delete. formatted_features = copy(overfeat_features.flatten()) formatted_likelihoods = copy(overfeat_likelihoods.flatten()) return formatted_likelihoods, formatted_features
def extract_feat(self, imgs, ftype): ''' extract features ''' if ftype is 'hog': from skimage.feature import hog L = np.sqrt(len(imgs[0])).astype(int) X_im = [imresize(arr.reshape((L, L)), (64, 64)) for arr in imgs] pool = Pool(processes=8) X = pool.map(hog, X_im) pool.close() return np.asarray(X) elif ftype is 'overfeat': overfeat.init('OverFeat/data/default/net_weight_0', 1) L = np.sqrt(len(imgs[0])).astype(int) imgs_color = [imresize(arr.reshape((L, L)), (231, 231)) for arr in imgs] if len(imgs_color[0].shape) != 3: cmap = pl.get_cmap('jet') imgs_color = [np.delete(cmap(im/255.), 3, 2) for im in imgs_color] imgs_roll = [im.transpose((2, 0, 1)).astype(np.float32) for im in imgs_color] feats = np.zeros((len(imgs_roll), 4096), dtype = float) for i in range(len(imgs_roll)): b = overfeat.fprop(imgs_roll[i]) f22 = overfeat.get_output(22) f22 = np.asarray(f22).squeeze().astype(np.float) feats[i, :] = f22 return feats elif ftype is 'pix': return imgs else: raise NameError('{0} is not implemented!'.format(ftype))
for net_type in net_types: timing_data[net_type] = {} for num_image in num_images: print("Timing %s for %s images" % (net_type, num_image)) # OverFeat does not use caffe if net_type == 'OverFeat': overfeat.init(overfeat_root + 'data/default/net_weight_0', 0) start = time.clock() for i in range(num_image): image = smush_overfeat_images(training_images[i]) b = overfeat.fprop(image) end = time.clock() elapsed = end - start print(elapsed) timing_data[net_type][num_image] = elapsed # Use caffe for all other models else: if user == 'ctnuser': caffe.set_mode_gpu() else: caffe.set_mode_cpu() if net_type == 'GoogLeNet':
def process_through_net(photo, feature_layer=None): if not feature_layer: feature_layer = overfeat.get_n_layers() - 2 likelihoods = copy(overfeat.fprop(photo).flatten()) features = copy(overfeat.get_output(feature_layer).flatten()) return likelihoods, features
def overfeatFeatures(image): overfeat.fprop(image) features = overfeat.get_output(19).copy() return features
# resize and crop into a 231x231 image h0 = image.shape[0] w0 = image.shape[1] d0 = float(min(h0, w0)) image = image[int(round((h0-d0)/2.)):int(round((h0-d0)/2.)+d0), int(round((w0-d0)/2.)):int(round((w0-d0)/2.)+d0), :] image = imresize(image, (231, 231)).astype(numpy.float32) # numpy loads image with colors as last dimension, transpose tensor h = image.shape[0] w = image.shape[1] c = image.shape[2] image = image.reshape(w*h, c) image = image.transpose() image = image.reshape(c, h, w) print "Image size :", image.shape # initialize overfeat. Note that this takes time, so do it only once if possible overfeat.init('../../data/default/net_weight_0', 0) # run overfeat on the image b = overfeat.fprop(image) # display top 5 classes b = b.flatten() top = [(b[i], i) for i in xrange(len(b))] top.sort() print "\nTop classes :" for i in xrange(5): print(overfeat.get_class_name(top[-(i+1)][1]))