def getDenseNet(num_classes, ctx): densenet = vision.densenet201(pretrained=True, ctx=ctx) net = vision.densenet201(classes=num_classes, prefix='densenet0_') with net.name_scope(): net.output = nn.Dense(num_classes, flatten=True) net.output.collect_params().initialize(mx.init.Xavier( rnd_type='gaussian', factor_type="in", magnitude=2), ctx=ctx) net.features = densenet.features net.collect_params().reset_ctx(ctx) inputs = mx.sym.var('data') out = net(inputs) internals = out.get_internals() outputs = [ internals['densenet0_conv3_fwd_output'], internals['densenet0_stage4_concat15_output'], internals['densenet0_dense1_fwd_output'] ] feat_model = gluon.SymbolBlock(outputs, inputs, params=net.collect_params()) feat_model._prefix = 'densenet0_' return feat_model
def load_model(): print("we're loading densenet model: \ https://modelzoo.co/model/densely-connected-convolutional-networks-2") densenetX = vision.densenet201(pretrained=True) print("we just loaded: ") type(densenetX) return densenetX
def load_model(): print("we're loading densenet model: \ https://modelzoo.co/model/densely-connected-convolutional-networks-2") densenetX = vision.densenet201(pretrained=True) print("we just loaded: ") type(densenetX) print("Now we're loading YOLO") yolo_netX = model_zoo.get_model('yolo3_darknet53_voc', pretrained=True) type(yolo_netX) return densenetX, yolo_netX
def densenet201mxnetload(): net = vision.densenet201(pretrained=True) net.hybridize() return net
from mxnet.gluon import nn from mxnet.gluon.model_zoo import vision as models dense_net=models.densenet201(pretrained=False,prefix="a_") net=nn.HybridSequential(prefix="a_") for layer in dense_net.features[0:13]: net.add(layer) class output_block(nn.HybridBlock): def __init__(self,**kwargs): super (output_block,self).__init__(**kwargs) with self.name_scope(): self.dense1=nn.Dense(256,activation='relu') self.globalavg=nn.GlobalAvgPool2D() self.dense2=nn.Dense(1) def hybrid_forward(self,F,x): out=self.globalavg(x) out=self.dense1(out) out=self.dense2(out) return out output=output_block(prefix="a_") with net.name_scope(): net.add(output)
out = net(data.as_in_context(mx.gpu())) print(out.shape) out = out.reshape( (30720)) # dense169 6656 dense201 7890 //122880 out = out.as_in_context(mx.cpu()) out = out.asnumpy() res = '' for item in out: res += str(item) + ' ' #print(res) fw1.write(res + '\n') label = root.strip().split('\\')[-1] fw2.write(label + '\n') print(label) fw1.close() fw2.close() print('net done!!!') pretrained_net = models.densenet201(pretrained=True) net = nn.HybridSequential() for layer in pretrained_net.features: net.add(layer) #print(net) ctx = mx.gpu() net.collect_params().reset_ctx(ctx) net.hybridize() dir = '../data/dense1920' suffix = ['jpg'] batch_net(dir, suffix, net)