【keras】图片压缩 自建训练集

prepare.py

import numpy as np
import cv2
def divide(img,K):  #每一块的大小
  col = K*K
  nrows,ncols = img.shape
  R = int(nrows/K)
  C = int(ncols/K)
  vec = np.ones((R*C,col))
  row = 0
  for i in range(R):
    for j in range(C):
      vec[row,0:col] = img[i*K:(i+1)*K,j*K:(j+1)*K].reshape(1,col)
      row += 1
  return vec

wh0=100
wh=50
import os
dir_path='../../img'
names=os.listdir(dir_path)
data=np.ones(  (len(names)*3*int(wh0/wh)*int(wh0/wh),wh*wh)  )
print(  len(names)*3*int(wh0/wh)*int(wh0/wh),wh*wh  )
index=0
for file in names:
    filepath=os.path.join(dir_path,file)
    print(filepath)
    image = cv2.imread(filepath)
    image= cv2.resize(image,dsize=(wh0,wh0))

    r=image[:,:,0]
    g=image[:,:,1]
    b=image[:,:,2]

    r2=divide(r,wh)
    g2=divide(g,wh)
    b2=divide(b,wh)

    for i in range(r2.shape[0]):
        data[index+3*i  ,:]=r2[i,:]
        data[index+3*i+1,:]=g2[i,:]
        data[index+3*i+2,:]=b2[i,:]
    index=index+3*int(wh0/wh)*int(wh0/wh)
print(data.shape)
x_train = data.astype('float32') / 255.
np.savez('x_train',x_train=x_train,wh=wh,l=image.shape[0]/wh)

train.py

import keras
from keras import layers
from keras.datasets import mnist
import numpy as np
import cv2
npzfile=np.load('x_train.npz')
x_train=npzfile['x_train']
wh=npzfile['wh']#int(npzfile['wh']/npzfile['partion'])
print(wh)

data_dim=wh*wh
encoding_dim = wh*25  #编码维度 # 32 floats -> compression of factor 24.5, assuming the input is 784 floats
print(wh)
input_img = keras.Input(shape=(data_dim,))#This is our input image

encoded = layers.Dense(encoding_dim, activation='relu')(input_img)#编码层 #"encoded" is the encoded representation of the input
decoded = layers.Dense(data_dim, activation='sigmoid')(encoded)#编码、解码 #"decoded" is the lossy reconstruction of the input

#解码模型
autoencoder = keras.Model(input_img, decoded)# This model maps an input to its reconstruction
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')#每像素二元交叉熵损失和 Adam 优化器
print(x_train.shape)
autoencoder.fit(x_train, x_train,#拟合
                epochs=60,#将数据使用多少遍
                batch_size=1,#将7500个数据分成多少份
                shuffle=True,
                validation_data=(x_train, x_train))
encoded_input = keras.Input(shape=(encoding_dim,))# This is our encoded (32-dimensional) input
autoencoder.save( filepath="modelResult.h5", overwrite=True, include_optimizer=True )

predict.py

import keras
from keras import layers
from keras.datasets import mnist
import numpy as np
import cv2
#数据读取
npzfile=np.load('x_train.npz')
x_train=npzfile['x_train']
wh=npzfile['wh']#int(npzfile['wh']/npzfile['partion'])
print(wh)

#数据处理
x_train = x_train.astype('float32') / 255.
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))

data_dim=wh*wh
encoding_dim = wh*25  #编码维度 # 32 floats -> compression of factor 24.5, assuming the input is 784 floats

input_img = keras.Input(shape=(data_dim,))#This is our input image#解码模型
encoded_input = keras.Input(shape=(encoding_dim,))# This is our encoded (32-dimensional) input
m=keras.models.load_model( "modelResult.h5" ).layers
encoder= keras.Model(input_img, m[1](input_img))#编码模型  # input_img 似乎没有参数#784
decoder = keras.Model(encoded_input, m[2](encoded_input))# Create the decoder model

encoded_imgs = encoder.predict(x_train)#32# 进行编码/解码测试 #Encode and decode some digits
decoded_imgs = decoder.predict(encoded_imgs)#784# Note that we take them from the *test* set
#https://blog.csdn.net/zhw864680355/article/details/103405677
'''
rgb=decoded_imgs.reshape(3,wh, wh)
result=255.*rgb.transpose((1,2,0))#getImg(rgb,wh,wh)
cv2.imwrite('test2.jpg',result)
'''
#图片效果展示 # Use Matplotlib (don't ask)
import matplotlib.pyplot as plt
n = 8  # How many digits we will display
plt.figure(figsize=(20, 4))
for i in range(n):# Display original
    ax = plt.subplot(2, n, i + 1)
    plt.imshow(x_train[i].reshape(wh, wh))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    # Display reconstruction
    ax = plt.subplot(2, n, i + 1 + n)
    plt.imshow(decoded_imgs[i].reshape(wh, wh))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值