无监督算法

from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D

from keras.models import Model

from keras import optimizers

from keras import backend as K

import tensorflow

import cv2

import glob

mh=704

mw=160

num_epochs=1000

input_img = Input(shape=(mh, mw, 1))  # adapt this if using `channels_first` image data format

# Encode-----------------------------------------------------------

x = Conv2D(32, (4, 4), strides=2 , activation='relu', padding='same')(input_img)

x = Conv2D(32, (4, 4), strides=2, activation='relu', padding='same')(x)

x = Conv2D(32, (3, 3), strides=1, activation='relu', padding='same')(x)

x = Conv2D(64, (4, 4), strides=2, activation='relu', padding='same')(x)

x = Conv2D(64, (3, 3), strides=1, activation='relu', padding='same')(x)

x = Conv2D(128, (4, 4), strides=2, activation='relu', padding='same')(x)

x = Conv2D(64, (3, 3), strides=1, activation='relu', padding='same')(x)

x = Conv2D(32, (3, 3), strides=1, activation='relu', padding='same')(x)

encoded = Conv2D(1, (8, 8), strides=1, padding='same')(x)

# Decode---------------------------------------------------------------------

x = Conv2D(32, (3, 3), strides=1, activation='relu', padding='same')(encoded)

x = Conv2D(64, (3, 3), strides=1, activation='relu', padding='same')(x)

x = UpSampling2D((2, 2))(x)

x = Conv2D(128, (4, 4), strides=2, activation='relu', padding='same')(x)

x = UpSampling2D((2, 2))(x)

x = Conv2D(64, (3, 3), strides=1, activation='relu', padding='same')(x)

x = UpSampling2D((2, 2))(x)

x = Conv2D(64, (4, 4), strides=2, activation='relu', padding='same')(x)

x = UpSampling2D((2, 2))(x)

x = Conv2D(32, (3, 3), strides=1, activation='relu', padding='same')(x)

x = UpSampling2D((2, 2))(x)

x = Conv2D(32, (4, 4), strides=2, activation='relu', padding='same')(x)

x = UpSampling2D((4, 4))(x)

x = Conv2D(32, (4, 4), strides=2, activation='relu', padding='same')(x)

x = UpSampling2D((2, 2))(x)

decoded = Conv2D(1, (8, 8), activation='sigmoid', padding='same')(x)

# ---------------------------------------------------------------------

autoencoder = Model(input_img, decoded)

adam = optimizers.Adam(lr=0.0002, decay=0.00001)

autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

autoencoder.summary()

import os, glob

import numpy as np

from sklearn.model_selection import train_test_split

from PIL import Image

# Own Your Image Directory

img_dir = ("./Samples/")

img_files = glob.glob(img_dir + "*.png")

# Setting Image Propertie

width = mw

height = mh

pixels = width * height * 1 # gray scale

# Load Image

# AutoEncoder does not have to label data

x = []

for i, f in enumerate(img_files):

    img = Image.open(f)

#    img = img.convert("RGB")

    img = img.convert("L") # gray sclae

    img = img.resize((width,height), 1)

    data = np.asarray(img)

    x.append(data)

    if i % 10 == 0:

        print(i, "\n", data)

x = np.array(x)

(x_train, x_test) = train_test_split(x, shuffle=False, train_size=0.7, random_state=1)

img_list = (x_train, x_test)

np.save("./obj.npy", img_list)

print("OK", len(x))

# change to float32

x_train = x_train.astype('float32') / 255.

x_test = x_test.astype('float32') / 255.

x_train = np.reshape(x_train, (len(x_train), mh, mw, 1))  # adapt this if using `channels_first` image data format

x_test = np.reshape(x_test, (len(x_test), mh,  mw, 1))  # adapt this if using `channels_first` image data format

print (x_train.shape)

print (x_test.shape)

from keras.callbacks import TensorBoard

autoencoder.fit(x_train, x_train,

                epochs=num_epochs,

                batch_size=32,

                shuffle=True,

                validation_data=(x_test, x_test),

                callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])

####################################################

####################################################

####################################################

####################################################

####################################################

####################################################

####################################################

