[PYTHON] How to convert (32,32,3) to 4D tensor (1,32,32,1) with ndarray type

The beginning of the matter

In python's ndarray type, it became necessary to convert (32,32,3) to a 4D tensor (1,32,32,1). The purpose is for machine learning data.

4D tensor

An array of type ndarray such as (1,32,32,1) is called a "four-dimensional tensor". The content of the image that can be read from this 4D tensor is (number of images, height of image, width of image, number of channels of image). The number of channels in the image is 1 for grayscale and 3 for color for rgb. One image is represented by an array of ndarray type (32,32,3), and it can be judged that it is not an image dataset.

Addendum) When I tell a person who specializes in mathematics that it is a 4D tensor, it seems to be conveyed in a different image, but I like the way it says, "If you do not use a 4D tensor dataset, you cannot use it in Keras." I use it a lot (laughs)

I want to convert an ndarray to a 4D tensor

I think it's quite difficult to convert the ndarray type as you want. For the time being, I confirmed that the ndarray type can be converted as follows.

import numpy as np

a = np.arange(6)
a = a.reshape(2, 3)
print(a)
#↓ Output result
#[[0 1 2]
# [3 4 5]]
print("===============\n")

a = a.reshape(2,3,1)
print(a)
#↓ Output result
#[[[0]
#  [1]
#  [2]]
#
# [[3]
#  [4]
#  [5]]]
print("---------------\n")
a = a.reshape(1,2,3,1)
print(a)
#↓ Output result
#[[[[0]
#   [1]
#   [2]]
#
#  [[3]
#   [4]
#   [5]]]]

Now it looks like we can put it in the following predict function. y_pred = model.predict(x) If you do not enter the data of (1, 32, 16, 1) in ndarray type for x, an error will occur. An error will occur even with (32, 16, 1).

code

from PIL import Image
import numpy as np

# 3 *Where 2 is actually 32*Please replace it with 32 or something.
c = np.arange(3 * 2)
c = c.reshape(3, 2)

pilImg = Image.fromarray(np.uint8(c))
# pilImg_1 = pilImg.convert("RGB")
pilImg_1 = pilImg.convert("L")
data = np.array(pilImg_1, dtype='int64')
print(type(data))
print(data)
print(data.shape)

a = data
print("===============\n")

a = a.reshape(3,2,1)
print(a)

print("===============\n")

a = data.reshape(1,3,2,1)
print(a)

How to convert (32,32,3) to (32,32) with ndarray type

It's a bonus. It is used when changing the image of rgb to grayscale and using it. I don't know how much demand there is.

from PIL import Image
import numpy as np


file = "neko.png "
image = Image.open(file)
image = image.convert("RGB")
data_rgb = np.array(image, dtype='int64')          

#Because it is rgb(height, width, 3)Will be an array
print(type(data_rgb))
print("data_rgb ... " + str(data_rgb.shape))

pilImg_rgb = Image.fromarray(np.uint8(data_rgb))
pilImg_gray = pilImg_rgb.convert("L")
data_gray = np.array(pilImg_gray, dtype='int64') 

#Because it is grayscale(height, width)Will be an array
print(type(data_gray))
print("data_gray ... " + str(data_gray.shape))

# 
pilImg_rgb_2 = Image.fromarray(np.uint8(data_gray))
pilImg_rgb_2 = pilImg_rgb_2.convert("RGB")
data_rgb_2 = np.array(pilImg_rgb_2, dtype='int64') 

#I converted it to rgb again, so(height, width, 3)Will be an array
print(type(data_rgb_2))
print("data_rgb ... " + str(data_rgb_2.shape))

So, when doing (height, width) ⇔ (height, width, 3), it was an example of doing this. It will be an array of (height, width) instead of (height, width, 1).

Read the image and convert it to (1, height, width, number of image channels)

P.S. It was badly written. After all, I think the code below is enough.

from PIL import Image
import numpy as np


file = "neko.png "
image = Image.open(file)
image = image.convert("RGB")
data_rgb = np.array(image, dtype='int64')          


