def forwardSize(self, inputsize): isize = inputsize[0] if len(isize) != 4: raise AssertionError('Conv input size should has a lenght of 4') if self.inputFeature is not None and isize[1] != self.inputFeature: raise AssertionError('Conv put feature map size does not match') if self.outputFeature is not None: self.inputFeature = isize[1] self.mapMulti = self.outputFeature / self.inputFeature elif self.mapMulti is not None: self.inputFeature = isize[1] self.outputFeature = int(self.inputFeature * self.mapMulti) weightIniter = winit.XavierInit() initweight = weightIniter.initialize( (self.outputFeature, self.inputFeature, *self.filterSize)) initbias = floatX(np.zeros((self.outputFeature, ))) self.w = theano.shared(initweight, borrow=True) self.b = theano.shared(initbias, borrow=True) retSize = None if self.border == 'half': retSize = [(isize[0], self.outputFeature, int(isize[2] / self.subsample[0]), int(isize[3] / self.subsample[1]))] else: raise NotImplementedError return retSize
def forwardSize(self, inputsize): isize = inputsize[0] ainit = floatX(np.ones((isize[1], ))) self.a = theano.shared(ainit, borrow=True) if len(isize) == 4: return [(isize[0], isize[1], isize[2], isize[3])] if len(isize) == 2: return [(isize[0], isize[1])] else: raise IndexError
def forwardSize(self, inputsize): #print(inputsize) xsize = inputsize[0] isize = xsize[1:] #print('bn.size: {}'.format(isize)) betaInit = floatX(np.zeros(isize)) self.beta = theano.shared(betaInit, name=self.name+'beta', borrow=True) gammaInit = floatX(np.ones(isize)) self.gamma = theano.shared(gammaInit, name=self.name+'gamma', borrow=True) meanInit = floatX(np.zeros(isize)) self.meanStats = theano.shared(meanInit, borrow=True) varInit = floatX(np.ones(isize)) self.varStats = theano.shared(varInit, borrow=True) return inputsize
def initialize(self, shape): if len(shape) == 2: # this is full connection coeff = np.sqrt(self.nonlinearGain / shape[0]) elif len(shape) == 4: # this is conv2d coeff = np.sqrt(self.nonlinearGain / np.prod(shape[1:])) else: raise NotImplementedError("Unknown shape") #print("coeff is {}".format(coeff)) return floatX(np.random.randn(*shape) * coeff)
def test_conv2d_forward(self): x = np.asarray(rng.uniform(low=-1, high=1, size=(500, 1, 28, 28))) w = floatX(np.random.randn(20, 1, *(3, 3)) * 0.01) input_x = T.tensor4() input_w = T.tensor4() y = T.nnet.conv2d(input_x, input_w, border_mode='half', subsample=(1, 1)) f = theano.function(inputs=[input_x, input_w], outputs=y, allow_input_downcast=True) y_shape = f(x, w).shape self.assertEqual(y_shape, (500, 20, 28, 28))
def forwardSize(self, inputsize): isize = inputsize[0] #print("upconv2d input size: {}".format(isize)) if not (len(isize) == 4 or len(isize) == 2): raise IndexError self.batchSize = isize[0] if len(isize) == 2: self.isAfterFullConn = True self.dataSize = ( 1, 1, ) self.outputFeatureDim = isize[1] self.inputFeatureDim = isize[1] // self.mapMulti elif len(isize) == 4: if self.mapMulti is None and isize[1] != self.outputFeatureDim: raise IndexError self.dataSize = isize[2:] if self.mapMulti is not None: self.outputFeatureDim = isize[1] self.inputFeatureDim = isize[1] // self.mapMulti initweight = floatX( np.random.randn(self.outputFeatureDim, self.inputFeatureDim, * self.filterSize) * 0.01) self.w = theano.shared(initweight, borrow=True) retSize = None if self.border == 'valid': retSize = [(isize[0], self.inputFeatureDim, int(isize[2] * self.subsample[0]), int(isize[3] * self.subsample[1]))] else: raise NotImplementedError #print('upconv2d output size: {}'.format(retSize)) return retSize
def convert(def_path, caffemodel_path, output_path, phase): # read from .prototxt file to collect layers. params = caffe.proto.caffe_pb2.NetParameter() with open(def_path, 'r') as def_file: text_format.Merge(def_file.read(), params) layerName2InstanceMap = {} # dict of str:layer layerName2Bottom = {} # dict of str:list inplaceOpMap = {} # dict of str:str inputLayer = None for layer in params.layer: lname = layer.name ltype = layer.type ltop = layer.top lbottom = layer.bottom if len(ltop) > 0 and len(lbottom) > 0 and ltop[0] == lbottom[0]: inplaceOpMap[lbottom[0]] = lname if ltype == 'Input': nl = L.RawInput((layer.input_param.shape[0].dim[1], layer.input_param.shape[0].dim[2], layer.input_param.shape[0].dim[3])) nl.name = lname layerName2InstanceMap[layer.name] = nl inputLayer = nl elif ltype == 'Convolution': name = layer.name bottom = layer.bottom output_feature_map = layer.convolution_param.num_output kernel_size = layer.convolution_param.kernel_size[0] stride = (1, 1) if len(layer.convolution_param.stride) > 0: stride = layer.convolution_param.stride[0] stride = (stride, stride) nl = L.Conv2d(filter_size=(kernel_size, kernel_size), output_feature=output_feature_map, subsample=stride) nl.name = lname layerName2InstanceMap[name] = nl layerName2Bottom[name] = layer.bottom elif ltype == 'ReLU': nl = L.Relu() nl.name = lname name = layer.name bottom = layer.bottom top = layer.top layerName2InstanceMap[name] = nl layerName2Bottom[name] = layer.bottom elif layer.type == 'Pooling': name = layer.name # 0: max, 1: average, 2: stochastic poolingMethod = layer.pooling_param.pool if poolingMethod == 0: poolingMethod = 'max' elif poolingMethod == 1: poolingMethod = 'avg' kernel_size = layer.pooling_param.kernel_size stride = layer.pooling_param.stride pad = layer.pooling_param.pad nl = L.Pooling(dsize=(kernel_size, kernel_size), stride=(stride, stride), pad=(pad, pad), mode=poolingMethod) nl.name = lname layerName2InstanceMap[name] = nl layerName2Bottom[name] = layer.bottom elif layer.type == 'LRN': name = layer.name local_size = layer.lrn_param.local_size alpha = layer.lrn_param.alpha beta = layer.lrn_param.beta nl = L.LRN(local_size=local_size, alpha=alpha, beta=beta) nl.name = lname layerName2InstanceMap[name] = nl layerName2Bottom[name] = layer.bottom elif layer.type == 'Concat': name = layer.name nl = L.Concat() nl.name = lname layerName2InstanceMap[name] = nl layerName2Bottom[name] = layer.bottom elif layer.type == 'Dropout': name = layer.name ratio = layer.dropout_param.dropout_ratio nl = L.Dropout(p=ratio) nl.name = lname layerName2InstanceMap[name] = nl layerName2Bottom[name] = layer.bottom elif layer.type == 'InnerProduct': name = layer.name output_feature = layer.inner_product_param.num_output nl = L.FullConn(output_feature=output_feature) nl.name = lname layerName2InstanceMap[name] = nl if not isinstance(layerName2InstanceMap[layer.bottom[0]], L.Flatten): print('Insert flatten layer before full connection') fl = L.Flatten() fl.name = name + 'pre_flatten' layerName2InstanceMap[fl.name] = fl layerName2Bottom[fl.name] = layer.bottom layerName2Bottom[name] = (fl.name, ) else: layerName2Bottom[name] = layer.bottom elif layer.type == 'Softmax': name = layer.name nl = L.SoftMax() nl.name = lname layerName2InstanceMap[name] = nl layerName2Bottom[name] = layer.bottom else: print(layer.type) raise NotImplementedError('unknown caffe layer.') print(inplaceOpMap) # create the network n = N.Network() for name in layerName2InstanceMap.keys(): if name in layerName2Bottom: for bottomLayer in layerName2Bottom[name]: if bottomLayer in inplaceOpMap.keys( ) and inplaceOpMap[bottomLayer] != name: n.connect(layerName2InstanceMap[inplaceOpMap[bottomLayer]], layerName2InstanceMap[name], reload=True) else: n.connect(layerName2InstanceMap[bottomLayer], layerName2InstanceMap[name], reload=True) n.setInput(inputLayer, reload=True) n.buildForwardSize() # read .caffemodel file to load real parameters. net_param = caffe_pb2.NetParameter() with open(caffemodel_path, 'rb') as f: net_param.ParseFromString(f.read()) for layer in net_param.layers: lname = layer.name # str ltype = layer.type # int, the mapping is defined in caffe/src/caffe/proto/caffe.proto bottom = layer.bottom # RepeatedScalarContainer top = layer.top # RepeatedScalarContainer print("{}, {}".format(lname, ltype)) if lname in layerName2InstanceMap.keys(): if ltype == 4: # convolution w = np.array(layer.blobs[0].data).reshape( (layer.blobs[0].num, layer.blobs[0].channels, layer.blobs[0].height, layer.blobs[0].width), ) bias = np.array(layer.blobs[1].data).reshape( (layer.blobs[1].width), ) print(w.shape) if w.shape != layerName2InstanceMap[lname].w.get_value().shape: print(w.shape) print(layerName2InstanceMap[lname].w.get_value().shape) raise Exception('Error, w shape do not match.') if bias.shape != layerName2InstanceMap[lname].b.get_value( ).shape: print(bias.shape) print(layerName2InstanceMap[lname].b.get_value().shape) raise Exception('Error, b shape do not match.') layerName2InstanceMap[lname].w = theano.shared(floatX(w), borrow=True) layerName2InstanceMap[lname].b = theano.shared(floatX(bias), borrow=True) elif ltype == 5: # data print('seen name: {}, {}'.format(lname, ltype)) elif ltype == 18: # relu print('seen name: {}, {}'.format(lname, ltype)) elif ltype == 17: # pooling print('seen name: {}, {}'.format(lname, ltype)) elif ltype == 15: # lrn print('seen name: {}, {}'.format(lname, ltype)) elif ltype == 3: # concat print('seen name: {}, {}'.format(lname, ltype)) elif ltype == 6: # dropout print('seen name: {}, {}'.format(lname, ltype)) elif ltype == 14: # fullconn print('seen name: {}, {}'.format(lname, ltype)) w = np.array(layer.blobs[0].data).reshape( (layer.blobs[0].width, layer.blobs[0].height), ) bias = np.array(layer.blobs[1].data).reshape( (layer.blobs[1].width), ) print("heh:{}".format(lname)) print(layerName2InstanceMap[lname]) print(layerName2InstanceMap[lname].w) if w.shape != layerName2InstanceMap[lname].w.get_value().shape: print(w.shape) print(layerName2InstanceMap[lname].w.get_value().shape) raise Exception('Error, w shape do not match.') if bias.shape != layerName2InstanceMap[lname].b.get_value( ).shape: print(bias.shape) print(layerName2InstanceMap[lname].b.get_value().shape) raise Exception('Error, b shape do not match.') layerName2InstanceMap[lname].w = theano.shared(floatX(w), borrow=True) layerName2InstanceMap[lname].b = theano.shared(floatX(bias), borrow=True) elif ltype == 21: # 21 for Softmax output, 20 for softmax print('seen name: {}, {}'.format(lname, ltype)) else: print('seen name: {}, unknown type: {}'.format(lname, ltype)) else: if len(layer.blobs) > 0: #raise NotImplementedError('unseen layer with blobs: {}'.format(lname)) print('error, unseen layer with blobs: {}, {}'.format( lname, ltype)) else: #print('warning, unseen name: {}, {}'.format(lname, ltype)) pass # finally build the network. n.build(reload=True) return n