JUNGU Alican commited on
Commit
0103298
·
0 Parent(s):

Duplicate from Alican/pixera

Browse files

Co-authored-by: Akca <[email protected]>

.gitattributes ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.npy filter=lfs diff=lfs merge=lfs -text
13
+ *.npz filter=lfs diff=lfs merge=lfs -text
14
+ *.onnx filter=lfs diff=lfs merge=lfs -text
15
+ *.ot filter=lfs diff=lfs merge=lfs -text
16
+ *.parquet filter=lfs diff=lfs merge=lfs -text
17
+ *.pickle filter=lfs diff=lfs merge=lfs -text
18
+ *.pkl filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pt filter=lfs diff=lfs merge=lfs -text
21
+ *.pth filter=lfs diff=lfs merge=lfs -text
22
+ *.rar filter=lfs diff=lfs merge=lfs -text
23
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
24
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
25
+ *.tflite filter=lfs diff=lfs merge=lfs -text
26
+ *.tgz filter=lfs diff=lfs merge=lfs -text
27
+ *.wasm filter=lfs diff=lfs merge=lfs -text
28
+ *.xz filter=lfs diff=lfs merge=lfs -text
29
+ *.zip filter=lfs diff=lfs merge=lfs -text
30
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
31
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
32
+ methods/white_box_cartoonizer/saved_models/model-33999.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
33
+ examples/pixelArt/white_box_cartoonizer/saved_models/model-33999.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Pixera
3
+ emoji: 💻
4
+ colorFrom: gray
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: 3.1.1
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: Alican/pixera
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import torch
4
+ import warnings
5
+ import numpy as np
6
+ import gradio as gr
7
+ import paddlehub as hub
8
+ from PIL import Image
9
+ from methods.img2pixl import pixL
10
+ from examples.pixelArt.combine import combine
11
+ from methods.media import Media
12
+
13
+ warnings.filterwarnings("ignore")
14
+
15
+ U2Net = hub.Module(name='U2Net')
16
+ device = "cuda" if torch.cuda.is_available() else "cpu"
17
+ face2paint = torch.hub.load("bryandlee/animegan2-pytorch:main", "face2paint", device=device, size=512)
18
+ model = torch.hub.load("bryandlee/animegan2-pytorch", "generator", device=device).eval()
19
+
20
+
21
+ def initilize(media,pixel_size,checkbox1):
22
+ #Author: Alican Akca
23
+ if media.name.endswith('.gif'):
24
+ return Media().split(media.name,pixel_size, 'gif')
25
+ elif media.name.endswith('.mp4'):
26
+ return None #Media().split(media.name,pixel_size, "video")
27
+ else:
28
+ media = Image.open(media.name).convert("RGB")
29
+ media = cv2.cvtColor(np.asarray(face2paint(model, media)), cv2.COLOR_BGR2RGB)
30
+ if checkbox1:
31
+ result = U2Net.Segmentation(images=[media],
32
+ paths=None,
33
+ batch_size=1,
34
+ input_size=320,
35
+ output_dir='output',
36
+ visualization=True)
37
+ result = combine().combiner(images = pixL().toThePixL([result[0]['front'][:,:,::-1], result[0]['mask']],
38
+ pixel_size),
39
+ background_image = media)
40
+ else:
41
+ result = pixL().toThePixL([media], pixel_size)
42
+ result = Image.fromarray(result)
43
+ result.save('cache.png')
44
+ return [None, result, 'cache.png']
45
+
46
+ inputs = [gr.File(label="Media"),
47
+ gr.Slider(4, 100, value=12, step = 2, label="Pixel Size"),
48
+ gr.Checkbox(label="Object-Oriented Inference", value=False)]
49
+
50
+ outputs = [gr.Video(label="Pixed Media"),
51
+ gr.Image(label="Pixed Media"),
52
+ gr.File(label="Download")]
53
+
54
+ title = "Pixera: Create your own Pixel Art"
55
+ description = """Object-Oriented Inference is currently only available for images. Also, Video Processing has currently suspended."""
56
+
57
+ gr.Interface(fn = initilize,
58
+ inputs = inputs,
59
+ outputs = outputs,
60
+ title=title,
61
+ description=description).launch()
62
+
cache.gif ADDED
cache.mp4 ADDED
Binary file (6.99 kB). View file
 
