# # A handy DataLoader would be very convenient for us to apply different transforms and aggregate data into mini-batches. # # Because Faster-RCNN handles raw images with various aspect ratios and various shapes, we provide a # :py:class:`gluoncv.data.batchify.Append`, which neither stack or pad images, but instead return lists. # In such way, image tensors and labels returned have their own shapes, unaware of the rest in the same batch. from gluoncv.data.batchify import Tuple, Append from mxnet.gluon.data import DataLoader batch_size = 2 # for tutorial, we use smaller batch-size num_workers = 0 # you can make it larger(if your CPU has more cores) to accelerate data loading # behavior of batchify_fn: stack images, and pad labels batchify_fn = Tuple(Append(), Append()) train_loader = DataLoader(train_dataset.transform(train_transform), batch_size, shuffle=True, batchify_fn=batchify_fn, last_batch='rollover', num_workers=num_workers) val_loader = DataLoader(val_dataset.transform(val_transform), batch_size, shuffle=False, batchify_fn=batchify_fn, last_batch='keep', num_workers=num_workers) for ib, batch in enumerate(train_loader): if ib > 3: break
# # A handy DataLoader would be very convenient for us to apply different transforms and aggregate data into mini-batches. # # Because Faster-RCNN handles raw images with various aspect ratios and various shapes, we provide a # :py:class:`gluoncv.data.batchify.Append`, which neither stack or pad images, but instead return lists. # In such way, image tensors and labels returned have their own shapes, unaware of the rest in the same batch. from gluoncv.data.batchify import Tuple, Append from mxnet.gluon.data import DataLoader batch_size = 2 # for tutorial, we use smaller batch-size num_workers = 0 # you can make it larger(if your CPU has more cores) to accelerate data loading # behavior of batchify_fn: stack images, and pad labels batchify_fn = Tuple(Append(), Append()) train_loader = DataLoader(train_dataset.transform(train_transform), batch_size, shuffle=True, batchify_fn=batchify_fn, last_batch='rollover', num_workers=num_workers) val_loader = DataLoader(val_dataset.transform(val_transform), batch_size, shuffle=False, batchify_fn=batchify_fn, last_batch='keep', num_workers=num_workers) for ib, batch in enumerate(train_loader): if ib > 3: break print('data 0:', batch[0][0].shape, 'label 0:', batch[1][0].shape) print('data 1:', batch[0][1].shape, 'label 1:', batch[1][1].shape) ########################################################## # Faster-RCNN Network # ------------------- # GluonCV's Faster-RCNN implementation is a composite Gluon HybridBlock :py:class:`gluoncv.model_zoo.FasterRCNN`. # In terms of structure, Faster-RCNN networks are composed of base feature extraction
# To deal with this problem, GluonCV provides :py:class:`gluoncv.data.batchify.Pad`, # which handles padding automatically. # :py:class:`gluoncv.data.batchify.Stack` in addition, is used to stack NDArrays with consistent shapes. # :py:class:`gluoncv.data.batchify.Tuple` is used to handle different behaviors across multiple outputs from transform functions. from gluoncv.data.batchify import Tuple, Stack, Pad from mxnet.gluon.data import DataLoader batch_size = 2 # for tutorial, we use smaller batch-size # you can make it larger(if your CPU has more cores) to accelerate data loading num_workers = 0 # behavior of batchify_fn: stack images, and pad labels batchify_fn = Tuple(Stack(), Pad(pad_val=-1)) train_loader = DataLoader( train_dataset.transform(train_transform), batch_size, shuffle=True, batchify_fn=batchify_fn, last_batch='rollover', num_workers=num_workers) val_loader = DataLoader( val_dataset.transform(val_transform), batch_size, shuffle=False, batchify_fn=batchify_fn, last_batch='keep', num_workers=num_workers) for ib, batch in enumerate(train_loader): if ib > 3:
# Because the number of objects varys a lot across images, we also have # varying label sizes. As a result, we need to pad those labels to the same size. # To deal with this problem, GluonCV provides :py:class:`gluoncv.data.batchify.Pad`, # which handles padding automatically. # :py:class:`gluoncv.data.batchify.Stack` in addition, is used to stack NDArrays with consistent shapes. # :py:class:`gluoncv.data.batchify.Tuple` is used to handle different behaviors across multiple outputs from transform functions. from gluoncv.data.batchify import Tuple, Stack, Pad from mxnet.gluon.data import DataLoader batch_size = 2 # for tutorial, we use smaller batch-size num_workers = 0 # you can make it larger(if your CPU has more cores) to accelerate data loading # behavior of batchify_fn: stack images, and pad labels batchify_fn = Tuple(Stack(), Pad(pad_val=-1)) train_loader = DataLoader(train_dataset.transform(train_transform), batch_size, shuffle=True, batchify_fn=batchify_fn, last_batch='rollover', num_workers=num_workers) val_loader = DataLoader(val_dataset.transform(val_transform), batch_size, shuffle=False, batchify_fn=batchify_fn, last_batch='keep', num_workers=num_workers) for ib, batch in enumerate(train_loader): if ib > 3: break print('data:', batch[0].shape, 'label:', batch[1].shape) ########################################################## # SSD Network # ------------------ # GluonCV's SSD implementation is a composite Gluon HybridBlock # (which means it can be exported # to symbol to run in C++, Scala and other language bindings.
width = 512 height = 512 batch_size = 16 maxTrain_inOneEpoch = 100 epoch_num = 1 lambd = 1 / 6 Models_tmp_Dir = 'F:/Temps/Models_tmp/' CPU_percentage = 0.3 #使用训练元件 #ctx = [mx.gpu()] ctx = [mx.gpu(i) for i in range(mx.context.num_gpus())] val_transform = presets.ssd.SSDDefaultValTransform(width, height) val_loader = DetectionDataLoader(val_dataset.transform(val_transform), batch_size, shuffle=False, last_batch='keep', num_workers=0) #%% 网络 classes = data.VOCDetection.CLASSES name = 'resnet50_v1' base_size = 512 features = ['stage3_activation5', 'stage4_activation2'] filters = [512, 512, 256, 256] sizes = [51.2, 102.4, 189.4, 276.4, 363.52, 450.6, 492] ratios = [[1, 2, 0.5]] + [[1, 2, 0.5, 3, 1.0 / 3]] * 3 + [[1, 2, 0.5]] * 2 steps = [16, 32, 64, 128, 256, 512]