Example #1
0
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)
Example #2
0
	def getTopClassesNames(self, numTopClasses):
		self.outputAtLastLayer = self.outputAtLastLayer.flatten()
		top = [(self.outputAtLastLayer[i], i) for i in xrange(len(self.outputAtLastLayer))]
		#pdb.set_trace()
		top.sort()
		#topNew = []
		print "\nTop classes :"
		for i in xrange(numTopClasses):
			#topNew.append(top[i])
			print(overfeat.get_class_name(top[-(i+1)][1])) + '/t' + str(top[-(i+1)][1])
Example #3
0
def top_n_predictions(likelihoods, n=1):
    '''
    returns top n predictions given an overfeat likelihoods vector whose 
    index corresponds with a category
    '''
    assert len(likelihoods) == 1000
    assert n >= 1 and n <= 1000

    Prediction = namedtuple('Prediction', ['name_index','likelihood'])
    predictions = (Prediction(i,v) for i,v in enumerate(likelihoods))

    # sort prediction by descending likelihood 
    predictions = sorted(predictions, key=lambda x: -x.likelihood)

    return [overfeat.get_class_name(pred.name_index) for pred in predictions[0:n]]
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 overfeat_predictions(likelihoods, n=1):
    '''
    returns top n predictions given an overfeat likelihoods 
    vector whose index corresponds with a category
    '''
    assert len(likelihoods) == 1000
    assert n >= 1 and n <= 1000

    Prediction = namedtuple('Prediction', ['name_index', 'likelihood'])
    predictions = (Prediction(i, v) for i, v in enumerate(likelihoods))

    # sort prediction by descending likelihood
    predictions = sorted(predictions, key=lambda x: -x.likelihood)

    return [
        overfeat.get_class_name(pred.name_index) for pred in predictions[0:n]
    ]
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
Example #7
0
# 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]))
Example #8
0
w0 = image.shape[1]
d0 = float(min(h0, w0))
h1 = int(round(231*h0/d0))
w1 = int(round(231*w0/d0))
image = imresize(image, (h1, w1)).astype(numpy.float32)
image = image[int(round((h0-d0)/2.)):int(round((h0-d0)/2.)+231),
              int(round((w0-d0)/2.)):int(round((w0-d0)/2.)+231), :]

# 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]))