Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added preprocess and reshape image dataset function #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore

This file was deleted.

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ python run_projector.py project-generated-images --network=gdrive:networks/style
python run_projector.py project-real-images --network=gdrive:networks/stylegan2-car-config-f.pkl \
--dataset=car --data-dir=~/datasets
```
## Resizing images to be of uniform size
The StyleGan2 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

Expand Down
62 changes: 62 additions & 0 deletions preprocess_images.py
Original file line number Diff line number Diff line change
@@ -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)