Creating multiple subplots to display numpy images:
def create_subplots (images , labels ):
plt .subplot ()
for _ in range (0 , len (images )):
plt .rcParams ["figure.figsize" ] = [10 ,20 ]
plt .subplot (len (images )/ 2 ,len (images )/ 5 , _ + 1 )
plt .xticks ([]), plt .yticks ([])
plt .imshow (npimg , cmap = 'gray' )
plt .title ("Label:{}" .format (classes [labels [_ ]]))
plt .tight_layout ()
Pytorch specific implementation (where we get Pytorch Tensors as inputs, needs to be checked for color images):
def imshow (images , labels , channels = 1 ):
"""For single channel inputs
"""
if channels == 1 :
plt .subplot ()
for _ in range (0 , len (images )):
img = images [_ ]/ 2 + 0.5
npimg = img .numpy ()
npimg = npimg .reshape (npimg .shape [1 ],npimg .shape [2 ])
plt .rcParams ["figure.figsize" ] = [10 ,20 ]
plt .subplot (len (images )/ 2 ,len (images )/ 5 , _ + 1 )
plt .xticks ([]), plt .yticks ([])
plt .imshow (npimg , cmap = 'gray' )
plt .title ("Label:{}" .format (classes [labels [_ ]]))
plt .tight_layout ()
elif channels == 0 :
img = img / 2 + 0.5 # unnormalize
npimg = img .numpy ()
plt .imshow (np .transpose (npimg , (1 , 2 , 0 )))
print ("Label:{}" .format (classes [labels [0 ]]))