-
Notifications
You must be signed in to change notification settings - Fork 0
/
afhq_tl.py
66 lines (53 loc) · 2.17 KB
/
afhq_tl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import numpy as np
from PIL import Image
from tensorflow.keras.utils import img_to_array
import matplotlib.pyplot as plt
import cv2
SIZE=160
def convert_images_to_bw(folder_path):
image_array = []
for filename in os.listdir(folder_path):
if filename.endswith('.jpg') or filename.endswith('.png'): # Filter image files
image_path = os.path.join(folder_path, filename)
# Open image and convert to black and white
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img, (SIZE, SIZE))
img = img.astype('float32') / 255.0
# Convert image to numpy array
image_array.append(img_to_array(img))
return image_array
def read_color(folder_path):
image_array = []
for filename in os.listdir(folder_path):
if filename.endswith('.jpg') or filename.endswith('.png'): # Filter image files
image_path = os.path.join(folder_path, filename)
# Open image and convert to black and white
img = cv2.imread(image_path)
# Convert image to numpy array
img = cv2.resize(img, (SIZE, SIZE))
img = img.astype('float32') / 255.0
image_array.append(img_to_array(img))
return image_array
# Convert images to black and white and save as array
train_gray_image = convert_images_to_bw('afhq/combine')
train_color_image = read_color('afhq/combine')
print("Trainging data loaded")
from sklearn.utils import resample
train_gray_image, train_color_image = resample(train_gray_image, train_color_image)
test_color_image = read_color('afhq/combine_val')
test_gray_image = convert_images_to_bw('afhq/combine_val')
test_color_image, test_gray_image = resample(test_color_image, test_gray_image)
print("Testing data loaded")
for i in range(45, 48):
color = train_color_image[i]
grayscale = train_gray_image[i]
plt.figure(figsize=(15, 15))
plt.subplot(1, 3, 1)
plt.title('Color Image', color='green', fontsize=20)
plt.imshow(color)
plt.subplot(1, 3, 2)
plt.title('Grayscale Image ', color='black', fontsize=20)
plt.imshow(grayscale)
plt.show()