From f444c415648912116b861f2c355490dcf53f813d Mon Sep 17 00:00:00 2001 From: Thirunayan Date: Sat, 12 Sep 2020 20:14:30 +0530 Subject: [PATCH 1/4] Added image resizing feature --- preprocess_images.py | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 preprocess_images.py diff --git a/preprocess_images.py b/preprocess_images.py new file mode 100644 index 000000000..e805b79a4 --- /dev/null +++ b/preprocess_images.py @@ -0,0 +1,62 @@ +import numpy as np +import sys +import tensorflow as tf +from tensorflow.keras.preprocessing.image import ImageDataGenerator +import pandas as pd +import matplotlib.pyplot as plt +import PIL +from PIL import Image +import os +import scipy +from scipy import ndimage +import tqdm +from tqdm import tqdm + +def preprocess_images(image_dir,save_dir,image_size,image_channels=3,extension=".png"): + print("processing....") + training_data = [] + if(not os.path.exists(save_dir)): + os.mkdir(save_dir) + if(os.path.exists(image_dir)): + print(len(os.listdir(image_dir))," images to resized and processed") + print("\n Processing images....") + for index ,filename in enumerate(tqdm(os.listdir(image_dir))): + + path = os.path.join(image_dir,filename) + image = Image.open(path).resize((image_size,image_size),Image.ANTIALIAS) + image_arr = np.asarray(image) + if(image_arr.shape == (image_size,image_size,image_channels)): + training_data.append(image_arr) + im = Image.fromarray(image_arr) + im.save(save_dir+"/image_"+str(index)+extension) + print(training_data[0].shape) + training_data = np.reshape(training_data,(-1,image_size,image_size,image_channels)) + training_data = training_data.astype(np.float32) + print("Successfully processed and reshaped images") + else: + print("Image data directory does not exist") + print("\nPlease check if specified training data directory path is correct") + return None + + +try : + image_dir = sys.argv[1] #Relative path of image directory + save_dir = sys.argv[2] # Relative path of save directory + image_shape = int(sys.argv[3]) #image shape an integer + + + print("Image source directory : ",os.path.join(os.getcwd(),image_dir)) + print("Saving PRocessed images in : ",os.path.join(os.getcwd()),save_dir) + print(f"Resized image shape : ({image_shape},{image_shape},{3})") +except IndexError as error: + print("Error") + if(len(sys.argv) == 0): + print("No parameters are specified") + elif(len(sys.argv) == 1): + print("Save directory , image shape and image_channel parameters are not specified") + elif(len(sys.argv) == 2): + print("image shape and image channel parameters not specified") + elif(len(sys.argv) == 3): + print("image channel parameter not specified") + +preprocess_images(image_dir=image_dir,save_dir=save_dir,image_size=image_shape) From 28f2973e048d3fda04767676e5927561fd730958 Mon Sep 17 00:00:00 2001 From: Thirunayan Date: Sat, 12 Sep 2020 20:30:37 +0530 Subject: [PATCH 2/4] Updated readme to include changes for image resizing feature --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index a86a64a60..422f1cc74 100755 --- a/README.md +++ b/README.md @@ -189,7 +189,12 @@ To obtain other datasets, including LSUN, please consult their corresponding pro > python dataset_tool.py create_cifar10 datasets/cifar10 ~/cifar10 > python dataset_tool.py create_from_images datasets/custom-dataset ~/custom-images ``` +## Resizing images to be of uniform size +The stylegan model requires all images in the dataset to be of uniform size , when using a custom dataset this may cause a frequent error because all the images in the dataset may not be of the same shape. To resize all images in your dataset to be of uniform shape execute the below command in your terminal or console. The arguments needed to be passed are , image_dataset_directory , image save directory , image_size. The image size should be a single integer such as 128,256,512 and in addition the number of channels are by default set to 3. +``` +> python preprocess_image.py image_dataset_directory/ resized_image_save_directory/ 256 +``` ## Training networks Once the datasets are set up, you can train your own StyleGAN networks as follows: From fcebb29f0eb7e069d0d2adb7ae7c722072f6578e Mon Sep 17 00:00:00 2001 From: Thirunayan Date: Sat, 12 Sep 2020 20:47:39 +0530 Subject: [PATCH 3/4] Fixed typos in messages --- preprocess_images.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/preprocess_images.py b/preprocess_images.py index e805b79a4..ab61fd2c7 100644 --- a/preprocess_images.py +++ b/preprocess_images.py @@ -46,7 +46,7 @@ def preprocess_images(image_dir,save_dir,image_size,image_channels=3,extension=" print("Image source directory : ",os.path.join(os.getcwd(),image_dir)) - print("Saving PRocessed images in : ",os.path.join(os.getcwd()),save_dir) + print("Saving processed images in : ",os.path.join(os.getcwd()),save_dir) print(f"Resized image shape : ({image_shape},{image_shape},{3})") except IndexError as error: print("Error") From 21d57aaf6d8d2b47cce14759ff7ed6299af5f08f Mon Sep 17 00:00:00 2001 From: Thirunayan Date: Sat, 12 Sep 2020 21:12:49 +0530 Subject: [PATCH 4/4] Added .gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..103039789 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +save_dir/ +image_dir/ +sample.py +.vscode/