def semilogy(x_vals, y_vals, x_label, y_label, x2_vals=None, y2_vals=None, legend=None, figsize=(3.5, 2.5)): d2l.set_figsize(figsize) d2l.plt.xlabel(x_label) d2l.plt.ylabel(y_label) d2l.plt.semilogy(x_vals, y_vals) if x2_vals and y2_vals: d2l.plt.semilogy(x2_vals, y2_vals, linestyle=':') d2l.plt.legend(legend)
from d2l import d2lzh as d2l import mxnet as mx from mxnet import autograd, gluon, image, init, nd from mxnet.gluon import data as gdata, loss as gloss, utils as gutils import sys import time d2l.set_figsize() img = image.imread('./d2l/img/cat1.jpg') print(img.asnumpy()) d2l.plt.imshow(img.asnumpy()) def show_images(imgs, num_rows, num_cols, scale=2): figsize = (num_cols * scale, num_rows * scale) _, axes = d2l.plt.subplots(num_rows, num_cols, figsize=figsize) for i in range(num_rows): for j in range(num_cols): axes[i][j].imshow(imgs[i * num_cols + j].asnumpy()) axes[i][j].axes.get_xaxis().set_visible(False) axes[i][j].axes.get_yaxis().set_visible(False) return axes def apply(img, aug, num_rows=2, num_cols=4, scale=1.5): Y = [aug(img) for _ in range(num_rows * num_cols)] show_images(Y, num_rows, num_cols, scale) #翻转和裁剪 apply(img, gdata.vision.transforms.RandomFlipLeftRight())
img = image.imread('./d2l/img/pikachu.jpg') feature = image.imresize(img, 256, 256).astype('float32') X = feature.transpose((2, 0, 1)).expand_dims(axis=0) def predict(X): anchors, cls_preds, bbox_preds = net(X.as_in_context(ctx)) 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] return output[0, idx] output = predict(X) d2l.set_figsize((5, 5)) def display(img, output, threshold): fig = d2l.plt.imshow(img.asnumpy()) for row in output: score = row[1].asscalar() if score < threshold: continue h, w = img.shape[0:2] bbox = [row[2:6] * nd.array((w, h, w, h), ctx=row.context)] d2l.show_bboxes(fig.axes, bbox, '%.2f' % score, 'w') display(img, output, threshold=0.3)