examples/GANexample1.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
examples/pixelArt/__pycache__/combine.cpython-38.pyc ADDED
Binary file (1.27 kB). View file
 
examples/pixelArt/combine.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+
4
+ class combine:
5
+ #Author: Alican Akca
6
+ def __init__(self, size = (400,300),images = [],background_image = None):
7
+ self.size = size
8
+ self.images = images
9
+ self.background_image = background_image
10
+
11
+ def combiner(self,images,background_image):
12
+ original = images[0]
13
+ masked = images[1]
14
+ background = cv2.resize(background_image,(images[0].shape[1],images[0].shape[0]))
15
+ result = blend_images_using_mask(original, background, masked)
16
+ return result
17
+
18
+ def mix_pixel(pix_1, pix_2, perc):
19
+
20
+ return (perc/255 * pix_1) + ((255 - perc)/255 * pix_2)
21
+
22
+ def blend_images_using_mask(img_orig, img_for_overlay, img_mask):
23
+
24
+ if len(img_mask.shape) != 3:
25
+ img_mask = cv2.cvtColor(img_mask, cv2.COLOR_GRAY2BGR)
26
+
27
+ img_res = mix_pixel(img_orig, img_for_overlay, img_mask)
28
+
29
+ return cv2.cvtColor(img_res.astype(np.uint8), cv2.COLOR_BGR2RGB)
img/example_1.jpg ADDED
img/logo.jpg ADDED
img/method_1.png ADDED
methods/__pycache__/img2pixl.cpython-38.pyc ADDED
Binary file (2.38 kB). View file
 
methods/__pycache__/media.cpython-38.pyc ADDED
Binary file (1.33 kB). View file
 
