#!/usr/bin/env python caffe_root = '../../' # this file should be run from {caffe_root}/examples (otherwise change this line) import sys, os sys.path.insert(0, caffe_root + 'python') import caffe dirname = os.path.abspath('./style-train-log') if not os.path.exists(dirname): os.mkdir(dirname) caffe.set_logdir(sys.argv[0], dirname) #default is cpu mode #caffe.set_device(0) #caffe.set_mode_gpu() #caffe.set_mode_cpu() import numpy as np from pylab import * #%matplotlib inline import tempfile # Helper function for deprocessing preprocessed images, e.g., for display. def deprocess_net_image(image): image = image.copy() # don't modify destructively image = image[::-1] # BGR -> RGB image = image.transpose(1, 2, 0) # CHW -> HWC image += [123, 117, 104] # (approximately) undo mean subtraction # clamp values in [0, 255]
import numpy as np from PIL import Image caffe_root = '../../' import sys, os sys.path.insert(0, caffe_root + 'python') import caffe caffe.set_logdir(sys.argv[0], os.path.abspath('./log')) # load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe #im = Image.open('pascal/VOC2010/JPEGImages/2007_000129.jpg') im = Image.open('data/pascal/VOC2007/JPEGImages/000129.jpg') in_ = np.array(im, dtype=np.float32) in_ = in_[:, :, ::-1] in_ -= np.array((104.00698793, 116.66876762, 122.67891434)) in_ = in_.transpose((2, 0, 1)) # load net net = caffe.Net('voc-fcn8s/deploy.prototxt', 'voc-fcn8s/fcn8s-heavy-pascal.caffemodel', caffe.TEST) # shape for input (data blob is N x C x H x W), set data net.blobs['data'].reshape(1, *in_.shape) net.blobs['data'].data[...] = in_ # run net and take argmax for prediction net.forward() out = net.blobs['score'].data[0].argmax(axis=0) #print str(out) class_num = {}