关乎symbol和module的一些基本属性
# 查看json每一个op的属性:kernel size、padding、stride等
sym.attr_dict() # 返回一个字典,根据key获取对应op的属性
# 查看网络的输出name
sym.list_outputs()
# 查看网络所有的输入节点name
sym.list_arguments()
# 查看网络所有内部节点
sym.get_internals()
# 获取网络的参数节点name
mod.get_params()[0]
# 获取网络的中间结果 fc7 output
all_layers = sym.get_internals()
sym = all_layers['fc7_output']
mod = mx.mod.Module(symbol=sym, context=ctx, label_names=None) # 然后做一次inference就能获取fc7 output
————————————————
版权声明:本文为CSDN博主「JustForYouW」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wwwhp/article/details/84556909
单gpu:
gpu_id=0
ctx = mx.gpu(gpu_id)
nets = []
sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch)
#arg_params, aux_params = ch_dev(arg_params, aux_params, ctx)
all_layers = sym.get_internals()
sym = all_layers['fc1_output']
model = mx.mod.Module(symbol=sym, context=ctx, label_names=None)
#model.bind(data_shapes=[('data', (args.batch_size, 3, image_size[0], image_size[1]))], label_shapes=[('softmax_label', (args.batch_size,))])
model.bind(data_shapes=[('data', (args.batch_size, 3, image_size[0],
image_size[1]))])
model.set_params(arg_params, aux_params)
nets.append(model)
#使用单块显卡时的代码:
devices = mx.gpu(0)
data = mx.nd.array(batch_data).as_in_context(devices)
label = mx.nd.array(batch_label).as_in_context(devices)
# 更新生成器G
with autograd.record():
output = G(data)
errG_idt = idt_loss(output, label)
errG_idt.backward()
metric_G.update([label, ], [output, ])
G_trainer.step(batch_size)
多GPU:
区别有很多,这是一点点不一样
1、mxnet 的batch ,设置的是单卡的,多卡是 batch*多卡,多一张卡,速度可以提升一倍
2、pytorch 是 batch/多卡数量,分配给每个gpu,设置的batch是总的batch
使用多块显卡时的代码:
devices = [mx.gpu(0), mx.gpu(1), mx.gpu(2)]
gpu_Xs = gutils.split_and_load(batch_data, devices)
gpu_ys = gutils.split_and_load(batch_label, devices)
ls = []
with autograd.record():
for gpu_X, gpu_y in zip(gpu_Xs, gpu_ys):
output = G(gpu_X)
errG_idt = idt_loss(output, gpu_y)
ls.append(errG_idt)
# update metrics
metric_G.update([gpu_y, ], [output, ])
for l in ls:
l.backward()
G_trainer.step(batch_size)