Giter Club home page Giter Club logo

autoencoder's Introduction

Autoencoder

We start with image colorization by using AutoEncoder technics

# -*- coding: utf-8 -*-
"""
Created on Tue Aug 17 22:10:12 2021

@author: talha
"""

#import
from matplotlib.pyplot import imshow
import numpy as np
import cv2
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.layers import Conv2D, MaxPooling2D, UpSampling2D
from tensorflow.keras.models import Sequential

#visualize the for loop
from tqdm import tqdm

import tensorflow as tf
gpus = tf.config.list_physical_devices('GPU')

#take image path
img_path =  "flower_images/"

#Append to list all image name 
import os
img_path_list = os.listdir(img_path) 

#Preprocessing 

img_data = []

for i in tqdm(img_path_list):
    path = os.path.join(img_path ,i)
    img = cv2.imread(path,1)
    # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #to see in original view
    img = img.astype('float32') / 255.     # all images are needed to convert between 0 and 1
    img=cv2.resize(img,(128, 128))
    img_data.append(img)
    #output
    

#Transform to the gray format , 
gray_image = []

for i in tqdm(img_data):
    img = cv2.cvtColor(i, cv2.COLOR_BGR2GRAY) 
    gray_image.append(img)

#show input and output 
import matplotlib.pyplot as plt

plt.figure(figsize=(20, 4))
for i in range(10):
    # display original
    ax = plt.subplot(3, 20, i + 1)
    plt.imshow(img_data[i], cmap="binary")
    
    # display gray
    ax = plt.subplot(3, 20, 20 +i+ 1)
    plt.imshow(gray_image[i], cmap="binary")
    

#Reshape the trainable format
output_data = np.reshape(img_data, (len(img_data), 128, 128, 3))
input_data = np.reshape(gray_image, (len(gray_image), 128, 128, 1))


#Build Model
model = Sequential()
model.add(Conv2D(64, (3, 3), activation='relu', padding='same',strides=2,  input_shape=(128, 128, 1)))
model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(128, (3, 3), activation='relu', padding='same',strides=2))

model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(256, (3, 3), activation='relu', padding='same',strides=2))

model.add(Conv2D(128, (3,3), activation='relu', padding='same'))
model.add(Conv2D(128, (3,3), activation='relu', padding='same'))

model.add(Conv2D(64, (3,3), activation='relu', padding='same'))
 
model.add(Conv2D(32, (3, 3), activation='relu', padding='same'))
model.add(UpSampling2D((2, 2)))
model.add(Conv2D(16, (3, 3), activation='relu', padding='same'))
model.add(UpSampling2D((2, 2)))
model.add(Conv2D(8, (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='mse',metrics=['accuracy'])

model.summary()

#Fit model 


model.fit(input_data, output_data,
        validation_split=0.1,
        epochs=1000,
        batch_size=16,
        shuffle=True)

#Test Image
test_img = cv2.imread("monalisa.jpg",0)
test_img = test_img.astype('float32') / 255.     # all images are needed to convert between 0 and 1
test_img=cv2.resize(test_img,(128, 128))
imshow(test_img, cmap="gray")
#model input
test_img = test_img.reshape(1,128,128,1)
pred = model.predict(test_img)
#show predicted image
imshow(pred[0].reshape(128,128,3), cmap="binary")

#Save Model
# model.save('imagecolorization_autoencoder.model')

for any problem , don't hesitate to contact me from Linkedin ๐Ÿ‘ ๐Ÿ‘

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.