Пример #1
0
 def set_up(self):
     logging.info('Loading jeffnet...')
     # We will simply instantiate a jeffnet manually
     data_root = '/u/vis/common/deeplearning/models/'
     self._net = JeffNet(data_root + 'imagenet.jeffnet.epoch90',
                         data_root + 'imagenet.jeffnet.meta')
     logging.info('Jeffnet loaded.')
     self._randproj = None
Пример #2
0
class DecafnetMapper(mapreducer.BasicMapper):
    """The ImageNet Compute mapper. The input value would be a synset name.
    """
    def set_up(self):
        logging.info('Loading jeffnet...')
        # We will simply instantiate a jeffnet manually
        data_root = '/u/vis/common/deeplearning/models/'
        self._net = JeffNet(data_root + 'imagenet.jeffnet.epoch90',
                            data_root + 'imagenet.jeffnet.meta')
        logging.info('Jeffnet loaded.')
        self._randproj = None

    def map(self, key, value):
        if type(value) is not str:
            value = str(value)
        if FLAGS.single:
            files = [os.path.join(FLAGS.input_folder, value)]
        else:
            files = glob.glob(os.path.join(FLAGS.input_folder, value,
                                           '*.JPEG'))
        files.sort()
        logging.info('%s: %d files', value, len(files))
        features = None

        for i, f in enumerate(files):
            try:
                img = io.imread(f)
                logging.info(f)
                _ = self._net.classify(img)
                feat = self._net.feature(FLAGS.feature_name)
                dim = np.prod(feat.shape[1:])
                if FLAGS.randprojection > 0:
                    if self._randproj is None:
                        np.random.seed(1701)
                        self._randproj = np.random.randn(
                            dim, FLAGS.randprojection).astype(FEATURE_DTYPE)
                        self._randproj /= dim
                    feat = np.dot(feat.reshape(feat.shape[0], dim),
                                  self._randproj)
                if features is None:
                    features = np.zeros((len(files) * 10, ) + feat.shape[1:],
                                        dtype=FEATURE_DTYPE)
                features[i * 10:(i + 1) * 10] = feat
            except IOError:
                # we ignore the exception (maybe the image is corrupted or
                # pygist has some bugs?)
                print f, Exception, e
        outname = str(uuid.uuid4()) + '.npy'
        try:
            os.makedirs(FLAGS.output_folder)
        except OSError:
            pass
        np.save(os.path.join(FLAGS.output_folder, outname), features)
        yield value, outname
class DecafnetMapper(mapreducer.BasicMapper):
    """The ImageNet Compute mapper. The input value would be a synset name.
    """
    def set_up(self):
        logging.info('Loading jeffnet...')
        # We will simply instantiate a jeffnet manually
        data_root = '/u/vis/common/deeplearning/models/'
        self._net = JeffNet(data_root+'imagenet.jeffnet.epoch90',
                data_root+'imagenet.jeffnet.meta')
        logging.info('Jeffnet loaded.')
        self._randproj = None

    def map(self, key, value):
        if type(value) is not str:
            value = str(value)
        if FLAGS.single:
            files = [os.path.join(FLAGS.input_folder, value)]
        else:
            files = glob.glob(os.path.join(FLAGS.input_folder, value, '*.JPEG'))
        files.sort()
        logging.info('%s: %d files', value, len(files))
        features = None

        for i, f in enumerate(files):
            try:
                img = io.imread(f)
                logging.info(f)
                _ = self._net.classify(img)
                feat = self._net.feature(FLAGS.feature_name) 
                dim = np.prod(feat.shape[1:])
                if FLAGS.randprojection > 0:
                    if self._randproj is None:
                        np.random.seed(1701)
                        self._randproj = np.random.randn(dim, FLAGS.randprojection).astype(FEATURE_DTYPE)
                        self._randproj /= dim
                    feat = np.dot(feat.reshape(feat.shape[0], dim), self._randproj)
                if features is None:
                    features = np.zeros((len(files) * 10,) + feat.shape[1:],
                                        dtype = FEATURE_DTYPE)
                features[i*10:(i+1)*10] = feat
            except IOError:
                # we ignore the exception (maybe the image is corrupted or
                # pygist has some bugs?)
                print f, Exception, e
        outname = str(uuid.uuid4()) + '.npy'
        try:
            os.makedirs(FLAGS.output_folder)
        except OSError:
            pass
        np.save(os.path.join(FLAGS.output_folder, outname), features)
        yield value, outname
 def set_up(self):
     logging.info('Loading jeffnet...')
     # We will simply instantiate a jeffnet manually
     data_root = '/u/vis/common/deeplearning/models/'
     self._net = JeffNet(data_root+'imagenet.jeffnet.epoch90',
             data_root+'imagenet.jeffnet.meta')
     logging.info('Jeffnet loaded.')
     self._randproj = None
Пример #5
0
import argparse
import sys
import numpy as np

try:
  import matplotlib.pyplot as pylab
except:
  import scipy.misc as pylab

from decaf.scripts.jeffnet import JeffNet
from decaf.util import smalldata

data_root = '/home/rodner/data/deeplearning/models/'

if len(sys.argv)<2:\
  img = smalldata.lena()
else:
  imgfn = sys.argv[1]
  img = pylab.imread(imgfn)
  

net = JeffNet(data_root+'imagenet.jeffnet.epoch90', data_root+'imagenet.jeffnet.meta')

print "Classify image"

for i in range(5):
  scores = net.classify(img, center_only=True)
  print "Okay"
  #print net.top_k_prediction(scores, 10)[1]
parser.add_argument('img_path_patterns',metavar='pattern', nargs='+',action='append',help='A path pattern for the input images. For example, ./lena.jpg will select lena.jpg from the current directory and ./*.jpg will select all images with the extension jpg in the current directory. If --file-list-mode is set, pattern refers to a path to a text file containing the paths to the images.')
parser.add_argument('--model',nargs='?',default=data_root+'imagenet.jeffnet.epoch90',action='store',help='Path to a model of a convulutional network.')
parser.add_argument('--meta',nargs='?',default=data_root+'imagenet.jeffnet.meta',action='store',help='Path to the meta data of the convulutional network.')
parser.add_argument('--out',nargs='?',default='feature_out.csv',action='store',help='File to store the DeCAF features. The output is a comma separated values (CSV) file. Each line corresponds to one input image.')
parser.add_argument('--layer',nargs='?',default='fc7_neuron_cudanet_out',action='store',help='The features of the layer specified here will be used.')
parser.add_argument('--all-features',action='store_true',help='Wheather to store all features or just for the center part of the image.')
parser.add_argument('--file-list-mode',action='store_true',help='If you set this flag, you can use a file list as input.')
parser.add_argument('--silent',action='store_true',help='Suppress output to console.')
parser.add_argument('--warp',action='store_true',help='Warp the input image to 227x227 and use this directly as input.')


args = parser.parse_args()
# Early check, if an image path is passed
args.img_path_patterns

net = JeffNet(args.model, args.meta)

csv_file=open(args.out,"w");
csv_writer = csv.writer(csv_file,delimiter=',',  quoting=csv.QUOTE_NONE) 

for (i,img_list) in enumerate(args.img_path_patterns):
  for img_path in img_list:
    if args.file_list_mode:
      with open(img_path) as f:
        content = f.readlines()
        for img_name in content:
          writeFeatures(net,img_name.strip('\n'),args.all_features,args.layer,csv_writer,args.silent,args.warp,args.out+'.mat')
    else:
      writeFeatures(net,img_path,args.all_features,args.layer,csv_writer,args.silent,args.warp,args.out+'.mat')