def __init__(self, ctx=mx.cpu(), warmup=10, runs=50, inputs=None): # Set the default Inputs. # Default prediction is (32, 1000, 1) to mimic a prediction of batch_size=32 and 1000 class classification. default_parameters = { "pred": (32, 1000, 1), "pred_initializer": nd.normal, "label": (32, 1000, 1), "label_initializer": nd.normal, "weight": 1.0, "run_backward": False, "dtype": "float32" } super().__init__(ctx=ctx, warmup=warmup, runs=runs, default_parameters=default_parameters, custom_parameters=inputs) # Create a random prediction and label tensor self.pred = get_mx_ndarray(ctx=self.ctx, in_tensor=self.inputs["pred"], dtype=self.inputs["dtype"], initializer=self.inputs["pred_initializer"], attach_grad=self.inputs["run_backward"]) self.label = get_mx_ndarray( ctx=self.ctx, in_tensor=self.inputs["label"], dtype=self.inputs["dtype"], initializer=self.inputs["label_initializer"], attach_grad=self.inputs["run_backward"]) self.block = loss.L1Loss(weight=self.inputs["weight"], batch_axis=0) self.block.initialize(ctx=self.ctx)
def train(net,start_epoch,epochs,lr,ctx): cls_loss = gloss.SoftmaxCrossEntropyLoss() box_loss = gloss.L1Loss() cls_metric = mx.metric.Accuracy() box_metric = mx.metric.MAE() data_shape = 256 batch_size = 1 train_data, test_data, class_names, num_class = get_iterators(data_shape, batch_size) train_data.reshape(label_shape=(6, 5)) train_data = test_data.sync_label_shape(train_data) trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': lr, 'wd': 0.0005}) log_interval = 1 for epoch in range(start_epoch, epochs+start_epoch): # reset iterator and tick train_data.reset() cls_metric.reset() box_metric.reset() tic = time.time() # iterate through all batch for i, batch in enumerate(train_data): #print(i) btic = time.time() # record gradients with ag.record(): x = batch.data[0].as_in_context(ctx) y = batch.label[0].as_in_context(ctx) default_anchors, class_predictions, box_predictions = net(x) box_target, box_mask, cls_target = training_targets(default_anchors, class_predictions, y) # losses loss1 = cls_loss(class_predictions, cls_target) loss2 = box_loss(box_predictions, box_target, box_mask) # sum all losses loss = 0.1*loss1 + loss2 # backpropagate loss.backward() # apply trainer.step(batch_size) # update metrics cls_metric.update([cls_target], [nd.transpose(class_predictions, (0, 2, 1))]) box_metric.update([box_target], [box_predictions * box_mask]) if (i + 1) % 10 == 0: name1, val1 = cls_metric.get() name2, val2 = box_metric.get() print('[Epoch %d Batch %d] speed: %f samples/s, training: %s=%f, %s=%f' %(epoch ,i, batch_size/(time.time()-btic), name1, val1, name2, val2)) if (i+1)%2000 == 0: net.save_params('ssd_%d.params'%(i)) # end of epoch logging name1, val1 = cls_metric.get() name2, val2 = box_metric.get() print('[Epoch %d] training: %s=%f, %s=%f'%(epoch, name1, val1, name2, val2)) print('[Epoch %d] time cost: %f'%(epoch, time.time()-tic)) valid(net,test_data,epoch,ctx) # we can save the trained parameters to disk net.save_params('ssd_%d.params' % (epochs+start_epoch))
def train(): batch_size = 32 train_iter, _ = d2l.load_data_pikachu(batch_size) ctx, net = d2l.try_gpu(), TinySSD(num_classes=1) net.initialize(init=init.Xavier(), ctx=ctx) trainer = gluon.Trainer(net.collect_params(), 'sgd', { 'learning_rate': 0.2, 'wd': 5e-4 }) cls_loss = gloss.SoftmaxCrossEntropyLoss() bbox_loss = gloss.L1Loss() def calc_loss(cls_preds, cls_labels, bbox_preds, bbox_labels, bbox_masks): cls = cls_loss(cls_preds, cls_labels) bbox = bbox_loss(bbox_preds * bbox_masks, bbox_labels * bbox_masks) return cls + bbox def cls_eval(cls_preds, cls_labels): # 由于类别预测结果放在最后一维,argmax需要指定最后一维 return (cls_preds.argmax(axis=-1) == cls_labels).sum().asscalar() def bbox_eval(bbox_preds, bbox_labels, bbox_masks): return ((bbox_labels - bbox_preds) * bbox_masks).abs().sum().asscalar() for epoch in range(20): acc_sum, mae_sum, n, m = 0.0, 0.0, 0, 0 train_iter.reset() # 从头读取数据 start = time.time() for batch in train_iter: X = batch.data[0].as_in_context(ctx) Y = batch.label[0].as_in_context(ctx) with autograd.record(): # 生成多尺度的锚框,为每个锚框预测类别和偏移量 anchors, cls_preds, bbox_preds = net(X) # 为每个锚框标注类别和偏移量 bbox_labels, bbox_masks, cls_labels = \ contrib.nd.MultiBoxTarget(anchors, Y, cls_preds.transpose((0, 2, 1))) # 根据类别和偏移量的预测和标注值计算损失函数 l = calc_loss(cls_preds, cls_labels, bbox_preds, bbox_labels, bbox_masks) l.backward() trainer.step(batch_size) acc_sum += cls_eval(cls_preds, cls_labels) n += cls_labels.size mae_sum += bbox_eval(bbox_preds, bbox_labels, bbox_masks) m += bbox_labels.size if (epoch + 1) % 5 == 0: print( 'epoch %2d, class err %.2e, bbox mae %.2e, time %.1f sec' % (epoch + 1, 1 - acc_sum / n, mae_sum / m, time.time() - start))
def __init__(self, batch_axis=0, weight=None, box_loss_type='mse', **kwargs): super(YOLOv3Loss, self).__init__(weight, batch_axis, **kwargs) self._sigmoid_ce = mloss.SigmoidBinaryCrossEntropyLoss( from_sigmoid=False) self.target = YOLOV3TargetMerger(20, ignore_iou_thresh=0.5) self._loss_type = box_loss_type if box_loss_type == 'mse': self._l1_loss = mloss.L1Loss() # self._l2_loss = mloss.L2Loss() else: self._iou_loss = IoULoss(x1y1x2y2=True, loss_type=box_loss_type)
def evaluate_test(data_iter, net, ctx, loss = gloss.L1Loss()): acc_sum, n = nd.array([0], ctx=ctx), 0 for inputs, features, outputs in data_iter: batch_size = inputs.shape[0] #in case batch_size is changed state = net.begin_state(batch_size = batch_size, ctx = ctx) inputs = inputs.as_in_context(ctx).astype('float32') features = features.as_in_context(ctx).astype('float32') outputs = outputs.as_in_context(ctx).astype('float32') for idx in range(inputs.shape[1]): cur_features = features.swapaxes(0, 1)[idx] # (batch_size, num_features) cur_price =inputs.swapaxes(0, 1)[idx] # (batch_size, num_features) output, state = net(cur_features, cur_price, state) acc_sum = acc_sum + (loss(output.reshape(batch_size,), outputs.swapaxes(0, 1)[idx])).sum() n = n + batch_size return acc_sum.asscalar() / n
def __init__(self,num_feature,num_hidden,num_layers,epoch,batch_size,ntype='LSTM',bidirectional = False,dropout=0,trainmethod = 'adam',\ lr=0.01,loss = gloss.L1Loss(),ctx = gpu(0),datashuffle=False,initialfunc=mx.init.Xavier(),prin=False,**kwargs): ''' 用于循环神经网络的训练和预测, 主函数 :param num_feature: 特征个数 :param num_hidden: 隐层神经元数目 :param num_layers: 隐层数 :param epoch: 训练轮数 :param batch_size: 小批量大小,在这里是股票实体数目多少 :param ntype: 神经网络类型,默认LSTM :param bidirectional: 是否双向神经网络,默认否 :param dropout: 丢弃率,默认不丢弃 :param trainmethod: 训练器,默认adam :param lr: 学习率 :param loss: 损失函数 :param ctx: 训练设备,默认gpu :param datashuffle: 是否随机打乱数据,默认否 :param initialfunc: 初始化方法,默认Xaiver :param prin: 是否输出训练指示,默认否 :param kwargs: 其他参数 ''' super(lstmmodule, self).__init__(**kwargs) mx.random.seed(521, ctx=gpu()) mx.random.seed(521) self.ctx = ctx self.net = BaseRnn(num_feature, num_hidden, num_layers, ntype, bidirectional, dropout) self.trainmethod = trainmethod self.lr = lr self.intialfunc = initialfunc self.net.collect_params().initialize(self.intialfunc, ctx=self.ctx) self.Trainer = gluon.Trainer(self.net.collect_params(), trainmethod, {'learning_rate': lr}) self.loss = loss self.datashuffle = datashuffle self.epoch = epoch self.batch_size = batch_size self.prin = prin
def calc_loss(cls_preds, cls_labels, bbox_preds, bbox_labels, bbox_masks): cls_loss = gloss.SoftmaxCrossEntropyLoss() cls = cls_loss(cls_preds, cls_labels) bbox_loss = gloss.L1Loss() bbox = bbox_loss(bbox_preds * bbox_masks, bbox_labels * bbox_masks) return cls + bbox
batch_size = 32 train_iter, _ = d2l.load_data_pikachu(batch_size) ctx, net = d2l.try_gpu(), TinySSD(num_classes=1) net.initialize(init=init.Xavier(), ctx=ctx) trainer = gluon.Trainer(net.collect_params(), 'sgd', { 'learning_rate': 0.2, 'wd': 5e-4 }) # %% [markdown] # ### Define Losses # %% cls_loss = gloss.SoftmaxCrossEntropyLoss() bbox_loss = gloss.L1Loss() def calc_loss(cls_preds, cls_labels, bbox_preds, bbox_labels, bbox_masks): cls = cls_loss(cls_preds, cls_labels) bbox = bbox_loss(bbox_preds * bbox_masks, bbox_labels * bbox_masks) return cls + bbox # %% [markdown] # ### Define Evaluation Metrics # %% def cls_eval(cls_preds, cls_labels): # Because the category prediction results are placed in the final dimension,
features = nd.array(f) labels = nd.array(l) data_num = len(f) batch_size = 500 dataset = gdata.ArrayDataset(features, labels) data_iter = gdata.DataLoader(dataset, batch_size, shuffle=True) net = nn.Sequential() net.add(nn.Dense(100, activation='sigmoid'), nn.Dense(100, activation='sigmoid'), nn.Dense(3)) net.initialize(init.Uniform(scale=20)) loss = gloss.L1Loss() trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.001}) def accuracy(y_hat, y, error): sum_acc = 0 yy = y_hat - y yyy = yy.asnumpy() yyy = np.abs(yyy) i = 0 for i, val in enumerate(yyy): sum_acc = sum_acc + equal(val, error) return sum_acc
def build_model(self): # DataLoader train_transform = transforms.Compose([ transforms.RandomFlipLeftRight(), transforms.Resize((self.img_size + 30, self.img_size + 30)), transforms.RandomResizedCrop(self.img_size), transforms.ToTensor(), transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) ]) test_transform = transforms.Compose([ transforms.Resize((self.img_size, self.img_size)), transforms.ToTensor(), transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) ]) self.trainA = ImageFolder( os.path.join('dataset', self.dataset, 'trainA'), train_transform) self.trainB = ImageFolder( os.path.join('dataset', self.dataset, 'trainB'), train_transform) self.testA = ImageFolder( os.path.join('dataset', self.dataset, 'testA'), test_transform) self.testB = ImageFolder( os.path.join('dataset', self.dataset, 'testB'), test_transform) self.trainA_loader = DataLoader(self.trainA, batch_size=self.batch_size, shuffle=True, num_workers=self.num_workers) self.trainB_loader = DataLoader(self.trainB, batch_size=self.batch_size, shuffle=True, num_workers=self.num_workers) self.testA_loader = DataLoader(self.testA, batch_size=1, shuffle=False) self.testB_loader = DataLoader(self.testB, batch_size=1, shuffle=False) """ Define Generator, Discriminator """ self.genA2B = ResnetGenerator(input_nc=3, output_nc=3, ngf=self.ch, n_blocks=self.n_res, img_size=self.img_size, light=self.light) self.genB2A = ResnetGenerator(input_nc=3, output_nc=3, ngf=self.ch, n_blocks=self.n_res, img_size=self.img_size, light=self.light) self.disGA = Discriminator(input_nc=3, ndf=self.ch, n_layers=7) self.disGB = Discriminator(input_nc=3, ndf=self.ch, n_layers=7) self.disLA = Discriminator(input_nc=3, ndf=self.ch, n_layers=5) self.disLB = Discriminator(input_nc=3, ndf=self.ch, n_layers=5) self.whole_model = nn.HybridSequential() self.whole_model.add(*[ self.genA2B, self.genB2A, self.disGA, self.disGB, self.disLA, self.disLB ]) self.whole_model.hybridize(static_alloc=False, static_shape=False) """ Define Loss """ self.L1_loss = gloss.L1Loss() self.MSE_loss = gloss.L2Loss(weight=2) self.BCE_loss = gloss.SigmoidBCELoss() """ Initialize Parameters""" params = self.whole_model.collect_params() block = self.whole_model if not self.debug: force_init(block.collect_params('.*?_weight'), KaimingUniform()) force_init(block.collect_params('.*?_bias'), BiasInitializer(params)) block.collect_params('.*?_rho').initialize() block.collect_params('.*?_gamma').initialize() block.collect_params('.*?_beta').initialize() block.collect_params('.*?_state_.*?').initialize() else: pass block.collect_params().reset_ctx(self.dev) """ Trainer """ self.G_params = param_dicts_merge( self.genA2B.collect_params(), self.genB2A.collect_params(), ) self.G_optim = gluon.Trainer( self.G_params, 'adam', dict(learning_rate=self.lr, beta1=0.5, beta2=0.999, wd=self.weight_decay), ) self.D_params = param_dicts_merge(self.disGA.collect_params(), self.disGB.collect_params(), self.disLA.collect_params(), self.disLB.collect_params()) self.D_optim = gluon.Trainer( self.D_params, 'adam', dict(learning_rate=self.lr, beta1=0.5, beta2=0.999, wd=self.weight_decay), ) """ Define Rho clipper to constraint the value of rho in AdaILN and ILN""" self.Rho_clipper = RhoClipper(0, 1)
def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) self.loss_fn = loss.L1Loss()
import mxnet as mx from mxnet.gluon import nn from mxnet.gluon import data as gdata from mxnet.gluon import loss as gloss from gluoncv import loss as gcvloss from mxnet import autograd, init, contrib, nd, sym from utils.utils import calc_loss, cls_eval, bbox_eval cls_lossfunc = gloss.SoftmaxCrossEntropyLoss() # cls_lossfunc = gcvloss.FocalLoss() bbox_lossfunc = gloss.L1Loss() def training(data_iter, num_epoches, cls_lossfunc, bbox_lossfunc): # TODO: define the way that the model should be trained # wth gluon.Trainer(...) for eph in range(num_epoches): pass pass def validate(val_iter, net, ctx=mx.gpu()): acc_cls, acc_bbox, acc_l, n, m = 0, 0, 0, 0, 0 val_iter.reset() for batch in val_iter: X = batch.data[0].as_in_context(ctx) Y = batch.label[0].as_in_context(ctx) # generate anchors and generate bboxes anchors, cls_preds, bbox_preds = net(X) # assign classes and bboxes for each anchor bbox_labels, bbox_masks, cls_labels = nd.contrib.MultiBoxTarget(
net[1].initialize(ctx=mx.gpu()) net[1].collect_params().setattr('lr_mult', 2) net.hybridize() batch_size, edge_size = args.batch_size, args.input_size train_iter, val_iter = load_data_uav(args.data_path, batch_size, edge_size) batch = train_iter.next() trainer = mx.gluon.Trainer(net.collect_params(), args.optimize_method, { 'learning_rate': args.learning_rate, 'wd': 5E-4 }) cls_loss = gloss.L1Loss() def err_eval(bbox_preds, bbox_labels): return (bbox_labels - bbox_preds).abs().sum().asscalar() if args.load: net.load_parameters(args.model_path + "Focuser-is256e50bs08-pResNet50-dUSC1479raw-lr0.01x10") focusplot(net, 3200, 1029, thr=0.8, dp="../../data/uav/usc/") print("pause here") else: lerr, lcnt = [], [] for epoch in range(args.num_epoches): train_iter.reset(
def test(ctx=mx.cpu()): net = MySSD(1, num_anchors) net.initialize(init="Xavier", ctx=ctx) net.hybridize() # print(net) # x = nd.random.normal(0,1,(100,3,256,256), ctx=ctx) # net(x) batch_size, edge_size = args.batch_size, args.input_size train_iter, _ = predata.load_data_uav(args.data_path, batch_size, edge_size) batch = train_iter.next() batch.data[0].shape, batch.label[0].shape if batch_size >= 25: # show f*****g pikachuus in grid imgs = (batch.data[0][0:25].transpose((0, 2, 3, 1))) / 255 axes = utils.show_images(imgs, 5, 5).flatten() for ax, label in zip(axes, batch.label[0][0:25]): utils.show_bboxes(ax, [label[0][1:5] * edge_size], colors=['w']) plt.show() # net.initialize(init=init.Xavier(), ctx=ctx) trainer = mx.gluon.Trainer(net.collect_params(), 'sgd', { 'learning_rate': 0.2, 'wd': 5e-4 }) cls_loss = gloss.SoftmaxCrossEntropyLoss() bbox_loss = gloss.L1Loss() def calc_loss(cls_preds, cls_labels, bbox_preds, bbox_labels, bbox_masks): cls = cls_loss(cls_preds, cls_labels) bbox = bbox_loss(bbox_preds * bbox_masks, bbox_labels * bbox_masks) return cls + bbox def cls_eval(cls_preds, cls_labels): # the result from class prediction is at the last dim # argmax() should be assigned with the last dim of cls_preds return (cls_preds.argmax(axis=-1) == cls_labels).sum().asscalar() def bbox_eval(bbox_preds, bbox_labels, bbox_masks): return ((bbox_labels - bbox_preds) * bbox_masks).abs().sum().asscalar() IF_LOAD_MODEL = args.load if IF_LOAD_MODEL: net.load_parameters(args.model_path) else: for epoch in range(args.num_epoches): acc_sum, mae_sum, n, m = 0.0, 0.0, 0, 0 train_iter.reset( ) # reset data iterator to read-in images from beginning start = time.time() for batch in train_iter: X = batch.data[0].as_in_context(ctx) Y = batch.label[0].as_in_context(ctx) with autograd.record(): # generate anchors and generate bboxes im, anchors, cls_preds, bbox_preds = net(X) # assign classes and bboxes for each anchor bbox_labels, bbox_masks, cls_labels = nd.contrib.MultiBoxTarget( anchors, Y, cls_preds.transpose((0, 2, 1))) # calc loss l = calc_loss(cls_preds, cls_labels, bbox_preds, bbox_labels, bbox_masks) l.backward() trainer.step(batch_size) acc_sum += cls_eval(cls_preds, cls_labels) n += cls_labels.size mae_sum += bbox_eval(bbox_preds, bbox_labels, bbox_masks) m += bbox_labels.size if (epoch + 1) % 1 == 0: print( 'epoch %2d, class err %.2e, bbox mae %.2e, time %.1f sec' % (epoch + 1, 1 - acc_sum / n, mae_sum / m, time.time() - start)) net.save_parameters("myssd.params") def predict(X): im, anchors, cls_preds, bbox_preds = net(X.as_in_context(ctx)) # im = im.transpose((2, 3, 1, 0)).asnumpy() # imgs = [im[1:-2,1:-2, k, 0] for k in range(256)] # why are there boundary effect? # utils.show_images_np(imgs, 16, 16) # plt.show() # plt.savefig("./activdation/figbase%s"%nd.random.randint(0,100,1).asscalar()) # plt.imshow(nd.sum(nd.array(im[1:-2, 1:-2, :, :]), axis=2).asnumpy()[:, :, 0], cmap='gray') # plt.savefig("./suming_act") cls_probs = cls_preds.softmax().transpose((0, 2, 1)) output = contrib.nd.MultiBoxDetection(cls_probs, bbox_preds, anchors) idx = [i for i, row in enumerate(output[0]) if row[0].asscalar() != -1] if idx == []: raise ValueError("NO TARGET. Seq Terminated.") return output[0, idx] def display(img, output, threshold): lscore = [] for row in output: lscore.append(row[1].asscalar()) for row in output: score = row[1].asscalar() if score < min(max(lscore), threshold): continue h, w = img.shape[0:2] bbox = [row[2:6] * nd.array((w, h, w, h), ctx=row.context)] cv.rectangle(img, (bbox[0][0].asscalar(), bbox[0][1].asscalar()), (bbox[0][2].asscalar(), bbox[0][3].asscalar()), (0, 255, 0), 3) cv.imshow("res", img) cv.waitKey(60) cap = cv.VideoCapture(args.test_path) rd = 0 while True: ret, frame = cap.read() img = nd.array(frame) feature = image.imresize(img, 512, 512).astype('float32') X = feature.transpose((2, 0, 1)).expand_dims(axis=0) countt = time.time() output = predict(X) if rd == 0: net.export('ssd') countt = time.time() - countt print("SPF: %3.2f" % countt) utils.set_figsize((5, 5)) display(frame / 255, output, threshold=0.8) plt.show() rd += 1