コード例 #1
0
def feat_extract(batch, extractor):
    from caffe_io import load_image

    img_names = get_image_names()
    np.save('filename.npy', np.array(img_names))
    print len(img_names)
    imgs = []
    feats_ret = np.ndarray((0, 4096), dtype='f')
    for i in xrange(len(img_names)):
        if i % batch == 0 and i != 0:
            start = time.time()
            feats = extractor.extract_batch(imgs, ['fc7'])['fc7']
            print 'batch extraction:', time.time() - start
            np.save('feats_temp_' + str(i), feats)
            feats_ret = np.append(feats_ret, feats, axis=0)
            imgs = []
        img_name = img_names[i]
        img = load_image(img_name)
        imgs.append(img)
    if imgs != []:
        start = time.time()
        feats = extractor.extract_batch(imgs, ['fc7'])['fc7']
        print 'batch extraction:', time.time() - start
        np.save('feats_temp_' + str(len(img_names)), feats)
        feats_ret = np.append(feats_ret, feats, axis=0)
    np.save('feats_total_4096.npy', feats_ret)
    return feats_ret
コード例 #2
0

if __name__ == "__main__":
    caffe.set_mode_gpu()
    model_file = "./caffe_models/vgg_16/VGG_ILSVRC_16_layers_deploy.prototxt"
    pretrained_file = "./caffe_models/vgg_16/VGG_ILSVRC_16_layers.caffemodel"
    img_list = "flickr8k_images_fullpath.lst"
    start = time.time()
    extractor = FeatExtractor(model_file, pretrained_file, oversample=False)
    print "intitialization time:", time.time() - start
    from caffe_io import load_image

    with open(img_list) as f:
        img_names = [l.rstrip() for l in f]
    imgs = []
    for i in range(13):
        img_name = img_names[i]
        img = load_image(img_name)
        imgs.append(img)
    start = time.time()
    feats1 = extractor.extract(imgs)
    print "non-batch extraction:", time.time() - start
    start = time.time()
    feats2 = extractor.extract_batch(imgs)
    print "batch extraction:", time.time() - start

    print len(feats1["fc6"]), len(feats2["fc6"])
    for i in xrange(len(feats1["fc6"])):
        print feats1["fc6"][i].shape
        print (feats1["fc6"][i] == feats2["fc6"][i]).all()
コード例 #3
0
    parser.add_argument('weights', type=str, help='binary caffe model')
    parser.add_argument('img_list', type=str, help='The image list file')
    parser.add_argument('--batch_num',
                        type=int,
                        default=10,
                        help='Num of images in a batch')
    args = parser.parse_args()
    return args.model, args.weights, args.img_list, args.batch_num


if __name__ == "__main__":
    model_f, weights_f, img_list_f, batch_num = parse_args()
    # Load the imgs
    print("Loading images...")
    with open(img_list_f, 'r') as f:
        img_list = [line.strip() for line in f.readlines()]
        imgs = [transform_image(load_image(name)) for name in img_list]

    net, net_param = load_net_and_param(model_f, weights_f)
    imgs_batch = [
        imgs[i:i + batch_num] for i in xrange(0, len(imgs), batch_num)
    ]
    for ind, batch in enumerate(imgs_batch):
        data = batch[0]
        for img in batch[1::]:
            data = np.vstack((data, img))
        ts = time.time()
        out = net.forward(**{net.inputs[0]: data})
        te = time.time()
        print("Time cost of batch(%d):%2.2f sec" % (ind, te - ts))
コード例 #4
0

if __name__ == "__main__":
    # Load the net and net_param
    net, net_param = load_net_and_param(
        "../../models/vgg16/VGG_ILSVRC_16_layers_deploy_upgrade.prototxt",
        "../../models/vgg16/VGG_ILSVRC_16_layers.caffemodel")
    conv_layer_names = [
        l.name for l in net_param.layer if l.type == "Convolution"
    ][1::]
    rank_sel = RankSelection(net, net_param)

    # Read the image
    with open('./data/input/1000_1_per_class.txt') as f:
        img_names = [line.strip() for line in f.readlines()]
    imgs = [load_image(img) for img in img_names]

    # Calculate the responses
    responses, sample_indics = extract_conv_response(net,
                                                     net_param,
                                                     IOParam(),
                                                     imgs,
                                                     conv_layer_names,
                                                     sample_ratio=0.1)
    # Compute the eigenvals for each response
    eigenvals = rank_sel.blob_eigenvale(responses)

    from collections import defaultdict
    import cPickle
    # {blob_names: {speedup-ratio: [non-3d-decomp-rank, 3d-decomp-rank]}
    batch_ranks = defaultdict(lambda: defaultdict(list))
