<s> import chainer . functions as F <EOL> import chainer . links as L <EOL> from chainer import Variable <EOL> from chainer . links import caffe <EOL> from chainer import computational_graph as c <EOL> from deel . tensor import * <EOL> from deel . network import * <EOL> import copy <EOL> from deel import * <EOL> import deel . network <EOL> import chainer <EOL> import json <EOL> import os <EOL> import multiprocessing <EOL> import threading <EOL> import time <EOL> import six <EOL> import numpy as np <EOL> import os . path <EOL> from PIL import Image <EOL> from six . moves import queue <EOL> import pickle <EOL> import hashlib <EOL> import datetime <EOL> import sys <EOL> import random <EOL> class AlexNet ( ImageNet ) : <EOL> def __init__ ( self , model = '<STR_LIT>' , mean = '<STR_LIT>' , labels = '<STR_LIT>' , optimizer = None ) : <EOL> super ( AlexNet , self ) . __init__ ( '<STR_LIT>' , in_size = <NUM_LIT> ) <EOL> self . func = LoadCaffeModel ( model ) <EOL> self . labels = np . loadtxt ( labels , str , delimiter = "<STR_LIT:\t>" ) <EOL> if Deel . gpu >= <NUM_LIT:0> : <EOL> cuda . check_cuda_available ( ) <EOL> if Deel . gpu >= <NUM_LIT:0> : <EOL> cuda . get_device ( self . gpu ) . use ( ) <EOL> self . func . to_gpu ( ) <EOL> mean_image = np . load ( mean ) <EOL> ImageNet . mean_image = mean_image <EOL> cropwidth = <NUM_LIT> - self . in_size <EOL> start = cropwidth // <NUM_LIT:2> <EOL> stop = start + self . in_size <EOL> self . mean_image = mean_image [ : , start : stop , start : stop ] . copy ( ) <EOL> self . batchsize = <NUM_LIT:1> <EOL> self . x_batch = np . ndarray ( ( self . batchsize , <NUM_LIT:3> , self . in_size , self . in_size ) , dtype = np . float32 ) <EOL> def forward ( self , x , layer = '<STR_LIT>' ) : <EOL> y , = self . func ( inputs = { '<STR_LIT:data>' : x } , outputs = [ layer ] , train = False ) <EOL> return y <EOL> def predict ( self , x , layer = '<STR_LIT>' ) : <EOL> y , = self . func ( inputs = { '<STR_LIT:data>' : x } , outputs = [ layer ] , train = False ) <EOL> return F . softmax ( y ) <EOL> def classify ( self , x = None ) : <EOL> if x is None : <EOL> x = Tensor . context <EOL> if not isinstance ( x , ImageTensor ) : <EOL> x = self . Input ( x ) <EOL> _x = Variable ( x . value , volatile = True ) <EOL> result = self . predict ( _x ) <EOL> result = Variable ( result . data ) <EOL> t = ChainerTensor ( result ) <EOL> t . owner = self <EOL> t . use ( ) <EOL> return t <EOL> def layerDim ( self , layer = '<STR_LIT>' ) : <EOL> image = self . Input ( '<STR_LIT>' ) . value <EOL> self . x_batch [ <NUM_LIT:0> ] = image <EOL> xp = Deel . xp <EOL> x_data = xp . asarray ( self . x_batch ) <EOL> x = chainer . Variable ( x_data , volatile = True ) <EOL> y , = self . func ( inputs = { '<STR_LIT:data>' : x } , outputs = [ layer ] , train = False ) <EOL> return y . data . shape <EOL> def feature ( self , x , layer = u'<STR_LIT>' ) : <EOL> if x is None : <EOL> x = Tensor . context <EOL> if not isinstance ( x , ImageTensor ) : <EOL> x = self . Input ( x ) <EOL> image = x . value <EOL> self . x_batch [ <NUM_LIT:0> ] = image <EOL> xp = Deel . xp <EOL> x_data = xp . asarray ( self . x_batch ) <EOL> if Deel . gpu >= <NUM_LIT:0> : <EOL> x_data = cuda . to_gpu ( x_data ) <EOL> x = chainer . Variable ( x_data , volatile = True ) <EOL> score = self . forward ( x , layer = layer ) <EOL> if Deel . gpu >= <NUM_LIT:0> : <EOL> score = cuda . to_cpu ( score . data ) <EOL> dim = getDim ( score . shape ) <EOL> score = score . reshape ( dim ) <EOL> else : <EOL> dim = getDim ( score . data . shape ) <EOL> score = score . data . reshape ( dim ) <EOL> score = chainer . Variable ( score * <NUM_LIT> , volatile = True ) <EOL> t = ChainerTensor ( score ) <EOL> t . owner = self <EOL> t . use ( ) <EOL> return t </s>
