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

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])
主站蜘蛛池模板: 吴川市| 贡觉县| 沭阳县| 弋阳县| 锦州市| 青铜峡市| 米脂县| 获嘉县| 宁海县| 平远县| 阜宁县| 曲松县| 苍山县| 洛南县| 岢岚县| 游戏| 泰兴市| 洪洞县| 科尔| 芮城县| 牡丹江市| 湘乡市| 天峨县| 延安市| 思茅市| 宁夏| 淳化县| 和田市| 白玉县| 平度市| 福鼎市| 英德市| 宁阳县| 景泰县| 西乌珠穆沁旗| 凌海市| 阜平县| 当阳市| 岳阳市| 罗平县| 邹平县|