#Because it is rgb(height, width, 3)Will be an array
print(type(data_rgb))
print("data_rgb ... " + str(data_rgb.shape))

pilImg_rgb = Image.fromarray(np.uint8(data_rgb))
pilImg_gray = pilImg_rgb.convert("L")
data_gray = np.array(pilImg_gray, dtype='int64') 

#Because it is grayscale(height, width)Will be an array
print(type(data_gray))
print("data_gray ... " + str(data_gray.shape))

#Because it is grayscale(height, width)Will be an array
print(type(data_gray))
print("data_gray ... " + str(data_gray.shape))

a = data_gray.reshape(1, image.height, image.width, 1)
print(a.shape)

#Execution result
# <class 'numpy.ndarray'>
# data_rgb ... (210, 160, 3)
# <class 'numpy.ndarray'>
# data_gray ... (210, 160)
# (1, 210, 160, 1)

It is the same as (1,32,32,1) because it has an array of (1, 210, 160, 1). Now you can use it when predicting machine learning. However, it seems that you usually use a color image, so the end is 3 instead of 1. If you want to learn letters etc., grayscale is fine, so I think you can use the sample in this article.

Addendum) Now I am doing well and working.

Convert an array of (1, 32, 32, 3) to (32, 32, 3)

print("img ... " + str(img.shape))
# img ... (1, 32, 32, 3)
print("img ..." + str(img[0].shape))
# img ... (32, 32, 3)

imwrite(img_path, img)
#↑ This is an error
imwrite(img_path, img[0])
#↑ This is a success

Recommended Posts

How to convert (32,32,3) to 4D tensor (1,32,32,1) with ndarray type
[Python] How to convert a 2D list to a 1D list
Convert 202003 to 2020-03 with pandas
How to convert / restore a string with [] in python
How to convert Python # type for Python super beginners: str
0 Convert unfilled date to datetime type with regular expression
How to convert horizontally held data to vertically held data with pandas
How to convert a class object to a dictionary with SQLAlchemy
How to convert JSON file to CSV file with Python Pandas
[Python] How to create a 2D histogram with Matplotlib
How to update with SQLAlchemy?
How to Alter with SQLAlchemy?
How to separate strings with','
How to Delete with SQLAlchemy?
How to convert an array to a dictionary with Python [Application]
How to convert Python # type for Python super beginners: int, float
Convert .ipynb to .html (with BatchFile)
How to cancel RT with tweepy
Python: How to use async with
How to use virtualenv with PowerShell
How to deal with imbalanced data
Convert list to DataFrame with python
Convert sentences to vectors with gensim
How to convert 0.5 to 1056964608 in one shot
How to get started with Scrapy
How to deal with DistributionNotFound errors
How to get started with Django
How to Data Augmentation with PyTorch
How to use FTP with Python
How to calculate date with python
How to convert from .mgz to .nii.gz
How to install mysql-connector with pip3
Convert PDF to image with ImageMagick
How to INNER JOIN with SQLAlchemy
How to install Anaconda with pyenv
How to authenticate with Django Part 2
How to authenticate with Django Part 3
numpy: I want to convert a single type ndarray to a structured array
How to display legend marks in one with Python 2D plot
How to do arithmetic with Django template
[Blender] How to set shape_key with script
[Python] How to convert db file to csv
How to title multiple figures with matplotlib
Convert memo at once with Python 2to3
How to get parent id with sqlalchemy
[Django] Convert QuerySet to dictionary type list
How to convert csv to tsv in CLI
Convert from PDF to CSV with pdfplumber
How to install DLIB with 2020 / CUDA enabled
How to use ManyToManyField with Django's Admin
How to use OpenVPN with Ubuntu 18.04.3 LTS
How to use Cmder with PyCharm (Windows)
Convert character strings to features with RoBERTa
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
How to prevent package updates with apt
How to work with BigQuery in Python
How to use Ass / Alembic with HtoA
Convert FX 1-minute data to 5-minute data with Python
How to deal with enum compatibility errors
How to use Japanese with NLTK plot