def __init__(self, model=defaultModel, imgDim=96, cuda=False): """__init__(self, model=defaultModel, imgDim=96, cuda=False) Instantiate a 'TorchNeuralNet' object. :param model: The path to the Torch model to use. :type model: str :param imgDim: The edge length of the square input image. :type imgDim: int :param cuda: Flag to use CUDA in the subprocess. :type cuda: bool """ assert model is not None assert imgDim is not None assert cuda is not None torch.setdefaulttensortype('torch.FloatTensor') self._net = torch.load(model) self._net.evaluate(self._net) self._tensor = torch.Tensor(1, 3, imgDim, imgDim) self._cuda_tensor = None if cuda: lua.require('cutorch') lua.require('cunn') self._net = self._net._cuda() self._cuda_tensor = torch.CudaTensor(1, 3, imgDim, imgDim) self._cuda = cuda self._imgDim = imgDim
#!/usr/bin/env python # see 1) https://github.com/BVLC/caffe/issues/290 (to read binaryproto) # see 2) https://github.com/hughperkins/pytorch/issues/7 (LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0) for me just conda install libgfortran import cv2 import sys import numpy as np import lutorpy as lua import os torch = lua.require("torch") loadcaffe = lua.require('loadcaffe') image = lua.require('image') # setup runtime and use zero-based index(optional, enabled by default) # lua.LuaRuntime(zero_based_index=True) imageHeight = 224 imageWidth = 224 def preprocess(img_bgr, img_mean): img_bgr = image.scale(img_bgr,imageHeight,imageWidth,'bilinear') img_mean_bgr = img_mean._squeeze() if img_mean is not None: for i in range(1, 4): img_bgr[i]._add(-img_mean_bgr[i]) return img_bgr net = loadcaffe.load('/home/gao/Hadoop/hduser/data/models/VGG_CNN_S_deploy.prototxt', '/home/gao/Hadoop/hduser/data/models/VGG_CNN_S.caffemodel', 'nn') net._evaluate(); #This sets the mode of the Module (or sub-modules) to train=false. This is useful for modules like Dropout that have a different behaviour during training vs evaluation. synset_words = [] fo = open('/home/gao/Hadoop/hduser/data/synset_words.txt', 'r') for line in fo.readlines():
""" A interface for Torch models """ import lutorpy as lua import numpy as np import os import sys import time lua.require('nn') lua.require('cunn') lua.require('cudnn') lua.eval("torch.setdefaulttensortype('torch.FloatTensor')") import logging from skimage import io, transform def thumbnail(img, size=150): """ Resize given image by the shortest edge """ from math import floor width, height = img.shape[1], img.shape[0] if width == height: img = transform.resize(img, [size, size]) elif height > width: ratio = float(height)/float(width) newheight = ratio * size img = transform.resize(img, [int(floor(newheight)), size ])
# # This file is Vitalius Parubochyi's modification of `torch_neural_net.py` # to use lutorpy instead of calling a Lua subprocess. # It's currently not used by default to avoid adding an # additional dependency. # More details are available on this mailing list thread: # https://groups.google.com/forum/#!topic/cmu-openface/Jj68LJBdN-Y """Module for Torch-based neural network usage.""" import lutorpy as lua import numpy as np import binascii import cv2 import os torch = lua.require('torch') nn = lua.require('nn') dpnn = lua.require('dpnn') image = lua.require('image') myDir = os.path.dirname(os.path.realpath(__file__)) class TorchNeuralNet: """Use a `Torch <http://torch.ch>` and `Lutorpy <https://github.com/imodpasteur/lutorpy>`.""" #: The default Torch model to use. defaultModel = os.path.join(myDir, '..', 'models', 'openface', 'nn4.v1.ascii.t7') def __init__(self, model=defaultModel, imgDim=96, cuda=False):
# see 1) https://github.com/BVLC/caffe/issues/290 # see 2) https://github.com/hughperkins/pytorch/issues/7 (LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0) import caffe import numpy as np import lutorpy as lua # setup runtime and use zero-based index(optional, enabled by default) lua.LuaRuntime(zero_based_index=True) torch = lua.require("torch") blob = caffe.proto.caffe_pb2.BlobProto() in_binary_proto_path = "models/VGG_mean.binaryproto" out_path = "out.npy" data = open(in_binary_proto_path , 'rb' ).read() blob.ParseFromString(data) arr = np.array(caffe.io.blobproto_to_array(blob)) out = arr[0] np.save(out_path, out) t = torch.DoubleTensor(11, 11)._fill(1) torch.save("test_save_from_python.t7", t) image_mean_torch_tensor = torch.fromNumpyArray(arr) torch.save("models/VGG_mean.t7", image_mean_torch_tensor) np.save("models/VGG_mean.npy", arr)
# This file is Vitalius Parubochyi's modification of `torch_neural_net.py` # to use lutorpy instead of calling a Lua subprocess. # It's currently not used by default to avoid adding an # additional dependency. # More details are available on this mailing list thread: # https://groups.google.com/forum/#!topic/cmu-openface/Jj68LJBdN-Y """Module for Torch-based neural network usage.""" import lutorpy as lua import numpy as np import binascii import cv2 import os torch = lua.require('torch') nn = lua.require('nn') dpnn = lua.require('dpnn') image = lua.require('image') myDir = os.path.dirname(os.path.realpath(__file__)) class TorchNeuralNet: """Use a `Torch <http://torch.ch>` and `Lutorpy <https://github.com/imodpasteur/lutorpy>`.""" #: The default Torch model to use. defaultModel = os.path.join( myDir, '..', 'models', 'openface', 'nn4.small2.v1.t7')