当前位置:网站首页>Tf.keras build neural network function expansion
Tf.keras build neural network function expansion
2022-07-28 06:14:00 【Jiyu Wangchuan】
tf.keras Build neural network function expansion
One 、 Homemade data sets , Solve the application problems in this field
1.1 Observation data set data structure , Match as a feature tag pair
mnist_image_label Folder :
Picture folder :
Label folder :
def generateds(path, txt):
f = open(txt, 'r') # Open as read-only txt file
contents = f.readlines() # Read all lines in the file
f.close() # close txt file
x, y_ = [], [] # Create an empty list
for content in contents: # Line by line
value = content.split() # Separate... By spaces , The picture path is value[0] , The label is value[1] , Save list
img_path = path + value[0] # Spell out the picture path and file name
img = Image.open(img_path) # Read in the picture
img = np.array(img.convert('L')) # The picture becomes 8 Bit width gray value np.array Format
img = img / 255. # Data normalization ( Implement preprocessing )
x.append(img) # Normalized data , Paste to list x
y_.append(value[1]) # Label to list y_
print('loading : ' + content) # Print status prompt
x = np.array(x) # Turn into np.array Format
y_ = np.array(y_) # Turn into np.array Format
y_ = y_.astype(np.int64) # Turn into 64 An integer
return x, y_ # Returns the input feature x, Return label y_
print('-------------Generate Datasets-----------------')
x_train, y_train = generateds(train_path, train_txt)
x_test, y_test = generateds(test_path, test_txt)
Two 、 Data to enhance , Increase the amount of data
2.1 Data to enhance
image_gen_train=tf.keras.preprocessing.image.ImageDataGenerator( Enhancement method )
image_gen_train.fit(x_train)
Common enhancement methods :
- Scaling factor :rescale= All data will be multiplied by the value provided
- Random rotation :rotation_range= Range of degrees of random rotation angle
- Width offset :width_shift_range= Random width offset
- Height offset :height_shift_range= Random height offset
- Flip horizontal :horizontal_flip= Whether to flip horizontally and randomly
- Random scaling :zoom_range= Range of random scaling [1-n,1+n]
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1) # Add a dimension to the data , from (60000, 28, 28)reshape by (60000, 28, 28, 1)
image_gen_train = ImageDataGenerator(
rescale=1. / 1., # If it is an image , The denominator is 255 when , Attributable to 0~1
rotation_range=45, # Random 45 Degree of rotation
width_shift_range=.15, # Width offset
height_shift_range=.15, # Height offset
horizontal_flip=False, # Flip horizontal
zoom_range=0.5 # Randomly scale the image by the threshold amount 50%
)
image_gen_train.fit(x_train)
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
model.fit(image_gen_train.flow(x_train, y_train, batch_size=32), epochs=5, validation_data=(x_test, y_test),
validation_freq=1)
model.summary()
2.2 Data enhanced visualization


