线性回归
import keras
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
x_data = np.random.rand(100)
noise = np.random.normal(0,0.01,x_data.shape)
y_data = x_data*0.1 + 0.2 + noise
plt.scatter(x_data,y_data)
plt.show()
model = Sequential()
model.add(Dense(units=1,input_dim=1))
model.compile(optimizer='sgd',loss='mse')
for step in range(3001):
cost = model.train_on_batch(x_data,y_data)
if step % 500 == 0:
print('cost:',cost)
W,b = model.layers[0].get_weights()
print('W:',W,'b:',b)
y_pred = model.predict(x_data)
plt.scatter(x_data,y_data)
plt.plot(x_data,y_pred,'r-',lw=3)
plt.show()
非线性
import keras
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense,Activation
from keras.optimizers import SGD
x_data = np.linspace(-0.5,0.5,200)
noise = np.random.normal(0,0.02,x_data.shape)
y_data = np.square(x_data) + noise
plt.scatter(x_data,y_data)
plt.show()
model = Sequential()
model.add(Dense(units=10,input_dim=1,activation='relu'))
model.add(Dense(units=1,activation='relu'))
sgd = SGD(lr=0.3)
model.compile(optimizer=sgd,loss='mse')
for step in range(3001):
cost = model.train_on_batch(x_data,y_data)
if step % 500 == 0:
print('cost:',cost)
y_pred = model.predict(x_data)
plt.scatter(x_data,y_data)
plt.plot(x_data,y_pred,'r-',lw=3)
plt.show()
MNIST
import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
(x_train,y_train),(x_test,y_test) = mnist.load_data()
print('x_shape:',x_train.shape)
print('y_shape:',y_train.shape)
x_train = x_train.reshape(x_train.shape[0],-1)/255.0
x_test = x_test.reshape(x_test.shape[0],-1)/255.0
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)
-------------------------------------------------------------------------------
model = Sequential([
Dense(units=10,input_dim=784,bias_initializer='one',activation='softmax')
])
sgd = SGD(lr=0.2)
model.compile(
optimizer = sgd,
loss = 'mse',
metrics=['accuracy'],
)
model.fit(x_train,y_train,batch_size=32,epochs=10)
-------------------------------------------------------------------------------
model = load_model('model.h5')
loss,accuracy = model.evaluate(x_test,y_test)
print('\ntest loss',loss)
print('accuracy',accuracy)
model.save('model.h5')
model.save_weights('my_model_weights.h5')
model.load_weights('my_model_weights.h5')
from keras.models import model_from_json
json_string = model.to_json()
model = model_from_json(json_string)
CNN
(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_train = x_train.reshape(-1,28,28,1)/255.0
x_test = x_test.reshape(-1,28,28,1)/255.0
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)
model = Sequential()
model.add(Convolution2D(
input_shape = (28,28,1),
filters = 32,
kernel_size = 5,
strides = 1,
padding = 'same',
activation = 'relu'
))
model.add(MaxPooling2D(
pool_size = 2,
strides = 2,
padding = 'same',
))
model.add(Convolution2D(64,5,strides=1,padding='same',activation = 'relu'))
model.add(MaxPooling2D(2,2,'same'))
model.add(Flatten())
model.add(Dense(1024,activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(10,activation='softmax'))
adam = Adam(lr=1e-4)
model.compile(optimizer=adam,loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=64,epochs=10)
loss,accuracy = model.evaluate(x_test,y_test)
print('test loss',loss)
print('test accuracy',accuracy)
绘制网络结构
plot_model(model,to_file="model.png",show_shapes=True,show_layer_names=True,rankdir='TB')
plt.figure(figsize=(10,10))
img = plt.imread("model.png")
plt.imshow(img)
plt.axis('off')
plt.show()
数据增强
from keras.preprocessing.image import ImageDataGenerator, array_to_img ,img_to_array, load_img
import numpy as np
datagen = ImageDataGenerator(
rotation_range = 40,
width_shift_range = 0.2,
height_shift_range = 0.2,
rescale = 1/255,
shear_range = 20,
zoom_range = 0.2,
horizontal_flip = True,
fill_mode = 'nearest',
)
img = load_img('image/train/cat/cat.1.jpg')
x = img_to_array(img)
print(x.shape)
x = np.expand_dims(x,0)
print(x.shape)
i = 0
for batch in datagen.flow(x, batch_size=1, save_to_dir='temp', save_prefix='new_cat', save_format='jpeg'):
i += 1
if i==20:
break
print('finished!')
CNN猫狗
from keras.models import Sequential
from keras.layers import Conv2D,MaxPool2D,Activation,Dropout,Flatten,Dense
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator,img_to_array,load_img
model = Sequential()
model.add(Conv2D(input_shape=(150,150,3),filters=32,kernel_size=3,padding='same',activation='relu'))
model.add(Conv2D(filters=32,kernel_size=3,padding='same',activation='relu'))
model.add(MaxPool2D(pool_size=2, strides=2))
model.add(Conv2D(filters=64,kernel_size=3,padding='same',activation='relu'))
model.add(Conv2D(filters=64,kernel_size=3,padding='same',activation='relu'))
model.add(MaxPool2D(pool_size=2, strides=2))
model.add(Conv2D(filters=128,kernel_size=3,padding='same',activation='relu'))
model.add(Conv2D(filters=128,kernel_size=3,padding='same',activation='relu'))
model.add(MaxPool2D(pool_size=2, strides=2))
model.add(Flatten())
model.add(Dense(64,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2,activation='softmax'))
adam = Adam(lr=1e-4)
model.compile(optimizer=adam,loss='categorical_crossentropy',metrics=['accuracy'])
train_datagen = ImageDataGenerator(
rotation_range = 40,
width_shift_range = 0.2,
height_shift_range = 0.2,
rescale = 1/255,
shear_range = 20,
zoom_range = 0.2,
horizontal_flip = True,
fill_mode = 'nearest',
)
test_datagen = ImageDataGenerator(
rescale = 1/255,
)
batch_size = 32
train_generator = train_datagen.flow_from_directory(
'image/train',
target_size=(150,150),
batch_size=batch_size,
)
test_generator = test_datagen.flow_from_directory(
'image/test',
target_size=(150,150),
batch_size=batch_size,
)
train_generator.class_indices
model.fit_generator(train_generator,steps_per_epoch=len(train_generator),epochs=30,validation_data=test_generator,validation_steps=len(test_generator))
model.save('model_cnn.h5')
from keras.models import load_model
import numpy as np
label = np.array(['cat','dog'])
model = load_model('model_cnn.h5')
image = load_img('image/test/cat/cat.1002.jpg')
image
image = image.resize((150,150))
image = img_to_array(image)
image = image/255
image = np.expand_dims(image,0)
image.shape
print(label[model.predict_classes(image)])
VGG16猫狗分类
from keras.applications.vgg16 import VGG16
from keras.models import Sequential
from keras.layers import Conv2D,MaxPool2D,Activation,Dropout,Flatten,Dense
from keras.optimizers import SGD
from keras.preprocessing.image import ImageDataGenerator,img_to_array,load_img
import numpy as np
vgg16_model = VGG16(weights='imagenet',include_top=False, input_shape=(150,150,3))
top_model = Sequential()
top_model.add(Flatten(input_shape=vgg16_model.output_shape[1:]))
top_model.add(Dense(256,activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(2,activation='softmax'))
model = Sequential()
model.add(vgg16_model)
model.add(top_model)
train_datagen = ImageDataGenerator(
rotation_range = 40,
width_shift_range = 0.2,
height_shift_range = 0.2,
rescale = 1/255,
shear_range = 20,
zoom_range = 0.2,
horizontal_flip = True,
fill_mode = 'nearest',
)
test_datagen = ImageDataGenerator(
rescale = 1/255,
)
batch_size = 32
train_generator = train_datagen.flow_from_directory(
'image/train',
target_size=(150,150),
batch_size=batch_size,
)
test_generator = test_datagen.flow_from_directory(
'image/test',
target_size=(150,150),
batch_size=batch_size,
)
train_generator.class_indices
model.compile(optimizer=SGD(lr=1e-4,momentum=0.9),loss='categorical_crossentropy',metrics=['accuracy'])
model.fit_generator(train_generator,steps_per_epoch=len(train_generator),epochs=20,validation_data=test_generator,validation_steps=len(test_generator))
model.save('model_vgg16.h5')
from keras.models import load_model
import numpy as np
label = np.array(['cat','dog'])
model = load_model('model_vgg16.h5')
image = load_img('image/test/cat/cat.1003.jpg')
image
image = image.resize((150,150))
image = img_to_array(image)
image = image/255
image = np.expand_dims(image,0)
image.shape
print(label[model.predict_classes(image)])