概述
文章目录
- 1.数据处理
- 1)数据shuffle
- 2)TFRecord
- 3)TextLineDataset方式
- 4)from_generator方法
- 2.建模方法
- 例子
- 3.Baseline构建
- 数据处理
- 数据读取
1.数据处理
1)数据shuffle
2)TFRecord
3)TextLineDataset方式
4)from_generator方法
2.建模方法
例子
3.Baseline构建
数据处理
import json
import os
import datetime as dt
from tqdm import tqdm
import pandas as pd
import numpy as np
# label
def f2cat(filename: str) -> str:
return filename.split('.')[0]
# 读取文件,label变成list
class Simplified():
def __init__(self, input_path='./input'):
self.input_path = input_path
def list_all_categories(self):
files = os.listdir(self.input_path)
return sorted([f2cat(f) for f in files], key=str.lower)
def read_training_csv(self, category, nrows=None, usecols=None, drawing_transform=False):
df = pd.read_csv(os.path.join(self.input_path, category + '.csv'),
nrows=nrows, parse_dates=['timestamp'], usecols=usecols)
if drawing_transform:
df['drawing'] = df['drawing'].apply(json.loads)
return df
start = dt.datetime.now()
# 加载数据路径
s = Simplified('./data/')
NCSVS = 100
# 加载类别
categories = s.list_all_categories()
print(len(categories))
# tqdm显示进度
for y, cat in tqdm(enumerate(categories)):
df = s.read_training_csv(cat)
df['y'] = y
# 构建cv,知道数据属于哪个文件
df['cv'] = (df.key_id // 10 ** 7) % NCSVS # 100
# 将csv文件的1/100的数据写到里面,for循环
for k in range(NCSVS):
filename = './shuffle_data/train_k{}.csv'.format(k)
chunk = df[df.cv == k]
chunk = chunk.drop(['key_id'], axis=1)
if y == 0:
chunk.to_csv(filename, index=False)
else:
chunk.to_csv(filename, mode='a', header=False, index=False)
#对csv数据打乱,进行压缩
for k in tqdm(range(NCSVS)):
filename = './shuffle_data/train_k{}.csv'.format(k)
if os.path.exists(filename):
df = pd.read_csv(filename)
df['rnd'] = np.random.rand(len(df))
df = df.sort_values(by='rnd').drop('rnd', axis=1)
df.to_csv('./shuffle_data_gzip/train_k{}.csv.gz'.format(k), compression='gzip', index=False)
os.remove(filename)
print(df.shape)
end = dt.datetime.now()
print('Latest run {}.nTotal time {}s'.format(end, (end - start).seconds))
数据读取
import glob
import tensorflow as tf
import numpy as np
import pandas as pd
import os
import cv2
import json
import matplotlib.pyplot as plt
os.environ['CUDA_VISIBLE_DEVICES']='1'
# 每一个csv.gz文件读取 -> y已经ok ->drawing 如何处理
# 单通道-黑白照片
def draw_cv2(raw_strokes, size=256, lw=6, time_color=True):
img = np.zeros((BASE_SIZE, BASE_SIZE), np.uint8) #画板
for t, stroke in enumerate(raw_strokes):
for i in range(len(stroke[0]) - 1):
color = 255 - min(t, 10) * 13 if time_color else 255
_ = cv2.line(img, (stroke[0][i], stroke[1][i]),
(stroke[0][i + 1], stroke[1][i + 1]), color, lw)
if size != BASE_SIZE:
return cv2.resize(img, (size, size))
else:
return img
# RGB图片
def draw_cv2(raw_strokes, size=256, lw=6):
img = np.zeros((BASE_SIZE, BASE_SIZE, 3), np.uint8)
for t, stroke in enumerate(raw_strokes):
points_count = len(stroke[0]) - 1
grad = 255//points_count
for i in range(len(stroke[0]) - 1):
_ = cv2.line(img, (stroke[0][i], stroke[1][i]), (stroke[0][i + 1], stroke[1][i + 1]), (255, 255 - min(t,10)*13, max(255 - grad*i, 20)), lw)
if size != BASE_SIZE:
img = cv2.resize(img, (size, size))
return img
class DataLoader(object):
def __init__(self, resize_height=64, resize_width=64, batch_size=512, fileList=None, size=256, lw=6):
self.resize_height = resize_height #图片高
self.resize_height = resize_height #图片宽
self.batch_size = batch_size #batch
self.fileList = fileList #文件数据
self.size = size #画图时图片大小
self.lw = lw
def __call__(self):
def _generator(size,lw):
while True: #
for filename in np.random.permutation(self.fileList):
df = pd.read_csv(filename)
df['drawing'] = df['drawing'].apply(json.loads)
x = np.zeros((len(df), size, size))
for i, raw_strokes in enumerate(df.drawing.values):
x[i] = draw_cv2(raw_strokes, size=size, lw=lw)
x = x / 255.
x = x.reshape((len(df), size, size, 1)).astype(np.float32)
y = tf.keras.utils.to_categorical(df.y, num_classes=n_labels)
for x_i,y_i in zip(x,y):
yield (x_i,y_i)
dataset = tf.data.Dataset.from_generator(generator=_generator,
output_types=(tf.dtypes.float32, tf.dtypes.int32),
output_shapes=((self.resize_height, self.resize_height, 1), (340, )),
args=(self.size, self.lw))
dataset = dataset.prefetch(buffer_size=10240)
dataset = dataset.shuffle(buffer_size=10240).batch(self.batch_size)
return dataset
DP_DIR = './shuffle_data_gzip/'
BASE_SIZE = 256
n_labels = 340
np.random.seed(seed=1987)
size = 64
batchsize = 1024
# 利用正则读取文件路径
fileList = glob.glob("./shuffle_data_gzip/*.csv.gz")
train_fileList = fileList[:-1]
val_fileList = fileList[-1:]
train_ds = DataLoader(resize_height=64, resize_width=64, batch_size=batchsize, fileList=train_fileList, size=size, lw=6)()
val_ds = DataLoader(resize_height=64, resize_width=64, batch_size=batchsize, fileList=val_fileList, size=size, lw=6)()
# 建模
inputs = tf.keras.layers.Input(shape=(size, size, 1))
# include_top不用全连接层
base_model = tf.keras.applications.MobileNet(input_shape=(size, size, 1), include_top=False, weights=None, classes=n_labels)
x = base_model(inputs)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(1024, activation='relu')(x)
predictions = tf.keras.layers.Dense(n_labels, activation='softmax')(x)
model = tf.keras.models.Model(inputs=inputs, outputs=predictions)
model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.002),
loss='categorical_crossentropy',
metrics=[tf.keras.metrics.CategoricalCrossentropy(),
tf.keras.metrics.CategoricalAccuracy(),
tf.keras.metrics.TopKCategoricalAccuracy(k=3,name='top_3_categorical_accuracy')])
# 训练
callbacks = [
tf.keras.callbacks.ReduceLROnPlateau(monitor='val_top_3_categorical_accuracy', factor=0.75, patience=3, min_delta=0.001,
mode='max', min_lr=1e-5, verbose=1),
tf.keras.callbacks.ModelCheckpoint('model_all.h5', monitor='val_top_3_categorical_accuracy', mode='max', save_best_only=True,
save_weights_only=True),
]
model.fit(
train_ds, epochs=50, verbose=1,steps_per_epoch=48500, #一个epoch用多少个step,为了快速迭代:可以设置1000。
validation_data = val_ds,
validation_steps = 400,
callbacks = callbacks
)
# 验证集测试
def apk(actual, predicted, k=3):
"""
Source: https://github.com/benhamner/Metrics/blob/master/Python/ml_metrics/average_precision.py
"""
if len(predicted) > k:
predicted = predicted[:k]
score = 0.0
num_hits = 0.0
for i, p in enumerate(predicted):
if p in actual and p not in predicted[:i]:
num_hits += 1.0
score += num_hits / (i + 1.0)
if not actual:
return 0.0
return score / min(len(actual), k)
def mapk(actual, predicted, k=3):
"""
Source: https://github.com/benhamner/Metrics/blob/master/Python/ml_metrics/average_precision.py
"""
return np.mean([apk(a, p, k) for a, p in zip(actual, predicted)])
MobileNet论文参考
from glob import glob
import cv2
import os
import tensorflow as tf
import pandas as pd
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
fileList = glob("./shuffle_data_gzip/*.csv.gz")
def draw_cv2(raw_strokes,BASE_SIZE=256, size=256, lw=6):
img = np.zeros((BASE_SIZE, BASE_SIZE), np.uint8)
for stroke in raw_strokes:
for i in range(len(stroke[0]) - 1):
_ = cv2.line(img, (stroke[0][i], stroke[1][i]), (stroke[0][i + 1], stroke[1][i + 1]), 255, lw)
if size != BASE_SIZE:
return cv2.resize(img, (size, size))
else:
return img
tfrecord_file = "./tfrecord_data/train.tfrecords"
with tf.io.TFRecordWriter(tfrecord_file) as writer:
for filename in fileList[:1]:
df = pd.read_csv(filename)
df['drawing'] = df['drawing'].apply(json.loads)
for row in range(df.shape[0]):
drawing = df.loc[row,'drawing']
img = draw_cv2(drawing,BASE_SIZE=128, size=128, lw=6)
img = img.tostring()
label = df.loc[row,'y']
# 建立 tf.train.Feature 字典
feature = {
'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img])),
# 图片是一个 Bytes 对象
'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[label]))
# 标签是一个 Int 对象
}
example = tf.train.Example(features=tf.train.Features(feature=feature)) # 通过字典建立 Example
writer.write(example.SerializeToString())
# 将Example序列化并写入 TFRecord 文件
# 读取 TFRecord 文件
raw_dataset = tf.data.TFRecordDataset(tfrecord_file)
# 定义Feature结构,告诉解码器每个Feature的类型是什么
feature_description = {
'image': tf.io.FixedLenFeature([], tf.string),
'label': tf.io.FixedLenFeature([], tf.int64),
}
# 将 TFRecord 文件中的每一个序列化的 tf.train.Example 解码
def _parse_example(example_string):
feature_dict = tf.io.parse_single_example(example_string, feature_description)
image = tf.io.decode_raw(feature_dict['image'], tf.uint8)
# 解码JPEG图片
image = tf.reshape(image, [128,128,1])
image = tf.dtypes.cast(image,tf.float32)
image = image / 255.0
label = tf.one_hot(feature_dict['label'],depth=340)
return image, label
dataset = raw_dataset.map(_parse_example)
train_ds = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE).shuffle(buffer_size=tf.data.experimental.AUTOTUNE).batch(1024)
class MobileNetModel(tf.keras.models.Model):
def __init__(self, size, n_labels, **kwargs):
super(MobileNetModel, self).__init__(**kwargs)
# 预训练模型
self.base_model = tf.keras.applications.MobileNet(input_shape=(size, size, 1), include_top=False, weights=None, classes=n_labels)
self.flatten = tf.keras.layers.Flatten()
self.dense = tf.keras.layers.Dense(1024, activation='relu')
self.outputs =
tf.keras.layers.Dense(n_labels, activation='softmax')
def call(self, inputs):
x = self.base_model(inputs)
x = self.flatten(x)
x = self.dense(x)
output_ = self.outputs(x)
return output_
model = MobileNetModel(size=128,n_labels=340)
loss_object = tf.keras.losses.CategoricalCrossentropy()
learning_rate = 0.001
optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
train_loss = tf.keras.metrics.Mean(name='train_loss')
train_accuracy = tf.keras.metrics.CategoricalAccuracy(name='train_accuracy')
train_top3_accuracy = tf.keras.metrics.TopKCategoricalAccuracy(k=3,name='train_top_3_categorical_accuracy')
test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.CategoricalAccuracy(name='test_accuracy')
test_top3_accuracy = tf.keras.metrics.TopKCategoricalAccuracy(k=3,name='test_top_3_categorical_accuracy')
# @tf.function
def train_one_step(images, labels):
with tf.GradientTape() as tape:
predictions = model(images)
loss = loss_object(labels, predictions)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
train_loss(loss)
train_accuracy(labels, predictions)
train_top3_accuracy(labels, predictions)
def val_one_step(images, labels):
predictions = model(images)
t_loss = loss_object(labels, predictions)
test_loss(t_loss)
test_accuracy(labels, predictions)
test_top3_accuracy(labels, predictions)
EPOCHS=10
for epoch in range(EPOCHS):
# 在下一个epoch开始时,重置评估指标
train_loss.reset_states()
train_accuracy.reset_states()
train_top3_accuracy.reset_states()
test_loss.reset_states()
test_accuracy.reset_states()
test_top3_accuracy.reset_states()
for step,(images, labels) in enumerate(train_ds):
train_one_step(images, labels)
if step % 200 == 0:
print("step:{0}; Samples:{1}; Train Loss:{2}; Train Accuracy:{3},Train Top3 Accuracy:{4}".format(step, (step + 1) * 1024,
train_loss.result(),
train_accuracy.result()*100,
train_top3_accuracy.result()*100))
template = 'Epoch {}, Loss: {}, Accuracy: {}, Test Loss: {}, Test Accuracy{}'
print(template.format(epoch + 1,
train_loss.result(),
train_accuracy.result() * 100,
train_top3_accuracy()*100,
test_loss.result(),
test_accuracy.result() * 100,
test_top3_accuracy()*100
))
最后
以上就是可耐纸鹤为你收集整理的深度学习15-Google涂鸦识别挑战项目(下)的全部内容,希望文章能够帮你解决深度学习15-Google涂鸦识别挑战项目(下)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复