作者 | 小白
来源 | 小白学视觉
importcv2
import os
capture=cv2.VideoCapture(0)
directory = "Bharath/"
path=os.listdir(directory)
count = 0
while True:
ret, frame = capture.read()
cv2.imshow('Frame', frame)
key = cv2.waitKey(1)
if key%256 == 32:
img_path = directory + str(count) + ".jpeg"
cv2.imwrite(img_path, frame)
count += 1
elif key%256 == 113:
break
capture.release()
cv2.destroyAllWindows()
当我们按键盘上的空格键时单击图片。
按下“q”时退出程序。
import cv2
import os
directory = "Bharath/"
path = os.listdir(directory)
for i in path:
img_path = directory + i
image = cv2.imread(img_path)
image = cv2.resize(image, (224, 224))
cv2.imwrite(img_path, image)
train_datagen = ImageDataGenerator(rescale=1./255,
rotation_range=30,
shear_range=0.3,
zoom_range=0.3,
width_shift_range=0.4,
height_shift_range=0.4,
horizontal_flip=True,
fill_mode='nearest')
train_generator = train_datagen.flow_from_directory(directory,
target_size=(Img_Height, Img_width),
batch_size=batch_size,
class_mode='categorical',
shuffle=True)
VGG16_MODEL=VGG16(input_shape=(Img_width,Img_Height,3),include_top=False,weights='imagenet')
for layers in VGG16_MODEL.layers:
layers.trainable=False
for layers in VGG16_MODEL.layers:
print(layers.trainable)
# Input layer
input_layer = VGG16_MODEL.output
# Convolutional Layer
Conv1 = Conv2D(filters=32, kernel_size=(3,3), strides=(1,1), padding='valid',
data_format='channels_last', activation='relu',
kernel_initializer=keras.initializers.he_normal(seed=0),
name='Conv1')(input_layer)
# MaxPool Layer
Pool1 = MaxPool2D(pool_size=(2,2),strides=(2,2),padding='valid',
data_format='channels_last',name='Pool1')(Conv1)
# Flatten
flatten = Flatten(data_format='channels_last',name='Flatten')(Pool1)
# Fully Connected layer-1
FC1 = Dense(units=30, activation='relu',
kernel_initializer=keras.initializers.glorot_normal(seed=32),
name='FC1')(flatten)
# Fully Connected layer-2
FC2 = Dense(units=30, activation='relu',
kernel_initializer=keras.initializers.glorot_normal(seed=33),
name='FC2')(FC1)
# Output layer
Out = Dense(units=num_classes, activation='softmax',
kernel_initializer=keras.initializers.glorot_normal(seed=3),
name='Output')(FC2)
model1 = Model(inputs=VGG16_MODEL.input,outputs=Out)
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.callbacks import TensorBoard
checkpoint = ModelCheckpoint("face_rec.h5", monitor='accuracy', verbose=1,
save_best_only=True, mode='auto', period=1)
reduce = ReduceLROnPlateau(monitor='accuracy', factor=0.2, patience=3, min_lr=0.00001, verbose = 1)
logdir='logsface'
tensorboard_Visualization = TensorBoard(log_dir=logdir, histogram_freq=True)
ModelCheckpoint — 此回调用于存储训练后模型的权重。我们通过指定 save_best_only=True 只保存模型的最佳权重。
ReduceLROnPlateau — 此回调用于在指定的epoch数后降低优化器的学习率。在这里,我们将耐心指定为 10。如果在 10 个 epoch 后准确率没有提高,那么我们的学习率就会相应地降低 0.2 倍。
Tensorboard — tensorboard 回调用于绘制图形的可视化,即精度和损失的图形。
model1.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=0.001),
metrics=['accuracy'])
epochs = 20
model1.fit(train_generator,
epochs = epochs,
callbacks = [checkpoint, reduce, tensorboard_Visualization])
分享
点收藏
点点赞
点在看
文章转发自AI科技大本营微信公众号,版权归其所有。文章内容不代表本站立场和任何投资暗示。
Copyright © 2021.Company 元宇宙YITB.COM All rights reserved.元宇宙YITB.COM