官术网_书友最值得收藏!

4-D tensors

One common example for four-dimensional tensor types is a batch of images. Modern CPUs and GPUs are optimized to perform the same operations on multiple examples faster. So, they take a similar time to process one image or a batch of images. So, it is common to use a batch of examples rather than use a single image at a time. Choosing the batch size is not straightforward; it depends on several factors. One major restriction for using a bigger batch or the complete dataset is GPU memory limitations—16, 32, and 64 are commonly used batch sizes.

Let's look at an example where we load a batch of cat images of size 64 x 224 x 224 x 3 where 64 represents the batch size or the number of images, 244 represents height and width, and 3 represents channels:

#Read cat images from disk
cats = glob(data_path+'*.jpg')
#Convert images into numpy arrays
cat_imgs = np.array([np.array(Image.open(cat).resize((224,224))) for cat in cats[:64]])
cat_imgs = cat_imgs.reshape(-1,224,224,3)
cat_tensors = torch.from_numpy(cat_imgs)
cat_tensors.size()

Output - torch.Size([64, 224, 224, 3])
主站蜘蛛池模板: 喀喇| 南安市| 南昌县| 兴山县| 黄梅县| 巴塘县| 龙州县| 调兵山市| 历史| 琼海市| 海伦市| 延边| 通城县| 新竹市| 中方县| 彩票| 安福县| 镇雄县| 广丰县| 乐昌市| 双牌县| 博客| 禄劝| 石楼县| 武宣县| 娱乐| 酉阳| 措勤县| 永年县| 襄垣县| 龙川县| 诸暨市| 建德市| 嘉定区| 上栗县| 高雄县| 东方市| 晋江市| 葫芦岛市| 镇安县| 周口市|