def decaf_features(ImagePath): try: sys.path.append("/share/code/caffe/python") from caffe import imagenet import numpy # Set the right path to your model file, pretrained model, # and the image you would like to classify. MODEL_FILE = '/share/code/caffe/examples_old/imagenet_deploy.prototxt' PRETRAINED = '/share/code/caffe/models/caffe_reference_imagenet_model' IMAGE_FILE = ImagePath net = imagenet.ImageNetClassifier(MODEL_FILE, PRETRAINED) net.caffenet.set_phase_test() net.caffenet.set_mode_cpu() # Compute the features net.predict(IMAGE_FILE) blobs = net.caffenet.blobs() features = blobs[-3].data[:] print features.shape print features[1].shape, features[1] features = numpy.resize(features, (10, 4096)) print features.shape, features[1].shape return features except Exception as e: raise e
def loadNetwork(self): modelFile = config.get('networkDir') + 'deploy.prototxt' meanImage = config.get('meanImagePickle') self.net = imagenet.ImageNetClassifier( modelFile, self.networkFile, IMAGE_DIM=config.geti('imageSize'), CROPPED_DIM=config.geti('cropSize'), MEAN_IMAGE=meanImage) self.net.caffenet.set_phase_test() self.net.caffenet.set_mode_gpu() self.meanImage = self.net._IMAGENET_MEAN.swapaxes(1, 2).swapaxes( 0, 1).astype('float32')
def caffe_classify_image(ImagePath): import sys sys.path.append("/share/code/caffe/python") import scipy.io as sio from caffe import imagenet results = {} matWNID = sio.loadmat( '/var/www/html/cloudcv/fileupload/executable/WNID.mat') WNID_cells = matWNID['wordsortWNID'] # Set the right path to your model file, pretrained model, # and the image you would like to classify. MODEL_FILE = '/share/code/caffe/examples_old/imagenet_deploy.prototxt' PRETRAINED = '/share/code/caffe/models/caffe_reference_imagenet_model' numoutputs = 1000 net = imagenet.ImageNetClassifier(MODEL_FILE, PRETRAINED, False, numoutputs) # Predict the image classes, then return the top 5 indicies net.caffenet.set_phase_test() net.caffenet.set_mode_cpu() print('Making predictions...') if os.path.isfile(ImagePath): prediction = net.predict(ImagePath) print('Computed scores') map = {} for i, j in enumerate(prediction): map[i] = j print('Getting top five tags...') predsorted = sorted(map.iteritems(), key=operator.itemgetter(1), reverse=True) top5 = predsorted[0:5] topresults = {} m = 0 n = 1 for i in top5: topresults[str(WNID_cells[i, 0][0][0])] = str(i[1]) results[ImagePath] = topresults return results
def decaf_features_centre(ImagePath): import sys import numpy import scipy.io as io sys.path.append("/share/code/caffe/python") from caffe import imagenet # Set the right path to your model file, pretrained model, # and the image you would like to classify. MODEL_FILE = '/share/code/caffe/examples_old/imagenet_deploy.prototxt' PRETRAINED = '/share/code/caffe/models/caffe_reference_imagenet_model' IMAGE_FILE = ImagePath net = imagenet.ImageNetClassifier(MODEL_FILE, PRETRAINED) net.caffenet.set_phase_test() net.caffenet.set_mode_cpu() # Compute the features net.predict(IMAGE_FILE) blobs = net.caffenet.blobs() features = blobs[-3].data[4] features = numpy.resize(features, (1, 4096)) return features
################################## # Parameter checking ################################# if len(sys.argv) < 3: print 'Use: extractCNNFeatures.py bboxes imgsDir outputDir' sys.exit() bboxes = [(x, x.split()) for x in open(sys.argv[1])] imgsDir = sys.argv[2] outDir = sys.argv[3] from caffe import imagenet MODEL_FILE = '/home/caicedo/software/caffe-master/examples/imagenet_deploy.prototxt' PRETRAINED = '/home/caicedo/Downloads/caffe_reference_imagenet_model' net = imagenet.ImageNetClassifier(MODEL_FILE, PRETRAINED) net.caffenet.set_phase_test() net.caffenet.set_mode_gpu() # From: https://github.com/zachrahan/scikits-image/blob/master/skimage/io/tests/test_freeimage.py?source=cc try: import skimage.io._plugins.freeimage_plugin as fi FI_available = True io.use_plugin('freeimage') except RuntimeError: FI_available = False ################################## # Functions #################################
<title>Upload image file</title> <h1>Upload image</h1> <form action="" method=post enctype=multipart/form-data> <p><input type=file name=image> <input type=submit value=Upload> </form> ''' if __name__ == '__main__': ''' We provide access to two trained models: 1. Lenet trained with MNISTClassifier for handwritten digit recognition 2. Imagenet for object recognition Both models are loaded before the web server. ''' # Set the path to your trained models trained_models_path = '/home/allan/Git/caffe/examples' net_lenet = lenet.MNISTClassifier(os.path.join(trained_models_path, 'lenet/lenet.prototxt'), \ os.path.join(trained_models_path, 'lenet/lenet_iter_10000')) net_lenet.caffenet.set_phase_test() net_lenet.caffenet.set_mode_gpu() net_imagenet = imagenet.ImageNetClassifier(os.path.join(trained_models_path, 'imagenet/imagenet_deploy.prototxt'), \ os.path.join(trained_models_path, 'imagenet/caffe_reference_imagenet_model')) net_imagenet.caffenet.set_phase_test() net_imagenet.caffenet.set_mode_cpu() app.debug = True app.run(host='0.0.0.0')
from caffe import imagenet #MODEL_FILE = '/u/sciteam/caicedor/models/imagenet_deploy.prototxt' MODEL_FILE = '/home/caicedo/workspace/caffe/examples/imagenet/alexnet_deploy.prototxt.200' #PRETRAINED = '/u/sciteam/caicedor/models/finetune_voc_2012_train_iter_70k' #PRETRAINED = '/home/caicedo/software/rcnn-master/data/caffe_nets/finetune_voc_2012_train_iter_70k' PRETRAINED = '/home/caicedo/software/rcnn-master/data/caffe_nets/ilsvrc_2012_train_iter_310k.original' IMG_DIM = 256 CROP_SIZE = 227 CONTEXT_PAD = 0 #meanImage = '/u/sciteam/caicedor/scratch/caffe/python/caffe/imagenet/ilsvrc_2012_mean.npy' meanImage = '/home/caicedo/workspace/caffe/python/caffe/imagenet/ilsvrc_2012_mean.npy' net = imagenet.ImageNetClassifier(MODEL_FILE, PRETRAINED, IMAGE_DIM=IMG_DIM, CROPPED_DIM=CROP_SIZE, MEAN_IMAGE=meanImage) net.caffenet.set_phase_test() net.caffenet.set_mode_gpu() ImageNetMean = net._IMAGENET_MEAN.swapaxes(1, 2).swapaxes(0, 1).astype('float32') ################################## # Functions ################################# def getWindow(img, box): dx = int(float(box[2] - box[0]) * 0.10)