| import warnings |
|
|
| warnings.filterwarnings("ignore") |
|
|
| import os |
| import sys |
| import glob |
| import time |
| import numpy as np |
| from PIL import Image |
| from pathlib import Path |
| from tqdm.notebook import tqdm |
| import matplotlib.pyplot as plt |
| from skimage.color import rgb2lab, lab2rgb |
|
|
| import torch |
| from torch import nn, optim |
| from torchvision import transforms |
| from torchvision.utils import make_grid |
| from torch.utils.data import Dataset, DataLoader |
|
|
|
|
| from utility import * |
| from model import * |
|
|
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
| model_path_1 = "model/ImageColorizationModel.pth" |
| model_path_2 = "model/ImageColorizationModel-ver2.pth" |
|
|
| MODEL_VER_1 = "https://drive.google.com/uc?id=1EhuMET76c02VFyRW8Pie7BwNCDHmQiad" |
| MODEL_VER_2 = "https://drive.google.com/uc?id=1SX9StBHdb-DO1lcDcy0Uz8OVmzoFdlxS" |
|
|
|
|
| model = None |
| if not os.path.exists(model_path_1): |
| download_from_drive(MODEL_VER_1 , model_path_1) |
| if not os.path.exists(model_path_2): |
| download_from_drive(MODEL_VER_2 , model_path_2) |
|
|
| model_1 = load_model_with_cpu(model_class=MainModel, file_path=model_path_1) |
| model_2 = load_model_with_cpu(model_class=MainModel, file_path=model_path_2) |
|
|
| def predict_and_return_image(image , model_name): |
| model = model_1 if model_name == "MODEL_1" else model_2 |
| if image is None: |
| return None |
| data = create_lab_tensors(image) |
| model.net_G.eval() |
| with torch.no_grad(): |
| model.setup_input(data) |
| model.forward() |
| fake_color = model.fake_color.detach() |
| L = model.L |
| fake_imgs = lab_to_rgb(L, fake_color) |
| return fake_imgs[0] |
|
|
|
|
| import gradio as gr |
| title = "<h1 style='font-size: 300%; text-align: center; color: #191970;'>Aiconvert.online</h1>" + \ |
| "<h2 style='font-size: 150%; text-align: center;'>Black&White to Color image</h2>" + \ |
| "<p>Transforming Black & White Image into a colored image. Upload a black and white image to see it colorized by our deep learning model.</p>" |
|
|
|
|
| css = ''' |
| .gradio-container {background-color: #F5FFFA} |
| .animate-spin { |
| animation: spin 1s linear infinite; |
| } |
| @keyframes spin { |
| from { |
| transform: rotate(0deg); |
| } |
| to { |
| transform: rotate(360deg); |
| } |
| } |
| ''' |
| gr.Interface( |
| title=title, |
| fn=predict_and_return_image, |
| inputs=[ |
| gr.Image(label="Gray Scale Image"), |
| gr.Dropdown(["MODEL_1", "MODEL_2"]) |
| ], |
| outputs=[gr.Image(label="Predicted Colored Image")], |
| css = ''' |
| .gradio-container {background-color: #FFF0F5} |
| .animate-spin { |
| animation: spin 1s linear infinite; |
| } |
| @keyframes spin { |
| from { |
| transform: rotate(0deg); |
| } |
| to { |
| transform: rotate(360deg); |
| } |
| } |
| footer{display:none !important} |
| ''' |
| ).launch(share=False) |
|
|