onexpeters
onexpeters

Reputation: 141

How to visualize CIFAR10 images as matrices

I am currently trying to work with CIFAR10 images. I have the following snippet

import tensorflow as tf
from tensorflow.keras import datasets,layers,models
import matplotlib.pyplot as plt
(train_images,train_labels),(test_images,test_labels)=datasets.cifar10.load_data()
#train_images,test_images=train_images/,test_images

when I print print(train_images[0]) I get 32*32*3 matrix, when I print print(train_images[0][0) I get 32*3 matrix, however I thought it should be 32*32 matrix. How does slicing work with this image, which dimension come first. Any insight and recommendation on reading material will be highly appreciated

Upvotes: 0

Views: 430

Answers (1)

Pogger
Pogger

Reputation: 382

train_images variable have batch of images and images are numpy metrics and slicing works same for all metrics in numpy.

Dimensions comes as [batch, rows, columns, channels].

To get first image you will print: print(train_images[0].shape) and it will output (32, 32, 3).

To get first channel of image you will print: print(train_images[0, :, :, 0]) and it will output (32, 32) first channel and so on print(train_images[0, :, :, 1]) for second channel, print(train_images[0, :, :, 2]) for third channel. Where ':' implies all values.

train_images[0, 0] will output values from first row of first image from batch (32, 3)

More on: basics indexing,arrays indexing

Upvotes: 1

Related Questions