コード例 #5
0
  caffe.set_mode_cpu()
  model_file = './caffe_models/vgg19/VGG_ILSVRC_19_layers.prototxt'
  pretrained_file = './caffe_models/vgg19/VGG_ILSVRC_19_layers.caffemodel'
  
  img_path_prefix="./images/"
  img_list = img_path_prefix+"images_fullpath.lst"
  start = time.time()
  extractor = FeatExtractor(model_file, pretrained_file, oversample=False)
  print 'intitialization time:', time.time() - start
  from caffe_io import load_image

  with open(img_list) as f:
    img_names = [l.rstrip() for l in f]
  imgs = []
  for i in range(len(img_names)):#range(13):
    img_name = img_names[i]
    img = load_image(img_path_prefix+img_name)
    imgs.append(img)
  start = time.time()
  feats1 = extractor.extract(imgs)
  print 'non-batch extraction:', time.time() - start
  start = time.time()
  feats2 = extractor.extract_batch(imgs)
  print 'batch extraction:', time.time() - start

  print len(feats1['fc6']), len(feats2['fc6'])
  for i in xrange(len(feats1['fc6'])):
    print feats1['fc6'][i].shape
    print feats1['fc6'][i]
    print (feats1['fc6'][i]==feats2['fc6'][i]).all()
コード例 #6
0

if __name__ == '__main__':
    caffe.set_mode_gpu()
    model_file = './caffe_models/vgg_16/VGG_ILSVRC_16_layers_deploy.prototxt'
    pretrained_file = './caffe_models/vgg_16/VGG_ILSVRC_16_layers.caffemodel'
    img_list = 'flickr8k_images_fullpath.lst'
    start = time.time()
    extractor = FeatExtractor(model_file, pretrained_file, oversample=False)
    print 'intitialization time:', time.time() - start
    from caffe_io import load_image

    with open(img_list) as f:
        img_names = [l.rstrip() for l in f]
    imgs = []
    for i in range(13):
        img_name = img_names[i]
        img = load_image(img_name)
        imgs.append(img)
    start = time.time()
    feats1 = extractor.extract(imgs)
    print 'non-batch extraction:', time.time() - start
    start = time.time()
    feats2 = extractor.extract_batch(imgs)
    print 'batch extraction:', time.time() - start

    print len(feats1['fc6']), len(feats2['fc6'])
    for i in xrange(len(feats1['fc6'])):
        print feats1['fc6'][i].shape
        print(feats1['fc6'][i] == feats2['fc6'][i]).all()
        data = np.vstack((data, transform_image(img, self.oversample, self.mean, self.img_dim, self.crop_dim)))
      if data.shape[0] == self.batch_size:
        self._process_batch(data, feats, blobs)
        data = None
    self._process_batch(data, feats, blobs)
    return feats

if __name__ == '__main__':
  #caffe.set_mode_gpu()
  model_file = './caffe_models/vgg_16/VGG_ILSVRC_16_layers_deploy.new.prototxt'
  pretrained_file = './caffe_models/vgg_16/VGG_ILSVRC_16_layers.new.caffemodel'
  img_list = 'flickr8k_images_fullpath.lst'
  start = time.time()
  extractor = FeatExtractor(model_file, pretrained_file, oversample=False)

  print ('intitialization time:', time.time() - start)
  from caffe_io import load_image

  with open(img_list) as f:
    img_names = [l.rstrip() for l in f]

  img_names = img_names[0:10]
  print('Reading in images')
  imgs = [load_image(img_name) for img_name in img_names]
  for i in range(3):
      print('Extracting')
      start = time.time()
      features = extractor.extract_batch(imgs)
      time_needed = (time.time() - start)
      print ('{:.4f} seconds/image\ntotal: {}'.format(time_needed / len(imgs), time_needed))