import tensorflow as tf
from keras.applications.inception_v3 import InceptionV3
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense,GlobalAveragePooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.import backend as K
import os
from keras.utils import plot_model
from matplotlib import pyplot as plt
from keras.applications import VGG16
train_dir=os.path.join(base_dir,'train')
conv_base=VGG16(weights='imagenet',
include_top=False,
input_shape=(150,150,3))from keras import layers
from keras import models
model=models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256,activation='relu'))
model.add(layers.Dense(1,activation='sigmoid'))
conv_base.trainable=Falsefrom keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
train_datagen=ImageDataGenerator(\
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
test_datagen=ImageDataGenerator(rescale=1./255)
train_generator=train_datagen.flow_from_dirctory(
train_dir,
target_size=(150,150),
batch_size=20,
class_mode='binary')
validation_generator=test_datagen.flow_from_dirctory(
validation_dir,
target_size=(150,150),
batch_size=20,
class_mode='binary')
model.compile(loss='binary_crossentropy',
optimizers=optimizers.RMSprop(2e-5),
metrics=['acc'])
history=model.fit_generator(
train_generator,
steps_per_epoch=100,
epoch=30,
validation_data=validation_generator,
validation_steps=100)import matplotlib.pyplot as plt
acc=history.history['acc']
val_acc=history.history['val_acc']
loss=history.history['loss']
val_loss=history.history['val_loss']
epochs=range(1,len(acc)+1)
plt.plot(epochs,acc,a'bo',label='Training acc')
plt.plot(epochs,val_acc,'b',label='validation acc')
from keras import backend as K
model=load_model('')
layer_1=K.function([model.layers[0].input],[model.layers[1].output])
f1=layer_1([input_image])[0]for _ inrange(32):
show_img=f1[:,:,:,_]
show_img.shape=[149,149]
plt.subplot(4,8,_+1)
plt.imshow(show_img,cmap='gray')
plt.axis('off')
plt.show()
import numpy as np
#from processor import process_imagefrom keras.models import load_model
from keras import backend as K
import matplotlib.pyplot as plt
import cv2
from keras.preprocessing.image import img_to_array, load_img
import numpy as np
defprocess_image(image, target_shape):#"""Given an image, process it and return the array."""# Load the image.
h, w, _ = target_shape
image = load_img(image, target_size=(h, w))# Turn it into numpy, normalize and return.
img_arr = img_to_array(image)
x =(img_arr /255.).astype(np.float32)return x
defmain():
model = load_model('inception.026-1.07.hdf5')#replaced by your model name# Get all our test images.
image='img03.jpg'
images=cv2.imread('img03.jpg')# cv2.imshow("Image", images)# cv2.waitKey(0)# Turn the image into an array.
image_arr = process_image(image,(299,299,3))# 根据载入的训练好的模型的配置,将图像统一尺寸
image_arr = np.expand_dims(image_arr, axis=0)# 设置可视化的层
layer_1 = K.function([model.layers[0].input],[model.layers[1].output])
f1 = layer_1([image_arr])[0]for _ inrange(32):
show_img = f1[:,:,:, _]
show_img.shape =[149,149]
plt.figure(figsize=(10,10))
plt.subplot(4,8, _ +1)
plt.subplot(4,8, _ +1)
plt.imshow(show_img, cmap='gray')
plt.axis('off')
plt.show()# conv layer: 299
layer_1 = K.function([model.layers[0].input],[model.layers[299].output])
f1 = layer_1([image_arr])[0]for _ inrange(81):
show_img = f1[:,:,:, _]
show_img.shape =[8,8]
plt.figure(figsize=(10,10))
plt.subplot(9,9, _ +1)
plt.imshow(show_img, cmap='gray')
plt.axis('off')
plt.show()print('This is the end !')if __name__ =='__main__':
main()