def create_net(depth, nb_classes, batchnorm=False, use_cpu=False): if use_cpu: layer.engine = 'singacpp' net = ffnet.FeedForwardNet() net = create_layers(net, cfg[depth], (3, 224, 224), batchnorm) net.add(Flatten('flat')) net.add(Dense('dense/classifier.0', 4096)) net.add(Activation('act/classifier.1')) net.add(Dropout('dropout/classifier.2')) net.add(Dense('dense/classifier.3', 4096)) net.add(Activation('act/classifier.4')) net.add(Dropout('dropout/classifier.5')) net.add(Dense('dense/classifier.6', nb_classes)) return net
def create_net(num_classes=1001, sample_shape=(3, 299, 299), is_training=True, dropout_keep_prob=0.8, final_endpoint='InceptionV4/Mixed_7d', aux_endpoint='InceptionV4/Mixed_6e'): """Creates the Inception V4 model. Args: num_classes: number of predicted classes. is_training: whether is training or not. dropout_keep_prob: float, the fraction to keep before final layer. final_endpoint, aux_endpoint: refer to inception_v4_base() Returns: logits: the logits outputs of the model. end_points: the set of end_points from the inception model. """ end_points = {} name = 'InceptionV4' net, end_points = inception_v4_base(sample_shape, final_endpoint=final_endpoint, aux_endpoint=aux_endpoint) # Auxiliary Head logits if aux_endpoint is not None: # 17 x 17 x 1024 aux_logits = end_points[aux_endpoint + '-aux'] blk = name + '/AuxLogits' net.add( AvgPooling2D('%s/AvgPool_1a_5x5' % blk, 5, stride=3, border_mode='VALID'), aux_logits) t = conv2d(net, '%s/Conv2d_1b_1x1' % blk, 128, 1) conv2d(net, '%s/Conv2d_2a' % blk, 768, t.get_output_sample_shape()[1:3], border_mode='VALID') net.add(Flatten('%s/flat' % blk)) end_points[blk] = net.add(Dense('%s/Aux_logits' % blk, num_classes)) # Final pooling and prediction # 8 x 8 x 1536 blk = name + '/Logits' last_layer = end_points[final_endpoint] net.add( AvgPooling2D('%s/AvgPool_1a' % blk, last_layer.get_output_sample_shape()[1:3], border_mode='VALID'), last_layer) # 1 x 1 x 1536 net.add(Dropout('%s/Dropout_1b' % blk, 1 - dropout_keep_prob)) net.add(Flatten('%s/PreLogitsFlatten' % blk)) # 1536 end_points[blk] = net.add(Dense('%s/Logits' % blk, num_classes)) return net, end_points
def create_net(num_classes=1001, sample_shape=(3, 299, 299), final_endpoint='InceptionV3/Mixed_7c', aux_endpoint='InceptionV3/Mixed_6e', dropout_keep_prob=0.8): """Creates the Inception V4 model. Args: num_classes: number of predicted classes. dropout_keep_prob: float, the fraction to keep before final layer. final_endpoint: 'InceptionV3/Mixed_7d', aux_endpoint: Returns: logits: the logits outputs of the model. end_points: the set of end_points from the inception model. """ name = 'InceptionV3' net, end_points = inception_v3_base(name, sample_shape, final_endpoint, aux_endpoint) # Auxiliary Head logits if aux_endpoint is not None: # 8 x 8 x 1280 aux_logits = end_points[aux_endpoint + '-aux'] blk = name + '/AuxLogits' net.add( AvgPooling2D('%s/AvgPool_1a_5x5' % blk, 5, stride=3, border_mode='VALID'), aux_logits) t = conv2d(net, '%s/Conv2d_1b_1x1' % blk, 128, 1) s = t.get_output_sample_shape()[1:3] conv2d(net, '%s/Conv2d_2a_%dx%d' % (blk, s[0], s[1]), 768, s, border_mode='VALID') net.add(Conv2D('%s/Conv2d_2b_1x1' % blk, num_classes, 1)) net.add(Flatten('%s/flat' % blk)) # Final pooling and prediction # 8 x 8 x 2048 blk = name + '/Logits' last_layer = end_points[final_endpoint] net.add( AvgPooling2D('%s/AvgPool_1a' % blk, last_layer.get_output_sample_shape()[1:3], 1, border_mode='VALID'), last_layer) # 1 x 1 x 2048 net.add(Dropout('%s/Dropout_1b' % blk, 1 - dropout_keep_prob)) net.add(Conv2D('%s/Conv2d_1c_1x1' % blk, num_classes, 1)) end_points[blk] = net.add(Flatten('%s/flat' % blk)) # 2048 return net, end_points
def create_net(shape, weight_path='bvlc_googlenet.pickle'): net = ffnet.FeedForwardNet() net.add(Conv2D('conv1/7x7_s2', 64, 7, 2, pad=3, input_sample_shape=shape)) c1 = net.add(Activation('conv1/relu_7x7')) pool1 = pool(net, c1, 'pool1/3x3_s2', 3, 2) norm1 = net.add(LRN('pool1/norm1', 5, 0.0001, 0.75)) c3x3r = conv(net, norm1, 'conv2', 64, 1, suffix='3x3_reduce') c3x3 = conv(net, c3x3r, 'conv2', 192, 3, pad=1, suffix='3x3') norm2 = net.add(LRN('conv2/norm2', 5, 0.0001, 0.75)) pool2 = pool(net, norm2, 'pool2/3x3_s2', 3, 2) i3a = inception(net, pool2, 'inception_3a', 64, 96, 128, 16, 32, 32) i3b = inception(net, i3a, 'inception_3b', 128, 128, 192, 32, 96, 64) pool3 = pool(net, i3b, 'pool3/3x3_s2', 3, 2) i4a = inception(net, pool3, 'inception_4a', 192, 96, 208, 16, 48, 64) i4b = inception(net, i4a, 'inception_4b', 160, 112, 224, 24, 64, 64) i4c = inception(net, i4b, 'inception_4c', 128, 128, 256, 24, 64, 64) i4d = inception(net, i4c, 'inception_4d', 112, 144, 288, 32, 64, 64) i4e = inception(net, i4d, 'inception_4e', 256, 160, 320, 32, 128, 128) pool4 = pool(net, i4e, 'pool4/3x3_s2', 3, 2) i5a = inception(net, pool4, 'inception_5a', 256, 160, 320, 32, 128, 128) i5b = inception(net, i5a, 'inception_5b', 384, 192, 384, 48, 128, 128) pool5 = net.add(AvgPooling2D('pool5/7x7_s1', 7, 1, pad=0)) drop5 = net.add(Dropout('drop', 0.4)) flat = net.add(Flatten('flat')) dense = net.add(Dense('loss3/classifier', 1000)) # prob=net.add(Softmax('softmax')) net.load(weight_path, use_pickle=True) print('total num of params %d' % (len(net.param_names()))) # SINGA and Caffe have different layout for the weight matrix of the dense # layer for key, val in zip(net.param_names(), net.param_values()): # print key if key == 'loss3/classifier_weight' or key == 'loss3/classifier/weight': tmp = tensor.to_numpy(val) tmp = tmp.reshape(tmp.shape[::-1]) val.copy_from_numpy(np.transpose(tmp)) return net