Python Tensorflow creating a dataset from real images

Viewed 315

I'm trying to build a GAN model which can draw a portrait, I can do it with mnist dataset and I want to change my dataset to target person's portrait. I had only several example images so I tried to expand my data with ImageDataGenerator library which I added the code below. My first problem is I'm not sure if it's the right way and here's my second question: This is the code that I've used for create data:

import numpy as np
import pandas as pd
import seaborn as sns 
import matplotlib.pyplot as plt
from matplotlib.image import imread
import matplotlib.image as mpimg
import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import random
img_dir = "C:\\Users\\Casper\\Desktop\\draw_ata\\atam\\"
image_shape = (150,150,3)
if not os.path.exists('pic_data'):
    os.makedirs('pic_data')
for i in range(60000):
    random_img_path = img_dir + random.choice(os.listdir(img_dir))
    image_gen = ImageDataGenerator(rotation_range=10,width_shift_range=0.1,height_shift_range=0.1,rescale=1/255, shear_range=0.05, zoom_range=0.1,horizontal_flip=True,fill_mode="nearest")
    img = imread(random_img_path, 0)
    new_img = image_gen.random_transform(img)

    mpimg.imsave("pic_data/%d.png" % i, new_img)

It has saved the images successfully, but now my next goal was getting the proper shape which I can use for GAN model. My photos were grayscaled but somehow their shapes were 3D just like they are colored images. I didn't pay attention to this problem and tried to move on. I wanted to create a np array just like the mnist dataset. And here is my code to do it:

from tensorflow.keras.layers import Input,SimpleRNN,GRU,LSTM,Dense,Flatten,GlobalMaxPooling1D,Embedding, Dropout, LeakyReLU, BatchNormalization
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.optimizers import SGD, Adam
from tensorflow.keras.datasets import mnist
from sklearn.preprocessing import StandardScaler
import numpy as np
import pandas as pd
from matplotlib.image import imread
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from sklearn.model_selection import train_test_split
import os
img_dir = "C:\\Users\\Casper\\Desktop\\draw_ata\\pic_data\\"
data_train = os.listdir(img_dir)[:10000]
data_test = os.listdir(img_dir)[50000:]

for i in range(len(data_train)):
    data_train[i] = imread(img_dir + data_train[i])
data_train = np.array(data_train)
print(data_train.shape)

and output was (10000,) but I want their shapes like (10000,150,150) -my photo scales are 150x150- just as mnist dataset. How can I set them to trainable dataset, thanks

EDITOK I solved it with the help of opencv library

1 Answers

Since you have mentioned that you solved the issue using OpenCV, I'm writing the solution below using OpenCV for the community.

from matplotlib.image import imread
import os
import cv2

path = "path"

data_train = os.listdir(path)

for i in range(len(data_train)):
  data_train[i] = cv2.resize(cv2.imread(path + data_train[i]), (150, 150))
data_train = np.array(data_train)
Related