'''

import matplotlib.pyplot as plt

decoded_imgs = autoencoder.predict(x_train)

n = 8  # how many digits we will display

plt.figure(figsize=(20, 5), dpi=100)

for i in range(n):

    # display original

    ax = plt.subplot(2, n, i + 1)

    plt.imshow(x_train[i].reshape(mh, mw))

    plt.gray()

    ax.get_xaxis().set_visible(True)

    ax.get_yaxis().set_visible(False)


    # SSIM Encode

    ax.set_title("Encode_Image")


    npImg = x_train[i]

    npImg = npImg.reshape((mh, mw))

    formatted = (npImg * 255 / np.max(npImg)).astype('uint8')

    img = Image.fromarray(formatted)

    # display reconstruction

    ax = plt.subplot(2, n, i + 1 + n)

    plt.imshow(decoded_imgs[i].reshape(mh, mw))

    plt.gray()

    ax.get_xaxis().set_visible(True)

    ax.get_yaxis().set_visible(False)


    # SSIM Decoded   

    npDecoded = decoded_imgs[i]

    npDecoded = npDecoded.reshape((mh,mw))

    formatted2 = (npDecoded * 255 / np.max(npDecoded)).astype('uint8')

    decoded = Image.fromarray(formatted2)


    from SSIM_PIL import compare_ssim as ssim

    value = ssim(img, decoded)

    label = 'SSIM: {:.3f}'


    ax.set_title("Decoded_Image")

    ax.set_xlabel(label.format(value))

plt.show()

'''

'''

import matplotlib.pyplot as plt

decoded_imgs = autoencoder.predict(x_test)

n = 3 # how many digits we will display

for i in range(n):

    # display original

    ax = plt.subplot(2, n, i + 1)

    plt.imshow(x_test[i].reshape(mh, mw))

    plt.gray()

    ax.get_xaxis().set_visible(True)

    ax.get_yaxis().set_visible(False)


    # SSIM Encode

    ax.set_title("Encode_Image")


    npImg = x_test[i]

    npImg = npImg.reshape((mh, mw))

    formatted = (npImg * 255 / np.max(npImg)).astype('uint8')

    img = Image.fromarray(formatted)

    # display reconstruction

    ax = plt.subplot(2, n, i + 1 + n)

    plt.imshow(decoded_imgs[i].reshape(mh, mw))

    plt.gray()

    ax.get_xaxis().set_visible(True)

    ax.get_yaxis().set_visible(False)


    # SSIM Decoded   

    npDecoded = decoded_imgs[i]

    npDecoded = npDecoded.reshape((mh,mw))

    formatted2 = (npDecoded * 255 / np.max(npDecoded)).astype('uint8')

    decoded = Image.fromarray(formatted2)


    from SSIM_PIL import compare_ssim as ssim

    value = ssim(img, decoded)

    label = 'SSIM: {:.3f}'


    ax.set_title("Decoded_Image")

    ax.set_xlabel(label.format(value))

plt.show()

'''

def  search_imgs():

      path = "./test/*.png"

      path_lists = glob.glob(path)

      imgs=[]

      for p in path_lists:

                img=cv2.imread(p,0)

                img=cv2.resize(img,(mw,mh))

                img=np.expand_dims(img,3)

                imgs.append(img.astype('float32') / 255.)

      imgs=np.array(imgs)

      print("------------------------------------------=",imgs.shape)

      #x_test = x_test.astype('float32') / 255.

      #x_test = np.reshape(imgs, (len(imgs), mh,  mw, 1))  # adapt this if using `channels_first` image data format

      return imgs

import matplotlib.pyplot as plt

x_test=search_imgs()

decoded_imgs = autoencoder.predict(x_test)

for i in range(len(x_test)):

    # display original

    ori_img = x_test[i].reshape(mh, mw)

    cv2.imwrite("./xxx/"+str(i)+"_ori_.jpg", (ori_img*255).astype(np.uint8))

    # Decoded   

    npDecoded = decoded_imgs[i]

    npDecoded = npDecoded.reshape((mh,mw))

    cv2.imwrite("./xxx/"+str(i)+"_npDecoded _.jpg", (npDecoded*255).astype(np.uint8) )

plt.show()

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 简单线性回归 import tensorflow as tf import numpy # 创造数据 x_dat...
    CAICAI0阅读 8,922评论 0 49
  • JavaClient 查询ES 1、term query 分词精确查询,查询hotelName 分词后包含 hot...
    jackLee阅读 27,603评论 0 2
  • 【清芳妖境】81 20180317 西子姑娘长的美。任何一个见过她的人都会惊叹。天生丽质,代理三草两木化妆品的优势...
    清芳福田阅读 2,994评论 4 4
  • 夏风透柳绿意浓, 水映塔桥气色空。 沿岸小林得风宠, 隐听虫鸣悦其中。 山间轻漾水波动, 却看亭中一点红。 古街客...
    该用户很帅阅读 1,351评论 1 1
  • “姐,我要吃肉……” “毛仔乖!姐姐卖了这些瓶子就给你买肉吃。” 五岁的毛仔高声欢呼一声,朝家跑去。 只有十岁的苏...
    弈之怪谈阅读 3,456评论 0 6

友情链接更多精彩内容