3、 ... and 、 Break point continued training , Access model
3.1 Read the model
load_weights( Pathfilename )
checkpoint_save_path = "./checkpoint/mnist.ckpt"
if os.path.exists(checkpoint_save_path + '.index'):
print('-------------load the model-----------------')
model.load_weights(checkpoint_save_path)
3.2 Save the model
With the help of tensorflow The callback function given , Save parameters and network directly
tf.keras.callbacks.ModelCheckpoint(
filepath= Pathfilename ,
save_weights_only=True,
monitor='val_loss', # val_loss or loss
save_best_only=True)
history = model.fit(x_train, y_train, batch_size=32, epochs=5,
validation_data=(x_test, y_test), validation_freq=1,
callbacks=[cp_callback])
)
import tensorflow as tf
import os
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
checkpoint_save_path = "./checkpoint/mnist.ckpt"
if os.path.exists(checkpoint_save_path + '.index'):
print('-------------load the model-----------------')
model.load_weights(checkpoint_save_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,
save_weights_only=True,
save_best_only=True)
history = model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1,
callbacks=[cp_callback])
model.summary()
Four 、 Parameter extraction , Write to text
4.1 Extract trainable parameters
model.trainable_variables Trainable parameters in the model
4.2 Set up print Output format
np.set_printoptions(precision= The decimal point shall be rounded to several places ,threshold= The number of array elements is less than or equal to the threshold , Print all elements ; Otherwise, print the threshold +1 Elements , Use an ellipsis in the middle to supplement )
>>> np.set_printoptions(precision=5)
>>> print(np.array([1.123456789]))
[1.12346]
>>> np.set_printoptions(threshold=5)
>>> print(np.arange(10))
[0 1 2 … , 7 8 9]
notes :precision=np.inf Print full decimal places ;threshold=np.nan Print all array elements .
import tensorflow as tf
import os
import numpy as np
np.set_printoptions(threshold=np.inf)
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
checkpoint_save_path = "./checkpoint/mnist.ckpt"
if os.path.exists(checkpoint_save_path + '.index'):
print('-------------load the model-----------------')
model.load_weights(checkpoint_save_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,
save_weights_only=True,
save_best_only=True)
history = model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1,
callbacks=[cp_callback])
model.summary()
print(model.trainable_variables)
file = open('./weights.txt', 'w')
for v in model.trainable_variables:
file.write(str(v.name) + '\n')
file.write(str(v.shape) + '\n')
file.write(str(v.numpy()) + '\n')
file.close()
weights.txt:
5、 ... and 、acc/loss visualization , See how your workout works
history=model.fit( Training set data , Training set label , batch_size=, epochs=,
validation_split= Proportion used as test data ,validation_data= Test set , validation_freq= Test frequency )
history:
loss: Training set loss
val_loss: Test set loss
sparse_categorical_accuracy: Training set accuracy
val_sparse_categorical_accuracy: Test set accuracy
acc = history.history['sparse_categorical_accuracy']
val_acc = history.history['val_sparse_categorical_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.subplot(1, 2, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.title('Training and Validation Loss')
plt.legend()
plt.show()

6、 ... and 、 Applications , Figure identification

from PIL import Image
import numpy as np
import tensorflow as tf
model_save_path = './checkpoint/mnist.ckpt'
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')])
model.load_weights(model_save_path)
preNum = int(input("input the number of test pictures:"))
for i in range(preNum):
image_path = input("the path of test picture:")
img = Image.open(image_path)
img = img.resize((28, 28), Image.ANTIALIAS)
img_arr = np.array(img.convert('L'))
for i in range(28):
for j in range(28):
if img_arr[i][j] < 200:
img_arr[i][j] = 255
else:
img_arr[i][j] = 0
img_arr = img_arr / 255.0
x_predict = img_arr[tf.newaxis, ...]
result = model.predict(x_predict)
pred = tf.argmax(result, axis=1)
print('\n')
tf.print(pred)
边栏推荐
- 将项目部署到GPU上,并且运行
- SQLAlchemy使用相关
- The business of digital collections is not so easy to do
- Deep learning (incremental learning) -- iccv2021:ss-il: separated softmax for incremental learning
- 神经网络实现鸢尾花分类
- Word2vec+ regression model to achieve classification tasks
- Deep learning - metaformer is actually what you need for vision
- Uview upload component upload upload auto upload mode image compression
- 《On Low-Resolution Face Recognition in the Wild:Comparisons and New Techniques》低分辨率人脸识别论文解读
- 五、视频处理与GStreamer
猜你喜欢

微信小程序开发详细步骤是什么?

基于tensorflow搭建神经网络

Using neural network to predict the weather

机器学习之聚类

Protecting Against DNN Model Stealing Attacks 论文阅读心得

What should we pay attention to when making template application of wechat applet?

Improved knowledge distillation for training fast lr_fr for fast low resolution face recognition model training

深度学习——Pay Attention to MLPs

神经网络学习

使用神经网络实现对天气的预测
随机推荐
Applet development
tensorboard可视化
深度学习(自监督:MoCo V3):An Empirical Study of Training Self-Supervised Vision Transformers
深度学习——Patches Are All You Need
Four perspectives to teach you to choose applet development tools?
Protecting Against DNN Model Stealing Attacks 论文阅读心得
Interviewer: let you design a set of image loading framework. How would you design it?
Sqlalchemy usage related
Reinforcement learning - incomplete observation problem, MCTs
卷积神经网络
How to improve the efficiency of small program development?
深度学习(二)走进机器学习与深度学习编程部分
Automatic scheduled backup of remote MySQL scripts
Wechat applet development and production should pay attention to these key aspects
更新包与已安装应用签名不一致
利用辅助未标记数据增强无约束人脸识别《Boosting Unconstrained Face Recognition with Auxiliary Unlabeled Data》
强化学习——基础概念
First meet flask
pytorch深度学习单卡训练和多卡训练
Reinforcement learning -- SARS in value learning