|
|
|
import tensorflow as tf |
|
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU'))) |
|
|
|
|
|
from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D |
|
from tensorflow.keras.models import Sequential, Model |
|
from keras.utils.image_utils import img_to_array, load_img |
|
from matplotlib.pyplot import imshow |
|
from PIL import Image, ImageDraw |
|
import numpy as np |
|
import cv2 |
|
|
|
np.random.seed(342) |
|
|
|
|
|
|
|
|
|
|
|
|
|
profile_img = cv2.imread('profile.PNG') |
|
profile_img = cv2.cvtColor(profile_img, cv2.COLOR_BGR2RGB) |
|
print(profile_img.shape) |
|
|
|
reshaped = cv2.resize(profile_img, (256, 256)) |
|
print(reshaped.shape) |
|
img_array = img_to_array(reshaped).astype('float32') / 255. |
|
|
|
img_array = np.expand_dims(img_array, axis=0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
print(img_array.shape) |
|
|
|
SIZE = 256 |
|
model = Sequential() |
|
|
|
model.add(Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(SIZE, SIZE, 3))) |
|
model.add(MaxPooling2D((2, 2), padding='same')) |
|
model.add(Conv2D(64, (3, 3), activation='relu', padding='same')) |
|
model.add(MaxPooling2D((2, 2), padding='same')) |
|
model.add(Conv2D(128, (3, 3), activation='relu', padding='same')) |
|
model.add(MaxPooling2D((2, 2), padding='same')) |
|
model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) |
|
model.add(MaxPooling2D((2, 2), padding='same')) |
|
model.add(Conv2D(512, (3, 3), activation='relu', padding='same')) |
|
|
|
|
|
model.add(MaxPooling2D((2, 2), padding='same')) |
|
|
|
|
|
model.add(Conv2D(512, (3, 3), activation='relu', padding='same')) |
|
model.add(UpSampling2D((2, 2))) |
|
model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) |
|
model.add(UpSampling2D((2, 2))) |
|
model.add(Conv2D(128, (3, 3), activation='relu', padding='same')) |
|
model.add(UpSampling2D((2, 2))) |
|
model.add(Conv2D(64, (3, 3), activation='relu', padding='same')) |
|
model.add(UpSampling2D((2, 2))) |
|
model.add(Conv2D(32, (3, 3), activation='relu', padding='same')) |
|
model.add(UpSampling2D((2, 2))) |
|
|
|
|
|
model.add(Conv2D(3, (3, 3), activation='relu', padding='same')) |
|
|
|
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy']) |
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
model.summary() |
|
|
|
|
|
model_outputs = [] |
|
|
|
|
|
def on_epoch_end(epoch, logs): |
|
model_outputs.append(model.predict(img_array)) |
|
|
|
|
|
|
|
predict_callback = tf.keras.callbacks.LambdaCallback(on_epoch_end=on_epoch_end) |
|
|
|
|
|
|
|
model.fit(x=img_array, y=img_array, epochs=1000, batch_size=20, shuffle=True, callbacks=[predict_callback]) |
|
|
|
|
|
predict = model.predict(x=img_array) |
|
cv2.imshow("output", predict[0].reshape(SIZE, SIZE, 3)) |
|
cv2.waitKey(0) |
|
|
|
print("Creating a gif/video from the model outputs.") |
|
|
|
print(len(model_outputs)) |
|
|
|
images = [(output[0] * 255).astype('uint8') for output in model_outputs] |
|
try: |
|
images = [(output[0] * 255).astype('uint8') for output in model_outputs] |
|
|
|
|
|
video_filename = 'model_outputs_5.4.mp4' |
|
fourcc = cv2.VideoWriter_fourcc(*'mp4v') |
|
|
|
|
|
height, width, _ = images[0].shape |
|
|
|
|
|
fps = 30 |
|
video = cv2.VideoWriter(video_filename, fourcc, fps, (width, height)) |
|
|
|
|
|
for image in images: |
|
|
|
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
|
video.write(rgb_image) |
|
|
|
|
|
video.release() |
|
except Exception as e: |
|
print(e) |
|
images = [Image.fromarray((output[0] * 255).astype('uint8')) for output in model_outputs] |
|
images[0].save('model_outputs_5.4m_param.gif', save_all=True, append_images=images[1:], optimize=False, duration=20, |
|
loop=0) |
|
|
|
|
|
|
|
|
|
|
|
images = [Image.fromarray((output[0] * 255).astype('uint8')) for output in model_outputs] |
|
images[0].save('model_outputs_5.4m_param.gif', save_all=True, append_images=images[1:], optimize=False, duration=20, |
|
loop=0) |