Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _test_model_list(model_list, ctx, x, pretrained=True, **kwargs):
pretrained_models = gcv.model_zoo.pretrained_model_list()
for model in model_list:
if model in pretrained_models:
net = gcv.model_zoo.get_model(model, pretrained=True, **kwargs)
else:
net = gcv.model_zoo.get_model(model, **kwargs)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
net.initialize()
net.collect_params().reset_ctx(ctx)
net(x)
mx.nd.waitall()
if i % 20 == 0:
print('[Epoch {}][Batch {}], Speed: {:.3f} samples/sec, {}={:.3f}, {}={:.3f}'.format(
epoch, i, batch_size/(time.time()-btic), name1, loss1, name2, loss2))
btic = time.time()
#############################################################################################
# Save finetuned weights to disk
net.save_parameters('ssd_512_mobilenet1.0_pikachu.params')
#############################################################################################
# Predict with finetuned model
# ----------------------------
# We can test the performance using finetuned weights
test_url = 'https://raw.githubusercontent.com/zackchase/mxnet-the-straight-dope/master/img/pikachu.jpg'
download(test_url, 'pikachu_test.jpg')
net = gcv.model_zoo.get_model('ssd_512_mobilenet1.0_custom', classes=classes, pretrained_base=False)
net.load_parameters('ssd_512_mobilenet1.0_pikachu.params')
x, image = gcv.data.transforms.presets.ssd.load_test('pikachu_test.jpg', 512)
cid, score, bbox = net(x)
ax = viz.plot_bbox(image, bbox[0], score[0], cid[0], class_names=classes)
plt.show()
def keypoint_detection(img_path, detector, pose_net):
x, img = data.transforms.presets.yolo.load_test(img_path, short=512)
class_IDs, scores, bounding_boxs = detector(x)
pose_input, upscale_bbox = detector_to_alpha_pose(img, class_IDs, scores, bounding_boxs)
predicted_heatmap = pose_net(pose_input)
pred_coords, confidence = heatmap_to_coord_alpha_pose(predicted_heatmap, upscale_bbox)
ax = plot_keypoints(img, pred_coords, confidence, class_IDs, bounding_boxs, scores,
box_thresh=0.5, keypoint_thresh=0.2)
plt.show()
if __name__ == '__main__':
detector = get_model(opt.detector, pretrained=True)
detector.reset_class(["person"], reuse_weights=['person'])
net = get_model(opt.pose_model, pretrained=True)
keypoint_detection(opt.input_pic, detector, net)
if __name__ == '__main__':
args = parse_args()
# training contexts
ctx = [mx.gpu(int(i)) for i in args.gpus.split(',') if i.strip()]
ctx = ctx if ctx else [mx.cpu()]
args.batch_size = len(ctx) # 1 batch per device
# network
net_name = '_'.join(('cascade_rfcn', args.network, args.dataset))
args.save_prefix += net_name
if args.pretrained.lower() in ['true', '1', 'yes', 't']:
net = gcv.model_zoo.get_model(net_name, pretrained=True)
else:
net = gcv.model_zoo.get_model(net_name, pretrained=False)
net.load_parameters(args.pretrained.strip())
net.collect_params().reset_ctx(ctx)
# training data
val_dataset, eval_metric = get_dataset(args.dataset, args)
val_data = get_dataloader(
net, val_dataset, args.batch_size, args.num_workers)
# validation
if not args.eval_all:
current_map = 0
for i in range(10):
iou_thresh = 0.5 + 0.05*i
eval_metric = VOC07MApMetric(iou_thresh=iou_thresh, class_names=val_dataset.classes)
names, values = validate(net, val_data, ctx, eval_metric, len(val_dataset))
for k, v in zip(names, values):
output_shape=(128, 96), ctx=ctx)
if len(upscale_bbox) > 0:
predicted_heatmap = pose_net(pose_input)
pred_coords, confidence = heatmap_to_coord(predicted_heatmap, upscale_bbox)
scale = 1.0 * img.shape[0] / scaled_img.shape[0]
img = cv_plot_keypoints(img.asnumpy(), pred_coords, confidence, class_IDs, bounding_boxs, scores,
box_thresh=1, keypoint_thresh=0.3, scale=scale)
return img
if __name__ == '__main__':
ctx = mx.cpu()
detector_name = "ssd_512_mobilenet1.0_coco"
detector = get_model(detector_name, pretrained=True, ctx=ctx)
detector.reset_class(classes=['person'], reuse_weights={'person':'person'})
net = get_model('simple_pose_resnet18_v1b', pretrained='ccd24037', ctx=ctx)
cap = cv2.VideoCapture(0)
time.sleep(1) ### letting the camera autofocus
for i in range(opt.num_frames):
ret, frame = cap.read()
frame = mx.nd.array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)).astype('uint8')
img = keypoint_detection(frame, detector, net, ctx=ctx)
cv_plot_image(img)
cv2.waitKey(1)
cap.release()
cv2.destroyAllWindows()
# Otherwise use synthetic data
image_shape = (3, 224, 224)
data_shape = (batch_size,) + image_shape
train_data = SyntheticDataIter(num_classes, data_shape, epoch_size,
np.float32, context)
val_data = None
# Get model from GluonCV model zoo
# https://gluon-cv.mxnet.io/model_zoo/index.html
kwargs = {'ctx': context,
'pretrained': args.use_pretrained,
'classes': num_classes}
if args.last_gamma:
kwargs['last_gamma'] = True
net = get_model(args.model, **kwargs)
net.cast(args.dtype)
# Create initializer
initializer = mx.init.Xavier(rnd_type='gaussian', factor_type="in",
magnitude=2)
def train_gluon():
def evaluate(epoch):
if not args.use_rec:
return
val_data.reset()
acc_top1 = mx.metric.Accuracy()
acc_top5 = mx.metric.TopKAccuracy(5)
for _, batch in enumerate(val_data):
from matplotlib import pyplot as plt
plt.imshow(img.asnumpy())
plt.show()
##############################################################################
# normalize the image using dataset mean
from gluoncv.data.transforms.presets.segmentation import test_transform
img = test_transform(img, ctx)
##############################################################################
# Load the pre-trained model and make prediction
# ----------------------------------------------
#
# get pre-trained model
model = gluoncv.model_zoo.get_model('fcn_resnet101_voc', pretrained=True)
##############################################################################
# make prediction using single scale
output = model.predict(img)
predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()
##############################################################################
# Add color pallete for visualization
from gluoncv.utils.viz import get_color_pallete
import matplotlib.image as mpimg
mask = get_color_pallete(predict, 'pascal_voc')
mask.save('output.png')
##############################################################################
# show the predicted mask
mmask = mpimg.imread('output.png')
from matplotlib import pyplot as plt
plt.imshow(img.asnumpy())
plt.show()
##############################################################################
# normalize the image using dataset mean
from gluoncv.data.transforms.presets.segmentation import test_transform
img = test_transform(img, ctx)
##############################################################################
# Load the pre-trained model and make prediction
# ----------------------------------------------
#
# get pre-trained model
model = gluoncv.model_zoo.get_model('psp_resnet101_ade', pretrained=True)
##############################################################################
# make prediction using single scale
output = model.predict(img)
predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()
##############################################################################
# Add color pallete for visualization
from gluoncv.utils.viz import get_color_pallete
import matplotlib.image as mpimg
mask = get_color_pallete(predict, 'ade20k')
mask.save('output.png')
##############################################################################
# show the predicted mask
mmask = mpimg.imread('output.png')
# In terms of structure, YOLOv3 networks are composed of base feature extraction
# network, convolutional transition layers, upsampling layers, and specially designed YOLOv3 output layers.
#
# We highly recommend you to read the original paper to learn more about the ideas
# behind YOLO [YOLOv3]_.
#
# `Gluon Model Zoo <../../model_zoo/index.html>`__ has a few built-in YOLO networks, more on the way.
# You can load your favorite one with one simple line of code:
#
# .. hint::
#
# To avoid downloading models in this tutorial, we set `pretrained_base=False`,
# in practice we usually want to load pre-trained imagenet models by setting
# `pretrained_base=True`.
from gluoncv import model_zoo
net = model_zoo.get_model('yolo3_darknet53_voc', pretrained_base=False)
print(net)
##############################################################################
# YOLOv3 network is callable with image tensor
import mxnet as mx
x = mx.nd.zeros(shape=(1, 3, 416, 416))
net.initialize()
cids, scores, bboxes = net(x)
##############################################################################
# YOLOv3 returns three values, where ``cids`` are the class labels,
# ``scores`` are confidence scores of each prediction,
# and ``bboxes`` are absolute coordinates of corresponding bounding boxes.
##########################################################
optimizer = 'nag'
else:
optimizer = 'sgd'
if opt.clip_grad > 0:
optimizer_params = {'learning_rate': opt.lr, 'wd': opt.wd, 'momentum': opt.momentum, 'clip_gradient': opt.clip_grad}
else:
optimizer_params = {'learning_rate': opt.lr, 'wd': opt.wd, 'momentum': opt.momentum}
if opt.dtype != 'float32':
optimizer_params['multi_precision'] = True
model_name = opt.model
if opt.use_pretrained and len(opt.hashtag) > 0:
opt.use_pretrained = opt.hashtag
net = get_model(name=model_name, nclass=classes, pretrained=opt.use_pretrained,
use_tsn=opt.use_tsn, num_segments=opt.num_segments, partial_bn=opt.partial_bn,
bn_frozen=opt.freeze_bn)
net.cast(opt.dtype)
net.collect_params().reset_ctx(context)
logger.info(net)
if opt.resume_params is not '':
net.load_parameters(opt.resume_params, ctx=context)
print('Continue training from model %s.' % (opt.resume_params))
if opt.kvstore is not None:
train_data, val_data, batch_fn = get_data_loader(opt, batch_size, num_workers, logger, kv)
else:
train_data, val_data, batch_fn = get_data_loader(opt, batch_size, num_workers, logger)
num_batches = len(train_data)