def predict(net,test_imgs,ctx,save_fig_path): test_iter=load_img_batch(test_imgs,64,'test') num_imgs=len(test_imgs) #从测试集中猫、狗各取三个样本,用于实测 index_cat=np.random.randint(0,num_imgs/2,size=3) index_dog=np.random.randint(num_imgs/2,num_imgs,size=3) index=np.append(index_cat,index_dog) np.random.shuffle(index) pred_list=evaluate_accuracy(test_iter,net,ctx,ispred=True) pred_label=[pred_list[i] for i in index] true_label=[test_imgs[i][1] for i in index] print('cat: 0, dog: 1') print('true label:') print(np.reshape(true_label,(2,-1))) print('predict label:') print(np.reshape(pred_label,(2,-1))) show_img=[test_imgs[i][0] for i in index] d2l.show_images(show_img,2,3,scale=1) plt.savefig(save_fig_path+'\\cat_dog.png') plt.show() plt.close()
def pikaqiu(): batch_size, edge_size = 32, 256 train_iter, _ = load_data_pikachu(batch_size, edge_size) batch = train_iter.next() print(batch.data[0].shape, batch.label[0].shape) imgs = (batch.data[0][0:10].transpose((0, 2, 3, 1))) / 255 axes = d2l.show_images(imgs, 2, 5).flatten() for ax, label in zip(axes, batch.label[0][0:10]): d2l.show_bboxes(ax, [label[0][1:5] * edge_size], colors=['w'])
train_iter = image.ImageDeIter( path_imgrec=os.path.join(data_dir, "train.rec"), path_imgidx=os.path.join(data_dir, 'train.idx'), batch_size=batch_size, data_shape=(3, edge_size, edge_size), shuffle=True, rand_crop=1, min_object_covered=0.95, max_attempts=200) val_iter = image.ImageDetIter(path_imgrec=os.path.join(data_dir, 'val.rec'), batch_size=batch_size, data_shape=(3, edge_size, edge_size), suhffle=False) return train_iter, val_iter # In[ ]: batch_size, edge_size = 32, 256 train_iter, _ = load_data_pikachu(batch_size, edge_size) batch = train_iter.next() batch.data[0].shape, batch.label[0].shape # In[ ]: #画出10张图片和pikachu的bounding_box imgs = (batch.data[0][0:10].transpose((0, 2, 3, 1))) / 255 axes = d2l.show_images(imgs, 2, 5).flatten() for ax, label in zip (axes, batch.label[0][0:10]): d2l.show_bboxes(ax, [label[0][1:5] * edge_size], colors=['w'])
root, 'train.txt' if is_train else 'val.txt') with open(txt_fname, 'r') as f: images = f.read().split() features, labels = [None] * len(images), [None] * len(images) for i, fname in enumerate(images): features[i] = image.imread('%s/JPEGImages/%s.jpg' % (root, fname)) labels[i] = image.imread( '%s/SegmentationClass/%s.png' % (root, fname)) return features, labels train_features, train_labels = read_voc_images() n = 5 imgs = train_features[0:n] + train_labels[0:n] d2l.show_images(imgs, 2, n) # 该常量已保存在d2lzh包中方便以后使用 VOC_COLORMAP = [[0, 0, 0], [128, 0, 0], [0, 128, 0], [128, 128, 0], [0, 0, 128], [128, 0, 128], [0, 128, 128], [128, 128, 128], [64, 0, 0], [192, 0, 0], [64, 128, 0], [192, 128, 0], [64, 0, 128], [192, 0, 128], [64, 128, 128], [192, 128, 128], [0, 64, 0], [128, 64, 0], [0, 192, 0], [128, 192, 0], [0, 64, 128]] # 该常量已保存在d2lzh包中方便以后使用 VOC_CLASSES = ['background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'potted plant', 'sheep', 'sofa', 'train', 'tv/monitor'] colormap2label = nd.zeros(256 ** 3)
'wd': 1e-3 }) print("start train...\n") d2l.train(train_iter, test_iter, net, loss, trainer, ctx, num_epochs=1) # 5 print("end train...\n") #9.10.6-预测像素类别 def predict(img): X = test_iter._dataset.normalize_image(img) X = X.transpose((2, 0, 1)).expand_dims(axis=0) pred = nd.argmax(net(X.as_in_context(ctx[0])), axis=1) return pred.reshape((pred.shape[1], pred.shape[2])) #可视化像素的预测类别,映射回数据集中的标注颜色 def label2image(pred): colormap = nd.array(d2l.VOC_COLORMAP, ctx=ctx[0], dtype='uint8') X = pred.astype('int32') return colormap[X, :] test_images, test_labels = d2l.read_voc_images(is_train=False) n, imgs = 4, [] for i in range(n): crop_rect = (0, 0, 480, 320) X = image.fixed_crop(test_images[i], *crop_rect) pred = label2image(predict(X)) imgs += [X, pred, image.fixed_crop(test_labels[i], *crop_rect)] d2l.show_images(imgs[::3] + imgs[1::3] + imgs[2::3], 3, n)
data_dir = '../data' base_url = 'https://apache-mxnet.s3-accelerate.amazonaws.com/' fname = gutils.download(base_url + 'gluon/dataset/hotdog.zip', path=data_dir, sha1_hash='fba480ffa8aa7e0febbb511d181409f899b9baa5') with zipfile.ZipFile(fname, 'r') as z: z.extractall(data_dir) train_imgs = gdata.vision.ImageFolderDataset( os.path.join(data_dir, 'hotdog/train')) test_imgs = gdata.vision.ImageFolderDataset( os.path.join(data_dir, 'hotdog/test')) hotdogs = [train_imgs[i][0] for i in range(8)] not_hotdogs = [train_imgs[-i - 1][0] for i in range(8)] d2l.show_images(hotdogs + not_hotdogs, 2, 8, scale=1.4) normalize = gdata.vision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) train_augs = gdata.vision.transforms.Compose([ gdata.vision.transforms.RandomResizedCrop(224), gdata.vision.transforms.RandomFlipLeftRight(), gdata.vision.transforms.ToTensor(), normalize ]) test_augs = gdata.vision.transforms.Compose([ gdata.vision.transforms.Resize(256), gdata.vision.transforms.CenterCrop(224), gdata.vision.transforms.ToTensor(), normalize ])
import d2lzh as d2l from mxnet import image from matplotlib import pyplot as plt batch_size=32 img_size=512 train_iter=image.ImageDetIter(path_imgrec='D:\d2l-zh20200904\data\pikachu\\train.rec', path_imgidx='D:\d2l-zh20200904\data\pikachu\\train.idx', batch_size=batch_size,data_shape=(3,img_size,img_size),shuffle=True,rand_crop=1, min_object_covered=0.95,max_attempts=200) test_iter=image.ImageDetIter(path_imgrec='D:\d2l-zh20200904\data\pikachu\\val.rec',batch_size=batch_size,data_shape=(3,img_size,img_size),shuffle=False) batch=train_iter.next() imgs=batch.data[0].transpose((0,2,3,1))/255 axes=d2l.show_images(imgs,4,8).flatten() # imgs = (batch.data[0][0:10].transpose((0, 2, 3, 1))) / 255 # axes = d2l.show_images(imgs, 2, 5).flatten() # for ax, label in zip(axes, batch.label[0][0:10]): # d2l.show_bboxes(ax, [label[0][1:5] * 256], colors=['w']) labels=batch.label[0] for ax,label in zip(axes,labels): d2l.show_bboxes(ax,[label[0][1:5]*img_size]) plt.show()