methods/img2pixl.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ from PIL import Image
4
+
5
+ class pixL:
6
+ #Author: Alican Akca
7
+ def __init__(self,numOfSquaresW = None, numOfSquaresH= None, size = [False, (512,512)],square = 6,ImgH = None,ImgW = None,images = [],background_image = None):
8
+ self.images = images
9
+ self.size = size
10
+ self.ImgH = ImgH
11
+ self.ImgW = ImgW
12
+ self.square = square
13
+ self.numOfSquaresW = numOfSquaresW
14
+ self.numOfSquaresH = numOfSquaresH
15
+
16
+ def preprocess(self):
17
+ for image in self.images:
18
+
19
+ size = (image.shape[0] - (image.shape[0] % 4), image.shape[1] - (image.shape[1] % 4))
20
+ image = cv2.resize(image, size)
21
+ image = cv2.cvtColor(image.astype(np.uint8), cv2.COLOR_BGR2RGB)
22
+
23
+ if len(self.images) == 1:
24
+ return self.images[0]
25
+ else:
26
+ return self.images
27
+
28
+ def toThePixL(self,images, pixel_size):
29
+ self.images = []
30
+ self.square = pixel_size
31
+ for image in images:
32
+ image = Image.fromarray(image)
33
+ image = image.convert("RGB")
34
+ self.ImgW, self.ImgH = image.size
35
+ self.images.append(pixL.epicAlgorithm(self, image))
36
+
37
+ return pixL.preprocess(self)
38
+
39
+ def numOfSquaresFunc(self):
40
+ self.numOfSquaresW = round((self.ImgW / self.square) + 1)
41
+ self.numOfSquaresH = round((self.ImgH / self.square) + 1)
42
+
43
+ def epicAlgorithm(self, image):
44
+ pixValues = []
45
+ pixL.numOfSquaresFunc(self)
46
+
47
+ for j in range(1,self.numOfSquaresH):
48
+
49
+ for i in range(1,self.numOfSquaresW):
50
+
51
+ pixValues.append((image.getpixel((
52
+ i * self.square - self.square//2,
53
+ j * self.square - self.square//2)),
54
+ (i * self.square - self.square//2,
55
+ j * self.square - self.square//2)))
56
+
57
+ background = 255 * np.ones(shape=[self.ImgH - self.square,
58
+ self.ImgW - self.square*2, 3],
59
+ dtype=np.uint8)
60
+
61
+ for pen in range(len(pixValues)):
62
+
63
+ cv2.rectangle(background,
64
+ pt1=(pixValues[pen][1][0] - self.square,pixValues[pen][1][1] - self.square),
65
+ pt2=(pixValues[pen][1][0] + self.square,pixValues[pen][1][1] + self.square),
66
+ color=(pixValues[pen][0][2],pixValues[pen][0][1],pixValues[pen][0][0]),
67
+ thickness=-1)
68
+ background = np.array(background).astype(np.uint8)
69
+ background = cv2.resize(background, (self.ImgW,self.ImgH), interpolation = cv2.INTER_AREA)
70
+
71
+ return background
methods/media.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import torch
3
+ import imageio
4
+ from methods.img2pixl import pixL
5
+
6
+
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ face2paint = torch.hub.load("bryandlee/animegan2-pytorch:main", "face2paint", device=device, size=512)
9
+ model = torch.hub.load("bryandlee/animegan2-pytorch", "generator", device=device).eval()
10
+
11
+ class Media:
12
+ #Author: Alican Akca
13
+ def __init__(self,fname = None,pixel_size = None):
14
+ self.fname = fname
15
+ self.pixel_size = pixel_size
16
+
17
+ def split(self,fname,pixel_size, mediaType):
18
+ media = cv2.VideoCapture(fname)
19
+ frames = []
20
+ while True:
21
+ ret, cv2Image = media.read()
22
+ if not ret:
23
+ break
24
+ frames.append(cv2Image)
25
+ frames = pixL().toThePixL(frames, pixel_size)
26
+ if mediaType == 'gif':
27
+ imageio.mimsave('cache.gif', frames)
28
+ return [None, 'cache.gif', 'cache.gif']
29
+ else:
30
+ output_file = "cache.mp4"
31
+ out = cv2.VideoWriter(output_file,cv2.VideoWriter_fourcc(*'h264'), 15, (frames[0].shape[1],frames[0].shape[0]))
32
+ for i in range(len(frames)):
33
+ out.write(frames[i])
34
+ out.release()
35
+ return [output_file, None, output_file]
output/result_0.png ADDED
output/result_mask_0.png ADDED
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ pip
2
+ torch
3
+ gradio
4
+ Pillow
5
+ imageio
6
+ paddlehub
7
+ torchaudio
8
+ torchvision
9
+ paddlepaddle
10
+ opencv_python
src/GAN.py ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import keras
4
+ import warnings
5
+ import numpy as np
6
+ from PIL import Image
7
+ import matplotlib.pyplot as plt
8
+
9
+ from tensorflow.keras.optimizers import Adam
10
+ from tensorflow.keras.models import Sequential, Model
11
+ from tensorflow.keras.layers import Dense, LeakyReLU, Reshape, Flatten, Input
12
+ from tensorflow.keras.layers import Conv2D, MaxPooling2D, Activation, Dropout, Conv2DTranspose
13
+
14
+ from tensorflow.compat.v1.keras.layers import BatchNormalization
15
+
16
+ images = []
17
+ def load_images(size=(64,64)):
18
+ pixed_faces = os.listdir("kaggle/working/results/pixed_faces")
19
+ images_Path = "kaggle/working/results/pixed_faces"
20
+ for i in pixed_faces:
21
+ try:
22
+ image = cv2.imread(f"{images_Path}/{i}")
23
+ image = cv2.resize(image,size)
24
+ images.append(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
25
+ except:
26
+ pass
27
+
28
+ load_images()
29
+
30
+
31
+ #--------vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
32
+ #Author: https://www.kaggle.com/nassimyagoub
33
+ #--------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
34
+ def __init__(self):
35
+ self.img_shape = (64, 64, 3)
36
+
37
+ self.noise_size = 100
38
+
39
+ optimizer = Adam(0.0002,0.5)
40
+
41
+ self.discriminator = self.build_discriminator()
42
+ self.discriminator.compile(loss='binary_crossentropy',
43
+ optimizer=optimizer,
44
+ metrics=['accuracy'])
45
+
46
+ self.generator = self.build_generator()
47
+ self.generator.compile(loss='binary_crossentropy', optimizer=optimizer)
48
+
49
+ self.combined = Sequential()
50
+ self.combined.add(self.generator)
51
+ self.combined.add(self.discriminator)
52
+
53
+ self.discriminator.trainable = False
54
+
55
+ self.combined.compile(loss='binary_crossentropy', optimizer=optimizer)
56
+
57
+ self.combined.summary()
58
+
59
+ def build_generator(self):
60
+ epsilon = 0.00001
61
+ noise_shape = (self.noise_size,)
62
+
63
+ model = Sequential()
64
+
65
+ model.add(Dense(4*4*512, activation='linear', input_shape=noise_shape))
66
+ model.add(LeakyReLU(alpha=0.2))
67
+ model.add(Reshape((4, 4, 512)))
68
+
69
+ model.add(Conv2DTranspose(512, kernel_size=[4,4], strides=[2,2], padding="same",
70
+ kernel_initializer= keras.initializers.TruncatedNormal(stddev=0.02)))
71
+ model.add(BatchNormalization(momentum=0.9, epsilon=epsilon))
72
+ model.add(LeakyReLU(alpha=0.2))
73
+
74
+ model.add(Conv2DTranspose(256, kernel_size=[4,4], strides=[2,2], padding="same",
75
+ kernel_initializer= keras.initializers.TruncatedNormal(stddev=0.02)))
76
+ model.add(BatchNormalization(momentum=0.9, epsilon=epsilon))
77
+ model.add(LeakyReLU(alpha=0.2))
78
+
79
+ model.add(Conv2DTranspose(128, kernel_size=[4,4], strides=[2,2], padding="same",
80
+ kernel_initializer= keras.initializers.TruncatedNormal(stddev=0.02)))
81
+ model.add(BatchNormalization(momentum=0.9, epsilon=epsilon))
82
+ model.add(LeakyReLU(alpha=0.2))
83
+
84
+ model.add(Conv2DTranspose(64, kernel_size=[4,4], strides=[2,2], padding="same",
85
+ kernel_initializer= keras.initializers.TruncatedNormal(stddev=0.02)))
86
+ model.add(BatchNormalization(momentum=0.9, epsilon=epsilon))
87
+ model.add(LeakyReLU(alpha=0.2))
88
+
89
+ model.add(Conv2DTranspose(3, kernel_size=[4,4], strides=[1,1], padding="same",
90
+ kernel_initializer= keras.initializers.TruncatedNormal(stddev=0.02)))
91
+
92
+ model.add(Activation("tanh"))
93
+
94
+ model.summary()
95
+
96
+ noise = Input(shape=noise_shape)
97
+ img = model(noise)
98
+
99
+ return Model(noise, img)
100
+
101
+ def build_discriminator(self):
102
+
103
+ model = Sequential()
104
+
105
+ model.add(Conv2D(128, (3,3), padding='same', input_shape=self.img_shape))
106
+ model.add(LeakyReLU(alpha=0.2))
107
+ model.add(BatchNormalization())
108
+ model.add(Conv2D(128, (3,3), padding='same'))
109
+ model.add(LeakyReLU(alpha=0.2))
110
+ model.add(BatchNormalization())
111
+ model.add(MaxPooling2D(pool_size=(3,3)))
112
+ model.add(Dropout(0.2))
113
+
114
+ model.add(Conv2D(128, (3,3), padding='same'))
115
+ model.add(LeakyReLU(alpha=0.2))
116
+ model.add(BatchNormalization())
117
+ model.add(Conv2D(128, (3,3), padding='same'))
118
+ model.add(LeakyReLU(alpha=0.2))
119
+ model.add(BatchNormalization())
120
+ model.add(MaxPooling2D(pool_size=(3,3)))
121
+ model.add(Dropout(0.3))
122
+
123
+ model.add(Flatten())
124
+ model.add(Dense(128))
125
+ model.add(LeakyReLU(alpha=0.2))
126
+ model.add(Dense(128))
127
+ model.add(LeakyReLU(alpha=0.2))
128
+ model.add(Dense(1, activation='sigmoid'))
129
+
130
+ model.summary()
131
+
132
+ img = Input(shape=self.img_shape)
133
+ validity = model(img)
134
+
135
+ return Model(img, validity)
136
+
137
+ def train(self, epochs, batch_size=128, metrics_update=50, save_images=100, save_model=2000):
138
+
139
+ X_train = np.array(images)
140
+ X_train = (X_train.astype(np.float32) - 127.5) / 127.5
141
+
142
+ half_batch = int(batch_size / 2)
143
+
144
+ mean_d_loss=[0,0]
145
+ mean_g_loss=0
146
+
147
+ for epoch in range(epochs):
148
+ idx = np.random.randint(0, X_train.shape[0], half_batch)
149
+ imgs = X_train[idx]
150
+
151
+ noise = np.random.normal(0, 1, (half_batch, self.noise_size))
152
+ gen_imgs = self.generator.predict(noise)
153
+
154
+
155
+
156
+
157
+ d_loss = 0.5 * np.add(self.discriminator.train_on_batch(imgs, np.ones((half_batch, 1))),
158
+ self.discriminator.train_on_batch(gen_imgs, np.zeros((half_batch, 1))))
159
+
160
+
161
+ noise = np.random.normal(0, 1, (batch_size, self.noise_size))
162
+
163
+ valid_y = np.array([1] * batch_size)
164
+ g_loss = self.combined.train_on_batch(noise, valid_y)
165
+
166
+ mean_d_loss[0] += d_loss[0]
167
+ mean_d_loss[1] += d_loss[1]
168
+ mean_g_loss += g_loss
169
+
170
+
171
+ if epoch % metrics_update == 0:
172
+ print ("%d [Discriminator loss: %f, acc.: %.2f%%] [Generator loss: %f]" % (epoch, mean_d_loss[0]/metrics_update, 100*mean_d_loss[1]/metrics_update, mean_g_loss/metrics_update))
173
+ mean_d_loss=[0,0]
174
+ mean_g_loss=0
175
+
176
+ if epoch % save_images == 0:
177
+ self.save_images(epoch)
178
+
179
+
180
+ if epoch % save_model == 0:
181
+ self.generator.save("kaggle/working/results/generators/generator_%d" % epoch)
182
+ self.discriminator.save("kaggle/working/results/discriminators/discriminator_%d" % epoch)
183
+
184
+
185
+ def save_images(self, epoch):
186
+ noise = np.random.normal(0, 1, (25, self.noise_size))
187
+ gen_imgs = self.generator.predict(noise)
188
+
189
+
190
+ gen_imgs = 0.5 * gen_imgs + 0.5
191
+
192
+ fig, axs = plt.subplots(5,5, figsize = (8,8))
193
+
194
+ for i in range(5):
195
+ for j in range(5):
196
+ axs[i,j].imshow(gen_imgs[5*i+j])
197
+ axs[i,j].axis('off')
198
+
199
+ plt.show()
200
+
201
+ fig.savefig("kaggle/working/results/pandaS_%d.png" % epoch)
202
+ plt.close()